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