def test_intervals_intersection(self): nullx = NullInterval(x) # All nulls assert nullx.intersection(nullx) == nullx nully = NullInterval(y) ix = Interval(x, -2, 2) iy = Interval(y, -2, 2) # Mixed nulls and defined assert nullx.intersection(ix) == nullx assert nullx.intersection(iy) == nullx assert nullx.intersection(iy) != nully assert nully.intersection(iy) == nully ix2 = Interval(x, -8, -3) ix3 = Interval(x, 3, 4) # All defined disjoint assert ix.intersection(ix2) == nullx assert ix.intersection(ix3) == nullx assert ix2.intersection(ix3) == nullx assert ix.intersection(iy) == nullx assert iy.intersection(ix) == nully ix4 = Interval(x, 1, 4) ix5 = Interval(x, -3, 0) # All defined overlapping assert ix.intersection(ix4) == Interval(x, 1, 2) assert ix.intersection(ix5) == Interval(x, -2, 0)
def test_intervals_subtract(self): nullx = NullInterval(x) # All nulls assert nullx.subtract(nullx) == nullx ix = Interval(x, 2, -2) # Mixed nulls and defined on the same dimension assert nullx.subtract(ix) == nullx assert ix.subtract(ix) == Interval(x, 0, 0) assert ix.subtract(nullx) == ix ix2 = Interval(x, 4, -4) ix3 = Interval(x, 6, -6) # All defined same dimension assert ix2.subtract(ix) == ix assert ix.subtract(ix2) == Interval(x, -2, 2) assert ix3.subtract(ix) == ix2
def test_intervals_merge(self): nullx = NullInterval(x) # All nulls assert nullx.merge(nullx) == nullx ix = Interval(x, -2, 2) # Mixed nulls and defined on the same dimension assert nullx.merge(ix) == ix assert ix.merge(ix) == ix assert ix.merge(nullx) == ix ix2 = Interval(x, 1, 4) ix3 = Interval(x, -3, 6) # All defined overlapping assert ix.merge(ix2) == Interval(x, -2, 4) assert ix.merge(ix3) == ix3 assert ix2.merge(ix3) == ix3 ix4 = Interval(x, 0, 0) ix5 = Interval(x, -1, -1) ix6 = Interval(x, 9, 11) # Non-overlapping assert ix.merge(ix4) == Interval(x, -2, 2) assert ix.merge(ix5) == Interval(x, -2, 2) assert ix4.merge(ix5) == Interval(x, -1, 0) assert ix.merge(ix6) == Interval(x, -2, 11) assert ix6.merge(ix) == Interval(x, -2, 11) assert ix5.merge(ix6) == Interval(x, -1, 11)
def test_intervals_union(self): nullx = NullInterval(x) # All nulls assert nullx.union(nullx) == nullx ix = Interval(x, -2, 2) # Mixed nulls and defined on the same dimension assert nullx.union(ix) == ix assert ix.union(ix) == ix assert ix.union(nullx) == ix ix2 = Interval(x, 1, 4) ix3 = Interval(x, -3, 6) # All defined overlapping assert ix.union(ix2) == Interval(x, -2, 4) assert ix.union(ix3) == ix3 assert ix2.union(ix3) == ix3 ix4 = Interval(x, 4, 8) ix5 = Interval(x, -3, -3) ix6 = Interval(x, -10, -3) nully = NullInterval(y) iy = Interval(y, -2, 2) # Mixed disjoint (note: IntervalGroup input order is relevant) assert ix.union(ix4) == IntervalGroup([ix, ix4]) assert ix.union(ix5) == Interval(x, -3, 2) assert ix6.union(ix) == IntervalGroup([ix6, ix]) assert ix.union(nully) == IntervalGroup([ix, nully]) assert ix.union(iy) == IntervalGroup([ix, iy]) assert iy.union(ix) == IntervalGroup([iy, ix])
def test_intervals_subtract(self, x, y): nullx = NullInterval(x) # All nulls assert nullx.subtract(nullx) == nullx ix = Interval(x, 2, -2) # Mixed nulls and defined on the same dimension assert nullx.subtract(ix) == nullx assert ix.subtract(ix) == Interval(x, 0, 0) assert ix.subtract(nullx) == ix ix2 = Interval(x, 4, -4) ix3 = Interval(x, 6, -6) # All defined same dimension assert ix2.subtract(ix) == ix assert ix.subtract(ix2) == Interval(x, -2, 2) assert ix3.subtract(ix) == ix2 c = Constant(name='c') ix4 = Interval(x, c + 2, c + 4) ix5 = Interval(x, c + 1, c + 5) # All defined symbolic assert ix4.subtract(ix5) == Interval(x, 1, -1) assert ix5.subtract(ix4) == Interval(x, -1, 1) assert ix5.subtract(ix) == Interval(x, c - 1, c + 7)
def test_intervals_union(self, x, y): nullx = NullInterval(x) # All nulls assert nullx.union(nullx) == nullx ix = Interval(x, -2, 2) # Mixed nulls and defined assert nullx.union(ix) == ix assert ix.union(ix) == ix assert ix.union(nullx) == ix ix2 = Interval(x, 1, 4) ix3 = Interval(x, -3, 6) assert ix.union(ix2) == Interval(x, -2, 4) assert ix.union(ix3) == ix3 assert ix2.union(ix3) == ix3 ix4 = Interval(x, 4, 8) ix5 = Interval(x, -3, -3) ix6 = Interval(x, -10, -3) nully = NullInterval(y) iy = Interval(y, -2, 2) assert ix.union(ix4) == Interval(x, -2, 8) assert ix.union(ix5) == Interval(x, -3, 2) assert ix6.union(ix) == Interval(x, -10, 2) # The union of non-compatible Intervals isn't possible, and an exception # is expected ixs1 = Interval(x, -2, 2, stamp=1) for i, j in [(ix, nully), (ix, iy), (iy, ix), (ix, ixs1), (ixs1, ix)]: try: i.union(j) assert False # Shouldn't arrive here except ValueError: assert True except: # No other types of exception expected assert False # Mixed symbolic and non-symbolic c = Constant(name='c') ix7 = Interval(x, c, c + 4) ix8 = Interval(x, c - 1, c + 5) assert ix7.union(ix8) == Interval(x, c - 1, c + 5) assert ix8.union(ix7) == Interval(x, c - 1, c + 5) # Symbolic with properties s = Scalar(name='s', nonnegative=True) ix9 = Interval(x, s - 2, s + 2) ix10 = Interval(x, s - 1, s + 1) assert ix.union(ix9) == Interval(x, -2, s + 2) assert ix9.union(ix) == Interval(x, -2, s + 2) assert ix9.union(ix10) == ix9 assert ix10.union(ix9) == ix9
def test_intervals_intersection(self, x, y): nullx = NullInterval(x) # All nulls assert nullx.intersection(nullx) == nullx nully = NullInterval(y) ix = Interval(x, -2, 2) iy = Interval(y, -2, 2) # Mixed nulls and defined assert nullx.intersection(ix) == nullx assert nullx.intersection(iy) == nullx assert nullx.intersection(iy) != nully assert nully.intersection(iy) == nully ix2 = Interval(x, -8, -3) ix3 = Interval(x, 3, 4) assert ix.intersection(ix2) == Interval(x, -2, -3) assert ix.intersection(ix3) == Interval(x, 3, 2) assert ix2.intersection(ix3) == Interval(x, 3, -3) assert ix.intersection(iy) == nullx assert iy.intersection(ix) == nully ix4 = Interval(x, 1, 4) ix5 = Interval(x, -3, 0) assert ix.intersection(ix4) == Interval(x, 1, 2) assert ix.intersection(ix5) == Interval(x, -2, 0) # Mixed symbolic and non-symbolic c = Constant(name='c') ix6 = Interval(x, c, c + 4) ix7 = Interval(x, c - 1, c + 5) assert ix6.intersection(ix7) == Interval(x, c, c + 4) assert ix7.intersection(ix6) == Interval(x, c, c + 4) # Symbolic with properties s = Scalar(name='s', nonnegative=True) ix8 = Interval(x, s - 2, s + 2) ix9 = Interval(x, s - 1, s + 1) assert ix.intersection(ix8) == Interval(x, s - 2, 2) assert ix8.intersection(ix) == Interval(x, s - 2, 2) assert ix8.intersection(ix9) == Interval(x, s - 1, s + 1) assert ix9.intersection(ix8) == Interval(x, s - 1, s + 1)