Update to Scalar-List-Utils 1.08
[p5sagit/p5-mst-13.2.git] / ext / List / Util / t / reduce.t
CommitLineData
1bfb5477 1#!./perl
2
f4a2945e 3BEGIN {
1bfb5477 4 unless (-d 'blib') {
f4a2945e 5 chdir 't' if -d 't';
6 @INC = '../lib';
6b05f64e 7 require Config; import Config;
1bfb5477 8 keys %Config; # Silence warning
6b05f64e 9 if ($Config{extensions} !~ /\bList\/Util\b/) {
10 print "1..0 # Skip: List::Util was not built\n";
11 exit 0;
12 }
1bfb5477 13 }
f4a2945e 14}
15
1bfb5477 16
f4a2945e 17use List::Util qw(reduce min);
18
60f3865b 19print "1..9\n";
f4a2945e 20
21print "not " if defined reduce {};
22print "ok 1\n";
23
24print "not " unless 9 == reduce { $a / $b } 756,3,7,4;
25print "ok 2\n";
26
27print "not " unless 9 == reduce { $a / $b } 9;
28print "ok 3\n";
29
30@a = map { rand } 0 .. 20;
31print "not " unless min(@a) == reduce { $a < $b ? $a : $b } @a;
32print "ok 4\n";
33
34@a = map { pack("C", int(rand(256))) } 0 .. 20;
35print "not " unless join("",@a) eq reduce { $a . $b } @a;
36print "ok 5\n";
1bfb5477 37
38sub add {
39 my($aa, $bb) = @_;
40 return $aa + $bb;
41}
42
43my $sum = reduce { my $t="$a $b\n"; 0+add($a, $b) } 3, 2, 1;
44print "not " unless $sum == 6;
45print "ok 6\n";
46
47# Check that eval{} inside the block works correctly
48print "not " unless 10 == reduce { eval { die }; $a + $b } 0,1,2,3,4;
49print "ok 7\n";
50
51print "not " if defined eval { reduce { die if $b > 2; $a + $b } 0,1,2,3,4 };
52print "ok 8\n";
60f3865b 53
54($x) = foobar();
55print "${x}ok 9\n";
56
57sub foobar { reduce { (defined(wantarray) && !wantarray) ? '' : 'not ' } 0,1,2,3 }
58