def test_intersects(self): a = segments.segmentlistdict({ "H1": segments.segmentlist( [segments.segment(0, 10), segments.segment(20, 30)]) }) b = segments.segmentlistdict({ "H1": segments.segmentlist([segments.segment(5, 15)]), "L1": segments.segmentlist([segments.segment(25, 35)]) }) c = segments.segmentlistdict({ "V1": segments.segmentlist( [segments.segment(7, 13), segments.segment(27, 40)]) }) assert a.intersects(b) assert b.intersects(a) assert a.intersects(a) assert not a.intersects(c) assert not b.intersects(segments.segmentlistdict({})) assert not segments.segmentlistdict({}).intersects( segments.segmentlistdict({})) assert not a.intersects_all(b) assert b.intersects_all(a) assert a.all_intersects(b) assert not b.all_intersects(a) assert not a.all_intersects_all(b)
def test_extent_all(self): a = segments.segmentlistdict({ "H1": segments.segmentlist(), "L1": segments.segmentlist([segments.segment(25, 35)]) }) assert a.extent_all() == segments.segment(25, 35)
def test_xor(self): for i in range(algebra_repeats): a = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) b = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) c = a ^ b # c contains nothing that can be found in # the intersection of a and b assert not c.intersects(a & b) # c contains nothing that cannot be found # in either a or b assert segments.segmentlist([]) == c - a - b # that c + the intersection of a and b # leaves no part of either a or b # unconvered assert segments.segmentlist([]) == a - (c | a & b) assert segments.segmentlist([]) == b - (c | a & b)
def test_pickle(self): a = segments.segmentlistdict({ "H1": segments.segmentlist( [segments.segment(0, 10), segments.segment(20, 30)]) }) a.offsets["H1"] = 10.0 assert a == pickle.loads(pickle.dumps(a, protocol=0)) assert a == pickle.loads(pickle.dumps(a, protocol=1)) assert a == pickle.loads(pickle.dumps(a, protocol=2))
def test_contract(self): assert segments.segmentlist([segments.segment(0, 20) ]) == (segments.segmentlist([ segments.segment(3, 7), segments.segment(13, 17) ]).contract(-3)) # confirm that .contract() preserves the type of the # segment objects class MyCustomSegment(segments.segment): pass class MyCustomSegmentList(segments.segmentlist): def coalesce(self): # must override for test, but don't have to # implement because test case is too simple return self assert type( MyCustomSegmentList([MyCustomSegment(0, 10) ]).contract(1)[0]) is MyCustomSegment
def test_coalesce(self): # check that mixed-type coalescing works x = segments.segmentlist( [segments.segment(1, 2), segments.segment(3, 4), (2, 3)]) try: assert x.coalesce() == segments.segmentlist( [segments.segment(1, 4)]) except AssertionError as e: raise AssertionError("mixed type coalesce failed: got %s" % str(x)) # try a bunch of random segment lists for i in range(algebra_repeats): a = verifyutils.random_uncoalesced_list( random.randint(1, algebra_listlength)) b = segments.segmentlist(a[:]).coalesce() assert verifyutils.iscoalesced(b) for seg in a: assert seg in b for seg in a: b -= segments.segmentlist([seg]) assert b == segments.segmentlist([])
def test_or(self): for i in range(algebra_repeats): a = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) b = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) c = a | b # make sure c is coalesced assert verifyutils.iscoalesced(c) # make sure c contains all of a assert a == c & a # make sure c contains all of b assert b == c & b # make sure c contains nothing except a and b assert segments.segmentlist([]) == c - a - b
def test_typesafety(self): w = "segments.segmentlist([segments.segment(0, 10), segments.segment(20, 30)])" x = "segments.segment(10, 20)" y = "[(10, 20)]" z = "None" for op in ("|", "&", "-", "^"): for arg1, arg2 in ((w, x), (x, w), (w, z), (z, w)): expr = "%s %s %s" % (arg1, op, arg2) try: eval(expr) except TypeError: pass else: raise AssertionError("%s did not raise TypeError" % expr) assert eval("%s | %s" % (w, y)) == segments.segmentlist( [segments.segment(0, 30)])
def test_extent(self): assert segments.segmentlist([(1, 0) ]).extent() == (segments.segment(0, 1))
class TestSegmentlist(object): @pytest.mark.parametrize( 'a, b', [ (segments.segmentlist( []), segments.segmentlist([]) - segments.segmentlist([])), (segments.segmentlist([]), segments.segmentlist([]) - segments.segmentlist([segments.segment(-1, 1)])), (segments.segmentlist([segments.segment(-1, 1)]) - segments.segmentlist([segments.segment(-1, 1)]), segments.segmentlist([])), (segments.segmentlist( []), segments.segmentlist([segments.segment(-1, 1)]) - segments.segmentlist([segments.segment(-1, 1)])), #(segments.segmentlist([]), # segments.segmentlist([segments.segment(0,0)]) - # segments.segmentlist([segments.segment(0,0)])), (segments.segmentlist([segments.segment( 0, 1)]), segments.segmentlist([segments.segment(0, 1)]) - segments.segmentlist([segments.segment(2, 3)])), (segments.segmentlist([segments.segment( 0, 1)]), segments.segmentlist([segments.segment(0, 1)]) - segments.segmentlist( [segments.segment(2, 3), segments.segment(4, 5)])), (segments.segmentlist([segments.segment(0, 1)]), segments.segmentlist( [segments.segment(0, 1), segments.segment(2, 3)]) - segments.segmentlist([segments.segment(2, 3)])), (segments.segmentlist([segments.segment(2, 3)]), segments.segmentlist( [segments.segment(0, 1), segments.segment(2, 3)]) - segments.segmentlist([segments.segment(0, 1)])), (segments.segmentlist( [segments.segment(0, 1), segments.segment(4, 5)]), segments.segmentlist([ segments.segment(0, 1), segments.segment(2, 3), segments.segment(4, 5) ]) - segments.segmentlist([segments.segment(2, 3)])), (segments.segmentlist([segments.segment( 0, 1)]), segments.segmentlist([segments.segment(0, 2)]) - segments.segmentlist([segments.segment(1, 2)])), (segments.segmentlist([ segments.segment(0.8, 0.9), segments.segment(1.0, 1.8) ]), segments.segmentlist([segments.segment(0, 2)]) - segments.segmentlist([ segments.segment(0, 0.8), segments.segment(0.9, 1.0), segments.segment(1.8, 2) ])), (segments.segmentlist([segments.segment( -5, 10)]), segments.segmentlist([segments.segment(-10, 10)]) - segments.segmentlist([segments.segment(-15, -5)])), (segments.segmentlist([ segments.segment(-10, -5), segments.segment(5, 10) ]), segments.segmentlist([segments.segment(-10, 10)]) - segments.segmentlist([segments.segment(-5, 5)])), (segments.segmentlist([segments.segment( -10, 5)]), segments.segmentlist([segments.segment(-10, 10)]) - segments.segmentlist([segments.segment(5, 15)])), (segments.segmentlist( [segments.segment(0, 5), segments.segment(45, 50)]), segments.segmentlist([ segments.segment(0, 10), segments.segment(20, 30), segments.segment(40, 50) ]) - segments.segmentlist([segments.segment(5, 45)])), ]) def test_sub(self, a, b): assert a == b @pytest.mark.parametrize('a, b', [ (segments.segmentlist([ segments.segment(-segments.infinity(), segments.infinity()) ]), segments.segmentlist([])), (segments.segmentlist([]), segments.segmentlist( [segments.segment(-segments.infinity(), segments.infinity())])), (segments.segmentlist([ segments.segment(-segments.infinity(), -5), segments.segment(5, segments.infinity()) ]), segments.segmentlist([segments.segment(-5, 5)])), ]) def test_invert(self, a, b): assert a == ~b def test_and(self): for i in range(algebra_repeats): a = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) b = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) c = a & b assert c == a - (a - b) assert c == b - (b - a) def test_or(self): for i in range(algebra_repeats): a = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) b = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) c = a | b # make sure c is coalesced assert verifyutils.iscoalesced(c) # make sure c contains all of a assert a == c & a # make sure c contains all of b assert b == c & b # make sure c contains nothing except a and b assert segments.segmentlist([]) == c - a - b def test_xor(self): for i in range(algebra_repeats): a = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) b = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) c = a ^ b # c contains nothing that can be found in # the intersection of a and b assert not c.intersects(a & b) # c contains nothing that cannot be found # in either a or b assert segments.segmentlist([]) == c - a - b # that c + the intersection of a and b # leaves no part of either a or b # unconvered assert segments.segmentlist([]) == a - (c | a & b) assert segments.segmentlist([]) == b - (c | a & b) def test_protract(self): assert segments.segmentlist([segments.segment(0, 20) ]) == (segments.segmentlist([ segments.segment(3, 7), segments.segment(13, 17) ]).protract(3)) # confirm that .protract() preserves the type of the # segment objects class MyCustomSegment(segments.segment): pass class MyCustomSegmentList(segments.segmentlist): def coalesce(self): # must override for test, but don't have to # implement because test case is too simple return self assert type( MyCustomSegmentList([MyCustomSegment(0, 10) ]).protract(1)[0]) is MyCustomSegment def test_contract(self): assert segments.segmentlist([segments.segment(0, 20) ]) == (segments.segmentlist([ segments.segment(3, 7), segments.segment(13, 17) ]).contract(-3)) # confirm that .contract() preserves the type of the # segment objects class MyCustomSegment(segments.segment): pass class MyCustomSegmentList(segments.segmentlist): def coalesce(self): # must override for test, but don't have to # implement because test case is too simple return self assert type( MyCustomSegmentList([MyCustomSegment(0, 10) ]).contract(1)[0]) is MyCustomSegment def test_intersects(self): for i in range(algebra_repeats): a = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) b = verifyutils.random_coalesced_list( random.randint(1, algebra_listlength)) c = a - b d = a & b if len(c): assert not c.intersects(b) if len(d): assert d.intersects(a) assert d.intersects(b) assert a.intersects(b) def test_extent(self): assert segments.segmentlist([(1, 0) ]).extent() == (segments.segment(0, 1)) def test_coalesce(self): # check that mixed-type coalescing works x = segments.segmentlist( [segments.segment(1, 2), segments.segment(3, 4), (2, 3)]) try: assert x.coalesce() == segments.segmentlist( [segments.segment(1, 4)]) except AssertionError as e: raise AssertionError("mixed type coalesce failed: got %s" % str(x)) # try a bunch of random segment lists for i in range(algebra_repeats): a = verifyutils.random_uncoalesced_list( random.randint(1, algebra_listlength)) b = segments.segmentlist(a[:]).coalesce() assert verifyutils.iscoalesced(b) for seg in a: assert seg in b for seg in a: b -= segments.segmentlist([seg]) assert b == segments.segmentlist([]) def test_typesafety(self): w = "segments.segmentlist([segments.segment(0, 10), segments.segment(20, 30)])" x = "segments.segment(10, 20)" y = "[(10, 20)]" z = "None" for op in ("|", "&", "-", "^"): for arg1, arg2 in ((w, x), (x, w), (w, z), (z, w)): expr = "%s %s %s" % (arg1, op, arg2) try: eval(expr) except TypeError: pass else: raise AssertionError("%s did not raise TypeError" % expr) assert eval("%s | %s" % (w, y)) == segments.segmentlist( [segments.segment(0, 30)])