Integrate changes 13474+13478+13584 from maintperl;
[p5sagit/p5-mst-13.2.git] / t / run / fresh_perl.t
1 #!./perl
2
3 # ** DO NOT ADD ANY MORE TESTS HERE **
4 # Instead, put the test in the appropriate test file and use the 
5 # fresh_perl_is()/fresh_perl_like() functions in t/test.pl.
6
7 # This is for tests that will normally cause segfaults, and other nasty
8 # errors that might kill the interpreter and for some reason you can't
9 # use an eval().
10 #
11 # New tests are added to the bottom.  For example.
12 #
13 #       ######## perlbug ID 20020831.001
14 #       ($a, b) = (1,2)
15 #       EXPECT
16 #       Can't modify constant item in list assignment - at line 1
17 #
18 # to test that the code "($a, b) = (1,2)" causes the appropriate syntax
19 # error, rather than just segfaulting as reported in perlbug ID
20 # 20020831.001
21
22 BEGIN {
23     chdir 't' if -d 't';
24     @INC = '../lib';
25     require './test.pl';        # for which_perl() etc
26 }
27
28 use strict;
29
30 my $Perl = which_perl();
31
32 $|=1;
33
34 my @prgs = ();
35 while(<DATA>) { 
36     if(m/^#{8,}\s*(.*)/) { 
37         push @prgs, ['', $1];
38     }
39     else { 
40         $prgs[-1][0] .= $_;
41     }
42 }
43 plan tests => scalar @prgs;
44
45 foreach my $prog (@prgs) {
46     my($raw_prog, $name) = @$prog;
47
48     my $switch;
49     if ($raw_prog =~ s/^\s*(-\w.*)//){
50         $switch = $1;
51     }
52
53     my($prog,$expected) = split(/\nEXPECT\n/, $raw_prog);
54
55     $expected =~ s/\n+$//;
56
57     fresh_perl_is($prog, $expected, { switches => [$switch] }, $name);
58 }
59
60 __END__
61 ########
62 $a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
63 EXPECT
64 a := b := c
65 ########
66 $cusp = ~0 ^ (~0 >> 1);
67 use integer;
68 $, = " ";
69 print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, 8 | (($cusp + 1) % 8 + 7), "!\n";
70 EXPECT
71 7 0 0 8 !
72 ########
73 $foo=undef; $foo->go;
74 EXPECT
75 Can't call method "go" on an undefined value at - line 1.
76 ########
77 BEGIN
78         {
79             "foo";
80         }
81 ########
82 $array[128]=1
83 ########
84 $x=0x0eabcd; print $x->ref;
85 EXPECT
86 Can't call method "ref" without a package or object reference at - line 1.
87 ########
88 chop ($str .= <DATA>);
89 ########
90 close ($banana);
91 ########
92 $x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
93 EXPECT
94 25
95 ########
96 eval {sub bar {print "In bar";}}
97 ########
98 system './perl -ne "print if eof" /dev/null'
99 ########
100 chop($file = <DATA>);
101 ########
102 package N;
103 sub new {my ($obj,$n)=@_; bless \$n}  
104 $aa=new N 1;
105 $aa=12345;
106 print $aa;
107 EXPECT
108 12345
109 ########
110 %@x=0;
111 EXPECT
112 Can't modify hash dereference in repeat (x) at - line 1, near "0;"
113 Execution of - aborted due to compilation errors.
114 ########
115 $_="foo";
116 printf(STDOUT "%s\n", $_);
117 EXPECT
118 foo
119 ########
120 push(@a, 1, 2, 3,)
121 ########
122 quotemeta ""
123 ########
124 for ("ABCDE") {
125  &sub;
126 s/./&sub($&)/eg;
127 print;}
128 sub sub {local($_) = @_;
129 $_ x 4;}
130 EXPECT
131 Modification of a read-only value attempted at - line 3.
132 ########
133 package FOO;sub new {bless {FOO => BAR}};
134 package main;
135 use strict vars;   
136 my $self = new FOO;
137 print $$self{FOO};
138 EXPECT
139 BAR
140 ########
141 $_="foo";
142 s/.{1}//s;
143 print;
144 EXPECT
145 oo
146 ########
147 print scalar ("foo","bar")
148 EXPECT
149 bar
150 ########
151 sub by_number { $a <=> $b; };# inline function for sort below
152 $as_ary{0}="a0";
153 @ordered_array=sort by_number keys(%as_ary);
154 ########
155 sub NewShell
156 {
157   local($Host) = @_;
158   my($m2) = $#Shells++;
159   $Shells[$m2]{HOST} = $Host;
160   return $m2;
161 }
162  
163 sub ShowShell
164 {
165   local($i) = @_;
166 }
167  
168 &ShowShell(&NewShell(beach,Work,"+0+0"));
169 &ShowShell(&NewShell(beach,Work,"+0+0"));
170 &ShowShell(&NewShell(beach,Work,"+0+0"));
171 ########
172    {
173        package FAKEARRAY;
174    
175        sub TIEARRAY
176        { print "TIEARRAY @_\n"; 
177          die "bomb out\n" unless $count ++ ;
178          bless ['foo'] 
179        }
180        sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
181        sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
182        sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
183    }
184    
185 eval 'tie @h, FAKEARRAY, fred' ;
186 tie @h, FAKEARRAY, fred ;
187 EXPECT
188 TIEARRAY FAKEARRAY fred
189 TIEARRAY FAKEARRAY fred
190 DESTROY 
191 ########
192 BEGIN { die "phooey\n" }
193 EXPECT
194 phooey
195 BEGIN failed--compilation aborted at - line 1.
196 ########
197 BEGIN { 1/$zero }
198 EXPECT
199 Illegal division by zero at - line 1.
200 BEGIN failed--compilation aborted at - line 1.
201 ########
202 BEGIN { undef = 0 }
203 EXPECT
204 Modification of a read-only value attempted at - line 1.
205 BEGIN failed--compilation aborted at - line 1.
206 ########
207 {
208     package foo;
209     sub PRINT {
210         shift;
211         print join(' ', reverse @_)."\n";
212     }
213     sub PRINTF {
214         shift;
215           my $fmt = shift;
216         print sprintf($fmt, @_)."\n";
217     }
218     sub TIEHANDLE {
219         bless {}, shift;
220     }
221     sub READLINE {
222         "Out of inspiration";
223     }
224     sub DESTROY {
225         print "and destroyed as well\n";
226   }
227   sub READ {
228       shift;
229       print STDOUT "foo->can(READ)(@_)\n";
230       return 100; 
231   }
232   sub GETC {
233       shift;
234       print STDOUT "Don't GETC, Get Perl\n";
235       return "a"; 
236   }    
237 }
238 {
239     local(*FOO);
240     tie(*FOO,'foo');
241     print FOO "sentence.", "reversed", "a", "is", "This";
242     print "-- ", <FOO>, " --\n";
243     my($buf,$len,$offset);
244     $buf = "string";
245     $len = 10; $offset = 1;
246     read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
247     getc(FOO) eq "a" or die "foo->GETC failed";
248     printf "%s is number %d\n", "Perl", 1;
249 }
250 EXPECT
251 This is a reversed sentence.
252 -- Out of inspiration --
253 foo->can(READ)(string 10 1)
254 Don't GETC, Get Perl
255 Perl is number 1
256 and destroyed as well
257 ########
258 my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
259 EXPECT
260 2 2 2
261 ########
262 # used to attach defelem magic to all immortal values,
263 # which made restore of local $_ fail.
264 foo(2>1);
265 sub foo { bar() for @_;  }
266 sub bar { local $_; }
267 print "ok\n";
268 EXPECT
269 ok
270 ########
271 @a = ($a, $b, $c, $d) = (5, 6);
272 print "ok\n"
273   if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
274 EXPECT
275 ok
276 ########
277 print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
278 EXPECT
279 ok
280 ########
281 print "ok\n" if ("\0" lt "\xFF");
282 EXPECT
283 ok
284 ########
285 open(H,'run/fresh_perl.t'); # must be in the 't' directory
286 stat(H);
287 print "ok\n" if (-e _ and -f _ and -r _);
288 EXPECT
289 ok
290 ########
291 sub thing { 0 || return qw(now is the time) }
292 print thing(), "\n";
293 EXPECT
294 nowisthetime
295 ########
296 $ren = 'joy';
297 $stimpy = 'happy';
298 { local $main::{ren} = *stimpy; print $ren, ' ' }
299 print $ren, "\n";
300 EXPECT
301 happy joy
302 ########
303 $stimpy = 'happy';
304 { local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
305 print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
306 EXPECT
307 happy joy
308 ########
309 package p;
310 sub func { print 'really ' unless wantarray; 'p' }
311 sub groovy { 'groovy' }
312 package main;
313 print p::func()->groovy(), "\n"
314 EXPECT
315 really groovy
316 ########
317 @list = ([ 'one', 1 ], [ 'two', 2 ]);
318 sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
319 print scalar(map &func($_), 1 .. 3), " ",
320       scalar(map scalar &func($_), 1 .. 3), "\n";
321 EXPECT
322 2 3
323 ########
324 ($k, $s)  = qw(x 0);
325 @{$h{$k}} = qw(1 2 4);
326 for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
327 print "bogus\n" unless $s == 7;
328 ########
329 my $a = 'outer';
330 eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
331 eval { my $x = 'peace'; eval q[ print "$x\n" ] }
332 EXPECT
333 inner peace
334 ########
335 -w
336 $| = 1;
337 sub foo {
338     print "In foo1\n";
339     eval 'sub foo { print "In foo2\n" }';
340     print "Exiting foo1\n";
341 }
342 foo;
343 foo;
344 EXPECT
345 In foo1
346 Subroutine foo redefined at (eval 1) line 1.
347 Exiting foo1
348 In foo2
349 ########
350 $s = 0;
351 map {#this newline here tickles the bug
352 $s += $_} (1,2,4);
353 print "eat flaming death\n" unless ($s == 7);
354 ########
355 sub foo { local $_ = shift; split; @_ }
356 @x = foo(' x  y  z ');
357 print "you die joe!\n" unless "@x" eq 'x y z';
358 ########
359 /(?{"{"})/      # Check it outside of eval too
360 EXPECT
361 Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
362 Sequence (?{...}) not terminated or not {}-balanced in regex; marked by <-- HERE in m/(?{ <-- HERE "{"})/ at - line 1.
363 ########
364 /(?{"{"}})/     # Check it outside of eval too
365 EXPECT
366 Unmatched right curly bracket at (re_eval 1) line 1, at end of line
367 syntax error at (re_eval 1) line 1, near ""{"}"
368 Compilation failed in regexp at - line 1.
369 ########
370 BEGIN { @ARGV = qw(a b c d e) }
371 BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
372 END { print "end <",shift,">\nargv <@ARGV>\n" }
373 INIT { print "init <",shift,">\n" }
374 CHECK { print "check <",shift,">\n" }
375 EXPECT
376 argv <a b c d e>
377 begin <a>
378 check <b>
379 init <c>
380 end <d>
381 argv <e>
382 ########
383 -l
384 # fdopen from a system descriptor to a system descriptor used to close
385 # the former.
386 open STDERR, '>&=STDOUT' or die $!;
387 select STDOUT; $| = 1; print fileno STDOUT or die $!;
388 select STDERR; $| = 1; print fileno STDERR or die $!;
389 EXPECT
390 1
391 2
392 ########
393 -w
394 sub testme { my $a = "test"; { local $a = "new test"; print $a }}
395 EXPECT
396 Can't localize lexical variable $a at - line 2.
397 ########
398 package X;
399 sub ascalar { my $r; bless \$r }
400 sub DESTROY { print "destroyed\n" };
401 package main;
402 *s = ascalar X;
403 EXPECT
404 destroyed
405 ########
406 package X;
407 sub anarray { bless [] }
408 sub DESTROY { print "destroyed\n" };
409 package main;
410 *a = anarray X;
411 EXPECT
412 destroyed
413 ########
414 package X;
415 sub ahash { bless {} }
416 sub DESTROY { print "destroyed\n" };
417 package main;
418 *h = ahash X;
419 EXPECT
420 destroyed
421 ########
422 package X;
423 sub aclosure { my $x; bless sub { ++$x } }
424 sub DESTROY { print "destroyed\n" };
425 package main;
426 *c = aclosure X;
427 EXPECT
428 destroyed
429 ########
430 package X;
431 sub any { bless {} }
432 my $f = "FH000"; # just to thwart any future optimisations
433 sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
434 sub DESTROY { print "destroyed\n" }
435 package main;
436 $x = any X; # to bump sv_objcount. IO objs aren't counted??
437 *f = afh X;
438 EXPECT
439 destroyed
440 destroyed
441 ########
442 BEGIN {
443   $| = 1;
444   $SIG{__WARN__} = sub {
445     eval { print $_[0] };
446     die "bar\n";
447   };
448   warn "foo\n";
449 }
450 EXPECT
451 foo
452 bar
453 BEGIN failed--compilation aborted at - line 8.
454 ########
455 package X;
456 @ISA='Y';
457 sub new {
458     my $class = shift;
459     my $self = { };
460     bless $self, $class;
461     my $init = shift;
462     $self->foo($init);
463     print "new", $init;
464     return $self;
465 }
466 sub DESTROY {
467     my $self = shift;
468     print "DESTROY", $self->foo;
469 }
470 package Y;
471 sub attribute {
472     my $self = shift;
473     my $var = shift;
474     if (@_ == 0) {
475         return $self->{$var};
476     } elsif (@_ == 1) {
477         $self->{$var} = shift;
478     }
479 }
480 sub AUTOLOAD {
481     $AUTOLOAD =~ /::([^:]+)$/;
482     my $method = $1;
483     splice @_, 1, 0, $method;
484     goto &attribute;
485 }
486 package main;
487 my $x = X->new(1);
488 for (2..3) {
489     my $y = X->new($_);
490     print $y->foo;
491 }
492 print $x->foo;
493 EXPECT
494 new1new22DESTROY2new33DESTROY31DESTROY1
495 ########
496 re();
497 sub re {
498     my $re = join '', eval 'qr/(??{ $obj->method })/';
499     $re;
500 }
501 EXPECT
502 ########
503 use strict;
504 my $foo = "ZZZ\n";
505 END { print $foo }
506 EXPECT
507 ZZZ
508 ########
509 eval '
510 use strict;
511 my $foo = "ZZZ\n";
512 END { print $foo }
513 ';
514 EXPECT
515 ZZZ
516 ########
517 -w
518 if (@ARGV) { print "" }
519 else {
520   if ($x == 0) { print "" } else { print $x }
521 }
522 EXPECT
523 Use of uninitialized value in numeric eq (==) at - line 4.
524 ########
525 $x = sub {};
526 foo();
527 sub foo { eval { return }; }
528 print "ok\n";
529 EXPECT
530 ok
531 ########
532 # moved to op/lc.t
533 EXPECT
534 ########
535 sub f { my $a = 1; my $b = 2; my $c = 3; my $d = 4; next }
536 my $x = "foo";
537 { f } continue { print $x, "\n" }
538 EXPECT
539 foo
540 ########
541 sub C () { 1 }
542 sub M { $_[0] = 2; }
543 eval "C";
544 M(C);
545 EXPECT
546 Modification of a read-only value attempted at - line 2.
547 ########
548 print qw(ab a\b a\\b);
549 EXPECT
550 aba\ba\b
551 ########
552 # lexicals declared after the myeval() definition should not be visible
553 # within it
554 sub myeval { eval $_[0] }
555 my $foo = "ok 2\n";
556 myeval('sub foo { local $foo = "ok 1\n"; print $foo; }');
557 die $@ if $@;
558 foo();
559 print $foo;
560 EXPECT
561 ok 1
562 ok 2
563 ########
564 # lexicals outside an eval"" should be visible inside subroutine definitions
565 # within it
566 eval <<'EOT'; die $@ if $@;
567 {
568     my $X = "ok\n";
569     eval 'sub Y { print $X }'; die $@ if $@;
570     Y();
571 }
572 EOT
573 EXPECT
574 ok
575 ########
576 # test that closures generated by eval"" hold on to the CV of the eval""
577 # for their entire lifetime
578 $code = eval q[
579   sub { eval '$x = "ok 1\n"'; }
580 ];
581 &{$code}();
582 print $x;
583 EXPECT
584 ok 1
585 ########
586 # This test is here instead of pragma/locale.t because
587 # the bug depends on in the internal state of the locale
588 # settings and pragma/locale messes up that state pretty badly.
589 # We need a "fresh run".
590 BEGIN {
591     eval { require POSIX };
592     if ($@) {
593         exit(0); # running minitest?
594     }
595 }
596 use Config;
597 my $have_setlocale = $Config{d_setlocale} eq 'define';
598 $have_setlocale = 0 if $@;
599 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
600 # and mingw32 uses said silly CRT
601 $have_setlocale = 0 if (($^O eq 'MSWin32' || $^O eq 'NetWare') && $Config{cc} =~ /^(cl|gcc)/i);
602 exit(0) unless $have_setlocale;
603 my @locales;
604 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a|")) {
605     while(<LOCALES>) {
606         chomp;
607         push(@locales, $_);
608     }
609     close(LOCALES);
610 }
611 exit(0) unless @locales;
612 for (@locales) {
613     use POSIX qw(locale_h);
614     use locale;
615     setlocale(LC_NUMERIC, $_) or next;
616     my $s = sprintf "%g %g", 3.1, 3.1;
617     next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
618     print "$_ $s\n";
619 }
620 EXPECT
621 ########
622 die qr(x)
623 EXPECT
624 (?-xism:x) at - line 1.
625 ########
626 # 20001210.003 mjd@plover.com
627 format REMITOUT_TOP =
628 FOO
629 .
630
631 format REMITOUT =
632 BAR
633 .
634
635 # This loop causes a segv in 5.6.0
636 for $lineno (1..61) {
637    write REMITOUT;
638 }
639
640 print "It's OK!";
641 EXPECT
642 It's OK!
643 ########
644 # Inaba Hiroto
645 reset;
646 if (0) {
647   if ("" =~ //) {
648   }
649 }
650 ########
651 # Nicholas Clark
652 $ENV{TERM} = 0;
653 reset;
654 // if 0;
655 ########
656 # Vadim Konovalov
657 use strict;
658 sub new_pmop($) {
659     my $pm = shift;
660     return eval "sub {shift=~/$pm/}";
661 }
662 new_pmop "abcdef"; reset;
663 new_pmop "abcdef"; reset;
664 new_pmop "abcdef"; reset;
665 new_pmop "abcdef"; reset;
666 ########
667 # David Dyck
668 # coredump in 5.7.1
669 close STDERR; die;
670 EXPECT
671 ########
672 -w
673 "x" =~ /(\G?x)?/;       # core dump in 20000716.007
674 ########
675 # Bug 20010515.004
676 my @h = 1 .. 10;
677 bad(@h);
678 sub bad {
679    undef @h;
680    print "O";
681    print for @_;
682    print "K";
683 }
684 EXPECT
685 OK
686 ########
687 # Bug 20010506.041
688 "abcd\x{1234}" =~ /(a)(b[c])(d+)?/i and print "ok\n";
689 EXPECT
690 ok
691 ########
692 # Bug 20010422.005
693 {s//${}/; //}
694 EXPECT
695 syntax error at - line 2, near "${}"
696 Execution of - aborted due to compilation errors.
697 ########
698 # Bug 20010528.007
699 "\x{"
700 EXPECT
701 Missing right brace on \x{} at - line 2, within string
702 Execution of - aborted due to compilation errors.
703 ########
704 my $foo = Bar->new();
705 my @dst;
706 END {
707     ($_ = "@dst") =~ s/\(0x.+?\)/(0x...)/;
708     print $_, "\n";
709 }
710 package Bar;
711 sub new {
712     my Bar $self = bless [], Bar;
713     eval '$self';
714     return $self;
715 }
716 sub DESTROY { 
717     push @dst, "$_[0]";
718 }
719 EXPECT
720 Bar=ARRAY(0x...)
721 ########
722 ######## found by Markov chain stress testing
723 eval "a.b.c.d.e.f;sub"
724 EXPECT
725
726 ######## perlbug ID 20010831.001
727 ($a, b) = (1, 2);
728 EXPECT
729 Can't modify constant item in list assignment at - line 1, near ");"
730 Execution of - aborted due to compilation errors.
731 ######## tying a bareword causes a segfault in 5.6.1
732 tie FOO, "Foo";
733 EXPECT
734 Can't modify constant item in tie at - line 1, near ""Foo";"
735 Execution of - aborted due to compilation errors.
736 ######## undefing constant causes a segfault in 5.6.1 [ID 20010906.019]
737 undef foo;
738 EXPECT
739 Can't modify constant item in undef operator at - line 1, near "foo;"
740 Execution of - aborted due to compilation errors.
741 ######## (?{...}) compilation bounces on PL_rs
742 -0
743 {
744   /(?{ $x })/;
745   # {
746 }
747 BEGIN { print "ok\n" }
748 EXPECT
749 ok
750 ######## read($var, FILE, 1) segfaults on 5.6.1 [ID 20011025.054]
751 read($bla, FILE, 1);
752 EXPECT
753 Can't modify constant item in read at - line 1, near "1)"
754 Execution of - aborted due to compilation errors.
755 ######## scalar ref to file test operator segfaults on 5.6.1 [ID 20011127.155]
756 # This only happens if the filename is 11 characters or less.
757 $foo = \-f "blah";
758 print "ok" if ref $foo && !$$foo;
759 EXPECT
760 ok
761 ######## [ID 20011128.159] 'X' =~ /\X/ segfault in 5.6.1
762 print "ok" if 'X' =~ /\X/;
763 EXPECT
764 ok
765 ######## segfault in 5.6.1 within peep()
766 @a = (1..9);
767 @b = sort { @c = sort { @d = sort { 0 } @a; @d; } @a; } @a;
768 print join '', @a, "\n";
769 EXPECT
770 123456789
771 ######## [ID 20020104.007] "coredump on dbmclose"
772 package Foo;
773 eval { dbmclose %h }; # not all places have dbm* functions
774 if ($@) {
775     print "ok\n";
776     exit 0;
777 }
778 package Foo;
779 sub new {
780         my $proto = shift;
781         my $class = ref($proto) || $proto;
782         my $self  = {};
783         bless($self,$class);
784         my %LT;
785         dbmopen(%LT, "dbmtest", 0666) ||
786             die "Can't open dbmtest because of $!\n";
787         $self->{'LT'} = \%LT;
788         return $self;
789 }
790 sub DESTROY {
791         my $self = shift;
792         dbmclose(%{$self->{'LT'}});
793         1 while unlink 'dbmtest';
794         1 while unlink <dbmtest.*>;
795         print "ok\n";
796 }
797 package main;
798 $test = Foo->new(); # must be package var
799 EXPECT
800 ok