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