File: slideatatime.pm

package info (click to toggle)
liblist-moreutils-perl 0.430-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,228 kB
  • sloc: perl: 13,167; makefile: 17
file content (64 lines) | stat: -rw-r--r-- 1,338 bytes parent folder | download | duplicates (4)
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

use Test::More;
use Test::LMU;

local $" = " ";
my $it;
my @r;

my @x = ('a' .. 'g');
$it = slideatatime 3, 3, @x;
while (my @vals = $it->())
{
    push @r, "@vals";
}
is(is_deeply(\@r, ['a b c', 'd e f', 'g']), 1, "slideatatime as natatime with 3 elements");

$it = slideatatime 2, 3, @x;
@r  = ();
while (my @vals = $it->())
{
    push @r, "@vals";
}
is(is_deeply(\@r, ['a b c', 'c d e', 'e f g', 'g']), 1, "slideatatime moving 3 elements by 2 items");

$it = slideatatime 1, 3, @x;
@r  = ();
while (my @vals = $it->())
{
    push @r, "@vals";
}
is(is_deeply(\@r, ['a b c', 'b c d', 'c d e', 'd e f', 'e f g', 'f g', 'g']), 1, "slideatatime moving 3 elements by 1 item");

my @a = (1 .. 1000);
$it = slideatatime 1, 1, @a;
@r  = ();
while (my @vals = &$it)
{
    push @r, @vals;
}
is(is_deeply(\@r, \@a), 1, "slideatatime as natatime with 1 element");

leak_free_ok(
    slideatatime => sub {
        my @y  = 1;
        my $it = slideatatime 2, 2, @y;
        while (my @vals = $it->())
        {
            # do nothing
        }
    },
    'slideatatime with exception' => sub {
        my @r;
        eval {
            my $it = slideatatime 1, 3, @x;
            while (my @vals = $it->())
            {
                scalar @vals == 3 or die;
                push @r, "@vals";
            }
        };
    }
);

done_testing;