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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
#!./perl
#
# grep() and map() tests
#
print "1..37\n";
$test = 1;
sub ok {
my ($got,$expect) = @_;
print "# expected [$expect], got [$got]\nnot " if $got ne $expect;
print "ok $test\n";
}
{
my @lol = ([qw(a b c)], [], [qw(1 2 3)]);
my @mapped = map {scalar @$_} @lol;
ok "@mapped", "3 0 3";
$test++;
my @grepped = grep {scalar @$_} @lol;
ok "@grepped", "$lol[0] $lol[2]";
$test++;
@grepped = grep { $_ } @mapped;
ok "@grepped", "3 3";
$test++;
}
{
print map({$_} ("ok $test\n"));
$test++;
print map
({$_} ("ok $test\n"));
$test++;
print((map({a => $_}, ("ok $test\n")))[0]->{a});
$test++;
print((map
({a=>$_},
("ok $test\n")))[0]->{a});
$test++;
print map { $_ } ("ok $test\n");
$test++;
print map
{ $_ } ("ok $test\n");
$test++;
print((map {a => $_}, ("ok $test\n"))[0]->{a});
$test++;
print((map
{a=>$_},
("ok $test\n"))[0]->{a});
$test++;
my $x = "ok \xFF\xFF\n";
print map($_&$x,("ok $test\n"));
$test++;
print map
($_ & $x, ("ok $test\n"));
$test++;
print map { $_ & $x } ("ok $test\n");
$test++;
print map
{ $_&$x } ("ok $test\n");
$test++;
print grep({$_} ("ok $test\n"));
$test++;
print grep
({$_} ("ok $test\n"));
$test++;
print grep({a => $_}->{a}, ("ok $test\n"));
$test++;
print grep
({a => $_}->{a},
("ok $test\n"));
$test++;
print grep { $_ } ("ok $test\n");
$test++;
print grep
{ $_ } ("ok $test\n");
$test++;
print grep {a => $_}->{a}, ("ok $test\n");
$test++;
print grep
{a => $_}->{a},
("ok $test\n");
$test++;
print grep($_&"X",("ok $test\n"));
$test++;
print grep
($_&"X", ("ok $test\n"));
$test++;
print grep { $_ & "X" } ("ok $test\n");
$test++;
print grep
{ $_ & "X" } ("ok $test\n");
$test++;
}
# Tests for "for" in "map" and "grep"
# Used to dump core, bug [perl #17771]
{
my @x;
my $y = '';
@x = map { $y .= $_ for 1..2; 1 } 3..4;
print "# @x,$y\n";
print "@x,$y" eq "1 1,1212" ? "ok $test\n" : "not ok $test\n";
$test++;
$y = '';
@x = map { $y .= $_ for 1..2; $y .= $_ } 3..4;
print "# @x,$y\n";
print "@x,$y" eq "123 123124,123124" ? "ok $test\n" : "not ok $test\n";
$test++;
$y = '';
@x = map { for (1..2) { $y .= $_ } $y .= $_ } 3..4;
print "# @x,$y\n";
print "@x,$y" eq "123 123124,123124" ? "ok $test\n" : "not ok $test\n";
$test++;
$y = '';
@x = grep { $y .= $_ for 1..2; 1 } 3..4;
print "# @x,$y\n";
print "@x,$y" eq "3 4,1212" ? "ok $test\n" : "not ok $test\n";
$test++;
$y = '';
@x = grep { for (1..2) { $y .= $_ } 1 } 3..4;
print "# @x,$y\n";
print "@x,$y" eq "3 4,1212" ? "ok $test\n" : "not ok $test\n";
$test++;
# Add also a sample test from [perl #18153]. (The same bug).
$a = 1; map {if ($a){}} (2);
print "ok $test\n"; # no core dump is all we need
$test++;
}
{
sub add_an_x(@){
map {"${_}x"} @_;
};
ok join("-",add_an_x(1,2,3,4)), "1x-2x-3x-4x";
$test++;
}
{
my $gimme;
sub gimme {
my $want = wantarray();
if (defined $want) {
$gimme = $want ? 'list' : 'scalar';
} else {
$gimme = 'void';
}
}
my @list = 0..9;
undef $gimme; gimme for @list; ok($gimme, 'void'); $test++;
undef $gimme; grep { gimme } @list; ok($gimme, 'scalar'); $test++;
undef $gimme; map { gimme } @list; ok($gimme, 'list'); $test++;
}
|