11 plan tests => 11 + 5 * 2 + 5 * 3;
14 my $desc = 'peephole optimization of conditionals';
18 my $code = eval <<' TESTCASE';
24 } elsif ($_[1] || $_[2]) {
27 } elsif ($_[3] && $_[4]) {
30 } elsif ($_[5] ? $_[6] : 0) {
40 is $@, '', "$desc compiled fine";
43 is_deeply $x, undef, "$desc : first branch did not autovivify";
44 is $ret, 1, "$desc : first branch returned 1";
47 is_deeply $x, undef, "$desc : second branch did not autovivify";
48 is $ret, 2, "$desc : second branch returned 2";
50 $ret = $code->(0, 0, 0, 1, 1);
51 is_deeply $x, undef, "$desc : third branch did not autovivify";
52 is $ret, 3, "$desc : third branch returned 3";
54 $ret = $code->(0, 0, 0, 0, 0, 1, 1);
55 is_deeply $x, undef, "$desc : fourth branch did not autovivify";
56 is $ret, 4, "$desc : fourth branch returned 4";
59 is_deeply $x, undef, "$desc : fifth branch did not autovivify";
60 is $ret, 5, "$desc : fifth branch returned 5";
64 my $desc = 'peephole optimization of C-style loops';
68 my $code = eval <<' TESTCASE';
73 my ($z, $i) = ($x->[100], 0)
75 do { my $z = $x->[200]; $i < 4 }
77 do { my $z = $x->[300]; ++$i }
85 is $@, '', "$desc compiled fine";
88 is_deeply $x, undef, "$desc did not autovivify";
89 is $ret, 6, "$desc returned 0+1+2+3";
93 my $desc = 'peephole optimization of range loops';
97 my $code = eval <<' TESTCASE';
101 for ((do { my $z = $x->[100]; 0 }) .. (do { my $z = $x->[200]; 3 })) {
108 is $@, '', "$desc compiled fine";
111 is_deeply $x, undef, "$desc did not autovivify";
112 is $ret, 6, "$desc returned 0+1+2+3";
116 my $base_desc = 'peephole optimization of infinite';
117 my %infinite_tests = (
118 "$base_desc for loops (RT #64435)" => <<' TESTCASE',
127 "$base_desc while loops" => <<' TESTCASE',
136 "$base_desc postfix while (RT #99458)" => <<' TESTCASE',
139 ++$ret && exit $ret while 1;
142 "$base_desc until loops" => <<' TESTCASE',
151 "$base_desc postfix until" => <<' TESTCASE',
154 ++$ret && exit $ret until 0;
159 for my $desc (keys %infinite_tests) {
160 my $code = $infinite_tests{$desc};
161 my $ret = run_perl $code;
162 my $stat = $ret & 255;
164 is $stat, 0, "$desc testcase did not crash";
165 is $ret, 1, "$desc compiled fine";
170 my $desc = 'peephole optimization of map';
174 my $code = eval <<' TESTCASE';
183 is $@, '', "$desc compiled fine";
185 my $ret = $code->(1, 2);
186 is_deeply $x, undef, "$desc did not autovivify";
187 is $ret, 'x1y:x2y', "$desc returned the right value";
191 my $desc = 'peephole optimization of grep';
195 my $code = eval <<' TESTCASE';
204 is $@, '', "$desc compiled fine";
206 my $ret = $code->(1 .. 5);
207 is_deeply $x, undef, "$desc did not autovivify";
208 is $ret, '1:2:3', "$desc returned the right value";
212 my $desc = 'peephole optimization of substitutions';
216 my $code = eval <<' TESTCASE';
229 is $@, '', "$desc compiled fine";
231 my $ret = $code->('0123456789');
232 is_deeply $x, undef, "$desc did not autovivify";
233 is $ret, '9876543210', "$desc returned the right value";