MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / test_build_kdtree

Function test_build_kdtree

data_structures/kd_tree/tests/test_kdtree.py:26–58  ·  view source on GitHub ↗

Test that KD-Tree is built correctly. Cases: - Empty points list. - Positive depth value. - Negative depth value.

(num_points, cube_size, num_dimensions, depth, expected_result)

Source from the content-addressed store, hash-verified

24 ],
25)
26def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_result):
27 """
28 Test that KD-Tree is built correctly.
29
30 Cases:
31 - Empty points list.
32 - Positive depth value.
33 - Negative depth value.
34 """
35 points = (
36 hypercube_points(num_points, cube_size, num_dimensions).tolist()
37 if num_points > 0
38 else []
39 )
40
41 kdtree = build_kdtree(points, depth=depth)
42
43 if expected_result is None:
44 # Empty points list case
45 assert kdtree is None, f"Expected None for empty points list, got {kdtree}"
46 else:
47 # Check if root node is not None
48 assert kdtree is not None, "Expected a KDNode, got None"
49
50 # Check if root has correct dimensions
51 assert len(kdtree.point) == num_dimensions, (
52 f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
53 )
54
55 # Check that the tree is balanced to some extent (simplistic check)
56 assert isinstance(kdtree, KDNode), (
57 f"Expected KDNode instance, got {type(kdtree)}"
58 )
59
60
61def test_nearest_neighbour_search():

Callers

nothing calls this directly

Calls 2

hypercube_pointsFunction · 0.90
build_kdtreeFunction · 0.90

Tested by

no test coverage detected