This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Don’t allow OPpTARGET_MY with integer negation
[perl5.git] / t / op / lex_assign.t
CommitLineData
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
6BEGIN {
7 chdir 't' if -d 't';
20822f61 8 @INC = '../lib';
5cd1152e 9 require './test.pl';
317982ac
IZ
10}
11
0ecd3ba2 12$| = 1;
317982ac
IZ
13umask 0;
14$xref = \ "";
16ed4686 15$runme = $^X;
317982ac
IZ
16@a = (1..5);
17%h = (1..6);
18$aref = \@a;
19$href = \%h;
562a7b0c 20open 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';
25pos $posstr = 3;
26$nn = $n = 2;
27sub subb {"in s"}
28
29@INPUT = <DATA>;
3dbf192b 30@simple_input = grep /^\s*\w+\s*\$\w+\s*[#\n]/, @INPUT;
317982ac
IZ
31
32sub wrn {"@_"}
33
ed7ab888 34# Check correct optimization of ucfirst etc
ed7ab888
GS
35my $a = "AB";
36my $b = "\u\L$a";
5cd1152e 37is( $b, 'Ab', 'Check correct optimization of ucfirst, etc');
ed7ab888
GS
38
39# Check correct destruction of objects:
40my $dc = 0;
41sub A::DESTROY {$dc += 1}
42$a=8;
43my $b;
44{ my $c = 6; $b = bless \$c, "A"}
45
5cd1152e 46is($dc, 0, 'No destruction yet');
ed7ab888
GS
47
48$b = $a+5;
49
5cd1152e 50is($dc, 1, 'object descruction via reassignment to variable');
ed7ab888 51
69b47968
GS
52my $xxx = 'b';
53$xxx = 'c' . ($xxx || 'e');
5cd1152e 54is( $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
84my ($l1, $l2, $l3, $l4);
85my $zzzz = 12;
86$zzz1 = $l1 = $l2 = $zzz2 = $l3 = $l4 = 1 + $zzzz;
87
5cd1152e
CK
88is($zzz1, 13, 'chain assignment, part1');
89is($zzz2, 13, 'chain assignment, part2');
90is($l1, 13, 'chain assignment, part3');
91is($l2, 13, 'chain assignment, part4');
92is($l3, 13, 'chain assignment, part5');
93is($l4, 13, 'chain assignment, part6');
6b66af17 94
317982ac 95for (@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
118EOE
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
128for (@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
140EOE
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
163eval {
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
174is($@, '', 'ex-PVBM assert'.$@);
175
176done_testing();
fdc5b023 177
317982ac
IZ
178__END__
179ref $xref # ref
180ref $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
187chop $chopit # chop
188(chop (@x=@chopar)) # schop
189chomp $chopit # chomp
190(chop (@x=@chopar)) # schomp
191pos $posstr # pos
192pos $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
225atan2 $n,$n # atan2
226sin $n # sin
227cos $n # cos
228'???' # rand
229exp $n # exp
230log $n # log
231sqrt $n # sqrt
232int $n # int
233hex $n # hex
234oct $n # oct
235abs $n # abs
236length $posstr # length
237substr $posstr, 2, 2 # substr
238vec("abc",2,8) # vec
239index $posstr, 2 # index
240rindex $posstr, 2 # rindex
241sprintf "%i%i", $n, $n # sprintf
242ord $n # ord
243chr $n # chr
244crypt $n, $n # crypt
245ucfirst ($cstr . "a") # ucfirst padtmp
246ucfirst $cstr # ucfirst
247lcfirst $cstr # lcfirst
248uc $cstr # uc
249lc $cstr # lc
250quotemeta $cstr # quotemeta
251@$aref # rv2av
252@$undefed # rv2av undef
3918450e 253(each %h) % 2 == 1 # each
317982ac
IZ
254values %h # values
255keys %h # keys
256%$href # rv2hv
257pack "C2", $n,$n # pack
258split /a/, "abad" # split
259join "a"; @a # join
260push @a,3==6 # push
261unshift @aaa # unshift
262reverse @a # reverse
263reverse $cstr # reverse - scal
264grep $_, 1,0,2,0,3 # grepwhile
265map "x$_", 1,0,2,0,3 # mapwhile
266subb() # entersub
267caller # caller
268warn "ignore this\n" # warn
269'faked' # die
270open BLAH, "<non-existent" # open
271fileno STDERR # fileno
272umask 0 # umask
273select STDOUT # sselect
ff97eb1b 274select undef,undef,undef,0 # select
317982ac
IZ
275getc 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
291chdir 'non-existent' # chdir
292'???' # chown
293'???' # chroot
294unlink 'non-existent' # unlink
295chmod 'non-existent' # chmod
296utime 'non-existent' # utime
297rename 'non-existent', 'non-existent1' # rename
298link 'non-existent', 'non-existent1' # link
ecece5d6 299'???' # symlink
317982ac
IZ
300readlink 'non-existent', 'non-existent1' # readlink
301'???' # mkdir
302'???' # rmdir
303'???' # telldir
304'???' # fork
305'???' # wait
306'???' # waitpid
562a7b0c 307system "$runme -e 0" # system skip(VMS)
317982ac 308'???' # exec
0f4592ef 309'???' # kill
317982ac
IZ
310getppid # getppid
311getpgrp # getpgrp
312'???' # setpgrp
313getpriority $$, $$ # getpriority
314'???' # setpriority
315time # time
7e3cfbc1
MG
316localtime $^T # localtime
317gmtime $^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