This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perluniprops: Make sc property refer to scx
[perl5.git] / t / io / utf8.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require './test.pl'; require './charset_tools.pl';
6     set_up_inc('../lib');
7 }
8 skip_all_without_perlio();
9
10 no utf8; # needed for use utf8 not griping about the raw octets
11
12
13 plan(tests => 63);
14
15 $| = 1;
16
17 my $a_file = tempfile();
18
19 open(F,"+>:utf8",$a_file);
20 print F chr(0x100).'£';
21 cmp_ok( tell(F), '==', 4, tell(F) );
22 print F "\n";
23 cmp_ok( tell(F), '>=', 5, tell(F) );
24 seek(F,0,0);
25 is( getc(F), chr(0x100) );
26 is( getc(F), "£" );
27 is( getc(F), "\n" );
28 seek(F,0,0);
29 binmode(F,":bytes");
30
31 # Byte representation of these characters
32 my $U_100 = byte_utf8a_to_utf8n("\xc4\x80");
33 my $POUND_SIGN = byte_utf8a_to_utf8n("\xc2\xa3");
34
35 my $chr = substr($U_100, 0, 1);
36 is( getc(F), $chr );
37 $chr = substr($U_100, 1, 1);
38 is( getc(F), $chr );
39 $chr = substr($POUND_SIGN, 0, 1);
40 is( getc(F), $chr );
41 $chr = substr($POUND_SIGN, 1, 1);
42 is( getc(F), $chr );
43 is( getc(F), "\n" );
44 seek(F,0,0);
45 binmode(F,":utf8");
46 is( scalar(<F>), "\x{100}£\n" );
47 seek(F,0,0);
48 $buf = chr(0x200);
49 $count = read(F,$buf,2,1);
50 cmp_ok( $count, '==', 2 );
51 is( $buf, "\x{200}\x{100}£" );
52 close(F);
53
54 {
55     $a = chr(300); # This *is* UTF-encoded
56     $b = chr(130); # This is not.
57
58     open F, ">:utf8", $a_file or die $!;
59     print F $a,"\n";
60     close F;
61
62     open F, "<:utf8", $a_file or die $!;
63     $x = <F>;
64     chomp($x);
65     is( $x, chr(300) );
66
67     open F, $a_file or die $!; # Not UTF
68     binmode(F, ":bytes");
69     $x = <F>;
70     chomp($x);
71     $chr = byte_utf8a_to_utf8n(chr(196).chr(172));
72     is( $x, $chr );
73     close F;
74
75     open F, ">:utf8", $a_file or die $!;
76     binmode(F);  # we write a "\n" and then tell() - avoid CRLF issues.
77     binmode(F,":utf8"); # turn UTF-8-ness back on
78     print F $a;
79     my $y;
80     { my $x = tell(F);
81       { use bytes; $y = length($a);}
82       cmp_ok( $x, '==', $y );
83   }
84
85     { # Check byte length of $b
86         use bytes; my $y = length($b);
87         cmp_ok( $y, '==', 1 );
88     }
89
90     print F $b,"\n"; # Don't upgrade $b
91
92     { # Check byte length of $b
93         use bytes; my $y = length($b);
94         cmp_ok( $y, '==', 1 );
95     }
96
97     {
98         my $x = tell(F);
99         { use bytes; if ($::IS_EBCDIC){$y += 2;}else{$y += 3;}} # EBCDIC ASCII
100         cmp_ok( $x, '==', $y );
101     }
102
103     close F;
104
105     open F, $a_file or die $!; # Not UTF
106     binmode(F, ":bytes");
107     $x = <F>;
108     chomp($x);
109     $chr = v196.172.194.130;
110     if ($::IS_EBCDIC) { $chr = v141.83.130; } # EBCDIC
111     is( $x, $chr, sprintf('(%vd)', $x) );
112
113     open F, "<:utf8", $a_file or die $!;
114     $x = <F>;
115     chomp($x);
116     close F;
117     is( $x, chr(300).chr(130), sprintf('(%vd)', $x) );
118
119     open F, ">", $a_file or die $!;
120     binmode(F, ":bytes:");
121
122     # Now let's make it suffer.
123     my $w;
124     {
125         use warnings 'utf8';
126         local $SIG{__WARN__} = sub { $w = $_[0] };
127         print F $a;
128         ok( (!$@));
129         like($w, qr/Wide character in print/i );
130     }
131 }
132
133 # Hm. Time to get more evil.
134 open F, ">:utf8", $a_file or die $!;
135 print F $a;
136 binmode(F, ":bytes");
137 print F chr(130)."\n";
138 close F;
139
140 open F, "<", $a_file or die $!;
141 binmode(F, ":bytes");
142 $x = <F>; chomp $x;
143 $chr = v196.172.130;
144 if ($::IS_EBCDIC) { $chr = v141.83.130; } # EBCDIC
145 is( $x, $chr );
146
147 # Right.
148 open F, ">:utf8", $a_file or die $!;
149 print F $a;
150 close F;
151 open F, ">>", $a_file or die $!;
152 binmode(F, ":bytes");
153 print F chr(130)."\n";
154 close F;
155
156 open F, "<", $a_file or die $!;
157 binmode(F, ":bytes");
158 $x = <F>; chomp $x;
159 SKIP: {
160     skip("Defaulting to UTF-8 output means that we can't generate a mangled file")
161         if $UTF8_OUTPUT;
162     is( $x, $chr );
163 }
164
165 # Now we have a deformed file.
166
167 SKIP: {
168     if ($::IS_EBCDIC) {
169         skip("EBCDIC The file isn't deformed in UTF-EBCDIC", 2);
170     } else {
171         my @warnings;
172         open F, "<:utf8", $a_file or die $!;
173         $x = <F>; chomp $x;
174         local $SIG{__WARN__} = sub { push @warnings, $_[0]; };
175         eval { sprintf "%vd\n", $x };
176         is (scalar @warnings, 1);
177         like ($warnings[0], qr/Malformed UTF-8 character: \\x82 \(unexpected continuation byte 0x82, with no preceding start byte/);
178     }
179 }
180
181 close F;
182 unlink($a_file);
183
184 open F, ">:utf8", $a_file;
185 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
186 unshift @a, chr(0); # ... and a null byte in front just for fun
187 print F @a;
188 close F;
189
190 my $c;
191
192 # read() should work on characters, not bytes
193 open F, "<:utf8", $a_file;
194 $a = 0;
195 my $failed;
196 for (@a) {
197     unless (($c = read(F, $b, 1) == 1)  &&
198             length($b)           == 1  &&
199             ord($b)              == ord($_) &&
200             tell(F)              == ($a += bytes::length($b))) {
201         print '# ord($_)           == ', ord($_), "\n";
202         print '# ord($b)           == ', ord($b), "\n";
203         print '# length($b)        == ', length($b), "\n";
204         print '# bytes::length($b) == ', bytes::length($b), "\n";
205         print '# tell(F)           == ', tell(F), "\n";
206         print '# $a                == ', $a, "\n";
207         print '# $c                == ', $c, "\n";
208         $failed++;
209         last;
210     }
211 }
212 close F;
213 is($failed, undef);
214
215 {
216     # Check that warnings are on on I/O, and that they can be muffled.
217
218     local $SIG{__WARN__} = sub { $@ = shift };
219
220     undef $@;
221     open F, ">$a_file";
222     binmode(F, ":bytes");
223     print F chr(0x100);
224     close(F);
225
226     like( $@, qr/Wide character in print/ );
227
228     undef $@;
229     open F, ">:utf8", $a_file;
230     print F chr(0x100);
231     close(F);
232
233     isnt( defined $@, !0 );
234
235     undef $@;
236     open F, ">$a_file";
237     binmode(F, ":utf8");
238     print F chr(0x100);
239     close(F);
240
241     isnt( defined $@, !0 );
242
243     no warnings 'utf8';
244
245     undef $@;
246     open F, ">$a_file";
247     print F chr(0x100);
248     close(F);
249
250     isnt( defined $@, !0 );
251
252     use warnings 'utf8';
253
254     undef $@;
255     open F, ">$a_file";
256     binmode(F, ":bytes");
257     print F chr(0x100);
258     close(F);
259
260     like( $@, qr/Wide character in print/ );
261 }
262
263 {
264     open F, ">:bytes",$a_file; print F "\xde"; close F;
265
266     open F, "<:bytes", $a_file;
267     my $b = chr 0x100;
268     $b .= <F>;
269     is( $b, chr(0x100).chr(0xde), "21395 '.= <>' utf8 vs. bytes" );
270     close F;
271 }
272
273 {
274     open F, ">:utf8",$a_file; print F chr 0x100; close F;
275
276     open F, "<:utf8", $a_file;
277     my $b = "\xde";
278     $b .= <F>;
279     is( $b, chr(0xde).chr(0x100), "21395 '.= <>' bytes vs. utf8" );
280     close F;
281 }
282
283 {
284     my @a = ( [ 0x007F, "bytes" ],
285               [ 0x0080, "bytes" ],
286               [ 0x0080, "utf8"  ],
287               [ 0x0100, "utf8"  ] );
288     my $t = 34;
289     for my $u (@a) {
290         for my $v (@a) {
291             # print "# @$u - @$v\n";
292             open F, ">$a_file";
293             binmode(F, ":" . $u->[1]);
294             print F chr($u->[0]);
295             close F;
296
297             open F, "<$a_file";
298             binmode(F, ":" . $u->[1]);
299
300             my $s = chr($v->[0]);
301             utf8::upgrade($s) if $v->[1] eq "utf8";
302
303             $s .= <F>;
304             is( $s, chr($v->[0]) . chr($u->[0]), 'rcatline utf8' );
305             close F;
306             $t++;
307         }
308     }
309     # last test here 49
310 }
311
312 {
313     # [perl #23428] Somethings rotten in unicode semantics
314     open F, ">$a_file";
315     binmode F, ":utf8";
316     no warnings qw(deprecated);
317     syswrite(F, $a = chr(0x100));
318     close F;
319     is( ord($a), 0x100, '23428 syswrite should not downgrade scalar' );
320     like( $a, qr/^\w+/, '23428 syswrite should not downgrade scalar' );
321 }
322
323 # sysread() and syswrite() tested in lib/open.t since Fcntl is used
324
325 {
326     # <FH> on a :utf8 stream should complain immediately with -w
327     # if it finds bad UTF-8 (:encoding(utf8) works this way)
328     use warnings 'utf8';
329     undef $@;
330     local $SIG{__WARN__} = sub { $@ = shift };
331     open F, ">$a_file";
332     binmode F;
333     my ($chrE4, $chrF6) = (chr(0xE4), chr(0xF6));
334     if ($::IS_EBCDIC)   # EBCDIC
335     { ($chrE4, $chrF6) = (chr(0x43), chr(0xEC)); }
336     print F "foo", $chrE4, "\n";
337     print F "foo", $chrF6, "\n";
338     close F;
339     open F, "<:utf8", $a_file;
340     undef $@;
341     my $line = <F>;
342     my ($chrE4, $chrF6) = ("E4", "F6");
343     if ($::IS_EBCDIC) { ($chrE4, $chrF6) = ("43", "EC"); } # EBCDIC
344     like( $@, qr/utf8 "\\x$chrE4" does not map to Unicode .+ <F> line 1/,
345           "<:utf8 readline must warn about bad utf8");
346     undef $@;
347     $line .= <F>;
348     like( $@, qr/utf8 "\\x$chrF6" does not map to Unicode .+ <F> line 2/,
349           "<:utf8 rcatline must warn about bad utf8");
350     close F;
351 }
352
353 {
354     # fixed record reads
355     open F, ">:utf8", $a_file;
356     print F "foo\xE4";
357     print F "bar\xFE";
358     print F "\xC0\xC8\xCC\xD2";
359     print F "a\xE4ab";
360     print F "a\xE4a";
361     close F;
362     open F, "<:utf8", $a_file;
363     local $/ = \4;
364     my $line = <F>;
365     is($line, "foo\xE4", "readline with \$/ = \\4");
366     $line .= <F>;
367     is($line, "foo\xE4bar\xFE", "rcatline with \$/ = \\4");
368     $line = <F>;
369     is($line, "\xC0\xC8\xCC\xD2", "readline with several encoded characters");
370     $line = <F>;
371     is($line, "a\xE4ab", "readline with another boundary condition");
372     $line = <F>;
373     is($line, "a\xE4a", "readline with boundary condition");
374     close F;
375
376     # badly encoded at EOF
377     open F, ">:raw", $a_file;
378     print F "foo\xEF\xAC"; # truncated \x{FB04} small ligature ffl
379     close F;
380
381     use warnings 'utf8';
382     open F, "<:utf8", $a_file;
383     undef $@;
384     local $SIG{__WARN__} = sub { $@ = shift };
385     $line = <F>;
386
387     like( $@, qr/utf8 "\\xEF" does not map to Unicode .+ <F> chunk 1/,
388           "<:utf8 readline (fixed) must warn about bad utf8");
389     close F;
390 }
391
392 # getc should reset the utf8 flag and not be affected by previous
393 # return values
394 SKIP: {
395     skip "no PerlIO::scalar on miniperl", 2, if is_miniperl();
396     open my $fh, "<:raw",  \($buf = chr 255);
397     open my $uh, "<:utf8", \($uuf = $U_100);
398     for([$uh,chr 256], [$fh,chr 255]) {
399         is getc $$_[0], $$_[1],
400           'getc returning non-utf8 after utf8';
401     }
402 }