def test_range_intersects(self): ''' Verifies that Range.intersects returns True when 2 ranges intersect and False otherwise. ''' tst_r = calculator.Range((5, 0), (10, math.inf), (15, 0), (20, math.inf)) r_1 = calculator.Range((4, 0), (9, math.inf), (15, 0), (20, math.inf)) r_2 = calculator.Range((6, 0), (11, math.inf), (15, 0), (20, math.inf)) r_3 = calculator.Range((4, 0), (11, math.inf), (15, 0), (20, math.inf)) r_4 = calculator.Range((6, 0), (10, math.inf), (15, 0), (20, math.inf)) r_5 = calculator.Range((5, 0), (10, math.inf), (14, 0), (19, math.inf)) r_6 = calculator.Range((5, 0), (10, math.inf), (16, 0), (21, math.inf)) r_7 = calculator.Range((5, 0), (10, math.inf), (14, 0), (21, math.inf)) r_8 = calculator.Range((5, 0), (10, math.inf), (16, 0), (19, math.inf)) r_9 = calculator.Range((20, 0), (25, math.inf), (30, 0), (35, math.inf)) self.assertTrue(tst_r.intersects(r_1)) self.assertTrue(tst_r.intersects(r_2)) self.assertTrue(tst_r.intersects(r_3)) self.assertTrue(tst_r.intersects(r_4)) self.assertTrue(tst_r.intersects(r_5)) self.assertTrue(tst_r.intersects(r_6)) self.assertTrue(tst_r.intersects(r_7)) self.assertTrue(tst_r.intersects(r_8)) self.assertTrue(tst_r.intersects(r_1)) self.assertTrue(tst_r.intersects(r_2)) self.assertTrue(tst_r.intersects(r_3)) self.assertFalse(tst_r.intersects(r_9))
def test_range_init_y_min_greater_than_y_max(self): ''' Verifies that range initializer raises TypeError when y_min > y_max. ''' self.assertRaises(TypeError,calculator.Range, (0,10), (8, 12), \ (10, 7), (6,8)) self.assertRaises(TypeError,calculator.Range, (0,10), (10, 9), \ (6, 9), (6,8)) #demonstrate that having y_min == y_max is acceptable. self.assertEqual(calculator.Range((0,10), (10, 10), (5, 7), (5,7)), \ calculator.Range((0,10), (10, 10), (5, 7), (5,7)))
def test_kdtree_report_subtree(self): ''' Verifies that report_subtree correctly reports the subtree from the root node of the kdtree. ''' #create a list of points point_list = [ calculator.Point(1, 1), calculator.Point(2, 2), calculator.Point(101, 101), calculator.Point(102, 102), calculator.Point(201, 201) ] #create a kdtree test_kdtree = calculator.KDTree() test_kdtree.build_kdtree(points=point_list, depth=0, \ reg=calculator.Range((0,0), (math.inf, math.inf), (0,0), (math.inf, math.inf))) #report the subtree of the kdtree test_point_list = test_kdtree.report_subtree() #sort the two lists and verify that they are the same point_list.sort(key=lambda point: point.cn[0]) test_point_list.sort(key=lambda point: point.cn[0]) self.assertEqual(len(point_list), len(test_point_list)) for i in range(len(point_list)): self.assertEqual(test_point_list[i], point_list[i])
def test_range_init_good_input(self): ''' Verifies that Range initializes correctly when given 4 values all of which are either non-negative integers or +infinity ''' test_range = calculator.Range((0,1), (math.inf, math.inf), \ (5, 7), (8, 9)) self.assertEqual(test_range.x_min[0], 0)
def test_range_equality(self): ''' Verifies that Range.__eq__ returns True if the compared Ranges are equal and false otherwise. ''' self.assertTrue(calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (7,8)) == calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (7,8))) self.assertFalse(calculator.Range((1,0), (math.inf, math.inf), \ (5,6), (7,8)) == calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (7,8))) self.assertFalse(calculator.Range((0,0), (5, math.inf), \ (5,6), (7,8)) == calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (7,8))) self.assertFalse(calculator.Range((0,0), (math.inf, math.inf), \ (6,6), (7,8)) == calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (7,8))) self.assertFalse(calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (7,8)) == calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (7,10)))
def test_lots_of_points(self): ''' Measures the time required to find points in a range with the kdtree and iterating through a list. Outputs the times so that a user can observe the time difference. It also prints out the list of points found with the kdtree and with the list so that the user can visually verify accuracy. ''' n = 2000 max_value = 100000 min_value = 0 min_xy = (50000,0) max_xy = (60000,math.inf) #generate n points randomly (0..100000, 0..100000) point_list = [] i = 0 excluded = set() while i < n: point = calculator.Point(random.randint(min_value, max_value), random.randint(min_value, max_value)) if point not in excluded: point_list.append(point) excluded.add(point) i = i + 1 #time placing the points into a KDTree start = time.time() my_tree = calculator.KDTree() slide_range = calculator.Range((0, 0), (math.inf, math.inf), (0,0), (math.inf,math.inf)) my_tree.build_kdtree(point_list, 0, slide_range) build_time = time.time() - start print('time to build', n, ' node kdtree: ', build_time) #time searching the tree for a 10,000 by 10,000 box start = time.time() found_points = my_tree.search_kdtree(calculator.Range(min_xy, max_xy, min_xy, max_xy)) search_time = time.time() - start print('time to search', n, ' node kdtree: ', search_time) print('found', len(found_points), 'points in ', search_time) found_points_sorted = sorted(found_points, key=lambda point: point.cn[0]) for i in range(len(found_points)): print(str(found_points_sorted[i]), end=" ") print() #time searching the tree for the same box start = time.time() found_points_2 = [] for point in point_list: if min_xy <= point.cn[0] and \ point.cn[0] <= max_xy and \ min_xy <= point.cn[1] and \ point.cn[1] <= max_xy: found_points_2.append(point) search_time = time.time() - start print('time to search', n, ' nodes in a list: ', search_time) print('found', len(found_points_2), 'points in ', search_time) found_points_sorted_2 = sorted(found_points_2, key=lambda point: point.cn[0]) for i in range(len(found_points_sorted_2)): print(str(found_points_sorted_2[i]), end=" ") print()
def test_range_string(self): ''' Verifies that Range.__str__ returns the string value of Range ''' self.assertEqual(str(calculator.Range((0,0), (math.inf, math.inf), \ (5,6), (10,12))), "x=(0,0)..(inf,inf), y=(5,6)..(10,12)")