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

plan 31;

my constant $elems  = 200000;
my constant $batch  = 16;
my constant @list   = (^$elems).List;
my constant $mapper = { $_ => 2 * $_ }
my constant %mapped = @list.map: $mapper;

isa-ok @list.&hyperize, ParaSeq, 'did hyperize produce a ParaSeq';
isa-ok @list.&racify,   ParaSeq, 'did racify produce a ParaSeq';

ok &hyperize, 'was hyperize exported';
ok &racify,   'was racify exported';

for 1, ParaSeq.default-degree {
    is-deeply @list.&hyperize($batch, $_).Array, [@list],
      "did .Array produce ok with degree $_";
    is-deeply @list.&hyperize($batch, $_).Hash, @list.Hash,
      "did .Hash produce ok with degree $_";
    is-deeply @list.&hyperize($batch, $_).List, @list,
      "did .List produce ok with degree $_";
    is-deeply @list.&hyperize($batch, $_).Map, @list.Map,
      "did .Map produce ok with degree $_";
    is-deeply @list.&hyperize($batch, $_).Seq, @list.Seq,
      "did .Seq produce ok with degree $_";
    is-deeply @list.&hyperize($batch, $_).Slip, @list.Slip,
      "did .Slip produce ok with degree $_";

    is-deeply @list.&hyperize($batch, $_).serial, @list.serial,
      "did .serial produce ok with degree $_";

    my %m is Map = @list.&racify($batch, $_).map($mapper);
    is-deeply %m, %mapped,
      "did .map(mapper) produce ok with degree $_";
}

my $s := (^20).&hyperize.grep({ die "Bazinga" if .is-prime });
is-deeply $s.auto,  True, 'is :auto reported ok';
is-deeply $s.catch, True, 'is :catch reported ok';
is-deeply $s.List, (), 'no values where produced';

my $b := $s.exceptions;
is $b.elems, 1, 'did we get 1 type of exception';

ok $b.keys.head.starts-with("Bazinga"), 'did we get the right error message';
is $b.values.head, 8, 'did we get the right amount';

is ParaSeq.default-batch, 16,   "is the default batch size correct";
ParaSeq.default-batch = 1024;
is ParaSeq.default-batch, 1024, "is the updated default batch size correct";

is ParaSeq.default-degree, Kernel.cpu-cores-but-one,
  "is the default degree correct";
ParaSeq.default-degree = 3;
is ParaSeq.default-degree, 3, "is the updated default degree correct";

my atomicint $seen;
my $*FOO = "foo";
(^10).&hyperize(1).map: { ++⚛$seen if $*FOO eq 'foo' }
is $seen, 10, 'was the dynamic variable seen all times';

# vim: expandtab shiftwidth=4


