use integer on the problematic subtest.
[p5sagit/p5-mst-13.2.git] / t / op / misc.t
1 #!./perl
2
3 # NOTE: Please don't add tests to this file unless they *need* to be run in
4 # separate executable and can't simply use eval.
5
6 chdir 't' if -d 't';
7 unshift @INC, "../lib";
8 $ENV{PERL5LIB} = "../lib";
9
10 $|=1;
11
12 undef $/;
13 @prgs = split "\n########\n", <DATA>;
14 print "1..", scalar @prgs, "\n";
15
16 $tmpfile = "misctmp000";
17 1 while -f ++$tmpfile;
18 END { unlink $tmpfile if $tmpfile; }
19
20 $CAT = (($^O eq 'MSWin32') ? '.\perl -e "print <>"' : 'cat');
21
22 for (@prgs){
23     my $switch;
24     if (s/^\s*(-\w.*)//){
25         $switch = $1;
26     }
27     my($prog,$expected) = split(/\nEXPECT\n/, $_);
28     open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
29     print TEST $prog, "\n";
30     close TEST or die "Cannot close $tmpfile: $!";
31
32     if ($^O eq 'MSWin32') {
33       $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
34     }
35     else {
36       $results = `./perl $switch $tmpfile 2>&1`;
37     }
38     $status = $?;
39     $results =~ s/\n+$//;
40     $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
41     $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
42 # bison says 'parse error' instead of 'syntax error',
43 # various yaccs may or may not capitalize 'syntax'.
44     $results =~ s/^(syntax|parse) error/syntax error/mig;
45     $expected =~ s/\n+$//;
46     if ( $results ne $expected ) {
47         print STDERR "PROG: $switch\n$prog\n";
48         print STDERR "EXPECTED:\n$expected\n";
49         print STDERR "GOT:\n$results\n";
50         print "not ";
51     }
52     print "ok ", ++$i, "\n";
53 }
54
55 __END__
56 ()=()
57 ########
58 $a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
59 EXPECT
60 a := b := c
61 ########
62 use integer;
63 $cusp = ~0 ^ (~0 >> 1);
64 $, = " ";
65 print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, ($cusp + 1) % 8, "!\n";
66 EXPECT
67 -1 0 0 1 !
68 ########
69 $foo=undef; $foo->go;
70 EXPECT
71 Can't call method "go" on an undefined value at - line 1.
72 ########
73 BEGIN
74         {
75             "foo";
76         }
77 ########
78 $array[128]=1
79 ########
80 $x=0x0eabcd; print $x->ref;
81 EXPECT
82 Can't call method "ref" without a package or object reference at - line 1.
83 ########
84 chop ($str .= <DATA>);
85 ########
86 close ($banana);
87 ########
88 $x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
89 EXPECT
90 25
91 ########
92 eval {sub bar {print "In bar";}}
93 ########
94 system './perl -ne "print if eof" /dev/null'
95 ########
96 chop($file = <DATA>);
97 ########
98 package N;
99 sub new {my ($obj,$n)=@_; bless \$n}  
100 $aa=new N 1;
101 $aa=12345;
102 print $aa;
103 EXPECT
104 12345
105 ########
106 %@x=0;
107 EXPECT
108 Can't modify hash deref in repeat at - line 1, near "0;"
109 Execution of - aborted due to compilation errors.
110 ########
111 $_="foo";
112 printf(STDOUT "%s\n", $_);
113 EXPECT
114 foo
115 ########
116 push(@a, 1, 2, 3,)
117 ########
118 quotemeta ""
119 ########
120 for ("ABCDE") {
121  &sub;
122 s/./&sub($&)/eg;
123 print;}
124 sub sub {local($_) = @_;
125 $_ x 4;}
126 EXPECT
127 Modification of a read-only value attempted at - line 3.
128 ########
129 package FOO;sub new {bless {FOO => BAR}};
130 package main;
131 use strict vars;   
132 my $self = new FOO;
133 print $$self{FOO};
134 EXPECT
135 BAR
136 ########
137 $_="foo";
138 s/.{1}//s;
139 print;
140 EXPECT
141 oo
142 ########
143 print scalar ("foo","bar")
144 EXPECT
145 bar
146 ########
147 sub by_number { $a <=> $b; };# inline function for sort below
148 $as_ary{0}="a0";
149 @ordered_array=sort by_number keys(%as_ary);
150 ########
151 sub NewShell
152 {
153   local($Host) = @_;
154   my($m2) = $#Shells++;
155   $Shells[$m2]{HOST} = $Host;
156   return $m2;
157 }
158  
159 sub ShowShell
160 {
161   local($i) = @_;
162 }
163  
164 &ShowShell(&NewShell(beach,Work,"+0+0"));
165 &ShowShell(&NewShell(beach,Work,"+0+0"));
166 &ShowShell(&NewShell(beach,Work,"+0+0"));
167 ########
168    {
169        package FAKEARRAY;
170    
171        sub TIEARRAY
172        { print "TIEARRAY @_\n"; 
173          die "bomb out\n" unless $count ++ ;
174          bless ['foo'] 
175        }
176        sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
177        sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
178        sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
179    }
180    
181 eval 'tie @h, FAKEARRAY, fred' ;
182 tie @h, FAKEARRAY, fred ;
183 EXPECT
184 TIEARRAY FAKEARRAY fred
185 TIEARRAY FAKEARRAY fred
186 DESTROY 
187 ########
188 BEGIN { die "phooey\n" }
189 EXPECT
190 phooey
191 BEGIN failed--compilation aborted at - line 1.
192 ########
193 BEGIN { 1/$zero }
194 EXPECT
195 Illegal division by zero at - line 1.
196 BEGIN failed--compilation aborted at - line 1.
197 ########
198 BEGIN { undef = 0 }
199 EXPECT
200 Modification of a read-only value attempted at - line 1.
201 BEGIN failed--compilation aborted at - line 1.
202 ########
203 {
204     package foo;
205     sub PRINT {
206         shift;
207         print join(' ', reverse @_)."\n";
208     }
209     sub PRINTF {
210         shift;
211           my $fmt = shift;
212         print sprintf($fmt, @_)."\n";
213     }
214     sub TIEHANDLE {
215         bless {}, shift;
216     }
217     sub READLINE {
218         "Out of inspiration";
219     }
220     sub DESTROY {
221         print "and destroyed as well\n";
222   }
223   sub READ {
224       shift;
225       print STDOUT "foo->can(READ)(@_)\n";
226       return 100; 
227   }
228   sub GETC {
229       shift;
230       print STDOUT "Don't GETC, Get Perl\n";
231       return "a"; 
232   }    
233 }
234 {
235     local(*FOO);
236     tie(*FOO,'foo');
237     print FOO "sentence.", "reversed", "a", "is", "This";
238     print "-- ", <FOO>, " --\n";
239     my($buf,$len,$offset);
240     $buf = "string";
241     $len = 10; $offset = 1;
242     read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
243     getc(FOO) eq "a" or die "foo->GETC failed";
244     printf "%s is number %d\n", "Perl", 1;
245 }
246 EXPECT
247 This is a reversed sentence.
248 -- Out of inspiration --
249 foo->can(READ)(string 10 1)
250 Don't GETC, Get Perl
251 Perl is number 1
252 and destroyed as well
253 ########
254 my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
255 EXPECT
256 2 2 2
257 ########
258 @a = ($a, $b, $c, $d) = (5, 6);
259 print "ok\n"
260   if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
261 EXPECT
262 ok
263 ########
264 print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
265 EXPECT
266 ok
267 ########
268 print "ok\n" if ("\0" lt "\xFF");
269 EXPECT
270 ok
271 ########
272 open(H,'op/misc.t'); # must be in the 't' directory
273 stat(H);
274 print "ok\n" if (-e _ and -f _ and -r _);
275 EXPECT
276 ok
277 ########
278 sub thing { 0 || return qw(now is the time) }
279 print thing(), "\n";
280 EXPECT
281 nowisthetime
282 ########
283 $ren = 'joy';
284 $stimpy = 'happy';
285 { local $main::{ren} = *stimpy; print $ren, ' ' }
286 print $ren, "\n";
287 EXPECT
288 happy joy
289 ########
290 $stimpy = 'happy';
291 { local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
292 print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
293 EXPECT
294 happy joy
295 ########
296 package p;
297 sub func { print 'really ' unless wantarray; 'p' }
298 sub groovy { 'groovy' }
299 package main;
300 print p::func()->groovy(), "\n"
301 EXPECT
302 really groovy
303 ########
304 @list = ([ 'one', 1 ], [ 'two', 2 ]);
305 sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
306 print scalar(map &func($_), 1 .. 3), " ",
307       scalar(map scalar &func($_), 1 .. 3), "\n";
308 EXPECT
309 2 3
310 ########
311 ($k, $s)  = qw(x 0);
312 @{$h{$k}} = qw(1 2 4);
313 for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
314 print "bogus\n" unless $s == 7;
315 ########
316 my $a = 'outer';
317 eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
318 eval { my $x = 'peace'; eval q[ print "$x\n" ] }
319 EXPECT
320 inner peace
321 ########
322 -w
323 $| = 1;
324 sub foo {
325     print "In foo1\n";
326     eval 'sub foo { print "In foo2\n" }';
327     print "Exiting foo1\n";
328 }
329 foo;
330 foo;
331 EXPECT
332 In foo1
333 Subroutine foo redefined at (eval 1) line 1.
334 Exiting foo1
335 In foo2
336 ########
337 $s = 0;
338 map {#this newline here tickles the bug
339 $s += $_} (1,2,4);
340 print "eat flaming death\n" unless ($s == 7);
341 ########
342 sub foo { local $_ = shift; split; @_ }
343 @x = foo(' x  y  z ');
344 print "you die joe!\n" unless "@x" eq 'x y z';
345 ########
346 /(?{"{"})/      # Check it outside of eval too
347 EXPECT
348 Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
349 /(?{"{"})/: Sequence (?{...}) not terminated or not {}-balanced at - line 1.
350 ########
351 /(?{"{"}})/     # Check it outside of eval too
352 EXPECT
353 Unmatched right curly bracket at (re_eval 1) line 1, at end of line
354 syntax error at (re_eval 1) line 1, near ""{"}"
355 Compilation failed in regexp at - line 1.
356 ########
357 BEGIN { @ARGV = qw(a b c) }
358 BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
359 END { print "end <",shift,">\nargv <@ARGV>\n" }
360 INIT { print "init <",shift,">\n" }
361 EXPECT
362 argv <a b c>
363 begin <a>
364 init <b>
365 end <c>
366 argv <>
367 ########
368 -l
369 # fdopen from a system descriptor to a system descriptor used to close
370 # the former.
371 open STDERR, '>&=STDOUT' or die $!;
372 select STDOUT; $| = 1; print fileno STDOUT;
373 select STDERR; $| = 1; print fileno STDERR;
374 EXPECT
375 1
376 2
377 ########
378 -w
379 sub testme { my $a = "test"; { local $a = "new test"; print $a }}
380 EXPECT
381 Can't localize lexical variable $a at - line 2.
382 ########
383 package X;
384 sub ascalar { my $r; bless \$r }
385 sub DESTROY { print "destroyed\n" };
386 package main;
387 *s = ascalar X;
388 EXPECT
389 destroyed
390 ########
391 package X;
392 sub anarray { bless [] }
393 sub DESTROY { print "destroyed\n" };
394 package main;
395 *a = anarray X;
396 EXPECT
397 destroyed
398 ########
399 package X;
400 sub ahash { bless {} }
401 sub DESTROY { print "destroyed\n" };
402 package main;
403 *h = ahash X;
404 EXPECT
405 destroyed
406 ########
407 package X;
408 sub aclosure { my $x; bless sub { ++$x } }
409 sub DESTROY { print "destroyed\n" };
410 package main;
411 *c = aclosure X;
412 EXPECT
413 destroyed
414 ########
415 package X;
416 sub any { bless {} }
417 my $f = "FH000"; # just to thwart any future optimisations
418 sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
419 sub DESTROY { print "destroyed\n" }
420 package main;
421 $x = any X; # to bump sv_objcount. IO objs aren't counted??
422 *f = afh X;
423 EXPECT
424 destroyed
425 destroyed
426 ########
427 BEGIN {
428   $| = 1;
429   $SIG{__WARN__} = sub {
430     eval { print $_[0] };
431     die "bar\n";
432   };
433   warn "foo\n";
434 }
435 EXPECT
436 foo
437 bar
438 BEGIN failed--compilation aborted at - line 8.
439 ########
440 package X;
441 @ISA='Y';
442 sub new {
443     my $class = shift;
444     my $self = { };
445     bless $self, $class;
446     my $init = shift;
447     $self->foo($init);
448     print "new", $init;
449     return $self;
450 }
451 sub DESTROY {
452     my $self = shift;
453     print "DESTROY", $self->foo;
454 }
455 package Y;
456 sub attribute {
457     my $self = shift;
458     my $var = shift;
459     if (@_ == 0) {
460         return $self->{$var};
461     } elsif (@_ == 1) {
462         $self->{$var} = shift;
463     }
464 }
465 sub AUTOLOAD {
466     $AUTOLOAD =~ /::([^:]+)$/;
467     my $method = $1;
468     splice @_, 1, 0, $method;
469     goto &attribute;
470 }
471 package main;
472 my $x = X->new(1);
473 for (2..3) {
474     my $y = X->new($_);
475     print $y->foo;
476 }
477 print $x->foo;
478 EXPECT
479 new1new22DESTROY2new33DESTROY31DESTROY1
480 ########
481 re();
482 sub re {
483     my $re = join '', eval 'qr/(?p{ $obj->method })/';
484     $re;
485 }
486 EXPECT
487 ########
488 use strict;
489 my $foo = "ZZZ\n";
490 END { print $foo }
491 EXPECT
492 ZZZ
493 ########
494 eval '
495 use strict;
496 my $foo = "ZZZ\n";
497 END { print $foo }
498 ';
499 EXPECT
500 ZZZ
501 ########
502 -w
503 if (@ARGV) { print "" }
504 else {
505   if ($x == 0) { print "" } else { print $x }
506 }
507 EXPECT
508 Use of uninitialized value at - line 4.