ExtUtils::MakeMaker 6.55_02
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Constant.t
CommitLineData
9f0ea43f 1#!/usr/bin/perl -w
2
af6c647e 3BEGIN {
39234879 4 if( $ENV{PERL_CORE} ) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 }
568558b7 8 use Config;
9 unless ($Config{usedl}) {
10 print "1..0 # no usedl, skipping\n";
11 exit 0;
12 }
af6c647e 13}
14
d7f97632 15# use warnings;
af6c647e 16use strict;
17use ExtUtils::MakeMaker;
6b58ea4c 18use ExtUtils::Constant qw (C_constant autoload);
4f2c4fd8 19use File::Spec;
7783f9f6 20use Cwd;
4f2c4fd8 21
22my $do_utf_tests = $] > 5.006;
23my $better_than_56 = $] > 5.007;
7783f9f6 24# For debugging set this to 1.
25my $keep_files = 0;
26$| = 1;
4f2c4fd8 27
835f860c 28# Because were are going to be changing directory before running Makefile.PL
4f2c4fd8 29my $perl = $^X;
30# 5.005 doesn't have new enough File::Spec to have rel2abs. But actually we
31# only need it when $^X isn't absolute, which is going to be 5.8.0 or later
32# (where ExtUtils::Constant is in the core, and tests against the uninstalled
7783f9f6 33# perl)
4f2c4fd8 34$perl = File::Spec->rel2abs ($perl) unless $] < 5.006;
6d79cad2 35# ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
36# compare output to ensure that it is the same. We were probably run as ./perl
37# whereas we will run the child with the full path in $perl. So make $^X for
38# us the same as our child will see.
39$^X = $perl;
7783f9f6 40my $lib = $ENV{PERL_CORE} ? '../../../lib' : '../../blib/lib';
41my $runperl = "$perl \"-I$lib\"";
835f860c 42print "# perl=$perl\n";
4f2c4fd8 43
7783f9f6 44my $make = $Config{make};
45$make = $ENV{MAKE} if exists $ENV{MAKE};
46if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
94b1a389 47
4b257301 48# VMS may be using something other than MMS/MMK
49my $mms_or_mmk = 0;
6ef6dcad 50my $vms_lc = 0;
51my $vms_nodot = 0;
4b257301 52if ($^O eq 'VMS') {
6ef6dcad 53 $mms_or_mmk = 1 if (($make eq 'MMK') || ($make eq 'MMS'));
54 $vms_lc = 1;
55 $vms_nodot = 1;
56 my $vms_unix_rpt = 0;
57 my $vms_efs = 0;
58 my $vms_efs_case = 0;
59 if (eval 'require VMS::Feature') {
60 $vms_unix_rpt = VMS::Feature::current("filename_unix_report");
61 $vms_efs = VMS::Feature::current("efs_case_preserve");
62 $vms_efs_case = VMS::Feature::current("efs_charset");
63 } else {
64 my $unix_rpt = $ENV{'DECC$FILENAME_UNIX_REPORT'} || '';
65 my $efs_charset = $ENV{'DECC$EFS_CHARSET'} || '';
66 my $efs_case = $ENV{'DECC$EFS_CASE_PRESERVE'} || '';
67 $vms_unix_rpt = $unix_rpt =~ /^[ET1]/i;
68 $vms_efs = $efs_charset =~ /^[ET1]/i;
69 $vms_efs_case = $efs_case =~ /^[ET1]/i;
70 }
71 $vms_lc = 0 if $vms_efs_case;
72 $vms_nodot = 0 if $vms_unix_rpt;
4b257301 73}
74
7783f9f6 75# Renamed by make clean
4b257301 76my $makefile = ($mms_or_mmk ? 'descrip' : 'Makefile');
77my $makefile_ext = ($mms_or_mmk ? '.mms' : '');
78my $makefile_rename = $makefile . ($mms_or_mmk ? '.mms_old' : '.old');
af6c647e 79
7783f9f6 80my $output = "output";
81my $package = "ExtTest";
af6c647e 82my $dir = "ext-$$";
7783f9f6 83my $subdir = 0;
84# The real test counter.
85my $realtest = 1;
86
87my $orig_cwd = cwd;
88my $updir = File::Spec->updir;
89die "Can't get current directory: $!" unless defined $orig_cwd;
94b1a389 90
91print "# $dir being created...\n";
92mkdir $dir, 0777 or die "mkdir: $!\n";
93
af6c647e 94END {
7783f9f6 95 if (defined $orig_cwd and length $orig_cwd) {
96 chdir $orig_cwd or die "Can't chdir back to '$orig_cwd': $!";
94b1a389 97 use File::Path;
98 print "# $dir being removed...\n";
6557ab03 99 rmtree($dir) unless $keep_files;
7783f9f6 100 } else {
101 # Can't get here.
102 die "cwd at start was empty, but directory '$dir' was created" if $dir;
103 }
af6c647e 104}
105
7783f9f6 106chdir $dir or die $!;
107push @INC, '../../lib', '../../../lib';
6d79cad2 108
16be8eab 109package TieOut;
110
111sub TIEHANDLE {
112 my $class = shift;
113 bless(\( my $ref = ''), $class);
114}
115
116sub PRINT {
117 my $self = shift;
118 $$self .= join('', @_);
119}
120
121sub PRINTF {
122 my $self = shift;
123 $$self .= sprintf shift, @_;
124}
125
126sub read {
127 my $self = shift;
128 return substr($$self, 0, length($$self), '');
129}
130
131package main;
132
7783f9f6 133sub check_for_bonus_files {
134 my $dir = shift;
6ef6dcad 135 my %expect = map {($vms_lc ? lc($_) : $_), 1} @_;
8ac27563 136
7783f9f6 137 my $fail;
138 opendir DIR, $dir or die "opendir '$dir': $!";
139 while (defined (my $entry = readdir DIR)) {
6ef6dcad 140 $entry =~ s/\.$// if $vms_nodot; # delete trailing dot that indicates no extension
7783f9f6 141 next if $expect{$entry};
142 print "# Extra file '$entry'\n";
143 $fail = 1;
144 }
4f2c4fd8 145
7783f9f6 146 closedir DIR or warn "closedir '.': $!";
147 if ($fail) {
148 print "not ok $realtest\n";
4f2c4fd8 149 } else {
7783f9f6 150 print "ok $realtest\n";
4f2c4fd8 151 }
7783f9f6 152 $realtest++;
4f2c4fd8 153}
cea00dc5 154
7783f9f6 155sub build_and_run {
156 my ($tests, $expect, $files) = @_;
157 my $core = $ENV{PERL_CORE} ? ' PERL_CORE=1' : '';
158 my @perlout = `$runperl Makefile.PL $core`;
159 if ($?) {
160 print "not ok $realtest # $runperl Makefile.PL failed: $?\n";
161 print "# $_" foreach @perlout;
162 exit($?);
163 } else {
164 print "ok $realtest\n";
165 }
166 $realtest++;
af6c647e 167
7783f9f6 168 if (-f "$makefile$makefile_ext") {
169 print "ok $realtest\n";
170 } else {
171 print "not ok $realtest\n";
172 }
173 $realtest++;
8ac27563 174
7783f9f6 175 my @makeout;
af6c647e 176
7783f9f6 177 if ($^O eq 'VMS') { $make .= ' all'; }
6557ab03 178
558fa1e8 179 # Sometimes it seems that timestamps can get confused
180
181 # make failed: 256
182 # Makefile out-of-date with respect to Makefile.PL
183 # Cleaning current config before rebuilding Makefile...
184 # make -f Makefile.old clean > /dev/null 2>&1 || /bin/sh -c true
185 # ../../perl "-I../../../lib" "-I../../../lib" Makefile.PL "PERL_CORE=1"
186 # Checking if your kit is complete...
187 # Looks good
188 # Writing Makefile for ExtTest
189 # ==> Your Makefile has been rebuilt. <==
190 # ==> Please rerun the make command. <==
191 # false
192
193 my $timewarp = (-M "Makefile.PL") - (-M "$makefile$makefile_ext");
194 # Convert from days to seconds
195 $timewarp *= 86400;
196 print "# Makefile.PL is $timewarp second(s) older than $makefile$makefile_ext\n";
197 if ($timewarp < 0) {
198 # Sleep for a while to catch up.
199 $timewarp = -$timewarp;
200 $timewarp+=2;
201 $timewarp = 10 if $timewarp > 10;
202 print "# Sleeping for $timewarp second(s) to try to resolve this\n";
203 sleep $timewarp;
204 }
205
7783f9f6 206 print "# make = '$make'\n";
207 @makeout = `$make`;
208 if ($?) {
209 print "not ok $realtest # $make failed: $?\n";
210 print "# $_" foreach @makeout;
211 exit($?);
212 } else {
213 print "ok $realtest\n";
214 }
215 $realtest++;
4f2c4fd8 216
7783f9f6 217 if ($^O eq 'VMS') { $make =~ s{ all}{}; }
6557ab03 218
7783f9f6 219 if ($Config{usedl}) {
220 print "ok $realtest # This is dynamic linking, so no need to make perl\n";
221 } else {
222 my $makeperl = "$make perl";
223 print "# make = '$makeperl'\n";
224 @makeout = `$makeperl`;
225 if ($?) {
226 print "not ok $realtest # $makeperl failed: $?\n";
227 print "# $_" foreach @makeout;
228 exit($?);
229 } else {
230 print "ok $realtest\n";
231 }
232 }
233 $realtest++;
6557ab03 234
7783f9f6 235 my $maketest = "$make test";
236 print "# make = '$maketest'\n";
6557ab03 237
7783f9f6 238 @makeout = `$maketest`;
6557ab03 239
7783f9f6 240 if (open OUTPUT, "<$output") {
241 local $/; # Slurp it - faster.
242 print <OUTPUT>;
243 close OUTPUT or print "# Close $output failed: $!\n";
244 } else {
245 # Harness will report missing test results at this point.
246 print "# Open <$output failed: $!\n";
247 }
4f2c4fd8 248
7783f9f6 249 $realtest += $tests;
250 if ($?) {
251 print "not ok $realtest # $maketest failed: $?\n";
252 print "# $_" foreach @makeout;
253 } else {
254 print "ok $realtest - maketest\n";
255 }
256 $realtest++;
257
6b58ea4c 258 if (defined $expect) {
259 # -x is busted on Win32 < 5.6.1, so we emulate it.
260 my $regen;
261 if( $^O eq 'MSWin32' && $] <= 5.006001 ) {
262 open(REGENTMP, ">regentmp") or die $!;
263 open(XS, "$package.xs") or die $!;
264 my $saw_shebang;
265 while(<XS>) {
266 $saw_shebang++ if /^#!.*/i ;
267 print REGENTMP $_ if $saw_shebang;
268 }
269 close XS; close REGENTMP;
270 $regen = `$runperl regentmp`;
271 unlink 'regentmp';
272 }
273 else {
274 $regen = `$runperl -x $package.xs`;
275 }
276 if ($?) {
277 print "not ok $realtest # $runperl -x $package.xs failed: $?\n";
278 } else {
279 print "ok $realtest - regen\n";
280 }
281 $realtest++;
282
283 if ($expect eq $regen) {
284 print "ok $realtest - regen worked\n";
285 } else {
286 print "not ok $realtest - regen worked\n";
287 # open FOO, ">expect"; print FOO $expect;
288 # open FOO, ">regen"; print FOO $regen; close FOO;
289 }
290 $realtest++;
7783f9f6 291 } else {
6b58ea4c 292 for (0..1) {
293 print "ok $realtest # skip no regen or expect for this set of tests\n";
294 $realtest++;
295 }
7783f9f6 296 }
7783f9f6 297
298 my $makeclean = "$make clean";
299 print "# make = '$makeclean'\n";
300 @makeout = `$makeclean`;
301 if ($?) {
302 print "not ok $realtest # $make failed: $?\n";
303 print "# $_" foreach @makeout;
304 } else {
305 print "ok $realtest\n";
306 }
307 $realtest++;
308
309 check_for_bonus_files ('.', @$files, $output, $makefile_rename, '.', '..');
310
4b257301 311 rename $makefile_rename, $makefile . $makefile_ext
312 or die "Can't rename '$makefile_rename' to '$makefile$makefile_ext': $!";
7783f9f6 313
314 unlink $output or warn "Can't unlink '$output': $!";
315
316 # Need to make distclean to remove ../../lib/ExtTest.pm
317 my $makedistclean = "$make distclean";
318 print "# make = '$makedistclean'\n";
319 @makeout = `$makedistclean`;
320 if ($?) {
321 print "not ok $realtest # $make failed: $?\n";
322 print "# $_" foreach @makeout;
323 } else {
324 print "ok $realtest\n";
325 }
326 $realtest++;
327
328 check_for_bonus_files ('.', @$files, '.', '..');
329
330 unless ($keep_files) {
331 foreach (@$files) {
332 unlink $_ or warn "unlink $_: $!";
333 }
334 }
335
336 check_for_bonus_files ('.', '.', '..');
337}
338
339sub Makefile_PL {
340 my $package = shift;
341 ################ Makefile.PL
342 # We really need a Makefile.PL because make test for a no dynamic linking perl
343 # will run Makefile.PL again as part of the "make perl" target.
344 my $makefilePL = "Makefile.PL";
345 open FH, ">$makefilePL" or die "open >$makefilePL: $!\n";
346 print FH <<"EOT";
347#!$perl -w
348use ExtUtils::MakeMaker;
349WriteMakefile(
350 'NAME' => "$package",
351 'VERSION_FROM' => "$package.pm", # finds \$VERSION
352 (\$] >= 5.005 ?
353 (#ABSTRACT_FROM => "$package.pm", # XXX add this
354 AUTHOR => "$0") : ())
355 );
af6c647e 356EOT
8ac27563 357
7783f9f6 358 close FH or die "close $makefilePL: $!\n";
359 return $makefilePL;
360}
361
362sub MANIFEST {
363 my (@files) = @_;
364 ################ MANIFEST
365 # We really need a MANIFEST because make distclean checks it.
366 my $manifest = "MANIFEST";
367 push @files, $manifest;
368 open FH, ">$manifest" or die "open >$manifest: $!\n";
369 print FH "$_\n" foreach @files;
370 close FH or die "close $manifest: $!\n";
371 return @files;
8ac27563 372}
af6c647e 373
7783f9f6 374sub write_and_run_extension {
6b58ea4c 375 my ($name, $items, $export_names, $package, $header, $testfile, $num_tests,
376 $wc_args) = @_;
7783f9f6 377
16be8eab 378 my $c = tie *C, 'TieOut';
379 my $xs = tie *XS, 'TieOut';
380
6b58ea4c 381 ExtUtils::Constant::WriteConstants(C_FH => \*C,
16be8eab 382 XS_FH => \*XS,
383 NAME => $package,
384 NAMES => $items,
6b58ea4c 385 @$wc_args,
16be8eab 386 );
387
388 my $C_code = $c->read();
389 my $XS_code = $xs->read();
390
391 undef $c;
392 undef $xs;
393
394 untie *C;
395 untie *XS;
396
6b58ea4c 397 # Don't check the regeneration code if we specify extra arguments to
398 # WriteConstants. (Fix this to give finer grained control if needed)
399 my $expect;
400 $expect = $C_code . "\n#### XS Section:\n" . $XS_code unless $wc_args;
7783f9f6 401
402 print "# $name\n# $dir/$subdir being created...\n";
403 mkdir $subdir, 0777 or die "mkdir: $!\n";
404 chdir $subdir or die $!;
af6c647e 405
7783f9f6 406 my @files;
407
408 ################ Header
409 my $header_name = "test.h";
410 push @files, $header_name;
411 open FH, ">$header_name" or die "open >$header_name: $!\n";
412 print FH $header or die $!;
413 close FH or die "close $header_name: $!\n";
414
415 ################ XS
16be8eab 416 my $xs_name = "$package.xs";
417 push @files, $xs_name;
418 open FH, ">$xs_name" or die "open >$xs_name: $!\n";
7783f9f6 419
16be8eab 420 print FH <<"EOT";
af6c647e 421#include "EXTERN.h"
422#include "perl.h"
423#include "XSUB.h"
16be8eab 424#include "$header_name"
425
426
427$C_code
428MODULE = $package PACKAGE = $package
429PROTOTYPES: ENABLE
430$XS_code;
af6c647e 431EOT
432
7783f9f6 433 close FH or die "close $xs: $!\n";
434
435 ################ PM
436 my $pm = "$package.pm";
437 push @files, $pm;
438 open FH, ">$pm" or die "open >$pm: $!\n";
439 print FH "package $package;\n";
440 print FH "use $];\n";
af6c647e 441
7783f9f6 442 print FH <<'EOT';
af6c647e 443
444use strict;
d7f97632 445EOT
7783f9f6 446 printf FH "use warnings;\n" unless $] < 5.006;
447 print FH <<'EOT';
af6c647e 448use Carp;
449
450require Exporter;
451require DynaLoader;
d7f97632 452use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
af6c647e 453
454$VERSION = '0.01';
455@ISA = qw(Exporter DynaLoader);
af6c647e 456EOT
7783f9f6 457 # Having this qw( in the here doc confuses cperl mode far too much to be
458 # helpful. And I'm using cperl mode to edit this, even if you're not :-)
459 print FH "\@EXPORT_OK = qw(\n";
460
461 # Print the names of all our autoloaded constants
462 print FH "\t$_\n" foreach (@$export_names);
463 print FH ");\n";
464 # Print the AUTOLOAD subroutine ExtUtils::Constant generated for us
465 print FH autoload ($package, $]);
466 print FH "bootstrap $package \$VERSION;\n1;\n__END__\n";
467 close FH or die "close $pm: $!\n";
468
469 ################ test.pl
470 my $testpl = "test.pl";
471 push @files, $testpl;
472 open FH, ">$testpl" or die "open >$testpl: $!\n";
473 # Standard test header (need an option to suppress this?)
474 print FH <<"EOT" or die $!;
475use strict;
476use $package qw(@$export_names);
af6c647e 477
7783f9f6 478print "1..2\n";
535acd0f 479if (open OUTPUT, ">$output") {
480 print "ok 1\n";
481 select OUTPUT;
482} else {
7783f9f6 483 print "not ok 1 # Failed to open '$output': \$!\n";
535acd0f 484 exit 1;
485}
486EOT
7783f9f6 487 print FH $testfile or die $!;
488 print FH <<"EOT" or die $!;
489select STDOUT;
490if (close OUTPUT) {
491 print "ok 2\n";
492} else {
493 print "not ok 2 # Failed to close '$output': \$!\n";
494}
495EOT
496 close FH or die "close $testpl: $!\n";
af6c647e 497
7783f9f6 498 push @files, Makefile_PL($package);
499 @files = MANIFEST (@files);
535acd0f 500
7783f9f6 501 build_and_run ($num_tests, $expect, \@files);
502
503 chdir $updir or die "chdir '$updir': $!";
504 ++$subdir;
505}
16be8eab 506
7783f9f6 507# Tests are arrayrefs of the form
508# $name, [items], [export_names], $package, $header, $testfile, $num_tests
509my @tests;
510my $before_tests = 4; # Number of "ok"s emitted to build extension
511my $after_tests = 8; # Number of "ok"s emitted after make test run
512my $dummytest = 1;
513
514my $here;
515sub start_tests {
516 $dummytest += $before_tests;
517 $here = $dummytest;
518}
519sub end_tests {
6b58ea4c 520 my ($name, $items, $export_names, $header, $testfile, $args) = @_;
7783f9f6 521 push @tests, [$name, $items, $export_names, $package, $header, $testfile,
6b58ea4c 522 $dummytest - $here, $args];
7783f9f6 523 $dummytest += $after_tests;
524}
525
526my $pound;
527if (ord('A') == 193) { # EBCDIC platform
528 $pound = chr 177; # A pound sign. (Currency)
529} else { # ASCII platform
530 $pound = chr 163; # A pound sign. (Currency)
531}
532my @common_items = (
533 {name=>"perl", type=>"PV",},
534 {name=>"*/", type=>"PV", value=>'"CLOSE"', macro=>1},
535 {name=>"/*", type=>"PV", value=>'"OPEN"', macro=>1},
536 {name=>$pound, type=>"PV", value=>'"Sterling"', macro=>1},
537 );
538
6b58ea4c 539my @args = undef;
540push @args, [PROXYSUBS => 1] if $] > 5.009002;
541foreach my $args (@args)
7783f9f6 542{
543 # Simple tests
544 start_tests();
545 my $parent_rfc1149 =
546 'A Standard for the Transmission of IP Datagrams on Avian Carriers';
547 # Test the code that generates 1 and 2 letter name comparisons.
548 my %compass = (
549 N => 0, 'NE' => 45, E => 90, SE => 135,
550 S => 180, SW => 225, W => 270, NW => 315
551 );
552
553 my $header = << "EOT";
554#define FIVE 5
555#define OK6 "ok 6\\n"
556#define OK7 1
557#define FARTHING 0.25
558#define NOT_ZERO 1
559#define Yes 0
560#define No 1
561#define Undef 1
562#define RFC1149 "$parent_rfc1149"
563#undef NOTDEF
564#define perl "rules"
565EOT
566
567 while (my ($point, $bearing) = each %compass) {
568 $header .= "#define $point $bearing\n"
569 }
4f2c4fd8 570
7783f9f6 571 my @items = ("FIVE", {name=>"OK6", type=>"PV",},
572 {name=>"OK7", type=>"PVN",
573 value=>['"not ok 7\\n\\0ok 7\\n"', 15]},
574 {name => "FARTHING", type=>"NV"},
575 {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"},
576 {name => "OPEN", type=>"PV", value=>'"/*"', macro=>1},
577 {name => "CLOSE", type=>"PV", value=>'"*/"',
578 macro=>["#if 1\n", "#endif\n"]},
579 {name => "ANSWER", default=>["UV", 42]}, "NOTDEF",
580 {name => "Yes", type=>"YES"},
581 {name => "No", type=>"NO"},
582 {name => "Undef", type=>"UNDEF"},
583 # OK. It wasn't really designed to allow the creation of dual valued
584 # constants.
585 # It was more for INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE
586 {name=>"RFC1149", type=>"SV", value=>"sv_2mortal(temp_sv)",
587 pre=>"SV *temp_sv = newSVpv(RFC1149, 0); "
588 . "(void) SvUPGRADE(temp_sv,SVt_PVIV); SvIOK_on(temp_sv); "
a6f787ca 589 . "SvIV_set(temp_sv, 1149);"},
7783f9f6 590 );
591
592 push @items, $_ foreach keys %compass;
593
594 # Automatically compile the list of all the macro names, and make them
595 # exported constants.
596 my @export_names = map {(ref $_) ? $_->{name} : $_} @items;
597
598 # Exporter::Heavy (currently) isn't able to export the last 3 of these:
599 push @items, @common_items;
600
6b58ea4c 601 my $test_body = <<"EOT";
602
603my \$test = $dummytest;
604
605EOT
606
607 $test_body .= <<'EOT';
535acd0f 608# What follows goes to the temporary file.
6d79cad2 609# IV
835f860c 610my $five = FIVE;
611if ($five == 5) {
6b58ea4c 612 print "ok $test\n";
af6c647e 613} else {
6b58ea4c 614 print "not ok $test # \$five\n";
af6c647e 615}
6b58ea4c 616$test++;
af6c647e 617
6d79cad2 618# PV
6b58ea4c 619if (OK6 eq "ok 6\n") {
620 print "ok $test\n";
621} else {
622 print "not ok $test # \$five\n";
623}
624$test++;
af6c647e 625
6d79cad2 626# PVN containing embedded \0s
835f860c 627$_ = OK7;
af6c647e 628s/.*\0//s;
6b58ea4c 629s/7/$test/;
630$test++;
af6c647e 631print;
632
6d79cad2 633# NV
af6c647e 634my $farthing = FARTHING;
635if ($farthing == 0.25) {
6b58ea4c 636 print "ok $test\n";
af6c647e 637} else {
6b58ea4c 638 print "not ok $test # $farthing\n";
af6c647e 639}
6b58ea4c 640$test++;
af6c647e 641
6d79cad2 642# UV
af6c647e 643my $not_zero = NOT_ZERO;
644if ($not_zero > 0 && $not_zero == ~0) {
6b58ea4c 645 print "ok $test\n";
af6c647e 646} else {
6b58ea4c 647 print "not ok $test # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 648}
6b58ea4c 649$test++;
af6c647e 650
6d79cad2 651# Value includes a "*/" in an attempt to bust out of a C comment.
652# Also tests custom cpp #if clauses
653my $close = CLOSE;
654if ($close eq '*/') {
6b58ea4c 655 print "ok $test\n";
6d79cad2 656} else {
6b58ea4c 657 print "not ok $test # \$close='$close'\n";
6d79cad2 658}
6b58ea4c 659$test++;
6d79cad2 660
661# Default values if macro not defined.
662my $answer = ANSWER;
663if ($answer == 42) {
6b58ea4c 664 print "ok $test\n";
6d79cad2 665} else {
6b58ea4c 666 print "not ok $test # What do you get if you multiply six by nine? '$answer'\n";
6d79cad2 667}
6b58ea4c 668$test++;
6d79cad2 669
670# not defined macro
671my $notdef = eval { NOTDEF; };
672if (defined $notdef) {
6b58ea4c 673 print "not ok $test # \$notdef='$notdef'\n";
6d79cad2 674} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
6b58ea4c 675 print "not ok $test # \$@='$@'\n";
6d79cad2 676} else {
6b58ea4c 677 print "ok $test\n";
6d79cad2 678}
6b58ea4c 679$test++;
6d79cad2 680
681# not a macro
682my $notthere = eval { &ExtTest::NOTTHERE; };
683if (defined $notthere) {
6b58ea4c 684 print "not ok $test # \$notthere='$notthere'\n";
6d79cad2 685} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
686 chomp $@;
6b58ea4c 687 print "not ok $test # \$@='$@'\n";
6d79cad2 688} else {
6b58ea4c 689 print "ok $test\n";
6d79cad2 690}
6b58ea4c 691$test++;
af6c647e 692
3414cef0 693# Truth
694my $yes = Yes;
695if ($yes) {
6b58ea4c 696 print "ok $test\n";
3414cef0 697} else {
6b58ea4c 698 print "not ok $test # $yes='\$yes'\n";
3414cef0 699}
6b58ea4c 700$test++;
3414cef0 701
702# Falsehood
703my $no = No;
704if (defined $no and !$no) {
6b58ea4c 705 print "ok $test\n";
3414cef0 706} else {
6b58ea4c 707 print "not ok $test # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
3414cef0 708}
6b58ea4c 709$test++;
3414cef0 710
711# Undef
712my $undef = Undef;
713unless (defined $undef) {
6b58ea4c 714 print "ok $test\n";
3414cef0 715} else {
6b58ea4c 716 print "not ok $test # \$undef='$undef'\n";
3414cef0 717}
6b58ea4c 718$test++;
3414cef0 719
8ac27563 720# invalid macro (chosen to look like a mix up between No and SW)
721$notdef = eval { &ExtTest::So };
722if (defined $notdef) {
6b58ea4c 723 print "not ok $test # \$notdef='$notdef'\n";
8ac27563 724} elsif ($@ !~ /^So is not a valid ExtTest macro/) {
6b58ea4c 725 print "not ok $test # \$@='$@'\n";
8ac27563 726} else {
6b58ea4c 727 print "ok $test\n";
8ac27563 728}
6b58ea4c 729$test++;
8ac27563 730
731# invalid defined macro
732$notdef = eval { &ExtTest::EW };
733if (defined $notdef) {
6b58ea4c 734 print "not ok $test # \$notdef='$notdef'\n";
8ac27563 735} elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
6b58ea4c 736 print "not ok $test # \$@='$@'\n";
8ac27563 737} else {
6b58ea4c 738 print "ok $test\n";
8ac27563 739}
6b58ea4c 740$test++;
8ac27563 741
742my %compass = (
743EOT
744
745while (my ($point, $bearing) = each %compass) {
7783f9f6 746 $test_body .= "'$point' => $bearing, "
8ac27563 747}
748
7783f9f6 749$test_body .= <<'EOT';
8ac27563 750
751);
752
753my $fail;
754while (my ($point, $bearing) = each %compass) {
755 my $val = eval $point;
756 if ($@) {
757 print "# $point: \$@='$@'\n";
758 $fail = 1;
759 } elsif (!defined $bearing) {
760 print "# $point: \$val=undef\n";
761 $fail = 1;
762 } elsif ($val != $bearing) {
763 print "# $point: \$val=$val, not $bearing\n";
764 $fail = 1;
765 }
766}
767if ($fail) {
6b58ea4c 768 print "not ok $test\n";
8ac27563 769} else {
6b58ea4c 770 print "ok $test\n";
8ac27563 771}
6b58ea4c 772$test++;
8ac27563 773
af6c647e 774EOT
775
7783f9f6 776$test_body .= <<"EOT";
cea00dc5 777my \$rfc1149 = RFC1149;
778if (\$rfc1149 ne "$parent_rfc1149") {
6b58ea4c 779 print "not ok \$test # '\$rfc1149' ne '$parent_rfc1149'\n";
cea00dc5 780} else {
6b58ea4c 781 print "ok \$test\n";
cea00dc5 782}
6b58ea4c 783\$test++;
cea00dc5 784
785if (\$rfc1149 != 1149) {
6b58ea4c 786 printf "not ok \$test # %d != 1149\n", \$rfc1149;
cea00dc5 787} else {
6b58ea4c 788 print "ok \$test\n";
cea00dc5 789}
6b58ea4c 790\$test++;
72f7b9a1 791
792EOT
793
7783f9f6 794$test_body .= <<'EOT';
72f7b9a1 795# test macro=>1
796my $open = OPEN;
797if ($open eq '/*') {
6b58ea4c 798 print "ok $test\n";
72f7b9a1 799} else {
6b58ea4c 800 print "not ok $test # \$open='$open'\n";
72f7b9a1 801}
6b58ea4c 802$test++;
cea00dc5 803EOT
7783f9f6 804$dummytest+=18;
805
6b58ea4c 806 end_tests("Simple tests", \@items, \@export_names, $header, $test_body,
807 $args);
7783f9f6 808}
6557ab03 809
4f2c4fd8 810if ($do_utf_tests) {
7783f9f6 811 # utf8 tests
812 start_tests();
813 my ($inf, $pound_bytes, $pound_utf8);
814
815 $inf = chr 0x221E;
816 # Check that we can distiguish the pathological case of a string, and the
817 # utf8 representation of that string.
818 $pound_utf8 = $pound . '1';
819 if ($better_than_56) {
820 $pound_bytes = $pound_utf8;
821 utf8::encode ($pound_bytes);
822 } else {
823 # Must have that "U*" to generate a zero length UTF string that forces
824 # top bit set chars (such as the pound sign) into UTF8, so that the
825 # unpack 'C*' then gets the byte form of the UTF8.
826 $pound_bytes = pack 'C*', unpack 'C*', $pound_utf8 . pack "U*";
827 }
828
829 my @items = (@common_items,
830 {name=>$inf, type=>"PV", value=>'"Infinity"', macro=>1},
831 {name=>$pound_utf8, type=>"PV", value=>'"1 Pound"', macro=>1},
832 {name=>$pound_bytes, type=>"PV", value=>'"1 Pound (as bytes)"',
833 macro=>1},
834 );
835
836=pod
837
838The above set of names seems to produce a suitably bad set of compile
839problems on a Unicode naive version of ExtUtils::Constant (ie 0.11):
840
841nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t
8421..33
843# perl=/stuff/perl5/15439-32-utf/perl
844# ext-30370 being created...
845Wide character in print at lib/ExtUtils/t/Constant.t line 140.
846ok 1
847ok 2
848# make = 'make'
849ExtTest.xs: In function `constant_1':
850ExtTest.xs:80: warning: multi-character character constant
851ExtTest.xs:80: warning: case value out of range
852ok 3
853
854=cut
855
856# Grr `
857
4f2c4fd8 858 # Do this in 7 bit in case someone is testing with some settings that cause
859 # 8 bit files incapable of storing this character.
860 my @values
861 = map {"'" . join (",", unpack "U*", $_ . pack "U*") . "'"}
862 ($pound, $inf, $pound_bytes, $pound_utf8);
863 # Values is a list of strings, such as ('194,163,49', '163,49')
6557ab03 864
7783f9f6 865 my $test_body .= "my \$test = $dummytest;\n";
866 $dummytest += 7 * 3; # 3 tests for each of the 7 things:
867
868 $test_body .= << 'EOT';
6557ab03 869
7783f9f6 870use utf8;
871my $better_than_56 = $] > 5.007;
6557ab03 872
7783f9f6 873my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}
6557ab03 874EOT
875
7783f9f6 876 $test_body .= join ",", @values;
6557ab03 877
7783f9f6 878 $test_body .= << 'EOT';
6557ab03 879;
880
881foreach (["perl", "rules", "rules"],
882 ["/*", "OPEN", "OPEN"],
883 ["*/", "CLOSE", "CLOSE"],
884 [$pound, 'Sterling', []],
885 [$inf, 'Infinity', []],
886 [$pound_utf8, '1 Pound', '1 Pound (as bytes)'],
887 [$pound_bytes, '1 Pound (as bytes)', []],
888 ) {
889 # Flag an expected error with a reference for the expect string.
890 my ($string, $expect, $expect_bytes) = @$_;
bd64360a 891 (my $name = $string) =~ s/([^ !"#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])/sprintf '\x{%X}', ord $1/ges;
6557ab03 892 print "# \"$name\" => \'$expect\'\n";
893 # Try to force this to be bytes if possible.
4f2c4fd8 894 if ($better_than_56) {
895 utf8::downgrade ($string, 1);
896 } else {
897 if ($string =~ tr/0-\377// == length $string) {
898 # No chars outside range 0-255
899 $string = pack 'C*', unpack 'U*', ($string . pack 'U*');
900 }
901 }
6557ab03 902EOT
903
7783f9f6 904 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 905
7783f9f6 906 $test_body .= <<'EOT';
6557ab03 907 if ($error or $got ne $expect) {
908 print "not ok $test # error '$error', got '$got'\n";
909 } else {
910 print "ok $test\n";
911 }
912 $test++;
913 print "# Now upgrade '$name' to utf8\n";
4f2c4fd8 914 if ($better_than_56) {
915 utf8::upgrade ($string);
916 } else {
917 $string = pack ('U*') . $string;
918 }
6557ab03 919EOT
920
7783f9f6 921 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 922
7783f9f6 923 $test_body .= <<'EOT';
6557ab03 924 if ($error or $got ne $expect) {
925 print "not ok $test # error '$error', got '$got'\n";
926 } else {
927 print "ok $test\n";
928 }
929 $test++;
930 if (defined $expect_bytes) {
931 print "# And now with the utf8 byte sequence for name\n";
932 # Try the encoded bytes.
4f2c4fd8 933 if ($better_than_56) {
934 utf8::encode ($string);
935 } else {
936 $string = pack 'C*', unpack 'C*', $string . pack "U*";
937 }
6557ab03 938EOT
939
7783f9f6 940 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 941
7783f9f6 942 $test_body .= <<'EOT';
6557ab03 943 if (ref $expect_bytes) {
944 # Error expected.
945 if ($error) {
946 print "ok $test # error='$error' (as expected)\n";
947 } else {
948 print "not ok $test # expected error, got no error and '$got'\n";
949 }
950 } elsif ($got ne $expect_bytes) {
951 print "not ok $test # error '$error', expect '$expect_bytes', got '$got'\n";
952 } else {
953 print "ok $test\n";
954 }
955 $test++;
956 }
957}
958EOT
6557ab03 959
7783f9f6 960 end_tests("utf8 tests", \@items, [], "#define perl \"rules\"\n", $test_body);
835f860c 961}
962
7783f9f6 963# XXX I think that I should merge this into the utf8 test above.
964sub explict_call_constant {
965 my ($string, $expect) = @_;
966 # This does assume simple strings suitable for ''
967 my $test_body = <<"EOT";
968{
969 my (\$error, \$got) = ${package}::constant ('$string');\n;
970EOT
835f860c 971
7783f9f6 972 if (defined $expect) {
973 # No error expected
974 $test_body .= <<"EOT";
975 if (\$error or \$got ne "$expect") {
976 print "not ok $dummytest # error '\$error', expect '$expect', got '\$got'\n";
835f860c 977 } else {
7783f9f6 978 print "ok $dummytest\n";
979 }
835f860c 980 }
7783f9f6 981EOT
982 } else {
983 # Error expected.
984 $test_body .= <<"EOT";
985 if (\$error) {
986 print "ok $dummytest # error='\$error' (as expected)\n";
987 } else {
988 print "not ok $dummytest # expected error, got no error and '\$got'\n";
989 }
990EOT
991 }
992 $dummytest++;
993 return $test_body . <<'EOT';
af6c647e 994}
7783f9f6 995EOT
535acd0f 996}
835f860c 997
7783f9f6 998# Simple tests to verify bits of the switch generation system work.
999sub simple {
1000 start_tests();
1001 # Deliberately leave $name in @_, so that it is indexed from 1.
1002 my ($name, @items) = @_;
1003 my $test_header;
1004 my $test_body = "my \$value;\n";
1005 foreach my $counter (1 .. $#_) {
1006 my $thisname = $_[$counter];
1007 $test_header .= "#define $thisname $counter\n";
1008 $test_body .= <<"EOT";
1009\$value = $thisname;
1010if (\$value == $counter) {
1011 print "ok $dummytest\n";
3414cef0 1012} else {
7783f9f6 1013 print "not ok $dummytest # $thisname gave \$value\n";
6d79cad2 1014}
7783f9f6 1015EOT
1016 ++$dummytest;
1017 # Yes, the last time round the loop appends a z to the string.
1018 for my $i (0 .. length $thisname) {
1019 my $copyname = $thisname;
1020 substr ($copyname, $i, 1) = 'z';
1021 $test_body .= explict_call_constant ($copyname,
1022 $copyname eq $thisname
1023 ? $thisname : undef);
39234879 1024 }
6557ab03 1025 }
7783f9f6 1026 # Ho. This seems to be buggy in 5.005_03:
1027 # # Now remove $name from @_:
1028 # shift @_;
1029 end_tests($name, \@items, \@items, $test_header, $test_body);
0ddb8edc 1030}
ccc70a53 1031
7783f9f6 1032# Check that the memeq clauses work correctly when there isn't a switch
1033# statement to bump off a character
1034simple ("Singletons", "A", "AB", "ABC", "ABCD", "ABCDE");
1035# Check the three code.
1036simple ("Three start", qw(Bea kea Lea lea nea pea rea sea tea Wea yea Zea));
1037# There were 162 2 letter words in /usr/share/dict/words on FreeBSD 4.6, which
1038# I felt was rather too many. So I used words with 2 vowels.
1039simple ("Twos and three middle", qw(aa ae ai ea eu ie io oe era eta));
1040# Given the choice go for the end, else the earliest point
1041simple ("Three end and four symetry", qw(ean ear eat barb marm tart));
ccc70a53 1042
ccc70a53 1043
7783f9f6 1044# Need this if the single test below is rolled into @tests :
1045# --$dummytest;
1046print "1..$dummytest\n";
ccc70a53 1047
7783f9f6 1048write_and_run_extension @$_ foreach @tests;
4f2c4fd8 1049
1050# This was causing an assertion failure (a C<confess>ion)
7783f9f6 1051# Any single byte > 128 should do it.
4f2c4fd8 1052C_constant ($package, undef, undef, undef, undef, undef, chr 255);
7783f9f6 1053print "ok $realtest\n"; $realtest++;
4f2c4fd8 1054
7783f9f6 1055print STDERR "# You were running with \$keep_files set to $keep_files\n"
1056 if $keep_files;