def test_standard(self): a = Interval(3, 5) self.assertEqual(a.length, 3) self.assertEqual(a.min, 3) self.assertEqual(a.max, 5) self.assertFalse(a.empty) self.assertEqual(a, Interval(max=5, min=3))
def test_single(self): a = Interval(5) self.assertEqual(a.length, 1) self.assertEqual(a.min, 5) self.assertEqual(a.max, 5) self.assertFalse(a.empty) self.assertEqual(a, Interval(5))
def test_empty(self): a = Box() self.assertTrue(a.empty) b = Box(x=Interval(3, 1), y=Interval(1, 2)) self.assertTrue(b.empty) c = Box(x=Interval(1, 4), y=Interval(4, 2)) self.assertTrue(c.empty)
def test_box(self): box = Box(x=Interval(min=1, max=5), y=Interval(min=7, max=9)) s = SpanSet(box) self.assertEqual(len(s), box.height) self.assertEqual(list(s), [Span(x=box.x, y=y) for y in range(box.y0, box.y1 + 1)]) e = SpanSet() self.assertEqual((s | e), s) self.assertEqual((s & e), e)
def test_overlaps(self): a = Interval(1, 4) c = Interval(3, 4) d = Interval(-2, 0) self.assertFalse(Box(c, c).overlaps(Box(d, d))) self.assertFalse(Box(c, c).overlaps(Box(a, d))) self.assertFalse(Box(c, c).overlaps(Box(d, a))) self.assertTrue(Box(c, c).overlaps(Box(a, a)))
def test_overlapping(self): box1 = Box(x=Interval(min=1, max=5), y=Interval(min=7, max=9)) box2 = Box(x=Interval(min=3, max=7), y=Interval(min=6, max=8)) s1 = SpanSet(box1) s2 = SpanSet(box2) s = s1 | s2 self.assertEqual(s1 & s2, SpanSet(box1 & box2)) self.assertEqual(s & s2, s2) self.assertEqual(s & s1, s1)
def test_expand(self): a = Interval(-1, 7) self.assertEqual(a, Interval(-1, 7)) c = a.expanded_to(9) self.assertEqual(c, Interval(-1, 9)) d = a.expanded_to(Interval(-3, -2)) self.assertEqual(d, Interval(-3, 7)) e = a.expanded_to(Interval()) self.assertEqual(a, e)
def test_intersect(self): a = Interval(1, 4) b = Interval(3, 6) c = Interval(5, 8) self.assertEqual(a & b, Interval(3, 4)) self.assertTrue((a & c).empty) a &= b self.assertEqual(a, Interval(3, 4)) a &= c self.assertTrue(a.empty)
def test_standard(self): a = Box(x=Interval(3, 5), y=Interval(7, 9)) self.assertEqual(a.width, 3) self.assertEqual(a.x0, 3) self.assertEqual(a.x1, 5) self.assertEqual(a.x, Interval(3, 5)) self.assertEqual(a.height, 3) self.assertEqual(a.y0, 7) self.assertEqual(a.y1, 9) self.assertEqual(a.y, Interval(7, 9)) self.assertFalse(a.empty) self.assertEqual(a.area, 9)
def test_disjoint(self): box1 = Box(x=Interval(min=1, max=5), y=Interval(min=7, max=9)) box2 = Box(x=Interval(min=7, max=9), y=Interval(min=6, max=8)) s1 = SpanSet(box1) s2 = SpanSet(box2) s = s1 | s2 for span in s: self.assertTrue(box1.overlaps(span) or box2.overlaps(span)) self.assertEqual(s & s1, s1) self.assertEqual(s & s2, s2) s1a, s2a = s.split() self.assertTrue((s1 == s1a and s2 == s2a) or (s1 == s2a and s2 == s1a))
def test_intersect(self): a = Interval(1, 4) b = Interval(3, 6) c = Interval(3, 4) d = Interval(-2, 0) self.assertEqual(Box(a, a) & Box(b, a), Box(c, a)) self.assertTrue((Box(a, a) & Box(d, a)).empty) box = Box(a, a) box &= Box(b, a) self.assertEqual(box, Box(c, a)) box = Box(a, a) box &= Box(d, a) self.assertTrue(box.empty)
def test_expand_and_overlaps(self): a = Interval(1, 4) b = Interval(3, 6) c = Interval(3, 4) d = Interval(1, 3) e = Interval(4, 6) box1 = Box(x=c, y=c) # x, y overload self.assertEqual(box1.expanded_to(x=1, y=3), Box(x=a, y=c)) self.assertEqual(box1.expanded_to(x=3, y=1), Box(x=c, y=a)) self.assertEqual(box1.expanded_to(x=6, y=3), Box(x=b, y=c)) self.assertEqual(box1.expanded_to(x=3, y=6), Box(x=c, y=b)) # Span overload self.assertEqual(box1.expanded_to(Span(x=d, y=3)), Box(x=a, y=c)) self.assertEqual(box1.expanded_to(Span(x=c, y=1)), Box(x=c, y=a)) self.assertEqual(box1.expanded_to(Span(x=e, y=3)), Box(x=b, y=c)) self.assertEqual(box1.expanded_to(Span(x=c, y=6)), Box(x=c, y=b)) # Box overload self.assertEqual(box1.expanded_to(Box(x=d, y=c)), Box(x=a, y=c)) self.assertEqual(box1.expanded_to(Box(x=c, y=d)), Box(x=c, y=a)) self.assertEqual(box1.expanded_to(Box(x=e, y=c)), Box(x=b, y=c)) self.assertEqual(box1.expanded_to(Box(x=c, y=e)), Box(x=c, y=b))
def test_overlap(self): a = Span(x=Interval(2, 6), y=1) self.assertFalse(a.overlaps(1, 1)) self.assertFalse(a.overlaps(3, 0)) self.assertTrue(a.overlaps(2, 1)) self.assertTrue(a.overlaps(4, 1)) self.assertTrue(a.overlaps(6, 1)) self.assertFalse(a.overlaps(7, 1)) self.assertFalse(a.overlaps(4, 2)) self.assertFalse(a.overlaps(Span(x=Interval(-1, 1), y=1))) self.assertFalse(a.overlaps(Span(x=Interval(-1, 2), y=0))) self.assertTrue(a.overlaps(Span(x=Interval(-1, 2), y=1))) self.assertTrue(a.overlaps(Span(x=Interval(3, 4), y=1))) self.assertTrue(a.overlaps(Span(x=Interval(5, 7), y=1))) self.assertFalse(a.overlaps(Span(x=Interval(5, 7), y=2))) self.assertFalse(a.overlaps(Span(x=Interval(7, 9), y=1)))
def test_intersect(self): a = Span(y=1, x=Interval(1, 4)) b = Span(y=1, x=Interval(3, 6)) c = Span(y=1, x=Interval(5, 8)) d = Span(y=0, x=Interval(2, 3)) self.assertEqual(a & b, Span(y=1, x=Interval(3, 4))) self.assertTrue((a & c).empty) self.assertTrue((a & d).empty) a &= b self.assertEqual(a, Span(y=1, x=Interval(3, 4))) a &= c self.assertTrue(a.empty) b &= d self.assertTrue(b.empty)
def test_standard(self): a = Span(Interval(3, 5), 2) self.assertEqual(a.width, 3) self.assertEqual(a.x0, 3) self.assertEqual(a.x1, 5) self.assertFalse(a.empty)
def test_empty(self): a = Interval() self.assertTrue(a.empty) b = Interval(3, 1) self.assertTrue(b.empty) self.assertEqual(a, b)
def test_overlap(self): a = Interval(2, 6) self.assertFalse(a.overlaps(1)) self.assertTrue(a.overlaps(2)) self.assertTrue(a.overlaps(4)) self.assertTrue(a.overlaps(6)) self.assertFalse(a.overlaps(7)) self.assertFalse(a.overlaps(Interval(-1, 1))) self.assertTrue(a.overlaps(Interval(-1, 2))) self.assertTrue(a.overlaps(Interval(3, 4))) self.assertTrue(a.overlaps(Interval(5, 7))) self.assertFalse(a.overlaps(Interval(7, 9)))
def test_empty(self): a = Span() self.assertTrue(a.empty) b = Span(y=2, x=Interval(3, 1)) self.assertTrue(b.empty)