Re: [perl #37731] junk and uninit'ed values in tied scalars
[p5sagit/p5-mst-13.2.git] / t / TEST
1 #!./perl
2
3 # This is written in a peculiar style, since we're trying to avoid
4 # most of the constructs we'll be testing for.  (This comment is
5 # probably obsolete on the avoidance side, though still currrent
6 # on the peculiarity side.)
7
8 $| = 1;
9
10 # for testing TEST only
11 #BEGIN { require '../lib/strict.pm'; strict->import() };
12 #BEGIN { require '../lib/warnings.pm'; warnings->import() };
13
14 # Let tests know they're running in the perl core.  Useful for modules
15 # which live dual lives on CPAN.
16 $ENV{PERL_CORE} = 1;
17
18 # remove empty elements due to insertion of empty symbols via "''p1'" syntax
19 @ARGV = grep($_,@ARGV) if $^O eq 'VMS';
20 our $show_elapsed_time = $ENV{HARNESS_TIMER} || 0;
21
22 # Cheesy version of Getopt::Std.  Maybe we should replace it with that.
23 {
24     my @argv = ();
25     foreach my $idx (0..$#ARGV) {
26         push( @argv, $ARGV[$idx] ), next unless $ARGV[$idx] =~ /^-(\S+)$/;
27         $::core    = 1 if $1 eq 'core';
28         $::verbose = 1 if $1 eq 'v';
29         $::torture = 1 if $1 eq 'torture';
30         $::with_utf8 = 1 if $1 eq 'utf8';
31         $::with_utf16 = 1 if $1 eq 'utf16';
32         $::bytecompile = 1 if $1 eq 'bytecompile';
33         $::compile = 1 if $1 eq 'compile';
34         $::taintwarn = 1 if $1 eq 'taintwarn';
35         $ENV{PERL_CORE_MINITEST} = 1 if $1 eq 'minitest';
36         if ($1 =~ /^deparse(,.+)?$/) {
37             $::deparse = 1;
38             $::deparse_opts = $1;
39         }
40     }
41     @ARGV = @argv;
42 }
43
44 chdir 't' if -f 't/TEST';
45
46 die "You need to run \"make test\" first to set things up.\n"
47   unless -e 'perl' or -e 'perl.exe' or -e 'perl.pm';
48
49 if ($ENV{PERL_3LOG}) { # Tru64 third(1) tool, see perlhack
50     unless (-x 'perl.third') {
51         unless (-x '../perl.third') {
52             die "You need to run \"make perl.third first.\n";
53         }
54         else {
55             print "Symlinking ../perl.third as perl.third...\n";
56             die "Failed to symlink: $!\n"
57                 unless symlink("../perl.third", "perl.third");
58             die "Symlinked but no executable perl.third: $!\n"
59                 unless -x 'perl.third';
60         }
61     }
62 }
63
64 # check leakage for embedders
65 $ENV{PERL_DESTRUCT_LEVEL} = 2 unless exists $ENV{PERL_DESTRUCT_LEVEL};
66
67 $ENV{EMXSHELL} = 'sh';        # For OS/2
68
69 # Roll your own File::Find!
70 use TestInit;
71 use File::Spec;
72 if ($show_elapsed_time) { require Time::HiRes }
73 my $curdir = File::Spec->curdir;
74 my $updir  = File::Spec->updir;
75
76 sub _find_tests {
77     my($dir) = @_;
78     opendir DIR, $dir or die "Trouble opening $dir: $!";
79     foreach my $f (sort { $a cmp $b } readdir DIR) {
80         next if $f eq $curdir or $f eq $updir or
81             $f =~ /^(?:CVS|RCS|SCCS|\.svn)$/;
82
83         my $fullpath = File::Spec->catfile($dir, $f);
84
85         _find_tests($fullpath) if -d $fullpath;
86         $fullpath = VMS::Filespec::unixify($fullpath) if $^O eq 'VMS';
87         push @ARGV, $fullpath if $f =~ /\.t$/;
88     }
89 }
90
91 sub _quote_args {
92     my ($args) = @_;
93     my $argstring = '';
94
95     foreach (split(/\s+/,$args)) {
96        # In VMS protect with doublequotes because otherwise
97        # DCL will lowercase -- unless already doublequoted.
98        $_ = q(").$_.q(") if ($^O eq 'VMS') && !/^\"/ && length($_) > 0;
99        $argstring .= ' ' . $_;
100     }
101     return $argstring;
102 }
103
104 sub _populate_hash {
105     return map {$_, 1} split /\s+/, $_[0];
106 }
107
108 unless (@ARGV) {
109     foreach my $dir (qw(base comp cmd run io op uni)) {
110         _find_tests($dir);
111     }
112     _find_tests("lib") unless $::core;
113     # Config.pm may be broken for make minitest. And this is only a refinement
114     # for skipping tests on non-default builds, so it is allowed to fail.
115     # What we want to to is make a list of extensions which we did not build.
116     my $configsh = File::Spec->catfile($updir, "config.sh");
117     my %skip;
118     if (-f $configsh) {
119         my (%extensions, %known_extensions);
120         open FH, $configsh or die "Can't open $configsh: $!";
121         while (<FH>) {
122             if (/^extensions=['"](.*)['"]$/) {
123                 # Deliberate string interpolation to avoid triggering possible
124                 # $1 resetting bugs.
125                 %extensions = _populate_hash ("$1");
126             }
127             elsif (/^known_extensions=['"](.*)['"]$/) {
128                 %known_extensions = _populate_hash ($1);
129             }
130         }
131         if (%extensions) {
132             if (%known_extensions) {
133                 foreach (keys %known_extensions) {
134                     $skip{$_}++ unless $extensions{$_};
135                 }
136             } else {
137                 warn "No known_extensions line found in $configsh";
138             }
139         } else {
140             warn "No extensions line found in $configsh";
141         }
142     }
143     my $mani = File::Spec->catfile($updir, "MANIFEST");
144     if (open(MANI, $mani)) {
145         my $ext_pat = $^O eq 'MSWin32' ? '(?:win32/)?ext' : 'ext';
146         while (<MANI>) { # similar code in t/harness
147             if (m!^($ext_pat/(\S+)/+(?:[^/\s]+\.t|test\.pl)|lib/\S+?(?:\.t|test\.pl))\s!) {
148                 my $t = $1;
149                 my $extension = $2;
150                 if (!$::core || $t =~ m!^lib/[a-z]!)
151                 {
152                     if (defined $extension) {
153                         $extension =~ s!/t$!!;
154                         # XXX Do I want to warn that I'm skipping these?
155                         next if $skip{$extension};
156                     }
157                     my $path = File::Spec->catfile($updir, $t);
158                     push @ARGV, $path;
159                     $::path_to_name{$path} = $t;
160                 }
161             }
162         }
163         close MANI;
164     } else {
165         warn "$0: cannot open $mani: $!\n";
166     }
167     unless ($::core) {
168         _find_tests('pod');
169         _find_tests('x2p');
170         _find_tests('japh') if $::torture;
171     }
172 }
173
174 # Tests known to cause infinite loops for the perlcc tests.
175 # %::infinite = ( 'comp/require.t', 1, 'op/bop.t', 1, 'lib/hostname.t', 1 );
176 %::infinite = ();
177
178 if ($::deparse) {
179     _testprogs('deparse', '',   @ARGV);
180 }
181 elsif( $::compile ) {
182     _testprogs('compile', '',   @ARGV);
183 }
184 elsif( $::bytecompile ) {
185     _testprogs('bytecompile', '', @ARGV);
186 }
187 elsif ($::with_utf16) {
188     for my $e (0, 1) {
189         for my $b (0, 1) {
190             print STDERR "# ENDIAN $e BOM $b\n";
191             my @UARGV;
192             for my $a (@ARGV) {
193                 my $u = $a . "." . ($e ? "l" : "b") . "e" . ($b ? "b" : "");
194                 my $f = $e ? "v" : "n";
195                 push @UARGV, $u;
196                 unlink($u);
197                 if (open(A, $a)) {
198                     if (open(U, ">$u")) {
199                         print U pack("$f", 0xFEFF) if $b;
200                         while (<A>) {
201                             print U pack("$f*", unpack("C*", $_));
202                         }
203                         close(U);
204                     }
205                     close(A);
206                 }
207             }
208             _testprogs('perl', '', @UARGV);
209             unlink(@UARGV);
210         }
211     }
212 }
213 else {
214     _testprogs('compile', '',   @ARGV) if -e "../testcompile";
215     _testprogs('perl',    '',   @ARGV);
216 }
217
218 sub _testprogs {
219     my ($type, $args, @tests) = @_;
220
221     print <<'EOT' if ($type eq 'compile');
222 ------------------------------------------------------------------------------
223 TESTING COMPILER
224 ------------------------------------------------------------------------------
225 EOT
226
227     print <<'EOT' if ($type eq 'deparse');
228 ------------------------------------------------------------------------------
229 TESTING DEPARSER
230 ------------------------------------------------------------------------------
231 EOT
232
233     print <<EOT if ($type eq 'bytecompile');
234 ------------------------------------------------------------------------------
235 TESTING BYTECODE COMPILER
236 ------------------------------------------------------------------------------
237 EOT
238
239     $ENV{PERLCC_TIMEOUT} = 120
240           if ($type eq 'compile' && !$ENV{PERLCC_TIMEOUT});
241
242     $::bad_files = 0;
243
244     foreach my $t (@tests) {
245       unless (exists $::path_to_name{$t}) {
246         my $tname = File::Spec->catfile('t',$t);
247         $tname = VMS::Filespec::unixify($tname) if $^O eq 'VMS';
248         $::path_to_name{$t} = $tname;
249       }
250     }
251     my $maxlen = 0;
252     foreach (@::path_to_name{@tests}) {
253         s/\.\w+\z/./;
254         my $len = length ;
255         $maxlen = $len if $len > $maxlen;
256     }
257     # + 3 : we want three dots between the test name and the "ok"
258     my $dotdotdot = $maxlen + 3 ;
259     my $valgrind = 0;
260     my $valgrind_log = 'current.valgrind';
261     my $total_files = @tests;
262     my $good_files = 0;
263     my $tested_files  = 0;
264     my $totmax = 0;
265     my %failed_tests;
266
267     while (my $test = shift @tests) {
268         my $test_start_time = $show_elapsed_time ? Time::HiRes::time() : 0;
269
270         if ( $::infinite{$test} && $type eq 'compile' ) {
271             print STDERR "$test creates infinite loop! Skipping.\n";
272             next;
273         }
274         if ($test =~ /^$/) {
275             next;
276         }
277         if ($type eq 'deparse') {
278             if ($test eq "comp/redef.t") {
279                 # Redefinition happens at compile time
280                 next;
281             }
282             elsif ($test =~ m{lib/Switch/t/}) {
283                 # B::Deparse doesn't support source filtering
284                 next;
285             }
286         }
287         my $te = $::path_to_name{$test} . '.'
288                     x ($dotdotdot - length($::path_to_name{$test}));
289
290         if ($^O ne 'VMS') {  # defer printing on VMS due to piping bug
291             print $te;
292             $te = '';
293         }
294
295         # XXX DAPM %OVER not defined anywhere
296         # $test = $OVER{$test} if exists $OVER{$test};
297
298         open(SCRIPT,"<",$test) or die "Can't run $test.\n";
299         $_ = <SCRIPT>;
300         close(SCRIPT) unless ($type eq 'deparse');
301         if ($::with_utf16) {
302             $_ =~ tr/\0//d;
303         }
304         my $switch;
305         if (/#!.*\bperl.*\s-\w*([tT])/) {
306             $switch = qq{"-$1"};
307         }
308         else {
309             if ($::taintwarn) {
310                 # not all tests are expected to pass with this option
311                 $switch = '"-t"';
312             }
313             else {
314                 $switch = '';
315             }
316         }
317
318         my $test_executable; # for 'compile' tests
319         my $file_opts = "";
320         if ($type eq 'deparse') {
321             # Look for #line directives which change the filename
322             while (<SCRIPT>) {
323                 $file_opts .= ",-f$3$4"
324                         if /^#\s*line\s+(\d+)\s+((\w+)|"([^"]+)")/;
325             }
326             close(SCRIPT);
327         }
328
329         my $utf8 = $::with_utf8 ? '-I../lib -Mutf8' : '';
330         my $testswitch = '-I. -MTestInit'; # -T will strict . from @INC
331         if ($type eq 'deparse') {
332             my $deparse_cmd =
333                 "./perl $testswitch $switch -I../lib -MO=-qq,Deparse,-sv1.,".
334                 "-l$::deparse_opts$file_opts ".
335                 "$test > $test.dp ".
336                 "&& ./perl $testswitch $switch -I../lib $test.dp |";
337             open(RESULTS, $deparse_cmd)
338                 or print "can't deparse '$deparse_cmd': $!.\n";
339         }
340         elsif ($type eq 'bytecompile') {
341             my ($pwd, $null);
342             if( $^O eq 'MSWin32') {
343                 $pwd = `cd`;
344                 $null = 'nul';
345             } else {
346                 $pwd = `pwd`;
347                 $null = '/dev/null';
348             }
349             chomp $pwd;
350             my $perl = $ENV{PERL} || "$pwd/perl";
351             my $bswitch = "-MO=Bytecode,-H,-TI,-s$pwd/$test,";
352             $bswitch .= "-TF$test.plc,"
353                 if $test =~ m(chdir|pod/|CGI/t/carp|lib/DB);
354             $bswitch .= "-k,"
355                 if $test =~ m(deparse|terse|ext/Storable/t/code);
356             $bswitch .= "-b,"
357                 if $test =~ m(op/getpid);
358             my $bytecompile_cmd =
359                 "$perl $testswitch $switch -I../lib $bswitch".
360                 "-o$test.plc $test 2>$null &&".
361                 "$perl $testswitch $switch -I../lib $utf8 $test.plc |";
362             open(RESULTS,$bytecompile_cmd)
363                 or print "can't byte-compile '$bytecompile_cmd': $!.\n";
364         }
365         elsif ($type eq 'perl') {
366             my $perl = $ENV{PERL} || './perl';
367             my $redir = $^O eq 'VMS' ? '2>&1' : '';
368             if ($ENV{PERL_VALGRIND}) {
369                 my $valgrind = $ENV{VALGRIND} // 'valgrind';
370                 $perl = "$valgrind --suppressions=perl.supp --leak-check=yes "
371                                 . "--leak-resolution=high --show-reachable=yes "
372                                 . "--num-callers=50 --log-fd=3 $perl";
373                 $redir = "3>$valgrind_log";
374             }
375             my $run = "$perl" . _quote_args("$testswitch $switch $utf8")
376                               . " $test $redir|";
377             open(RESULTS,$run) or print "can't run '$run': $!.\n";
378         }
379         else {
380             my $compile_cmd;
381             my $pl2c = "$testswitch -I../lib ../utils/perlcc --testsuite " .
382               # -O9 for good measure, -fcog is broken ATM
383                        "$switch -Wb=-O9,-fno-cog -L .. " .
384                        "-I \".. ../lib/CORE\" $args $utf8 $test -o ";
385
386             if( $^O eq 'MSWin32' ) {
387                 $test_executable = "$test.exe";
388                 # hopefully unused name...
389                 open HACK, "> xweghyz.pl";
390                 print HACK <<EOT;
391 #!./perl
392
393 open HACK, '.\\perl $pl2c $test_executable |';
394 # cl.exe prints the name of the .c file on stdout (\%^\$^#)
395 while(<HACK>) {m/^\\w+\\.[cC]\$/ && next;print}
396 open HACK, '$test_executable |';
397 while(<HACK>) {print}
398 EOT
399                 close HACK;
400                 $compile_cmd = 'xweghyz.pl |';
401             }
402             else {
403                 $test_executable = "$test.plc";
404                 $compile_cmd
405                     = "./perl $pl2c $test_executable && $test_executable |";
406             }
407             unlink $test_executable if -f $test_executable;
408             open(RESULTS, $compile_cmd)
409                 or print "can't compile '$compile_cmd': $!.\n";
410         }
411         # Our environment may force us to use UTF-8, but we can't be sure that
412         # anything we're reading from will be generating (well formed) UTF-8
413         # This may not be the best way - possibly we should unset ${^OPEN} up
414         # top?
415         binmode RESULTS;
416
417         my $failure;
418         my $next = 0;
419         my $seen_leader = 0;
420         my $seen_ok = 0;
421         my $trailing_leader = 0;
422         my $max;
423         my %todo;
424         while (<RESULTS>) {
425             next if /^\s*$/; # skip blank lines
426             if ($::verbose) {
427                 print $_;
428             }
429             unless (/^\#/) {
430                 if ($trailing_leader) {
431                     # shouldn't be anything following a postfix 1..n
432                     $failure = 'FAILED--extra output after trailing 1..n';
433                     last;
434                 }
435                 if (/^1\.\.([0-9]+)( todo ([\d ]+))?/) {
436                     if ($seen_leader) {
437                         $failure = 'FAILED--seen duplicate leader';
438                         last;
439                     }
440                     $max = $1;
441                     %todo = map { $_ => 1 } split / /, $3 if $3;
442                     $totmax += $max;
443                     $tested_files++;
444                     if ($seen_ok) {
445                         # 1..n appears at end of file
446                         $trailing_leader = 1;
447                         if ($next != $max) {
448                             $failure = "FAILED--expected $max tests, saw $next";
449                             last;
450                         }
451                     }
452                     else {
453                         $next = 0;
454                     }
455                     $seen_leader = 1;
456                 }
457                 else {
458                     if (/^(not )?ok(?: (\d+))?[^\#]*(\s*\#.*)?/) {
459                         unless ($seen_leader) {
460                             unless ($seen_ok) {
461                                 $next = 0;
462                             }
463                         }
464                         $seen_ok = 1;
465                         $next++;
466                         my($not, $num, $extra, $istodo) = ($1, $2, $3, 0);
467                         $num = $next unless $num;
468
469                         if ($num == $next) {
470
471                             # SKIP is essentially the same as TODO for t/TEST
472                             # this still conforms to TAP:
473                             # http://search.cpan.org/dist/Test-Harness/lib/Test/Harness/TAP.pod
474                             $extra and $istodo = $extra =~ /#\s*(?:TODO|SKIP)\b/;
475                             $istodo = 1 if $todo{$num};
476
477                             if( $not && !$istodo ) {
478                                 $failure = "FAILED at test $num";
479                                 last;
480                             }
481                         }
482                         else {
483                             $failure ="FAILED--expected test $next, saw test $num";
484                             last;
485                         }
486                     }
487                     elsif (/^Bail out!\s*(.*)/i) { # magic words
488                         die "FAILED--Further testing stopped" . ($1 ? ": $1\n" : ".\n");
489                     }
490                     else {
491                         # module tests are allowed extra output,
492                         # because Test::Harness allows it
493                         next if $test =~ /^\W*(ext|lib)\b/;
494                         $failure = "FAILED--unexpected output at test $next";
495                         last;
496                     }
497                 }
498             }
499         }
500         close RESULTS;
501
502         if (not defined $failure) {
503             $failure = 'FAILED--no leader found' unless $seen_leader;
504         }
505
506         if ($ENV{PERL_VALGRIND}) {
507             my @valgrind;
508             if (-e $valgrind_log) {
509                 if (open(V, $valgrind_log)) {
510                     @valgrind = <V>;
511                     close V;
512                 } else {
513                     warn "$0: Failed to open '$valgrind_log': $!\n";
514                 }
515             }
516             if (@valgrind) {
517                 my $leaks = 0;
518                 my $errors = 0;
519                 for my $i (0..$#valgrind) {
520                     local $_ = $valgrind[$i];
521                     if (/^==\d+== ERROR SUMMARY: (\d+) errors? /) {
522                         $errors += $1;   # there may be multiple error summaries
523                     } elsif (/^==\d+== LEAK SUMMARY:/) {
524                         for my $off (1 .. 4) {
525                             if ($valgrind[$i+$off] =~
526                                 /(?:lost|reachable):\s+\d+ bytes in (\d+) blocks/) {
527                                 $leaks += $1;
528                             }
529                         }
530                     }
531                 }
532                 if ($errors or $leaks) {
533                     if (rename $valgrind_log, "$test.valgrind") {
534                         $valgrind++;
535                     } else {
536                         warn "$0: Failed to create '$test.valgrind': $!\n";
537                     }
538                 }
539             } else {
540                 warn "No valgrind output?\n";
541             }
542             if (-e $valgrind_log) {
543                 unlink $valgrind_log
544                     or warn "$0: Failed to unlink '$valgrind_log': $!\n";
545             }
546         }
547         if ($type eq 'deparse') {
548             unlink "./$test.dp";
549         }
550         if ($ENV{PERL_3LOG}) {
551             my $tpp = $test;
552             $tpp =~ s:^\.\./::;
553             $tpp =~ s:/:_:g;
554             $tpp =~ s:\.t$:.3log:;
555             rename("perl.3log", $tpp) ||
556                 die "rename: perl3.log to $tpp: $!\n";
557         }
558         # test if the compiler compiled something
559         if( $type eq 'compile' && !-e "$test_executable" ) {
560             $failure = "Test did not compile";
561         }
562         if (not defined $failure and $next != $max) {
563             $failure="FAILED--expected $max tests, saw $next";
564         }
565
566         if (defined $failure) {
567             print "${te}$failure\n";
568             $::bad_files++;
569             if ($test =~ /^base/) {
570                 die "Failed a basic test ($test) -- cannot continue.\n";
571             }
572             ++$failed_tests{$test};
573         }
574         else {
575             if ($max) {
576                 my $elapsed;
577                 if ( $show_elapsed_time ) {
578                     $elapsed = sprintf( " %8.0f ms", (Time::HiRes::time() - $test_start_time) * 1000 );
579                 }
580                 else {
581                     $elapsed = "";
582                 }
583                 print "${te}ok$elapsed\n";
584                 $good_files++;
585             }
586             else {
587                 print "${te}skipping test on this platform\n";
588                 $tested_files -= 1;
589             }
590         }
591     } # while tests
592
593     if ($::bad_files == 0) {
594         if ($good_files) {
595             print "All tests successful.\n";
596             # XXX add mention of 'perlbug -ok' ?
597         }
598         else {
599             die "FAILED--no tests were run for some reason.\n";
600         }
601     }
602     else {
603         my $pct = $tested_files ? sprintf("%.2f", ($tested_files - $::bad_files) / $tested_files * 100) : "0.00";
604         my $s = $::bad_files == 1 ? "" : "s";
605         warn "Failed $::bad_files test$s out of $tested_files, $pct% okay.\n";
606         for my $test ( sort keys %failed_tests ) {
607             print "\t$test\n";
608         }
609         warn <<'SHRDLU_1';
610 ### Since not all tests were successful, you may want to run some of
611 ### them individually and examine any diagnostic messages they produce.
612 ### See the INSTALL document's section on "make test".
613 SHRDLU_1
614         warn <<'SHRDLU_2' if $good_files / $total_files > 0.8;
615 ### You have a good chance to get more information by running
616 ###   ./perl harness
617 ### in the 't' directory since most (>=80%) of the tests succeeded.
618 SHRDLU_2
619         if (eval {require Config; import Config; 1}) {
620             if ($::Config{usedl} && (my $p = $::Config{ldlibpthname})) {
621                 warn <<SHRDLU_3;
622 ### You may have to set your dynamic library search path,
623 ### $p, to point to the build directory:
624 SHRDLU_3
625                 if (exists $ENV{$p} && $ENV{$p} ne '') {
626                     warn <<SHRDLU_4a;
627 ###   setenv $p `pwd`:\$$p; cd t; ./perl harness
628 ###   $p=`pwd`:\$$p; export $p; cd t; ./perl harness
629 ###   export $p=`pwd`:\$$p; cd t; ./perl harness
630 SHRDLU_4a
631                 } else {
632                     warn <<SHRDLU_4b;
633 ###   setenv $p `pwd`; cd t; ./perl harness
634 ###   $p=`pwd`; export $p; cd t; ./perl harness
635 ###   export $p=`pwd`; cd t; ./perl harness
636 SHRDLU_4b
637                 }
638                 warn <<SHRDLU_5;
639 ### for csh-style shells, like tcsh; or for traditional/modern
640 ### Bourne-style shells, like bash, ksh, and zsh, respectively.
641 SHRDLU_5
642             }
643         }
644     }
645     my ($user,$sys,$cuser,$csys) = times;
646     print sprintf("u=%.2f  s=%.2f  cu=%.2f  cs=%.2f  scripts=%d  tests=%d\n",
647         $user,$sys,$cuser,$csys,$tested_files,$totmax);
648     if ($ENV{PERL_VALGRIND}) {
649         my $s = $valgrind == 1 ? '' : 's';
650         print "$valgrind valgrind report$s created.\n", ;
651     }
652 }
653 exit ($::bad_files != 0);
654
655 # ex: set ts=8 sts=4 sw=4 noet: