Re: Stateful PerlIO implemented [Was: [perl #22261] Was: Unrecognised BOM...]
[p5sagit/p5-mst-13.2.git] / ext / PerlIO / t / encoding.t
CommitLineData
bb7af5ca 1#!./perl -w
9ba8831b 2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
bb7af5ca 6 no warnings; # Need global -w flag for later tests, but don't want this
7 # to warn here:
e69a2255 8 push @INC, "::lib:$MacPerl::Architecture:" if $^O eq 'MacOS';
9ba8831b 9 unless (find PerlIO::Layer 'perlio') {
10 print "1..0 # Skip: not perlio\n";
11 exit 0;
12 }
13}
14
19d607df 15print "1..14\n";
08efa405 16
8e86646e 17my $grk = "grk$$";
18my $utf = "utf$$";
92e410c2 19my $fail1 = "fa$$";
20my $fail2 = "fb$$";
ed53a2bb 21my $russki = "koi8r$$";
19d607df 22my $threebyte = "3byte$$";
8e86646e 23
2b18b92a 24if (open(GRK, ">$grk")) {
25 binmode(GRK, ":bytes");
8e86646e 26 # alpha beta gamma in ISO 8859-7
27 print GRK "\xe1\xe2\xe3";
d1e4d418 28 close GRK or die "Could not close: $!";
8e86646e 29}
30
31{
32 use Encode;
33 open(my $i,'<:encoding(iso-8859-7)',$grk);
34 print "ok 1\n";
35 open(my $o,'>:utf8',$utf);
36 print "ok 2\n";
37 print $o readline($i);
38 print "ok 3\n";
d1e4d418 39 close($o) or die "Could not close: $!";
8e86646e 40 close($i);
41}
42
2b18b92a 43if (open(UTF, "<$utf")) {
44 binmode(UTF, ":bytes");
07229bbd 45 if (ord('A') == 193) { # EBCDIC
46 # alpha beta gamma in UTF-EBCDIC Unicode (0x3b1 0x3b2 0x3b3)
47 print "not " unless <UTF> eq "\xb4\x58\xb4\x59\xb4\x62";
48 } else {
49 # alpha beta gamma in UTF-8 Unicode (0x3b1 0x3b2 0x3b3)
50 print "not " unless <UTF> eq "\xce\xb1\xce\xb2\xce\xb3";
51 }
8e86646e 52 print "ok 4\n";
206b12d5 53 close UTF;
8e86646e 54}
55
56{
57 use Encode;
58 open(my $i,'<:utf8',$utf);
59 print "ok 5\n";
60 open(my $o,'>:encoding(iso-8859-7)',$grk);
61 print "ok 6\n";
62 print $o readline($i);
63 print "ok 7\n";
d1e4d418 64 close($o) or die "Could not close: $!";
8e86646e 65 close($i);
66}
67
2b18b92a 68if (open(GRK, "<$grk")) {
69 binmode(GRK, ":bytes");
8e86646e 70 print "not " unless <GRK> eq "\xe1\xe2\xe3";
71 print "ok 8\n";
206b12d5 72 close GRK;
8e86646e 73}
74
bb7af5ca 75$SIG{__WARN__} = sub {$warn .= $_[0]};
b26b1ab5 76
77if (open(FAIL, ">:encoding(NoneSuch)", $fail1)) {
78 print "not ok 9 # Open should fail\n";
79} else {
80 print "ok 9\n";
81}
82if (!defined $warn) {
83 print "not ok 10 # warning is undef\n";
84} elsif ($warn =~ /^Cannot find encoding "NoneSuch" at/) {
85 print "ok 10\n";
86} else {
87 print "not ok 10 # warning is '$warn'";
88}
89
ed53a2bb 90if (open(RUSSKI, ">$russki")) {
91 print RUSSKI "\x3c\x3f\x78";
d1e4d418 92 close RUSSKI or die "Could not close: $!";
ed53a2bb 93 open(RUSSKI, "$russki");
94 binmode(RUSSKI, ":raw");
95 my $buf1;
96 read(RUSSKI, $buf1, 1);
bb7af5ca 97 # eof(RUSSKI);
ed53a2bb 98 binmode(RUSSKI, ":encoding(koi8-r)");
99 my $buf2;
100 read(RUSSKI, $buf2, 1);
101 my $offset = tell(RUSSKI);
07229bbd 102 if (ord($buf1) == 0x3c &&
103 ord($buf2) == (ord('A') == 193) ? 0x6f : 0x3f &&
104 $offset == 2) {
ed53a2bb 105 print "ok 11\n";
106 } else {
07229bbd 107 printf "not ok 11 # [%s] [%s] %d\n",
108 join(" ", unpack("H*", $buf1)),
109 join(" ", unpack("H*", $buf2)), $offset;
ed53a2bb 110 }
111 close(RUSSKI);
112} else {
113 print "not ok 11 # open failed: $!\n";
114}
115
bb7af5ca 116undef $warn;
117
118# Check there is no Use of uninitialized value in concatenation (.) warning
119# due to the way @latin2iso_num was used to make aliases.
120if (open(FAIL, ">:encoding(latin42)", $fail2)) {
121 print "not ok 12 # Open should fail\n";
122} else {
123 print "ok 12\n";
124}
125if (!defined $warn) {
126 print "not ok 13 # warning is undef\n";
127} elsif ($warn =~ /^Cannot find encoding "latin42" at.*line \d+\.$/) {
128 print "ok 13\n";
129} else {
130 print "not ok 13 # warning is: \n";
131 $warn =~ s/^/# /mg;
132 print "$warn";
133}
134
19d607df 135# Create a string of chars that are 3 bytes in UTF-8
136my $str = "\x{1f80}" x 2048;
137
138# Write them to a file
139open(F,'>:utf8',$threebyte) || die "Cannot open $threebyte:$!";
140print F $str;
141close(F);
142
143# Read file back as UTF-8
144open(F,'<:encoding(utf-8)',$threebyte) || die "Cannot open $threebyte:$!";
145my $dstr = <F>;
146close(F);
147print "not " unless ($dstr eq $str);
148print "ok 14\n";
149
8e86646e 150END {
19d607df 151 unlink($grk, $utf, $fail1, $fail2, $russki, $threebyte);
8e86646e 152}