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