This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
make /\p{isUserDefined}/ die on taint
[perl5.git] / t / test.pl
1 #
2 # t/test.pl - most of Test::More functionality without the fuss, plus
3 # has mappings native_to_latin1 and latin1_to_native so that fewer tests
4 # on non ASCII-ish platforms need to be skipped
5
6
7 # NOTE:
8 #
9 # Increment ($x++) has a certain amount of cleverness for things like
10 #
11 #   $x = 'zz';
12 #   $x++; # $x eq 'aaa';
13 #
14 # stands more chance of breaking than just a simple
15 #
16 #   $x = $x + 1
17 #
18 # In this file, we use the latter "Baby Perl" approach, and increment
19 # will be worked over by t/op/inc.t
20
21 $Level = 1;
22 my $test = 1;
23 my $planned;
24 my $noplan;
25 my $Perl;       # Safer version of $^X set by which_perl()
26
27 $TODO = 0;
28 $NO_ENDING = 0;
29 $Tests_Are_Passing = 1;
30
31 # Use this instead of print to avoid interference while testing globals.
32 sub _print {
33     local($\, $", $,) = (undef, ' ', '');
34     print STDOUT @_;
35 }
36
37 sub _print_stderr {
38     local($\, $", $,) = (undef, ' ', '');
39     print STDERR @_;
40 }
41
42 sub plan {
43     my $n;
44     if (@_ == 1) {
45         $n = shift;
46         if ($n eq 'no_plan') {
47           undef $n;
48           $noplan = 1;
49         }
50     } else {
51         my %plan = @_;
52         $n = $plan{tests};
53     }
54     _print "1..$n\n" unless $noplan;
55     $planned = $n;
56 }
57
58
59 # Set the plan at the end.  See Test::More::done_testing.
60 sub done_testing {
61     my $n = $test - 1;
62     $n = shift if @_;
63
64     _print "1..$n\n";
65     $planned = $n;
66 }
67
68
69 END {
70     my $ran = $test - 1;
71     if (!$NO_ENDING) {
72         if (defined $planned && $planned != $ran) {
73             _print_stderr
74                 "# Looks like you planned $planned tests but ran $ran.\n";
75         } elsif ($noplan) {
76             _print "1..$ran\n";
77         }
78     }
79 }
80
81 sub _diag {
82     return unless @_;
83     my @mess = _comment(@_);
84     $TODO ? _print(@mess) : _print_stderr(@mess);
85 }
86
87 # Use this instead of "print STDERR" when outputting failure diagnostic
88 # messages
89 sub diag {
90     _diag(@_);
91 }
92
93 # Use this instead of "print" when outputting informational messages
94 sub note {
95     return unless @_;
96     _print( _comment(@_) );
97 }
98
99 sub _comment {
100     return map { /^#/ ? "$_\n" : "# $_\n" }
101            map { split /\n/ } @_;
102 }
103
104 sub skip_all {
105     if (@_) {
106         _print "1..0 # Skip @_\n";
107     } else {
108         _print "1..0\n";
109     }
110     exit(0);
111 }
112
113 sub skip_all_if_miniperl {
114     skip_all(@_) if !defined &DynaLoader::boot_DynaLoader;
115 }
116
117 sub _ok {
118     my ($pass, $where, $name, @mess) = @_;
119     # Do not try to microoptimize by factoring out the "not ".
120     # VMS will avenge.
121     my $out;
122     if ($name) {
123         # escape out '#' or it will interfere with '# skip' and such
124         $name =~ s/#/\\#/g;
125         $out = $pass ? "ok $test - $name" : "not ok $test - $name";
126     } else {
127         $out = $pass ? "ok $test" : "not ok $test";
128     }
129
130     if ($TODO) {
131         $out = $out . " # TODO $TODO";
132     } else {
133         $Tests_Are_Passing = 0 unless $pass;
134     }
135
136     _print "$out\n";
137
138     if ($pass) {
139         note @mess; # Ensure that the message is properly escaped.
140     }
141     else {
142         _diag "# Failed $where\n";
143         _diag @mess;
144     }
145
146     $test = $test + 1; # don't use ++
147
148     return $pass;
149 }
150
151 sub _where {
152     my @caller = caller($Level);
153     return "at $caller[1] line $caller[2]";
154 }
155
156 # DON'T use this for matches. Use like() instead.
157 sub ok ($@) {
158     my ($pass, $name, @mess) = @_;
159     _ok($pass, _where(), $name, @mess);
160 }
161
162 sub _q {
163     my $x = shift;
164     return 'undef' unless defined $x;
165     my $q = $x;
166     $q =~ s/\\/\\\\/g;
167     $q =~ s/'/\\'/g;
168     return "'$q'";
169 }
170
171 sub _qq {
172     my $x = shift;
173     return defined $x ? '"' . display ($x) . '"' : 'undef';
174 };
175
176 # keys are the codes \n etc map to, values are 2 char strings such as \n
177 my %backslash_escape;
178 foreach my $x (split //, 'nrtfa\\\'"') {
179     $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
180 }
181 # A way to display scalars containing control characters and Unicode.
182 # Trying to avoid setting $_, or relying on local $_ to work.
183 sub display {
184     my @result;
185     foreach my $x (@_) {
186         if (defined $x and not ref $x) {
187             my $y = '';
188             foreach my $c (unpack("U*", $x)) {
189                 if ($c > 255) {
190                     $y = $y . sprintf "\\x{%x}", $c;
191                 } elsif ($backslash_escape{$c}) {
192                     $y = $y . $backslash_escape{$c};
193                 } else {
194                     my $z = chr $c; # Maybe we can get away with a literal...
195                     if ($z =~ /[[:^print:]]/) {
196
197                         # Use octal for characters traditionally expressed as
198                         # such: the low controls
199                         if ($c <= 037) {
200                             $z = sprintf "\\%03o", $c;
201                         } else {
202                             $z = sprintf "\\x{%x}", $c;
203                         }
204                     }
205                     $y = $y . $z;
206                 }
207             }
208             $x = $y;
209         }
210         return $x unless wantarray;
211         push @result, $x;
212     }
213     return @result;
214 }
215
216 sub is ($$@) {
217     my ($got, $expected, $name, @mess) = @_;
218
219     my $pass;
220     if( !defined $got || !defined $expected ) {
221         # undef only matches undef
222         $pass = !defined $got && !defined $expected;
223     }
224     else {
225         $pass = $got eq $expected;
226     }
227
228     unless ($pass) {
229         unshift(@mess, "#      got "._qq($got)."\n",
230                        "# expected "._qq($expected)."\n");
231     }
232     _ok($pass, _where(), $name, @mess);
233 }
234
235 sub isnt ($$@) {
236     my ($got, $isnt, $name, @mess) = @_;
237
238     my $pass;
239     if( !defined $got || !defined $isnt ) {
240         # undef only matches undef
241         $pass = defined $got || defined $isnt;
242     }
243     else {
244         $pass = $got ne $isnt;
245     }
246
247     unless( $pass ) {
248         unshift(@mess, "# it should not be "._qq($got)."\n",
249                        "# but it is.\n");
250     }
251     _ok($pass, _where(), $name, @mess);
252 }
253
254 sub cmp_ok ($$$@) {
255     my($got, $type, $expected, $name, @mess) = @_;
256
257     my $pass;
258     {
259         local $^W = 0;
260         local($@,$!);   # don't interfere with $@
261                         # eval() sometimes resets $!
262         $pass = eval "\$got $type \$expected";
263     }
264     unless ($pass) {
265         # It seems Irix long doubles can have 2147483648 and 2147483648
266         # that stringify to the same thing but are actually numerically
267         # different. Display the numbers if $type isn't a string operator,
268         # and the numbers are stringwise the same.
269         # (all string operators have alphabetic names, so tr/a-z// is true)
270         # This will also show numbers for some unneeded cases, but will
271         # definitely be helpful for things such as == and <= that fail
272         if ($got eq $expected and $type !~ tr/a-z//) {
273             unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
274         }
275         unshift(@mess, "#      got "._qq($got)."\n",
276                        "# expected $type "._qq($expected)."\n");
277     }
278     _ok($pass, _where(), $name, @mess);
279 }
280
281 # Check that $got is within $range of $expected
282 # if $range is 0, then check it's exact
283 # else if $expected is 0, then $range is an absolute value
284 # otherwise $range is a fractional error.
285 # Here $range must be numeric, >= 0
286 # Non numeric ranges might be a useful future extension. (eg %)
287 sub within ($$$@) {
288     my ($got, $expected, $range, $name, @mess) = @_;
289     my $pass;
290     if (!defined $got or !defined $expected or !defined $range) {
291         # This is a fail, but doesn't need extra diagnostics
292     } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
293         # This is a fail
294         unshift @mess, "# got, expected and range must be numeric\n";
295     } elsif ($range < 0) {
296         # This is also a fail
297         unshift @mess, "# range must not be negative\n";
298     } elsif ($range == 0) {
299         # Within 0 is ==
300         $pass = $got == $expected;
301     } elsif ($expected == 0) {
302         # If expected is 0, treat range as absolute
303         $pass = ($got <= $range) && ($got >= - $range);
304     } else {
305         my $diff = $got - $expected;
306         $pass = abs ($diff / $expected) < $range;
307     }
308     unless ($pass) {
309         if ($got eq $expected) {
310             unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
311         }
312         unshift@mess, "#      got "._qq($got)."\n",
313                       "# expected "._qq($expected)." (within "._qq($range).")\n";
314     }
315     _ok($pass, _where(), $name, @mess);
316 }
317
318 # Note: this isn't quite as fancy as Test::More::like().
319
320 sub like   ($$@) { like_yn (0,@_) }; # 0 for -
321 sub unlike ($$@) { like_yn (1,@_) }; # 1 for un-
322
323 sub like_yn ($$$@) {
324     my ($flip, $got, $expected, $name, @mess) = @_;
325     my $pass;
326     $pass = $got =~ /$expected/ if !$flip;
327     $pass = $got !~ /$expected/ if $flip;
328     unless ($pass) {
329         unshift(@mess, "#      got '$got'\n",
330                 $flip
331                 ? "# expected !~ /$expected/\n" : "# expected /$expected/\n");
332     }
333     local $Level = $Level + 1;
334     _ok($pass, _where(), $name, @mess);
335 }
336
337 sub pass {
338     _ok(1, '', @_);
339 }
340
341 sub fail {
342     _ok(0, _where(), @_);
343 }
344
345 sub curr_test {
346     $test = shift if @_;
347     return $test;
348 }
349
350 sub next_test {
351   my $retval = $test;
352   $test = $test + 1; # don't use ++
353   $retval;
354 }
355
356 # Note: can't pass multipart messages since we try to
357 # be compatible with Test::More::skip().
358 sub skip {
359     my $why = shift;
360     my $n    = @_ ? shift : 1;
361     for (1..$n) {
362         _print "ok $test # skip $why\n";
363         $test = $test + 1;
364     }
365     local $^W = 0;
366     last SKIP;
367 }
368
369 sub skip_if_miniperl {
370     skip(@_) if !defined &DynaLoader::boot_DynaLoader;
371 }
372
373 sub todo_skip {
374     my $why = shift;
375     my $n   = @_ ? shift : 1;
376
377     for (1..$n) {
378         _print "not ok $test # TODO & SKIP $why\n";
379         $test = $test + 1;
380     }
381     local $^W = 0;
382     last TODO;
383 }
384
385 sub eq_array {
386     my ($ra, $rb) = @_;
387     return 0 unless $#$ra == $#$rb;
388     for my $i (0..$#$ra) {
389         next     if !defined $ra->[$i] && !defined $rb->[$i];
390         return 0 if !defined $ra->[$i];
391         return 0 if !defined $rb->[$i];
392         return 0 unless $ra->[$i] eq $rb->[$i];
393     }
394     return 1;
395 }
396
397 sub eq_hash {
398   my ($orig, $suspect) = @_;
399   my $fail;
400   while (my ($key, $value) = each %$suspect) {
401     # Force a hash recompute if this perl's internals can cache the hash key.
402     $key = "" . $key;
403     if (exists $orig->{$key}) {
404       if ($orig->{$key} ne $value) {
405         _print "# key ", _qq($key), " was ", _qq($orig->{$key}),
406                      " now ", _qq($value), "\n";
407         $fail = 1;
408       }
409     } else {
410       _print "# key ", _qq($key), " is ", _qq($value),
411                    ", not in original.\n";
412       $fail = 1;
413     }
414   }
415   foreach (keys %$orig) {
416     # Force a hash recompute if this perl's internals can cache the hash key.
417     $_ = "" . $_;
418     next if (exists $suspect->{$_});
419     _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
420     $fail = 1;
421   }
422   !$fail;
423 }
424
425 # We only provide a subset of the Test::More functionality.
426 sub require_ok ($) {
427     my ($require) = @_;
428     if ($require =~ tr/[A-Za-z0-9:.]//c) {
429         fail("Invalid character in \"$require\", passed to require_ok");
430     } else {
431         eval <<REQUIRE_OK;
432 require $require;
433 REQUIRE_OK
434         is($@, '', _where(), "require $require");
435     }
436 }
437
438 sub use_ok ($) {
439     my ($use) = @_;
440     if ($use =~ tr/[A-Za-z0-9:.]//c) {
441         fail("Invalid character in \"$use\", passed to use");
442     } else {
443         eval <<USE_OK;
444 use $use;
445 USE_OK
446         is($@, '', _where(), "use $use");
447     }
448 }
449
450 # runperl - Runs a separate perl interpreter.
451 # Arguments :
452 #   switches => [ command-line switches ]
453 #   nolib    => 1 # don't use -I../lib (included by default)
454 #   non_portable => Don't warn if a one liner contains quotes
455 #   prog     => one-liner (avoid quotes)
456 #   progs    => [ multi-liner (avoid quotes) ]
457 #   progfile => perl script
458 #   stdin    => string to feed the stdin
459 #   stderr   => redirect stderr to stdout
460 #   args     => [ command-line arguments to the perl program ]
461 #   verbose  => print the command line
462
463 my $is_mswin    = $^O eq 'MSWin32';
464 my $is_netware  = $^O eq 'NetWare';
465 my $is_vms      = $^O eq 'VMS';
466 my $is_cygwin   = $^O eq 'cygwin';
467
468 sub _quote_args {
469     my ($runperl, $args) = @_;
470
471     foreach (@$args) {
472         # In VMS protect with doublequotes because otherwise
473         # DCL will lowercase -- unless already doublequoted.
474        $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
475        $runperl = $runperl . ' ' . $_;
476     }
477     return $runperl;
478 }
479
480 sub _create_runperl { # Create the string to qx in runperl().
481     my %args = @_;
482     my $runperl = which_perl();
483     if ($runperl =~ m/\s/) {
484         $runperl = qq{"$runperl"};
485     }
486     #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
487     if ($ENV{PERL_RUNPERL_DEBUG}) {
488         $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
489     }
490     unless ($args{nolib}) {
491         $runperl = $runperl . ' "-I../lib"'; # doublequotes because of VMS
492     }
493     if ($args{switches}) {
494         local $Level = 2;
495         die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
496             unless ref $args{switches} eq "ARRAY";
497         $runperl = _quote_args($runperl, $args{switches});
498     }
499     if (defined $args{prog}) {
500         die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
501             if defined $args{progs};
502         $args{progs} = [$args{prog}]
503     }
504     if (defined $args{progs}) {
505         die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
506             unless ref $args{progs} eq "ARRAY";
507         foreach my $prog (@{$args{progs}}) {
508             if ($prog =~ tr/'"// && !$args{non_portable}) {
509                 warn "quotes in prog >>$prog<< are not portable";
510             }
511             if ($is_mswin || $is_netware || $is_vms) {
512                 $runperl = $runperl . qq ( -e "$prog" );
513             }
514             else {
515                 $runperl = $runperl . qq ( -e '$prog' );
516             }
517         }
518     } elsif (defined $args{progfile}) {
519         $runperl = $runperl . qq( "$args{progfile}");
520     } else {
521         # You probably didn't want to be sucking in from the upstream stdin
522         die "test.pl:runperl(): none of prog, progs, progfile, args, "
523             . " switches or stdin specified"
524             unless defined $args{args} or defined $args{switches}
525                 or defined $args{stdin};
526     }
527     if (defined $args{stdin}) {
528         # so we don't try to put literal newlines and crs onto the
529         # command line.
530         $args{stdin} =~ s/\n/\\n/g;
531         $args{stdin} =~ s/\r/\\r/g;
532
533         if ($is_mswin || $is_netware || $is_vms) {
534             $runperl = qq{$Perl -e "print qq(} .
535                 $args{stdin} . q{)" | } . $runperl;
536         }
537         else {
538             $runperl = qq{$Perl -e 'print qq(} .
539                 $args{stdin} . q{)' | } . $runperl;
540         }
541     }
542     if (defined $args{args}) {
543         $runperl = _quote_args($runperl, $args{args});
544     }
545     $runperl = $runperl . ' 2>&1' if $args{stderr};
546     if ($args{verbose}) {
547         my $runperldisplay = $runperl;
548         $runperldisplay =~ s/\n/\n\#/g;
549         _print_stderr "# $runperldisplay\n";
550     }
551     return $runperl;
552 }
553
554 sub runperl {
555     die "test.pl:runperl() does not take a hashref"
556         if ref $_[0] and ref $_[0] eq 'HASH';
557     my $runperl = &_create_runperl;
558     my $result;
559
560     my $tainted = ${^TAINT};
561     my %args = @_;
562     exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1;
563
564     if ($tainted) {
565         # We will assume that if you're running under -T, you really mean to
566         # run a fresh perl, so we'll brute force launder everything for you
567         my $sep;
568
569         if (! eval 'require Config; 1') {
570             warn "test.pl had problems loading Config: $@";
571             $sep = ':';
572         } else {
573             $sep = $Config::Config{path_sep};
574         }
575
576         my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV);
577         local @ENV{@keys} = ();
578         # Untaint, plus take out . and empty string:
579         local $ENV{'DCL$PATH'} = $1 if $is_vms && exists($ENV{'DCL$PATH'}) && ($ENV{'DCL$PATH'} =~ /(.*)/s);
580         $ENV{PATH} =~ /(.*)/s;
581         local $ENV{PATH} =
582             join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and
583                 ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) }
584                     split quotemeta ($sep), $1;
585         if ($is_cygwin) {   # Must have /bin under Cygwin
586             if (length $ENV{PATH}) {
587                 $ENV{PATH} = $ENV{PATH} . $sep;
588             }
589             $ENV{PATH} = $ENV{PATH} . '/bin';
590         }
591         $runperl =~ /(.*)/s;
592         $runperl = $1;
593
594         $result = `$runperl`;
595     } else {
596         $result = `$runperl`;
597     }
598     $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
599     return $result;
600 }
601
602 # Nice alias
603 *run_perl = *run_perl = \&runperl; # shut up "used only once" warning
604
605 sub DIE {
606     _print_stderr "# @_\n";
607     exit 1;
608 }
609
610 # A somewhat safer version of the sometimes wrong $^X.
611 sub which_perl {
612     unless (defined $Perl) {
613         $Perl = $^X;
614
615         # VMS should have 'perl' aliased properly
616         return $Perl if $is_vms;
617
618         my $exe;
619         if (! eval 'require Config; 1') {
620             warn "test.pl had problems loading Config: $@";
621             $exe = '';
622         } else {
623             $exe = $Config::Config{_exe};
624         }
625        $exe = '' unless defined $exe;
626
627         # This doesn't absolutize the path: beware of future chdirs().
628         # We could do File::Spec->abs2rel() but that does getcwd()s,
629         # which is a bit heavyweight to do here.
630
631         if ($Perl =~ /^perl\Q$exe\E$/i) {
632             my $perl = "perl$exe";
633             if (! eval 'require File::Spec; 1') {
634                 warn "test.pl had problems loading File::Spec: $@";
635                 $Perl = "./$perl";
636             } else {
637                 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
638             }
639         }
640
641         # Build up the name of the executable file from the name of
642         # the command.
643
644         if ($Perl !~ /\Q$exe\E$/i) {
645             $Perl = $Perl . $exe;
646         }
647
648         warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
649
650         # For subcommands to use.
651         $ENV{PERLEXE} = $Perl;
652     }
653     return $Perl;
654 }
655
656 sub unlink_all {
657     my $count = 0;
658     foreach my $file (@_) {
659         1 while unlink $file;
660         if( -f $file ){
661             _print_stderr "# Couldn't unlink '$file': $!\n";
662         }else{
663             ++$count;
664         }
665     }
666     $count;
667 }
668
669 my %tmpfiles;
670 END { unlink_all keys %tmpfiles }
671
672 # A regexp that matches the tempfile names
673 $::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?';
674
675 # Avoid ++, avoid ranges, avoid split //
676 my @letters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
677 sub tempfile {
678     my $count = 0;
679     do {
680         my $temp = $count;
681         my $try = "tmp$$";
682         do {
683             $try = $try . $letters[$temp % 26];
684             $temp = int ($temp / 26);
685         } while $temp;
686         # Need to note all the file names we allocated, as a second request may
687         # come before the first is created.
688         if (!-e $try && !$tmpfiles{$try}) {
689             # We have a winner
690             $tmpfiles{$try} = 1;
691             return $try;
692         }
693         $count = $count + 1;
694     } while $count < 26 * 26;
695     die "Can't find temporary file name starting 'tmp$$'";
696 }
697
698 # This is the temporary file for _fresh_perl
699 my $tmpfile = tempfile();
700
701 #
702 # _fresh_perl
703 #
704 # The $resolve must be a subref that tests the first argument
705 # for success, or returns the definition of success (e.g. the
706 # expected scalar) if given no arguments.
707 #
708
709 sub _fresh_perl {
710     my($prog, $resolve, $runperl_args, $name) = @_;
711
712     # Given the choice of the mis-parsable {}
713     # (we want an anon hash, but a borked lexer might think that it's a block)
714     # or relying on taking a reference to a lexical
715     # (\ might be mis-parsed, and the reference counting on the pad may go
716     #  awry)
717     # it feels like the least-worse thing is to assume that auto-vivification
718     # works. At least, this is only going to be a run-time failure, so won't
719     # affect tests using this file but not this function.
720     $runperl_args->{progfile} = $tmpfile;
721     $runperl_args->{stderr} = 1;
722
723     open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
724
725     # VMS adjustments
726     if( $is_vms ) {
727         $prog =~ s#/dev/null#NL:#;
728
729         # VMS file locking
730         $prog =~ s{if \(-e _ and -f _ and -r _\)}
731                   {if (-e _ and -f _)}
732     }
733
734     print TEST $prog;
735     close TEST or die "Cannot close $tmpfile: $!";
736
737     my $results = runperl(%$runperl_args);
738     my $status = $?;
739
740     # Clean up the results into something a bit more predictable.
741     $results  =~ s/\n+$//;
742     $results =~ s/at\s+$::tempfile_regexp\s+line/at - line/g;
743     $results =~ s/of\s+$::tempfile_regexp\s+aborted/of - aborted/g;
744
745     # bison says 'parse error' instead of 'syntax error',
746     # various yaccs may or may not capitalize 'syntax'.
747     $results =~ s/^(syntax|parse) error/syntax error/mig;
748
749     if ($is_vms) {
750         # some tests will trigger VMS messages that won't be expected
751         $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
752
753         # pipes double these sometimes
754         $results =~ s/\n\n/\n/g;
755     }
756
757     my $pass = $resolve->($results);
758     unless ($pass) {
759         _diag "# PROG: \n$prog\n";
760         _diag "# EXPECTED:\n", $resolve->(), "\n";
761         _diag "# GOT:\n$results\n";
762         _diag "# STATUS: $status\n";
763     }
764
765     # Use the first line of the program as a name if none was given
766     unless( $name ) {
767         ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
768         $name = $name . '...' if length $first_line > length $name;
769     }
770
771     _ok($pass, _where(), "fresh_perl - $name");
772 }
773
774 #
775 # fresh_perl_is
776 #
777 # Combination of run_perl() and is().
778 #
779
780 sub fresh_perl_is {
781     my($prog, $expected, $runperl_args, $name) = @_;
782
783     # _fresh_perl() is going to clip the trailing newlines off the result.
784     # This will make it so the test author doesn't have to know that.
785     $expected =~ s/\n+$//;
786
787     local $Level = 2;
788     _fresh_perl($prog,
789                 sub { @_ ? $_[0] eq $expected : $expected },
790                 $runperl_args, $name);
791 }
792
793 #
794 # fresh_perl_like
795 #
796 # Combination of run_perl() and like().
797 #
798
799 sub fresh_perl_like {
800     my($prog, $expected, $runperl_args, $name) = @_;
801     local $Level = 2;
802     _fresh_perl($prog,
803                 sub { @_ ? $_[0] =~ $expected : $expected },
804                 $runperl_args, $name);
805 }
806
807 sub can_ok ($@) {
808     my($proto, @methods) = @_;
809     my $class = ref $proto || $proto;
810
811     unless( @methods ) {
812         return _ok( 0, _where(), "$class->can(...)" );
813     }
814
815     my @nok = ();
816     foreach my $method (@methods) {
817         local($!, $@);  # don't interfere with caller's $@
818                         # eval sometimes resets $!
819         eval { $proto->can($method) } || push @nok, $method;
820     }
821
822     my $name;
823     $name = @methods == 1 ? "$class->can('$methods[0]')"
824                           : "$class->can(...)";
825
826     _ok( !@nok, _where(), $name );
827 }
828
829
830 # Call $class->new( @$args ); and run the result through isa_ok.
831 # See Test::More::new_ok
832 sub new_ok {
833     my($class, $args, $obj_name) = @_;
834     $args ||= [];
835     $object_name = "The object" unless defined $obj_name;
836
837     local $Level = $Level + 1;
838
839     my $obj;
840     my $ok = eval { $obj = $class->new(@$args); 1 };
841     my $error = $@;
842
843     if($ok) {
844         isa_ok($obj, $class, $object_name);
845     }
846     else {
847         ok( 0, "new() died" );
848         diag("Error was:  $@");
849     }
850
851     return $obj;
852
853 }
854
855
856 sub isa_ok ($$;$) {
857     my($object, $class, $obj_name) = @_;
858
859     my $diag;
860     $obj_name = 'The object' unless defined $obj_name;
861     my $name = "$obj_name isa $class";
862     if( !defined $object ) {
863         $diag = "$obj_name isn't defined";
864     }
865     elsif( !ref $object ) {
866         $diag = "$obj_name isn't a reference";
867     }
868     else {
869         # We can't use UNIVERSAL::isa because we want to honor isa() overrides
870         local($@, $!);  # eval sometimes resets $!
871         my $rslt = eval { $object->isa($class) };
872         if( $@ ) {
873             if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) {
874                 if( !UNIVERSAL::isa($object, $class) ) {
875                     my $ref = ref $object;
876                     $diag = "$obj_name isn't a '$class' it's a '$ref'";
877                 }
878             } else {
879                 die <<WHOA;
880 WHOA! I tried to call ->isa on your object and got some weird error.
881 This should never happen.  Please contact the author immediately.
882 Here's the error.
883 $@
884 WHOA
885             }
886         }
887         elsif( !$rslt ) {
888             my $ref = ref $object;
889             $diag = "$obj_name isn't a '$class' it's a '$ref'";
890         }
891     }
892
893     _ok( !$diag, _where(), $name );
894 }
895
896 # Set a watchdog to timeout the entire test file
897 # NOTE:  If the test file uses 'threads', then call the watchdog() function
898 #        _AFTER_ the 'threads' module is loaded.
899 sub watchdog ($;$)
900 {
901     my $timeout = shift;
902     my $method  = shift || "";
903     my $timeout_msg = 'Test process timed out - terminating';
904
905     # Valgrind slows perl way down so give it more time before dying.
906     $timeout *= 10 if $ENV{PERL_VALGRIND};
907
908     my $pid_to_kill = $$;   # PID for this process
909
910     if ($method eq "alarm") {
911         goto WATCHDOG_VIA_ALARM;
912     }
913
914     # shut up use only once warning
915     my $threads_on = $threads::threads && $threads::threads;
916
917     # Don't use a watchdog process if 'threads' is loaded -
918     #   use a watchdog thread instead
919     if (!$threads_on) {
920
921         # On Windows and VMS, try launching a watchdog process
922         #   using system(1, ...) (see perlport.pod)
923         if ($is_mswin || $is_vms) {
924             # On Windows, try to get the 'real' PID
925             if ($is_mswin) {
926                 eval { require Win32; };
927                 if (defined(&Win32::GetCurrentProcessId)) {
928                     $pid_to_kill = Win32::GetCurrentProcessId();
929                 }
930             }
931
932             # If we still have a fake PID, we can't use this method at all
933             return if ($pid_to_kill <= 0);
934
935             # Launch watchdog process
936             my $watchdog;
937             eval {
938                 local $SIG{'__WARN__'} = sub {
939                     _diag("Watchdog warning: $_[0]");
940                 };
941                 my $sig = $is_vms ? 'TERM' : 'KILL';
942                 my $cmd = _create_runperl( prog =>  "sleep($timeout);" .
943                                                     "warn qq/# $timeout_msg" . '\n/;' .
944                                                     "kill($sig, $pid_to_kill);");
945                 $watchdog = system(1, $cmd);
946             };
947             if ($@ || ($watchdog <= 0)) {
948                 _diag('Failed to start watchdog');
949                 _diag($@) if $@;
950                 undef($watchdog);
951                 return;
952             }
953
954             # Add END block to parent to terminate and
955             #   clean up watchdog process
956             eval "END { local \$! = 0; local \$? = 0;
957                         wait() if kill('KILL', $watchdog); };";
958             return;
959         }
960
961         # Try using fork() to generate a watchdog process
962         my $watchdog;
963         eval { $watchdog = fork() };
964         if (defined($watchdog)) {
965             if ($watchdog) {   # Parent process
966                 # Add END block to parent to terminate and
967                 #   clean up watchdog process
968                 eval "END { local \$! = 0; local \$? = 0;
969                             wait() if kill('KILL', $watchdog); };";
970                 return;
971             }
972
973             ### Watchdog process code
974
975             # Load POSIX if available
976             eval { require POSIX; };
977
978             # Execute the timeout
979             sleep($timeout - 2) if ($timeout > 2);   # Workaround for perlbug #49073
980             sleep(2);
981
982             # Kill test process if still running
983             if (kill(0, $pid_to_kill)) {
984                 _diag($timeout_msg);
985                 kill('KILL', $pid_to_kill);
986             }
987
988             # Don't execute END block (added at beginning of this file)
989             $NO_ENDING = 1;
990
991             # Terminate ourself (i.e., the watchdog)
992             POSIX::_exit(1) if (defined(&POSIX::_exit));
993             exit(1);
994         }
995
996         # fork() failed - fall through and try using a thread
997     }
998
999     # Use a watchdog thread because either 'threads' is loaded,
1000     #   or fork() failed
1001     if (eval 'require threads; 1') {
1002         'threads'->create(sub {
1003                 # Load POSIX if available
1004                 eval { require POSIX; };
1005
1006                 # Execute the timeout
1007                 my $time_left = $timeout;
1008                 do {
1009                     $time_left = $time_left - sleep($time_left);
1010                 } while ($time_left > 0);
1011
1012                 # Kill the parent (and ourself)
1013                 select(STDERR); $| = 1;
1014                 _diag($timeout_msg);
1015                 POSIX::_exit(1) if (defined(&POSIX::_exit));
1016                 my $sig = $is_vms ? 'TERM' : 'KILL';
1017                 kill($sig, $pid_to_kill);
1018             })->detach();
1019         return;
1020     }
1021
1022     # If everything above fails, then just use an alarm timeout
1023 WATCHDOG_VIA_ALARM:
1024     if (eval { alarm($timeout); 1; }) {
1025         # Load POSIX if available
1026         eval { require POSIX; };
1027
1028         # Alarm handler will do the actual 'killing'
1029         $SIG{'ALRM'} = sub {
1030             select(STDERR); $| = 1;
1031             _diag($timeout_msg);
1032             POSIX::_exit(1) if (defined(&POSIX::_exit));
1033             my $sig = $is_vms ? 'TERM' : 'KILL';
1034             kill($sig, $pid_to_kill);
1035         };
1036     }
1037 }
1038
1039 my $cp_0037 =   # EBCDIC code page 0037
1040     '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F' .
1041     '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
1042     '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
1043     '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
1044     '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
1045     '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBA\xE0\xBB\xB0\x6D' .
1046     '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1047     '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
1048     '\x20\x21\x22\x23\x24\x15\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1049     '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
1050     '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBD\xB4\x9A\x8A\x5F\xCA\xAF\xBC' .
1051     '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1052     '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1053     '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xAD\xAE\x59' .
1054     '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1055     '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
1056
1057 my $cp_1047 =   # EBCDIC code page 1047
1058     '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
1059     '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
1060     '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
1061     '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
1062     '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
1063     '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D' .
1064     '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1065     '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
1066     '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1067     '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
1068     '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBB\xB4\x9A\x8A\xB0\xCA\xAF\xBC' .
1069     '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1070     '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1071     '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xBA\xAE\x59' .
1072     '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1073     '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
1074
1075 my $cp_bc = # EBCDIC code page POSiX-BC
1076     '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
1077     '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
1078     '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
1079     '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
1080     '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
1081     '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBB\xBC\xBD\x6A\x6D' .
1082     '\x4A\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1083     '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xFB\x4F\xFD\xFF\x07' .
1084     '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1085     '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\x5F' .
1086     '\x41\xAA\xB0\xB1\x9F\xB2\xD0\xB5\x79\xB4\x9A\x8A\xBA\xCA\xAF\xA1' .
1087     '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1088     '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1089     '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xE0\xFE\xDD\xFC\xAD\xAE\x59' .
1090     '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1091     '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xC0\xDE\xDB\xDC\x8D\x8E\xDF';
1092
1093 my $straight =  # Avoid ranges
1094     '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F' .
1095     '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F' .
1096     '\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F' .
1097     '\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F' .
1098     '\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' .
1099     '\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F' .
1100     '\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F' .
1101     '\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F' .
1102     '\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F' .
1103     '\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F' .
1104     '\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF' .
1105     '\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF' .
1106     '\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF' .
1107     '\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF' .
1108     '\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF' .
1109     '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF';
1110
1111 # The following 2 functions allow tests to work on both EBCDIC and
1112 # ASCII-ish platforms.  They convert string scalars between the native
1113 # character set and the set of 256 characters which is usually called
1114 # Latin1.
1115 #
1116 # These routines don't work on UTF-EBCDIC and UTF-8.
1117
1118 sub native_to_latin1($) {
1119     my $string = shift;
1120
1121     return $string if ord('^') == 94;   # ASCII, Latin1
1122     my $cp;
1123     if (ord('^') == 95) {    # EBCDIC 1047
1124         $cp = \$cp_1047;
1125     }
1126     elsif (ord('^') == 106) {   # EBCDIC POSIX-BC
1127         $cp = \$cp_bc;
1128     }
1129     elsif (ord('^') == 176)  {   # EBCDIC 037 */
1130         $cp = \$cp_0037;
1131     }
1132     else {
1133         die "Unknown native character set";
1134     }
1135
1136     eval '$string =~ tr/' . $$cp . '/' . $straight . '/';
1137     return $string;
1138 }
1139
1140 sub latin1_to_native($) {
1141     my $string = shift;
1142
1143     return $string if ord('^') == 94;   # ASCII, Latin1
1144     my $cp;
1145     if (ord('^') == 95) {    # EBCDIC 1047
1146         $cp = \$cp_1047;
1147     }
1148     elsif (ord('^') == 106) {   # EBCDIC POSIX-BC
1149         $cp = \$cp_bc;
1150     }
1151     elsif (ord('^') == 176)  {   # EBCDIC 037 */
1152         $cp = \$cp_0037;
1153     }
1154     else {
1155         die "Unknown native character set";
1156     }
1157
1158     eval '$string =~ tr/' . $straight . '/' . $$cp . '/';
1159     return $string;
1160 }
1161
1162 sub ord_latin1_to_native {
1163     # given an input code point, return the platform's native
1164     # equivalent value.  Anything above latin1 is itself.
1165
1166     my $ord = shift;
1167     return $ord if $ord > 255;
1168     return ord latin1_to_native(chr $ord);
1169 }
1170
1171 sub ord_native_to_latin1 {
1172     # given an input platform code point, return the latin1 equivalent value.
1173     # Anything above latin1 is itself.
1174
1175     my $ord = shift;
1176     return $ord if $ord > 255;
1177     return ord native_to_latin1(chr $ord);
1178 }
1179
1180 1;