File: frequency.t

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 (128 lines) | stat: -rw-r--r-- 3,992 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
122
123
124
125
126
127
128
#!perl

use strict ("subs", "vars", "refs");
use warnings ("all");
BEGIN { $ENV{LIST_MOREUTILS_PP} = 1; }
END { delete $ENV{LIST_MOREUTILS_PP} } # for VMS
use lib ("t/lib");
use List::MoreUtils (":all");


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

SCOPE:
{
    my @s  = (1001 .. 1200);
    my @d  = (1 .. 1000);
    my @a  = (@d, @s, @d);
    my %e  = ((map { $_ => 2 } @d), map { $_ => 1 } @s);
    my $fa = freeze(\@a);
    my %f  = frequency @a;
    is($fa, freeze(\@a), "frequency:G_ARRAY leaves numbers untouched");
    is_deeply(\%f, {%e}, "frequency of numbers");
    my $f = frequency @a;
    is($fa,            freeze(\@a), "frequency:G:SCALAR leaves numbers untouched");
    is(scalar keys %e, $f,          "scalar result of frequency of numbers");
}

# Test strings
SCOPE:
{
    my @s  = ("AA" .. "ZZ");
    my @d  = ("aa" .. "zz");
    my @a  = (@d, @s, @d);
    my $fa = freeze(\@a);
    my %e  = ((map { $_ => 2 } @d), map { $_ => 1 } @s);
    my %f  = frequency @a;
    is($fa, freeze(\@a), "frequency:G_ARRAY leaves strings untouched");
    is_deeply(\%f, {%e}, "frequency of strings");
    my $f = frequency @a;
    is($fa,            freeze(\@a), "frequency:G_SCALAR leaves strings untouched");
    is(scalar keys %e, $f,          "scalar result of frequency of strings");
}

# Test mixing strings and numbers
SCOPE:
{
    my @s  = (1001 .. 1200, "AA" .. "ZZ");
    my @d  = (1 .. 1000,    "aa" .. "zz");
    my @a  = (@d, @s, @d);
    my %e  = ((map { $_ => 2 } @d), map { $_ => 1 } @s);
    my $fa = freeze(\@a);
    my %f  = frequency @a;
    is($fa, freeze(\@a), "frequency:G_ARRAY leaves number/strings mixture untouched");
    is_deeply(\%f, {%e}, "frequency of number/strings mixture");
    my $f = frequency @a;
    is($fa,            freeze(\@a), "frequency:G_SCALAR leaves number/strings mixture untouched");
    is(scalar keys %e, $f,          "scalar result of frequency of number/strings mixture");
}

SCOPE:
{
    my @a;
    tie @a, "Tie::StdArray";
    my @s = (1001 .. 1200, "AA" .. "ZZ");
    my @d = (1 .. 1000,    "aa" .. "zz");
    @a = (@d, @s, @d);
    my $fa = freeze(\@a);
    my %e  = ((map { $_ => 2 } @d), map { $_ => 1 } @s);
    my %f  = frequency @a;
    is($fa, freeze(\@a), "frequency:G_ARRAY leaves tied array of number/strings mixture untouched");
    is_deeply(\%f, {%e}, "frequency of tied array of number/strings mixture");
    my $f = frequency @a;
    is($fa,            freeze(\@a), "frequency:G_SCALAR leaves tied array of number/strings mixture untouched");
    is(scalar keys %e, $f,          "scalar result of frequency of tied array of number/strings mixture");
}

SCOPE:
{
    my @foo = ('a', 'b', '', undef, 'b', 'c', '', undef);
    my %e   = (
        a  => 1,
        b  => 2,
        '' => 2,
        c  => 1
    );
    my @f = frequency @foo;
    my $seen_undef;
    ref $f[-2] and ref $f[-2] eq "SCALAR" and not defined ${$f[-2]} and (undef, $seen_undef) = splice @f, -2, 2, ();
    my %f = @f;
    is_deeply(\%f, \%e, "stuff around undef's is supported correctly by frequency");
    is($seen_undef, 2, "two undef's are supported correctly by frequency");
}

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

leak_free_ok(
    'frequency with exception in overloading stringify',
    sub {
        eval {
            my $obj = DieOnStringify->new;
            my @foo = ('a', 'b', '', undef, $obj, 'b', 'c', '', undef, $obj);
            my %f   = frequency @foo;
        };
        eval {
            my $obj = DieOnStringify->new;
            my $f   = frequency 'a', 'b', '', undef, $obj, 'b', 'c', '', undef, $obj;
        };
    }
);

done_testing;