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