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