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