7 skip_all_without_perlio();
10 no utf8; # needed for use utf8 not griping about the raw octets
17 my $a_file = tempfile();
19 open(F,"+>:utf8",$a_file);
20 print F chr(0x100).'£';
21 cmp_ok( tell(F), '==', 4, tell(F) );
23 cmp_ok( tell(F), '>=', 5, tell(F) );
25 is( getc(F), chr(0x100) );
31 if (ord($a_file) == 193) { $chr = chr(0x8c); } # EBCDIC
34 if (ord($a_file) == 193) { $chr = chr(0x41); } # EBCDIC
37 if (ord($a_file) == 193) { $chr = chr(0x80); } # EBCDIC
40 if (ord($a_file) == 193) { $chr = chr(0x44); } # EBCDIC
45 is( scalar(<F>), "\x{100}£\n" );
48 $count = read(F,$buf,2,1);
49 cmp_ok( $count, '==', 2 );
50 is( $buf, "\x{200}\x{100}£" );
54 $a = chr(300); # This *is* UTF-encoded
55 $b = chr(130); # This is not.
57 open F, ">:utf8", $a_file or die $!;
61 open F, "<:utf8", $a_file or die $!;
66 open F, $a_file or die $!; # Not UTF
70 $chr = chr(196).chr(172);
71 if (ord($a_file) == 193) { $chr = chr(141).chr(83); } # EBCDIC
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
81 { use bytes; $y = length($a);}
82 cmp_ok( $x, '==', $y );
85 { # Check byte length of $b
86 use bytes; my $y = length($b);
87 cmp_ok( $y, '==', 1 );
90 print F $b,"\n"; # Don't upgrades $b
92 { # Check byte length of $b
93 use bytes; my $y = length($b);
94 cmp_ok( $y, '==', 1 );
99 { use bytes; if (ord('A')==193){$y += 2;}else{$y += 3;}} # EBCDIC ASCII
100 cmp_ok( $x, '==', $y );
105 open F, $a_file or die $!; # Not UTF
106 binmode(F, ":bytes");
109 $chr = v196.172.194.130;
110 if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
111 is( $x, $chr, sprintf('(%vd)', $x) );
113 open F, "<:utf8", $a_file or die $!;
117 is( $x, chr(300).chr(130), sprintf('(%vd)', $x) );
119 open F, ">", $a_file or die $!;
120 binmode(F, ":bytes:");
122 # Now let's make it suffer.
126 local $SIG{__WARN__} = sub { $w = $_[0] };
129 like($w, qr/Wide character in print/i );
133 # Hm. Time to get more evil.
134 open F, ">:utf8", $a_file or die $!;
136 binmode(F, ":bytes");
137 print F chr(130)."\n";
140 open F, "<", $a_file or die $!;
141 binmode(F, ":bytes");
144 if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
148 open F, ">:utf8", $a_file or die $!;
151 open F, ">>", $a_file or die $!;
152 binmode(F, ":bytes");
153 print F chr(130)."\n";
156 open F, "<", $a_file or die $!;
157 binmode(F, ":bytes");
160 skip("Defaulting to UTF-8 output means that we can't generate a mangled file")
165 # Now we have a deformed file.
168 if (ord('A') == 193) {
169 skip("EBCDIC doesn't complain", 2);
172 open F, "<:utf8", $a_file or die $!;
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 \(unexpected continuation byte 0x82, with no preceding start byte/);
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
192 # read() should work on characters, not bytes
193 open F, "<:utf8", $a_file;
197 unless (($c = read(F, $b, 1) == 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";
216 # Check that warnings are on on I/O, and that they can be muffled.
218 local $SIG{__WARN__} = sub { $@ = shift };
222 binmode(F, ":bytes");
226 like( $@, 'Wide character in print' );
229 open F, ">:utf8", $a_file;
233 isnt( defined $@, !0 );
241 isnt( defined $@, !0 );
250 isnt( defined $@, !0 );
256 binmode(F, ":bytes");
260 like( $@, 'Wide character in print' );
264 open F, ">:bytes",$a_file; print F "\xde"; close F;
266 open F, "<:bytes", $a_file;
269 is( $b, chr(0x100).chr(0xde), "21395 '.= <>' utf8 vs. bytes" );
274 open F, ">:utf8",$a_file; print F chr 0x100; close F;
276 open F, "<:utf8", $a_file;
279 is( $b, chr(0xde).chr(0x100), "21395 '.= <>' bytes vs. utf8" );
284 my @a = ( [ 0x007F, "bytes" ],
287 [ 0x0100, "utf8" ] );
291 # print "# @$u - @$v\n";
293 binmode(F, ":" . $u->[1]);
294 print F chr($u->[0]);
298 binmode(F, ":" . $u->[1]);
300 my $s = chr($v->[0]);
301 utf8::upgrade($s) if $v->[1] eq "utf8";
304 is( $s, chr($v->[0]) . chr($u->[0]), 'rcatline utf8' );
313 # [perl #23428] Somethings rotten in unicode semantics
316 syswrite(F, $a = chr(0x100));
318 is( ord($a), 0x100, '23428 syswrite should not downgrade scalar' );
319 like( $a, qr/^\w+/, '23428 syswrite should not downgrade scalar' );
322 # sysread() and syswrite() tested in lib/open.t since Fcntl is used
325 # <FH> on a :utf8 stream should complain immediately with -w
326 # if it finds bad UTF-8 (:encoding(utf8) works this way)
329 local $SIG{__WARN__} = sub { $@ = shift };
332 my ($chrE4, $chrF6) = (chr(0xE4), chr(0xF6));
333 if (ord('A') == 193) # EBCDIC
334 { ($chrE4, $chrF6) = (chr(0x43), chr(0xEC)); }
335 print F "foo", $chrE4, "\n";
336 print F "foo", $chrF6, "\n";
338 open F, "<:utf8", $a_file;
341 my ($chrE4, $chrF6) = ("E4", "F6");
342 if (ord('A') == 193) { ($chrE4, $chrF6) = ("43", "EC"); } # EBCDIC
343 like( $@, qr/utf8 "\\x$chrE4" does not map to Unicode .+ <F> line 1/,
344 "<:utf8 readline must warn about bad utf8");
347 like( $@, qr/utf8 "\\x$chrF6" does not map to Unicode .+ <F> line 2/,
348 "<:utf8 rcatline must warn about bad utf8");