6 use Test::More tests => 11 + 6 * 3;
9 my $desc = 'peephole optimization of conditionals';
13 my $code = eval <<' TESTCASE';
19 } elsif ($_[1] || $_[2]) {
22 } elsif ($_[3] && $_[4]) {
25 } elsif ($_[5] ? $_[6] : 0) {
35 is $@, '', "$desc compiled fine";
38 is_deeply $x, undef, "$desc : first branch did not autovivify";
39 is $ret, 1, "$desc : first branch returned 1";
42 is_deeply $x, undef, "$desc : second branch did not autovivify";
43 is $ret, 2, "$desc : second branch returned 2";
45 $ret = $code->(0, 0, 0, 1, 1);
46 is_deeply $x, undef, "$desc : third branch did not autovivify";
47 is $ret, 3, "$desc : third branch returned 3";
49 $ret = $code->(0, 0, 0, 0, 0, 1, 1);
50 is_deeply $x, undef, "$desc : fourth branch did not autovivify";
51 is $ret, 4, "$desc : fourth branch returned 4";
54 is_deeply $x, undef, "$desc : fifth branch did not autovivify";
55 is $ret, 5, "$desc : fifth branch returned 5";
59 my $desc = 'peephole optimization of C-style loops';
63 my $code = eval <<' TESTCASE';
68 my ($z, $i) = ($x->[100], 0)
70 do { my $z = $x->[200]; $i < 4 }
72 do { my $z = $x->[300]; ++$i }
80 is $@, '', "$desc compiled fine";
83 is_deeply $x, undef, "$desc did not autovivify";
84 is $ret, 6, "$desc returned 0+1+2+3";
88 my $desc = 'peephole optimization of range loops';
92 my $code = eval <<' TESTCASE';
96 for ((do { my $z = $x->[100]; 0 }) .. (do { my $z = $x->[200]; 3 })) {
103 is $@, '', "$desc compiled fine";
106 is_deeply $x, undef, "$desc did not autovivify";
107 is $ret, 6, "$desc returned 0+1+2+3";
111 my $desc = 'peephole optimization of empty loops (RT #64435)';
115 my $code = eval <<' TESTCASE';
126 is $@, '', "$desc compiled fine";
129 is_deeply $x, undef, "$desc did not autovivify";
130 is $ret, 1, "$desc returned 1";
134 my $desc = 'peephole optimization of map';
138 my $code = eval <<' TESTCASE';
147 is $@, '', "$desc compiled fine";
149 my $ret = $code->(1, 2);
150 is_deeply $x, undef, "$desc did not autovivify";
151 is $ret, 'x1y:x2y', "$desc returned the right value";
155 my $desc = 'peephole optimization of grep';
159 my $code = eval <<' TESTCASE';
168 is $@, '', "$desc compiled fine";
170 my $ret = $code->(1 .. 5);
171 is_deeply $x, undef, "$desc did not autovivify";
172 is $ret, '1:2:3', "$desc returned the right value";
176 my $desc = 'peephole optimization of substitutions';
180 my $code = eval <<' TESTCASE';
193 is $@, '', "$desc compiled fine";
195 my $ret = $code->('0123456789');
196 is_deeply $x, undef, "$desc did not autovivify";
197 is $ret, '9876543210', "$desc returned the right value";