Commit | Line | Data |
---|---|---|
317982ac IZ |
1 | #!./perl |
2 | ||
f6616c32 FC |
3 | # Test that $lexical = <some op> optimises the assignment away correctly |
4 | # and causes no ill side-effects. | |
5 | ||
317982ac IZ |
6 | BEGIN { |
7 | chdir 't' if -d 't'; | |
20822f61 | 8 | @INC = '../lib'; |
5cd1152e | 9 | require './test.pl'; |
317982ac IZ |
10 | } |
11 | ||
0ecd3ba2 | 12 | $| = 1; |
317982ac IZ |
13 | umask 0; |
14 | $xref = \ ""; | |
16ed4686 | 15 | $runme = $^X; |
317982ac IZ |
16 | @a = (1..5); |
17 | %h = (1..6); | |
18 | $aref = \@a; | |
19 | $href = \%h; | |
562a7b0c | 20 | open OP, qq{$runme -le "print 'aaa Ok ok' for 1..100"|}; |
317982ac IZ |
21 | $chopit = 'aaaaaa'; |
22 | @chopar = (113 .. 119); | |
23 | $posstr = '123456'; | |
24 | $cstr = 'aBcD.eF'; | |
25 | pos $posstr = 3; | |
26 | $nn = $n = 2; | |
27 | sub subb {"in s"} | |
28 | ||
29 | @INPUT = <DATA>; | |
3dbf192b | 30 | @simple_input = grep /^\s*\w+\s*\$\w+\s*[#\n]/, @INPUT; |
317982ac IZ |
31 | |
32 | sub wrn {"@_"} | |
33 | ||
ed7ab888 | 34 | # Check correct optimization of ucfirst etc |
ed7ab888 GS |
35 | my $a = "AB"; |
36 | my $b = "\u\L$a"; | |
5cd1152e | 37 | is( $b, 'Ab', 'Check correct optimization of ucfirst, etc'); |
ed7ab888 GS |
38 | |
39 | # Check correct destruction of objects: | |
40 | my $dc = 0; | |
41 | sub A::DESTROY {$dc += 1} | |
42 | $a=8; | |
43 | my $b; | |
44 | { my $c = 6; $b = bless \$c, "A"} | |
45 | ||
5cd1152e | 46 | is($dc, 0, 'No destruction yet'); |
ed7ab888 GS |
47 | |
48 | $b = $a+5; | |
49 | ||
5cd1152e | 50 | is($dc, 1, 'object descruction via reassignment to variable'); |
ed7ab888 | 51 | |
69b47968 GS |
52 | my $xxx = 'b'; |
53 | $xxx = 'c' . ($xxx || 'e'); | |
5cd1152e | 54 | is( $xxx, 'cb', 'variables can be read before being overwritten'); |
69b47968 | 55 | |
ed7ab888 | 56 | { # Check calling STORE |
5cd1152e | 57 | note('Tied variables, calling STORE'); |
ed7ab888 GS |
58 | my $sc = 0; |
59 | sub B::TIESCALAR {bless [11], 'B'} | |
60 | sub B::FETCH { -(shift->[0]) } | |
61 | sub B::STORE { $sc++; my $o = shift; $o->[0] = 17 + shift } | |
62 | ||
63 | my $m; | |
64 | tie $m, 'B'; | |
65 | $m = 100; | |
66 | ||
5cd1152e | 67 | is( $sc, 1, 'STORE called when assigning scalar to tied variable' ); |
ed7ab888 GS |
68 | |
69 | my $t = 11; | |
70 | $m = $t + 89; | |
71 | ||
5cd1152e | 72 | is( $sc, 2, 'and again' ); |
5cd1152e | 73 | is( $m, -117, 'checking the tied variable result' ); |
ed7ab888 GS |
74 | |
75 | $m += $t; | |
76 | ||
5cd1152e | 77 | is( $sc, 3, 'called on self-increment' ); |
5cd1152e | 78 | is( $m, 89, 'checking the tied variable result' ); |
ed7ab888 GS |
79 | |
80 | } | |
81 | ||
6b66af17 GS |
82 | # Chains of assignments |
83 | ||
84 | my ($l1, $l2, $l3, $l4); | |
85 | my $zzzz = 12; | |
86 | $zzz1 = $l1 = $l2 = $zzz2 = $l3 = $l4 = 1 + $zzzz; | |
87 | ||
5cd1152e CK |
88 | is($zzz1, 13, 'chain assignment, part1'); |
89 | is($zzz2, 13, 'chain assignment, part2'); | |
90 | is($l1, 13, 'chain assignment, part3'); | |
91 | is($l2, 13, 'chain assignment, part4'); | |
92 | is($l3, 13, 'chain assignment, part5'); | |
93 | is($l4, 13, 'chain assignment, part6'); | |
6b66af17 | 94 | |
317982ac | 95 | for (@INPUT) { |
317982ac IZ |
96 | ($op, undef, $comment) = /^([^\#]+)(\#\s+(.*))?/; |
97 | $comment = $op unless defined $comment; | |
ee8c7f54 | 98 | chomp; |
317982ac IZ |
99 | $op = "$op==$op" unless $op =~ /==/; |
100 | ($op, $expectop) = $op =~ /(.*)==(.*)/; | |
101 | ||
21ce149d | 102 | $skip = ($op =~ /^'\?\?\?'/ or $comment =~ /skip\(.*\Q$^O\E.*\)/i); |
317982ac | 103 | $integer = ($comment =~ /^i_/) ? "use integer" : '' ; |
21ce149d | 104 | if ($skip) { |
5cd1152e CK |
105 | SKIP: { |
106 | skip $comment, 1; | |
5cd1152e CK |
107 | } |
108 | next; | |
109 | } | |
317982ac IZ |
110 | |
111 | eval <<EOE; | |
112 | local \$SIG{__WARN__} = \\&wrn; | |
113 | my \$a = 'fake'; | |
114 | $integer; | |
115 | \$a = $op; | |
116 | \$b = $expectop; | |
21ce149d | 117 | is (\$a, \$b, \$comment); |
317982ac IZ |
118 | EOE |
119 | if ($@) { | |
5cd1152e CK |
120 | $warning = $@; |
121 | chomp $warning; | |
21ce149d | 122 | if ($@ !~ /is unimplemented/) { |
5cd1152e | 123 | fail($_ . ' ' . $warning); |
317982ac IZ |
124 | } |
125 | } | |
126 | } | |
3dbf192b IZ |
127 | |
128 | for (@simple_input) { | |
3dbf192b IZ |
129 | ($op, undef, $comment) = /^([^\#]+)(\#\s+(.*))?/; |
130 | $comment = $op unless defined $comment; | |
ee8c7f54 | 131 | chomp; |
3dbf192b IZ |
132 | ($operator, $variable) = /^\s*(\w+)\s*\$(\w+)/ or warn "misprocessed '$_'\n"; |
133 | eval <<EOE; | |
134 | local \$SIG{__WARN__} = \\&wrn; | |
135 | my \$$variable = "Ac# Ca\\nxxx"; | |
136 | \$$variable = $operator \$$variable; | |
137 | \$toself = \$$variable; | |
138 | \$direct = $operator "Ac# Ca\\nxxx"; | |
5cd1152e | 139 | is(\$toself, \$direct); |
3dbf192b IZ |
140 | EOE |
141 | if ($@) { | |
5cd1152e CK |
142 | $warning = $@; |
143 | chomp $warning; | |
3dbf192b | 144 | if ($@ =~ /is unimplemented/) { |
5cd1152e CK |
145 | SKIP: { |
146 | skip $warning, 1; | |
147 | pass($comment); | |
148 | } | |
3dbf192b | 149 | } elsif ($@ =~ /Can't (modify|take log of 0)/) { |
5cd1152e CK |
150 | SKIP: { |
151 | skip $warning . ' ' . $comment . ' syntax not good for selfassign', 1; | |
152 | pass(); | |
153 | } | |
3dbf192b | 154 | } else { |
5cd1152e CK |
155 | ##Something bad happened |
156 | fail($_ . ' ' . $warning); | |
3dbf192b IZ |
157 | } |
158 | } | |
159 | } | |
fdc5b023 | 160 | |
f6616c32 FC |
161 | # XXX This test does not really belong here, as it has nothing to do with |
162 | # OPpTARGET_MY optimisation. But where should it go? | |
fdc5b023 BM |
163 | eval { |
164 | sub PVBM () { 'foo' } | |
165 | index 'foo', PVBM; | |
166 | my $x = PVBM; | |
167 | ||
168 | my $str = 'foo'; | |
169 | my $pvlv = \substr $str, 0, 1; | |
170 | $x = $pvlv; | |
171 | ||
172 | 1; | |
173 | }; | |
5cd1152e CK |
174 | is($@, '', 'ex-PVBM assert'.$@); |
175 | ||
176 | done_testing(); | |
fdc5b023 | 177 | |
317982ac IZ |
178 | __END__ |
179 | ref $xref # ref | |
180 | ref $cstr # ref nonref | |
75f2fcd2 | 181 | `$runme -e "print qq[1\\n]"` # backtick skip(MSWin32) |
0f4592ef | 182 | `$undefed` # backtick undef skip(MSWin32) |
317982ac IZ |
183 | <*> # glob |
184 | <OP> # readline | |
185 | 'faked' # rcatline | |
186 | (@z = (1 .. 3)) # aassign | |
187 | chop $chopit # chop | |
188 | (chop (@x=@chopar)) # schop | |
189 | chomp $chopit # chomp | |
190 | (chop (@x=@chopar)) # schomp | |
191 | pos $posstr # pos | |
192 | pos $chopit # pos returns undef | |
193 | $nn++==2 # postinc | |
194 | $nn++==3 # i_postinc | |
195 | $nn--==4 # postdec | |
196 | $nn--==3 # i_postdec | |
197 | $n ** $n # pow | |
198 | $n * $n # multiply | |
199 | $n * $n # i_multiply | |
200 | $n / $n # divide | |
201 | $n / $n # i_divide | |
202 | $n % $n # modulo | |
203 | $n % $n # i_modulo | |
204 | $n x $n # repeat | |
205 | $n + $n # add | |
206 | $n + $n # i_add | |
207 | $n - $n # subtract | |
208 | $n - $n # i_subtract | |
209 | $n . $n # concat | |
210 | $n . $a=='2fake' # concat with self | |
211 | "3$a"=='3fake' # concat with self in stringify | |
212 | "$n" # stringify | |
213 | $n << $n # left_shift | |
214 | $n >> $n # right_shift | |
215 | $n <=> $n # ncmp | |
216 | $n <=> $n # i_ncmp | |
217 | $n cmp $n # scmp | |
218 | $n & $n # bit_and | |
219 | $n ^ $n # bit_xor | |
220 | $n | $n # bit_or | |
221 | -$n # negate | |
222 | -$n # i_negate | |
a9dec3fe | 223 | -$a=="-fake" # i_negate with string |
317982ac IZ |
224 | ~$n # complement |
225 | atan2 $n,$n # atan2 | |
226 | sin $n # sin | |
227 | cos $n # cos | |
228 | '???' # rand | |
229 | exp $n # exp | |
230 | log $n # log | |
231 | sqrt $n # sqrt | |
232 | int $n # int | |
233 | hex $n # hex | |
234 | oct $n # oct | |
235 | abs $n # abs | |
236 | length $posstr # length | |
237 | substr $posstr, 2, 2 # substr | |
238 | vec("abc",2,8) # vec | |
239 | index $posstr, 2 # index | |
240 | rindex $posstr, 2 # rindex | |
241 | sprintf "%i%i", $n, $n # sprintf | |
242 | ord $n # ord | |
243 | chr $n # chr | |
244 | crypt $n, $n # crypt | |
245 | ucfirst ($cstr . "a") # ucfirst padtmp | |
246 | ucfirst $cstr # ucfirst | |
247 | lcfirst $cstr # lcfirst | |
248 | uc $cstr # uc | |
249 | lc $cstr # lc | |
250 | quotemeta $cstr # quotemeta | |
251 | @$aref # rv2av | |
252 | @$undefed # rv2av undef | |
3918450e | 253 | (each %h) % 2 == 1 # each |
317982ac IZ |
254 | values %h # values |
255 | keys %h # keys | |
256 | %$href # rv2hv | |
257 | pack "C2", $n,$n # pack | |
258 | split /a/, "abad" # split | |
259 | join "a"; @a # join | |
260 | push @a,3==6 # push | |
261 | unshift @aaa # unshift | |
262 | reverse @a # reverse | |
263 | reverse $cstr # reverse - scal | |
264 | grep $_, 1,0,2,0,3 # grepwhile | |
265 | map "x$_", 1,0,2,0,3 # mapwhile | |
266 | subb() # entersub | |
267 | caller # caller | |
268 | warn "ignore this\n" # warn | |
269 | 'faked' # die | |
270 | open BLAH, "<non-existent" # open | |
271 | fileno STDERR # fileno | |
272 | umask 0 # umask | |
273 | select STDOUT # sselect | |
ff97eb1b | 274 | select undef,undef,undef,0 # select |
317982ac IZ |
275 | getc OP # getc |
276 | '???' # read | |
277 | '???' # sysread | |
278 | '???' # syswrite | |
279 | '???' # send | |
280 | '???' # recv | |
281 | '???' # tell | |
282 | '???' # fcntl | |
283 | '???' # ioctl | |
284 | '???' # flock | |
285 | '???' # accept | |
286 | '???' # shutdown | |
287 | '???' # ftsize | |
288 | '???' # ftmtime | |
289 | '???' # ftatime | |
290 | '???' # ftctime | |
291 | chdir 'non-existent' # chdir | |
292 | '???' # chown | |
293 | '???' # chroot | |
294 | unlink 'non-existent' # unlink | |
295 | chmod 'non-existent' # chmod | |
296 | utime 'non-existent' # utime | |
297 | rename 'non-existent', 'non-existent1' # rename | |
298 | link 'non-existent', 'non-existent1' # link | |
ecece5d6 | 299 | '???' # symlink |
317982ac IZ |
300 | readlink 'non-existent', 'non-existent1' # readlink |
301 | '???' # mkdir | |
302 | '???' # rmdir | |
303 | '???' # telldir | |
304 | '???' # fork | |
305 | '???' # wait | |
306 | '???' # waitpid | |
562a7b0c | 307 | system "$runme -e 0" # system skip(VMS) |
317982ac | 308 | '???' # exec |
0f4592ef | 309 | '???' # kill |
317982ac IZ |
310 | getppid # getppid |
311 | getpgrp # getpgrp | |
312 | '???' # setpgrp | |
313 | getpriority $$, $$ # getpriority | |
314 | '???' # setpriority | |
315 | time # time | |
7e3cfbc1 MG |
316 | localtime $^T # localtime |
317 | gmtime $^T # gmtime | |
dbb6c582 | 318 | '???' # sleep: can randomly fail |
317982ac IZ |
319 | '???' # alarm |
320 | '???' # shmget | |
321 | '???' # shmctl | |
322 | '???' # shmread | |
323 | '???' # shmwrite | |
324 | '???' # msgget | |
325 | '???' # msgctl | |
326 | '???' # msgsnd | |
327 | '???' # msgrcv | |
328 | '???' # semget | |
329 | '???' # semctl | |
330 | '???' # semop | |
331 | '???' # getlogin | |
332 | '???' # syscall |