def test_subdivide(self): node = QuadNode(Point(0, 0), 20, 20) node.points = [ Point(1, 2), Point(-3, -3), Point(-9, 6), Point(7, 2), ] # Sanity check. self.assertIsNone(node.ul) self.assertIsNone(node.ur) self.assertIsNone(node.ll) self.assertIsNone(node.lr) node.subdivide() # There should now be child nodes. self.assertIsNotNone(node.ul) self.assertIsNotNone(node.ur) self.assertIsNotNone(node.ll) self.assertIsNotNone(node.lr) # Check the points. self.assertEqual(len(node.points), 0) self.assertEqual(len(node.ul.points), 1) self.assertEqual(len(node.ur.points), 2) self.assertEqual(len(node.ll.points), 1) pairs = [(pnt.x, pnt.y) for pnt in node.ur.points] self.assertEqual(pairs, [(1, 2), (7, 2)])
def test_insert(self): node = QuadNode(Point(0, 0), 20, 20) node.insert(Point(7, 5)) node.insert(Point(6, 4)) node.insert(Point(-1, -2)) node.insert(Point(9, -9)) # No subdivision yet. self.assertEqual(len(node.points), 4) self.assertIsNone(node.ul) self.assertIsNone(node.ur) self.assertIsNone(node.ll) self.assertIsNone(node.lr) # Over the edge, automatically subdivide before the insert happens. node.insert(Point(8, 8)) self.assertEqual(len(node.points), 0) self.assertIsNotNone(node.ul) self.assertEqual(len(node.ul.points), 0) self.assertIsNotNone(node.ur) self.assertEqual(len(node.ur.points), 3) self.assertIsNotNone(node.ll) self.assertEqual(len(node.ll.points), 1) self.assertIsNotNone(node.lr) self.assertEqual(len(node.lr.points), 1)
def test_dunder_contains(self): node = QuadNode(Point(0, 0), 10, 10) node.points = [ Point(1, 2), Point(-3, -1), ] self.assertTrue(Point(1, 2) in node) self.assertFalse(Point(2, 3) in node)
def test_is_lr(self): node = QuadNode(Point(1, 2), 10, 10) yes = Point(3, -1) self.assertTrue(node.is_lr(yes)) no = Point(2, 4) self.assertFalse(node.is_lr(no)) also_no = Point(2, 2) self.assertFalse(node.is_lr(also_no))
def test_is_ll(self): node = QuadNode(Point(1, 2), 10, 10) yes = Point(-1, 1) self.assertTrue(node.is_ll(yes)) no = Point(2, 4) self.assertFalse(node.is_ll(no)) also_no = Point(0, 2) self.assertFalse(node.is_ll(also_no))
def test_is_ur(self): node = QuadNode(Point(1, 2), 10, 10) yes = Point(3, 3) self.assertTrue(node.is_ur(yes)) no = Point(0, 1) self.assertFalse(node.is_ur(no)) also_yes = Point(3, 2) self.assertTrue(node.is_ur(also_yes))
def test_init(self): node = QuadNode(Point(0, 0), 10, 10) self.assertEqual(node.center.x, 0) self.assertEqual(node.center.y, 0) self.assertEqual(node.width, 10) self.assertEqual(node.height, 10) self.assertEqual(len(node.points), 0) self.assertIsNone(node.ul) self.assertIsNone(node.ur) self.assertIsNone(node.ll) self.assertIsNone(node.lr)
def test_contains_point(self): node = QuadNode(Point(1, 2), 10, 10) yes = Point(3, -1) self.assertTrue(node.contains_point(yes)) also_yes = Point(-3.9, -2.9) self.assertTrue(node.contains_point(also_yes)) no = Point(12, 14) self.assertFalse(node.contains_point(no)) also_no = Point(-5, 2) self.assertFalse(node.contains_point(also_no))
def create_medium_tree(self): node = QuadNode(Point(0, 0), 100, 100) node.insert(Point(1, 2, data=True)) node.insert(Point(7, 5, data="dog")) node.insert(Point(6, 4, data="cat")) node.insert(Point(-1, -2, data=False)) node.insert(Point(10, -22, data=35)) node.insert(Point(10, -22.5, data=["a", "b"])) node.insert(Point(9, -17, data=89.567)) node.insert(Point(10, 35, data="fish")) node.insert(Point(11, 42, data="Samus")) node.insert(Point(-15, 17, data={"hello": "world"})) node.insert(Point(-15, 9, data="whatev")) node.insert(Point(-13, 6, data=-69)) return node
def create_simple_tree(self): node = QuadNode(Point(0, 0), 20, 20) node.insert(Point(7, 5, data="dog")) node.insert(Point(6, 4, data="cat")) node.insert(Point(-1, -2, data=True)) node.insert(Point(9, -9, data={"hello": "world"})) node.insert(Point(8, 8, data=("a", "b", "c"))) node.insert(Point(-3, 2, data=False)) return node
def test_insert_ll(self): # Without this, a lower-left insert fails to be seen on coverage, # which is weird. Ensure that happens & things look right. node = QuadNode(Point(0, 0), 20, 20) node.ul = QuadNode(Point(-5, 5), 10, 10) node.ur = QuadNode(Point(5, 5), 10, 10) node.ll = QuadNode(Point(-5, -5), 10, 10) node.lr = QuadNode(Point(5, -5), 10, 10) node.insert(Point(-7, -5)) self.assertTrue(Point(-7, -5) in node) self.assertTrue(Point(-7, -5) in node.ll)
def test_insert_fail(self): node = QuadNode(Point(0, 0), 20, 20) with self.assertRaises(ValueError): node.insert(Point(17, 55))
def test_calc_bounding_box_moderate(self): node = QuadNode(Point(13, 17), 50, 60) self.assertEqual(node.bounding_box.min_x, -12) self.assertEqual(node.bounding_box.min_y, -13) self.assertEqual(node.bounding_box.max_x, 38) self.assertEqual(node.bounding_box.max_y, 47)
def test_calc_bounding_box_basic(self): node = QuadNode(Point(0, 0), 10, 10) self.assertEqual(node.bounding_box.min_x, -5) self.assertEqual(node.bounding_box.min_y, -5) self.assertEqual(node.bounding_box.max_x, 5) self.assertEqual(node.bounding_box.max_y, 5)
def test_str(self): node = QuadNode(Point(0, 0), 10, 10) self.assertEqual(str(node), "<QuadNode: (0, 0) 10x10>")