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