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