def test_equality(self): p1 = Point(3, 4) p2 = Point(3, 4) p3 = Point(4, 3) self.assertTrue(p1 == p2) self.assertFalse(p1 == p3) self.assertFalse(p2 == p3)
def test_initialization(self): r = Rectangle(1, 2, 3, 4) self.assertEqual(r.left, 1) self.assertEqual(r.bottom, 2) self.assertEqual(r.right, 4) self.assertEqual(r.top, 6) self.assertEqual(r.width, 3) self.assertEqual(r.height, 4) self.assertEqual(r.corner_top_l, Point(1, 6)) self.assertEqual(r.corner_top_r, Point(4, 6)) self.assertEqual(r.corner_bot_l, Point(1, 2)) self.assertEqual(r.corner_bot_r, Point(4, 2))
def test_move(self): r = Rectangle(1, 2, 2, 2) self.assertEqual(r.bottom, 2) self.assertEqual(r.left, 1) r.move(10, 12) self.assertEqual(r.bottom, 12) self.assertEqual(r.left, 10) self.assertEqual(r.top, 14) self.assertEqual(r.right, 12) self.assertEqual(r.height, 2) self.assertEqual(r.width, 2) self.assertEqual(r.corner_top_l, Point(10, 14)) self.assertEqual(r.corner_top_r, Point(12, 14)) self.assertEqual(r.corner_bot_l, Point(10, 12)) self.assertEqual(r.corner_bot_r, Point(12, 12))
def test_init(self): s = VSegment(Point(1, 1), 10) self.assertEqual(s.start, Point(1, 1)) self.assertEqual(s.end, Point(1, 11)) with self.assertRaises(AssertionError): VSegment(Point(1, 1), Point(1, 3)) with self.assertRaises(AssertionError): VSegment(1, Point(1, 1))
def test_init(self): s = Segment(Point(0, 0), Point(3, 3)) self.assertEqual(s.start, Point(0, 0)) self.assertEqual(s.end, Point(3, 3)) with self.assertRaises(AssertionError): Segment(Point(1, 1), 2) with self.assertRaises(AssertionError): Segment(2, Point(1, 1))
def test_length(self): s = VSegment(Point(1, 1), 10) self.assertEqual(s.length, 10)
def test_eq(self): s1 = Segment(Point(0, 0), Point(0, 10)) s2 = Segment(Point(1, 1), Point(1, 11)) s3 = Segment(Point(0, 0), Point(0, 10)) self.assertEqual(s1, s3) self.assertNotEqual(s1, s2)
def test_init(self): p = Point(1, 2) self.assertEqual(p.x, 1) self.assertEqual(p.y, 2)
def test_maxmin(self): s = Segment(Point(1, 1), Point(5, 3)) self.assertEqual(s.top, 3) self.assertEqual(s.bottom, 1) self.assertEqual(s.right, 5) self.assertEqual(s.left, 1)
def test_iter(self): r = Rectangle(1, 1, 1, 2) corners = [Point(1, 1), Point(2, 1), Point(2, 3), Point(1, 3)] for c in r: self.assertTrue(c in corners)
def test_length(self): s = Segment(Point(0, 0), Point(0, 10)) self.assertEqual(s.length, 10) s = Segment(Point(0, 0), Point(0, 3)) self.assertEqual(s.length, 3)
def test_join(self): # Top edge r = Rectangle(4, 4, 2, 2) r_top = Rectangle(4, 6, 2, 2) r_top_big = Rectangle(4, 6, 3, 2) self.assertFalse(r.join(r_top_big)) self.assertTrue(r.join(r_top)) self.assertEqual(r.corner_top_l, Point(4, 8)) self.assertEqual(r.corner_top_r, Point(6, 8)) self.assertEqual(r.corner_bot_l, Point(4, 4)) self.assertEqual(r.corner_bot_r, Point(6, 4)) # Bottom edge r = Rectangle(4, 4, 2, 2) r_bottom = Rectangle(4, 2, 2, 2) r_bottom_big = Rectangle(4, 2, 4, 2) self.assertFalse(r.join(r_bottom_big)) self.assertTrue(r.join(r_bottom)) self.assertEqual(r.corner_top_l, Point(4, 6)) self.assertEqual(r.corner_top_r, Point(6, 6)) self.assertEqual(r.corner_bot_l, Point(4, 2)) self.assertEqual(r.corner_bot_r, Point(6, 2)) # Right edge r = Rectangle(4, 4, 2, 2) r_right = Rectangle(6, 4, 3, 2) r_right_big = Rectangle(6, 4, 2, 4) self.assertFalse(r.join(r_right_big)) self.assertTrue(r.join(r_right)) self.assertEqual(r.corner_top_l, Point(4, 6)) self.assertEqual(r.corner_top_r, Point(9, 6)) self.assertEqual(r.corner_bot_l, Point(4, 4)) self.assertEqual(r.corner_bot_r, Point(9, 4)) # Left edge r = Rectangle(4, 4, 2, 2) r_left = Rectangle(2, 4, 2, 2) r_left_big = Rectangle(2, 4, 2, 4) self.assertFalse(r.join(r_left_big)) self.assertTrue(r.join(r_left)) self.assertEqual(r.corner_top_l, Point(2, 6)) self.assertEqual(r.corner_top_r, Point(6, 6)) self.assertEqual(r.corner_bot_l, Point(2, 4)) self.assertEqual(r.corner_bot_r, Point(6, 4)) # Contains r = Rectangle(4, 4, 3, 3) r_inside = Rectangle(4, 4, 1, 1) self.assertTrue(r.join(r_inside)) self.assertEqual(r.corner_top_l, Point(4, 7)) self.assertEqual(r.corner_top_r, Point(7, 7)) self.assertEqual(r.corner_bot_l, Point(4, 4)) self.assertEqual(r.corner_bot_r, Point(7, 4)) # Is contained r = Rectangle(4, 4, 1, 1) r_inside = Rectangle(4, 4, 3, 3) self.assertTrue(r.join(r_inside)) self.assertEqual(r.corner_top_l, Point(4, 7)) self.assertEqual(r.corner_top_r, Point(7, 7)) self.assertEqual(r.corner_bot_l, Point(4, 4)) self.assertEqual(r.corner_bot_r, Point(7, 4)) # The same r = Rectangle(2, 2, 2, 2) r_same = Rectangle(2, 2, 2, 2) self.assertTrue(r.join(r_same))
def test_distance(self): p1 = Point(1, 1) p2 = Point(2, 1) self.assertEqual(p1.distance(p2), 1)