9 use VPIT::TestHelpers 'run_perl';
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;
163 skip RUN_PERL_FAILED() => 2 unless defined $ret;
164 my $stat = $ret & 255;
166 is $stat, 0, "$desc testcase did not crash";
167 is $ret, 1, "$desc compiled fine";
173 my $desc = 'peephole optimization of map';
177 my $code = eval <<' TESTCASE';
186 is $@, '', "$desc compiled fine";
188 my $ret = $code->(1, 2);
189 is_deeply $x, undef, "$desc did not autovivify";
190 is $ret, 'x1y:x2y', "$desc returned the right value";
194 my $desc = 'peephole optimization of grep';
198 my $code = eval <<' TESTCASE';
207 is $@, '', "$desc compiled fine";
209 my $ret = $code->(1 .. 5);
210 is_deeply $x, undef, "$desc did not autovivify";
211 is $ret, '1:2:3', "$desc returned the right value";
215 my $desc = 'peephole optimization of substitutions';
219 my $code = eval <<' TESTCASE';
232 is $@, '', "$desc compiled fine";
234 my $ret = $code->('0123456789');
235 is_deeply $x, undef, "$desc did not autovivify";
236 is $ret, '9876543210', "$desc returned the right value";