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