Supporess spurious warnings for @+ and and @-
[p5sagit/p5-mst-13.2.git] / t / io / utf8.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = '../lib';
6     unless (find PerlIO::Layer 'perlio') {
7         print "1..0 # Skip: not perlio\n";
8         exit 0;
9     }
10 }
11
12 no utf8; # needed for use utf8 not griping about the raw octets
13
14 $| = 1;
15 print "1..31\n";
16
17 open(F,"+>:utf8",'a');
18 print F chr(0x100).'£';
19 print '#'.tell(F)."\n";
20 print "not " unless tell(F) == 4;
21 print "ok 1\n";
22 print F "\n";
23 print '#'.tell(F)."\n";
24 print "not " unless tell(F) >= 5;
25 print "ok 2\n";
26 seek(F,0,0);
27 print "not " unless getc(F) eq chr(0x100);
28 print "ok 3\n";
29 print "not " unless getc(F) eq "£";
30 print "ok 4\n";
31 print "not " unless getc(F) eq "\n";
32 print "ok 5\n";
33 seek(F,0,0);
34 binmode(F,":bytes");
35 my $chr = chr(0xc4);
36 if (ord('A') == 193) { $chr = chr(0x8c); } # EBCDIC
37 print "not " unless getc(F) eq $chr;
38 print "ok 6\n";
39 $chr = chr(0x80);
40 if (ord('A') == 193) { $chr = chr(0x41); } # EBCDIC
41 print "not " unless getc(F) eq $chr;
42 print "ok 7\n";
43 $chr = chr(0xc2);
44 if (ord('A') == 193) { $chr = chr(0x80); } # EBCDIC
45 print "not " unless getc(F) eq $chr;
46 print "ok 8\n";
47 $chr = chr(0xa3);
48 if (ord('A') == 193) { $chr = chr(0x44); } # EBCDIC
49 print "not " unless getc(F) eq $chr;
50 print "ok 9\n";
51 print "not " unless getc(F) eq "\n";
52 print "ok 10\n";
53 seek(F,0,0);
54 binmode(F,":utf8");
55 print "not " unless scalar(<F>) eq "\x{100}£\n";
56 print "ok 11\n";
57 seek(F,0,0);
58 $buf = chr(0x200);
59 $count = read(F,$buf,2,1);
60 print "not " unless $count == 2;
61 print "ok 12\n";
62 print "not " unless $buf eq "\x{200}\x{100}£";
63 print "ok 13\n";
64 close(F);
65
66 {
67     $a = chr(300); # This *is* UTF-encoded
68     $b = chr(130); # This is not.
69     
70     open F, ">:utf8", 'a' or die $!;
71     print F $a,"\n";
72     close F;
73     
74     open F, "<:utf8", 'a' or die $!;
75     $x = <F>;
76     chomp($x);
77     print "not " unless $x eq chr(300);
78     print "ok 14\n";
79     
80     open F, "a" or die $!; # Not UTF
81     binmode(F, ":bytes");
82     $x = <F>;
83     chomp($x);
84     $chr = chr(196).chr(172);
85     if (ord('A') == 193) { $chr = chr(141).chr(83); } # EBCDIC
86     print "not " unless $x eq $chr;
87     print "ok 15\n";
88     close F;
89     
90     open F, ">:utf8", 'a' or die $!;
91     binmode(F);  # we write a "\n" and then tell() - avoid CRLF issues.
92     print F $a;
93     my $y;
94     { my $x = tell(F);
95       { use bytes; $y = length($a);}
96       print "not " unless $x == $y;
97       print "ok 16\n";
98   }
99     
100     { # Check byte length of $b
101         use bytes; my $y = length($b);
102         print "not " unless $y == 1;
103         print "ok 17\n";
104     }
105     
106     print F $b,"\n"; # Don't upgrades $b
107     
108     { # Check byte length of $b
109         use bytes; my $y = length($b);
110         print "not ($y) " unless $y == 1;
111         print "ok 18\n";
112     }
113     
114     {
115         my $x = tell(F);
116         { use bytes; if (ord('A')==193){$y += 2;}else{$y += 3;}} # EBCDIC ASCII
117         print "not ($x,$y) " unless $x == $y;
118         print "ok 19\n";
119     }
120     
121     close F;
122     
123     open F, "a" or die $!; # Not UTF
124     binmode(F, ":bytes");
125     $x = <F>;
126     chomp($x);
127     $chr = v196.172.194.130;
128     if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
129     printf "not (%vd) ", $x unless $x eq $chr;
130     print "ok 20\n";
131     
132     open F, "<:utf8", "a" or die $!;
133     $x = <F>;
134     chomp($x);
135     close F;
136     printf "not (%vd) ", $x unless $x eq chr(300).chr(130);
137     print "ok 21\n";
138     
139     open F, ">", "a" or die $!;
140     if (${^OPEN} =~ /:utf8/) {
141         binmode(F, ":bytes:");
142     }
143
144     # Now let's make it suffer.
145     my $w;
146     {
147         use warnings 'utf8';
148         local $SIG{__WARN__} = sub { $w = $_[0] };
149         print F $a;
150         print "not " if ($@ || $w !~ /Wide character in print/i);
151     }
152     print "ok 22\n";
153 }
154
155 # Hm. Time to get more evil.
156 open F, ">:utf8", "a" or die $!;
157 print F $a;
158 binmode(F, ":bytes");
159 print F chr(130)."\n";
160 close F;
161  
162 open F, "<", "a" or die $!;
163 binmode(F, ":bytes");
164 $x = <F>; chomp $x;
165 $chr = v196.172.130;
166 if (ord('A') == 193) { $chr = v141.83.130; } # EBCDIC
167 print "not " unless $x eq $chr;
168 print "ok 23\n";
169
170 # Right.
171 open F, ">:utf8", "a" or die $!;
172 print F $a;
173 close F;
174 open F, ">>", "a" or die $!;
175 print F chr(130)."\n";
176 close F;
177
178 open F, "<", "a" or die $!;
179 $x = <F>; chomp $x;
180 print "not " unless $x eq $chr;
181 print "ok 24\n";
182
183 # Now we have a deformed file.
184
185 if (ord('A') == 193) {
186     print "ok 25 # Skip: EBCDIC\n"; # EBCDIC doesn't complain
187 } else {
188     open F, "<:utf8", "a" or die $!;
189     $x = <F>; chomp $x;
190     local $SIG{__WARN__} = sub { print "ok 25\n" };
191     eval { sprintf "%vd\n", $x };
192 }
193
194 close F;
195 unlink('a');
196
197 open F, ">:utf8", "a";
198 @a = map { chr(1 << ($_ << 2)) } 0..5; # 0x1, 0x10, .., 0x100000
199 unshift @a, chr(0); # ... and a null byte in front just for fun
200 print F @a;
201 close F;
202
203 my $c;
204
205 # read() should work on characters, not bytes
206 open F, "<:utf8", "a";
207 $a = 0;
208 for (@a) {
209     unless (($c = read(F, $b, 1) == 1)  &&
210             length($b)           == 1  &&
211             ord($b)              == ord($_) &&
212             tell(F)              == ($a += bytes::length($b))) {
213         print '# ord($_)           == ', ord($_), "\n";
214         print '# ord($b)           == ', ord($b), "\n";
215         print '# length($b)        == ', length($b), "\n";
216         print '# bytes::length($b) == ', bytes::length($b), "\n";
217         print '# tell(F)           == ', tell(F), "\n";
218         print '# $a                == ', $a, "\n";
219         print '# $c                == ', $c, "\n";
220         print "not ";
221         last;
222     }
223 }
224 close F;
225 print "ok 26\n";
226
227 {
228     # Check that warnings are on on I/O, and that they can be muffled.
229
230     local $SIG{__WARN__} = sub { $@ = shift };
231
232     undef $@;
233     open F, ">a";
234     binmode(F, ":bytes");
235     print F chr(0x100);
236     close(F);
237
238     print $@ =~ /Wide character in print/ ? "ok 27\n" : "not ok 27\n";
239
240     undef $@;
241     open F, ">:utf8", "a";
242     print F chr(0x100);
243     close(F);
244
245     print defined $@ ? "not ok 28\n" : "ok 28\n";
246
247     undef $@;
248     open F, ">a";
249     binmode(F, ":utf8");
250     print F chr(0x100);
251     close(F);
252
253     print defined $@ ? "not ok 29\n" : "ok 29\n";
254
255     no warnings 'utf8';
256
257     undef $@;
258     open F, ">a";
259     print F chr(0x100);
260     close(F);
261
262     print defined $@ ? "not ok 30\n" : "ok 30\n";
263
264     use warnings 'utf8';
265
266     undef $@;
267     open F, ">a";
268     binmode(F, ":bytes");
269     print F chr(0x100);
270     close(F);
271
272     print $@ =~ /Wide character in print/ ? "ok 31\n" : "not ok 31\n";
273 }
274
275 # sysread() and syswrite() tested in lib/open.t since Fnctl is used
276
277 END {
278     1 while unlink "a";
279     1 while unlink "b";
280 }
281