This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: 2 patches: goto.t, B.pm/xs
[perl5.git] / t / TEST
CommitLineData
8d063cd8
LW
1#!./perl
2
8d063cd8
LW
3# This is written in a peculiar style, since we're trying to avoid
4# most of the constructs we'll be testing for.
5
a687059c
LW
6$| = 1;
7
60e23f2f
MS
8# Let tests know they're running in the perl core. Useful for modules
9# which live dual lives on CPAN.
10$ENV{PERL_CORE} = 1;
11
cc6ae9e5
CB
12# remove empty elements due to insertion of empty symbols via "''p1'" syntax
13@ARGV = grep($_,@ARGV) if $^O eq 'VMS';
14
5d9a6404 15# Cheesy version of Getopt::Std. Maybe we should replace it with that.
b326da91 16@argv = ();
5d9a6404
MS
17if ($#ARGV >= 0) {
18 foreach my $idx (0..$#ARGV) {
b326da91 19 push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
5a6e071d 20 $core = 1 if $1 eq 'core';
5d9a6404 21 $verbose = 1 if $1 eq 'v';
e018f8be 22 $torture = 1 if $1 eq 'torture';
5d9a6404 23 $with_utf= 1 if $1 eq 'utf8';
b26492ee
RGS
24 $bytecompile = 1 if $1 eq 'bytecompile';
25 $compile = 1 if $1 eq 'compile';
26 $taintwarn = 1 if $1 eq 'taintwarn';
43651d81 27 $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
485988ae
RH
28 if ($1 =~ /^deparse(,.+)?$/) {
29 $deparse = 1;
30 $deparse_opts = $1;
31 }
5d9a6404 32 }
8d063cd8 33}
b326da91 34@ARGV = @argv;
8d063cd8 35
378cc40b
LW
36chdir 't' if -f 't/TEST';
37
3e6e8be7 38die "You need to run \"make test\" first to set things up.\n"
196918b0 39 unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
4633a7c4 40
7a315204 41if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
09187cb1
JH
42 unless (-x 'perl.third') {
43 unless (-x '../perl.third') {
44 die "You need to run \"make perl.third first.\n";
45 }
46 else {
47 print "Symlinking ../perl.third as perl.third...\n";
48 die "Failed to symlink: $!\n"
49 unless symlink("../perl.third", "perl.third");
50 die "Symlinked but no executable perl.third: $!\n"
51 unless -x 'perl.third';
52 }
53 }
54}
55
3fb91a5e
GS
56# check leakage for embedders
57$ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
58
4633a7c4 59$ENV{EMXSHELL} = 'sh'; # For OS/2
748a9306 60
24c841ba
MS
61# Roll your own File::Find!
62use TestInit;
63use File::Spec;
64my $curdir = File::Spec->curdir;
65my $updir = File::Spec->updir;
66
67sub _find_tests {
68 my($dir) = @_;
93e325a7 69 opendir DIR, $dir or die "Trouble opening $dir: $!";
a1886d87 70 foreach my $f (sort { $a cmp $b } readdir DIR) {
0b834283
AB
71 next if $f eq $curdir or $f eq $updir or
72 $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
24c841ba 73
cc6ae9e5 74 my $fullpath = File::Spec->catfile($dir, $f);
24c841ba
MS
75
76 _find_tests($fullpath) if -d $fullpath;
cc6ae9e5 77 $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
24c841ba
MS
78 push @ARGV, $fullpath if $f =~ /\.t$/;
79 }
80}
81
cc6ae9e5
CB
82sub _quote_args {
83 my ($args) = @_;
84 my $argstring = '';
85
86 foreach (split(/\s+/,$args)) {
87 # In VMS protect with doublequotes because otherwise
88 # DCL will lowercase -- unless already doublequoted.
89 $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
90 $argstring .= ' ' . $_;
91 }
92 return $argstring;
93}
94
24c841ba 95unless (@ARGV) {
9f3d340b 96 foreach my $dir (qw(base comp cmd run io op uni)) {
24c841ba
MS
97 _find_tests($dir);
98 }
5a6e071d 99 _find_tests("lib") unless $core;
cc6ae9e5 100 my $mani = File::Spec->catfile($updir, "MANIFEST");
7a315204 101 if (open(MANI, $mani)) {
80ffb5f9 102 while (<MANI>) { # similar code in t/harness
9c9537e6 103 if (m!^(ext/\S+/?(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
5a6e071d
PJ
104 $t = $1;
105 if (!$core || $t =~ m!^lib/[a-z]!)
106 {
cc6ae9e5 107 $path = File::Spec->catfile($updir, $t);
73ddec28
RB
108 push @ARGV, $path;
109 $name{$path} = $t;
5a6e071d 110 }
7a315204
JH
111 }
112 }
35d88760 113 close MANI;
7a315204
JH
114 } else {
115 warn "$0: cannot open $mani: $!\n";
116 }
e018f8be 117 unless ($core) {
d44161bf 118 _find_tests('pod');
e018f8be
JH
119 _find_tests('x2p');
120 _find_tests('japh') if $torture;
121 }
8d063cd8
LW
122}
123
7a315204 124# Tests known to cause infinite loops for the perlcc tests.
595ae481 125# %infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
24c841ba 126%infinite = ();
6ee623d5 127
485988ae 128if ($deparse) {
f193aa2f
MS
129 _testprogs('deparse', '', @ARGV);
130}
1df34986
AE
131elsif( $compile ) {
132 _testprogs('compile', '', @ARGV);
133}
134elsif( $bytecompile ) {
135 _testprogs('bytecompile', '', @ARGV);
f193aa2f
MS
136}
137else {
138 _testprogs('compile', '', @ARGV) if -e "../testcompile";
139 _testprogs('perl', '', @ARGV);
485988ae 140}
6ee623d5 141
bb365837
GS
142sub _testprogs {
143 $type = shift @_;
f193aa2f 144 $args = shift;
bb365837 145 @tests = @_;
6ee623d5 146
bb365837 147 print <<'EOT' if ($type eq 'compile');
7a315204 148------------------------------------------------------------------------------
6ee623d5 149TESTING COMPILER
7a315204 150------------------------------------------------------------------------------
bb365837
GS
151EOT
152
485988ae 153 print <<'EOT' if ($type eq 'deparse');
7a315204 154------------------------------------------------------------------------------
485988ae 155TESTING DEPARSER
7a315204 156------------------------------------------------------------------------------
485988ae
RH
157EOT
158
566ece03 159 print <<EOT if ($type eq 'bytecompile');
1df34986
AE
160------------------------------------------------------------------------------
161TESTING BYTECODE COMPILER
162------------------------------------------------------------------------------
163EOT
164
595ae481 165 $ENV{PERLCC_TIMEOUT} = 120
9636a016 166 if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
ef712cf7 167
bb365837
GS
168 $bad = 0;
169 $good = 0;
170 $total = @tests;
171 $files = 0;
172 $totmax = 0;
73ddec28 173
cc6ae9e5
CB
174 foreach my $t (@tests) {
175 unless (exists $name{$t}) {
176 my $tname = File::Spec->catfile('t',$t);
177 $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
178 $name{$t} = $tname;
179 }
73ddec28 180 }
908801fe 181 my $maxlen = 0;
73ddec28
RB
182 foreach (@name{@tests}) {
183 s/\.\w+\z/./;
184 my $len = length ;
185 $maxlen = $len if $len > $maxlen;
088b5126 186 }
908801fe 187 # + 3 : we want three dots between the test name and the "ok"
73ddec28 188 $dotdotdot = $maxlen + 3 ;
7a834142 189 my $valgrind = 0;
da51b73c 190 my $valgrind_log = 'current.valgrind';
bb365837
GS
191 while ($test = shift @tests) {
192
193 if ( $infinite{$test} && $type eq 'compile' ) {
595ae481 194 print STDERR "$test creates infinite loop! Skipping.\n";
bb365837 195 next;
6ee623d5 196 }
bb365837
GS
197 if ($test =~ /^$/) {
198 next;
6ee623d5 199 }
485988ae
RH
200 if ($type eq 'deparse') {
201 if ($test eq "comp/redef.t") {
202 # Redefinition happens at compile time
203 next;
204 }
7a834142 205 elsif ($test =~ m{lib/Switch/t/}) {
485988ae
RH
206 # B::Deparse doesn't support source filtering
207 next;
208 }
209 }
cc6ae9e5
CB
210 $te = $name{$test} . '.' x ($dotdotdot - length($name{$test}));
211
212 if ($^O ne 'VMS') { # defer printing on VMS due to piping bug
213 print $te;
214 $te = '';
215 }
bb365837 216
7a315204
JH
217 $test = $OVER{$test} if exists $OVER{$test};
218
2f6bec1d
MS
219 open(SCRIPT,"<$test") or die "Can't run $test.\n";
220 $_ = <SCRIPT>;
221 close(SCRIPT) unless ($type eq 'deparse');
5dc83c40 222 if (/#!.*\bperl.*\s-\w*([tT])/) {
6537fe72 223 $switch = qq{"-$1"};
2f6bec1d
MS
224 }
225 else {
b26492ee
RGS
226 if ($taintwarn) {
227 # not all tests are expected to pass with this option
228 $switch = '"-t"';
229 }
230 else {
231 $switch = '';
232 }
2f6bec1d 233 }
6ee623d5 234
b326da91 235 my $test_executable; # for 'compile' tests
485988ae
RH
236 my $file_opts = "";
237 if ($type eq 'deparse') {
238 # Look for #line directives which change the filename
239 while (<SCRIPT>) {
240 $file_opts .= ",-f$3$4"
241 if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
242 }
243 close(SCRIPT);
244 }
7a315204 245
7a315204 246 my $utf = $with_utf ? '-I../lib -Mutf8' : '';
4343e7c3 247 my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
485988ae
RH
248 if ($type eq 'deparse') {
249 my $deparse =
127212b2 250 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
485988ae 251 "-l$deparse_opts$file_opts ".
7a315204
JH
252 "$test > $test.dp ".
253 "&& ./perl $testswitch $switch -I../lib $test.dp |";
485988ae
RH
254 open(RESULTS, $deparse)
255 or print "can't deparse '$deparse': $!.\n";
256 }
1df34986 257 elsif ($type eq 'bytecompile') {
c7e45529
AE
258 my ($pwd, $null);
259 if( $^O eq 'MSWin32') {
260 $pwd = `cd`;
261 $null = 'nul';
262 } else {
263 $pwd = `pwd`;
264 $null = '/dev/null';
265 }
266 chomp $pwd;
267 my $perl = $ENV{PERL} || "$pwd/perl";
268 my $bswitch = "-MO=Bytecode,-H,-TI,-s$pwd/$test,";
566ece03 269 $bswitch .= "-TF$test.plc,"
1df34986
AE
270 if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
271 $bswitch .= "-k,"
272 if $test =~ m(deparse|terse|ext/Storable/t/code);
1df34986
AE
273 $bswitch .= "-b,"
274 if $test =~ m(op/getpid);
275 my $bytecompile =
276 "$perl $testswitch $switch -I../lib $bswitch".
c7e45529
AE
277 "-o$test.plc $test 2>$null &&".
278 "$perl $testswitch $switch -I../lib $utf $test.plc |";
1df34986
AE
279 open(RESULTS,$bytecompile)
280 or print "can't byte-compile '$bytecompile': $!.\n";
281 }
485988ae 282 elsif ($type eq 'perl') {
a7da9a42 283 my $perl = $ENV{PERL} || './perl';
da51b73c 284 my $redir = $^O eq 'VMS' ? '2>&1' : '';
7a834142 285 if ($ENV{PERL_VALGRIND}) {
d44161bf
MHM
286 $perl = "valgrind --suppressions=perl.supp --leak-check=yes "
287 . "--leak-resolution=high --show-reachable=yes "
da51b73c
MHM
288 . "--num-callers=50 --logfile-fd=3 $perl";
289 $redir = "3>$valgrind_log";
7a834142 290 }
cc6ae9e5 291 my $run = "$perl" . _quote_args("$testswitch $switch $utf") . " $test $redir|";
be24517c 292 open(RESULTS,$run) or print "can't run '$run': $!.\n";
d638aca2
GS
293 }
294 else {
b326da91
MB
295 my $compile;
296 my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
9d2bbe64
MB
297 # -O9 for good measure, -fcog is broken ATM
298 "$switch -Wb=-O9,-fno-cog -L .. " .
b326da91
MB
299 "-I \".. ../lib/CORE\" $args $utf $test -o ";
300
301 if( $^O eq 'MSWin32' ) {
302 $test_executable = "$test.exe";
303 # hopefully unused name...
304 open HACK, "> xweghyz.pl";
305 print HACK <<EOT;
306#!./perl
307
308open HACK, '.\\perl $pl2c $test_executable |';
309# cl.exe prints the name of the .c file on stdout (\%^\$^#)
6d73d07f 310while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
b326da91
MB
311open HACK, '$test_executable |';
312while(<HACK>) {print}
313EOT
314 close HACK;
315 $compile = 'xweghyz.pl |';
316 }
317 else {
318 $test_executable = "$test.plc";
319 $compile = "./perl $pl2c $test_executable && $test_executable |";
320 }
321 unlink $test_executable if -f $test_executable;
be24517c
JH
322 open(RESULTS, $compile)
323 or print "can't compile '$compile': $!.\n";
6ee623d5 324 }
d638aca2 325
b326da91
MB
326 $ok = 0;
327 $next = 0;
21c74f43
A
328 my $seen_leader = 0;
329 my $seen_ok = 0;
bb365837 330 while (<RESULTS>) {
cc6ae9e5 331 next if /^\s*$/; # skip blank lines
bb365837
GS
332 if ($verbose) {
333 print $_;
334 }
21c74f43 335 unless (/^\#/) {
809908f7 336 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
bb365837 337 $max = $1;
809908f7 338 %todo = map { $_ => 1 } split / /, $3 if $3;
bb365837
GS
339 $totmax += $max;
340 $files += 1;
21c74f43
A
341 unless ($seen_ok) {
342 $next = 1;
343 $ok = 1;
344 }
345 $seen_leader = 1;
bb365837
GS
346 }
347 else {
21c74f43
A
348 if (/^(not )?ok (\d+)[^\#]*(\s*\#.*)?/) {
349 unless ($seen_leader) {
350 unless ($seen_ok) {
351 $next = 1;
352 $ok = 1;
353 }
37ce32a7 354 }
21c74f43
A
355 $seen_ok = 1;
356 if ($2 == $next) {
357 my($not, $num, $extra) = ($1, $2, $3);
6c0731c3 358 my($istodo) = $extra =~ /#\s*TODO/ if $extra;
21c74f43
A
359 $istodo = 1 if $todo{$num};
360
361 if( $not && !$istodo ) {
362 $ok = 0;
363 $next = $num;
364 last;
365 }
366 else {
367 $next = $next + 1;
368 }
37ce32a7 369 }
d667a7e6
A
370 }
371 elsif (/^Bail out!\s*(.*)/i) { # magic words
372 die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
bb365837
GS
373 }
374 else {
375 $ok = 0;
376 }
8d063cd8
LW
377 }
378 }
379 }
bb365837 380 close RESULTS;
7a834142 381 if ($ENV{PERL_VALGRIND}) {
da51b73c
MHM
382 my @valgrind;
383 if (-e $valgrind_log) {
384 if (open(V, $valgrind_log)) {
385 @valgrind = <V>;
386 close V;
387 } else {
388 warn "$0: Failed to open '$valgrind_log': $!\n";
389 }
390 }
7a834142 391 if (@valgrind) {
d44161bf
MHM
392 my $leaks = 0;
393 my $errors = 0;
7a834142
JH
394 for my $i (0..$#valgrind) {
395 local $_ = $valgrind[$i];
d44161bf
MHM
396 if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
397 $errors += $1; # there may be multiple error summaries
398 } elsif (/^==\d+== LEAK SUMMARY:/) {
399 for my $off (1 .. 4) {
400 if ($valgrind[$i+$off] =~
401 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
402 $leaks += $1;
403 }
404 }
7a834142
JH
405 }
406 }
d44161bf 407 if ($errors or $leaks) {
da51b73c 408 if (rename $valgrind_log, "$test.valgrind") {
d44161bf
MHM
409 $valgrind++;
410 } else {
411 warn "$0: Failed to create '$test.valgrind': $!\n";
7a834142
JH
412 }
413 }
414 } else {
415 warn "No valgrind output?\n";
416 }
da51b73c
MHM
417 if (-e $valgrind_log) {
418 unlink $valgrind_log
419 or warn "$0: Failed to unlink '$valgrind_log': $!\n";
420 }
7a834142 421 }
485988ae
RH
422 if ($type eq 'deparse') {
423 unlink "./$test.dp";
424 }
211f317f
JH
425 if ($ENV{PERL_3LOG}) {
426 my $tpp = $test;
3716a21d 427 $tpp =~ s:^\.\./::;
9c54ecba 428 $tpp =~ s:/:_:g;
3716a21d
JH
429 $tpp =~ s:\.t$:.3log:;
430 rename("perl.3log", $tpp) ||
431 die "rename: perl3.log to $tpp: $!\n";
211f317f 432 }
bb365837 433 $next = $next - 1;
b326da91
MB
434 # test if the compiler compiled something
435 if( $type eq 'compile' && !-e "$test_executable" ) {
436 $ok = 0;
437 print "Test did not compile\n";
438 }
439 if ($ok && $next == $max ) {
bb365837 440 if ($max) {
cc6ae9e5 441 print "${te}ok\n";
bb365837
GS
442 $good = $good + 1;
443 }
444 else {
cc6ae9e5 445 print "${te}skipping test on this platform\n";
bb365837
GS
446 $files -= 1;
447 }
bcce72a7 448 }
bb365837
GS
449 else {
450 $next += 1;
26affc6c
AT
451 if ($next > $max) {
452 print "${te}FAILED at test $next\tpossibly due to extra output\n";
453 }
454 else {
455 print "${te}FAILED at test $next\n";
456 }
bb365837
GS
457 $bad = $bad + 1;
458 $_ = $test;
459 if (/^base/) {
460 die "Failed a basic test--cannot continue.\n";
461 }
8d063cd8
LW
462 }
463 }
8d063cd8 464
bb365837
GS
465 if ($bad == 0) {
466 if ($ok) {
467 print "All tests successful.\n";
468 # XXX add mention of 'perlbug -ok' ?
469 }
470 else {
471 die "FAILED--no tests were run for some reason.\n";
472 }
8d063cd8 473 }
bb365837 474 else {
ba1398cf 475 $pct = $files ? sprintf("%.2f", ($files - $bad) / $files * 100) : "0.00";
bb365837 476 if ($bad == 1) {
e824fb2c 477 warn "Failed 1 test script out of $files, $pct% okay.\n";
bb365837
GS
478 }
479 else {
e824fb2c 480 warn "Failed $bad test scripts out of $files, $pct% okay.\n";
bb365837 481 }
4e4732c1 482 warn <<'SHRDLU_1';
f7d228c6
JH
483### Since not all tests were successful, you may want to run some of
484### them individually and examine any diagnostic messages they produce.
485### See the INSTALL document's section on "make test".
4e4732c1
NC
486SHRDLU_1
487 warn <<'SHRDLU_2' if $good / $total > 0.8;
f7d228c6
JH
488### You have a good chance to get more information by running
489### ./perl harness
490### in the 't' directory since most (>=80%) of the tests succeeded.
4e4732c1
NC
491SHRDLU_2
492 if (eval {require Config; import Config; 1}) {
e6af294e 493 if ($Config{usedl} && (my $p = $Config{ldlibpthname})) {
4e4732c1 494 warn <<SHRDLU_3;
f7d228c6
JH
495### You may have to set your dynamic library search path,
496### $p, to point to the build directory:
4e4732c1
NC
497SHRDLU_3
498 if (exists $ENV{$p} && $ENV{$p} ne '') {
499 warn <<SHRDLU_4a;
f7d228c6
JH
500### setenv $p `pwd`:\$$p; cd t; ./perl harness
501### $p=`pwd`:\$$p; export $p; cd t; ./perl harness
502### export $p=`pwd`:\$$p; cd t; ./perl harness
4e4732c1
NC
503SHRDLU_4a
504 } else {
505 warn <<SHRDLU_4b;
f7d228c6
JH
506### setenv $p `pwd`; cd t; ./perl harness
507### $p=`pwd`; export $p; cd t; ./perl harness
508### export $p=`pwd`; cd t; ./perl harness
4e4732c1
NC
509SHRDLU_4b
510 }
511 warn <<SHRDLU_5;
f7d228c6
JH
512### for csh-style shells, like tcsh; or for traditional/modern
513### Bourne-style shells, like bash, ksh, and zsh, respectively.
4e4732c1
NC
514SHRDLU_5
515 }
afd33fa9 516 }
bb365837
GS
517 }
518 ($user,$sys,$cuser,$csys) = times;
519 print sprintf("u=%g s=%g cu=%g cs=%g scripts=%d tests=%d\n",
520 $user,$sys,$cuser,$csys,$files,$totmax);
7a834142
JH
521 if ($ENV{PERL_VALGRIND}) {
522 my $s = $valgrind == 1 ? '' : 's';
523 print "$valgrind valgrind report$s created.\n", ;
524 }
6ee623d5 525}
3e6e8be7 526exit ($bad != 0);