File: reduce.t

package info (click to toggle)
perl 5.8.4-8sarge6
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 58,128 kB
  • ctags: 31,422
  • sloc: perl: 224,262; ansic: 155,398; sh: 32,253; pascal: 7,747; lisp: 6,121; makefile: 2,341; cpp: 2,035; yacc: 1,019; java: 23
file content (76 lines) | stat: -rwxr-xr-x 1,596 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
#!./perl

BEGIN {
    unless (-d 'blib') {
	chdir 't' if -d 't';
	@INC = '../lib';
	require Config; import Config;
	keys %Config; # Silence warning
	if ($Config{extensions} !~ /\bList\/Util\b/) {
	    print "1..0 # Skip: List::Util was not built\n";
	    exit 0;
	}
    }
}


use List::Util qw(reduce min);

print "1..13\n";

print "not " if defined reduce {};
print "ok 1\n";

print "not " unless 9 == reduce { $a / $b } 756,3,7,4;
print "ok 2\n";

print "not " unless 9 == reduce { $a / $b } 9;
print "ok 3\n";

@a = map { rand } 0 .. 20;
print "not " unless min(@a) == reduce { $a < $b ? $a : $b } @a;
print "ok 4\n";

@a = map { pack("C", int(rand(256))) } 0 .. 20;
print "not " unless join("",@a) eq reduce { $a . $b } @a;
print "ok 5\n";

sub add {
  my($aa, $bb) = @_;
  return $aa + $bb;
}

my $sum = reduce { my $t="$a $b\n"; 0+add($a, $b) } 3, 2, 1;
print "not " unless $sum == 6;
print "ok 6\n";

# Check that eval{} inside the block works correctly
print "not " unless 10 == reduce { eval { die }; $a + $b } 0,1,2,3,4;
print "ok 7\n";

print "not " if defined eval { reduce { die if $b > 2; $a + $b } 0,1,2,3,4 };
print "ok 8\n";

($x) = foobar();
print "${x}ok 9\n";

sub foobar { reduce { (defined(wantarray) && !wantarray) ? '' : 'not ' } 0,1,2,3 }

sub add2 { $a + $b }

print "not " unless 6 == reduce \&add2, 1,2,3;
print "ok 10\n";

print "not " unless 6 == reduce { add2() } 1,2,3;
print "ok 11\n";


print "not " unless 6 == reduce { eval "$a + $b" } 1,2,3;
print "ok 12\n";

$a = $b = 9;
reduce { $a * $b } 1,2,3;
print "not " unless $a == 9 and $b == 9;
print "ok 13\n";