This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
open.pm minor cleanup
[perl5.git] / t / io / utf8.t
CommitLineData
7d59b7e4
NIS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
5 @INC = '../lib';
0c4f7ff0 6 unless (find PerlIO::Layer 'perlio') {
7d59b7e4
NIS
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;
e111333b 15print "1..26\n";
7d59b7e4
NIS
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
PP
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
PP
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
PP
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
PP
47$chr = chr(0xa3);
48if (ord('A') == 193) { $chr = chr(0x44); } # EBCDIC
49print "not " unless getc(F) eq $chr;
7d59b7e4
NIS
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
NIS
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
NIS
64close(F);
65
360eb788
NIS
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
PP
83$chr = chr(196).chr(172);
84if (ord('A') == 193) { $chr = chr(141).chr(83); } # EBCDIC
85print "not " unless $x eq $chr;
360eb788
NIS
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
NIS
91print F $a;
92my $y;
f6c77cf1 93{ my $x = tell(F);
360eb788
NIS
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
NIS
106
107{ # Check byte length of $b
108use bytes; my $y = length($b);
f9a63242 109print "not ($y) " unless $y == 1;
360eb788
NIS
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
NIS
116 print "ok 19\n";
117}
118
119close F;
120
121open F, "a" or die $!; # Not UTF
122$x = <F>;
123chomp($x);
d2f5bb60
PP
124$chr = v196.172.194.130;
125if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
126printf "not (%vd) ", $x unless $x eq $chr;
360eb788
NIS
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
NIS
134print "ok 21\n";
135
136# Now let's make it suffer.
137open F, ">", "a" or die $!;
ae798467 138my $w;
54d2e5f1
JH
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
NIS
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
PP
157$chr = v196.172.130;
158if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
159print "not " unless $x eq $chr;
360eb788
NIS
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
NIS
173print "ok 24\n";
174
175# Now we have a deformed file.
d0965105
JH
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
NIS
184}
185
4f0c37ba 186close F;
360eb788 187unlink('a');
7d59b7e4 188
d0965105
JH
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
JH
192print F @a;
193close F;
c36dfd09 194
c88828dc
JH
195my $c;
196
197# read() should work on characters, not bytes
d0965105
JH
198open F, "<:utf8", "a";
199$a = 0;
200for (@a) {
c88828dc
JH
201 unless (($c = read(F, $b, 1) == 1) &&
202 length($b) == 1 &&
203 ord($b) == ord($_) &&
204 tell(F) == ($a += bytes::length($b))) {
34fce102
JH
205 print '# ord($_) == ', ord($_), "\n";
206 print '# ord($b) == ', ord($b), "\n";
207 print '# length($b) == ', length($b), "\n";
208 print '# bytes::length($b) == ', bytes::length($b), "\n";
209 print '# tell(F) == ', tell(F), "\n";
210 print '# $a == ', $a, "\n";
211 print '# $c == ', $c, "\n";
d0965105
JH
212 print "not ";
213 last;
214 }
215}
c36dfd09 216close F;
d0965105
JH
217print "ok 26\n";
218
e111333b 219# sysread() and syswrite() tested in lib/open.t since Fnctl is used
c88828dc
JH
220
221END {
222 1 while unlink "a";
223 1 while unlink "b";
224}
e111333b
JH
225
226