1# $Id: enc_utf8.t,v 2.6 2019/12/25 09:23:21 dankogai Exp $ 2# This is the twin of enc_eucjp.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.025003 and !$Config{usecperl}){ 19 print "1..0 # Skip: Perl <=5.25.2 or cperl required\n"; 20 exit 0; 21 } 22} 23 24no warnings "deprecated"; 25use encoding 'utf8'; 26use warnings; 27 28my @c = (127, 128, 255, 256); 29 30print "1.." . (scalar @c + 2) . "\n"; 31 32my @f; 33 34for my $i (0..$#c) { 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 close F; 41} 42 43my $t = 1; 44 45for my $i (0..$#c) { 46 my $file = filename("f$i"); 47 open(F, "<$file") or die "$0: failed to open '$file' for reading: $!"; 48 binmode(F, ":utf8"); 49 my $c = <F>; 50 my $o = ord($c); 51 print $o == $c[$i] ? "ok $t - utf8 I/O $c[$i]\n" : "not ok $t - utf8 I/O $c[$i]: $o != $c[$i]\n"; 52 $t++; 53} 54 55my $f = filename("f" . @f); 56 57push @f, $f; 58open(F, ">$f") or die "$0: failed to open '$f' for writing: $!"; 59binmode(F, ":raw"); # Output raw bytes. 60print F chr(128); # Output illegal UTF-8. 61close F; 62open(F, $f) or die "$0: failed to open '$f' for reading: $!"; 63binmode(F, ":encoding(UTF-8)"); 64{ 65 local $^W = 1; 66 local $SIG{__WARN__} = sub { $a = shift }; 67 eval { <F> }; # This should get caught. 68} 69close F; 70print $a =~ qr{^UTF-8 "\\x80" does not map to Unicode} ? 71 "ok $t - illegal UTF-8 input\n" : "not ok $t - illegal UTF-8 input: a = " . unpack("H*", $a) . "\n"; 72$t++; 73 74open(F, $f) or die "$0: failed to open '$f' for reading: $!"; 75binmode(F, ":encoding(utf8)"); 76{ 77 local $^W = 1; 78 local $SIG{__WARN__} = sub { $a = shift }; 79 eval { <F> }; # This should get caught. 80} 81close F; 82print $a =~ qr{^utf8 "\\x80" does not map to Unicode} ? 83 "ok $t - illegal utf8 input\n" : "not ok $t - illegal utf8 input: a = " . unpack("H*", $a) . "\n"; 84$t++; 85 86# On VMS temporary file names like "f0." may be more readable than "f0" since 87# "f0" could be a logical name pointing elsewhere. 88sub filename { 89 my $name = shift; 90 $name .= '.' if $^O eq 'VMS'; 91 return $name; 92} 93 94END { 95 1 while unlink @f; 96} 97