File: indexes.t

package info (click to toggle)
liblist-moreutils-xs-perl 0.430-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,116 kB
  • sloc: perl: 9,038; ansic: 159; makefile: 3
file content (75 lines) | stat: -rw-r--r-- 2,067 bytes parent folder | download | duplicates (3)
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
#!perl

use strict ("subs", "vars", "refs");
use warnings ("all");
use lib ("t/lib");
use List::MoreUtils::XS (":all");


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

my @x = indexes { $_ > 5 } (4 .. 9);
is_deeply(\@x, [2 .. 5], "indexes > 5 ...");
@x = indexes { $_ > 5 } (1 .. 4);
is_deeply(\@x, [], 'Got the null list');

my ($lr, @s, @n, @o, @e);
leak_free_ok(
    indexes => sub {
        $lr = 1;
        @s  = indexes { $_ > 5 } (4 .. 9);
        @n  = indexes { $_ > 5 } (1 .. 5);
        @o  = indexes { $_ & 1 } (10 .. 15);
        @e  = indexes { !($_ & 1) } (10 .. 15);
    }
);
$lr and is_deeply(\@s, [2 .. 5],  "indexes/leak: some");
$lr and is_deeply(\@n, [],        "indexes/leak: none");
$lr and is_deeply(\@o, [1, 3, 5], "indexes/leak: odd");
$lr and is_deeply(\@e, [0, 2, 4], "indexes/leak: even");

@n = map { $_ + 1 } @o = (0 .. 9);
@x = indexes { ++$_ > 7 } @o;
is_deeply(\@o, \@n,      "indexes behaves like grep on modified \$_");
is_deeply(\@x, [7 .. 9], "indexes/modify");

not_dying(
    'indexes_on_set' => sub {
        @x = indexes { ++$_ > 7 } (0 .. 9);
    }
);
is_deeply(\@x, [7 .. 9], "indexes/modify set");

leak_free_ok(
    indexes => sub {
        @s = indexes { grow_stack; $_ > 5 } (4 .. 9);
        @n = indexes { grow_stack; $_ > 5 } (1 .. 4);
        @o = indexes { grow_stack; $_ & 1 } (10 .. 15);
        @e = indexes { grow_stack; !($_ & 1) } (10 .. 15);
    },
    'indexes interrupted by exception' => sub {
        eval {
            @s = indexes { $_ > 10 and die "range exceeded"; $_ > 5 } (1 .. 15);
        };
    },
);

$lr and is_deeply(\@s, [2 .. 5],  "indexes/leak: some");
$lr and is_deeply(\@n, [],        "indexes/leak: none");
$lr and is_deeply(\@o, [1, 3, 5], "indexes/leak: odd");
$lr and is_deeply(\@e, [0, 2, 4], "indexes/leak: even");

my $have_scalar_util = eval { require Scalar::Util; 1 };

if ($have_scalar_util)
{
    my $ref = \(indexes(sub { 1 }, 123));
    Scalar::Util::weaken($ref);
    is($ref, undef, "weakened away");
}
is_dying('indexes without sub' => sub { &indexes(42, 4711); });

done_testing;