6 unless (find PerlIO::Layer 'perlio') {
7 print "1..0 # Skip: not perlio\n";
12 no utf8; # needed for use utf8 not griping about the raw octets
14 BEGIN { require "./test.pl"; }
20 my $a_file = tempfile();
22 open(F,"+>:utf8",$a_file);
23 print F chr(0x100).'£';
24 cmp_ok( tell(F), '==', 4, tell(F) );
26 cmp_ok( tell(F), '>=', 5, tell(F) );
28 is( getc(F), chr(0x100) );
34 if (ord($a_file) == 193) { $chr = chr(0x8c); } # EBCDIC
37 if (ord($a_file) == 193) { $chr = chr(0x41); } # EBCDIC
40 if (ord($a_file) == 193) { $chr = chr(0x80); } # EBCDIC
43 if (ord($a_file) == 193) { $chr = chr(0x44); } # EBCDIC
48 is( scalar(<F>), "\x{100}£\n" );
51 $count = read(F,$buf,2,1);
52 cmp_ok( $count, '==', 2 );
53 is( $buf, "\x{200}\x{100}£" );
57 $a = chr(300); # This *is* UTF-encoded
58 $b = chr(130); # This is not.
60 open F, ">:utf8", $a_file or die $!;
64 open F, "<:utf8", $a_file or die $!;
69 open F, $a_file or die $!; # Not UTF
73 $chr = chr(196).chr(172);
74 if (ord($a_file) == 193) { $chr = chr(141).chr(83); } # EBCDIC
78 open F, ">:utf8", $a_file or die $!;
79 binmode(F); # we write a "\n" and then tell() - avoid CRLF issues.
80 binmode(F,":utf8"); # turn UTF-8-ness back on
84 { use bytes; $y = length($a);}
85 cmp_ok( $x, '==', $y );
88 { # Check byte length of $b
89 use bytes; my $y = length($b);
90 cmp_ok( $y, '==', 1 );
93 print F $b,"\n"; # Don't upgrades $b
95 { # Check byte length of $b
96 use bytes; my $y = length($b);
97 cmp_ok( $y, '==', 1 );
102 { use bytes; if (ord('A')==193){$y += 2;}else{$y += 3;}} # EBCDIC ASCII
103 cmp_ok( $x, '==', $y );
108 open F, $a_file or die $!; # Not UTF
109 binmode(F, ":bytes");
112 $chr = v196.172.194.130;
113 if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
114 is( $x, $chr, sprintf('(%vd)', $x) );
116 open F, "<:utf8", $a_file or die $!;
120 is( $x, chr(300).chr(130), sprintf('(%vd)', $x) );
122 open F, ">", $a_file or die $!;
123 binmode(F, ":bytes:");
125 # Now let's make it suffer.
129 local $SIG{__WARN__} = sub { $w = $_[0] };
132 like($w, qr/Wide character in print/i );
136 # Hm. Time to get more evil.
137 open F, ">:utf8", $a_file or die $!;
139 binmode(F, ":bytes");
140 print F chr(130)."\n";
143 open F, "<", $a_file or die $!;
144 binmode(F, ":bytes");
147 if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
151 open F, ">:utf8", $a_file or die $!;
154 open F, ">>", $a_file or die $!;
155 binmode(F, ":bytes");
156 print F chr(130)."\n";
159 open F, "<", $a_file or die $!;
160 binmode(F, ":bytes");
163 skip("Defaulting to UTF-8 output means that we can't generate a mangled file")
168 # Now we have a deformed file.
171 if (ord('A') == 193) {
172 skip("EBCDIC doesn't complain", 2);
175 open F, "<:utf8", $a_file or die $!;
177 local $SIG{__WARN__} = sub { push @warnings, $_[0]; };
178 eval { sprintf "%vd\n", $x };
179 is (scalar @warnings, 1);
180 like ($warnings[0], qr/Malformed UTF-8 character \(unexpected continuation byte 0x82, with no preceding start byte/);
187 open F, ">:utf8", $a_file;
188 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
189 unshift @a, chr(0); # ... and a null byte in front just for fun
195 # read() should work on characters, not bytes
196 open F, "<:utf8", $a_file;
200 unless (($c = read(F, $b, 1) == 1) &&
202 ord($b) == ord($_) &&
203 tell(F) == ($a += bytes::length($b))) {
204 print '# ord($_) == ', ord($_), "\n";
205 print '# ord($b) == ', ord($b), "\n";
206 print '# length($b) == ', length($b), "\n";
207 print '# bytes::length($b) == ', bytes::length($b), "\n";
208 print '# tell(F) == ', tell(F), "\n";
209 print '# $a == ', $a, "\n";
210 print '# $c == ', $c, "\n";
219 # Check that warnings are on on I/O, and that they can be muffled.
221 local $SIG{__WARN__} = sub { $@ = shift };
225 binmode(F, ":bytes");
229 like( $@, 'Wide character in print' );
232 open F, ">:utf8", $a_file;
236 isnt( defined $@, !0 );
244 isnt( defined $@, !0 );
253 isnt( defined $@, !0 );
259 binmode(F, ":bytes");
263 like( $@, 'Wide character in print' );
267 open F, ">:bytes",$a_file; print F "\xde"; close F;
269 open F, "<:bytes", $a_file;
272 is( $b, chr(0x100).chr(0xde), "21395 '.= <>' utf8 vs. bytes" );
277 open F, ">:utf8",$a_file; print F chr 0x100; close F;
279 open F, "<:utf8", $a_file;
282 is( $b, chr(0xde).chr(0x100), "21395 '.= <>' bytes vs. utf8" );
287 my @a = ( [ 0x007F, "bytes" ],
290 [ 0x0100, "utf8" ] );
294 # print "# @$u - @$v\n";
296 binmode(F, ":" . $u->[1]);
297 print F chr($u->[0]);
301 binmode(F, ":" . $u->[1]);
303 my $s = chr($v->[0]);
304 utf8::upgrade($s) if $v->[1] eq "utf8";
307 is( $s, chr($v->[0]) . chr($u->[0]), 'rcatline utf8' );
316 # [perl #23428] Somethings rotten in unicode semantics
319 syswrite(F, $a = chr(0x100));
321 is( ord($a), 0x100, '23428 syswrite should not downgrade scalar' );
322 like( $a, qr/^\w+/, '23428 syswrite should not downgrade scalar' );
325 # sysread() and syswrite() tested in lib/open.t since Fcntl is used
328 # <FH> on a :utf8 stream should complain immediately with -w
329 # if it finds bad UTF-8 (:encoding(utf8) works this way)
332 local $SIG{__WARN__} = sub { $@ = shift };
335 my ($chrE4, $chrF6) = (chr(0xE4), chr(0xF6));
336 if (ord('A') == 193) # EBCDIC
337 { ($chrE4, $chrF6) = (chr(0x43), chr(0xEC)); }
338 print F "foo", $chrE4, "\n";
339 print F "foo", $chrF6, "\n";
341 open F, "<:utf8", $a_file;
344 my ($chrE4, $chrF6) = ("E4", "F6");
345 if (ord('A') == 193) { ($chrE4, $chrF6) = ("43", "EC"); } # EBCDIC
346 like( $@, qr/utf8 "\\x$chrE4" does not map to Unicode .+ <F> line 1/,
347 "<:utf8 readline must warn about bad utf8");
350 like( $@, qr/utf8 "\\x$chrF6" does not map to Unicode .+ <F> line 2/,
351 "<:utf8 rcatline must warn about bad utf8");