4 # grep() and map() tests
16 my @lol = ([qw(a b c)], [], [qw(1 2 3)]);
17 my @mapped = map {scalar @$_} @lol;
18 cmp_ok("@mapped", 'eq', "3 0 3", 'map scalar list of list');
20 my @grepped = grep {scalar @$_} @lol;
21 cmp_ok("@grepped", 'eq', "$lol[0] $lol[2]", 'grep scalar list of list');
24 @grepped = grep { $_ } @mapped;
25 cmp_ok( "@grepped", 'eq', "3 3", 'grep basic');
31 @res = map({$_} ("geronimo"));
32 cmp_ok( scalar(@res), '==', 1, 'basic map nr');
33 cmp_ok( $res[0], 'eq', 'geronimo', 'basic map is');
37 cmp_ok( scalar(@res), '==', 1, 'linefeed map nr');
38 cmp_ok( $res[0], 'eq', 'yoyodyne', 'linefeed map is');
43 cmp_ok( scalar(@res), '==', 1, 'deref map nr');
44 cmp_ok( $res[0], 'eq', 'chobb', 'deref map is');
46 @res = map {$_} ("geronimo");
47 cmp_ok( scalar(@res), '==', 1, 'no paren basic map nr');
48 cmp_ok( $res[0], 'eq', 'geronimo', 'no paren basic map is');
52 cmp_ok( scalar(@res), '==', 1, 'no paren linefeed map nr');
53 cmp_ok( $res[0], 'eq', 'yoyodyne', 'no paren linefeed map is');
58 cmp_ok( scalar(@res), '==', 1, 'no paren deref map nr');
59 cmp_ok( $res[0], 'eq', 'chobb', 'no paren deref map is');
61 my $x = "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\n";
63 @res = map($_&$x,("sferics\n"));
64 cmp_ok( scalar(@res), '==', 1, 'binand map nr 1');
65 cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 1');
68 ($_ & $x, ("sferics\n"));
69 cmp_ok( scalar(@res), '==', 1, 'binand map nr 2');
70 cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 2');
72 @res = map { $_ & $x } ("sferics\n");
73 cmp_ok( scalar(@res), '==', 1, 'binand map nr 3');
74 cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 3');
77 { $_&$x } ("sferics\n");
78 cmp_ok( scalar(@res), '==', 1, 'binand map nr 4');
79 cmp_ok( $res[0], 'eq', "sferics\n", 'binand map is 4');
81 @res = grep({$_} ("geronimo"));
82 cmp_ok( scalar(@res), '==', 1, 'basic grep nr');
83 cmp_ok( $res[0], 'eq', 'geronimo', 'basic grep is');
87 cmp_ok( scalar(@res), '==', 1, 'linefeed grep nr');
88 cmp_ok( $res[0], 'eq', 'yoyodyne', 'linefeed grep is');
93 cmp_ok( scalar(@res), '==', 1, 'deref grep nr');
94 cmp_ok( $res[0], 'eq', 'chobb', 'deref grep is');
96 @res = grep {$_} ("geronimo");
97 cmp_ok( scalar(@res), '==', 1, 'no paren basic grep nr');
98 cmp_ok( $res[0], 'eq', 'geronimo', 'no paren basic grep is');
102 cmp_ok( scalar(@res), '==', 1, 'no paren linefeed grep nr');
103 cmp_ok( $res[0], 'eq', 'yoyodyne', 'no paren linefeed grep is');
105 @res = grep {a=>$_}->{a}, ("chobb");
106 cmp_ok( scalar(@res), '==', 1, 'no paren deref grep nr');
107 cmp_ok( $res[0], 'eq', 'chobb', 'no paren deref grep is');
110 {a=>$_}->{a}, ("chobb");
111 cmp_ok( scalar(@res), '==', 1, 'no paren deref linefeed nr');
112 cmp_ok( $res[0], 'eq', 'chobb', 'no paren deref linefeed is');
114 @res = grep($_&"X", ("bodine"));
115 cmp_ok( scalar(@res), '==', 1, 'binand X grep nr');
116 cmp_ok( $res[0], 'eq', 'bodine', 'binand X grep is');
119 ($_&"X", ("bodine"));
120 cmp_ok( scalar(@res), '==', 1, 'binand X linefeed grep nr');
121 cmp_ok( $res[0], 'eq', 'bodine', 'binand X linefeed grep is');
123 @res = grep {$_&"X"} ("bodine");
124 cmp_ok( scalar(@res), '==', 1, 'no paren binand X grep nr');
125 cmp_ok( $res[0], 'eq', 'bodine', 'no paren binand X grep is');
129 cmp_ok( scalar(@res), '==', 1, 'no paren binand X linefeed grep nr');
130 cmp_ok( $res[0], 'eq', 'bodine', 'no paren binand X linefeed grep is');
134 # Tests for "for" in "map" and "grep"
135 # Used to dump core, bug [perl #17771]
139 @x = map { $y .= $_ for 1..2; 1 } 3..4;
140 cmp_ok( "@x,$y",'eq',"1 1,1212", '[perl #17771] for in map 1');
143 @x = map { $y .= $_ for 1..2; $y .= $_ } 3..4;
144 cmp_ok( "@x,$y",'eq',"123 123124,123124", '[perl #17771] for in map 2');
147 @x = map { for (1..2) { $y .= $_ } $y .= $_ } 3..4;
148 cmp_ok( "@x,$y",'eq',"123 123124,123124", '[perl #17771] for in map 3');
151 @x = grep { $y .= $_ for 1..2; 1 } 3..4;
152 cmp_ok( "@x,$y",'eq',"3 4,1212", '[perl #17771] for in grep 1');
155 @x = grep { for (1..2) { $y .= $_ } 1 } 3..4;
156 cmp_ok( "@x,$y",'eq',"3 4,1212", '[perl #17771] for in grep 2');
158 # Add also a sample test from [perl #18153]. (The same bug).
159 $a = 1; map {if ($a){}} (2);
160 pass( '[perl #18153] (not dead yet)' ); # no core dump is all we need
167 cmp_ok( join("-",add_an_x(1,2,3,4)), 'eq', "1x-2x-3x-4x", 'add-an-x');
174 my $want = wantarray();
176 $gimme = $want ? 'list' : 'scalar';
184 undef $gimme; gimme for @list; cmp_ok($gimme, 'eq', 'void', 'gimme a V!');
185 undef $gimme; grep { gimme } @list; cmp_ok($gimme, 'eq', 'scalar', 'gimme an S!');
186 undef $gimme; map { gimme } @list; cmp_ok($gimme, 'eq', 'list', 'gimme an L!');
190 # test scalar context return
191 my @list = (7, 14, 21);
193 my $x = map {$_ *= 2} @list;
194 cmp_ok("@list", 'eq', "14 28 42", 'map scalar return');
195 cmp_ok($x, '==', 3, 'map scalar count');
197 @list = (9, 16, 25, 36);
198 $x = grep {$_ % 2} @list;
199 cmp_ok($x, '==', 2, 'grep scalar count');
201 my @res = grep {$_ % 2} @list;
202 cmp_ok("@res", 'eq', "9 25", 'grep extract');
206 # This shouldn't loop indefinitively.
207 my @empty = map { while (1) {} } ();
208 cmp_ok("@empty", 'eq', '', 'staying alive');