Skip to content

Commit

Permalink
Add fuzzy test and Readme; solves #18
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenautumns committed Nov 29, 2021
1 parent 4fd7cef commit 92ad12b
Show file tree
Hide file tree
Showing 8 changed files with 442 additions and 17 deletions.
8 changes: 8 additions & 0 deletions kd_tree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# KD-Tree implementation

Implements only functions required for searching an already initialized KD-Tree.
Because of this, this crate is no_std compatible

Code for building the KD-Tree can be found in `kd_tree_sort`.
`kd_tree_sort` is not no_std compatible which is why these two crates were seperated.
A fuzzy tester as well as a benchmark verifying this implementation can be found in `kd_tree_sort`.
3 changes: 3 additions & 0 deletions kd_tree_sort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ rand_pcg = "*"
itertools = "0.10"
criterion = "0.3"

[lib]
bench = false

[[bench]]
name = "search"
harness = false
19 changes: 19 additions & 0 deletions kd_tree_sort/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# KD-Tree sort implemention

Implements a non-no_std compatible sort function for usage with the `kd_tree` crate.

## Benchmark
For benchmarking purposed, criterion.rs was used, comparing our kd-tree against a basic linear_search.

Run: `cargo bench` in this directory.

## Fuzzy Test
The implemented fuzzy test uses `cargo fuzz`
Install it before running the fuzzy test (a nightly toolchain may be required).

It compares the result of the linear search algorithm against the result of the kd-tree search.
The fuzzy test input is filtering out `Inf` and `NaN` values since our kd-tree is
supposed to find the nearest-neighbor on valid input data only.

For starting the fuzzy test run: `cargo fuzz run kd_tree_search --sanitizer=leak` in this directory.
*Note: the fuzzy test will run until it finds an error*
40 changes: 23 additions & 17 deletions kd_tree_sort/benches/search.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::time::Duration;
use criterion::{criterion_group, criterion_main, Criterion, BenchmarkId, BatchSize};
use criterion::{criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
use kd_tree::{euclid, Node, Tree};
use rand::Rng;
use kd_tree_sort::sort;
use rand::Rng;
use std::time::Duration;

type Prng = rand_pcg::Mcg128Xsl64;

Expand All @@ -15,17 +15,15 @@ fn linear_search(nodes: &[Node<f64, i32, 3>], point: &[f64; 3]) -> f64 {
}

fn generate_values(len: usize, rng: &mut Prng) -> Vec<([f64; 3], i32)> {
(0..len)
.map(|_| (generate_point(rng), 0))
.collect()
(0..len).map(|_| (generate_point(rng), 0)).collect()
}

fn generate_sorted(len: usize, rng: &mut Prng) -> Vec<Node<f64, i32, 3>>{
fn generate_sorted(len: usize, rng: &mut Prng) -> Vec<Node<f64, i32, 3>> {
let sorted = sort(generate_values(len, rng));
sorted.iter().map(|(p, v)| Node::new(*p, *v)).collect()
}

fn generate_point(rng: &mut Prng) -> [f64; 3]{
fn generate_point(rng: &mut Prng) -> [f64; 3] {
[rng.gen(), rng.gen(), rng.gen()]
}

Expand All @@ -41,19 +39,27 @@ pub fn search(c: &mut Criterion) {
group.warm_up_time(Duration::from_millis(50));
group.measurement_time(Duration::from_secs(1));

for len in (START_LEN..END_LEN).step_by(1000){
for len in (START_LEN..END_LEN).step_by(1000) {
let nodes = generate_sorted(len, &mut rng);
let tree = Tree::<f64, i32, 3, MAX_LEVEL> {
nodes: nodes.as_slice()
nodes: nodes.as_slice(),
};
group.bench_with_input(BenchmarkId::new("KD Tree", len),
&tree, |b, t|
b.iter_batched(|| generate_point(&mut rng),|p| t.search(&p), BatchSize::SmallInput));
group.bench_with_input(BenchmarkId::new("Linear", len),
&nodes, |b, n|
b.iter_batched(|| generate_point(&mut rng),|p| linear_search(n, &p), BatchSize::SmallInput));
group.bench_with_input(BenchmarkId::new("KD Tree", len), &tree, |b, t| {
b.iter_batched(
|| generate_point(&mut rng),
|p| t.search(&p),
BatchSize::SmallInput,
)
});
group.bench_with_input(BenchmarkId::new("Linear", len), &nodes, |b, n| {
b.iter_batched(
|| generate_point(&mut rng),
|p| linear_search(n, &p),
BatchSize::SmallInput,
)
});
}
}

criterion_group!(benches, search);
criterion_main!(benches);
criterion_main!(benches);
3 changes: 3 additions & 0 deletions kd_tree_sort/fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target
corpus
artifacts
Loading

0 comments on commit 92ad12b

Please sign in to comment.