This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
lex_assign.t: Make store count test stricter
[perl5.git] / t / op / lex_assign.t
1 #!./perl
2
3 # Test that $lexical = <some op> optimises the assignment away correctly
4 # and causes no ill side-effects.
5
6 BEGIN {
7     chdir 't' if -d 't';
8     @INC = '../lib';
9     require './test.pl';
10 }
11
12 $| = 1;
13 umask 0;
14 $xref = \ "";
15 $runme = $^X;
16 @a = (1..5);
17 %h = (1..6);
18 $aref = \@a;
19 $href = \%h;
20 open OP, qq{$runme -le "print 'aaa Ok ok' for 1..100"|};
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>;
30 @simple_input = grep /^\s*\w+\s*\$\w+\s*[#\n]/, @INPUT;
31
32 sub wrn {"@_"}
33
34 # Check correct optimization of ucfirst etc
35 my $a = "AB";
36 my $b = "\u\L$a";
37 is( $b, 'Ab', 'Check correct optimization of ucfirst, etc');
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
46 is($dc, 0, 'No destruction yet');
47
48 $b = $a+5;
49
50 is($dc, 1, 'object descruction via reassignment to variable');
51
52 my $xxx = 'b';
53 $xxx = 'c' . ($xxx || 'e');
54 is( $xxx, 'cb', 'variables can be read before being overwritten');
55
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
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');
68
69 for (@INPUT) {
70   ($op, undef, $comment) = /^([^\#]+)(\#\s+(.*))?/;
71   $comment = $op unless defined $comment;
72   chomp;
73   $op = "$op==$op" unless $op =~ /==/;
74   ($op, $expectop) = $op =~ /(.*)==(.*)/;
75   
76   $skip = ($op =~ /^'\?\?\?'/ or $comment =~ /skip\(.*\Q$^O\E.*\)/i);
77   $integer = ($comment =~ /^i_/) ? "use integer" : '' ;
78   if ($skip) {
79     SKIP: {
80         skip $comment, 1;
81     }
82     next;
83   }
84   
85   eval <<EOE;
86   local \$SIG{__WARN__} = \\&wrn;
87   my \$a = 'fake';
88   $integer;
89   \$a = $op;
90   \$b = $expectop;
91   is (\$a, \$b, \$comment);
92 EOE
93   if ($@) {
94     $warning = $@;
95     chomp $warning;
96     if ($@ !~ /(?:is un|not )implemented/) {
97       fail($_ . ' ' . $warning);
98     }
99   }
100 }
101
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;
133     local $SIG{__WARN__} = \&wrn;
134     eval "\$m = $op";
135     is $sc, $@ ? 0 : 1, "STORE count for $comment";
136   }
137 }
138
139 for (@simple_input) {
140   ($op, undef, $comment) = /^([^\#]+)(\#\s+(.*))?/;
141   $comment = $op unless defined $comment;
142   chomp;
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";
150   is(\$toself, \$direct);
151 EOE
152   if ($@) {
153     $warning = $@;
154     chomp $warning;
155     if ($@ =~ /(?:is un|not )implemented/) {
156       SKIP: {
157         skip $warning, 1;
158         pass($comment);
159       }
160     } elsif ($@ =~ /Can't (modify|take log of 0)/) {
161       SKIP: {
162         skip $warning . ' ' . $comment . ' syntax not good for selfassign', 1;
163         pass();
164       }
165     } else {
166       ##Something bad happened
167       fail($_ . ' ' . $warning);
168     }
169   }
170 }
171
172 # XXX This test does not really belong here, as it has nothing to do with
173 #     OPpTARGET_MY optimisation.  But where should it go?
174 eval {
175     sub PVBM () { 'foo' }
176     index 'foo', PVBM;
177     my $x = PVBM;
178
179     my $str = 'foo';
180     my $pvlv = \substr $str, 0, 1;
181     $x = $pvlv;
182
183     1;
184 };
185 is($@, '', 'ex-PVBM assert'.$@);
186
187 done_testing();
188
189 __END__
190 ref $xref                       # ref
191 ref $cstr                       # ref nonref
192 `$runme -e "print qq[1\\n]"`                            # backtick skip(MSWin32)
193 `$undefed`                      # backtick undef skip(MSWin32)
194 <*>                             # glob
195 <OP>                            # readline
196 'faked'                         # rcatline
197 (@z = (1 .. 3))                 # aassign
198 chop $chopit                    # chop
199 (chop (@x=@chopar))             # schop
200 chomp $chopit                   # chomp
201 (chop (@x=@chopar))             # schomp
202 pos $posstr                     # pos
203 pos $chopit                     # pos returns undef
204 $nn++==2                        # postinc
205 $nn++==3                        # i_postinc
206 $nn--==4                        # postdec
207 $nn--==3                        # i_postdec
208 $n ** $n                        # pow
209 $n * $n                         # multiply
210 $n * $n                         # i_multiply
211 $n / $n                         # divide
212 $n / $n                         # i_divide
213 $n % $n                         # modulo
214 $n % $n                         # i_modulo
215 $n x $n                         # repeat
216 $n + $n                         # add
217 $n + $n                         # i_add
218 $n - $n                         # subtract
219 $n - $n                         # i_subtract
220 $n . $n                         # concat
221 $n . $a=='2fake'                # concat with self
222 "3$a"=='3fake'                  # concat with self in stringify
223 "$n"                            # stringify
224 $n << $n                        # left_shift
225 $n >> $n                        # right_shift
226 $n <=> $n                       # ncmp
227 $n <=> $n                       # i_ncmp
228 $n cmp $n                       # scmp
229 $n & $n                         # bit_and
230 $n ^ $n                         # bit_xor
231 $n | $n                         # bit_or
232 -$n                             # negate
233 -$n                             # i_negate
234 -$a=="-fake"                    # i_negate with string
235 ~$n                             # complement
236 atan2 $n,$n                     # atan2
237 sin $n                          # sin
238 cos $n                          # cos
239 '???'                           # rand
240 exp $n                          # exp
241 log $n                          # log
242 sqrt $n                         # sqrt
243 int $n                          # int
244 hex $n                          # hex
245 oct $n                          # oct
246 abs $n                          # abs
247 length $posstr                  # length
248 substr $posstr, 2, 2            # substr
249 vec("abc",2,8)                  # vec
250 index $posstr, 2                # index
251 rindex $posstr, 2               # rindex
252 sprintf "%i%i", $n, $n          # sprintf
253 ord $n                          # ord
254 chr $n                          # chr
255 chr ${\256}                     # chr $wide
256 crypt $n, $n                    # crypt
257 ucfirst ($cstr . "a")           # ucfirst padtmp
258 ucfirst $cstr                   # ucfirst
259 lcfirst $cstr                   # lcfirst
260 uc $cstr                        # uc
261 lc $cstr                        # lc
262 quotemeta $cstr                 # quotemeta
263 @$aref                          # rv2av
264 @$undefed                       # rv2av undef
265 (each %h) % 2 == 1              # each
266 values %h                       # values
267 keys %h                         # keys
268 %$href                          # rv2hv
269 pack "C2", $n,$n                # pack
270 split /a/, "abad"               # split
271 join "a"; @a                    # join
272 push @a,3==6                    # push
273 unshift @aaa                    # unshift
274 reverse @a                      # reverse
275 reverse $cstr                   # reverse - scal
276 grep $_, 1,0,2,0,3              # grepwhile
277 map "x$_", 1,0,2,0,3            # mapwhile
278 subb()                          # entersub
279 caller                          # caller
280 warn "ignore this\n"            # warn
281 'faked'                         # die
282 open BLAH, "<non-existent"      # open
283 fileno STDERR                   # fileno
284 umask 0                         # umask
285 select STDOUT                   # sselect
286 select undef,undef,undef,0      # select
287 getc OP                         # getc
288 '???'                           # read
289 '???'                           # sysread
290 '???'                           # syswrite
291 '???'                           # send
292 '???'                           # recv
293 '???'                           # tell
294 '???'                           # fcntl
295 '???'                           # ioctl
296 '???'                           # flock
297 '???'                           # accept
298 '???'                           # shutdown
299 '???'                           # ftsize
300 '???'                           # ftmtime
301 '???'                           # ftatime
302 '???'                           # ftctime
303 chdir 'non-existent'            # chdir
304 '???'                           # chown
305 '???'                           # chroot
306 unlink 'non-existent'           # unlink
307 chmod 'non-existent'            # chmod
308 utime 'non-existent'            # utime
309 rename 'non-existent', 'non-existent1'  # rename
310 link 'non-existent', 'non-existent1' # link
311 '???'                           # symlink
312 readlink 'non-existent', 'non-existent1' # readlink
313 '???'                           # mkdir
314 '???'                           # rmdir
315 '???'                           # telldir
316 '???'                           # fork
317 '???'                           # wait
318 '???'                           # waitpid
319 system "$runme -e 0"            # system skip(VMS)
320 '???'                           # exec
321 '???'                           # kill
322 getppid                         # getppid
323 getpgrp                         # getpgrp
324 setpgrp                         # setpgrp
325 getpriority $$, $$              # getpriority
326 '???'                           # setpriority
327 time                            # time
328 localtime $^T                   # localtime
329 gmtime $^T                      # gmtime
330 '???'                           # sleep: can randomly fail
331 '???'                           # alarm
332 '???'                           # shmget
333 '???'                           # shmctl
334 '???'                           # shmread
335 '???'                           # shmwrite
336 '???'                           # msgget
337 '???'                           # msgctl
338 '???'                           # msgsnd
339 '???'                           # msgrcv
340 '???'                           # semget
341 '???'                           # semctl
342 '???'                           # semop
343 '???'                           # getlogin
344 '???'                           # syscall