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