📄 constant.t
字号:
push @args, [PROXYSUBS => 1] if $] > 5.009002;foreach my $args (@args){ # Simple tests start_tests(); my $parent_rfc1149 = 'A Standard for the Transmission of IP Datagrams on Avian Carriers'; # Test the code that generates 1 and 2 letter name comparisons. my %compass = ( N => 0, 'NE' => 45, E => 90, SE => 135, S => 180, SW => 225, W => 270, NW => 315 ); my $header = << "EOT";#define FIVE 5#define OK6 "ok 6\\n"#define OK7 1#define FARTHING 0.25#define NOT_ZERO 1#define Yes 0#define No 1#define Undef 1#define RFC1149 "$parent_rfc1149"#undef NOTDEF#define perl "rules"EOT while (my ($point, $bearing) = each %compass) { $header .= "#define $point $bearing\n" } my @items = ("FIVE", {name=>"OK6", type=>"PV",}, {name=>"OK7", type=>"PVN", value=>['"not ok 7\\n\\0ok 7\\n"', 15]}, {name => "FARTHING", type=>"NV"}, {name => "NOT_ZERO", type=>"UV", value=>"~(UV)0"}, {name => "OPEN", type=>"PV", value=>'"/*"', macro=>1}, {name => "CLOSE", type=>"PV", value=>'"*/"', macro=>["#if 1\n", "#endif\n"]}, {name => "ANSWER", default=>["UV", 42]}, "NOTDEF", {name => "Yes", type=>"YES"}, {name => "No", type=>"NO"}, {name => "Undef", type=>"UNDEF"}, # OK. It wasn't really designed to allow the creation of dual valued # constants. # It was more for INADDR_ANY INADDR_BROADCAST INADDR_LOOPBACK INADDR_NONE {name=>"RFC1149", type=>"SV", value=>"sv_2mortal(temp_sv)", pre=>"SV *temp_sv = newSVpv(RFC1149, 0); " . "(void) SvUPGRADE(temp_sv,SVt_PVIV); SvIOK_on(temp_sv); " . "SvIV_set(temp_sv, 1149);"}, ); push @items, $_ foreach keys %compass; # Automatically compile the list of all the macro names, and make them # exported constants. my @export_names = map {(ref $_) ? $_->{name} : $_} @items; # Exporter::Heavy (currently) isn't able to export the last 3 of these: push @items, @common_items; my $test_body = <<"EOT";my \$test = $dummytest;EOT $test_body .= <<'EOT';# What follows goes to the temporary file.# IVmy $five = FIVE;if ($five == 5) { print "ok $test\n";} else { print "not ok $test # \$five\n";}$test++;# PVif (OK6 eq "ok 6\n") { print "ok $test\n";} else { print "not ok $test # \$five\n";}$test++;# PVN containing embedded \0s$_ = OK7;s/.*\0//s;s/7/$test/;$test++;print;# NVmy $farthing = FARTHING;if ($farthing == 0.25) { print "ok $test\n";} else { print "not ok $test # $farthing\n";}$test++;# UVmy $not_zero = NOT_ZERO;if ($not_zero > 0 && $not_zero == ~0) { print "ok $test\n";} else { print "not ok $test # \$not_zero=$not_zero ~0=" . (~0) . "\n";}$test++;# Value includes a "*/" in an attempt to bust out of a C comment.# Also tests custom cpp #if clausesmy $close = CLOSE;if ($close eq '*/') { print "ok $test\n";} else { print "not ok $test # \$close='$close'\n";}$test++;# Default values if macro not defined.my $answer = ANSWER;if ($answer == 42) { print "ok $test\n";} else { print "not ok $test # What do you get if you multiply six by nine? '$answer'\n";}$test++;# not defined macromy $notdef = eval { NOTDEF; };if (defined $notdef) { print "not ok $test # \$notdef='$notdef'\n";} elsif ($@ !~ /Your vendor has not defined ExtTest macro NOTDEF/) { print "not ok $test # \$@='$@'\n";} else { print "ok $test\n";}$test++;# not a macromy $notthere = eval { &ExtTest::NOTTHERE; };if (defined $notthere) { print "not ok $test # \$notthere='$notthere'\n";} elsif ($@ !~ /NOTTHERE is not a valid ExtTest macro/) { chomp $@; print "not ok $test # \$@='$@'\n";} else { print "ok $test\n";}$test++;# Truthmy $yes = Yes;if ($yes) { print "ok $test\n";} else { print "not ok $test # $yes='\$yes'\n";}$test++;# Falsehoodmy $no = No;if (defined $no and !$no) { print "ok $test\n";} else { print "not ok $test # \$no=" . defined ($no) ? "'$no'\n" : "undef\n";}$test++;# Undefmy $undef = Undef;unless (defined $undef) { print "ok $test\n";} else { print "not ok $test # \$undef='$undef'\n";}$test++;# invalid macro (chosen to look like a mix up between No and SW)$notdef = eval { &ExtTest::So };if (defined $notdef) { print "not ok $test # \$notdef='$notdef'\n";} elsif ($@ !~ /^So is not a valid ExtTest macro/) { print "not ok $test # \$@='$@'\n";} else { print "ok $test\n";}$test++;# invalid defined macro$notdef = eval { &ExtTest::EW };if (defined $notdef) { print "not ok $test # \$notdef='$notdef'\n";} elsif ($@ !~ /^EW is not a valid ExtTest macro/) { print "not ok $test # \$@='$@'\n";} else { print "ok $test\n";}$test++;my %compass = (EOTwhile (my ($point, $bearing) = each %compass) { $test_body .= "'$point' => $bearing, "}$test_body .= <<'EOT';);my $fail;while (my ($point, $bearing) = each %compass) { my $val = eval $point; if ($@) { print "# $point: \$@='$@'\n"; $fail = 1; } elsif (!defined $bearing) { print "# $point: \$val=undef\n"; $fail = 1; } elsif ($val != $bearing) { print "# $point: \$val=$val, not $bearing\n"; $fail = 1; }}if ($fail) { print "not ok $test\n";} else { print "ok $test\n";}$test++;EOT$test_body .= <<"EOT";my \$rfc1149 = RFC1149;if (\$rfc1149 ne "$parent_rfc1149") { print "not ok \$test # '\$rfc1149' ne '$parent_rfc1149'\n";} else { print "ok \$test\n";}\$test++;if (\$rfc1149 != 1149) { printf "not ok \$test # %d != 1149\n", \$rfc1149;} else { print "ok \$test\n";}\$test++;EOT$test_body .= <<'EOT';# test macro=>1my $open = OPEN;if ($open eq '/*') { print "ok $test\n";} else { print "not ok $test # \$open='$open'\n";}$test++;EOT$dummytest+=18; end_tests("Simple tests", \@items, \@export_names, $header, $test_body, $args);}if ($do_utf_tests) { # utf8 tests start_tests(); my ($inf, $pound_bytes, $pound_utf8); $inf = chr 0x221E; # Check that we can distiguish the pathological case of a string, and the # utf8 representation of that string. $pound_utf8 = $pound . '1'; if ($better_than_56) { $pound_bytes = $pound_utf8; utf8::encode ($pound_bytes); } else { # Must have that "U*" to generate a zero length UTF string that forces # top bit set chars (such as the pound sign) into UTF8, so that the # unpack 'C*' then gets the byte form of the UTF8. $pound_bytes = pack 'C*', unpack 'C*', $pound_utf8 . pack "U*"; } my @items = (@common_items, {name=>$inf, type=>"PV", value=>'"Infinity"', macro=>1}, {name=>$pound_utf8, type=>"PV", value=>'"1 Pound"', macro=>1}, {name=>$pound_bytes, type=>"PV", value=>'"1 Pound (as bytes)"', macro=>1}, );=podThe above set of names seems to produce a suitably bad set of compileproblems on a Unicode naive version of ExtUtils::Constant (ie 0.11):nick@thinking-cap 15439-32-utf$ PERL_CORE=1 ./perl lib/ExtUtils/t/Constant.t1..33# perl=/stuff/perl5/15439-32-utf/perl# ext-30370 being created...Wide character in print at lib/ExtUtils/t/Constant.t line 140.ok 1ok 2# make = 'make'ExtTest.xs: In function `constant_1':ExtTest.xs:80: warning: multi-character character constantExtTest.xs:80: warning: case value out of rangeok 3=cut# Grr ` # Do this in 7 bit in case someone is testing with some settings that cause # 8 bit files incapable of storing this character. my @values = map {"'" . join (",", unpack "U*", $_ . pack "U*") . "'"} ($pound, $inf, $pound_bytes, $pound_utf8); # Values is a list of strings, such as ('194,163,49', '163,49') my $test_body .= "my \$test = $dummytest;\n"; $dummytest += 7 * 3; # 3 tests for each of the 7 things: $test_body .= << 'EOT';use utf8;my $better_than_56 = $] > 5.007;my ($pound, $inf, $pound_bytes, $pound_utf8) = map {eval "pack 'U*', $_"}EOT $test_body .= join ",", @values; $test_body .= << 'EOT';;foreach (["perl", "rules", "rules"], ["/*", "OPEN", "OPEN"], ["*/", "CLOSE", "CLOSE"], [$pound, 'Sterling', []], [$inf, 'Infinity', []], [$pound_utf8, '1 Pound', '1 Pound (as bytes)'], [$pound_bytes, '1 Pound (as bytes)', []], ) { # Flag an expected error with a reference for the expect string. my ($string, $expect, $expect_bytes) = @$_; (my $name = $string) =~ s/([^ !"#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~])/sprintf '\x{%X}', ord $1/ges; print "# \"$name\" => \'$expect\'\n"; # Try to force this to be bytes if possible. if ($better_than_56) { utf8::downgrade ($string, 1); } else { if ($string =~ tr/0-\377// == length $string) { # No chars outside range 0-255 $string = pack 'C*', unpack 'U*', ($string . pack 'U*'); } }EOT $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n"; $test_body .= <<'EOT'; if ($error or $got ne $expect) { print "not ok $test # error '$error', got '$got'\n"; } else { print "ok $test\n"; } $test++; print "# Now upgrade '$name' to utf8\n"; if ($better_than_56) { utf8::upgrade ($string); } else { $string = pack ('U*') . $string; }EOT $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n"; $test_body .= <<'EOT'; if ($error or $got ne $expect) { print "not ok $test # error '$error', got '$got'\n"; } else { print "ok $test\n"; } $test++; if (defined $expect_bytes) { print "# And now with the utf8 byte sequence for name\n"; # Try the encoded bytes. if ($better_than_56) { utf8::encode ($string); } else { $string = pack 'C*', unpack 'C*', $string . pack "U*"; }EOT $test_body .= "my (\$error, \$got) = ${package}::constant (\$string);\n"; $test_body .= <<'EOT'; if (ref $expect_bytes) { # Error expected. if ($error) { print "ok $test # error='$error' (as expected)\n"; } else { print "not ok $test # expected error, got no error and '$got'\n"; } } elsif ($got ne $expect_bytes) { print "not ok $test # error '$error', expect '$expect_bytes', got '$got'\n"; } else { print "ok $test\n"; } $test++; }}EOT end_tests("utf8 tests", \@items, [], "#define perl \"rules\"\n", $test_body);}# XXX I think that I should merge this into the utf8 test above.sub explict_call_constant { my ($string, $expect) = @_; # This does assume simple strings suitable for '' my $test_body = <<"EOT";{ my (\$error, \$got) = ${package}::constant ('$string');\n;EOT if (defined $expect) { # No error expected $test_body .= <<"EOT"; if (\$error or \$got ne "$expect") { print "not ok $dummytest # error '\$error', expect '$expect', got '\$got'\n"; } else { print "ok $dummytest\n"; } }EOT } else { # Error expected. $test_body .= <<"EOT"; if (\$error) { print "ok $dummytest # error='\$error' (as expected)\n"; } else { print "not ok $dummytest # expected error, got no error and '\$got'\n"; }EOT } $dummytest++; return $test_body . <<'EOT';}EOT}# Simple tests to verify bits of the switch generation system work.sub simple { start_tests(); # Deliberately leave $name in @_, so that it is indexed from 1. my ($name, @items) = @_; my $test_header; my $test_body = "my \$value;\n"; foreach my $counter (1 .. $#_) { my $thisname = $_[$counter]; $test_header .= "#define $thisname $counter\n"; $test_body .= <<"EOT";\$value = $thisname;if (\$value == $counter) { print "ok $dummytest\n";} else { print "not ok $dummytest # $thisname gave \$value\n";}EOT ++$dummytest; # Yes, the last time round the loop appends a z to the string. for my $i (0 .. length $thisname) { my $copyname = $thisname; substr ($copyname, $i, 1) = 'z'; $test_body .= explict_call_constant ($copyname, $copyname eq $thisname ? $thisname : undef); } } # Ho. This seems to be buggy in 5.005_03: # # Now remove $name from @_: # shift @_; end_tests($name, \@items, \@items, $test_header, $test_body);}# Check that the memeq clauses work correctly when there isn't a switch# statement to bump off a charactersimple ("Singletons", "A", "AB", "ABC", "ABCD", "ABCDE");# Check the three code.simple ("Three start", qw(Bea kea Lea lea nea pea rea sea tea Wea yea Zea));# There were 162 2 letter words in /usr/share/dict/words on FreeBSD 4.6, which# I felt was rather too many. So I used words with 2 vowels.simple ("Twos and three middle", qw(aa ae ai ea eu ie io oe era eta));# Given the choice go for the end, else the earliest pointsimple ("Three end and four symetry", qw(ean ear eat barb marm tart));# Need this if the single test below is rolled into @tests :# --$dummytest;print "1..$dummytest\n";write_and_run_extension @$_ foreach @tests;# This was causing an assertion failure (a C<confess>ion)# Any single byte > 128 should do it.C_constant ($package, undef, undef, undef, undef, undef, chr 255);print "ok $realtest\n"; $realtest++;print STDERR "# You were running with \$keep_files set to $keep_files\n" if $keep_files;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -