def test_interval_issubset_interval_union(): """ * Neither of both is empty. * i2 is a subset of i1 * i2 is an intervalUnion """ i1 = Interval(6, 7) i2 = IntervalUnion([[2, 3], [5, 7]]) assert i1.issubset(i2)
def test_interval_union_union_interval(): iu1 = IntervalUnion([[0, 1], [2, 3], [4, 5]]) iu2 = Interval(6, 7) iu3 = iu1.union(iu2) assert iu3.intervals == [ Interval(0, 1), Interval(2, 3), Interval(4, 5), Interval(6, 7), ]
def test_interval_union_union_2(): iu1 = IntervalUnion([[0, 1], [2, 3], [4, 5]]) iu2 = IntervalUnion([[-2, -1], [3.5, 3.7]]) iu3 = iu1.union(iu2) assert iu3.intervals == [ Interval(-2, -1), Interval(0, 1), Interval(2, 3), Interval(3.5, 3.7), Interval(4, 5), ]
def _get_straight_line_intersection(x, other_y1, other_y2, self_y1, self_y2): """Get the intersection point of two straight vertical lines.""" self_y = Interval(left=min(self_y1, self_y2), right=max(self_y1, self_y2)) other_y = Interval(left=min(other_y1, other_y2), right=max(other_y1, other_y2)) intersection = self_y.intersection(other_y) if intersection.left == intersection.right: return Point(x, intersection.left) else: return LineSegment(Point(x, intersection.left), Point(x, intersection.right))
def test_impossible_union(): class Impossible: def __init__(self): self.left = -1 self.right = float("nan") def is_empty(self): return False with pytest.raises(NotImplementedError) as exinfo: interval = Interval(0, 1) other = Impossible() interval.union(other) assert str(exinfo.value).startswith("Can't merge [0, 1] and ")
def test_interval_invalid_issubset(): class Impossible: def __init__(self): self.left = -1 self.right = float("nan") def is_empty(self): return False interval = Interval(0, 1) other = Impossible() with pytest.raises(RuntimeError) as excinfo: interval.issubset(other) assert "XX" not in str(excinfo)
def test_impossible_issubset(): class Impossible: def __init__(self): self.left = -1 self.right = float("nan") def is_empty(self): return False interval = Interval(0, 1) other = Impossible() with pytest.raises(RuntimeError) as exinfo: interval.issubset(other) msg = "issubset is only defined on Interval and IntervalUnion, but" assert str(exinfo.value).startswith(msg)
def test_interval_union_intersection_1(): iu1 = IntervalUnion([[0, 100]]) iu2 = IntervalUnion([[1, 2]]) iu3 = iu1.intersection(iu2) iu4 = iu1 & iu2 assert iu3.intervals == [Interval(1, 2)] assert iu3 == iu4
def test_interval_creation_successes(): empty = Interval() assert str(empty) == "[]" assert repr(empty) == "Interval()" a = Interval(0, 1) assert str(a) == "[0, 1]" b = Interval(0.0, 1.0) assert str(b) == "[0.0, 1.0]" c = Interval(datetime(2000, 1, 1), datetime(2000, 1, 2)) assert str(c) == "[2000-01-01 00:00:00, 2000-01-02 00:00:00]" d = Interval(None, None) assert str(d) == "[]" e = Interval() assert str(e) == "[]"
def test_interval_or(integers): a, b, c, d = integers i1 = Interval(a, c) i2 = Interval(b, d) i3 = Interval(a, d) assert i1 | i2 == i3
def test_interval_and(integers): a, b, c, d = integers i1 = Interval(a, c) i2 = Interval(b, d) i3 = Interval(b, c) assert i1 & i2 == i3
def test_interval_union_equality(): iu1 = IntervalUnion([[0, 10]]) iu2 = Interval(0, 10) assert iu1 == iu2
def test_interval_union_issubset_not(): iu = IntervalUnion([[0, 10], [20, 30], [40, 50]]) interval = Interval(12, 14) assert not iu.issubset(interval) assert not interval.issubset(iu)
def test_interval_union_intersection(): iu1 = IntervalUnion([[0, 10], [20, 30], [40, 50]]) iu2 = IntervalUnion([[-1, 60]]) iu3 = iu1.intersection(iu2) expected_intervals = [Interval(0, 10), Interval(20, 30), Interval(40, 50)] assert iu3.intervals == expected_intervals
def test_interval_issubset_error(): with pytest.raises(Exception) as exinfo: Interval(0, 1).issubset([0, 1]) assert str(exinfo.value) == "'list' object has no attribute 'is_empty'"
def test_interval_intersection_non_interval(): i1 = Interval(0, 10) iu1 = IntervalUnion([[0, 1], [2, 3]]) i1.intersection(iu1) == iu1
def test_interval_creation_fail_left_bigger(): with pytest.raises(RuntimeError) as exinfo: Interval(1, -1) assert str(exinfo.value) == "left may not be bigger than right"
def test_interval_union(): i01 = Interval(0, 1) assert Interval(None, None).union(Interval(42, 1337)) == Interval(42, 1337) assert Interval(42, 1337).union(Interval(None, None)) == Interval(42, 1337) assert i01.union(Interval(1, 3)) == Interval(0, 3) assert i01.union(Interval(2, 4)) == IntervalUnion([[0, 1], [2, 4]]) assert i01.union(Interval(3, 5)) == IntervalUnion([[0, 1], [3, 5]]) assert i01.union(Interval(-1, 6)) == Interval(-1, 6) assert Interval(1, 3).union(Interval(2, 4)) == Interval(1, 4) assert Interval(1, 3).union(Interval(3, 5)) == Interval(1, 5) assert Interval(1, 3).union(Interval(-1, 6)) == Interval(-1, 6) assert Interval(2, 4).union(Interval(3, 5)) == Interval(2, 5) assert Interval(2, 4).union(Interval(-1, 6)) == Interval(-1, 6) assert Interval(3, 5).union(Interval(-1, 6)) == Interval(-1, 6)
def test_interval_union_interval_unition(): i01 = Interval(0, 1) iu = IntervalUnion([[4, 5], [6, 7]]) iu_expected = IntervalUnion([[0, 1], [4, 5], [6, 7]]) assert i01.union(iu) == iu_expected assert i01 | iu == iu_expected
def test_interval_union_union_1(): iu1 = IntervalUnion([[0, 1], [2, 3], [4, 5]]) iu2 = IntervalUnion([[1, 2], [3, 4]]) iu3 = iu1.union(iu2) assert iu3.intervals == [Interval(0, 5)]
def test_interval_union_simplification_three_step_connected(): iu = IntervalUnion([[0, 1], [2, 3], [4, 5], [1, 1337]]) iu._simplify() assert iu.intervals == [Interval(0, 1337)]
def test_interval_union_simplification_overlap_connected(): iu = IntervalUnion([[0, 3], [1, 1337]]) iu._simplify() assert iu.intervals == [Interval(0, 1337)]
def test_interval_union_simplification_disjoint(): iu = IntervalUnion([[0, 1], [42, 1337]]) iu._simplify() assert iu.intervals == [Interval(0, 1), Interval(42, 1337)]
def test_interval_str(): i1 = Interval(0, 10) assert str(i1) == "[0, 10]"
def test_interval_repr(): i1 = Interval(0, 10) assert repr(i1) == "Interval(0, 10)"
def test_interval_issubset(integer_list): a, b, c, d = integer_list assert Interval(b, c).issubset(Interval(a, d))
def test_interval_union_intersection_3(): iu1 = IntervalUnion([[0, 10], [20, 30], [40, 50]]) iu2 = IntervalUnion([[10, 20]]) iu3 = iu1.intersection(iu2) assert iu3.intervals == [Interval(10, 10), Interval(20, 20)]
def test_interval_issubset_not(integer_list): a, b, c, d = integer_list assert not Interval(a, b).issubset(Interval(c, d)) or c <= a <= b <= d assert not Interval(a, c).issubset(Interval(b, d)) or b <= a <= c <= d assert not Interval(c, d).issubset(Interval(a, b)) or a <= c <= d <= b assert not Interval(b, d).issubset(Interval(a, c)) or a <= b <= d <= c
def test_interval_union_intersection_2(): iu1 = IntervalUnion([[0, 10], [20, 30], [40, 50]]) iu2 = IntervalUnion([[5, 25], [45, 60]]) iu3 = iu1.intersection(iu2) iu_expected = [Interval(5, 10), Interval(20, 25), Interval(45, 50)] assert iu3.intervals == iu_expected
def test_interval_intersection(): i42 = Interval(42, 1337) assert Interval(None, None).intersection(i42) == Interval(None, None) assert i42.intersection(Interval(None, None)) == Interval(None, None) assert Interval(0, 1).intersection(Interval(1, 3)) == Interval(1, 1) assert Interval(0, 1).intersection(Interval(2, 4)) == Interval(None, None) assert Interval(0, 1).intersection(Interval(3, 5)) == Interval(None, None) assert Interval(0, 1).intersection(Interval(-1, 6)) == Interval(0, 1) assert Interval(1, 3).intersection(Interval(2, 4)) == Interval(2, 3) assert Interval(1, 3).intersection(Interval(3, 5)) == Interval(3, 3) assert Interval(1, 3).intersection(Interval(-1, 6)) == Interval(1, 3) assert Interval(2, 4).intersection(Interval(3, 5)) == Interval(3, 4) assert Interval(2, 4).intersection(Interval(-1, 6)) == Interval(2, 4) assert Interval(3, 5).intersection(Interval(-1, 6)) == Interval(3, 5) assert Interval(1, 10).intersection(Interval(-1, 12)) == Interval(1, 10)