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
9 # Increment ($x++) has a certain amount of cleverness for things like
12 # $x++; # $x eq 'aaa';
14 # stands more chance of breaking than just a simple
18 # In this file, we use the latter "Baby Perl" approach, and increment
19 # will be worked over by t/op/inc.t
25 my $Perl; # Safer version of $^X set by which_perl()
27 # This defines ASCII/UTF-8 vs EBCDIC/UTF-EBCDIC
28 $::IS_ASCII = ord 'A' == 65;
29 $::IS_EBCDIC = ord 'A' == 193;
33 $Tests_Are_Passing = 1;
35 # Use this instead of print to avoid interference while testing globals.
37 local($\, $", $,) = (undef, ' ', '');
42 local($\, $", $,) = (undef, ' ', '');
50 if ($n eq 'no_plan') {
58 _print "1..$n\n" unless $noplan;
63 # Set the plan at the end. See Test::More::done_testing.
76 if (defined $planned && $planned != $ran) {
78 "# Looks like you planned $planned tests but ran $ran.\n";
87 my @mess = _comment(@_);
88 $TODO ? _print(@mess) : _print_stderr(@mess);
91 # Use this instead of "print STDERR" when outputting failure diagnostic
97 # Use this instead of "print" when outputting informational messages
100 _print( _comment(@_) );
104 return !defined &DynaLoader::boot_DynaLoader;
108 return map { /^#/ ? "$_\n" : "# $_\n" }
109 map { split /\n/ } @_;
112 sub _have_dynamic_extension {
113 my $extension = shift;
114 unless (eval {require Config; 1}) {
115 warn "test.pl had problems loading Config: $@";
118 $extension =~ s!::!/!g;
119 return 1 if ($Config::Config{extensions} =~ /\b$extension\b/);
124 _print "1..0 # Skip @_\n";
131 sub skip_all_if_miniperl {
132 skip_all(@_) if is_miniperl();
135 sub skip_all_without_dynamic_extension {
136 my ($extension) = @_;
137 skip_all("no dynamic loading on miniperl, no $extension") if is_miniperl();
138 return if &_have_dynamic_extension;
139 skip_all("$extension was not built");
142 sub skip_all_without_perlio {
143 skip_all('no PerlIO') unless PerlIO::Layer->find('perlio');
146 sub skip_all_without_config {
147 unless (eval {require Config; 1}) {
148 warn "test.pl had problems loading Config: $@";
152 next if $Config::Config{$_};
153 my $key = $_; # Need to copy, before trying to modify.
160 sub find_git_or_skip {
161 my ($source_dir, $reason);
164 } elsif (-l 'MANIFEST' && -l 'AUTHORS') {
165 my $where = readlink 'MANIFEST';
166 die "Can't readling MANIFEST: $!" unless defined $where;
167 die "Confusing symlink target for MANIFEST, '$where'"
168 unless $where =~ s!/MANIFEST\z!!;
169 if (-d "$where/.git") {
170 # Looks like we are in a symlink tree
171 if (exists $ENV{GIT_DIR}) {
172 diag("Found source tree at $where, but \$ENV{GIT_DIR} is $ENV{GIT_DIR}. Not changing it");
174 note("Found source tree at $where, setting \$ENV{GIT_DIR}");
175 $ENV{GIT_DIR} = "$where/.git";
177 $source_dir = $where;
181 my $version_string = `git --version`;
182 if (defined $version_string
183 && $version_string =~ /\Agit version (\d+\.\d+\.\d+)(.*)/) {
184 return $source_dir if eval "v$1 ge v1.5.0";
185 # If you have earlier than 1.5.0 and it works, change this test
186 $reason = "in git checkout, but git version '$1$2' too old";
188 $reason = "in git checkout, but cannot run git";
191 $reason = 'not being run from a git checkout';
193 skip_all($reason) if $_[0] && $_[0] eq 'all';
199 _print("Bail out! $reason\n");
204 my ($pass, $where, $name, @mess) = @_;
205 # Do not try to microoptimize by factoring out the "not ".
209 # escape out '#' or it will interfere with '# skip' and such
211 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
213 $out = $pass ? "ok $test" : "not ok $test";
217 $out = $out . " # TODO $TODO";
219 $Tests_Are_Passing = 0 unless $pass;
225 note @mess; # Ensure that the message is properly escaped.
228 my $msg = "# Failed test $test - ";
229 $msg.= "$name " if $name;
235 $test = $test + 1; # don't use ++
241 my @caller = caller($Level);
242 return "at $caller[1] line $caller[2]";
245 # DON'T use this for matches. Use like() instead.
247 my ($pass, $name, @mess) = @_;
248 _ok($pass, _where(), $name, @mess);
253 return 'undef' unless defined $x;
262 return defined $x ? '"' . display ($x) . '"' : 'undef';
265 # keys are the codes \n etc map to, values are 2 char strings such as \n
266 my %backslash_escape;
267 foreach my $x (split //, 'nrtfa\\\'"') {
268 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
270 # A way to display scalars containing control characters and Unicode.
271 # Trying to avoid setting $_, or relying on local $_ to work.
275 if (defined $x and not ref $x) {
277 foreach my $c (unpack("U*", $x)) {
279 $y = $y . sprintf "\\x{%x}", $c;
280 } elsif ($backslash_escape{$c}) {
281 $y = $y . $backslash_escape{$c};
283 my $z = chr $c; # Maybe we can get away with a literal...
284 if ($z =~ /[[:^print:]]/) {
286 # Use octal for characters traditionally expressed as
287 # such: the low controls
289 $z = sprintf "\\%03o", $c;
291 $z = sprintf "\\x{%x}", $c;
299 return $x unless wantarray;
306 my ($got, $expected, $name, @mess) = @_;
309 if( !defined $got || !defined $expected ) {
310 # undef only matches undef
311 $pass = !defined $got && !defined $expected;
314 $pass = $got eq $expected;
318 unshift(@mess, "# got "._qq($got)."\n",
319 "# expected "._qq($expected)."\n");
321 _ok($pass, _where(), $name, @mess);
325 my ($got, $isnt, $name, @mess) = @_;
328 if( !defined $got || !defined $isnt ) {
329 # undef only matches undef
330 $pass = defined $got || defined $isnt;
333 $pass = $got ne $isnt;
337 unshift(@mess, "# it should not be "._qq($got)."\n",
340 _ok($pass, _where(), $name, @mess);
344 my($got, $type, $expected, $name, @mess) = @_;
349 local($@,$!); # don't interfere with $@
350 # eval() sometimes resets $!
351 $pass = eval "\$got $type \$expected";
354 # It seems Irix long doubles can have 2147483648 and 2147483648
355 # that stringify to the same thing but are actually numerically
356 # different. Display the numbers if $type isn't a string operator,
357 # and the numbers are stringwise the same.
358 # (all string operators have alphabetic names, so tr/a-z// is true)
359 # This will also show numbers for some unneeded cases, but will
360 # definitely be helpful for things such as == and <= that fail
361 if ($got eq $expected and $type !~ tr/a-z//) {
362 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
364 unshift(@mess, "# got "._qq($got)."\n",
365 "# expected $type "._qq($expected)."\n");
367 _ok($pass, _where(), $name, @mess);
370 # Check that $got is within $range of $expected
371 # if $range is 0, then check it's exact
372 # else if $expected is 0, then $range is an absolute value
373 # otherwise $range is a fractional error.
374 # Here $range must be numeric, >= 0
375 # Non numeric ranges might be a useful future extension. (eg %)
377 my ($got, $expected, $range, $name, @mess) = @_;
379 if (!defined $got or !defined $expected or !defined $range) {
380 # This is a fail, but doesn't need extra diagnostics
381 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
383 unshift @mess, "# got, expected and range must be numeric\n";
384 } elsif ($range < 0) {
385 # This is also a fail
386 unshift @mess, "# range must not be negative\n";
387 } elsif ($range == 0) {
389 $pass = $got == $expected;
390 } elsif ($expected == 0) {
391 # If expected is 0, treat range as absolute
392 $pass = ($got <= $range) && ($got >= - $range);
394 my $diff = $got - $expected;
395 $pass = abs ($diff / $expected) < $range;
398 if ($got eq $expected) {
399 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
401 unshift@mess, "# got "._qq($got)."\n",
402 "# expected "._qq($expected)." (within "._qq($range).")\n";
404 _ok($pass, _where(), $name, @mess);
407 # Note: this isn't quite as fancy as Test::More::like().
409 sub like ($$@) { like_yn (0,@_) }; # 0 for -
410 sub unlike ($$@) { like_yn (1,@_) }; # 1 for un-
413 my ($flip, undef, $expected, $name, @mess) = @_;
415 $pass = $_[1] =~ /$expected/ if !$flip;
416 $pass = $_[1] !~ /$expected/ if $flip;
418 unshift(@mess, "# got '$_[1]'\n",
420 ? "# expected !~ /$expected/\n" : "# expected /$expected/\n");
422 local $Level = $Level + 1;
423 _ok($pass, _where(), $name, @mess);
431 _ok(0, _where(), @_);
441 $test = $test + 1; # don't use ++
445 # Note: can't pass multipart messages since we try to
446 # be compatible with Test::More::skip().
449 my $n = @_ ? shift : 1;
451 _print "ok $test # skip $why\n";
458 sub skip_if_miniperl {
459 skip(@_) if is_miniperl();
462 sub skip_without_dynamic_extension {
463 my ($extension) = @_;
464 skip("no dynamic loading on miniperl, no $extension") if is_miniperl();
465 return if &_have_dynamic_extension;
466 skip("$extension was not built");
471 my $n = @_ ? shift : 1;
474 _print "not ok $test # TODO & SKIP $why\n";
483 return 0 unless $#$ra == $#$rb;
484 for my $i (0..$#$ra) {
485 next if !defined $ra->[$i] && !defined $rb->[$i];
486 return 0 if !defined $ra->[$i];
487 return 0 if !defined $rb->[$i];
488 return 0 unless $ra->[$i] eq $rb->[$i];
494 my ($orig, $suspect) = @_;
496 while (my ($key, $value) = each %$suspect) {
497 # Force a hash recompute if this perl's internals can cache the hash key.
499 if (exists $orig->{$key}) {
501 defined $orig->{$key} != defined $value
502 || (defined $value && $orig->{$key} ne $value)
504 _print "# key ", _qq($key), " was ", _qq($orig->{$key}),
505 " now ", _qq($value), "\n";
509 _print "# key ", _qq($key), " is ", _qq($value),
510 ", not in original.\n";
514 foreach (keys %$orig) {
515 # Force a hash recompute if this perl's internals can cache the hash key.
517 next if (exists $suspect->{$_});
518 _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
524 # We only provide a subset of the Test::More functionality.
527 if ($require =~ tr/[A-Za-z0-9:.]//c) {
528 fail("Invalid character in \"$require\", passed to require_ok");
533 is($@, '', _where(), "require $require");
539 if ($use =~ tr/[A-Za-z0-9:.]//c) {
540 fail("Invalid character in \"$use\", passed to use");
545 is($@, '', _where(), "use $use");
549 # runperl - Runs a separate perl interpreter.
551 # switches => [ command-line switches ]
552 # nolib => 1 # don't use -I../lib (included by default)
553 # non_portable => Don't warn if a one liner contains quotes
554 # prog => one-liner (avoid quotes)
555 # progs => [ multi-liner (avoid quotes) ]
556 # progfile => perl script
557 # stdin => string to feed the stdin (or undef to redirect from /dev/null)
558 # stderr => redirect stderr to stdout
559 # args => [ command-line arguments to the perl program ]
560 # verbose => print the command line
562 my $is_mswin = $^O eq 'MSWin32';
563 my $is_netware = $^O eq 'NetWare';
564 my $is_vms = $^O eq 'VMS';
565 my $is_cygwin = $^O eq 'cygwin';
568 my ($runperl, $args) = @_;
571 # In VMS protect with doublequotes because otherwise
572 # DCL will lowercase -- unless already doublequoted.
573 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
574 $runperl = $runperl . ' ' . $_;
579 sub _create_runperl { # Create the string to qx in runperl().
581 my $runperl = which_perl();
582 if ($runperl =~ m/\s/) {
583 $runperl = qq{"$runperl"};
585 #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
586 if ($ENV{PERL_RUNPERL_DEBUG}) {
587 $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
589 unless ($args{nolib}) {
590 $runperl = $runperl . ' "-I../lib"'; # doublequotes because of VMS
592 if ($args{switches}) {
594 die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
595 unless ref $args{switches} eq "ARRAY";
596 $runperl = _quote_args($runperl, $args{switches});
598 if (defined $args{prog}) {
599 die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
600 if defined $args{progs};
601 $args{progs} = [$args{prog}]
603 if (defined $args{progs}) {
604 die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
605 unless ref $args{progs} eq "ARRAY";
606 foreach my $prog (@{$args{progs}}) {
607 if ($prog =~ tr/'"// && !$args{non_portable}) {
608 warn "quotes in prog >>$prog<< are not portable";
610 if ($is_mswin || $is_netware || $is_vms) {
611 $runperl = $runperl . qq ( -e "$prog" );
614 $runperl = $runperl . qq ( -e '$prog' );
617 } elsif (defined $args{progfile}) {
618 $runperl = $runperl . qq( "$args{progfile}");
620 # You probably didn't want to be sucking in from the upstream stdin
621 die "test.pl:runperl(): none of prog, progs, progfile, args, "
622 . " switches or stdin specified"
623 unless defined $args{args} or defined $args{switches}
624 or defined $args{stdin};
626 if (defined $args{stdin}) {
627 # so we don't try to put literal newlines and crs onto the
629 $args{stdin} =~ s/\n/\\n/g;
630 $args{stdin} =~ s/\r/\\r/g;
632 if ($is_mswin || $is_netware || $is_vms) {
633 $runperl = qq{$Perl -e "print qq(} .
634 $args{stdin} . q{)" | } . $runperl;
637 $runperl = qq{$Perl -e 'print qq(} .
638 $args{stdin} . q{)' | } . $runperl;
640 } elsif (exists $args{stdin}) {
641 # Using the pipe construction above can cause fun on systems which use
642 # ksh as /bin/sh, as ksh does pipes differently (with one less process)
643 # With sh, for the command line 'perl -e 'print qq()' | perl -e ...'
644 # the sh process forks two children, which use exec to start the two
645 # perl processes. The parent shell process persists for the duration of
646 # the pipeline, and the second perl process starts with no children.
647 # With ksh (and zsh), the shell saves a process by forking a child for
648 # just the first perl process, and execing itself to start the second.
649 # This means that the second perl process starts with one child which
650 # it didn't create. This causes "fun" when if the tests assume that
651 # wait (or waitpid) will only return information about processes
652 # started within the test.
653 # They also cause fun on VMS, where the pipe implementation returns
654 # the exit code of the process at the front of the pipeline, not the
655 # end. This messes up any test using OPTION FATAL.
656 # Hence it's useful to have a way to make STDIN be at eof without
657 # needing a pipeline, so that the fork tests have a sane environment
658 # without these surprises.
660 # /dev/null appears to be surprisingly portable.
661 $runperl = $runperl . ($is_mswin ? ' <nul' : ' </dev/null');
663 if (defined $args{args}) {
664 $runperl = _quote_args($runperl, $args{args});
666 $runperl = $runperl . ' 2>&1' if $args{stderr};
667 if ($args{verbose}) {
668 my $runperldisplay = $runperl;
669 $runperldisplay =~ s/\n/\n\#/g;
670 _print_stderr "# $runperldisplay\n";
676 die "test.pl:runperl() does not take a hashref"
677 if ref $_[0] and ref $_[0] eq 'HASH';
678 my $runperl = &_create_runperl;
681 my $tainted = ${^TAINT};
683 exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1;
686 # We will assume that if you're running under -T, you really mean to
687 # run a fresh perl, so we'll brute force launder everything for you
690 if (! eval {require Config; 1}) {
691 warn "test.pl had problems loading Config: $@";
694 $sep = $Config::Config{path_sep};
697 my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV);
698 local @ENV{@keys} = ();
699 # Untaint, plus take out . and empty string:
700 local $ENV{'DCL$PATH'} = $1 if $is_vms && exists($ENV{'DCL$PATH'}) && ($ENV{'DCL$PATH'} =~ /(.*)/s);
701 $ENV{PATH} =~ /(.*)/s;
703 join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and
704 ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) }
705 split quotemeta ($sep), $1;
706 if ($is_cygwin) { # Must have /bin under Cygwin
707 if (length $ENV{PATH}) {
708 $ENV{PATH} = $ENV{PATH} . $sep;
710 $ENV{PATH} = $ENV{PATH} . '/bin';
715 $result = `$runperl`;
717 $result = `$runperl`;
719 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
724 *run_perl = *run_perl = \&runperl; # shut up "used only once" warning
727 _print_stderr "# @_\n";
731 # A somewhat safer version of the sometimes wrong $^X.
733 unless (defined $Perl) {
736 # VMS should have 'perl' aliased properly
737 return $Perl if $is_vms;
740 if (! eval {require Config; 1}) {
741 warn "test.pl had problems loading Config: $@";
744 $exe = $Config::Config{_exe};
746 $exe = '' unless defined $exe;
748 # This doesn't absolutize the path: beware of future chdirs().
749 # We could do File::Spec->abs2rel() but that does getcwd()s,
750 # which is a bit heavyweight to do here.
752 if ($Perl =~ /^perl\Q$exe\E$/i) {
753 my $perl = "perl$exe";
754 if (! eval {require File::Spec; 1}) {
755 warn "test.pl had problems loading File::Spec: $@";
758 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
762 # Build up the name of the executable file from the name of
765 if ($Perl !~ /\Q$exe\E$/i) {
766 $Perl = $Perl . $exe;
769 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
771 # For subcommands to use.
772 $ENV{PERLEXE} = $Perl;
779 foreach my $file (@_) {
780 1 while unlink $file;
782 _print_stderr "# Couldn't unlink '$file': $!\n";
790 # _num_to_alpha - Returns a string of letters representing a positive integer.
793 # maximum number of letters
795 # returns undef if the number is negative
796 # returns undef if the number of letters is greater than the maximum wanted
798 # _num_to_alpha( 0) eq 'A';
799 # _num_to_alpha( 1) eq 'B';
800 # _num_to_alpha(25) eq 'Z';
801 # _num_to_alpha(26) eq 'AA';
802 # _num_to_alpha(27) eq 'AB';
804 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);
806 # Avoid ++ -- ranges split negative numbers
808 my($num,$max_char) = @_;
809 return unless $num >= 0;
812 $max_char = 0 if $max_char < 0;
815 $alpha = $letters[ $num % 26 ] . $alpha;
816 $num = int( $num / 26 );
821 next unless $max_char;
822 $char_count = $char_count + 1;
823 return if $char_count == $max_char;
829 END { unlink_all keys %tmpfiles }
831 # A regexp that matches the tempfile names
832 $::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?';
834 # Avoid ++, avoid ranges, avoid split //
835 my $tempfile_count = 0;
839 my $alpha = _num_to_alpha($tempfile_count,2);
840 last unless defined $alpha;
841 $try = $try . $alpha;
842 $tempfile_count = $tempfile_count + 1;
844 # Need to note all the file names we allocated, as a second request may
845 # come before the first is created.
846 if (!$tmpfiles{$try} && !-e $try) {
852 die "Can't find temporary file name starting \"tmp$$\"";
855 # This is the temporary file for _fresh_perl
856 my $tmpfile = tempfile();
859 my($prog, $action, $expect, $runperl_args, $name) = @_;
861 # Given the choice of the mis-parsable {}
862 # (we want an anon hash, but a borked lexer might think that it's a block)
863 # or relying on taking a reference to a lexical
864 # (\ might be mis-parsed, and the reference counting on the pad may go
866 # it feels like the least-worse thing is to assume that auto-vivification
867 # works. At least, this is only going to be a run-time failure, so won't
868 # affect tests using this file but not this function.
869 $runperl_args->{progfile} ||= $tmpfile;
870 $runperl_args->{stderr} = 1 unless exists $runperl_args->{stderr};
872 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
874 close TEST or die "Cannot close $tmpfile: $!";
876 my $results = runperl(%$runperl_args);
879 # Clean up the results into something a bit more predictable.
880 $results =~ s/\n+$//;
881 $results =~ s/at\s+$::tempfile_regexp\s+line/at - line/g;
882 $results =~ s/of\s+$::tempfile_regexp\s+aborted/of - aborted/g;
884 # bison says 'parse error' instead of 'syntax error',
885 # various yaccs may or may not capitalize 'syntax'.
886 $results =~ s/^(syntax|parse) error/syntax error/mig;
889 # some tests will trigger VMS messages that won't be expected
890 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
892 # pipes double these sometimes
893 $results =~ s/\n\n/\n/g;
896 # Use the first line of the program as a name if none was given
898 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
899 $name = $name . '...' if length $first_line > length $name;
902 # Historically this was implemented using a closure, but then that means
903 # that the tests for closures avoid using this code. Given that there
904 # are exactly two callers, doing exactly two things, the simpler approach
905 # feels like a better trade off.
907 if ($action eq 'eq') {
908 $pass = is($results, $expect, $name);
909 } elsif ($action eq '=~') {
910 $pass = like($results, $expect, $name);
912 die "_fresh_perl can't process action '$action'";
916 _diag "# PROG: \n$prog\n";
917 _diag "# STATUS: $status\n";
926 # Combination of run_perl() and is().
930 my($prog, $expected, $runperl_args, $name) = @_;
932 # _fresh_perl() is going to clip the trailing newlines off the result.
933 # This will make it so the test author doesn't have to know that.
934 $expected =~ s/\n+$//;
937 _fresh_perl($prog, 'eq', $expected, $runperl_args, $name);
943 # Combination of run_perl() and like().
946 sub fresh_perl_like {
947 my($prog, $expected, $runperl_args, $name) = @_;
949 _fresh_perl($prog, '=~', $expected, $runperl_args, $name);
952 # Many tests use the same format in __DATA__ or external files to specify a
953 # sequence of (fresh) tests to run, extra files they may temporarily need, and
954 # what the expected output is. So have excatly one copy of the code to run that
956 # Each program is source code to run followed by an "EXPECT" line, followed
957 # by the expected output.
959 # The code to run may begin with a command line switch such as -w or -0777
960 # (alphanumerics only), and may contain (note the '# ' on each):
961 # # TODO reason for todo
962 # # SKIP reason for skip
963 # # SKIP ?code to test if this should be skipped
964 # # NAME name of the test (as with ok($ok, $name))
966 # The expected output may contain:
967 # OPTION list of options
968 # OPTIONS list of options
970 # The possible options for OPTION may be:
971 # regex - the expected output is a regular expression
972 # random - all lines match but in any order
973 # fatal - the code will fail fatally (croak, die)
975 # If the actual output contains a line "SKIPPED" the test will be
978 # If the actual output contains a line "PREFIX", any output starting with that
979 # line will be ignored when comparing with the expected output
981 # If the global variable $FATAL is true then OPTION fatal is the
984 sub _setup_one_file {
986 # Store the filename as a program that started at line 0.
987 # Real files count lines starting at line 1.
988 my @these = (0, shift);
989 my ($lineno, $current);
991 if ($_ eq "########\n") {
992 if (defined $current) {
993 push @these, $lineno, $current;
997 if (!defined $current) {
1003 if (defined $current) {
1004 push @these, $lineno, $current;
1006 ((scalar @these) / 2 - 1, @these);
1009 sub setup_multiple_progs {
1011 foreach my $file (@_) {
1012 next if $file =~ /(?:~|\.orig|,v)$/;
1013 next if $file =~ /perlio$/ && !PerlIO::Layer->find('perlio');
1016 open my $fh, '<', $file or die "Cannot open $file: $!\n" ;
1024 # This is an internal error, and should never happen. All bar one of
1025 # the files had an __END__ marker to signal the end of their preamble,
1026 # although for some it wasn't technically necessary as they have no
1027 # tests. It might be possible to process files without an __END__ by
1028 # seeking back to the start and treating the whole file as tests, but
1029 # it's simpler and more reliable just to make the rule that all files
1030 # must have __END__ in. This should never fail - a file without an
1031 # __END__ should not have been checked in, because the regression tests
1032 # would not have passed.
1033 die "Could not find '__END__' in $file"
1036 my ($t, @p) = _setup_one_file($fh, $file);
1041 or die "Cannot close $file: $!\n";
1043 return ($tests, @prgs);
1046 sub run_multiple_progs {
1050 # The tests in lib run in a temporary subdirectory of t, and always
1051 # pass in a list of "programs" to run
1054 # The tests below t run in t and pass in a file handle. In theory we
1055 # can pass (caller)[1] as the second argument to report errors with
1056 # the filename of our caller, as the handle is always DATA. However,
1057 # line numbers in DATA count from the __END__ token, so will be wrong.
1058 # Which is more confusing than not providing line numbers. So, for now,
1059 # don't provide line numbers. No obvious clean solution - one hack
1060 # would be to seek DATA back to the start and read to the __END__ token,
1061 # but that feels almost like we should just open $0 instead.
1063 # Not going to rely on undef in list assignment.
1065 ($dummy, @prgs) = _setup_one_file(shift);
1068 my $tmpfile = tempfile();
1072 while (defined ($line = shift @prgs)) {
1076 if (defined $file) {
1077 print "# From $file\n";
1084 if (s/^(\s*-\w+)//) {
1087 my ($prog, $expected) = split(/\nEXPECT(?:\n|$)/, $_, 2);
1090 foreach my $what (qw(skip todo)) {
1091 $prog =~ s/^#\s*\U$what\E\s*(.*)\n//m and $reason{$what} = $1;
1092 # If the SKIP reason starts ? then it's taken as a code snippet to
1093 # evaluate. This provides the flexibility to have conditional SKIPs
1094 if ($reason{$what} && $reason{$what} =~ s/^\?//) {
1095 my $temp = eval $reason{$what};
1097 die "# In \U$what\E code reason:\n# $reason{$what}\n$@";
1099 $reason{$what} = $temp;
1104 if ($prog =~ s/^#\s*NAME\s+(.+)\n//m) {
1108 if ($reason{skip}) {
1111 skip($name ? "$name - $reason{skip}" : $reason{skip}, 1);
1116 if ($prog =~ /--FILE--/) {
1117 my @files = split(/\n?--FILE--\s*([^\s\n]*)\s*\n/, $prog) ;
1119 die "Internal error: test $_ didn't split into pairs, got " .
1120 scalar(@files) . "[" . join("%%%%", @files) ."]\n"
1122 while (@files > 2) {
1123 my $filename = shift @files;
1124 my $code = shift @files;
1125 push @temps, $filename;
1126 if ($filename =~ m#(.*)/# && $filename !~ m#^\.\./#) {
1128 File::Path::mkpath($1);
1129 push(@temp_path, $1);
1131 open my $fh, '>', $filename or die "Cannot open $filename: $!\n";
1133 close $fh or die "Cannot close $filename: $!\n";
1136 $prog = shift @files;
1139 open my $fh, '>', $tmpfile or die "Cannot open >$tmpfile: $!";
1142 open STDERR, '>&', STDOUT
1143 or die "Can't dup STDOUT->STDERR: $!;";
1146 print $fh "\n#line 1\n"; # So the line numbers don't get messed up.
1147 print $fh $prog,"\n";
1148 close $fh or die "Cannot close $tmpfile: $!";
1149 my $results = runperl( stderr => 1, progfile => $tmpfile,
1151 ? (switches => ["-I$up/lib", $switch], nolib => 1)
1152 : (switches => [$switch])
1155 $results =~ s/\n+$//;
1156 # allow expected output to be written as if $prog is on STDIN
1157 $results =~ s/$::tempfile_regexp/-/g;
1159 # some tests will trigger VMS messages that won't be expected
1160 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
1162 # pipes double these sometimes
1163 $results =~ s/\n\n/\n/g;
1165 # bison says 'parse error' instead of 'syntax error',
1166 # various yaccs may or may not capitalize 'syntax'.
1167 $results =~ s/^(syntax|parse) error/syntax error/mig;
1168 # allow all tests to run when there are leaks
1169 $results =~ s/Scalars leaked: \d+\n//g;
1171 $expected =~ s/\n+$//;
1172 my $prefix = ($results =~ s#^PREFIX(\n|$)##) ;
1173 # any special options? (OPTIONS foo bar zap)
1174 my $option_regex = 0;
1175 my $option_random = 0;
1177 if ($expected =~ s/^OPTIONS? (.+)\n//) {
1178 foreach my $option (split(' ', $1)) {
1179 if ($option eq 'regex') { # allow regular expressions
1182 elsif ($option eq 'random') { # all lines match, but in any order
1185 elsif ($option eq 'fatal') { # perl should fail
1189 die "$0: Unknown OPTION '$option'\n";
1193 die "$0: can't have OPTION regex and random\n"
1194 if $option_regex + $option_random > 1;
1196 if ($results =~ s/^SKIPPED\n//) {
1197 print "$results\n" ;
1201 if ($option_random) {
1202 my @got = sort split "\n", $results;
1203 my @expected = sort split "\n", $expected;
1205 $ok = "@got" eq "@expected";
1207 elsif ($option_regex) {
1208 $ok = $results =~ /^$expected/;
1211 $ok = $results =~ /^\Q$expected/;
1214 $ok = $results eq $expected;
1217 if ($ok && $fatal && !($status >> 8)) {
1222 local $::TODO = $reason{todo};
1225 my $err_line = "PROG: $switch\n$prog\n" .
1226 "EXPECTED:\n$expected\n";
1227 $err_line .= "EXIT STATUS: != 0\n" if $fatal;
1228 $err_line .= "GOT:\n$results\n";
1229 $err_line .= "EXIT STATUS: " . ($status >> 8) . "\n" if $fatal;
1231 $err_line =~ s/^/# /mg;
1232 print $err_line; # Harness can't filter it out from STDERR.
1235 print STDERR $err_line;
1239 if (defined $file) {
1240 _ok($ok, "at $file line $line", $name);
1242 # We don't have file and line number data for the test, so report
1243 # errors as coming from our caller.
1244 local $Level = $Level + 1;
1251 foreach (@temp_path) {
1252 File::Path::rmtree $_ if -d $_;
1258 my($proto, @methods) = @_;
1259 my $class = ref $proto || $proto;
1261 unless( @methods ) {
1262 return _ok( 0, _where(), "$class->can(...)" );
1266 foreach my $method (@methods) {
1267 local($!, $@); # don't interfere with caller's $@
1268 # eval sometimes resets $!
1269 eval { $proto->can($method) } || push @nok, $method;
1273 $name = @methods == 1 ? "$class->can('$methods[0]')"
1274 : "$class->can(...)";
1276 _ok( !@nok, _where(), $name );
1280 # Call $class->new( @$args ); and run the result through object_ok.
1281 # See Test::More::new_ok
1283 my($class, $args, $obj_name) = @_;
1285 $object_name = "The object" unless defined $obj_name;
1287 local $Level = $Level + 1;
1290 my $ok = eval { $obj = $class->new(@$args); 1 };
1294 object_ok($obj, $class, $object_name);
1297 ok( 0, "new() died" );
1298 diag("Error was: $@");
1307 my($object, $class, $obj_name) = @_;
1310 $obj_name = 'The object' unless defined $obj_name;
1311 my $name = "$obj_name isa $class";
1312 if( !defined $object ) {
1313 $diag = "$obj_name isn't defined";
1316 my $whatami = ref $object ? 'object' : 'class';
1318 # We can't use UNIVERSAL::isa because we want to honor isa() overrides
1319 local($@, $!); # eval sometimes resets $!
1320 my $rslt = eval { $object->isa($class) };
1321 my $error = $@; # in case something else blows away $@
1324 if( $error =~ /^Can't call method "isa" on unblessed reference/ ) {
1325 # It's an unblessed reference
1326 $obj_name = 'The reference' unless defined $obj_name;
1327 if( !UNIVERSAL::isa($object, $class) ) {
1328 my $ref = ref $object;
1329 $diag = "$obj_name isn't a '$class' it's a '$ref'";
1332 elsif( $error =~ /Can't call method "isa" without a package/ ) {
1333 # It's something that can't even be a class
1334 $obj_name = 'The thing' unless defined $obj_name;
1335 $diag = "$obj_name isn't a class or reference";
1339 WHOA! I tried to call ->isa on your object and got some weird error.
1340 This should never happen. Please contact the author immediately.
1347 $obj_name = "The $whatami" unless defined $obj_name;
1348 my $ref = ref $object;
1349 $diag = "$obj_name isn't a '$class' it's a '$ref'";
1353 _ok( !$diag, _where(), $name );
1358 my($class, $isa, $class_name) = @_;
1360 # Written so as to count as one test
1361 local $Level = $Level + 1;
1363 ok( 0, "$class is a refrence, not a class name" );
1366 isa_ok($class, $isa, $class_name);
1372 my($obj, $isa, $obj_name) = @_;
1374 local $Level = $Level + 1;
1376 ok( 0, "$obj is not a reference" );
1379 isa_ok($obj, $isa, $obj_name);
1384 # Purposefully avoiding a closure.
1386 push @::__capture, join "", @_;
1389 sub capture_warnings {
1393 local $SIG {__WARN__} = \&__capture;
1395 return @::__capture;
1398 # This will generate a variable number of tests.
1399 # Use done_testing() instead of a fixed plan.
1401 my ($code, $expect, $name) = @_;
1402 local $Level = $Level + 1;
1404 my @w = capture_warnings($code);
1406 cmp_ok(scalar @w, '==', scalar @$expect, $name);
1407 foreach my $e (@$expect) {
1409 like(shift @w, $e, $name);
1411 is(shift @w, $e, $name);
1415 diag("Saw these additional warnings:");
1416 diag($_) foreach @w;
1420 sub _fail_excess_warnings {
1421 my($expect, $got, $name) = @_;
1422 local $Level = $Level + 1;
1423 # This will fail, and produce diagnostics
1424 is($expect, scalar @$got, $name);
1425 diag("Saw these warnings:");
1426 diag($_) foreach @$got;
1430 my ($code, $expect, $name) = @_;
1431 die sprintf "Expect must be a string or undef, not a %s reference", ref $expect
1433 local $Level = $Level + 1;
1434 my @w = capture_warnings($code);
1436 _fail_excess_warnings(0 + defined $expect, \@w, $name);
1438 is($w[0], $expect, $name);
1443 my ($code, $expect, $name) = @_;
1444 die sprintf "Expect must be a regexp object"
1445 unless ref $expect eq 'Regexp';
1446 local $Level = $Level + 1;
1447 my @w = capture_warnings($code);
1449 _fail_excess_warnings(0 + defined $expect, \@w, $name);
1451 like($w[0], $expect, $name);
1455 # Set a watchdog to timeout the entire test file
1456 # NOTE: If the test file uses 'threads', then call the watchdog() function
1457 # _AFTER_ the 'threads' module is loaded.
1460 my $timeout = shift;
1461 my $method = shift || "";
1462 my $timeout_msg = 'Test process timed out - terminating';
1464 # Valgrind slows perl way down so give it more time before dying.
1465 $timeout *= 10 if $ENV{PERL_VALGRIND};
1467 my $pid_to_kill = $$; # PID for this process
1469 if ($method eq "alarm") {
1470 goto WATCHDOG_VIA_ALARM;
1473 # shut up use only once warning
1474 my $threads_on = $threads::threads && $threads::threads;
1476 # Don't use a watchdog process if 'threads' is loaded -
1477 # use a watchdog thread instead
1478 if (!$threads_on || $method eq "process") {
1480 # On Windows and VMS, try launching a watchdog process
1481 # using system(1, ...) (see perlport.pod)
1482 if ($is_mswin || $is_vms) {
1483 # On Windows, try to get the 'real' PID
1485 eval { require Win32; };
1486 if (defined(&Win32::GetCurrentProcessId)) {
1487 $pid_to_kill = Win32::GetCurrentProcessId();
1491 # If we still have a fake PID, we can't use this method at all
1492 return if ($pid_to_kill <= 0);
1494 # Launch watchdog process
1497 local $SIG{'__WARN__'} = sub {
1498 _diag("Watchdog warning: $_[0]");
1500 my $sig = $is_vms ? 'TERM' : 'KILL';
1501 my $cmd = _create_runperl( prog => "sleep($timeout);" .
1502 "warn qq/# $timeout_msg" . '\n/;' .
1503 "kill($sig, $pid_to_kill);");
1504 $watchdog = system(1, $cmd);
1506 if ($@ || ($watchdog <= 0)) {
1507 _diag('Failed to start watchdog');
1513 # Add END block to parent to terminate and
1514 # clean up watchdog process
1515 eval "END { local \$! = 0; local \$? = 0;
1516 wait() if kill('KILL', $watchdog); };";
1520 # Try using fork() to generate a watchdog process
1522 eval { $watchdog = fork() };
1523 if (defined($watchdog)) {
1524 if ($watchdog) { # Parent process
1525 # Add END block to parent to terminate and
1526 # clean up watchdog process
1527 eval "END { local \$! = 0; local \$? = 0;
1528 wait() if kill('KILL', $watchdog); };";
1532 ### Watchdog process code
1534 # Load POSIX if available
1535 eval { require POSIX; };
1537 # Execute the timeout
1538 sleep($timeout - 2) if ($timeout > 2); # Workaround for perlbug #49073
1541 # Kill test process if still running
1542 if (kill(0, $pid_to_kill)) {
1543 _diag($timeout_msg);
1544 kill('KILL', $pid_to_kill);
1546 # sometimes the above isn't enough on cygwin
1547 sleep 1; # wait a little, it might have worked after all
1548 system("/bin/kill -f $pid_to_kill");
1552 # Don't execute END block (added at beginning of this file)
1555 # Terminate ourself (i.e., the watchdog)
1556 POSIX::_exit(1) if (defined(&POSIX::_exit));
1560 # fork() failed - fall through and try using a thread
1563 # Use a watchdog thread because either 'threads' is loaded,
1565 if (eval {require threads; 1}) {
1566 'threads'->create(sub {
1567 # Load POSIX if available
1568 eval { require POSIX; };
1570 # Execute the timeout
1571 my $time_left = $timeout;
1573 $time_left = $time_left - sleep($time_left);
1574 } while ($time_left > 0);
1576 # Kill the parent (and ourself)
1577 select(STDERR); $| = 1;
1578 _diag($timeout_msg);
1579 POSIX::_exit(1) if (defined(&POSIX::_exit));
1580 my $sig = $is_vms ? 'TERM' : 'KILL';
1581 kill($sig, $pid_to_kill);
1586 # If everything above fails, then just use an alarm timeout
1588 if (eval { alarm($timeout); 1; }) {
1589 # Load POSIX if available
1590 eval { require POSIX; };
1592 # Alarm handler will do the actual 'killing'
1593 $SIG{'ALRM'} = sub {
1594 select(STDERR); $| = 1;
1595 _diag($timeout_msg);
1596 POSIX::_exit(1) if (defined(&POSIX::_exit));
1597 my $sig = $is_vms ? 'TERM' : 'KILL';
1598 kill($sig, $pid_to_kill);
1603 my $cp_0037 = # EBCDIC code page 0037
1604 '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F' .
1605 '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
1606 '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
1607 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
1608 '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
1609 '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBA\xE0\xBB\xB0\x6D' .
1610 '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1611 '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
1612 '\x20\x21\x22\x23\x24\x15\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1613 '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
1614 '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBD\xB4\x9A\x8A\x5F\xCA\xAF\xBC' .
1615 '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1616 '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1617 '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xAD\xAE\x59' .
1618 '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1619 '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
1621 my $cp_1047 = # EBCDIC code page 1047
1622 '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
1623 '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
1624 '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
1625 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
1626 '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
1627 '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xAD\xE0\xBD\x5F\x6D' .
1628 '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1629 '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' .
1630 '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1631 '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' .
1632 '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBB\xB4\x9A\x8A\xB0\xCA\xAF\xBC' .
1633 '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1634 '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1635 '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xBA\xAE\x59' .
1636 '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1637 '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF';
1639 my $cp_bc = # EBCDIC code page POSiX-BC
1640 '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x15\x0B\x0C\x0D\x0E\x0F' .
1641 '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' .
1642 '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' .
1643 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' .
1644 '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' .
1645 '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBB\xBC\xBD\x6A\x6D' .
1646 '\x4A\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' .
1647 '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xFB\x4F\xFD\xFF\x07' .
1648 '\x20\x21\x22\x23\x24\x25\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' .
1649 '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\x5F' .
1650 '\x41\xAA\xB0\xB1\x9F\xB2\xD0\xB5\x79\xB4\x9A\x8A\xBA\xCA\xAF\xA1' .
1651 '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' .
1652 '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' .
1653 '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xE0\xFE\xDD\xFC\xAD\xAE\x59' .
1654 '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' .
1655 '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xC0\xDE\xDB\xDC\x8D\x8E\xDF';
1657 my $straight = # Avoid ranges
1658 '\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F' .
1659 '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F' .
1660 '\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2A\x2B\x2C\x2D\x2E\x2F' .
1661 '\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3A\x3B\x3C\x3D\x3E\x3F' .
1662 '\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4A\x4B\x4C\x4D\x4E\x4F' .
1663 '\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5A\x5B\x5C\x5D\x5E\x5F' .
1664 '\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6A\x6B\x6C\x6D\x6E\x6F' .
1665 '\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7A\x7B\x7C\x7D\x7E\x7F' .
1666 '\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F' .
1667 '\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F' .
1668 '\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF' .
1669 '\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF' .
1670 '\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF' .
1671 '\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF' .
1672 '\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF' .
1673 '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF';
1675 # The following 2 functions allow tests to work on both EBCDIC and
1676 # ASCII-ish platforms. They convert string scalars between the native
1677 # character set and the set of 256 characters which is usually called
1680 # These routines don't work on UTF-EBCDIC and UTF-8.
1682 sub native_to_latin1($) {
1685 return $string if ord('^') == 94; # ASCII, Latin1
1687 if (ord('^') == 95) { # EBCDIC 1047
1690 elsif (ord('^') == 106) { # EBCDIC POSIX-BC
1693 elsif (ord('^') == 176) { # EBCDIC 037 */
1697 die "Unknown native character set";
1700 eval '$string =~ tr/' . $$cp . '/' . $straight . '/';
1704 sub latin1_to_native($) {
1707 return $string if ord('^') == 94; # ASCII, Latin1
1709 if (ord('^') == 95) { # EBCDIC 1047
1712 elsif (ord('^') == 106) { # EBCDIC POSIX-BC
1715 elsif (ord('^') == 176) { # EBCDIC 037 */
1719 die "Unknown native character set";
1722 eval '$string =~ tr/' . $straight . '/' . $$cp . '/';
1726 sub ord_latin1_to_native {
1727 # given an input code point, return the platform's native
1728 # equivalent value. Anything above latin1 is itself.
1731 return $ord if $ord > 255;
1732 return ord latin1_to_native(chr $ord);
1735 sub ord_native_to_latin1 {
1736 # given an input platform code point, return the latin1 equivalent value.
1737 # Anything above latin1 is itself.
1740 return $ord if $ord > 255;
1741 return ord native_to_latin1(chr $ord);