hv_fetchs() support
[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;
6b58ea4c 18use ExtUtils::Constant qw (C_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
4b257301 48# VMS may be using something other than MMS/MMK
49my $mms_or_mmk = 0;
50if ($^O eq 'VMS') {
51 $mms_or_mmk = 1 if (($make eq 'MMK') || ($make eq 'MMS'));
52}
53
7783f9f6 54# Renamed by make clean
4b257301 55my $makefile = ($mms_or_mmk ? 'descrip' : 'Makefile');
56my $makefile_ext = ($mms_or_mmk ? '.mms' : '');
57my $makefile_rename = $makefile . ($mms_or_mmk ? '.mms_old' : '.old');
af6c647e 58
7783f9f6 59my $output = "output";
60my $package = "ExtTest";
af6c647e 61my $dir = "ext-$$";
7783f9f6 62my $subdir = 0;
63# The real test counter.
64my $realtest = 1;
65
66my $orig_cwd = cwd;
67my $updir = File::Spec->updir;
68die "Can't get current directory: $!" unless defined $orig_cwd;
94b1a389 69
70print "# $dir being created...\n";
71mkdir $dir, 0777 or die "mkdir: $!\n";
72
af6c647e 73END {
7783f9f6 74 if (defined $orig_cwd and length $orig_cwd) {
75 chdir $orig_cwd or die "Can't chdir back to '$orig_cwd': $!";
94b1a389 76 use File::Path;
77 print "# $dir being removed...\n";
6557ab03 78 rmtree($dir) unless $keep_files;
7783f9f6 79 } else {
80 # Can't get here.
81 die "cwd at start was empty, but directory '$dir' was created" if $dir;
82 }
af6c647e 83}
84
7783f9f6 85chdir $dir or die $!;
86push @INC, '../../lib', '../../../lib';
6d79cad2 87
16be8eab 88package TieOut;
89
90sub TIEHANDLE {
91 my $class = shift;
92 bless(\( my $ref = ''), $class);
93}
94
95sub PRINT {
96 my $self = shift;
97 $$self .= join('', @_);
98}
99
100sub PRINTF {
101 my $self = shift;
102 $$self .= sprintf shift, @_;
103}
104
105sub read {
106 my $self = shift;
107 return substr($$self, 0, length($$self), '');
108}
109
110package main;
111
7783f9f6 112sub check_for_bonus_files {
113 my $dir = shift;
114 my %expect = map {($^O eq 'VMS' ? lc($_) : $_), 1} @_;
8ac27563 115
7783f9f6 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 }
4f2c4fd8 124
7783f9f6 125 closedir DIR or warn "closedir '.': $!";
126 if ($fail) {
127 print "not ok $realtest\n";
4f2c4fd8 128 } else {
7783f9f6 129 print "ok $realtest\n";
4f2c4fd8 130 }
7783f9f6 131 $realtest++;
4f2c4fd8 132}
cea00dc5 133
7783f9f6 134sub 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++;
af6c647e 146
7783f9f6 147 if (-f "$makefile$makefile_ext") {
148 print "ok $realtest\n";
149 } else {
150 print "not ok $realtest\n";
151 }
152 $realtest++;
8ac27563 153
7783f9f6 154 my @makeout;
af6c647e 155
7783f9f6 156 if ($^O eq 'VMS') { $make .= ' all'; }
6557ab03 157
558fa1e8 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
7783f9f6 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++;
4f2c4fd8 195
7783f9f6 196 if ($^O eq 'VMS') { $make =~ s{ all}{}; }
6557ab03 197
7783f9f6 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++;
6557ab03 213
7783f9f6 214 my $maketest = "$make test";
215 print "# make = '$maketest'\n";
6557ab03 216
7783f9f6 217 @makeout = `$maketest`;
6557ab03 218
7783f9f6 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 }
4f2c4fd8 227
7783f9f6 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
6b58ea4c 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++;
7783f9f6 270 } else {
6b58ea4c 271 for (0..1) {
272 print "ok $realtest # skip no regen or expect for this set of tests\n";
273 $realtest++;
274 }
7783f9f6 275 }
7783f9f6 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
4b257301 290 rename $makefile_rename, $makefile . $makefile_ext
291 or die "Can't rename '$makefile_rename' to '$makefile$makefile_ext': $!";
7783f9f6 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
318sub 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
327use ExtUtils::MakeMaker;
328WriteMakefile(
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 );
af6c647e 335EOT
8ac27563 336
7783f9f6 337 close FH or die "close $makefilePL: $!\n";
338 return $makefilePL;
339}
340
341sub 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;
8ac27563 351}
af6c647e 352
7783f9f6 353sub write_and_run_extension {
6b58ea4c 354 my ($name, $items, $export_names, $package, $header, $testfile, $num_tests,
355 $wc_args) = @_;
7783f9f6 356
16be8eab 357 my $c = tie *C, 'TieOut';
358 my $xs = tie *XS, 'TieOut';
359
6b58ea4c 360 ExtUtils::Constant::WriteConstants(C_FH => \*C,
16be8eab 361 XS_FH => \*XS,
362 NAME => $package,
363 NAMES => $items,
6b58ea4c 364 @$wc_args,
16be8eab 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
6b58ea4c 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;
7783f9f6 380
381 print "# $name\n# $dir/$subdir being created...\n";
382 mkdir $subdir, 0777 or die "mkdir: $!\n";
383 chdir $subdir or die $!;
af6c647e 384
7783f9f6 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
16be8eab 395 my $xs_name = "$package.xs";
396 push @files, $xs_name;
397 open FH, ">$xs_name" or die "open >$xs_name: $!\n";
7783f9f6 398
16be8eab 399 print FH <<"EOT";
af6c647e 400#include "EXTERN.h"
401#include "perl.h"
402#include "XSUB.h"
16be8eab 403#include "$header_name"
404
405
406$C_code
407MODULE = $package PACKAGE = $package
408PROTOTYPES: ENABLE
409$XS_code;
af6c647e 410EOT
411
7783f9f6 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";
af6c647e 420
7783f9f6 421 print FH <<'EOT';
af6c647e 422
423use strict;
d7f97632 424EOT
7783f9f6 425 printf FH "use warnings;\n" unless $] < 5.006;
426 print FH <<'EOT';
af6c647e 427use Carp;
428
429require Exporter;
430require DynaLoader;
d7f97632 431use vars qw ($VERSION @ISA @EXPORT_OK $AUTOLOAD);
af6c647e 432
433$VERSION = '0.01';
434@ISA = qw(Exporter DynaLoader);
af6c647e 435EOT
7783f9f6 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 $!;
454use strict;
455use $package qw(@$export_names);
af6c647e 456
7783f9f6 457print "1..2\n";
535acd0f 458if (open OUTPUT, ">$output") {
459 print "ok 1\n";
460 select OUTPUT;
461} else {
7783f9f6 462 print "not ok 1 # Failed to open '$output': \$!\n";
535acd0f 463 exit 1;
464}
465EOT
7783f9f6 466 print FH $testfile or die $!;
467 print FH <<"EOT" or die $!;
468select STDOUT;
469if (close OUTPUT) {
470 print "ok 2\n";
471} else {
472 print "not ok 2 # Failed to close '$output': \$!\n";
473}
474EOT
475 close FH or die "close $testpl: $!\n";
af6c647e 476
7783f9f6 477 push @files, Makefile_PL($package);
478 @files = MANIFEST (@files);
535acd0f 479
7783f9f6 480 build_and_run ($num_tests, $expect, \@files);
481
482 chdir $updir or die "chdir '$updir': $!";
483 ++$subdir;
484}
16be8eab 485
7783f9f6 486# Tests are arrayrefs of the form
487# $name, [items], [export_names], $package, $header, $testfile, $num_tests
488my @tests;
489my $before_tests = 4; # Number of "ok"s emitted to build extension
490my $after_tests = 8; # Number of "ok"s emitted after make test run
491my $dummytest = 1;
492
493my $here;
494sub start_tests {
495 $dummytest += $before_tests;
496 $here = $dummytest;
497}
498sub end_tests {
6b58ea4c 499 my ($name, $items, $export_names, $header, $testfile, $args) = @_;
7783f9f6 500 push @tests, [$name, $items, $export_names, $package, $header, $testfile,
6b58ea4c 501 $dummytest - $here, $args];
7783f9f6 502 $dummytest += $after_tests;
503}
504
505my $pound;
506if (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}
511my @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
6b58ea4c 518my @args = undef;
519push @args, [PROXYSUBS => 1] if $] > 5.009002;
520foreach my $args (@args)
7783f9f6 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"
544EOT
545
546 while (my ($point, $bearing) = each %compass) {
547 $header .= "#define $point $bearing\n"
548 }
4f2c4fd8 549
7783f9f6 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); "
a6f787ca 568 . "SvIV_set(temp_sv, 1149);"},
7783f9f6 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
6b58ea4c 580 my $test_body = <<"EOT";
581
582my \$test = $dummytest;
583
584EOT
585
586 $test_body .= <<'EOT';
535acd0f 587# What follows goes to the temporary file.
6d79cad2 588# IV
835f860c 589my $five = FIVE;
590if ($five == 5) {
6b58ea4c 591 print "ok $test\n";
af6c647e 592} else {
6b58ea4c 593 print "not ok $test # \$five\n";
af6c647e 594}
6b58ea4c 595$test++;
af6c647e 596
6d79cad2 597# PV
6b58ea4c 598if (OK6 eq "ok 6\n") {
599 print "ok $test\n";
600} else {
601 print "not ok $test # \$five\n";
602}
603$test++;
af6c647e 604
6d79cad2 605# PVN containing embedded \0s
835f860c 606$_ = OK7;
af6c647e 607s/.*\0//s;
6b58ea4c 608s/7/$test/;
609$test++;
af6c647e 610print;
611
6d79cad2 612# NV
af6c647e 613my $farthing = FARTHING;
614if ($farthing == 0.25) {
6b58ea4c 615 print "ok $test\n";
af6c647e 616} else {
6b58ea4c 617 print "not ok $test # $farthing\n";
af6c647e 618}
6b58ea4c 619$test++;
af6c647e 620
6d79cad2 621# UV
af6c647e 622my $not_zero = NOT_ZERO;
623if ($not_zero > 0 && $not_zero == ~0) {
6b58ea4c 624 print "ok $test\n";
af6c647e 625} else {
6b58ea4c 626 print "not ok $test # \$not_zero=$not_zero ~0=" . (~0) . "\n";
af6c647e 627}
6b58ea4c 628$test++;
af6c647e 629
6d79cad2 630# Value includes a "*/" in an attempt to bust out of a C comment.
631# Also tests custom cpp #if clauses
632my $close = CLOSE;
633if ($close eq '*/') {
6b58ea4c 634 print "ok $test\n";
6d79cad2 635} else {
6b58ea4c 636 print "not ok $test # \$close='$close'\n";
6d79cad2 637}
6b58ea4c 638$test++;
6d79cad2 639
640# Default values if macro not defined.
641my $answer = ANSWER;
642if ($answer == 42) {
6b58ea4c 643 print "ok $test\n";
6d79cad2 644} else {
6b58ea4c 645 print "not ok $test # What do you get if you multiply six by nine? '$answer'\n";
6d79cad2 646}
6b58ea4c 647$test++;
6d79cad2 648
649# not defined macro
650my $notdef = eval { NOTDEF; };
651if (defined $notdef) {
6b58ea4c 652 print "not ok $test # \$notdef='$notdef'\n";
6d79cad2 653} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) {
6b58ea4c 654 print "not ok $test # \$@='$@'\n";
6d79cad2 655} else {
6b58ea4c 656 print "ok $test\n";
6d79cad2 657}
6b58ea4c 658$test++;
6d79cad2 659
660# not a macro
661my $notthere = eval { &ExtTest::NOTTHERE; };
662if (defined $notthere) {
6b58ea4c 663 print "not ok $test # \$notthere='$notthere'\n";
6d79cad2 664} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) {
665 chomp $@;
6b58ea4c 666 print "not ok $test # \$@='$@'\n";
6d79cad2 667} else {
6b58ea4c 668 print "ok $test\n";
6d79cad2 669}
6b58ea4c 670$test++;
af6c647e 671
3414cef0 672# Truth
673my $yes = Yes;
674if ($yes) {
6b58ea4c 675 print "ok $test\n";
3414cef0 676} else {
6b58ea4c 677 print "not ok $test # $yes='\$yes'\n";
3414cef0 678}
6b58ea4c 679$test++;
3414cef0 680
681# Falsehood
682my $no = No;
683if (defined $no and !$no) {
6b58ea4c 684 print "ok $test\n";
3414cef0 685} else {
6b58ea4c 686 print "not ok $test # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";
3414cef0 687}
6b58ea4c 688$test++;
3414cef0 689
690# Undef
691my $undef = Undef;
692unless (defined $undef) {
6b58ea4c 693 print "ok $test\n";
3414cef0 694} else {
6b58ea4c 695 print "not ok $test # \$undef='$undef'\n";
3414cef0 696}
6b58ea4c 697$test++;
3414cef0 698
8ac27563 699# invalid macro (chosen to look like a mix up between No and SW)
700$notdef = eval { &ExtTest::So };
701if (defined $notdef) {
6b58ea4c 702 print "not ok $test # \$notdef='$notdef'\n";
8ac27563 703} elsif ($@ !~ /^So is not a valid ExtTest macro/) {
6b58ea4c 704 print "not ok $test # \$@='$@'\n";
8ac27563 705} else {
6b58ea4c 706 print "ok $test\n";
8ac27563 707}
6b58ea4c 708$test++;
8ac27563 709
710# invalid defined macro
711$notdef = eval { &ExtTest::EW };
712if (defined $notdef) {
6b58ea4c 713 print "not ok $test # \$notdef='$notdef'\n";
8ac27563 714} elsif ($@ !~ /^EW is not a valid ExtTest macro/) {
6b58ea4c 715 print "not ok $test # \$@='$@'\n";
8ac27563 716} else {
6b58ea4c 717 print "ok $test\n";
8ac27563 718}
6b58ea4c 719$test++;
8ac27563 720
721my %compass = (
722EOT
723
724while (my ($point, $bearing) = each %compass) {
7783f9f6 725 $test_body .= "'$point' => $bearing, "
8ac27563 726}
727
7783f9f6 728$test_body .= <<'EOT';
8ac27563 729
730);
731
732my $fail;
733while (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}
746if ($fail) {
6b58ea4c 747 print "not ok $test\n";
8ac27563 748} else {
6b58ea4c 749 print "ok $test\n";
8ac27563 750}
6b58ea4c 751$test++;
8ac27563 752
af6c647e 753EOT
754
7783f9f6 755$test_body .= <<"EOT";
cea00dc5 756my \$rfc1149 = RFC1149;
757if (\$rfc1149 ne "$parent_rfc1149") {
6b58ea4c 758 print "not ok \$test # '\$rfc1149' ne '$parent_rfc1149'\n";
cea00dc5 759} else {
6b58ea4c 760 print "ok \$test\n";
cea00dc5 761}
6b58ea4c 762\$test++;
cea00dc5 763
764if (\$rfc1149 != 1149) {
6b58ea4c 765 printf "not ok \$test # %d != 1149\n", \$rfc1149;
cea00dc5 766} else {
6b58ea4c 767 print "ok \$test\n";
cea00dc5 768}
6b58ea4c 769\$test++;
72f7b9a1 770
771EOT
772
7783f9f6 773$test_body .= <<'EOT';
72f7b9a1 774# test macro=>1
775my $open = OPEN;
776if ($open eq '/*') {
6b58ea4c 777 print "ok $test\n";
72f7b9a1 778} else {
6b58ea4c 779 print "not ok $test # \$open='$open'\n";
72f7b9a1 780}
6b58ea4c 781$test++;
cea00dc5 782EOT
7783f9f6 783$dummytest+=18;
784
6b58ea4c 785 end_tests("Simple tests", \@items, \@export_names, $header, $test_body,
786 $args);
7783f9f6 787}
6557ab03 788
4f2c4fd8 789if ($do_utf_tests) {
7783f9f6 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
817The above set of names seems to produce a suitably bad set of compile
818problems on a Unicode naive version of ExtUtils::Constant (ie 0.11):
819
820nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t
8211..33
822# perl=/stuff/perl5/15439-32-utf/perl
823# ext-30370 being created...
824Wide character in print at lib/ExtUtils/t/Constant.t line 140.
825ok 1
826ok 2
827# make = 'make'
828ExtTest.xs: In function `constant_1':
829ExtTest.xs:80: warning: multi-character character constant
830ExtTest.xs:80: warning: case value out of range
831ok 3
832
833=cut
834
835# Grr `
836
4f2c4fd8 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')
6557ab03 843
7783f9f6 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';
6557ab03 848
7783f9f6 849use utf8;
850my $better_than_56 = $] > 5.007;
6557ab03 851
7783f9f6 852my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}
6557ab03 853EOT
854
7783f9f6 855 $test_body .= join ",", @values;
6557ab03 856
7783f9f6 857 $test_body .= << 'EOT';
6557ab03 858;
859
860foreach (["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.
4f2c4fd8 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 }
6557ab03 881EOT
882
7783f9f6 883 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 884
7783f9f6 885 $test_body .= <<'EOT';
6557ab03 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";
4f2c4fd8 893 if ($better_than_56) {
894 utf8::upgrade ($string);
895 } else {
896 $string = pack ('U*') . $string;
897 }
6557ab03 898EOT
899
7783f9f6 900 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 901
7783f9f6 902 $test_body .= <<'EOT';
6557ab03 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.
4f2c4fd8 912 if ($better_than_56) {
913 utf8::encode ($string);
914 } else {
915 $string = pack 'C*', unpack 'C*', $string . pack "U*";
916 }
6557ab03 917EOT
918
7783f9f6 919 $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n";
6557ab03 920
7783f9f6 921 $test_body .= <<'EOT';
6557ab03 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}
937EOT
6557ab03 938
7783f9f6 939 end_tests("utf8 tests", \@items, [], "#define perl \"rules\"\n", $test_body);
835f860c 940}
941
7783f9f6 942# XXX I think that I should merge this into the utf8 test above.
943sub 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;
949EOT
835f860c 950
7783f9f6 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";
835f860c 956 } else {
7783f9f6 957 print "ok $dummytest\n";
958 }
835f860c 959 }
7783f9f6 960EOT
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 }
969EOT
970 }
971 $dummytest++;
972 return $test_body . <<'EOT';
af6c647e 973}
7783f9f6 974EOT
535acd0f 975}
835f860c 976
7783f9f6 977# Simple tests to verify bits of the switch generation system work.
978sub 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;
989if (\$value == $counter) {
990 print "ok $dummytest\n";
3414cef0 991} else {
7783f9f6 992 print "not ok $dummytest # $thisname gave \$value\n";
6d79cad2 993}
7783f9f6 994EOT
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);
39234879 1003 }
6557ab03 1004 }
7783f9f6 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);
0ddb8edc 1009}
ccc70a53 1010
7783f9f6 1011# Check that the memeq clauses work correctly when there isn't a switch
1012# statement to bump off a character
1013simple ("Singletons", "A", "AB", "ABC", "ABCD", "ABCDE");
1014# Check the three code.
1015simple ("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.
1018simple ("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
1020simple ("Three end and four symetry", qw(ean ear eat barb marm tart));
ccc70a53 1021
ccc70a53 1022
7783f9f6 1023# Need this if the single test below is rolled into @tests :
1024# --$dummytest;
1025print "1..$dummytest\n";
ccc70a53 1026
7783f9f6 1027write_and_run_extension @$_ foreach @tests;
4f2c4fd8 1028
1029# This was causing an assertion failure (a C<confess>ion)
7783f9f6 1030# Any single byte > 128 should do it.
4f2c4fd8 1031C_constant ($package, undef, undef, undef, undef, undef, chr 255);
7783f9f6 1032print "ok $realtest\n"; $realtest++;
4f2c4fd8 1033
7783f9f6 1034print STDERR "# You were running with \$keep_files set to $keep_files\n"
1035 if $keep_files;