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