def test_nearest_neighbors_large(self): # Load up a "big" quadtree. tree = QuadTree((0, 0), 100, 100) for x, y in test_data.data.get("large_random", []): tree.insert((x, y)) ul_pnt = Point(-35, 25) nearby = tree.nearest_neighbors(ul_pnt, count=10) self.assertEqual(len(nearby), 10) self.assertEqual( [(pnt.x, pnt.y) for pnt in nearby], [ (-33, 22), (-36, 29), (-34, 21), (-38, 28), (-40, 25), (-35, 30), (-40, 26), (-30, 26), (-33, 30), (-31, 29), ], ) distances = [euclidean_distance(ul_pnt, found) for found in nearby] self.assertAlmostEqual(distances[0], 3.605551275463989) self.assertAlmostEqual(distances[-1], 5.656854249492381)
def test_len(self): tree = self.create_sample_tree() self.assertEqual(len(tree), 12) # Load up a "big" quadtree. tree = QuadTree((0, 0), 100, 100) for x, y in test_data.data.get("large_random", []): tree.insert((x, y)) self.assertEqual(len(tree), 1000)
def test_nearest_neighbors_outside(self): tree = QuadTree((0, 0), 20, 20) tree.insert((1, 2), data="oof") tree.insert((-7, 5), data="we") tree.insert((-1, -2), data="are") tree.insert((3, -6), data="small") ur_pnt = Point(21, 21) nearby = tree.nearest_neighbors(ur_pnt, count=10) self.assertEqual(len(nearby), 0)
def test_iter(self): tree = self.create_sample_tree() count = 0 for pnt in tree: count += 1 self.assertEqual(count, 12) # Load up a "big" quadtree. tree = QuadTree((0, 0), 100, 100) for x, y in test_data.data.get("large_random", []): tree.insert((x, y)) count = 0 for pnt in tree: count += 1 self.assertEqual(count, 1000)
def test_insert(self): tree = QuadTree((0, 0), 20, 20) self.assertTrue(tree.insert((1, 2))) self.assertTrue(tree.insert((7, 5))) self.assertTrue(tree.insert((6, 4))) self.assertTrue(tree.insert((-1, -2))) self.assertTrue(tree.insert((9, -9)))
def test_dunder_contains(self): tree = QuadTree((0, 0), 20, 20) tree.insert((1, 2)) tree.insert((7, 5)) tree.insert((6, 4)) self.assertTrue(Point(1, 2) in tree) self.assertTrue(Point(6, 4) in tree) self.assertFalse(Point(2, 3) in tree)
def test_nearest_neighbors_tiny(self): tree = QuadTree((0, 0), 20, 20) tree.insert((1, 2), data="oof") tree.insert((-7, 5), data="we") tree.insert((-1, -2), data="are") tree.insert((3, -6), data="small") lr_pnt = Point(9, -9) nearby = tree.nearest_neighbors(lr_pnt, count=10) self.assertEqual(len(nearby), 4) self.assertEqual( [(pnt.x, pnt.y) for pnt in nearby], [(3, -6), (-1, -2), (1, 2), (-7, 5)], ) self.assertEqual( [pnt.data for pnt in nearby], ["small", "are", "oof", "we"], ) distances = [euclidean_distance(lr_pnt, found) for found in nearby] self.assertAlmostEqual(distances[0], 6.708203932499369) self.assertAlmostEqual(distances[-1], 21.2602916254693)
def create_sample_tree(self): tree = QuadTree((0, 0), 100, 100) # Insert a wide variety of points & data values. tree.insert((1, 2), data=True) tree.insert((7, 5), data="dog") tree.insert((6, 4), data="cat") tree.insert((-1, -2), data=False) tree.insert((10, -22), data=35) tree.insert((10, -22.5), data=["a", "b"]) tree.insert((9, -17), data=89.567) tree.insert((10, 35), data="fish") tree.insert((11, 42), data="Samus") tree.insert((-15, 17), data={"hello": "world"}) tree.insert((-15, 9), data="whatev") tree.insert((-13, 6), data=-69) return tree
def test_convert_to_point_nope(self): tree = QuadTree((0, 0), 10, 10) with self.assertRaises(ValueError): tree.convert_to_point("Samus")
def test_convert_to_point_none(self): tree = QuadTree((0, 0), 10, 10) converted = tree.convert_to_point(None) self.assertEqual(converted.x, 0) self.assertEqual(converted.y, 0)
def test_convert_to_point_tuple(self): tree = QuadTree((0, 0), 10, 10) converted = tree.convert_to_point((5, 6)) self.assertEqual(converted.x, 5) self.assertEqual(converted.y, 6)
def test_convert_to_point_already_a_point(self): tree = QuadTree((0, 0), 10, 10) pt = Point(1, 2) converted = tree.convert_to_point(pt) self.assertEqual(converted.x, pt.x) self.assertEqual(converted.y, pt.y)
def test_str(self): tree = QuadTree((0, 0), 10, 10) self.assertEqual(str(tree), "<QuadTree: (0, 0) 10x10>")
def test_init(self): tree = QuadTree((0, 0), 10, 10) self.assertEqual(tree.width, 10) self.assertEqual(tree.height, 10) self.assertEqual(tree.center, Point(0, 0))