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