This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix open.pm to work via XS-implemented method calls rather
[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
3ba0e062
JH
12no utf8; # so that the naked 8-bit chars won't gripe under use utf8
13
7d59b7e4 14$| = 1;
d2f5bb60
PP
15my $total_tests = 25;
16if (ord('A') == 193) { $total_tests = 24; } # EBCDIC platforms do not warn on UTF-8
17print "1..$total_tests\n";
7d59b7e4
NIS
18
19open(F,"+>:utf8",'a');
20print F chr(0x100).'£';
21print '#'.tell(F)."\n";
22print "not " unless tell(F) == 4;
23print "ok 1\n";
24print F "\n";
25print '#'.tell(F)."\n";
26print "not " unless tell(F) >= 5;
27print "ok 2\n";
28seek(F,0,0);
29print "not " unless getc(F) eq chr(0x100);
30print "ok 3\n";
31print "not " unless getc(F) eq "£";
32print "ok 4\n";
33print "not " unless getc(F) eq "\n";
34print "ok 5\n";
35seek(F,0,0);
36binmode(F,":bytes");
d2f5bb60
PP
37my $chr = chr(0xc4);
38if (ord('A') == 193) { $chr = chr(0x8c); } # EBCDIC
39print "not " unless getc(F) eq $chr;
7d59b7e4 40print "ok 6\n";
d2f5bb60
PP
41$chr = chr(0x80);
42if (ord('A') == 193) { $chr = chr(0x41); } # EBCDIC
43print "not " unless getc(F) eq $chr;
7d59b7e4 44print "ok 7\n";
d2f5bb60
PP
45$chr = chr(0xc2);
46if (ord('A') == 193) { $chr = chr(0x80); } # EBCDIC
47print "not " unless getc(F) eq $chr;
7d59b7e4 48print "ok 8\n";
d2f5bb60
PP
49$chr = chr(0xa3);
50if (ord('A') == 193) { $chr = chr(0x44); } # EBCDIC
51print "not " unless getc(F) eq $chr;
7d59b7e4
NIS
52print "ok 9\n";
53print "not " unless getc(F) eq "\n";
54print "ok 10\n";
55seek(F,0,0);
56binmode(F,":utf8");
57print "not " unless scalar(<F>) eq "\x{100}£\n";
58print "ok 11\n";
eb5c063a
NIS
59seek(F,0,0);
60$buf = chr(0x200);
61$count = read(F,$buf,2,1);
62print "not " unless $count == 2;
63print "ok 12\n";
64print "not " unless $buf eq "\x{200}\x{100}£";
65print "ok 13\n";
7d59b7e4
NIS
66close(F);
67
360eb788
NIS
68{
69$a = chr(300); # This *is* UTF-encoded
70$b = chr(130); # This is not.
71
72open F, ">:utf8", 'a' or die $!;
73print F $a,"\n";
74close F;
75
76open F, "<:utf8", 'a' or die $!;
77$x = <F>;
78chomp($x);
79print "not " unless $x eq chr(300);
80print "ok 14\n";
81
82open F, "a" or die $!; # Not UTF
83$x = <F>;
84chomp($x);
d2f5bb60
PP
85$chr = chr(196).chr(172);
86if (ord('A') == 193) { $chr = chr(141).chr(83); } # EBCDIC
87print "not " unless $x eq $chr;
360eb788
NIS
88print "ok 15\n";
89close F;
90
91open F, ">:utf8", 'a' or die $!;
79086a00 92binmode(F); # we write a "\n" and then tell() - avoid CRLF issues.
360eb788
NIS
93print F $a;
94my $y;
f6c77cf1 95{ my $x = tell(F);
360eb788
NIS
96 { use bytes; $y = length($a);}
97 print "not " unless $x == $y;
98 print "ok 16\n";
99}
100
101{ # Check byte length of $b
102use bytes; my $y = length($b);
103print "not " unless $y == 1;
104print "ok 17\n";
105}
106
f9a63242 107print F $b,"\n"; # Don't upgrades $b
360eb788
NIS
108
109{ # Check byte length of $b
110use bytes; my $y = length($b);
f9a63242 111print "not ($y) " unless $y == 1;
360eb788
NIS
112print "ok 18\n";
113}
114
f6c77cf1 115{ my $x = tell(F);
d2f5bb60 116 { use bytes; if (ord('A')==193){$y += 2;}else{$y += 3;}} # EBCDIC ASCII
f9a63242 117 print "not ($x,$y) " unless $x == $y;
360eb788
NIS
118 print "ok 19\n";
119}
120
121close F;
122
123open F, "a" or die $!; # Not UTF
124$x = <F>;
125chomp($x);
d2f5bb60
PP
126$chr = v196.172.194.130;
127if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
128printf "not (%vd) ", $x unless $x eq $chr;
360eb788
NIS
129print "ok 20\n";
130
131open F, "<:utf8", "a" or die $!;
132$x = <F>;
133chomp($x);
134close F;
f9a63242 135printf "not (%vd) ", $x unless $x eq chr(300).chr(130);
360eb788
NIS
136print "ok 21\n";
137
138# Now let's make it suffer.
139open F, ">", "a" or die $!;
ae798467
NIS
140my $w;
141eval {local $SIG{__WARN__} = sub { $w = $_[0] }; print F $a; };
142print "not " if ($@ || $w !~ /Wide character in print/i);
360eb788
NIS
143print "ok 22\n";
144}
145
146# Hm. Time to get more evil.
147open F, ">:utf8", "a" or die $!;
148print F $a;
149binmode(F, ":bytes");
150print F chr(130)."\n";
151close F;
152
153open F, "<", "a" or die $!;
154$x = <F>; chomp $x;
d2f5bb60
PP
155$chr = v196.172.130;
156if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
157print "not " unless $x eq $chr;
360eb788
NIS
158print "ok 23\n";
159
160# Right.
161open F, ">:utf8", "a" or die $!;
162print F $a;
163close F;
164open F, ">>", "a" or die $!;
165print F chr(130)."\n";
166close F;
167
168open F, "<", "a" or die $!;
169$x = <F>; chomp $x;
d2f5bb60 170print "not " unless $x eq $chr;
360eb788
NIS
171print "ok 24\n";
172
173# Now we have a deformed file.
174open F, "<:utf8", "a" or die $!;
175$x = <F>; chomp $x;
176{ local $SIG{__WARN__} = sub { print "ok 25\n"; };
177eval { sprintf "%vd\n", $x; }
178}
179
180unlink('a');
7d59b7e4 181