File: 01_enlist.t

package info (click to toggle)
libperlude-perl 0.61-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 360 kB
  • sloc: perl: 903; makefile: 2
file content (121 lines) | stat: -rw-r--r-- 2,460 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
use strict;
use warnings;
use 5.10.0;
use Test::More;
use Perlude::Lazy;

plan tests => my $tests;

# very basic test
BEGIN { $tests += 1 }
my $l = enlist { state $n; $n++ };
is( ref $l, 'CODE', 'enlist returns a coderef' );

# check some values
BEGIN { $tests += 3 }
my @v;
( $l, @v ) = $l->();
is_deeply( \@v, [0], 'first item' );
( $l, @v ) = $l->(1);    # peek at the next value
is_deeply( \@v, [1], 'peek at the next item' );
( $l, @v ) = $l->();
is_deeply( \@v, [1], 'next item' );

# peek at a lot of values at once
BEGIN { $tests += 3 }
my $n = int 1000 * rand;
( $l, @v ) = $l->($n);
is( scalar @v, $n, "Peeked at $n items" );
is( $v[-1], 1 + $n, "Last item is " . ( $n + 1 ) );
( $l, @v ) = $l->();
is_deeply( \@v, [2], 'next item' );

# corner cases
BEGIN { $tests += 4 }
( $l, @v ) = $l->(0);
is( scalar @v, 0, 'peek at nothing' );
( $l, @v ) = $l->();
is_deeply( \@v, [3], 'next item' );

( $l, @v ) = $l->(-1);
is( scalar @v, 0, 'peek at nothing (-1)' );
( $l, @v ) = $l->();
is_deeply( \@v, [4], 'next item' );

# bounded list
BEGIN { $tests += 4 }
$l = unfold 0 .. 5;
( $l, @v ) = $l->(6);
is( scalar @v, 6, 'peek at the remaining items' );
( $l, @v ) = $l->();
is_deeply( \@v, [0], 'next item' );
( $l, @v ) = $l->(7);
is( scalar @v, 5, 'peek at more than the remaining total' );
( $l, @v ) = $l->();
is_deeply( \@v, [1], 'next item' );

my @tests;
BEGIN {
@tests = (
    [
        (unfold 0..5),
        [6], [0..5],
        [],  [0],
        [],  [1],
        [6], [2..5],
        [],  [2],
        [],  [3],
        [],  [4],
        [9], [5],
        [],  [5],
        [9], [],
        [],  [],
    ],
    [
        (unfold 0..1),
        [1], [0],
        [2], [0..1],
        [2], [0..1],
        [],  [0],
        [2], [1],
        [],  [1],
        [2], [],
        [],  [],
    ],
    [
        (unfold 0..3),
        [2], [0..1],
        [],  [0],
        [1], [1],
        [],  [1],
        [2], [2..3],
        [],  [2],
        [],  [3],
        [1],  [],
        [],  [],
    ],
    [
        Perlude::Lazy::NIL,
        [],  [],
    ],
);

$tests += @$_ for @tests;
}

my $m = 1;
while (@tests) {
    my $t = shift @tests;
    my $l = shift @$t;
    my $n = 1;
    while (@$t) {
        ($l, my @v) = $l->(@{ shift @$t });
        is_deeply \@v, (shift @$t), "test $m,$n";
        ($l,    @v) = $l->(0);
        is_deeply \@v, [], "test $m,$n: peek 0";
        $n++;
    };
    is $l, Perlude::Lazy::NIL, "end $m";
    $m++;
}