I don't think trying to bracket the hires time with lores
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils.t
CommitLineData
af6c647e 1#!./perl -w
2
72f7b9a1 3print "1..27\n";
af6c647e 4
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8}
9
d7f97632 10# use warnings;
af6c647e 11use strict;
12use ExtUtils::MakeMaker;
13use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
14use Config;
357229c3 15use File::Spec::Functions qw(catfile rel2abs);
835f860c 16# Because were are going to be changing directory before running Makefile.PL
8338e367 17my $perl;
d7f97632 18$perl = rel2abs( $^X ) unless $] < 5.006; # Hack. Until 5.00503 has rel2abs
6d79cad2 19# ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
20# compare output to ensure that it is the same. We were probably run as ./perl
21# whereas we will run the child with the full path in $perl. So make $^X for
22# us the same as our child will see.
23$^X = $perl;
af6c647e 24
835f860c 25print "# perl=$perl\n";
6d79cad2 26my $runperl = "$perl -x \"-I../../lib\"";
94b1a389 27
af6c647e 28$| = 1;
29
30my $dir = "ext-$$";
0ddb8edc 31my @files;
94b1a389 32
33print "# $dir being created...\n";
34mkdir $dir, 0777 or die "mkdir: $!\n";
35
535acd0f 36my $output = "output";
af6c647e 37
38END {
94b1a389 39 use File::Path;
40 print "# $dir being removed...\n";
41 rmtree($dir);
af6c647e 42}
43
6d79cad2 44my $package = "ExtTest";
45
8ac27563 46# Test the code that generates 1 and 2 letter name comparisons.
47my %compass = (
d7f97632 48N => 0, 'NE' => 45, E => 90, SE => 135, S => 180, SW => 225, W => 270, NW => 315
8ac27563 49);
50
cea00dc5 51my $parent_rfc1149 =
52 'A Standard for the Transmission of IP Datagrams on Avian Carriers';
53
835f860c 54my @names = ("FIVE", {name=>"OK6", type=>"PV",},
55 {name=>"OK7", type=>"PVN",
56 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
af6c647e 57 {name => "FARTHING", type=>"NV"},
6d79cad2 58 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
72f7b9a1 59 {name => "OPEN", type=>"PV", value=>'"/*"', macro=>1},
6d79cad2 60 {name => "CLOSE", type=>"PV", value=>'"*/"',
61 macro=>["#if 1\n", "#endif\n"]},
3414cef0 62 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
63 {name => "Yes", type=>"YES"},
64 {name => "No", type=>"NO"},
19d75eda 65 {name => "Undef", type=>"UNDEF"},
cea00dc5 66# OK. It wasn't really designed to allow the creation of dual valued constants.
67# It was more for INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
68 {name=>"RFC1149", type=>"SV", value=>"sv_2mortal(temp_sv)",
69 pre=>"SV *temp_sv = newSVpv(RFC1149, 0); "
70 . "(void) SvUPGRADE(temp_sv,SVt_PVIV); SvIOK_on(temp_sv); "
71 . "SvIVX(temp_sv) = 1149;"},
3414cef0 72);
af6c647e 73
8ac27563 74push @names, $_ foreach keys %compass;
75
af6c647e 76my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
77
6d79cad2 78my $types = {};
79my $constant_types = constant_types(); # macro defs
80my $C_constant = join "\n",
81 C_constant ($package, undef, "IV", $types, undef, undef, @names);
82my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
83
af6c647e 84################ Header
94b1a389 85my $header = catfile($dir, "test.h");
0ddb8edc 86push @files, "test.h";
94b1a389 87open FH, ">$header" or die "open >$header: $!\n";
cea00dc5 88print FH <<"EOT";
835f860c 89#define FIVE 5
7b6ffde5 90#define OK6 "ok 6\\n"
835f860c 91#define OK7 1
af6c647e 92#define FARTHING 0.25
93#define NOT_ZERO 1
3414cef0 94#define Yes 0
95#define No 1
96#define Undef 1
cea00dc5 97#define RFC1149 "$parent_rfc1149"
6d79cad2 98#undef NOTDEF
8ac27563 99
af6c647e 100EOT
8ac27563 101
102while (my ($point, $bearing) = each %compass) {
103 print FH "#define $point $bearing\n"
104}
94b1a389 105close FH or die "close $header: $!\n";
af6c647e 106
107################ XS
94b1a389 108my $xs = catfile($dir, "$package.xs");
0ddb8edc 109push @files, "$package.xs";
94b1a389 110open FH, ">$xs" or die "open >$xs: $!\n";
af6c647e 111
112print FH <<'EOT';
113#include "EXTERN.h"
114#include "perl.h"
115#include "XSUB.h"
116EOT
117
118print FH "#include \"test.h\"\n\n";
6d79cad2 119print FH $constant_types;
120print FH $C_constant, "\n";
af6c647e 121print FH "MODULE = $package PACKAGE = $package\n";
122print FH "PROTOTYPES: ENABLE\n";
6d79cad2 123print FH $XS_constant;
94b1a389 124close FH or die "close $xs: $!\n";
af6c647e 125
126################ PM
94b1a389 127my $pm = catfile($dir, "$package.pm");
0ddb8edc 128push @files, "$package.pm";
94b1a389 129open FH, ">$pm" or die "open >$pm: $!\n";
af6c647e 130print FH "package $package;\n";
131print FH "use $];\n";
132
133print FH <<'EOT';
134
135use strict;
d7f97632 136EOT
137printf FH "use warnings;\n" unless $] < 5.006;
138print FH <<'EOT';
af6c647e 139use Carp;
140
141require Exporter;
142require DynaLoader;
d7f97632 143use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
af6c647e 144
145$VERSION = '0.01';
146@ISA = qw(Exporter DynaLoader);
147@EXPORT_OK = qw(
148EOT
149
150print FH "\t$_\n" foreach (@names_only);
151print FH ");\n";
152print FH autoload ($package, $]);
153print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
94b1a389 154close FH or die "close $pm: $!\n";
af6c647e 155
156################ test.pl
94b1a389 157my $testpl = catfile($dir, "test.pl");
0ddb8edc 158push @files, "test.pl";
94b1a389 159open FH, ">$testpl" or die "open >$testpl: $!\n";
af6c647e 160
6d79cad2 161print FH "use strict;\n";
af6c647e 162print FH "use $package qw(@names_only);\n";
535acd0f 163print FH <<"EOT";
164
165print "1..1\n";
166if (open OUTPUT, ">$output") {
167 print "ok 1\n";
168 select OUTPUT;
169} else {
170 print "not ok 1 # Failed to open '$output': $!\n";
171 exit 1;
172}
173EOT
af6c647e 174
535acd0f 175print FH << 'EOT';
176
177# What follows goes to the temporary file.
6d79cad2 178# IV
835f860c 179my $five = FIVE;
180if ($five == 5) {
181 print "ok 5\n";
af6c647e 182} else {
835f860c 183 print "not ok 5 # $five\n";
af6c647e 184}
185
6d79cad2 186# PV
835f860c 187print OK6;
af6c647e 188
6d79cad2 189# PVN containing embedded \0s
835f860c 190$_ = OK7;
af6c647e 191s/.*\0//s;
192print;
193
6d79cad2 194# NV
af6c647e 195my $farthing = FARTHING;
196if ($farthing == 0.25) {
835f860c 197 print "ok 8\n";
af6c647e 198} else {
835f860c 199 print "not ok 8 # $farthing\n";
af6c647e 200}
201
6d79cad2 202# UV
af6c647e 203my $not_zero = NOT_ZERO;
204if ($not_zero > 0 && $not_zero == ~0) {
835f860c 205 print "ok 9\n";
af6c647e 206} else {
835f860c 207 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 208}
209
6d79cad2 210# Value includes a "*/" in an attempt to bust out of a C comment.
211# Also tests custom cpp #if clauses
212my $close = CLOSE;
213if ($close eq '*/') {
214 print "ok 10\n";
215} else {
216 print "not ok 10 # \$close='$close'\n";
217}
218
219# Default values if macro not defined.
220my $answer = ANSWER;
221if ($answer == 42) {
222 print "ok 11\n";
223} else {
224 print "not ok 11 # What do you get if you multiply six by nine? '$answer'\n";
225}
226
227# not defined macro
228my $notdef = eval { NOTDEF; };
229if (defined $notdef) {
230 print "not ok 12 # \$notdef='$notdef'\n";
231} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
232 print "not ok 12 # \$@='$@'\n";
233} else {
234 print "ok 12\n";
235}
236
237# not a macro
238my $notthere = eval { &ExtTest::NOTTHERE; };
239if (defined $notthere) {
240 print "not ok 13 # \$notthere='$notthere'\n";
241} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
242 chomp $@;
243 print "not ok 13 # \$@='$@'\n";
244} else {
245 print "ok 13\n";
246}
af6c647e 247
3414cef0 248# Truth
249my $yes = Yes;
250if ($yes) {
251 print "ok 14\n";
252} else {
253 print "not ok 14 # $yes='\$yes'\n";
254}
255
256# Falsehood
257my $no = No;
258if (defined $no and !$no) {
259 print "ok 15\n";
260} else {
261 print "not ok 15 # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
262}
263
264# Undef
265my $undef = Undef;
266unless (defined $undef) {
267 print "ok 16\n";
268} else {
269 print "not ok 16 # \$undef='$undef'\n";
270}
271
8ac27563 272
273# invalid macro (chosen to look like a mix up between No and SW)
274$notdef = eval { &ExtTest::So };
275if (defined $notdef) {
276 print "not ok 17 # \$notdef='$notdef'\n";
277} elsif ($@ !~ /^So is not a valid ExtTest macro/) {
278 print "not ok 17 # \$@='$@'\n";
279} else {
280 print "ok 17\n";
281}
282
283# invalid defined macro
284$notdef = eval { &ExtTest::EW };
285if (defined $notdef) {
286 print "not ok 18 # \$notdef='$notdef'\n";
287} elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
288 print "not ok 18 # \$@='$@'\n";
289} else {
290 print "ok 18\n";
291}
292
293my %compass = (
294EOT
295
296while (my ($point, $bearing) = each %compass) {
d7f97632 297 print FH "'$point' => $bearing, "
8ac27563 298}
299
300print FH <<'EOT';
301
302);
303
304my $fail;
305while (my ($point, $bearing) = each %compass) {
306 my $val = eval $point;
307 if ($@) {
308 print "# $point: \$@='$@'\n";
309 $fail = 1;
310 } elsif (!defined $bearing) {
311 print "# $point: \$val=undef\n";
312 $fail = 1;
313 } elsif ($val != $bearing) {
314 print "# $point: \$val=$val, not $bearing\n";
315 $fail = 1;
316 }
317}
318if ($fail) {
319 print "not ok 19\n";
320} else {
321 print "ok 19\n";
322}
323
af6c647e 324EOT
325
cea00dc5 326print FH <<"EOT";
327my \$rfc1149 = RFC1149;
328if (\$rfc1149 ne "$parent_rfc1149") {
329 print "not ok 20 # '\$rfc1149' ne '$parent_rfc1149'\n";
330} else {
331 print "ok 20\n";
332}
333
334if (\$rfc1149 != 1149) {
335 printf "not ok 21 # %d != 1149\n", \$rfc1149;
336} else {
337 print "ok 21\n";
338}
72f7b9a1 339
340EOT
341
342print FH <<'EOT';
343# test macro=>1
344my $open = OPEN;
345if ($open eq '/*') {
346 print "ok 22\n";
347} else {
348 print "not ok 22 # \$open='$open'\n";
349}
cea00dc5 350EOT
94b1a389 351close FH or die "close $testpl: $!\n";
af6c647e 352
835f860c 353################ Makefile.PL
6d79cad2 354# We really need a Makefile.PL because make test for a no dynamic linking perl
355# will run Makefile.PL again as part of the "make perl" target.
94b1a389 356my $makefilePL = catfile($dir, "Makefile.PL");
0ddb8edc 357push @files, "Makefile.PL";
94b1a389 358open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
835f860c 359print FH <<"EOT";
6d79cad2 360#!$perl -w
835f860c 361use ExtUtils::MakeMaker;
362WriteMakefile(
363 'NAME' => "$package",
364 'VERSION_FROM' => "$package.pm", # finds \$VERSION
365 (\$] >= 5.005 ?
366 (#ABSTRACT_FROM => "$package.pm", # XXX add this
367 AUTHOR => "$0") : ())
368 );
369EOT
370
94b1a389 371close FH or die "close $makefilePL: $!\n";
af6c647e 372
373chdir $dir or die $!; push @INC, '../../lib';
374END {chdir ".." or warn $!};
375
4bfb3f62 376my @perlout = `$runperl Makefile.PL PERL_CORE=1`;
835f860c 377if ($?) {
378 print "not ok 1 # $runperl Makefile.PL failed: $?\n";
379 print "# $_" foreach @perlout;
380 exit($?);
381} else {
382 print "ok 1\n";
383}
384
385
24874030 386my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
387my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
388if (-f "$makefile$makefile_ext") {
835f860c 389 print "ok 2\n";
af6c647e 390} else {
835f860c 391 print "not ok 2\n";
af6c647e 392}
24874030 393my $makefile_rename = ($^O eq 'VMS' ? '.mms' : '.old');
394push @files, "$makefile$makefile_rename"; # Renamed by make clean
af6c647e 395
396my $make = $Config{make};
94b1a389 397
af6c647e 398$make = $ENV{MAKE} if exists $ENV{MAKE};
94b1a389 399
91e5ac1f 400if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
76f26e35 401
535acd0f 402my @makeout;
94b1a389 403
af6c647e 404print "# make = '$make'\n";
535acd0f 405@makeout = `$make`;
94b1a389 406if ($?) {
835f860c 407 print "not ok 3 # $make failed: $?\n";
535acd0f 408 print "# $_" foreach @makeout;
94b1a389 409 exit($?);
af6c647e 410} else {
835f860c 411 print "ok 3\n";
412}
413
414if ($Config{usedl}) {
415 print "ok 4\n";
416} else {
835f860c 417 my $makeperl = "$make perl";
418 print "# make = '$makeperl'\n";
535acd0f 419 @makeout = `$makeperl`;
835f860c 420 if ($?) {
421 print "not ok 4 # $makeperl failed: $?\n";
535acd0f 422 print "# $_" foreach @makeout;
835f860c 423 exit($?);
424 } else {
425 print "ok 4\n";
426 }
af6c647e 427}
428
535acd0f 429push @files, $output;
430
0ddb8edc 431my $maketest = "$make test";
432print "# make = '$maketest'\n";
3414cef0 433
535acd0f 434@makeout = `$maketest`;
94b1a389 435
535acd0f 436if (open OUTPUT, "<$output") {
437 print while <OUTPUT>;
438 close OUTPUT or print "# Close $output failed: $!\n";
439} else {
440 # Harness will report missing test results at this point.
441 print "# Open <$output failed: $!\n";
442}
835f860c 443
535acd0f 444my $test = 23;
3414cef0 445
446if ($?) {
447 print "not ok $test # $maketest failed: $?\n";
535acd0f 448 print "# $_" foreach @makeout;
3414cef0 449} else {
6d79cad2 450 print "ok $test\n";
451}
452$test++;
453
454my $regen = `$runperl $package.xs`;
455if ($?) {
456 print "not ok $test # $runperl $package.xs failed: $?\n";
457} else {
458 print "ok $test\n";
af6c647e 459}
6d79cad2 460$test++;
461
462my $expect = $constant_types . $C_constant .
463 "\n#### XS Section:\n" . $XS_constant;
464
465if ($expect eq $regen) {
466 print "ok $test\n";
467} else {
468 print "not ok $test\n";
469 # open FOO, ">expect"; print FOO $expect;
470 # open FOO, ">regen"; print FOO $regen; close FOO;
471}
472$test++;
0ddb8edc 473
474my $makeclean = "$make clean";
475print "# make = '$makeclean'\n";
535acd0f 476@makeout = `$makeclean`;
0ddb8edc 477if ($?) {
6d79cad2 478 print "not ok $test # $make failed: $?\n";
535acd0f 479 print "# $_" foreach @makeout;
0ddb8edc 480} else {
6d79cad2 481 print "ok $test\n";
0ddb8edc 482}
6d79cad2 483$test++;
0ddb8edc 484
485foreach (@files) {
486 unlink $_ or warn "unlink $_: $!";
487}
488
489my $fail;
490opendir DIR, "." or die "opendir '.': $!";
491while (defined (my $entry = readdir DIR)) {
492 next if $entry =~ /^\.\.?$/;
493 print "# Extra file '$entry'\n";
494 $fail = 1;
495}
496closedir DIR or warn "closedir '.': $!";
497if ($fail) {
6d79cad2 498 print "not ok $test\n";
0ddb8edc 499} else {
6d79cad2 500 print "ok $test\n";
0ddb8edc 501}