### ----------------------------------------------------
### -- Array::Sorted::Util
### -- Licenses: Artistic-2.0
### -- Authors: Elizabeth Mattijsen
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Test;
use Array::Sorted::Util;

plan 3;

my @instants;
for ^10 {
    @instants.push(now);
    sleep .1;
}

subtest "finds on simple Instant array" =>  {
    my @a = @instants;
    plan @a + 6;

    for @a.kv -> $i, $instant {
        is finds(@a, $instant), $i, "did we find '$instant'";
    }

    for Instant.from-posix(0), Instant.from-posix(time + 100) {
        nok finds(@a, $_).defined, "did we not find '$_'";
    }

    is-deeply nexts(@a, @a[1]), @a[2], 'does nexts find the next';
    is-deeply nexts(@a, @a[9]), Nil,   'does nexts handles end ok';

    is-deeply prevs(@a, @a[1]), @a[0], 'does prevs find the previous';
    is-deeply prevs(@a, @a[0]), Nil,   'does prevs handles end ok';
}

subtest "inserts on an empty array" => {
    my @a = @instants;
    my @b;

    plan @a + 2;

    for @a.kv -> $i, $value {
        is inserts(@b,$value).Int, $i, "inserted ok at $i";
    }
    is-deeply @b, @a, "did we get a correctly sorted array (1)";

    inserts(@b,$_) for @a.pick(*);
    is-deeply @b, @a, "did we get a correctly sorted array (2)";
}

subtest "deletes on an array" => {
    my @a = @instants;

    plan @a + 2;

    for @instants.pick(*) {
        is-deeply deletes(@a,$_), $_, "did '$_' get deleted";
    }
    is +@a, 0, "did we empty out the array";

    for @instants.pick(*) -> $instant {
        inserts(@a,$instant,:pos($_)) without finds(@a, $instant);
    }
    is-deeply @a, @instants, "did we get a correctly sorted array (3)";
}

# vim: expandtab shiftwidth=4


