Skip to content

Commit

Permalink
Merge pull request #19 from lgharibashvili/18-non-empty
Browse files Browse the repository at this point in the history
Overlap and normalization corrections
  • Loading branch information
runfalk committed Apr 21, 2021
2 parents 420a7bd + 757f84e commit ea6341d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ build/
dist/
*venv/
doc/_build/

.vscode/
16 changes: 12 additions & 4 deletions spans/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ def __lt__(self, other):
if self.is_lower:
if self.inc:
return other.is_lower ^ other.inc
else:
return not other.is_lower and other.inc
return False
else:
if self.inc:
return other.is_lower and not other.inc
else:
return other.is_lower or other.inc
return other.is_lower or other.inc
else:
return False

Expand Down Expand Up @@ -154,6 +152,11 @@ def __init__(self, lower=None, upper=None, lower_inc=None, upper_inc=None):

if upper is None and upper_inc:
raise ValueError("Upper bound can not be inclusive when infinite")

# Handle normalization to empty ranges
if lower is not None and lower == upper and not (lower_inc and upper_inc):
self._range = _empty_internal_range
return

self._range = _internal_range(
lower, upper, lower_inc, upper_inc, False)
Expand Down Expand Up @@ -937,6 +940,11 @@ class intrange(DiscreteRange):
def __init__(self, *args, **kwargs):
super(DiscreteRange, self).__init__(*args, **kwargs)

if self._range.empty:
# If the range has already been normalized,
# then we return here
return

# Normalize the internal range
lb = self._range.lower
if not self.lower_inf and not self._range.lower_inc:
Expand Down
18 changes: 17 additions & 1 deletion tests/test_range.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pickle
import itertools
import pytest

from spans import (
Expand Down Expand Up @@ -36,7 +37,7 @@ def test_bound_helper():
assert _Bound(1, inc=False, is_lower=False) < _Bound(1, inc=True, is_lower=True)
assert _Bound(1, inc=False, is_lower=False) < _Bound(1, inc=True, is_lower=False)

assert _Bound(1, inc=False, is_lower=True) < _Bound(1, inc=True, is_lower=False)
assert _Bound(1, inc=False, is_lower=True) > _Bound(1, inc=True, is_lower=False)

assert _Bound(1, inc=True, is_lower=False) < _Bound(1, inc=False, is_lower=True)

Expand Down Expand Up @@ -453,6 +454,7 @@ def test_overlap(a, b):
(floatrange(1.0, 5.0), floatrange(5.0, 10.0)),
(floatrange(1.0, 5.0), floatrange(5.0, 10.0, lower_inc=False)),
(floatrange(upper=5.0), floatrange(5.0)),
(floatrange(1.0, 5.0, upper_inc=True), floatrange(5.0, 10.0, lower_inc=False))
])
def test_not_overlap(a, b):
assert not a.overlap(b)
Expand Down Expand Up @@ -627,3 +629,17 @@ def test_bug11_valid_union_call_detected_as_invalid():
b = floatrange(middle, end)

assert a.union(b) == floatrange(start, end)

@pytest.mark.parametrize("value, empty", [
(floatrange(5.0, 5.0, lower_inc=True, upper_inc=True), False),
(floatrange(5.0, 5.0, lower_inc=True, upper_inc=False), True),
(floatrange(5.0, 5.0, lower_inc=True, upper_inc=False), True),
(floatrange(5.0, 5.0, lower_inc=False, upper_inc=True), True),
(floatrange(0.0, 5.0, upper_inc=True).intersection(
floatrange(5.0, lower_inc=False)), True)
])
def test_bug18_range_not_normalized_to_empty(value, empty):
"""
`Bug #18 <https://github.com/runfalk/spans/issues/18>`_
"""
assert bool(value) is not empty

0 comments on commit ea6341d

Please sign in to comment.