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