### ----------------------------------------------------
### -- MergeOrderedSeqs
### -- Licenses: Artistic-2.0
### -- Authors: Fernando Corrêa
### -- File: t/01-basic.rakutest
### ----------------------------------------------------
use Test;
use MergeOrderedSeqs;

ok &merge-ordered-seqs;

lives-ok {
    my @a = 1, 3, 6, 9;
    my @b = 1, 2, 4, 8;
    my @c = 1, 5, 25, 255;

    is-deeply merge-ordered-seqs(@a), (1, 3, 6, 9);
    is-deeply merge-ordered-seqs(@a, @b), (1, 1, 2, 3, 4, 6, 8, 9);
    is-deeply merge-ordered-seqs(@a, @b, @c), (1, 1, 1, 2, 3, 4, 5, 6, 8, 9, 25, 255);
    is-deeply merge-ordered-seqs(@a.grep(* !%% 2), @b.grep(* !%% 2)), (1, 1, 3, 9);

    is-deeply merge-ordered-seqs(:before(More), (6, 4, 2), (5, 3, 1)), (6, 5, 4, 3, 2, 1);
    is-deeply merge-ordered-seqs(:before{(-2) ** $_}, 1, 2, 3, 4), (3, 1, 2, 4);
    is-deeply merge-ordered-seqs(:before{$^a > $^b}, 1, 2, 3, 4), (4, 3, 2, 1);

    my @d = lazy gather { take 1; take 2 }

    is-deeply merge-ordered-seqs(@a, @d), (1, 1, 2, 3, 6, 9);

    my $e = 0;
    my @f = lazy gather { $e = 1; take 1; $e = 2; take 2; $e = 3 }

    my $merged = merge-ordered-seqs([3, 4, 5], @f);
    my $i = $merged.iterator;

    is $e, 1;
    is $i.pull-one, 1;
    is $e, 2;
    is $i.pull-one, 2;
    is $e, 3;
    $i.push-all(my @rest);
    is-deeply @rest, [ 3, 4, 5 ];

    is-deeply merge-ordered-seqs(<b c d>, <a e i o u>), <a b c d e i o u>;
    is-deeply
      merge-ordered-seqs(
        [1, 3, 5].map({ Date.new: 2023, 1, $_ }),
        [2, 4, 6].map({ Date.new: 2023, 1, $_ }),
      ),
      [1, 2, 3, 4, 5, 6].map({ Date.new: 2023, 1, $_ })
    ;
    is-deeply
      merge-ordered-seqs(
        [1, 3, 5].map({ DateTime.new: year => 1981, month =>5, day => 21, hour => 0, minute => 0, second => $_ }),
        [2, 4, 6].map({ DateTime.new: year => 1981, month =>5, day => 21, hour => 0, minute => 0, second => $_ }),
      ),
      [1, 2, 3, 4, 5, 6].map({ DateTime.new: year => 1981, month =>5, day => 21, hour => 0, minute => 0, second => $_ })
    ;
}

done-testing;


