Skip to content

Commit

Permalink
add unit test for balltree
Browse files Browse the repository at this point in the history
  • Loading branch information
minshao authored and msk committed Apr 18, 2022
1 parent 7f38759 commit e55fc70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
27 changes: 27 additions & 0 deletions src/ball_tree.rs
Expand Up @@ -577,6 +577,23 @@ mod test {
tree.query_nearest(&point);
}

#[test]
#[should_panic]
fn ball_tree_column_base() {
let array = array![[1., 1.], [1., 1.1], [9., 9.]];
let fortran = array.reversed_axes();
let _ = BallTree::euclidean(fortran).expect("`array` should not be empty");
}

#[test]
fn ball_tree_metric() {
let array = array![[1., 1.], [1., 1.1], [9., 9.]];
let tree = BallTree::new(array.clone(), Euclidean::default())
.expect("`array` should not be empty");
let tree1 = BallTree::euclidean(array).expect("`array` should not be empty");
assert_eq!(tree.metric, tree1.metric);
}

#[test]
fn ball_tree_3() {
let array = array![[1., 1.], [1., 1.1], [9., 9.]];
Expand All @@ -598,6 +615,12 @@ mod test {
neighbors.sort_unstable();
assert_eq!(neighbors, &[0, 1]);

let neighbors = tree.nearest_neighbor_in_subtree(&aview1(&[20., 20.]), 0, 1.);
assert_eq!(neighbors, None);

let neighbors = tree.query_radius(&aview1(&[20., 20.]), 1.);
assert_eq!(neighbors, &[]);

let point = aview1(&[1.1, 1.2]);
let neighbor = tree.query_nearest(&point);
assert_eq!(neighbor.0, 1);
Expand Down Expand Up @@ -658,6 +681,10 @@ mod test {
let point = aview1(&[1., 2.]);
let neighbor = tree.query_nearest(&point);
assert!(approx::abs_diff_eq!(neighbor.1, 1_f64.sqrt()));

let point = aview1(&[1., 1.]);
let neighbor = tree.query_nearest(&point);
assert!(approx::abs_diff_eq!(neighbor.1, 0_f64.sqrt()));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/distance.rs
Expand Up @@ -9,7 +9,7 @@ pub trait Metric<A> {
fn distance(&self, _: &ArrayView1<A>, _: &ArrayView1<A>) -> A;
}

#[derive(Default, Clone)]
#[derive(Default, Clone, Debug, PartialEq)]
pub struct Euclidean {}

unsafe impl Sync for Euclidean {}
Expand Down

0 comments on commit e55fc70

Please sign in to comment.