def test_simplify(self): """Range reduce should group overlapping ranges""" # consolidate should have no effect when no overlap r = self.two r.simplify() self.assertEqual(r.spans, [Span(3, 5), Span(8, 11)]) # should consolidate an overlap of the same direction r.spans.append(Span(-1, 4)) r.simplify() self.assertEqual(r.spans, [Span(-1, 5), Span(8, 11)]) # should also consolidate _adjacent_ spans of the same direction r.spans.append(Span(11, 14)) r.simplify() self.assertEqual(r.spans, [Span(-1, 5), Span(8, 14)]) # bridge should cause consolidations s = Range(r) s.spans.append(Span(5, 8)) s.simplify() self.assertEqual(s.spans, [Span(-1, 14)]) # ditto for bridge that overlaps everything s = Range(r) s.spans.append(Span(-100, 100)) s.simplify() self.assertEqual(s.spans, [Span(-100, 100)]) # however, can't consolidate span in other orientation s = Range(r) s.spans.append(Span(-100, 100, reverse=True)) self.assertEqual( s.spans, [Span(-1, 5), Span(8, 14), Span(-100, 100, reverse=True)])
def test_init(self): self.assertEqual(RangeFromString(""), Range()) self.assertEqual(RangeFromString(" 3 , 4\t, ,, 10 ,"), Range([3, 4, 10])) self.assertEqual(RangeFromString("3,4-10,1-5"), Range([Span(3), Span(4, 10), Span(1, 5)]))
def test_contains(self): """Range contains an item if any span contains it""" self.assertIn(50, self.one) self.assertIn(0, self.one) self.assertIn(99, self.one) self.assertNotIn(100, self.one) self.assertIn(6, self.three) self.assertNotIn(7, self.three) self.assertNotIn(8, self.three) self.assertNotIn(14, self.three) self.assertIn(15, self.three) self.assertNotIn(29, self.three) self.assertIn(30, self.three) self.assertIn(34, self.three) self.assertNotIn(35, self.three) self.assertNotIn(40, self.three) # should work if a span is added self.three.spans.append(40) self.assertIn(40, self.three) # should work for spans self.assertIn(Span(31, 33), self.three) self.assertNotIn(Span(31, 37), self.three) # span contains itself self.assertIn(self.twocopy, self.two) # should work for ranges self.assertIn(Range([6, Span(15, 16), Span(30, 33)]), self.three) # should work for copy, except when extra piece added threecopy = Range(self.three) self.assertIn(threecopy, self.three) threecopy.spans.append(1000) self.assertNotIn(threecopy, self.three) self.three.spans.append(Span(950, 1050)) self.assertIn(threecopy, self.three) self.assertNotIn(self.three, threecopy)
def test_contains(self): """Range contains an item if any span contains it""" self.assertContains(self.one, 50) self.assertContains(self.one, 0) self.assertContains(self.one, 99) self.assertNotContains(self.one, 100) self.assertContains(self.three, 6) self.assertNotContains(self.three, 7) self.assertNotContains(self.three, 8) self.assertNotContains(self.three, 14) self.assertContains(self.three, 15) self.assertNotContains(self.three, 29) self.assertContains(self.three, 30) self.assertContains(self.three, 34) self.assertNotContains(self.three, 35) self.assertNotContains(self.three, 40) # should work if a span is added self.three.spans.append(40) self.assertContains(self.three, 40) # should work for spans self.assertContains(self.three, Span(31, 33)) self.assertNotContains(self.three, Span(31, 37)) # span contains itself self.assertContains(self.two, self.twocopy) # should work for ranges self.assertContains(self.three, Range([6, Span(15, 16), Span(30, 33)])) # should work for copy, except when extra piece added threecopy = Range(self.three) self.assertContains(self.three, threecopy) threecopy.spans.append(1000) self.assertNotContains(self.three, threecopy) self.three.spans.append(Span(950, 1050)) self.assertContains(self.three, threecopy) self.assertNotContains(threecopy, self.three)
def test_cmp(self): """Ranges should compare equal if they have the same spans""" self.assertEqual( self.twothree, Range([Span(3, 5), Span(8, 11), Span(6, 7), Span(15, 17), Span(30, 35)]), ) self.assertEqual(Range(), Range())
def setUp(self): """Define some standard Spans""" self.empty = Range(Span(0, 0)) self.full = Range(Span(30, 35)) self.overlapping = Range(Span(32, 36)) self.inside = Range(Span(31, 32)) self.before = Range(Span(25, 30)) self.after = Range(Span(35, 40)) self.reverse = Range(Span(30, 35, reverse=True)) self.spans_zero = Range(Span(-5, 5))
def test_init(self): """Range init from Spans, numbers, or Ranges should work OK.""" # single span self.assertEqual(self.one, Span(0, 100)) # list of spans self.assertEqual(self.two.spans, [Span(3, 5), Span(8, 11)]) # another range self.assertEqual(self.two, self.twocopy) # list of ranges self.assertEqual( self.twothree.spans, [Span(3, 5), Span(8, 11), Span(6, 7), Span(15, 17), Span(30, 35)], ) # list of numbers self.assertEqual(self.singles.spans, [Span(3, 4), Span(11, 12)]) # single number self.assertEqual(self.single.spans, [Span(0, 1)]) # nothing self.assertEqual(Range().spans, [])
def setUp(self): """Set up a few standard ranges.""" self.one = Range(Span(0, 100)) self.two = Range([Span(3, 5), Span(8, 11)]) self.three = Range([Span(6, 7), Span(15, 17), Span(30, 35)]) self.overlapping = Range([Span(6, 10), Span(7, 3)]) self.single = Range(0) self.singles = Range([3, 11]) self.twocopy = Range(self.two) self.twothree = Range([self.two, self.three]) self.empty = Range([Span(6, 6), Span(8, 8)])
class RangeTests(TestCase): """Tests of the Range object.""" def setUp(self): """Set up a few standard ranges.""" self.one = Range(Span(0, 100)) self.two = Range([Span(3, 5), Span(8, 11)]) self.three = Range([Span(6, 7), Span(15, 17), Span(30, 35)]) self.overlapping = Range([Span(6, 10), Span(7, 3)]) self.single = Range(0) self.singles = Range([3, 11]) self.twocopy = Range(self.two) self.twothree = Range([self.two, self.three]) self.empty = Range([Span(6, 6), Span(8, 8)]) def test_init(self): """Range init from Spans, numbers, or Ranges should work OK.""" # single span self.assertEqual(self.one, Span(0, 100)) # list of spans self.assertEqual(self.two.spans, [Span(3, 5), Span(8, 11)]) # another range self.assertEqual(self.two, self.twocopy) # list of ranges self.assertEqual( self.twothree.spans, [Span(3, 5), Span(8, 11), Span(6, 7), Span(15, 17), Span(30, 35)], ) # list of numbers self.assertEqual(self.singles.spans, [Span(3, 4), Span(11, 12)]) # single number self.assertEqual(self.single.spans, [Span(0, 1)]) # nothing self.assertEqual(Range().spans, []) def test_str(self): """Range str should print nested with parens""" self.assertEqual(str(self.one), "((0,100,False))") self.assertEqual( str(self.twothree), "((3,5,False),(8,11,False),(6,7,False),(15,17,False),(30,35,False))", ) self.assertEqual(str(self.single), "((0,1,False))") def test_len(self): """Range len should sum span lengths""" self.assertEqual(len(self.one), 100) self.assertEqual(len(self.single), 1) self.assertEqual(len(self.empty), 0) self.assertEqual(len(self.three), 8) def test_cmp(self): """Ranges should compare equal if they have the same spans""" self.assertEqual( self.twothree, Range([ Span(3, 5), Span(8, 11), Span(6, 7), Span(15, 17), Span(30, 35) ]), ) self.assertEqual(Range(), Range()) def test_start_end(self): """Range start and end should behave as expected""" self.assertEqual(self.one.start, 0) self.assertEqual(self.one.end, 100) self.assertEqual(self.overlapping.start, 3) self.assertEqual(self.overlapping.end, 10) self.assertEqual(self.three.start, 6) self.assertEqual(self.three.end, 35) def test_reverse(self): """Range reverse method should reverse each span""" for s in self.overlapping.spans: self.assertFalse(s.reverse) self.overlapping.reverses() for s in self.overlapping.spans: self.assertTrue(s.reverse) self.overlapping.spans.append(Span(0, 100)) self.overlapping.reverses() for s in self.overlapping.spans[0:1]: self.assertFalse(s.reverse) self.assertTrue(self.overlapping.spans[-1].reverse) def test_Reverse(self): """Range reverse property should return True if any span reversed""" self.assertFalse(self.one.reverse) self.one.reverses() self.assertTrue(self.one.reverse) self.assertFalse(self.two.reverse) self.two.spans.append(Span(0, 100, reverse=True)) self.assertTrue(self.two.reverse) self.two.reverses() self.assertTrue(self.two.reverse) def test_contains(self): """Range contains an item if any span contains it""" self.assertContains(self.one, 50) self.assertContains(self.one, 0) self.assertContains(self.one, 99) self.assertNotContains(self.one, 100) self.assertContains(self.three, 6) self.assertNotContains(self.three, 7) self.assertNotContains(self.three, 8) self.assertNotContains(self.three, 14) self.assertContains(self.three, 15) self.assertNotContains(self.three, 29) self.assertContains(self.three, 30) self.assertContains(self.three, 34) self.assertNotContains(self.three, 35) self.assertNotContains(self.three, 40) # should work if a span is added self.three.spans.append(40) self.assertContains(self.three, 40) # should work for spans self.assertContains(self.three, Span(31, 33)) self.assertNotContains(self.three, Span(31, 37)) # span contains itself self.assertContains(self.two, self.twocopy) # should work for ranges self.assertContains(self.three, Range([6, Span(15, 16), Span(30, 33)])) # should work for copy, except when extra piece added threecopy = Range(self.three) self.assertContains(self.three, threecopy) threecopy.spans.append(1000) self.assertNotContains(self.three, threecopy) self.three.spans.append(Span(950, 1050)) self.assertContains(self.three, threecopy) self.assertNotContains(threecopy, self.three) def test_overlaps(self): """Range overlaps should return true if any component overlapping""" self.assertTrue(self.two.overlaps(self.one)) self.assertTrue(self.one.overlaps(self.two)) self.assertTrue(self.three.overlaps(self.one)) # two and three are interleaved but not overlapping self.assertFalse(self.two.overlaps(self.three)) self.assertFalse(self.three.overlaps(self.two)) self.assertTrue(self.one.overlaps(self.empty)) self.assertTrue(self.empty.overlaps(self.one)) self.assertTrue(self.singles.overlaps(self.two)) def test_overlaps_extent(self): """Range overlaps_extent should return true for interleaved ranges""" self.assertTrue(self.two.overlaps_extent(self.three)) self.assertTrue(self.three.overlaps_extent(self.two)) self.assertFalse(self.single.overlaps_extent(self.two)) self.assertFalse(self.single.overlaps_extent(self.three)) self.assertTrue(self.one.overlaps_extent(self.three)) def test_sort(self): """Range sort should sort component spans""" one = self.one one.sort() self.assertEqual(one.spans, [Span(100, 0)]) one.spans.append(Span(-20, -10)) self.assertEqual(one.spans, [Span(0, 100), Span(-20, -10)]) one.sort() self.assertEqual(one.spans, [Span(-20, -10), Span(0, 100)]) one.spans.append(Span(-20, -10, reverse=True)) self.assertEqual( one.spans, [Span(-20, -10), Span(0, 100), Span(-20, -10, reverse=True)]) one.sort() self.assertEqual( one.spans, [Span(-20, -10), Span(-20, -10, reverse=True), Span(0, 100)]) def test_iter(self): """Range iter should iterate through each span in turn""" self.assertEqual(list(iter(self.two)), [3, 4, 8, 9, 10]) self.two.spans.insert(1, Span(103, 101, reverse=True)) self.assertEqual(list(iter(self.two)), [3, 4, 102, 101, 8, 9, 10]) def test_extent(self): """Range extent should span limits of range""" self.assertEqual(self.one.extent, Span(0, 100)) self.assertEqual(self.three.extent, Span(6, 35)) self.assertEqual(self.singles.extent, Span(3, 12)) self.assertEqual(self.single.extent, Span(0, 1)) self.three.spans.append(Span(100, 105, reverse=True)) self.assertEqual(self.three.extent, Span(6, 105)) self.three.spans.append(Span(-100, -1000)) self.assertEqual(self.three.extent, Span(-1000, 105)) def test_simplify(self): """Range reduce should group overlapping ranges""" # consolidate should have no effect when no overlap r = self.two r.simplify() self.assertEqual(r.spans, [Span(3, 5), Span(8, 11)]) # should consolidate an overlap of the same direction r.spans.append(Span(-1, 4)) r.simplify() self.assertEqual(r.spans, [Span(-1, 5), Span(8, 11)]) # should also consolidate _adjacent_ spans of the same direction r.spans.append(Span(11, 14)) r.simplify() self.assertEqual(r.spans, [Span(-1, 5), Span(8, 14)]) # bridge should cause consolidations s = Range(r) s.spans.append(Span(5, 8)) s.simplify() self.assertEqual(s.spans, [Span(-1, 14)]) # ditto for bridge that overlaps everything s = Range(r) s.spans.append(Span(-100, 100)) s.simplify() self.assertEqual(s.spans, [Span(-100, 100)]) # however, can't consolidate span in other orientation s = Range(r) s.spans.append(Span(-100, 100, reverse=True)) self.assertEqual( s.spans, [Span(-1, 5), Span(8, 14), Span(-100, 100, reverse=True)])