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_point_eq(self): ''' Verifies that point.__eq__ returns true when comparing the same point and to itself and false when comparing different points. ''' test_point_1 = calculator.Point(2, 3) test_point_2 = calculator.Point(2, 3) test_point_3 = calculator.Point(2, 4) test_point_4 = calculator.Point(4, 3) self.assertEqual(test_point_1, test_point_2) self.assertNotEqual(test_point_1, test_point_3) self.assertNotEqual(test_point_1, test_point_4)
def test_point_hash(self): ''' Verifies that point.__hash__ results in Points being correctly added to a set. ''' test_point_1 = calculator.Point(2, 3) test_point_2 = calculator.Point(2, 3) test_point_3 = calculator.Point(2, 4) test_set = set() self.assertEqual(len(test_set), 0) test_set.add(test_point_1) self.assertEqual(len(test_set), 1) test_set.add(test_point_2) self.assertEqual(len(test_set), 1) test_set.add(test_point_3) self.assertEqual(len(test_set), 2)
def test_point_str(self): ''' Verifies that point.__str__ prints "(xval,yval)" when passed a Point(xval,yval). ''' test_point = calculator.Point(2, 3) self.assertEqual(str(test_point), '(2,3)')
def test_point_init_good_values(self): ''' Verifies that point initializer returns correct values when passed two non-negative integers for x and y (good input). ''' test_point = calculator.Point(2, 3) self.assertEqual(test_point.x, 2) self.assertEqual(test_point.y, 3) self.assertEqual(test_point.cn, ((2, 3), (3, 2)))
def test_filter_by_circle(self): ''' tests filter_by_circle in the calculator module ''' center = (11000, 11000) points = [ calculator.Point(11000, 11500), calculator.Point(16501, 11000), calculator.Point(10000, 16501), calculator.Point(11000, 5499), calculator.Point(20000, 20000), calculator.Point(16500, 11001) ] points = calculator.filter_by_circle(points, center) self.assertEqual(len(points), 1) self.assertEqual(points[0].x, 11000) self.assertEqual(points[0].y, 11500)
def test_find_squares_and_circles(self): ''' prints out the set of upper left corners of squares and centers of circles corresponding to the point (20000, 40000) for visual user verification ''' pt = calculator.Point(20000, 40000) squares, circles = calculator.find_squares_and_circles(pt) self.assertEqual(squares[0], (14500, 40000)) self.assertEqual(squares[1], (10611, 38389)) self.assertEqual(squares[2], (9000, 34500)) self.assertEqual(squares[3], (10611, 30611)) self.assertEqual(squares[4], (14500, 29000)) self.assertEqual(squares[5], (18389, 30611)) self.assertEqual(squares[6], (20000, 34500)) self.assertEqual(squares[7], (18389, 38389)) self.assertEqual(circles[0], (20000, 45500)) self.assertEqual(circles[1], (16111, 43889)) self.assertEqual(circles[2], (14500, 40000)) self.assertEqual(circles[3], (16111, 36111)) self.assertEqual(circles[4], (20000, 34500)) self.assertEqual(circles[5], (23889, 36111)) self.assertEqual(circles[6], (25500, 40000)) self.assertEqual(circles[7], (23889, 43889))
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_print_point_list(self): ''' Verifies that print_point_list prints the point list as expected. ''' point_list = [calculator.Point(2, 3), calculator.Point(3, 5)] calculator.print_point_list(point_list)