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