2 # Tests counting number of FETCHes.
4 # See Bugs #76814 and #87708.
19 # tie $var, "main", $val; # FETCH returns $val
20 # tie $var, "main", $val1, $val2; # FETCH returns the values in order,
21 # # one at a time, repeating the last
22 # # when the list is exhausted.
23 sub TIESCALAR {my $pack = shift; bless [@_], $pack;}
24 sub FETCH {$count ++; @{$_ [0]} == 1 ? ${$_ [0]}[0] : shift @{$_ [0]}}
25 sub STORE { unshift @{$_[0]}, $_[1] }
30 my $expected = shift() // 1;
31 local $::Level = $::Level + 1;
34 $expected == 1 ? "just once" :
35 $expected == 2 ? "twice" :
43 tie my $var => 'main', 1;
46 $dummy = $var ; check_count "=";
47 *dummy = $var ; check_count '*glob = $tied';
50 $dummy = +$var ; check_count "unary +";
51 $dummy = -$var ; check_count "unary -";
53 # Basic arithmetic and string operators.
54 $dummy = $var + 1 ; check_count '+';
55 $dummy = $var - 1 ; check_count '-';
56 $dummy = $var / 1 ; check_count '/';
57 $dummy = $var * 1 ; check_count '*';
58 $dummy = $var % 1 ; check_count '%';
59 $dummy = $var ** 1 ; check_count '**';
60 $dummy = $var << 1 ; check_count '<<';
61 $dummy = $var >> 1 ; check_count '>>';
62 $dummy = $var x 1 ; check_count 'x';
63 @dummy = ($var) x 1 ; check_count 'x';
64 $dummy = $var . 1 ; check_count '.';
65 @dummy = $var .. 1 ; check_count '$tied..1';
66 @dummy = 1 .. $var; check_count '1..$tied';
67 tie my $v42 => 'main', "z";
68 @dummy = $v42 .. "a"; check_count '$tied.."a"';
69 @dummy = "a" .. $v42; check_count '"a"..$tied';
71 # Pre/post in/decrement
72 $var ++ ; check_count 'post ++';
73 $var -- ; check_count 'post --';
74 ++ $var ; check_count 'pre ++';
75 -- $var ; check_count 'pre --';
78 $dummy = $var < 1 ; check_count '<';
79 $dummy = $var <= 1 ; check_count '<=';
80 $dummy = $var == 1 ; check_count '==';
81 $dummy = $var >= 1 ; check_count '>=';
82 $dummy = $var > 1 ; check_count '>';
83 $dummy = $var != 1 ; check_count '!=';
84 $dummy = $var <=> 1 ; check_count '<=>';
87 $dummy = $var lt 1 ; check_count 'lt';
88 $dummy = $var le 1 ; check_count 'le';
89 $dummy = $var eq 1 ; check_count 'eq';
90 $dummy = $var ge 1 ; check_count 'ge';
91 $dummy = $var gt 1 ; check_count 'gt';
92 $dummy = $var ne 1 ; check_count 'ne';
93 $dummy = $var cmp 1 ; check_count 'cmp';
96 $dummy = $var & 1 ; check_count '&';
97 $dummy = $var ^ 1 ; check_count '^';
98 $dummy = $var | 1 ; check_count '|';
99 $dummy = ~$var ; check_count '~';
102 $dummy = !$var ; check_count '!';
103 tie my $v_1, "main", 0;
104 $dummy = $v_1 || 1 ; check_count '||';
105 $dummy = ($v_1 or 1); check_count 'or';
106 $dummy = $var && 1 ; check_count '&&';
107 $dummy = ($var and 1); check_count 'and';
108 $dummy = ($var xor 1); check_count 'xor';
109 $dummy = $var ? 1 : 1 ; check_count '?:';
111 # Overloadable functions
112 $dummy = sin $var ; check_count 'sin';
113 $dummy = cos $var ; check_count 'cos';
114 $dummy = exp $var ; check_count 'exp';
115 $dummy = abs $var ; check_count 'abs';
116 $dummy = log $var ; check_count 'log';
117 $dummy = sqrt $var ; check_count 'sqrt';
118 $dummy = int $var ; check_count 'int';
119 $dummy = atan2 $var, 1 ; check_count 'atan2';
122 tie my $var0, "main", \*DATA;
123 $dummy = <$var0> ; check_count '<readline>';
125 $var .= <DATA> ; check_count '$tiedref .= <rcatline>';
127 $var .= <DATA> ; check_count '$tiedstr .= <rcatline>';
129 $var .= <DATA> ; check_count '$tiedglob .= <rcatline>';
130 { no warnings "glob";
131 $dummy = <${var}> ; check_count '<glob>';
135 for (split //, 'rwxoRWXOezsfdpSbctugkTBMAC') {
136 no warnings 'unopened';
137 $dummy = eval "-$_ \$var"; check_count "-$_";
138 # Make $var hold a glob:
139 $var = *dummy; $dummy = $var; $count = 0;
140 $dummy = eval "-$_ \$var"; check_count "-$_ \$tied_glob";
142 $var = *dummy; $dummy = $var; $count = 0;
143 eval "\$dummy = -$_ \\\$var";
144 check_count "-$_ \\\$tied_glob";
146 $dummy = -l $var ; check_count '-l';
148 $dummy = -e -e -e $var ; check_count '-e -e';
152 $dummy = $var =~ m/ / ; check_count 'm//';
153 $dummy = $var =~ s/ //; check_count 's///';
154 $dummy = $var ~~ 1 ; check_count '~~';
155 $dummy = $var =~ y/ //; check_count 'y///';
157 $dummy = $var =~y/ /-/; check_count '$ref =~ y///';
158 /$var/ ; check_count 'm/pattern/';
159 /$var foo/ ; check_count 'm/$tied foo/';
160 s/$var// ; check_count 's/pattern//';
161 s/$var foo// ; check_count 's/$tied foo//';
162 s/./$var/ ; check_count 's//replacement/';
165 tie my $var1 => 'main', \1;
166 $dummy = $$var1 ; check_count '${}';
167 tie my $var2 => 'main', [];
168 $dummy = @$var2 ; check_count '@{}';
169 $dummy = shift $var2 ; check_count 'shift arrayref';
170 tie my $var3 => 'main', {};
171 $dummy = %$var3 ; check_count '%{}';
172 $dummy = keys $var3 ; check_count 'keys hashref';
175 tie my $var4 => 'main', **;
176 $dummy = *$var4 ; check_count '*{}';
179 tie my $var5 => 'main', sub {1};
180 $dummy = &$var5 ; check_count '&{}';
184 tie my $var1 => 'main', 1;
185 $dummy = $$var1 ; check_count 'symbolic ${}';
186 $dummy = @$var1 ; check_count 'symbolic @{}';
187 $dummy = %$var1 ; check_count 'symbolic %{}';
188 $dummy = *$var1 ; check_count 'symbolic *{}';
190 $dummy = &$var1 ; check_count 'symbolic &{}';
192 # This test will not be a complete test if *988 has been created
193 # already. If this dies, change it to use another built-in variable.
194 # In 5.10-14, rv2gv calls get-magic more times for built-in vars, which
195 # is why we need the test this way.
196 if (exists $::{988}) {
197 die "*988 already exists. Please adjust this test"
199 tie my $var6 => main => 988;
201 readdir $var6 ; check_count 'symbolic readdir';
202 if (exists $::{973}) { # Need a different variable here
203 die "*973 already exists. Please adjust this test"
205 tie my $var7 => main => 973;
206 defined $$var7 ; check_count 'symbolic defined ${}';
209 tie my $var8 => 'main', 'main';
211 $var8->bolgy ; check_count '->method';
215 # This must be the name of an existing glob to trigger the maximum
216 # number of fetches in 5.14:
217 tie my $var9 => 'main', 'swibble';
219 use constant glumscrin => 'shreggleboughet';
220 *$var9 = \&{"glumscrin"}; check_count '*$tied = \&{"name of const"}';
223 # Functions that operate on filenames or filehandles
224 for ([chdir=>''],[chmod=>'0,'],[chown=>'0,0,'],[utime=>'0,0,'],
225 [truncate=>'',',0'],[stat=>''],[lstat=>''],[open=>'my $fh,"<&",'],
226 ['()=sort'=>'',' 1,2,3']) {
227 my($op,$args,$postargs) = @$_; $postargs //= '';
228 # This line makes $var8 hold a glob:
229 $var8 = *dummy; $dummy = $var8; $count = 0;
230 eval "$op $args \$var8 $postargs";
231 check_count "$op $args\$tied_glob$postargs";
232 $var8 = *dummy; $dummy = $var8; $count = 0;
234 eval "$op $args \$ref $postargs";
235 check_count "$op $args\\\$tied_glob$postargs";
241 $dummy = select $var, undef, undef, 0
242 ; check_count 'select $tied_glob, ...';
244 $dummy = select $var, undef, undef, 0
245 ; check_count 'select $tied_ref, ...';
247 $dummy = select $var, undef, undef, 0
248 ; check_count 'select $tied_undef, ...';
251 tie $var, "main", "\x{100}";
252 pos$var = 0 ; check_count 'lvalue pos $utf8';
253 $dummy = substr$var,0,1; check_count 'substr $utf8';
254 my $l =\substr$var,0,1;
255 $dummy = $$l ; check_count 'reading lvalue substr($utf8)';
256 $$l = 0 ; check_count 'setting lvalue substr($utf8)';
259 local $SIG{__WARN__} = sub {};
260 $dummy = warn $var ; check_count 'warn $tied';
261 tie $@, => 'main', 1;
262 $dummy = warn ; check_count 'warn() with $@ tied (num)';
263 tie $@, => 'main', \1;
264 $dummy = warn ; check_count 'warn() with $@ tied (ref)';
265 tie $@, => 'main', "foo\n";
266 $dummy = warn ; check_count 'warn() with $@ tied (str)';
270 ###############################################
271 # Tests for $foo binop $foo #
272 ###############################################
274 # These test that binary ops call FETCH twice if the same scalar is used
275 # for both operands. They also test that both return values from
278 my %mutators = map { ($_ => 1) } qw(. + - * / % ** << >> & | ^);
287 $int = $int ? 'use integer; ' : '';
289 tie my $var, "main", @fetches;
290 is(eval "$int\$var $op \$var", $exp, "retval of $int\$var $op \$var");
291 check_count "$int$op", 2;
293 return unless $mutators{$op};
295 tie my $var2, "main", @fetches;
296 is(eval "$int \$var2 $op= \$var2", $exp, "retval of $int \$var2 $op= \$var2");
297 check_count "$int$op=", 3;
308 bin_test '**', 2, 3, 8;
309 bin_test '*' , 2, 3, 6;
310 bin_test '/' , 10, 2, 5;
311 bin_test '%' , 11, 2, 1;
312 bin_test 'x' , 11, 2, 1111;
313 bin_test '-' , 11, 2, 9;
314 bin_test '<<', 11, 2, 44;
315 bin_test '>>', 44, 2, 11;
316 bin_test '<' , 1, 2, 1;
317 bin_test '>' , 44, 2, 1;
318 bin_test '<=', 44, 2, "";
319 bin_test '>=', 1, 2, "";
320 bin_test '!=', 1, 2, 1;
321 bin_test '<=>', 1, 2, -1;
322 bin_test 'le', 4, 2, "";
323 bin_test 'lt', 1, 2, 1;
324 bin_test 'gt', 4, 2, 1;
325 bin_test 'ge', 1, 2, "";
326 bin_test 'eq', 1, 2, "";
327 bin_test 'ne', 1, 2, 1;
328 bin_test 'cmp', 1, 2, -1;
329 bin_test '&' , 1, 2, 0;
330 bin_test '|' , 1, 2, 3;
331 bin_test '^' , 3, 5, 6;
332 bin_test '.' , 1, 2, 12;
333 bin_test '==', 1, 2, "";
334 bin_test '+' , 1, 2, 3;
335 bin_int_test '*' , 2, 3, 6;
336 bin_int_test '/' , 10, 2, 5;
337 bin_int_test '%' , 11, 2, 1;
338 bin_int_test '+' , 1, 2, 3;
339 bin_int_test '-' , 11, 2, 9;
340 bin_int_test '<' , 1, 2, 1;
341 bin_int_test '>' , 44, 2, 1;
342 bin_int_test '<=', 44, 2, "";
343 bin_int_test '>=', 1, 2, "";
344 bin_int_test '==', 1, 2, "";
345 bin_int_test '!=', 1, 2, 1;
346 bin_int_test '<=>', 1, 2, -1;
347 tie $var, "main", 1, 4;
348 cmp_ok(atan2($var, $var), '<', .3, 'retval of atan2 $var, $var');
349 check_count 'atan2', 2;