6 use Test::More tests => 11 + 6 * 3;
10 sub Int::TYPEDSCALAR { ++$counter }
13 my $desc = 'peephole optimization of conditionals';
17 my $code = eval <<' TESTCASE';
23 } elsif ($_[1] || $_[2]) {
26 } elsif ($_[3] && $_[4]) {
29 } elsif ($_[5] ? $_[6] : 0) {
39 is $@, '', "$desc compiled fine";
42 is $counter, 1, "$desc : first branch was properly compiled";
43 is $ret, 1, "$desc : first branch returned 1";
46 is $counter, 2, "$desc : second branch was properly compiled";
47 is $ret, 2, "$desc : second branch returned 2";
49 $ret = $code->(0, 0, 0, 1, 1);
50 is $counter, 3, "$desc : third branch was properly compiled";
51 is $ret, 3, "$desc : third branch returned 3";
53 $ret = $code->(0, 0, 0, 0, 0, 1, 1);
54 is $counter, 4, "$desc : fourth branch was properly compiled";
55 is $ret, 4, "$desc : fourth branch returned 4";
58 is $counter, 5, "$desc : fifth branch was properly compiled";
59 is $ret, 5, "$desc : fifth branch returned 5";
63 my $desc = 'peephole optimization of C-style loops';
68 my $code = eval <<' TESTCASE';
75 do { my Int $x; $i < 4 }
77 do { my Int $y; ++$i }
85 is $@, '', "$desc compiled fine";
88 is $counter, 1 + 5 + 4 + 4, "$desc was properly compiled";
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 Int $z; 0 }) .. (do { my Int $z; 3 })) {
108 is $@, '', "$desc compiled fine";
111 is $counter, 2 + 4, "$desc was properly compiled";
112 is $ret, 6, "$desc returned 0+1+2+3";
116 my $desc = 'peephole optimization of empty loops (RT #66164)';
120 my $code = eval <<' TESTCASE';
132 is $@, '', "$desc compiled fine";
135 is $counter, 1, "$desc was properly compiled";
136 is $ret, 1, "$desc returned 1";
140 my $desc = 'peephole optimization of map';
144 my $code = eval <<' TESTCASE';
153 is $@, '', "$desc compiled fine";
155 my $ret = $code->(1, 2);
156 is $counter, 2, "$desc was properly compiled";
157 is $ret, 'x1y:x2y', "$desc returned the right value";
161 my $desc = 'peephole optimization of grep';
165 my $code = eval <<' TESTCASE';
174 is $@, '', "$desc compiled fine";
176 my $ret = $code->(1 .. 5);
177 is $counter, 5, "$desc was properly compiled";
178 is $ret, '1:2:3', "$desc returned the right value";
182 my $desc = 'peephole optimization of substitutions';
186 my $code = eval <<' TESTCASE';
199 is $@, '', "$desc compiled fine";
201 my $ret = $code->('0123456789');
202 is $counter, 10, "$desc was properly compiled";
203 is $ret, '9876543210', "$desc returned the right value";