6 if( $ENV{PERL_CORE} ) {
14 use ExtUtils::MakeMaker;
15 use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
19 my $do_utf_tests = $] > 5.006;
20 my $better_than_56 = $] > 5.007;
22 # Because were are going to be changing directory before running Makefile.PL
24 # 5.005 doesn't have new enough File::Spec to have rel2abs. But actually we
25 # only need it when $^X isn't absolute, which is going to be 5.8.0 or later
26 # (where ExtUtils::Constant is in the core, and tests against the uninstalled
28 $perl = File::Spec->rel2abs ($perl) unless $] < 5.006;
29 # ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
30 # compare output to ensure that it is the same. We were probably run as ./perl
31 # whereas we will run the child with the full path in $perl. So make $^X for
32 # us the same as our child will see.
35 print "# perl=$perl\n";
37 my $lib = $ENV{PERL_CORE} ? '../../lib' : '../blib/lib';
38 my $runperl = "$perl \"-I$lib\"";
45 print "# $dir being created...\n";
46 mkdir $dir, 0777 or die "mkdir: $!\n";
48 my $output = "output";
50 # For debugging set this to 1.
55 print "# $dir being removed...\n";
56 rmtree($dir) unless $keep_files;
59 my $package = "ExtTest";
61 # Test the code that generates 1 and 2 letter name comparisons.
63 N => 0, 'NE' => 45, E => 90, SE => 135, S => 180, SW => 225, W => 270, NW => 315
67 'A Standard for the Transmission of IP Datagrams on Avian Carriers';
68 # Check that 8 bit and unicode names don't cause problems.
70 if (ord('A') == 193) { # EBCDIC platform
71 $pound = chr 177; # A pound sign. (Currency)
72 } else { # ASCII platform
73 $pound = chr 163; # A pound sign. (Currency)
76 my ($inf, $pound_bytes, $pound_utf8);
79 # Check that we can distiguish the pathological case of a string, and the
80 # utf8 representation of that string.
81 $pound_utf8 = $pound . '1';
82 if ($better_than_56) {
83 $pound_bytes = $pound_utf8;
84 utf8::encode ($pound_bytes);
86 # Must have that "U*" to generate a zero length UTF string that forces
87 # top bit set chars (such as the pound sign) into UTF8, so that the
88 # unpack 'C*' then gets the byte form of the UTF8.
89 $pound_bytes = pack 'C*', unpack 'C*', $pound_utf8 . pack "U*";
93 my @names = ("FIVE", {name=>"OK6", type=>"PV",},
94 {name=>"OK7", type=>"PVN",
95 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
96 {name => "FARTHING", type=>"NV"},
97 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
98 {name => "OPEN", type=>"PV", value=>'"/*"', macro=>1},
99 {name => "CLOSE", type=>"PV", value=>'"*/"',
100 macro=>["#if 1\n", "#endif\n"]},
101 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
102 {name => "Yes", type=>"YES"},
103 {name => "No", type=>"NO"},
104 {name => "Undef", type=>"UNDEF"},
105 # OK. It wasn't really designed to allow the creation of dual valued constants.
106 # It was more for INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
107 {name=>"RFC1149", type=>"SV", value=>"sv_2mortal(temp_sv)",
108 pre=>"SV *temp_sv = newSVpv(RFC1149, 0); "
109 . "(void) SvUPGRADE(temp_sv,SVt_PVIV); SvIOK_on(temp_sv); "
110 . "SvIVX(temp_sv) = 1149;"},
111 {name=>"perl", type=>"PV",},
114 push @names, $_ foreach keys %compass;
116 # Automatically compile the list of all the macro names, and make them
117 # exported constants.
118 my @names_only = map {(ref $_) ? $_->{name} : $_} @names;
120 # Exporter::Heavy (currently) isn't able to export these names:
121 push @names, ({name=>"*/", type=>"PV", value=>'"CLOSE"', macro=>1},
122 {name=>"/*", type=>"PV", value=>'"OPEN"', macro=>1},
123 {name=>$pound, type=>"PV", value=>'"Sterling"', macro=>1},
127 push @names, ({name=>$inf, type=>"PV", value=>'"Infinity"', macro=>1},
128 {name=>$pound_utf8, type=>"PV", value=>'"1 Pound"', macro=>1},
129 {name=>$pound_bytes, type=>"PV", value=>'"1 Pound (as bytes)"',
136 The above set of names seems to produce a suitably bad set of compile
137 problems on a Unicode naive version of ExtUtils::Constant (ie 0.11):
139 nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t
141 # perl=/stuff/perl5/15439-32-utf/perl
142 # ext-30370 being created...
143 Wide character in print at lib/ExtUtils/t/Constant.t line 140.
147 ExtTest.xs: In function `constant_1':
148 ExtTest.xs:80: warning: multi-character character constant
149 ExtTest.xs:80: warning: case value out of range
157 my $constant_types = constant_types(); # macro defs
158 my $C_constant = join "\n",
159 C_constant ($package, undef, "IV", $types, undef, undef, @names);
160 my $XS_constant = XS_constant ($package, $types); # XS for ExtTest::constant
162 ################ Header
163 my $header = File::Spec->catdir($dir, "test.h");
164 push @files, "test.h";
165 open FH, ">$header" or die "open >$header: $!\n";
168 #define OK6 "ok 6\\n"
170 #define FARTHING 0.25
175 #define RFC1149 "$parent_rfc1149"
180 while (my ($point, $bearing) = each %compass) {
181 print FH "#define $point $bearing\n"
183 close FH or die "close $header: $!\n";
186 my $xs = File::Spec->catdir($dir, "$package.xs");
187 push @files, "$package.xs";
188 open FH, ">$xs" or die "open >$xs: $!\n";
196 print FH "#include \"test.h\"\n\n";
197 print FH $constant_types;
198 print FH $C_constant, "\n";
199 print FH "MODULE = $package PACKAGE = $package\n";
200 print FH "PROTOTYPES: ENABLE\n";
201 print FH $XS_constant;
202 close FH or die "close $xs: $!\n";
205 my $pm = File::Spec->catdir($dir, "$package.pm");
206 push @files, "$package.pm";
207 open FH, ">$pm" or die "open >$pm: $!\n";
208 print FH "package $package;\n";
209 print FH "use $];\n";
215 printf FH "use warnings;\n" unless $] < 5.006;
221 use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
224 @ISA = qw(Exporter DynaLoader);
228 # Print the names of all our autoloaded constants
229 print FH "\t$_\n" foreach (@names_only);
231 # Print the AUTOLOAD subroutine ExtUtils::Constant generated for us
232 print FH autoload ($package, $]);
233 print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
234 close FH or die "close $pm: $!\n";
236 ################ test.pl
237 my $testpl = File::Spec->catdir($dir, "test.pl");
238 push @files, "test.pl";
239 open FH, ">$testpl" or die "open >$testpl: $!\n";
241 print FH "use strict;\n";
242 print FH "use $package qw(@names_only);\n\n";
244 print FH "use utf8\n\n" if $do_utf_tests;
249 if (open OUTPUT, ">$output") {
253 print "not ok 1 # Failed to open '$output': $!\n";
260 my $better_than_56 = $] > 5.007;
262 # What follows goes to the temporary file.
268 print "not ok 5 # $five\n";
274 # PVN containing embedded \0s
280 my $farthing = FARTHING;
281 if ($farthing == 0.25) {
284 print "not ok 8 # $farthing\n";
288 my $not_zero = NOT_ZERO;
289 if ($not_zero > 0 && $not_zero == ~0) {
292 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
295 # Value includes a "*/" in an attempt to bust out of a C comment.
296 # Also tests custom cpp #if clauses
298 if ($close eq '*/') {
301 print "not ok 10 # \$close='$close'\n";
304 # Default values if macro not defined.
309 print "not ok 11 # What do you get if you multiply six by nine? '$answer'\n";
313 my $notdef = eval { NOTDEF; };
314 if (defined $notdef) {
315 print "not ok 12 # \$notdef='$notdef'\n";
316 } elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
317 print "not ok 12 # \$@='$@'\n";
323 my $notthere = eval { &ExtTest::NOTTHERE; };
324 if (defined $notthere) {
325 print "not ok 13 # \$notthere='$notthere'\n";
326 } elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
328 print "not ok 13 # \$@='$@'\n";
338 print "not ok 14 # $yes='\$yes'\n";
343 if (defined $no and !$no) {
346 print "not ok 15 # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
351 unless (defined $undef) {
354 print "not ok 16 # \$undef='$undef'\n";
358 # invalid macro (chosen to look like a mix up between No and SW)
359 $notdef = eval { &ExtTest::So };
360 if (defined $notdef) {
361 print "not ok 17 # \$notdef='$notdef'\n";
362 } elsif ($@ !~ /^So is not a valid ExtTest macro/) {
363 print "not ok 17 # \$@='$@'\n";
368 # invalid defined macro
369 $notdef = eval { &ExtTest::EW };
370 if (defined $notdef) {
371 print "not ok 18 # \$notdef='$notdef'\n";
372 } elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
373 print "not ok 18 # \$@='$@'\n";
381 while (my ($point, $bearing) = each %compass) {
382 print FH "'$point' => $bearing, "
390 while (my ($point, $bearing) = each %compass) {
391 my $val = eval $point;
393 print "# $point: \$@='$@'\n";
395 } elsif (!defined $bearing) {
396 print "# $point: \$val=undef\n";
398 } elsif ($val != $bearing) {
399 print "# $point: \$val=$val, not $bearing\n";
412 my \$rfc1149 = RFC1149;
413 if (\$rfc1149 ne "$parent_rfc1149") {
414 print "not ok 20 # '\$rfc1149' ne '$parent_rfc1149'\n";
419 if (\$rfc1149 != 1149) {
420 printf "not ok 21 # %d != 1149\n", \$rfc1149;
433 print "not ok 22 # \$open='$open'\n";
438 # Do this in 7 bit in case someone is testing with some settings that cause
439 # 8 bit files incapable of storing this character.
441 = map {"'" . join (",", unpack "U*", $_ . pack "U*") . "'"}
442 ($pound, $inf, $pound_bytes, $pound_utf8);
443 # Values is a list of strings, such as ('194,163,49', '163,49')
447 # I can see that this child test program might be about to use parts of
451 my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}
454 print FH join ",", @values;
459 foreach (["perl", "rules", "rules"],
460 ["/*", "OPEN", "OPEN"],
461 ["*/", "CLOSE", "CLOSE"],
462 [$pound, 'Sterling', []],
463 [$inf, 'Infinity', []],
464 [$pound_utf8, '1 Pound', '1 Pound (as bytes)'],
465 [$pound_bytes, '1 Pound (as bytes)', []],
467 # Flag an expected error with a reference for the expect string.
468 my ($string, $expect, $expect_bytes) = @$_;
469 (my $name = $string) =~ s/([^ -~])/sprintf '\x{%X}', ord $1/ges;
470 print "# \"$name\" => \'$expect\'\n";
471 # Try to force this to be bytes if possible.
472 if ($better_than_56) {
473 utf8::downgrade ($string, 1);
475 if ($string =~ tr/0-\377// == length $string) {
476 # No chars outside range 0-255
477 $string = pack 'C*', unpack 'U*', ($string . pack 'U*');
482 print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
485 if ($error or $got ne $expect) {
486 print "not ok $test # error '$error', got '$got'\n";
491 print "# Now upgrade '$name' to utf8\n";
492 if ($better_than_56) {
493 utf8::upgrade ($string);
495 $string = pack ('U*') . $string;
499 print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
502 if ($error or $got ne $expect) {
503 print "not ok $test # error '$error', got '$got'\n";
508 if (defined $expect_bytes) {
509 print "# And now with the utf8 byte sequence for name\n";
510 # Try the encoded bytes.
511 if ($better_than_56) {
512 utf8::encode ($string);
514 $string = pack 'C*', unpack 'C*', $string . pack "U*";
518 print FH "my (\$error, \$got) = ${package}::constant (\$string);\n";
521 if (ref $expect_bytes) {
524 print "ok $test # error='$error' (as expected)\n";
526 print "not ok $test # expected error, got no error and '$got'\n";
528 } elsif ($got ne $expect_bytes) {
529 print "not ok $test # error '$error', expect '$expect_bytes', got '$got'\n";
540 print "ok $_ # Skipped on non Unicode perl\n" foreach 23..43;
544 close FH or die "close $testpl: $!\n";
546 # This is where the test numbers carry on after the test number above are
550 ################ Makefile.PL
551 # We really need a Makefile.PL because make test for a no dynamic linking perl
552 # will run Makefile.PL again as part of the "make perl" target.
553 my $makefilePL = File::Spec->catdir($dir, "Makefile.PL");
554 push @files, "Makefile.PL";
555 open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
558 use ExtUtils::MakeMaker;
560 'NAME' => "$package",
561 'VERSION_FROM' => "$package.pm", # finds \$VERSION
563 (#ABSTRACT_FROM => "$package.pm", # XXX add this
564 AUTHOR => "$0") : ())
568 close FH or die "close $makefilePL: $!\n";
570 ################ MANIFEST
571 # We really need a MANIFEST because make distclean checks it.
572 my $manifest = File::Spec->catdir($dir, "MANIFEST");
573 push @files, "MANIFEST";
574 open FH, ">$manifest" or die "open >$manifest: $!\n";
575 print FH "$_\n" foreach @files;
576 close FH or die "close $manifest: $!\n";
578 chdir $dir or die $!; push @INC, '../../lib';
579 END {chdir ".." or warn $!};
581 my $core = $ENV{PERL_CORE} ? ' PERL_CORE=1' : '';
582 my @perlout = `$runperl Makefile.PL $core`;
584 print "not ok 1 # $runperl Makefile.PL failed: $?\n";
585 print "# $_" foreach @perlout;
592 my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
593 my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
594 if (-f "$makefile$makefile_ext") {
600 # Renamed by make clean
601 my $makefile_rename = $makefile . ($^O eq 'VMS' ? '.mms' : '.old');
603 my $make = $Config{make};
605 $make = $ENV{MAKE} if exists $ENV{MAKE};
607 if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
611 if ($^O eq 'VMS') { $make .= ' all'; }
612 print "# make = '$make'\n";
615 print "not ok 3 # $make failed: $?\n";
616 print "# $_" foreach @makeout;
622 if ($^O eq 'VMS') { $make =~ s{ all}{}; }
624 if ($Config{usedl}) {
627 my $makeperl = "$make perl";
628 print "# make = '$makeperl'\n";
629 @makeout = `$makeperl`;
631 print "not ok 4 # $makeperl failed: $?\n";
632 print "# $_" foreach @makeout;
639 my $maketest = "$make test";
640 print "# make = '$maketest'\n";
642 @makeout = `$maketest`;
644 if (open OUTPUT, "<$output") {
645 print while <OUTPUT>;
646 close OUTPUT or print "# Close $output failed: $!\n";
648 # Harness will report missing test results at this point.
649 print "# Open <$output failed: $!\n";
653 print "not ok $test # $maketest failed: $?\n";
654 print "# $_" foreach @makeout;
656 print "ok $test - maketest\n";
661 # -x is busted on Win32 < 5.6.1, so we emulate it.
663 if( $^O eq 'MSWin32' && $] <= 5.006001 ) {
664 open(REGENTMP, ">regentmp") or die $!;
665 open(XS, "$package.xs") or die $!;
668 $saw_shebang++ if /^#!.*/i ;
669 print REGENTMP $_ if $saw_shebang;
671 close XS; close REGENTMP;
672 $regen = `$runperl regentmp`;
676 $regen = `$runperl -x $package.xs`;
679 print "not ok $test # $runperl -x $package.xs failed: $?\n";
681 print "ok $test - regen\n";
685 my $expect = $constant_types . $C_constant .
686 "\n#### XS Section:\n" . $XS_constant;
688 if ($expect eq $regen) {
689 print "ok $test - regen worked\n";
691 print "not ok $test - regen worked\n";
692 # open FOO, ">expect"; print FOO $expect;
693 # open FOO, ">regen"; print FOO $regen; close FOO;
697 my $makeclean = "$make clean";
698 print "# make = '$makeclean'\n";
699 @makeout = `$makeclean`;
701 print "not ok $test # $make failed: $?\n";
702 print "# $_" foreach @makeout;
708 sub check_for_bonus_files {
710 my %expect = map {($^O eq 'VMS' ? lc($_) : $_), 1} @_;
713 opendir DIR, $dir or die "opendir '$dir': $!";
714 while (defined (my $entry = readdir DIR)) {
715 $entry =~ s/\.$// if $^O eq 'VMS'; # delete trailing dot that indicates no extension
716 next if $expect{$entry};
717 print "# Extra file '$entry'\n";
721 closedir DIR or warn "closedir '.': $!";
723 print "not ok $test\n";
730 check_for_bonus_files ('.', @files, $output, $makefile_rename, '.', '..');
732 rename $makefile_rename, $makefile
733 or die "Can't rename '$makefile_rename' to '$makefile': $!";
735 unlink $output or warn "Can't unlink '$output': $!";
737 # Need to make distclean to remove ../../lib/ExtTest.pm
738 my $makedistclean = "$make distclean";
739 print "# make = '$makedistclean'\n";
740 @makeout = `$makedistclean`;
742 print "not ok $test # $make failed: $?\n";
743 print "# $_" foreach @makeout;
749 check_for_bonus_files ('.', @files, '.', '..');
751 unless ($keep_files) {
753 unlink $_ or warn "unlink $_: $!";
757 check_for_bonus_files ('.', '.', '..');
759 # This was causing an assertion failure (a C<confess>ion)
760 C_constant ($package, undef, undef, undef, undef, undef, chr 255);
762 print "ok $test\n"; $test++;