Skip to content

Commit

Permalink
implement le for UnitRange
Browse files Browse the repository at this point in the history
  • Loading branch information
havogt committed Nov 17, 2023
1 parent e9893be commit 7bc0689
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/gt4py/next/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,16 @@ def __and__(self, other: Set[int]) -> UnitRange:
else:
raise NotImplementedError("Can only find the intersection between UnitRange instances.")

def __le__(self, other: Set[Any]):
def __le__(self, other: Set[int]):
if isinstance(other, UnitRange):
return self.start >= other.start and self.stop <= other.stop
elif len(self) == Infinity.positive():
return False
else:
return Set.__le__(self, other)

__ge__ = __lt__ = __gt__ = lambda self, other: NotImplemented

def __str__(self) -> str:
return f"({self.start}:{self.stop})"

Expand Down
16 changes: 16 additions & 0 deletions tests/next_tests/unit_tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# distribution for a copy of the license or check <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
import operator
from typing import Optional, Pattern

import pytest
Expand Down Expand Up @@ -150,6 +151,21 @@ def test_mixed_infinity_range():
assert len(mixed_inf_range) == Infinity.positive()


@pytest.mark.parametrize(
"op, rng1, rng2, expected",
[
(operator.le, UnitRange(-1, 2), UnitRange(-2, 3), True),
(operator.le, UnitRange(-1, 2), {-1, 0, 1}, True),
(operator.le, UnitRange(-1, 2), {-1, 0}, False),
(operator.le, UnitRange(-1, 2), {-2, -1, 0, 1, 2}, True),
(operator.le, UnitRange(Infinity.negative(), 2), UnitRange(Infinity.negative(), 3), True),
(operator.le, UnitRange(Infinity.negative(), 2), {1, 2, 3}, False),
],
)
def test_range_comparison(op, rng1, rng2, expected):
assert op(rng1, rng2) == expected


@pytest.mark.parametrize(
"named_rng_like",
[
Expand Down

0 comments on commit 7bc0689

Please sign in to comment.