Retract #13082 until a better solution is found:
[p5sagit/p5-mst-13.2.git] / t / io / utf8.t
CommitLineData
7d59b7e4 1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
0c4f7ff0 6 unless (find PerlIO::Layer 'perlio') {
7d59b7e4 7 print "1..0 # Skip: not perlio\n";
8 exit 0;
9 }
10}
11
169da838 12no utf8; # needed for use utf8 not griping about the raw octets
3ba0e062 13
7d59b7e4 14$| = 1;
c88828dc 15print "1..29\n";
7d59b7e4 16
17open(F,"+>:utf8",'a');
18print F chr(0x100).'£';
19print '#'.tell(F)."\n";
20print "not " unless tell(F) == 4;
21print "ok 1\n";
22print F "\n";
23print '#'.tell(F)."\n";
24print "not " unless tell(F) >= 5;
25print "ok 2\n";
26seek(F,0,0);
27print "not " unless getc(F) eq chr(0x100);
28print "ok 3\n";
29print "not " unless getc(F) eq "£";
30print "ok 4\n";
31print "not " unless getc(F) eq "\n";
32print "ok 5\n";
33seek(F,0,0);
34binmode(F,":bytes");
d2f5bb60 35my $chr = chr(0xc4);
36if (ord('A') == 193) { $chr = chr(0x8c); } # EBCDIC
37print "not " unless getc(F) eq $chr;
7d59b7e4 38print "ok 6\n";
d2f5bb60 39$chr = chr(0x80);
40if (ord('A') == 193) { $chr = chr(0x41); } # EBCDIC
41print "not " unless getc(F) eq $chr;
7d59b7e4 42print "ok 7\n";
d2f5bb60 43$chr = chr(0xc2);
44if (ord('A') == 193) { $chr = chr(0x80); } # EBCDIC
45print "not " unless getc(F) eq $chr;
7d59b7e4 46print "ok 8\n";
d2f5bb60 47$chr = chr(0xa3);
48if (ord('A') == 193) { $chr = chr(0x44); } # EBCDIC
49print "not " unless getc(F) eq $chr;
7d59b7e4 50print "ok 9\n";
51print "not " unless getc(F) eq "\n";
52print "ok 10\n";
53seek(F,0,0);
54binmode(F,":utf8");
55print "not " unless scalar(<F>) eq "\x{100}£\n";
56print "ok 11\n";
eb5c063a 57seek(F,0,0);
58$buf = chr(0x200);
59$count = read(F,$buf,2,1);
60print "not " unless $count == 2;
61print "ok 12\n";
62print "not " unless $buf eq "\x{200}\x{100}£";
63print "ok 13\n";
7d59b7e4 64close(F);
65
360eb788 66{
67$a = chr(300); # This *is* UTF-encoded
68$b = chr(130); # This is not.
69
70open F, ">:utf8", 'a' or die $!;
71print F $a,"\n";
72close F;
73
74open F, "<:utf8", 'a' or die $!;
75$x = <F>;
76chomp($x);
77print "not " unless $x eq chr(300);
78print "ok 14\n";
79
80open F, "a" or die $!; # Not UTF
81$x = <F>;
82chomp($x);
d2f5bb60 83$chr = chr(196).chr(172);
84if (ord('A') == 193) { $chr = chr(141).chr(83); } # EBCDIC
85print "not " unless $x eq $chr;
360eb788 86print "ok 15\n";
87close F;
88
89open F, ">:utf8", 'a' or die $!;
79086a00 90binmode(F); # we write a "\n" and then tell() - avoid CRLF issues.
360eb788 91print F $a;
92my $y;
f6c77cf1 93{ my $x = tell(F);
360eb788 94 { use bytes; $y = length($a);}
95 print "not " unless $x == $y;
96 print "ok 16\n";
97}
98
99{ # Check byte length of $b
100use bytes; my $y = length($b);
101print "not " unless $y == 1;
102print "ok 17\n";
103}
104
f9a63242 105print F $b,"\n"; # Don't upgrades $b
360eb788 106
107{ # Check byte length of $b
108use bytes; my $y = length($b);
f9a63242 109print "not ($y) " unless $y == 1;
360eb788 110print "ok 18\n";
111}
112
f6c77cf1 113{ my $x = tell(F);
d2f5bb60 114 { use bytes; if (ord('A')==193){$y += 2;}else{$y += 3;}} # EBCDIC ASCII
f9a63242 115 print "not ($x,$y) " unless $x == $y;
360eb788 116 print "ok 19\n";
117}
118
119close F;
120
121open F, "a" or die $!; # Not UTF
122$x = <F>;
123chomp($x);
d2f5bb60 124$chr = v196.172.194.130;
125if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
126printf "not (%vd) ", $x unless $x eq $chr;
360eb788 127print "ok 20\n";
128
129open F, "<:utf8", "a" or die $!;
130$x = <F>;
131chomp($x);
132close F;
f9a63242 133printf "not (%vd) ", $x unless $x eq chr(300).chr(130);
360eb788 134print "ok 21\n";
135
136# Now let's make it suffer.
137open F, ">", "a" or die $!;
ae798467 138my $w;
54d2e5f1 139{
140 use warnings 'utf8';
141 local $SIG{__WARN__} = sub { $w = $_[0] };
142 print F $a;
143}
ae798467 144print "not " if ($@ || $w !~ /Wide character in print/i);
360eb788 145print "ok 22\n";
146}
147
148# Hm. Time to get more evil.
149open F, ">:utf8", "a" or die $!;
150print F $a;
151binmode(F, ":bytes");
152print F chr(130)."\n";
153close F;
154
155open F, "<", "a" or die $!;
156$x = <F>; chomp $x;
d2f5bb60 157$chr = v196.172.130;
158if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
159print "not " unless $x eq $chr;
360eb788 160print "ok 23\n";
161
162# Right.
163open F, ">:utf8", "a" or die $!;
164print F $a;
165close F;
166open F, ">>", "a" or die $!;
167print F chr(130)."\n";
168close F;
169
170open F, "<", "a" or die $!;
171$x = <F>; chomp $x;
d2f5bb60 172print "not " unless $x eq $chr;
360eb788 173print "ok 24\n";
174
175# Now we have a deformed file.
d0965105 176
177if (ord('A') == 193) {
178 print "ok 25 # Skip: EBCDIC\n"; # EBCDIC doesn't complain
179} else {
180 open F, "<:utf8", "a" or die $!;
181 $x = <F>; chomp $x;
182 local $SIG{__WARN__} = sub { print "ok 25\n" };
183 eval { sprintf "%vd\n", $x };
360eb788 184}
185
4f0c37ba 186close F;
360eb788 187unlink('a');
7d59b7e4 188
d0965105 189open F, ">a";
190@a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
c36dfd09 191unshift @a, chr(0); # ... and a null byte in front just for fun
d0965105 192print F @a;
193close F;
c36dfd09 194
c88828dc 195my $c;
196
197# read() should work on characters, not bytes
d0965105 198open F, "<:utf8", "a";
199$a = 0;
200for (@a) {
c88828dc 201 unless (($c = read(F, $b, 1) == 1) &&
202 length($b) == 1 &&
203 ord($b) == ord($_) &&
204 tell(F) == ($a += bytes::length($b))) {
d0965105 205 print '# ord($_) == ', ord($_), "\n";
206 print '# ord($b) == ', ord($b), "\n";
207 print '# length($b) == ', length($b), "\n";
208 print '# tell(F) == ', tell(F), "\n";
c88828dc 209 print '# $a == ', $a, "\n";
210 print '# $c == ', $c, "\n";
d0965105 211 print "not ";
212 last;
213 }
214}
c36dfd09 215close F;
d0965105 216print "ok 26\n";
217
c88828dc 218# sysread() should work on characters, not bytes
003a51c1 219open F, "<:utf8", "a";
220$a = 0;
221for (@a) {
c88828dc 222 unless (($c = sysread(F, $b, 1)) == 1 &&
223 length($b) == 1 &&
224 ord($b) == ord($_) &&
225 tell(F) == ($a += bytes::length($b))) {
003a51c1 226 print '# ord($_) == ', ord($_), "\n";
227 print '# ord($b) == ', ord($b), "\n";
228 print '# length($b) == ', length($b), "\n";
229 print '# tell(F) == ', tell(F), "\n";
c88828dc 230 print '# $a == ', $a, "\n";
231 print '# $c == ', $c, "\n";
003a51c1 232 print "not ";
233 last;
234 }
235}
236close F;
237print "ok 27\n";
238
c88828dc 239# syswrite() on should work on characters, not bytes
240open G, ">:utf8", "b";
241$a = 0;
242for (@a) {
243 unless (($c = syswrite(G, $_, 1)) == 1 &&
244 tell(G) == ($a += bytes::length($_))) {
245 print '# ord($_) == ', ord($_), "\n";
246 print '# tell(G) == ', tell(G), "\n";
247 print '# $a == ', $a, "\n";
248 print '# $c == ', $c, "\n";
249 print "not ";
250 last;
251 }
252}
253close G;
254print "ok 28\n";
255
256# did syswrite() get it right?
257open G, "<:utf8", "b";
258$a = 0;
259for (@a) {
260 unless (($c = sysread(G, $b, 1)) == 1 &&
261 length($b) == 1 &&
262 ord($b) == ord($_) &&
263 tell(G) == ($a += bytes::length($_))) {
264 print '# ord($_) == ', ord($_), "\n";
265 print '# ord($b) == ', ord($b), "\n";
266 print '# length($b) == ', length($b), "\n";
267 print '# tell(G) == ', tell(G), "\n";
268 print '# $a == ', $a, "\n";
269 print '# $c == ', $c, "\n";
270 print "not ";
271 last;
272 }
273}
274close G;
275print "ok 29\n";
276
277END {
278 1 while unlink "a";
279 1 while unlink "b";
280}