ExtUtils::MakeMaker 6.55_02
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Constant.t
1 #!/usr/bin/perl -w
2
3 BEGIN {
4     if( $ENV{PERL_CORE} ) {
5         chdir 't' if -d 't';
6         @INC = '../lib';
7     }
8     use Config;
9     unless ($Config{usedl}) {
10         print "1..0 # no usedl, skipping\n";
11         exit 0;
12     }
13 }
14
15 # use warnings;
16 use strict;
17 use ExtUtils::MakeMaker;
18 use ExtUtils::Constant qw (C_constant autoload);
19 use File::Spec;
20 use Cwd;
21
22 my $do_utf_tests = $] > 5.006;
23 my $better_than_56 = $] > 5.007;
24 # For debugging set this to 1.
25 my $keep_files = 0;
26 $| = 1;
27
28 # Because were are going to be changing directory before running Makefile.PL
29 my $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
33 # perl)
34 $perl = File::Spec->rel2abs ($perl) unless $] < 5.006;
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;
40 my $lib = $ENV{PERL_CORE} ? '../../../lib' : '../../blib/lib';
41 my $runperl = "$perl \"-I$lib\"";
42 print "# perl=$perl\n";
43
44 my $make = $Config{make};
45 $make = $ENV{MAKE} if exists $ENV{MAKE};
46 if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
47
48 # VMS may be using something other than MMS/MMK
49 my $mms_or_mmk = 0;
50 my $vms_lc = 0;
51 my $vms_nodot = 0;
52 if ($^O eq 'VMS') {
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;
73 }
74
75 # Renamed by make clean
76 my $makefile = ($mms_or_mmk ? 'descrip' : 'Makefile');
77 my $makefile_ext = ($mms_or_mmk ? '.mms' : '');
78 my $makefile_rename = $makefile . ($mms_or_mmk ? '.mms_old' : '.old');
79
80 my $output = "output";
81 my $package = "ExtTest";
82 my $dir = "ext-$$";
83 my $subdir = 0;
84 # The real test counter.
85 my $realtest = 1;
86
87 my $orig_cwd = cwd;
88 my $updir = File::Spec->updir;
89 die "Can't get current directory: $!" unless defined $orig_cwd;
90
91 print "# $dir being created...\n";
92 mkdir $dir, 0777 or die "mkdir: $!\n";
93
94 END {
95   if (defined $orig_cwd and length $orig_cwd) {
96     chdir $orig_cwd or die "Can't chdir back to '$orig_cwd': $!";
97     use File::Path;
98     print "# $dir being removed...\n";
99     rmtree($dir) unless $keep_files;
100   } else {
101     # Can't get here.
102     die "cwd at start was empty, but directory '$dir' was created" if $dir;
103   }
104 }
105
106 chdir $dir or die $!;
107 push @INC, '../../lib', '../../../lib';
108
109 package TieOut;
110
111 sub TIEHANDLE {
112     my $class = shift;
113     bless(\( my $ref = ''), $class);
114 }
115
116 sub PRINT {
117     my $self = shift;
118     $$self .= join('', @_);
119 }
120
121 sub PRINTF {
122     my $self = shift;
123     $$self .= sprintf shift, @_;
124 }
125
126 sub read {
127     my $self = shift;
128     return substr($$self, 0, length($$self), '');
129 }
130
131 package main;
132
133 sub check_for_bonus_files {
134   my $dir = shift;
135   my %expect = map {($vms_lc ? lc($_) : $_), 1} @_;
136
137   my $fail;
138   opendir DIR, $dir or die "opendir '$dir': $!";
139   while (defined (my $entry = readdir DIR)) {
140     $entry =~ s/\.$// if $vms_nodot;  # delete trailing dot that indicates no extension
141     next if $expect{$entry};
142     print "# Extra file '$entry'\n";
143     $fail = 1;
144   }
145
146   closedir DIR or warn "closedir '.': $!";
147   if ($fail) {
148     print "not ok $realtest\n";
149   } else {
150     print "ok $realtest\n";
151   }
152   $realtest++;
153 }
154
155 sub 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++;
167
168   if (-f "$makefile$makefile_ext") {
169     print "ok $realtest\n";
170   } else {
171     print "not ok $realtest\n";
172   }
173   $realtest++;
174
175   my @makeout;
176
177   if ($^O eq 'VMS') { $make .= ' all'; }
178
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
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++;
216
217   if ($^O eq 'VMS') { $make =~ s{ all}{}; }
218
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++;
234
235   my $maketest = "$make test";
236   print "# make = '$maketest'\n";
237
238   @makeout = `$maketest`;
239
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   }
248
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
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++;
291   } else {
292     for (0..1) {
293       print "ok $realtest # skip no regen or expect for this set of tests\n";
294       $realtest++;
295     }
296   }
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
311   rename $makefile_rename, $makefile . $makefile_ext
312     or die "Can't rename '$makefile_rename' to '$makefile$makefile_ext': $!";
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
339 sub 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
348 use ExtUtils::MakeMaker;
349 WriteMakefile(
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              );
356 EOT
357
358   close FH or die "close $makefilePL: $!\n";
359   return $makefilePL;
360 }
361
362 sub 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;
372 }
373
374 sub write_and_run_extension {
375   my ($name, $items, $export_names, $package, $header, $testfile, $num_tests,
376       $wc_args) = @_;
377
378   my $c = tie *C, 'TieOut';
379   my $xs = tie *XS, 'TieOut';
380
381   ExtUtils::Constant::WriteConstants(C_FH => \*C,
382                                      XS_FH => \*XS,
383                                      NAME => $package,
384                                      NAMES => $items,
385                                      @$wc_args,
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
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;
401
402   print "# $name\n# $dir/$subdir being created...\n";
403   mkdir $subdir, 0777 or die "mkdir: $!\n";
404   chdir $subdir or die $!;
405
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
416   my $xs_name = "$package.xs";
417   push @files, $xs_name;
418   open FH, ">$xs_name" or die "open >$xs_name: $!\n";
419
420   print FH <<"EOT";
421 #include "EXTERN.h"
422 #include "perl.h"
423 #include "XSUB.h"
424 #include "$header_name"
425
426
427 $C_code
428 MODULE = $package               PACKAGE = $package
429 PROTOTYPES: ENABLE
430 $XS_code;
431 EOT
432
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";
441
442   print FH <<'EOT';
443
444 use strict;
445 EOT
446   printf FH "use warnings;\n" unless $] < 5.006;
447   print FH <<'EOT';
448 use Carp;
449
450 require Exporter;
451 require DynaLoader;
452 use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
453
454 $VERSION = '0.01';
455 @ISA = qw(Exporter DynaLoader);
456 EOT
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 $!;
475 use strict;
476 use $package qw(@$export_names);
477
478 print "1..2\n";
479 if (open OUTPUT, ">$output") {
480   print "ok 1\n";
481   select OUTPUT;
482 } else {
483   print "not ok 1 # Failed to open '$output': \$!\n";
484   exit 1;
485 }
486 EOT
487   print FH $testfile or die $!;
488   print FH <<"EOT" or die $!;
489 select STDOUT;
490 if (close OUTPUT) {
491   print "ok 2\n";
492 } else {
493   print "not ok 2 # Failed to close '$output': \$!\n";
494 }
495 EOT
496   close FH or die "close $testpl: $!\n";
497
498   push @files, Makefile_PL($package);
499   @files = MANIFEST (@files);
500
501   build_and_run ($num_tests, $expect, \@files);
502
503   chdir $updir or die "chdir '$updir': $!";
504   ++$subdir;
505 }
506
507 # Tests are arrayrefs of the form
508 # $name, [items], [export_names], $package, $header, $testfile, $num_tests
509 my @tests;
510 my $before_tests = 4; # Number of "ok"s emitted to build extension
511 my $after_tests = 8; # Number of "ok"s emitted after make test run
512 my $dummytest = 1;
513
514 my $here;
515 sub start_tests {
516   $dummytest += $before_tests;
517   $here = $dummytest;
518 }
519 sub end_tests {
520   my ($name, $items, $export_names, $header, $testfile, $args) = @_;
521   push @tests, [$name, $items, $export_names, $package, $header, $testfile,
522                $dummytest - $here, $args];
523   $dummytest += $after_tests;
524 }
525
526 my $pound;
527 if (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 }
532 my @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
539 my @args = undef;
540 push @args, [PROXYSUBS => 1] if $] > 5.009002;
541 foreach my $args (@args)
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"
565 EOT
566
567   while (my ($point, $bearing) = each %compass) {
568     $header .= "#define $point $bearing\n"
569   }
570
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); "
589                 . "SvIV_set(temp_sv, 1149);"},
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
601   my $test_body = <<"EOT";
602
603 my \$test = $dummytest;
604
605 EOT
606
607   $test_body .= <<'EOT';
608 # What follows goes to the temporary file.
609 # IV
610 my $five = FIVE;
611 if ($five == 5) {
612   print "ok $test\n";
613 } else {
614   print "not ok $test # \$five\n";
615 }
616 $test++;
617
618 # PV
619 if (OK6 eq "ok 6\n") {
620   print "ok $test\n";
621 } else {
622   print "not ok $test # \$five\n";
623 }
624 $test++;
625
626 # PVN containing embedded \0s
627 $_ = OK7;
628 s/.*\0//s;
629 s/7/$test/;
630 $test++;
631 print;
632
633 # NV
634 my $farthing = FARTHING;
635 if ($farthing == 0.25) {
636   print "ok $test\n";
637 } else {
638   print "not ok $test # $farthing\n";
639 }
640 $test++;
641
642 # UV
643 my $not_zero = NOT_ZERO;
644 if ($not_zero > 0 && $not_zero == ~0) {
645   print "ok $test\n";
646 } else {
647   print "not ok $test # \$not_zero=$not_zero ~0=" . (~0) . "\n";
648 }
649 $test++;
650
651 # Value includes a "*/" in an attempt to bust out of a C comment.
652 # Also tests custom cpp #if clauses
653 my $close = CLOSE;
654 if ($close eq '*/') {
655   print "ok $test\n";
656 } else {
657   print "not ok $test # \$close='$close'\n";
658 }
659 $test++;
660
661 # Default values if macro not defined.
662 my $answer = ANSWER;
663 if ($answer == 42) {
664   print "ok $test\n";
665 } else {
666   print "not ok $test # What do you get if you multiply six by nine? '$answer'\n";
667 }
668 $test++;
669
670 # not defined macro
671 my $notdef = eval { NOTDEF; };
672 if (defined $notdef) {
673   print "not ok $test # \$notdef='$notdef'\n";
674 } elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
675   print "not ok $test # \$@='$@'\n";
676 } else {
677   print "ok $test\n";
678 }
679 $test++;
680
681 # not a macro
682 my $notthere = eval { &ExtTest::NOTTHERE; };
683 if (defined $notthere) {
684   print "not ok $test # \$notthere='$notthere'\n";
685 } elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
686   chomp $@;
687   print "not ok $test # \$@='$@'\n";
688 } else {
689   print "ok $test\n";
690 }
691 $test++;
692
693 # Truth
694 my $yes = Yes;
695 if ($yes) {
696   print "ok $test\n";
697 } else {
698   print "not ok $test # $yes='\$yes'\n";
699 }
700 $test++;
701
702 # Falsehood
703 my $no = No;
704 if (defined $no and !$no) {
705   print "ok $test\n";
706 } else {
707   print "not ok $test # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
708 }
709 $test++;
710
711 # Undef
712 my $undef = Undef;
713 unless (defined $undef) {
714   print "ok $test\n";
715 } else {
716   print "not ok $test # \$undef='$undef'\n";
717 }
718 $test++;
719
720 # invalid macro (chosen to look like a mix up between No and SW)
721 $notdef = eval { &ExtTest::So };
722 if (defined $notdef) {
723   print "not ok $test # \$notdef='$notdef'\n";
724 } elsif ($@ !~ /^So is not a valid ExtTest macro/) {
725   print "not ok $test # \$@='$@'\n";
726 } else {
727   print "ok $test\n";
728 }
729 $test++;
730
731 # invalid defined macro
732 $notdef = eval { &ExtTest::EW };
733 if (defined $notdef) {
734   print "not ok $test # \$notdef='$notdef'\n";
735 } elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
736   print "not ok $test # \$@='$@'\n";
737 } else {
738   print "ok $test\n";
739 }
740 $test++;
741
742 my %compass = (
743 EOT
744
745 while (my ($point, $bearing) = each %compass) {
746   $test_body .= "'$point' => $bearing, "
747 }
748
749 $test_body .= <<'EOT';
750
751 );
752
753 my $fail;
754 while (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 }
767 if ($fail) {
768   print "not ok $test\n";
769 } else {
770   print "ok $test\n";
771 }
772 $test++;
773
774 EOT
775
776 $test_body .= <<"EOT";
777 my \$rfc1149 = RFC1149;
778 if (\$rfc1149 ne "$parent_rfc1149") {
779   print "not ok \$test # '\$rfc1149' ne '$parent_rfc1149'\n";
780 } else {
781   print "ok \$test\n";
782 }
783 \$test++;
784
785 if (\$rfc1149 != 1149) {
786   printf "not ok \$test # %d != 1149\n", \$rfc1149;
787 } else {
788   print "ok \$test\n";
789 }
790 \$test++;
791
792 EOT
793
794 $test_body .= <<'EOT';
795 # test macro=>1
796 my $open = OPEN;
797 if ($open eq '/*') {
798   print "ok $test\n";
799 } else {
800   print "not ok $test # \$open='$open'\n";
801 }
802 $test++;
803 EOT
804 $dummytest+=18;
805
806   end_tests("Simple tests", \@items, \@export_names, $header, $test_body,
807             $args);
808 }
809
810 if ($do_utf_tests) {
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
838 The above set of names seems to produce a suitably bad set of compile
839 problems on a Unicode naive version of ExtUtils::Constant (ie 0.11):
840
841 nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t
842 1..33
843 # perl=/stuff/perl5/15439-32-utf/perl
844 # ext-30370 being created...
845 Wide character in print at lib/ExtUtils/t/Constant.t line 140.
846 ok 1
847 ok 2
848 # make = 'make'
849 ExtTest.xs: In function `constant_1':
850 ExtTest.xs:80: warning: multi-character character constant
851 ExtTest.xs:80: warning: case value out of range
852 ok 3
853
854 =cut
855
856 # Grr `
857
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')
864
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';
869
870 use utf8;
871 my $better_than_56 = $] > 5.007;
872
873 my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}
874 EOT
875
876   $test_body .= join ",", @values;
877
878   $test_body .= << 'EOT';
879 ;
880
881 foreach (["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) = @$_;
891   (my $name = $string) =~ s/([^ !"#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])/sprintf '\x{%X}', ord $1/ges;
892   print "# \"$name\" => \'$expect\'\n";
893   # Try to force this to be bytes if possible.
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   }
902 EOT
903
904   $test_body .=  "my (\$error, \$got) = ${package}::constant (\$string);\n";
905
906   $test_body .= <<'EOT';
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";
914   if ($better_than_56) {
915     utf8::upgrade ($string);
916   } else {
917     $string = pack ('U*') . $string;
918   }
919 EOT
920
921   $test_body .=  "my (\$error, \$got) = ${package}::constant (\$string);\n";
922
923   $test_body .= <<'EOT';
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.
933     if ($better_than_56) {
934       utf8::encode ($string);
935     } else {
936       $string = pack 'C*', unpack 'C*', $string . pack "U*";
937     }
938 EOT
939
940     $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
941
942     $test_body .= <<'EOT';
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 }
958 EOT
959
960   end_tests("utf8 tests", \@items, [], "#define perl \"rules\"\n", $test_body);
961 }
962
963 # XXX I think that I should merge this into the utf8 test above.
964 sub 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;
970 EOT
971
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";
977   } else {
978     print "ok $dummytest\n";
979     }
980   }
981 EOT
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   }
990 EOT
991   }
992   $dummytest++;
993   return $test_body . <<'EOT';
994 }
995 EOT
996 }
997
998 # Simple tests to verify bits of the switch generation system work.
999 sub 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;
1010 if (\$value == $counter) {
1011   print "ok $dummytest\n";
1012 } else {
1013   print "not ok $dummytest # $thisname gave \$value\n";
1014 }
1015 EOT
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);
1024     }
1025   }
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);
1030 }
1031
1032 # Check that the memeq clauses work correctly when there isn't a switch
1033 # statement to bump off a character
1034 simple ("Singletons", "A", "AB", "ABC", "ABCD", "ABCDE");
1035 # Check the three code.
1036 simple ("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.
1039 simple ("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
1041 simple ("Three end and four symetry", qw(ean ear eat barb marm tart));
1042
1043
1044 # Need this if the single test below is rolled into @tests :
1045 # --$dummytest;
1046 print "1..$dummytest\n";
1047
1048 write_and_run_extension @$_ foreach @tests;
1049
1050 # This was causing an assertion failure (a C<confess>ion)
1051 # Any single byte > 128 should do it.
1052 C_constant ($package, undef, undef, undef, undef, undef, chr 255);
1053 print "ok $realtest\n"; $realtest++;
1054
1055 print STDERR "# You were running with \$keep_files set to $keep_files\n"
1056   if $keep_files;