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

plan 4;

subtest "finds / deletes on simple int array" =>  {
    my int @a = 1,3,5,7,9;
    plan 3 * @a + 11;

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

    for @a.kv -> $i, $number {
        is finds(@a, $number, :cmp(&[<=>])), $i,
        "did we find '$number' using &[<=>]";
    }

    for 0, 0, 10, 5, 6, 3 -> $number, $pos {
        my $index := finds(@a,$number);
        nok $index.defined, "did the search for '$number' fail";
        is $index.Int, $pos, "did it give insert location $index";
    }

    is-deeply nexts(@a, @a[1]), @a[2], 'does nexts find the next';
    is-deeply nexts(@a, @a[4]), 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';

    for @a.clone.pick(*) {
        is-deeply deletes(@a,$_), $_, "did '$_' get deleted";
    }
    is +@a, 0, 'Did we delete all elements';
}

subtest "finds / deletes on all ints from this file" => {
    my int @a = $?FILE.IO.comb(/\d+/).sort(*.Int).squish.map: *.Int;
    plan 2 * @a + 1;

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

    for @a.clone.pick(*) {
        is-deeply deletes(@a,$_,:cmp(&[<=>])), $_,
        "did '$_' get deletedi with &[<=>]";
    }
    is +@a, 0, 'Did we delete all elements';
}

subtest "inserts on an empty int array" => {
    my int @a = 1,3,5,7,9;
    my int @b;

    plan 1;

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

subtest "inserts / deletes with associated array" => {
    my int @a = 1,3,5,7,9;
    my int @b;
    my int @o;

    plan @a + 5;

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

    for @a.pick(*) {
        is-deeply deletes(@b, $_, @o), $_,
          "did '$_' get deleted";
    }
    is +@b, 0, 'Did we delete all elements in primary';
    is +@o, 0, 'Did we delete all elements in secondary';

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

# vim: expandtab shiftwidth=4


