1# $Id: enc_eucjp.t,v 2.2 2013/02/18 02:23:56 dankogai Exp $ 2# This is the twin of enc_utf8.t . 3 4BEGIN { 5 require Config; import Config; 6 if ($Config{'extensions'} !~ /\bEncode\b/) { 7 print "1..0 # Skip: Encode was not built\n"; 8 exit 0; 9 } 10 unless (find PerlIO::Layer 'perlio') { 11 print "1..0 # Skip: PerlIO was not built\n"; 12 exit 0; 13 } 14 if (ord("A") == 193) { 15 print "1..0 # encoding pragma does not support EBCDIC platforms\n"; 16 exit(0); 17 } 18 if ($] <= 5.008 and !$Config{perl_patchlevel}){ 19 print "1..0 # Skip: Perl 5.8.1 or later required\n"; 20 exit 0; 21 } 22} 23 24no warnings "deprecated"; 25use encoding 'euc-jp'; 26 27my @c = (127, 128, 255, 256); 28 29print "1.." . (scalar @c + 1) . "\n"; 30 31my @f; 32 33for my $i (0..$#c) { 34 no warnings 'pack'; 35 my $file = filename("f$i"); 36 push @f, $file; 37 open(F, ">$file") or die "$0: failed to open '$file' for writing: $!"; 38 binmode(F, ":utf8"); 39 print F chr($c[$i]); 40 print F pack("C" => $c[$i]); 41 close F; 42} 43 44my $t = 1; 45 46for my $i (0..$#c) { 47 my $file = filename("f$i"); 48 open(F, "<$file") or die "$0: failed to open '$file' for reading: $!"; 49 binmode(F, ":utf8"); 50 my $c = <F>; 51 my $o = ord($c); 52 print $o == $c[$i] ? "ok $t - utf8 I/O $c[$i]\n" : "not ok $t - utf8 I/O $c[$i]: $o != $c[$i]\n"; 53 $t++; 54} 55 56my $f = filename("f" . @f); 57 58push @f, $f; 59open(F, ">$f") or die "$0: failed to open '$f' for writing: $!"; 60binmode(F, ":raw"); # Output raw bytes. 61print F chr(128); # Output illegal UTF-8. 62close F; 63open(F, $f) or die "$0: failed to open '$f' for reading: $!"; 64binmode(F, ":encoding(utf-8)"); 65{ 66 local $^W = 1; 67 local $SIG{__WARN__} = sub { $a = shift }; 68 eval { <F> }; # This should get caught. 69} 70close F; 71print $a =~ qr{^utf8 "\\x80" does not map to Unicode} ? 72 "ok $t - illegal utf8 input\n" : "not ok $t - illegal utf8 input: a = " . unpack("H*", $a) . "\n"; 73 74# On VMS temporary file names like "f0." may be more readable than "f0" since 75# "f0" could be a logical name pointing elsewhere. 76sub filename { 77 my $name = shift; 78 $name .= '.' if $^O eq 'VMS'; 79 return $name; 80} 81 82END { 83 1 while unlink @f; 84} 85