Update to MakeMaker 6.30
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / t / Constant.t
CommitLineData
9f0ea43f 1#!/usr/bin/perl -w
2
af6c647e 3BEGIN {
39234879 4 if( $ENV{PERL_CORE} ) {
5 chdir 't' if -d 't';
6 @INC = '../lib';
7 }
568558b7 8 use Config;
9 unless ($Config{usedl}) {
10 print "1..0 # no usedl, skipping\n";
11 exit 0;
12 }
af6c647e 13}
14
d7f97632 15# use warnings;
af6c647e 16use strict;
17use ExtUtils::MakeMaker;
18use ExtUtils::Constant qw (constant_types C_constant XS_constant autoload);
4f2c4fd8 19use File::Spec;
7783f9f6 20use Cwd;
4f2c4fd8 21
22my $do_utf_tests = $] > 5.006;
23my $better_than_56 = $] > 5.007;
7783f9f6 24# For debugging set this to 1.
25my $keep_files = 0;
26$| = 1;
4f2c4fd8 27
835f860c 28# Because were are going to be changing directory before running Makefile.PL
4f2c4fd8 29my $perl = $^X;
30# 5.005 doesn't have new enough File::Spec to have rel2abs. But actually we
31# only need it when $^X isn't absolute, which is going to be 5.8.0 or later
32# (where ExtUtils::Constant is in the core, and tests against the uninstalled
7783f9f6 33# perl)
4f2c4fd8 34$perl = File::Spec->rel2abs ($perl) unless $] < 5.006;
6d79cad2 35# ExtUtils::Constant::C_constant uses $^X inside a comment, and we want to
36# compare output to ensure that it is the same. We were probably run as ./perl
37# whereas we will run the child with the full path in $perl. So make $^X for
38# us the same as our child will see.
39$^X = $perl;
7783f9f6 40my $lib = $ENV{PERL_CORE} ? '../../../lib' : '../../blib/lib';
41my $runperl = "$perl \"-I$lib\"";
835f860c 42print "# perl=$perl\n";
4f2c4fd8 43
7783f9f6 44my $make = $Config{make};
45$make = $ENV{MAKE} if exists $ENV{MAKE};
46if ($^O eq 'MSWin32' && $make eq 'nmake') { $make .= " -nologo"; }
94b1a389 47
7783f9f6 48# Renamed by make clean
49my $makefile = ($^O eq 'VMS' ? 'descrip' : 'Makefile');
50my $makefile_ext = ($^O eq 'VMS' ? '.mms' : '');
51my $makefile_rename = $makefile . ($^O eq 'VMS' ? '.mms' : '.old');
af6c647e 52
7783f9f6 53my $output = "output";
54my $package = "ExtTest";
af6c647e 55my $dir = "ext-$$";
7783f9f6 56my $subdir = 0;
57# The real test counter.
58my $realtest = 1;
59
60my $orig_cwd = cwd;
61my $updir = File::Spec->updir;
62die "Can't get current directory: $!" unless defined $orig_cwd;
94b1a389 63
64print "# $dir being created...\n";
65mkdir $dir, 0777 or die "mkdir: $!\n";
66
af6c647e 67END {
7783f9f6 68 if (defined $orig_cwd and length $orig_cwd) {
69 chdir $orig_cwd or die "Can't chdir back to '$orig_cwd': $!";
94b1a389 70 use File::Path;
71 print "# $dir being removed...\n";
6557ab03 72 rmtree($dir) unless $keep_files;
7783f9f6 73 } else {
74 # Can't get here.
75 die "cwd at start was empty, but directory '$dir' was created" if $dir;
76 }
af6c647e 77}
78
7783f9f6 79chdir $dir or die $!;
80push @INC, '../../lib', '../../../lib';
6d79cad2 81
7783f9f6 82sub check_for_bonus_files {
83 my $dir = shift;
84 my %expect = map {($^O eq 'VMS' ? lc($_) : $_), 1} @_;
8ac27563 85
7783f9f6 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 }
4f2c4fd8 94
7783f9f6 95 closedir DIR or warn "closedir '.': $!";
96 if ($fail) {
97 print "not ok $realtest\n";
4f2c4fd8 98 } else {
7783f9f6 99 print "ok $realtest\n";
4f2c4fd8 100 }
7783f9f6 101 $realtest++;
4f2c4fd8 102}
cea00dc5 103
7783f9f6 104sub 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++;
af6c647e 116
7783f9f6 117 if (-f "$makefile$makefile_ext") {
118 print "ok $realtest\n";
119 } else {
120 print "not ok $realtest\n";
121 }
122 $realtest++;
8ac27563 123
7783f9f6 124 my @makeout;
af6c647e 125
7783f9f6 126 if ($^O eq 'VMS') { $make .= ' all'; }
6557ab03 127
558fa1e8 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
7783f9f6 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++;
4f2c4fd8 165
7783f9f6 166 if ($^O eq 'VMS') { $make =~ s{ all}{}; }
6557ab03 167
7783f9f6 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++;
6557ab03 183
7783f9f6 184 my $maketest = "$make test";
185 print "# make = '$maketest'\n";
6557ab03 186
7783f9f6 187 @makeout = `$maketest`;
6557ab03 188
7783f9f6 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 }
4f2c4fd8 197
7783f9f6 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
281sub 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
290use ExtUtils::MakeMaker;
291WriteMakefile(
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 );
af6c647e 298EOT
8ac27563 299
7783f9f6 300 close FH or die "close $makefilePL: $!\n";
301 return $makefilePL;
302}
303
304sub 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;
8ac27563 314}
af6c647e 315
7783f9f6 316sub 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 $!;
af6c647e 331
7783f9f6 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';
af6c647e 347#include "EXTERN.h"
348#include "perl.h"
349#include "XSUB.h"
350EOT
351
7783f9f6 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";
af6c647e 367
7783f9f6 368 print FH <<'EOT';
af6c647e 369
370use strict;
d7f97632 371EOT
7783f9f6 372 printf FH "use warnings;\n" unless $] < 5.006;
373 print FH <<'EOT';
af6c647e 374use Carp;
375
376require Exporter;
377require DynaLoader;
d7f97632 378use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
af6c647e 379
380$VERSION = '0.01';
381@ISA = qw(Exporter DynaLoader);
af6c647e 382EOT
7783f9f6 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 $!;
401use strict;
402use $package qw(@$export_names);
af6c647e 403
7783f9f6 404print "1..2\n";
535acd0f 405if (open OUTPUT, ">$output") {
406 print "ok 1\n";
407 select OUTPUT;
408} else {
7783f9f6 409 print "not ok 1 # Failed to open '$output': \$!\n";
535acd0f 410 exit 1;
411}
412EOT
7783f9f6 413 print FH $testfile or die $!;
414 print FH <<"EOT" or die $!;
415select STDOUT;
416if (close OUTPUT) {
417 print "ok 2\n";
418} else {
419 print "not ok 2 # Failed to close '$output': \$!\n";
420}
421EOT
422 close FH or die "close $testpl: $!\n";
af6c647e 423
7783f9f6 424 push @files, Makefile_PL($package);
425 @files = MANIFEST (@files);
535acd0f 426
7783f9f6 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
434my @tests;
435my $before_tests = 4; # Number of "ok"s emitted to build extension
436my $after_tests = 8; # Number of "ok"s emitted after make test run
437my $dummytest = 1;
438
439my $here;
440sub start_tests {
441 $dummytest += $before_tests;
442 $here = $dummytest;
443}
444sub 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
451my $pound;
452if (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}
457my @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"
487EOT
488
489 while (my ($point, $bearing) = each %compass) {
490 $header .= "#define $point $bearing\n"
491 }
4f2c4fd8 492
7783f9f6 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); "
a6f787ca 511 . "SvIV_set(temp_sv, 1149);"},
7783f9f6 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';
535acd0f 525# What follows goes to the temporary file.
6d79cad2 526# IV
835f860c 527my $five = FIVE;
528if ($five == 5) {
529 print "ok 5\n";
af6c647e 530} else {
7783f9f6 531 print "not ok 5 # \$five\n";
af6c647e 532}
533
6d79cad2 534# PV
835f860c 535print OK6;
af6c647e 536
6d79cad2 537# PVN containing embedded \0s
835f860c 538$_ = OK7;
af6c647e 539s/.*\0//s;
540print;
541
6d79cad2 542# NV
af6c647e 543my $farthing = FARTHING;
544if ($farthing == 0.25) {
835f860c 545 print "ok 8\n";
af6c647e 546} else {
835f860c 547 print "not ok 8 # $farthing\n";
af6c647e 548}
549
6d79cad2 550# UV
af6c647e 551my $not_zero = NOT_ZERO;
552if ($not_zero > 0 && $not_zero == ~0) {
835f860c 553 print "ok 9\n";
af6c647e 554} else {
835f860c 555 print "not ok 9 # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 556}
557
6d79cad2 558# Value includes a "*/" in an attempt to bust out of a C comment.
559# Also tests custom cpp #if clauses
560my $close = CLOSE;
561if ($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.
568my $answer = ANSWER;
569if ($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
576my $notdef = eval { NOTDEF; };
577if (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
586my $notthere = eval { &ExtTest::NOTTHERE; };
587if (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}
af6c647e 595
3414cef0 596# Truth
597my $yes = Yes;
598if ($yes) {
599 print "ok 14\n";
600} else {
601 print "not ok 14 # $yes='\$yes'\n";
602}
603
604# Falsehood
605my $no = No;
606if (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
613my $undef = Undef;
614unless (defined $undef) {
615 print "ok 16\n";
616} else {
617 print "not ok 16 # \$undef='$undef'\n";
618}
619
8ac27563 620# invalid macro (chosen to look like a mix up between No and SW)
621$notdef = eval { &ExtTest::So };
622if (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 };
632if (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
640my %compass = (
641EOT
642
643while (my ($point, $bearing) = each %compass) {
7783f9f6 644 $test_body .= "'$point' => $bearing, "
8ac27563 645}
646
7783f9f6 647$test_body .= <<'EOT';
8ac27563 648
649);
650
651my $fail;
652while (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}
665if ($fail) {
666 print "not ok 19\n";
667} else {
668 print "ok 19\n";
669}
670
af6c647e 671EOT
672
7783f9f6 673$test_body .= <<"EOT";
cea00dc5 674my \$rfc1149 = RFC1149;
675if (\$rfc1149 ne "$parent_rfc1149") {
676 print "not ok 20 # '\$rfc1149' ne '$parent_rfc1149'\n";
677} else {
678 print "ok 20\n";
679}
680
681if (\$rfc1149 != 1149) {
682 printf "not ok 21 # %d != 1149\n", \$rfc1149;
683} else {
684 print "ok 21\n";
685}
72f7b9a1 686
687EOT
688
7783f9f6 689$test_body .= <<'EOT';
72f7b9a1 690# test macro=>1
691my $open = OPEN;
692if ($open eq '/*') {
693 print "ok 22\n";
694} else {
695 print "not ok 22 # \$open='$open'\n";
696}
cea00dc5 697EOT
7783f9f6 698$dummytest+=18;
699
700 end_tests("Simple tests", \@items, \@export_names, $header, $test_body);
701}
6557ab03 702
4f2c4fd8 703if ($do_utf_tests) {
7783f9f6 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
731The above set of names seems to produce a suitably bad set of compile
732problems on a Unicode naive version of ExtUtils::Constant (ie 0.11):
733
734nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t
7351..33
736# perl=/stuff/perl5/15439-32-utf/perl
737# ext-30370 being created...
738Wide character in print at lib/ExtUtils/t/Constant.t line 140.
739ok 1
740ok 2
741# make = 'make'
742ExtTest.xs: In function `constant_1':
743ExtTest.xs:80: warning: multi-character character constant
744ExtTest.xs:80: warning: case value out of range
745ok 3
746
747=cut
748
749# Grr `
750
4f2c4fd8 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')
6557ab03 757
7783f9f6 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';
6557ab03 762
7783f9f6 763use utf8;
764my $better_than_56 = $] > 5.007;
6557ab03 765
7783f9f6 766my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}
6557ab03 767EOT
768
7783f9f6 769 $test_body .= join ",", @values;
6557ab03 770
7783f9f6 771 $test_body .= << 'EOT';
6557ab03 772;
773
774foreach (["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.
4f2c4fd8 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 }
6557ab03 795EOT
796
7783f9f6 797 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 798
7783f9f6 799 $test_body .= <<'EOT';
6557ab03 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";
4f2c4fd8 807 if ($better_than_56) {
808 utf8::upgrade ($string);
809 } else {
810 $string = pack ('U*') . $string;
811 }
6557ab03 812EOT
813
7783f9f6 814 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 815
7783f9f6 816 $test_body .= <<'EOT';
6557ab03 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.
4f2c4fd8 826 if ($better_than_56) {
827 utf8::encode ($string);
828 } else {
829 $string = pack 'C*', unpack 'C*', $string . pack "U*";
830 }
6557ab03 831EOT
832
7783f9f6 833 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 834
7783f9f6 835 $test_body .= <<'EOT';
6557ab03 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}
851EOT
6557ab03 852
7783f9f6 853 end_tests("utf8 tests", \@items, [], "#define perl \"rules\"\n", $test_body);
835f860c 854}
855
7783f9f6 856# XXX I think that I should merge this into the utf8 test above.
857sub 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;
863EOT
835f860c 864
7783f9f6 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";
835f860c 870 } else {
7783f9f6 871 print "ok $dummytest\n";
872 }
835f860c 873 }
7783f9f6 874EOT
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 }
883EOT
884 }
885 $dummytest++;
886 return $test_body . <<'EOT';
af6c647e 887}
7783f9f6 888EOT
535acd0f 889}
835f860c 890
7783f9f6 891# Simple tests to verify bits of the switch generation system work.
892sub 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;
903if (\$value == $counter) {
904 print "ok $dummytest\n";
3414cef0 905} else {
7783f9f6 906 print "not ok $dummytest # $thisname gave \$value\n";
6d79cad2 907}
7783f9f6 908EOT
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);
39234879 917 }
6557ab03 918 }
7783f9f6 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);
0ddb8edc 923}
ccc70a53 924
7783f9f6 925# Check that the memeq clauses work correctly when there isn't a switch
926# statement to bump off a character
927simple ("Singletons", "A", "AB", "ABC", "ABCD", "ABCDE");
928# Check the three code.
929simple ("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.
932simple ("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
934simple ("Three end and four symetry", qw(ean ear eat barb marm tart));
ccc70a53 935
ccc70a53 936
7783f9f6 937# Need this if the single test below is rolled into @tests :
938# --$dummytest;
939print "1..$dummytest\n";
ccc70a53 940
7783f9f6 941write_and_run_extension @$_ foreach @tests;
4f2c4fd8 942
943# This was causing an assertion failure (a C<confess>ion)
7783f9f6 944# Any single byte > 128 should do it.
4f2c4fd8 945C_constant ($package, undef, undef, undef, undef, undef, chr 255);
7783f9f6 946print "ok $realtest\n"; $realtest++;
4f2c4fd8 947
7783f9f6 948print STDERR "# You were running with \$keep_files set to $keep_files\n"
949 if $keep_files;