File: uniq.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 (91 lines) | stat: -rw-r--r-- 1,858 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
BEGIN
{
    $INC{'List/MoreUtils.pm'} or *distinct = __PACKAGE__->can("uniq");
}

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

SCOPE:
{
    my @a = map { (1 .. 10) } 0 .. 1;
    my @u = uniq @a;
    is_deeply(\@u, [1 .. 10]);
    my $u = uniq @a;
    is(10, $u);
}

# Test aliases
SCOPE:
{
    my @a = map { (1 .. 10) } 0 .. 1;
    my @u = distinct @a;
    is_deeply(\@u, [1 .. 10]);
    my $u = distinct @a;
    is(10, $u);
}

# Test strings
SCOPE:
{
    my @a = map { ("a" .. "z") } 0 .. 1;
    my @u = uniq @a;
    is_deeply(\@u, ["a" .. "z"]);
    my $u = uniq @a;
    is(26, $u);
}

# Test mixing strings and numbers
SCOPE:
{
    my @a  = ((map { (1 .. 10) } 0 .. 1), (map { ("a" .. "z") } 0 .. 1));
    my $fa = freeze(\@a);
    my @u  = uniq map { $_ } @a;
    my $fu = freeze(\@u);
    is_deeply(\@u, [1 .. 10, "a" .. "z"]);
    is($fa, freeze(\@a));
    is($fu, freeze([1 .. 10, "a" .. "z"]));
    my $u = uniq @a;
    is(10 + 26, $u);
}

SCOPE:
{
    my @a;
    tie @a, "Tie::StdArray";
    @a = ((map { (1 .. 10) } 0 .. 1), (map { ("a" .. "z") } 0 .. 1));
    my @u = uniq @a;
    is_deeply(\@u, [1 .. 10, "a" .. "z"]);
    @a = ((map { (1 .. 10) } 0 .. 1), (map { ("a" .. "z") } 0 .. 1));
    my $u = uniq @a;
    is(10 + 26, $u);
}

SCOPE:
{
    my @foo  = ('a', 'b', '', undef, 'b', 'c', '');
    my @ufoo = ('a', 'b', '', undef, 'c');
    is_deeply([uniq @foo], \@ufoo, 'undef is supported correctly');
}

leak_free_ok(
    uniq => sub {
        my @a = map { (1 .. 1000) } 0 .. 1;
        my @u = uniq @a;
        uniq @a[1 .. 100];
    }
);

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

done_testing;