Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: e630836923
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 113 lines (94 sloc) 2.139 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
use v6;

use Test;
# L<S04/"Loop statements"/redo>
plan 10;

{
    my $i = 0;
    while (defined($i)) { if (++$i < 3) { redo }; last }
    is($i, 3, "redo caused reapplication of block");
}

{
    my @log;
    my $i;
    while ++$i < 5 {
        push @log, "before";
        if (++$i == 2) {
            redo;
        } else {
            push @log, "no_redo";
        }
        push @log, "after";
    }
    
    is(~@log, "before before no_redo after before no_redo after", "statements after redo are not executed");
}

{
    my $i = 0;
    my $j = 0;

    for (1, 0) -> $x {
        if ($x && (++$i % 2 == 0)) { redo };
        $j++;
    }

    is($j, 2, '$j++ encountered twice');
    is($i, 1, '$i++ encountered once');
}


{
    my $i = 0;
    my $j = 0;

    for (1, 0, 1, 0) -> $x {
        if ($x && (++$i % 2 == 0)) { redo };
        $j++;
    }

    is($j, 4, '$j++ encountered four times');
    is($i, 3, '$i++ encountered three times');
}


{
    my $i = 0;
    my $j;

    loop ($j = 0; $j < 4; $j++) {
        if ($j % 2 == 0 and $i++ % 2 == 0) { redo }
        $i-=2;
    }

    is($j, 4, '$j unaltered by the fiasco');
    is($i, -4, '$i incremented and decremented correct number of times');
}

{
    # rubicon TestLoopStuff.rb
    # def testRedoWithFor
    # sum = 0
    # for i in 1..10
    # sum += i
    # i -= 1
    # if i > 0
    # redo
    # end
    # end
    # assert_equal(220, sum)
    # end
    my $stopping = 100;
    my $sum = 0;
    for 1..10 -> $i is copy {
        $sum += $i;
        $i -= 1;
        last if !$stopping--;
        if $i > 0 {
            redo
        }
    }
    say $sum;
    # pugs, rakudo and perl5 independently agree that this should be
    # 201, not 220 as the ruby example says.
    # that's because the ruby example doesn't have the 'is copy' trait.
    is($sum, 201, "testRedoWithFor");

    $stopping = 100;
    $sum = 0;
    my $j = 1;
    my $i;
    while do {$i = $j; $j++ <= 10} {
    $sum += $i;
    $i -= 1;
    last if !$stopping--;
    if $i > 0 { redo }
    }
    is($sum, 220, "test redo with while");
}

# vim: ft=perl6
Something went wrong with that request. Please try again.