source: trunk/essentials/dev-lang/perl/t/run/fresh_perl.t@ 3184

Last change on this file since 3184 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 18.0 KB
Line 
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 used to abnormally cause segfaults, and other nasty
8# errors that might kill the interpreter and for some reason you can't
9# use an eval().
10
11BEGIN {
12 chdir 't' if -d 't';
13 @INC = '../lib';
14 require './test.pl'; # for which_perl() etc
15}
16
17use strict;
18
19my $Perl = which_perl();
20
21$|=1;
22
23my @prgs = ();
24while(<DATA>) {
25 if(m/^#{8,}\s*(.*)/) {
26 push @prgs, ['', $1];
27 }
28 else {
29 $prgs[-1][0] .= $_;
30 }
31}
32plan tests => scalar @prgs;
33
34foreach my $prog (@prgs) {
35 my($raw_prog, $name) = @$prog;
36
37 my $switch;
38 if ($raw_prog =~ s/^\s*(-\w.*)\n//){
39 $switch = $1;
40 }
41
42 my($prog,$expected) = split(/\nEXPECT\n/, $raw_prog);
43 $prog .= "\n";
44 $expected = '' unless defined $expected;
45
46 if ($prog =~ /^\# SKIP: (.+)/m) {
47 if (eval $1) {
48 ok(1, "Skip: $1");
49 next;
50 }
51 }
52
53 $expected =~ s/\n+$//;
54
55 fresh_perl_is($prog, $expected, { switches => [$switch || ''] }, $name);
56}
57
58__END__
59########
60$a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
61EXPECT
62a := b := c
63########
64$cusp = ~0 ^ (~0 >> 1);
65use integer;
66$, = " ";
67print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, 8 | (($cusp + 1) % 8 + 7), "!\n";
68EXPECT
697 0 0 8 !
70########
71$foo=undef; $foo->go;
72EXPECT
73Can't call method "go" on an undefined value at - line 1.
74########
75BEGIN
76 {
77 "foo";
78 }
79########
80$array[128]=1
81########
82$x=0x0eabcd; print $x->ref;
83EXPECT
84Can't call method "ref" without a package or object reference at - line 1.
85########
86chop ($str .= <DATA>);
87########
88close ($banana);
89########
90$x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
91EXPECT
9225
93########
94eval 'sub bar {print "In bar"}';
95########
96system './perl -ne "print if eof" /dev/null' unless $^O eq 'MacOS'
97########
98chop($file = <DATA>);
99########
100package N;
101sub new {my ($obj,$n)=@_; bless \$n}
102$aa=new N 1;
103$aa=12345;
104print $aa;
105EXPECT
10612345
107########
108$_="foo";
109printf(STDOUT "%s\n", $_);
110EXPECT
111foo
112########
113push(@a, 1, 2, 3,)
114########
115quotemeta ""
116########
117for ("ABCDE") {
118 &sub;
119s/./&sub($&)/eg;
120print;}
121sub sub {local($_) = @_;
122$_ x 4;}
123EXPECT
124Modification of a read-only value attempted at - line 3.
125########
126package FOO;sub new {bless {FOO => BAR}};
127package main;
128use strict vars;
129my $self = new FOO;
130print $$self{FOO};
131EXPECT
132BAR
133########
134$_="foo";
135s/.{1}//s;
136print;
137EXPECT
138oo
139########
140print scalar ("foo","bar")
141EXPECT
142bar
143########
144sub by_number { $a <=> $b; };# inline function for sort below
145$as_ary{0}="a0";
146@ordered_array=sort by_number keys(%as_ary);
147########
148sub NewShell
149{
150 local($Host) = @_;
151 my($m2) = $#Shells++;
152 $Shells[$m2]{HOST} = $Host;
153 return $m2;
154}
155
156sub ShowShell
157{
158 local($i) = @_;
159}
160
161&ShowShell(&NewShell(beach,Work,"+0+0"));
162&ShowShell(&NewShell(beach,Work,"+0+0"));
163&ShowShell(&NewShell(beach,Work,"+0+0"));
164########
165 {
166 package FAKEARRAY;
167
168 sub TIEARRAY
169 { print "TIEARRAY @_\n";
170 die "bomb out\n" unless $count ++ ;
171 bless ['foo']
172 }
173 sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
174 sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
175 sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
176 }
177
178eval 'tie @h, FAKEARRAY, fred' ;
179tie @h, FAKEARRAY, fred ;
180EXPECT
181TIEARRAY FAKEARRAY fred
182TIEARRAY FAKEARRAY fred
183DESTROY
184########
185BEGIN { die "phooey\n" }
186EXPECT
187phooey
188BEGIN failed--compilation aborted at - line 1.
189########
190BEGIN { 1/$zero }
191EXPECT
192Illegal division by zero at - line 1.
193BEGIN failed--compilation aborted at - line 1.
194########
195BEGIN { undef = 0 }
196EXPECT
197Modification of a read-only value attempted at - line 1.
198BEGIN failed--compilation aborted at - line 1.
199########
200{
201 package foo;
202 sub PRINT {
203 shift;
204 print join(' ', reverse @_)."\n";
205 }
206 sub PRINTF {
207 shift;
208 my $fmt = shift;
209 print sprintf($fmt, @_)."\n";
210 }
211 sub TIEHANDLE {
212 bless {}, shift;
213 }
214 sub READLINE {
215 "Out of inspiration";
216 }
217 sub DESTROY {
218 print "and destroyed as well\n";
219 }
220 sub READ {
221 shift;
222 print STDOUT "foo->can(READ)(@_)\n";
223 return 100;
224 }
225 sub GETC {
226 shift;
227 print STDOUT "Don't GETC, Get Perl\n";
228 return "a";
229 }
230}
231{
232 local(*FOO);
233 tie(*FOO,'foo');
234 print FOO "sentence.", "reversed", "a", "is", "This";
235 print "-- ", <FOO>, " --\n";
236 my($buf,$len,$offset);
237 $buf = "string";
238 $len = 10; $offset = 1;
239 read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
240 getc(FOO) eq "a" or die "foo->GETC failed";
241 printf "%s is number %d\n", "Perl", 1;
242}
243EXPECT
244This is a reversed sentence.
245-- Out of inspiration --
246foo->can(READ)(string 10 1)
247Don't GETC, Get Perl
248Perl is number 1
249and destroyed as well
250########
251my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
252EXPECT
2532 2 2
254########
255# used to attach defelem magic to all immortal values,
256# which made restore of local $_ fail.
257foo(2>1);
258sub foo { bar() for @_; }
259sub bar { local $_; }
260print "ok\n";
261EXPECT
262ok
263########
264@a = ($a, $b, $c, $d) = (5, 6);
265print "ok\n"
266 if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
267EXPECT
268ok
269########
270print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
271EXPECT
272ok
273########
274print "ok\n" if ("\0" lt "\xFF");
275EXPECT
276ok
277########
278open(H,$^O eq 'MacOS' ? ':run:fresh_perl.t' : 'run/fresh_perl.t'); # must be in the 't' directory
279stat(H);
280print "ok\n" if (-e _ and -f _ and -r _);
281EXPECT
282ok
283########
284sub thing { 0 || return qw(now is the time) }
285print thing(), "\n";
286EXPECT
287nowisthetime
288########
289$ren = 'joy';
290$stimpy = 'happy';
291{ local $main::{ren} = *stimpy; print $ren, ' ' }
292print $ren, "\n";
293EXPECT
294happy joy
295########
296$stimpy = 'happy';
297{ local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
298print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
299EXPECT
300happy joy
301########
302package p;
303sub func { print 'really ' unless wantarray; 'p' }
304sub groovy { 'groovy' }
305package main;
306print p::func()->groovy(), "\n"
307EXPECT
308really groovy
309########
310@list = ([ 'one', 1 ], [ 'two', 2 ]);
311sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
312print scalar(map &func($_), 1 .. 3), " ",
313 scalar(map scalar &func($_), 1 .. 3), "\n";
314EXPECT
3152 3
316########
317($k, $s) = qw(x 0);
318@{$h{$k}} = qw(1 2 4);
319for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
320print "bogus\n" unless $s == 7;
321########
322my $a = 'outer';
323eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
324eval { my $x = 'peace'; eval q[ print "$x\n" ] }
325EXPECT
326inner peace
327########
328-w
329$| = 1;
330sub foo {
331 print "In foo1\n";
332 eval 'sub foo { print "In foo2\n" }';
333 print "Exiting foo1\n";
334}
335foo;
336foo;
337EXPECT
338In foo1
339Subroutine foo redefined at (eval 1) line 1.
340Exiting foo1
341In foo2
342########
343$s = 0;
344map {#this newline here tickles the bug
345$s += $_} (1,2,4);
346print "eat flaming death\n" unless ($s == 7);
347########
348sub foo { local $_ = shift; split; @_ }
349@x = foo(' x y z ');
350print "you die joe!\n" unless "@x" eq 'x y z';
351########
352/(?{"{"})/ # Check it outside of eval too
353EXPECT
354Sequence (?{...}) not terminated or not {}-balanced in regex; marked by <-- HERE in m/(?{ <-- HERE "{"})/ at - line 1.
355########
356/(?{"{"}})/ # Check it outside of eval too
357EXPECT
358Unmatched right curly bracket at (re_eval 1) line 1, at end of line
359syntax error at (re_eval 1) line 1, near ""{"}"
360Compilation failed in regexp at - line 1.
361########
362BEGIN { @ARGV = qw(a b c d e) }
363BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
364END { print "end <",shift,">\nargv <@ARGV>\n" }
365INIT { print "init <",shift,">\n" }
366CHECK { print "check <",shift,">\n" }
367EXPECT
368argv <a b c d e>
369begin <a>
370check <b>
371init <c>
372end <d>
373argv <e>
374########
375-l
376# fdopen from a system descriptor to a system descriptor used to close
377# the former.
378open STDERR, '>&=STDOUT' or die $!;
379select STDOUT; $| = 1; print fileno STDOUT or die $!;
380select STDERR; $| = 1; print fileno STDERR or die $!;
381EXPECT
3821
3832
384########
385-w
386sub testme { my $a = "test"; { local $a = "new test"; print $a }}
387EXPECT
388Can't localize lexical variable $a at - line 1.
389########
390package X;
391sub ascalar { my $r; bless \$r }
392sub DESTROY { print "destroyed\n" };
393package main;
394*s = ascalar X;
395EXPECT
396destroyed
397########
398package X;
399sub anarray { bless [] }
400sub DESTROY { print "destroyed\n" };
401package main;
402*a = anarray X;
403EXPECT
404destroyed
405########
406package X;
407sub ahash { bless {} }
408sub DESTROY { print "destroyed\n" };
409package main;
410*h = ahash X;
411EXPECT
412destroyed
413########
414package X;
415sub aclosure { my $x; bless sub { ++$x } }
416sub DESTROY { print "destroyed\n" };
417package main;
418*c = aclosure X;
419EXPECT
420destroyed
421########
422package X;
423sub any { bless {} }
424my $f = "FH000"; # just to thwart any future optimisations
425sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
426sub DESTROY { print "destroyed\n" }
427package main;
428$x = any X; # to bump sv_objcount. IO objs aren't counted??
429*f = afh X;
430EXPECT
431destroyed
432destroyed
433########
434BEGIN {
435 $| = 1;
436 $SIG{__WARN__} = sub {
437 eval { print $_[0] };
438 die "bar\n";
439 };
440 warn "foo\n";
441}
442EXPECT