File: reduce_u.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 (48 lines) | stat: -rw-r--r-- 1,183 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
#!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 List::Util qw(sum);

SCOPE:
{
    my @exam_results = (0, 2, 4, 6, 5, 3, 0);
    my $pupil        = sum @exam_results;
    my $wa           = reduce_u { defined $a ? $a + $_ * $b / $pupil : 0 } @exam_results;
    $wa = sprintf("%0.2f", $wa);
    is($wa, 3.15, "weighted average of exam");
}

leak_free_ok(
    'reduce_u' => sub {
        my @exam_results = (undef, 2, 4, 6, 5, 3, 0);
        my $pupil        = 20;
        my $wa           = reduce_u { defined $a ? $a + $_ * $b / $pupil : 0 } @exam_results;
    },
    'reduce_u X' => sub {
        my @w  = map { int(rand(5)) + 1; } 1 .. 100;
        my $c1 = reduce_u { ($a || 0) + $w[$_] * $b } 1 .. 100;
    }
);
leak_free_ok(
    'reduce_u with a coderef that dies' => sub {
        # This test is from Kevin Ryde; see RT#48669
        eval {
            my $ok = reduce_u { die } 1;
        };
    }
);
is_dying('reduce_u without sub' => sub { &reduce_u(42, 4711); });

done_testing