File: singleton.pm

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 (97 lines) | stat: -rw-r--r-- 2,302 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
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

use Test::More;
use Test::LMU;
use Tie::Array ();

SCOPE:
{
    my @s = (1001 .. 1200);
    my @d = map { (1 .. 1000) } 0 .. 1;
    my @a = (@d, @s);
    my @u = singleton @a;
    is_deeply(\@u, [@s]);
    my $u = singleton @a;
    is(200, $u);
}

# Test strings
SCOPE:
{
    my @s = ("AA" .. "ZZ");
    my @d = map { ("aa" .. "zz") } 0 .. 1;
    my @a = (@d, @s);
    my @u = singleton @a;
    is_deeply(\@u, [@s]);
    my $u = singleton @a;
    is(scalar @s, $u);
}

# Test mixing strings and numbers
SCOPE:
{
    my @s  = (1001 .. 1200, "AA" .. "ZZ");
    my $fs = freeze(\@s);
    my @d  = map { (1 .. 1000, "aa" .. "zz") } 0 .. 1;
    my @a  = (@d, @s);
    my $fa = freeze(\@a);
    my @u  = singleton map { $_ } @a;
    my $fu = freeze(\@u);
    is_deeply(\@u, [@s]);
    is($fs, freeze(\@s));
    is($fa, freeze(\@a));
    is($fu, $fs);
    my $u = singleton @a;
    is(scalar @s, $u);
}

SCOPE:
{
    my @a;
    tie @a, "Tie::StdArray";
    my @s = (1001 .. 1200, "AA" .. "ZZ");
    my @d = map { (1 .. 1000, "aa" .. "zz") } 0 .. 1;
    @a = (@d, @s);
    my @u = singleton map { $_ } @a;
    is_deeply(\@u, [@s]);
    @a = (@d, @s);
    my $u = singleton @a;
    is(scalar @s, $u);
}

SCOPE:
{
    my @foo  = ('a', 'b',   '', undef, 'b', 'c', '');
    my @sfoo = ('a', undef, 'c');
    is_deeply([singleton @foo], \@sfoo, 'one undef is supported correctly by singleton');
    @foo  = ('a', 'b', '', undef, 'b', 'c', undef);
    @sfoo = ('a', '',  'c');
    is_deeply([singleton @foo], \@sfoo, 'twice undef is supported correctly by singleton');
    is((scalar singleton @foo), scalar @sfoo, 'scalar twice undef is supported correctly by singleton');
}

leak_free_ok(
    singleton => sub {
        my @s = (1001 .. 1200, "AA" .. "ZZ");
        my @d = map { (1 .. 1000, "aa" .. "zz") } 0 .. 1;
        my @a = (@d, @s);
        my @u = singleton @a;
        scalar singleton @a;
    }
);

# This test (and the associated fix) are from Kevin Ryde; see RT#49796
leak_free_ok(
    'singleton with exception in overloading stringify',
    sub {
        eval {
            my $obj = DieOnStringify->new;
            my @u   = singleton $obj, $obj;
        };
        eval {
            my $obj = DieOnStringify->new;
            my $u   = singleton $obj, $obj;
        };
    }
);

done_testing;