|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +from data_structures.kd_tree.build_kdtree import build_kdtree |
| 5 | +from data_structures.kd_tree.example.hypercube_points import hypercube_points |
| 6 | +from data_structures.kd_tree.kd_node import KDNode |
| 7 | +from data_structures.kd_tree.nearest_neighbour_search import nearest_neighbour_search |
| 8 | + |
| 9 | + |
| 10 | +@pytest.mark.parametrize( |
| 11 | + ("num_points", "cube_size", "num_dimensions", "depth", "expected_result"), |
| 12 | + [ |
| 13 | + (0, 10.0, 2, 0, None), # Empty points list |
| 14 | + (10, 10.0, 2, 2, KDNode), # Depth = 2, 2D points |
| 15 | + (10, 10.0, 3, -2, KDNode), # Depth = -2, 3D points |
| 16 | + ], |
| 17 | +) |
| 18 | +def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_result): |
| 19 | + """ |
| 20 | + Test that KD-Tree is built correctly. |
| 21 | +
|
| 22 | + Cases: |
| 23 | + - Empty points list. |
| 24 | + - Positive depth value. |
| 25 | + - Negative depth value. |
| 26 | + """ |
| 27 | + points = ( |
| 28 | + hypercube_points(num_points, cube_size, num_dimensions).tolist() |
| 29 | + if num_points > 0 |
| 30 | + else [] |
| 31 | + ) |
| 32 | + |
| 33 | + kdtree = build_kdtree(points, depth=depth) |
| 34 | + |
| 35 | + if expected_result is None: |
| 36 | + # Empty points list case |
| 37 | + assert kdtree is None, f"Expected None for empty points list, got {kdtree}" |
| 38 | + else: |
| 39 | + # Check if root node is not None |
| 40 | + assert kdtree is not None, "Expected a KDNode, got None" |
| 41 | + |
| 42 | + # Check if root has correct dimensions |
| 43 | + assert ( |
| 44 | + len(kdtree.point) == num_dimensions |
| 45 | + ), f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}" |
| 46 | + |
| 47 | + # Check that the tree is balanced to some extent (simplistic check) |
| 48 | + assert isinstance( |
| 49 | + kdtree, KDNode |
| 50 | + ), f"Expected KDNode instance, got {type(kdtree)}" |
| 51 | + |
| 52 | + |
| 53 | +def test_nearest_neighbour_search(): |
| 54 | + """ |
| 55 | + Test the nearest neighbor search function. |
| 56 | + """ |
| 57 | + num_points = 10 |
| 58 | + cube_size = 10.0 |
| 59 | + num_dimensions = 2 |
| 60 | + points = hypercube_points(num_points, cube_size, num_dimensions) |
| 61 | + kdtree = build_kdtree(points.tolist()) |
| 62 | + |
| 63 | + rng = np.random.default_rng() |
| 64 | + query_point = rng.random(num_dimensions).tolist() |
| 65 | + |
| 66 | + nearest_point, nearest_dist, nodes_visited = nearest_neighbour_search( |
| 67 | + kdtree, query_point |
| 68 | + ) |
| 69 | + |
| 70 | + # Check that nearest point is not None |
| 71 | + assert nearest_point is not None |
| 72 | + |
| 73 | + # Check that distance is a non-negative number |
| 74 | + assert nearest_dist >= 0 |
| 75 | + |
| 76 | + # Check that nodes visited is a non-negative integer |
| 77 | + assert nodes_visited >= 0 |
| 78 | + |
| 79 | + |
| 80 | +def test_edge_cases(): |
| 81 | + """ |
| 82 | + Test edge cases such as an empty KD-Tree. |
| 83 | + """ |
| 84 | + empty_kdtree = build_kdtree([]) |
| 85 | + query_point = [0.0] * 2 # Using a default 2D query point |
| 86 | + |
| 87 | + nearest_point, nearest_dist, nodes_visited = nearest_neighbour_search( |
| 88 | + empty_kdtree, query_point |
| 89 | + ) |
| 90 | + |
| 91 | + # With an empty KD-Tree, nearest_point should be None |
| 92 | + assert nearest_point is None |
| 93 | + assert nearest_dist == float("inf") |
| 94 | + assert nodes_visited == 0 |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + import pytest |
| 99 | + |
| 100 | + pytest.main() |
0 commit comments