### ----------------------------------------------------
### -- Algorithm::KDimensionalTree
### -- Licenses: Artistic-2.0
### -- Authors: Anton Antonov
### -- File: t/01-basic-usage.rakutest
### ----------------------------------------------------
use v6.d;
use Test;

use lib <. lib>;

use Algorithm::KDimensionalTree;

plan *;

my @points = ([(^100).rand, (^100).rand] xx 100).unique;

## 1
my $kdTree1;
$kdTree1 = Algorithm::KDimensionalTree.new(@points);
isa-ok $kdTree1, Algorithm::KDimensionalTree:D;

## 2
isa-ok Algorithm::KDimensionalTree.new(points => (3.rand xx 30), distance-function => 'euclidean-distance'),  Algorithm::KDimensionalTree:D;

## 3
my @searchPoint1 = |@points.head;
is-deeply $kdTree1.k-nearest(@searchPoint1, 1, :!values).map(*<point>.value).Array, [@searchPoint1,];

## 4
is-deeply
        $kdTree1.k-nearest(@searchPoint1, 3, :!values).map(*<point>.value).Array,
        $kdTree1.k-nearest(@searchPoint1, 3, :values).Array,
        "Values adverb";

## 5
is-deeply
        $kdTree1.k-nearest(@searchPoint1, 5),
        $kdTree1.nearest-within-ball(@searchPoint1, 60, :!values).sort(*<distance>).map(*<point>.value)[^5],
        "Equivalence for k-nearest and within ball";

## 6
# Make verification test with direct scanning of @points.
my @nns6 = @points.sort({ sqrt [+] ($_.Array Z- @searchPoint1).map(* ** 2) });
is-deeply $kdTree1.k-nearest(@searchPoint1, 12), @nns6[^12];

## 7
my $kdTree7 = Algorithm::KDimensionalTree.new(points => (3.rand xx 30), distance-function => 'euclidean-distance');
isa-ok $kdTree7.k-nearest(2, 2), Iterable;

## 8
isa-ok $kdTree7.nearest-within-ball(2, 1), Iterable;

done-testing;


