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