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