Skip to content

Commit

Permalink
[v6.d REVIEW] Rewrite range iterator tests using helper sub
Browse files Browse the repository at this point in the history
- Simplify code by using the iterator helper sub, which tests
    all the values as well as all the optimization methods, if
    they are implemented. It also tests types of the pulled values
- Remove fudges for optional optimization methods. In some of the
    cases they're not even implementable due to precision of Num
    Orig: 3ceb07840
- Remove pull-after-IterationEnd tests entirely, as pulling in
    that case is a violation of the Iterator protocol
- Swap `is` to `is-deeply` to test actual types instead of their
    stringy representation (do a bit of .succ'ing for Nums to
    avoid potential precision differences)
  • Loading branch information
zoffixznet committed Jul 15, 2018
1 parent 6402f02 commit ab6ade4
Showing 1 changed file with 48 additions and 106 deletions.
154 changes: 48 additions & 106 deletions S07-iterators/range-iterator.t
@@ -1,103 +1,73 @@
use v6;
use lib $?FILE.IO.parent(2).add("packages");
use Test;

plan 108;

{
my $r = (1..5).iterator;
does-ok $r, Iterator, '$r is an Iterator';
is $r.count-only, 5, '$r.count-only works';
is $r.pull-one, 1, '$r.pull-one == 1 and Range Iterator kept place';
is $r.pull-one, 2, '$r.pull-one == 2';
is $r.pull-one, 3, '$r.pull-one == 3';
is $r.pull-one, 4, '$r.pull-one == 4';
is $r.pull-one, 5, '$r.pull-one == 5';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is done';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is still done';
}

{
my $r = (-1.5.Num..^3).iterator;
does-ok $r, Iterator, '$r is an Iterator';
#?rakudo skip "Method 'count-only' not found for invocant of class"
is $r.count-only, 5, '$r.count-only works';
is $r.pull-one, -1.5, '$r.pull-one == -1.5 and Range Iterator kept place';
is $r.pull-one, -.5, '$r.pull-one == -0.5';
is $r.pull-one, .5, '$r.pull-one == .5';
is $r.pull-one, 1.5, '$r.pull-one == 1.5';
is $r.pull-one, 2.5, '$r.pull-one == 2.5';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is done';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is still done';
}

{
my $r = (-1.5..^3).iterator;
does-ok $r, Iterator, '$r is an Iterator';
#?rakudo skip "Method 'count-only' not found for invocant of class"
is $r.count-only, 5, '$r.count-only works';
is $r.pull-one, -1.5, '$r.pull-one == -1.5 and Range Iterator kept place';
is $r.pull-one, -.5, '$r.pull-one == -0.5';
is $r.pull-one, .5, '$r.pull-one == .5';
is $r.pull-one, 1.5, '$r.pull-one == 1.5';
is $r.pull-one, 2.5, '$r.pull-one == 2.5';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is done';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is still done';
}

{
my $r = (-1.5.Num^..3).iterator;
does-ok $r, Iterator, '$r is an Iterator';
#?rakudo skip "Method 'count-only' not found for invocant of class"
is $r.count-only, 4, '$r.count-only works';
is $r.pull-one, -.5, '$r.pull-one == -0.5 and Range Iterator kept place';
is $r.pull-one, .5, '$r.pull-one == .5';
is $r.pull-one, 1.5, '$r.pull-one == 1.5';
is $r.pull-one, 2.5, '$r.pull-one == 2.5';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is done';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is still done';
}
use Test::Util;

plan 61;

test-iter-opt (1..5).iterator,
(1, 2, 3, 4, 5), '1..5';
test-iter-opt (-1.5.Num..^3).iterator, (
(-1.5).Num, (-1.5).Num.succ, (-1.5).Num.succ.succ,
(-1.5).Num.succ.succ.succ, (-1.5).Num.succ.succ.succ.succ), '-1.5.Num..^3';
test-iter-opt (-1.5..^3).iterator,
(-1.5, -½, ½, 1.5, 2.5), '-1.5..^3';
test-iter-opt (-1.5.Num^..3).iterator, (
(-1.5).Num.succ, (-1.5).Num.succ.succ, (-1.5).Num.succ.succ.succ,
(-1.5).Num.succ.succ.succ.succ), '-1.5.Num^..3';
test-iter-opt ('d'..'g').iterator,
<d e f g>, 'd'..'g';
test-iter-opt (0..'50').iterator,
(050), 0..'50';

{
my $r = (-1..*).iterator;
does-ok $r, Iterator, '$r is an Iterator';
ok $r.is-lazy, '$r.is-lazy works';
is $r.pull-one, -1, '$r.pull-one == -1 and Range Iterator kept place';
is $r.pull-one, 0, '$r.pull-one == 0';
is $r.pull-one, 1, '$r.pull-one == 1';
is $r.pull-one, 2, '$r.pull-one == 2';
is $r.pull-one, 3, '$r.pull-one == 3';
is $r.pull-one, 4, '$r.pull-one == 4';
is $r.pull-one, 5, '$r.pull-one == 5';
is-deeply $r.pull-one, -1,
'$r.pull-one == -1 and Range Iterator kept place';
is-deeply $r.pull-one, 0, '$r.pull-one == 0';
is-deeply $r.pull-one, 1, '$r.pull-one == 1';
is-deeply $r.pull-one, 2, '$r.pull-one == 2';
is-deeply $r.pull-one, 3, '$r.pull-one == 3';
is-deeply $r.pull-one, 4, '$r.pull-one == 4';
is-deeply $r.pull-one, 5, '$r.pull-one == 5';
loop (my $i = 0; $i < 100; $i++) {
$r.pull-one; # 6 through 105
}
is $r.pull-one, 106, '$r.pull-one == 106';
is-deeply $r.pull-one, 106, '$r.pull-one == 106';
}

{
my $r = (-1.5.Num..*).iterator;
does-ok $r, Iterator, '$r is an Iterator';
ok $r.is-lazy, '$r.is-lazy works';
is $r.pull-one, -1.5, '$r.pull-one == -1.5 and Range Iterator kept place';
is $r.pull-one, -.5, '$r.pull-one == -0.5';
is $r.pull-one, .5, '$r.pull-one == .5';
is $r.pull-one, 1.5, '$r.pull-one == 1.5';
is $r.pull-one, 2.5, '$r.pull-one == 2.5';
is $r.pull-one, 3.5, '$r.pull-one == 3.5';
is $r.pull-one, 4.5, '$r.pull-one == 4.5';

is-deeply $r.pull-one, Num(-1.5),
'$r.pull-one == -1.5 and Range Iterator kept place';
is-deeply $r.pull-one, Num(-1.5).succ, '$r.pull-one == -0.5e0';
is-deeply $r.pull-one, Num(-1.5).succ.succ, '$r.pull-one == .5e0';
is-deeply $r.pull-one, Num(-1.5).succ.succ.succ, '$r.pull-one == 1.5e0';
is-deeply $r.pull-one, Num(-1.5).succ.succ.succ.succ,
'$r.pull-one == 2.5e0';
is-deeply $r.pull-one, Num(-1.5).succ.succ.succ.succ.succ,
'$r.pull-one == 3.5e0';
is-deeply $r.pull-one, Num(-1.5).succ.succ.succ.succ.succ.succ,
'$r.pull-one == 4.5e0';
}

{
my $r = (-1.5..*).iterator;
does-ok $r, Iterator, '$r is an Iterator';
ok $r.is-lazy, '$r.is-lazy works';
is $r.pull-one, -1.5, '$r.pull-one == -1.5 and Range Iterator kept place';
is $r.pull-one, -.5, '$r.pull-one == -0.5';
is $r.pull-one, .5, '$r.pull-one == .5';
is $r.pull-one, 1.5, '$r.pull-one == 1.5';
is $r.pull-one, 2.5, '$r.pull-one == 2.5';
is $r.pull-one, 3.5, '$r.pull-one == 3.5';
is $r.pull-one, 4.5, '$r.pull-one == 4.5';
is-deeply $r.pull-one, -1.5,
'$r.pull-one == -1.5 and Range Iterator kept place';
is-deeply $r.pull-one, -.5, '$r.pull-one == -0.5';
is-deeply $r.pull-one, .5, '$r.pull-one == .5';
is-deeply $r.pull-one, 1.5, '$r.pull-one == 1.5';
is-deeply $r.pull-one, 2.5, '$r.pull-one == 2.5';
is-deeply $r.pull-one, 3.5, '$r.pull-one == 3.5';
is-deeply $r.pull-one, 4.5, '$r.pull-one == 4.5';
}

{
Expand All @@ -124,19 +94,6 @@ plan 108;
is $r2.pull-one, 48, '$r2.pull-one == 48';
}

{
my $r = ('d'..'g').iterator;
does-ok $r, Iterator, '$r is an Iterator';
#?rakudo skip "Method 'count-only' not found for invocant of class"
is $r.count-only, 4, '$r.count-only works';
is $r.pull-one, 'd', '$r.pull-one == d and Range Iterator kept place';
is $r.pull-one, 'e', '$r.pull-one == e';
is $r.pull-one, 'f', '$r.pull-one == f';
is $r.pull-one, 'g', '$r.pull-one == g';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is done';
is $r.pull-one.WHICH, IterationEnd.WHICH, '$r.pull-one is still done';
}

{
my $r = ('d'..*).iterator;
does-ok $r, Iterator, '$r is an Iterator';
Expand All @@ -149,21 +106,6 @@ plan 108;
is $r.pull-one, 'i', '$r.pull-one == i';
}

{
my $r = (0..'50').iterator;
does-ok $r, Iterator, '$r is an Iterator';
is $r.pull-one, 0, '$r.pull-one == 0';
is $r.pull-one, 1, '$r.pull-one == 1';
is $r.pull-one, 2, '$r.pull-one == 2';
is $r.pull-one, 3, '$r.pull-one == 3';
#?rakudo skip "Method 'count-only' not found for invocant of class"
is $r.count-only, 47, '$r.count-only works partially through';
is $r.pull-one, 4, '$r.pull-one == 4 and Range Iterator kept place';
is $r.pull-one, 5, '$r.pull-one == 5';
is $r.pull-one, 6, '$r.pull-one == 6';
is $r.pull-one, 7, '$r.pull-one == 7';
}

subtest 'Iterator.skip-one' => {
plan 6;

Expand Down

0 comments on commit ab6ade4

Please sign in to comment.