Skip to content

Commit ac2829f

Browse files
committed
test: fix broken test of custom struct as T. Add test for () as T
1 parent 3968e04 commit ac2829f

1 file changed

Lines changed: 32 additions & 4 deletions

File tree

tests/string_data_test.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ fn test_kdtree_with_numeric_id() {
3737

3838
// The closest point should be [0.0, 0.0] with ID 1001
3939
assert_eq!(nearest.item, 1001);
40-
//assert!((nearest.distance - 0.5).abs() < f64::EPSILON);
41-
assert!((nearest.distance - (0.5f64 * 0.5 + 0.25 * 0.25).sqrt()).abs() < f64::EPSILON);
40+
assert!((nearest.distance - (0.5f64 * 0.5 + 0.25 * 0.25)).abs() < f64::EPSILON);
4241

4342
// Test k-nearest neighbors
4443
let k_nearest = tree.nearest_n::<SquaredEuclidean>(&query_point, 2);
@@ -72,14 +71,14 @@ fn test_kdtree_with_fixed_string() {
7271

7372
// The closest point should be [0.0, 0.0] with data "Origin"
7473
assert_eq!(nearest.item.as_str(), "Origin");
75-
assert!((nearest.distance - (0.5f64 * 0.5 + 0.25 * 0.25).sqrt()).abs() < f64::EPSILON);
74+
assert!((nearest.distance - (0.5f64 * 0.5 + 0.25 * 0.25)).abs() < f64::EPSILON);
7675

7776
let (nearest, nearest_point) = tree.nearest_one_point::<SquaredEuclidean>(&query_point);
7877

7978
// The closest point should be [0.0, 0.0] with data "Origin"
8079
assert_eq!(nearest.item.as_str(), "Origin");
8180
assert_eq!(nearest_point, [0.0, 0.0]);
82-
assert!((nearest.distance - (0.5f64 * 0.5 + 0.25 * 0.25).sqrt()).abs() < f64::EPSILON);
81+
assert!((nearest.distance - (0.5f64 * 0.5 + 0.25 * 0.25)).abs() < f64::EPSILON);
8382

8483
// Test k-nearest neighbors
8584
let k_nearest = tree.nearest_n::<SquaredEuclidean>(&query_point, 2);
@@ -92,3 +91,32 @@ fn test_kdtree_with_fixed_string() {
9291
let within_results = tree.within::<SquaredEuclidean>(&query_point, radius);
9392
assert_eq!(within_results.len(), 2); // Should find "Origin" and "Point A"
9493
}
94+
95+
#[test]
96+
fn test_kdtree_with_empty_type_as_content() {
97+
// Create a new KdTree with 2D points (K=2) and () as the data type
98+
let mut tree: KdTree<f64, (), 2, 32, u32> = KdTree::new();
99+
100+
// Add some points with associated numeric IDs
101+
tree.add(&[0.0, 0.0], ());
102+
tree.add(&[1.0, 1.0], ());
103+
tree.add(&[2.0, 2.0], ());
104+
tree.add(&[-1.0, -1.0], ());
105+
106+
// Test nearest neighbor query
107+
let query_point = [0.5, 0.25];
108+
let nearest = tree.nearest_one_point::<SquaredEuclidean>(&query_point);
109+
110+
// The closest point should be [0.0, 0.0]
111+
assert!((nearest.0.distance - (0.5f64 * 0.5 + 0.25 * 0.25)).abs() < f64::EPSILON);
112+
assert_eq!(nearest.1, [0.0, 0.0]);
113+
114+
// Test k-nearest neighbors
115+
let k_nearest = tree.nearest_n::<SquaredEuclidean>(&query_point, 2);
116+
assert_eq!(k_nearest.len(), 2);
117+
118+
// Test within radius
119+
let radius = 2.0;
120+
let within_results = tree.within::<SquaredEuclidean>(&query_point, radius);
121+
assert_eq!(within_results.len(), 2); // Should find two points within radius
122+
}

0 commit comments

Comments
 (0)