4 unless (find PerlIO::Layer 'perlio') {
5 print "1..0 # Skip: not perlio\n";
8 unless (eval { require Encode } ) {
9 print "1..0 # Skip: not Encode\n";
14 use Test::More tests => 24;
20 my $russki = "koi8r$$";
21 my $threebyte = "3byte$$";
23 if (open(GRK, ">$grk")) {
24 binmode(GRK, ":bytes");
25 # alpha beta gamma in ISO 8859-7
26 print GRK "\xe1\xe2\xe3";
27 close GRK or die "Could not close: $!";
31 is(open(my $i,'<:encoding(iso-8859-7)',$grk), 1);
32 is(open(my $o,'>:utf8',$utf), 1);
33 is((print $o readline $i), 1);
34 close($o) or die "Could not close: $!";
38 if (open(UTF, "<$utf")) {
39 binmode(UTF, ":bytes");
40 if (ord('A') == 193) { # EBCDIC
41 # alpha beta gamma in UTF-EBCDIC Unicode (0x3b1 0x3b2 0x3b3)
42 is(scalar <UTF>, "\xb4\x58\xb4\x59\xb4\x62");
44 # alpha beta gamma in UTF-8 Unicode (0x3b1 0x3b2 0x3b3)
45 is(scalar <UTF>, "\xce\xb1\xce\xb2\xce\xb3");
52 is (open(my $i,'<:utf8',$utf), 1);
53 is (open(my $o,'>:encoding(iso-8859-7)',$grk), 1);
54 is ((scalar print $o readline $i), 1);
55 close($o) or die "Could not close: $!";
59 if (open(GRK, "<$grk")) {
60 binmode(GRK, ":bytes");
61 is(scalar <GRK>, "\xe1\xe2\xe3");
65 $SIG{__WARN__} = sub {$warn .= $_[0]};
67 is (open(FAIL, ">:encoding(NoneSuch)", $fail1), undef, 'Open should fail');
68 like($warn, qr/^Cannot find encoding "NoneSuch" at/);
70 is(open(RUSSKI, ">$russki"), 1);
71 print RUSSKI "\x3c\x3f\x78";
72 close RUSSKI or die "Could not close: $!";
73 open(RUSSKI, "$russki");
74 binmode(RUSSKI, ":raw");
76 read(RUSSKI, $buf1, 1);
78 binmode(RUSSKI, ":encoding(koi8-r)");
80 read(RUSSKI, $buf2, 1);
81 my $offset = tell(RUSSKI);
83 is(ord $buf2, (ord('A') == 193) ? 0x6f : 0x3f);
89 # Check there is no Use of uninitialized value in concatenation (.) warning
90 # due to the way @latin2iso_num was used to make aliases.
91 is(open(FAIL, ">:encoding(latin42)", $fail2), undef, 'Open should fail');
93 like($warn, qr/^Cannot find encoding "latin42" at.*line \d+\.$/);
95 # Create a string of chars that are 3 bytes in UTF-8
96 my $str = "\x{1f80}" x 2048;
98 # Write them to a file
99 open(F,'>:utf8',$threebyte) || die "Cannot open $threebyte:$!";
103 # Read file back as UTF-8
104 open(F,'<:encoding(utf-8)',$threebyte) || die "Cannot open $threebyte:$!";
109 # Try decoding some bad stuff
110 open(F,'>:raw',$threebyte) || die "Cannot open $threebyte:$!";
111 if (ord('A') == 193) { # EBCDIC
112 print F "foo\x8c\x80\x80\x80bar\n\x80foo\n";
114 print F "foo\xF0\x80\x80\x80bar\n\x80foo\n";
118 open(F,'<:encoding(utf-8)',$threebyte) || die "Cannot open $threebyte:$!";
119 $dstr = join(":", <F>);
121 if (ord('A') == 193) { # EBCDIC
122 is($dstr, "foo\\x8C\\x80\\x80\\x80bar\n:\\x80foo\n");
124 is($dstr, "foo\\xF0\\x80\\x80\\x80bar\n:\\x80foo\n");
127 # Check that PerlIO::encoding can handle custom encodings that do funny
128 # things with the buffer.
129 use Encode::Encoding;
131 @ISA = Encode::Encoding;
132 __PACKAGE__->Define('extensive');
134 my ($self,$buf,$chk) = @_;
136 if ($buf =~ /(.*\n)(?!\z)/) {
142 my @x = (' ') x 8000; # reuse the just-freed buffer
143 $_[1] = $leftovers; # SvPVX now points elsewhere and is shorter
150 open my $fh, ">:encoding(extensive)", \$buf;
152 print $fh "doughnut\n";
153 print $fh "quaffee\n";
154 # Print something longer than the buffer that encode() shrunk:
155 print $fh "The beech leaves beech leaves on the beach by the beech.\n";
157 is $buf, "doughnut\nquaffee\nThe beech leaves beech leaves on the beach by"
158 ." the beech.\n", 'buffer realloc during encoding';
159 $buf = "Sheila surely shod Sean\nin shoddy shoes.\n";
160 open $fh, "<:encoding(extensive)", \$buf;
161 is join("", <$fh>), "Sheila surely shod Sean\nin shoddy shoes.\n",
162 'buffer realloc during decoding';
165 @ISA = Encode::Encoding;
166 __PACKAGE__->Define('cower');
168 my ($self,$buf,$chk) = @_;
170 if ($buf =~ /(.*\n)(?!\z)/) {
175 no warnings; # stupid @_[1] warning
176 @_[1] = keys %{{$leftovers=>1}}; # shared hash key (copy-on-write)
183 open $fh, ">:encoding(cower)", \$buf;
185 print $fh $_ for qw "pumping plum pits";
187 is $buf, "pumpingplumpits", 'cowing buffer during encoding';
188 $buf = "pumping\nplum\npits\n";
189 open $fh, "<:encoding(cower)", \$buf;
190 is join("", <$fh>), "pumping\nplum\npits\n",
191 'cowing buffer during decoding';
195 @ISA = Encode::Encoding;
196 __PACKAGE__->Define('globber');
198 my ($self,$buf,$chk) = @_;
199 $_[1] = *foo if $chk;
205 # Here we just want to test there is no crash. The actual output is not so
207 # We need a double eval, as scope unwinding will close the handle,
210 open my $fh, ">:encoding(globber)", \$buf;
211 print $fh "Agathopous Goodfoot\n";
214 like $@||$e, qr/Close with partial character/,
215 'no crash when assigning glob to buffer in encode';
216 $buf = "To hymn him who heard her herd herd\n";
217 open $fh, "<:encoding(globber)", \$buf;
220 is $x, "To hymn him who heard her herd herd\n",
221 'no crash when assigning glob to buffer in decode';
224 1 while unlink($grk, $utf, $fail1, $fail2, $russki, $threebyte);