### ----------------------------------------------------
### -- Iter::Able
### -- Licenses: Artistic-2.0
### -- Authors: 
### -- File: t/18-is-all-same.rakutest
### ----------------------------------------------------
use Test;
use Iter::Able::Is-All-Same;

my &id = -> \g, \e, Str $desc? { g | e ~~ Seq:D ?? cmp-ok(g, "eqv", e, $desc) !! is-deeply(g, e, $desc) };

plan 27;

my $s;
$s := [1, 2, 1];
id is-all-same($s), False, "Differing list";

$s := (1,);
id $s.&is-all-same, True, "All-the-same list, length 1";

$s := (1, 1);
id $s.&is-all-same, True, "All-the-same list, length 2";

$s := 1 xx 777;
id $s.&is-all-same, True, "All-the-same list, length > 2";

$s := ["yes", "and", "for"];
id is-all-same($s, :as(&chars)), True, "Using :\&as";

$s := [$(3), $(3)];
id is-all-same($s, :with(&[=:=])), False, "Using :\&with";

$s := [7, -7.000000000000000000001];
id is-all-same($s, as => &abs, with => &[=~=]), True, "Using :\&as and :\&with";

$s := [];
id is-all-same($s), True,  "Empty list";

my $t = ();
id $t.&is-all-same, True,  "Empty itemized list";

$s := [1, |(0 xx 100_000)].Seq;
id is-all-same($s), False, "Long list to see shortcircuiting";

$s := "";
id is-all-same($s), True, "Empty string";

$s := "z";
id is-all-same($s), True, "Length-1 string";

$s := "Real String";
id is-all-same($s), False, "Differing string ";

$s := "aa";
id is-all-same($s), True, "All-the-same string, length 2";

$s := "a" x 84;
id is-all-same($s), True, "All-the-same string, length > 2";

$s := "aaAaA";
id $s.&is-all-same, False, "Same character different cases";
id $s.&is-all-same(:as(&fc)), True, "Same character different cases but :as(\&fc)";

my \it = [12, 3, 4, 4, 4].iterator;
it.pull-one for ^2;
id is-all-same(it), True, "Iterator (all the same)";
ok it.pull-one =:= IterationEnd, "Iterator (exhausted)";

my \it2 = [12, 3, 4, pi, 4].iterator;
it2.pull-one for ^2;
id is-all-same(it2), False, "Iterator (not all the same)";
nok it2.pull-one =:= IterationEnd, "Iterator (not exhausted because of short-circuiting)";

my \it-as = [12, 3, 4, 5, 7].iterator;
it-as.pull-one for ^3;
id is-all-same(it-as, :as{.is-prime}), True, "Iterator (as)";
ok it-as.pull-one =:= IterationEnd, "Iterator (exhausted)";

my \it-with = [12, 3, 4, (my $a = 5), (my $b = 5)].iterator;
it-with.pull-one for ^3;
id is-all-same(it-with, :with(&[=:=])), False, "Iterator (with)";
ok it-with.pull-one =:= IterationEnd, "Iterator (exhausted; would not be, but the differer is the last element)";

my \it-withas = [12, 3, 4, -5, 5.00000000000001].iterator;
it-withas.pull-one for ^3;
id do { my $*TOLERANCE = 1e-5; is-all-same(it-withas, :as{.abs}, :with(&[=~=])) }, True, "Iterator (with and as)";
ok it-withas.pull-one =:= IterationEnd, "Iterator (exhausted)";

done-testing;


