2 require Config; import Config;
3 if ($Config{'extensions'} !~ /\bEncode\b/) {
4 print "1..0 # Skip: Encode was not built\n";
8 print "1..0 # Skip: EBCDIC\n";
11 unless (PerlIO::Layer->find('perlio')){
12 print "1..0 # Skip: PerlIO required\n";
21 use File::Compare qw(compare_text);
25 #use Test::More qw(no_plan);
26 use Test::More tests => 38;
33 @ARGV and $DEBUG = shift;
34 #require Encode::JP::JIS7;
35 #require Encode::KR::2022_KR;
36 #$Encode::JP::JIS7::DEBUG = $DEBUG;
40 my $dir = dirname(__FILE__);
44 jisx0208 => [ qw/euc-jp shiftjis 7bit-jis iso-2022-jp iso-2022-jp-1/],
45 ksc5601 => [ qw/euc-kr/],
46 gb2312 => [ qw/euc-cn hz/],
49 $/ = "\x0a"; # may fix VMS problem for test #28 and #29
51 for my $src (sort keys %e) {
52 my $ufile = File::Spec->catfile($dir,"$src.utf");
53 open my $fh, "<:utf8", $ufile or die "$ufile : $!";
55 my $utext = join('' => @uline);
58 for my $e (@{$e{$src}}){
59 my $sfile = File::Spec->catfile($dir,"$$.sio");
60 my $pfile = File::Spec->catfile($dir,"$$.pio");
62 # first create a file without perlio
63 dump2file($sfile, &encode($e, $utext, 0));
65 # then create a file via perlio without autoflush
68 skip "$e: !perlio_ok", 4 unless (perlio_ok($e) or $DEBUG);
69 no warnings 'uninitialized';
70 open $fh, ">:encoding($e)", $pfile or die "$sfile : $!";
75 is(compare_text($sfile, $pfile), 0 => ">:encoding($e)");
77 copy $sfile, "$sfile.$seq";
78 copy $pfile, "$pfile.$seq";
81 # this time print line by line.
82 # works even for ISO-2022 but not ISO-2022-KR
83 open $fh, ">:encoding($e)", $pfile or die "$sfile : $!";
90 is(compare_text($sfile, $pfile), 0 => ">:encoding($e) by lines");
92 copy $sfile, "$sfile.$seq";
93 copy $pfile, "$pfile.$seq";
96 open $fh, "<:encoding($e)", $pfile or die "$pfile : $!";
98 $dtext = join('' => <$fh>);
101 ok($utext eq $dtext, "<:encoding($e)");
103 dump2file("$sfile.$seq", $utext);
104 dump2file("$pfile.$seq", $dtext);
106 if (perlio_ok($e) or $DEBUG){
108 open $fh, "<:encoding($e)", $pfile or die "$pfile : $!";
109 while(defined(my $l = <$fh>)) {
115 ok($utext eq $dtext, "<:encoding($e) by lines");
117 dump2file("$sfile.$seq", $utext);
118 dump2file("$pfile.$seq", $dtext);
122 1 while unlink ($sfile);
123 1 while unlink ($pfile);
131 my $pev = PerlIO::encoding->VERSION;
132 skip "PerlIO::encoding->VERSION = $pev <= 0.07 ", 6
133 unless ($pev >= 0.07 or $DEBUG);
135 my $file = File::Spec->catfile($dir,"jisx0208.utf");
136 open my $fh, "<:utf8", $file or die "$file : $!";
137 my $str = join('' => <$fh>);
140 'UTF-16BE' => pack('n', 0xFeFF),
141 'UTF-16LE' => pack('v', 0xFeFF),
142 'UTF-32BE' => pack('N', 0xFeFF),
143 'UTF-32LE' => pack('V', 0xFeFF),
146 for my $utf (sort keys %bom){
147 my $bomed = $bom{$utf} . encode($utf, $str);
148 my $sfile = File::Spec->catfile($dir,".${utf}_${seq}_$$");
149 dump2file($sfile, $bomed);
150 my $utf_nobom = $utf; $utf_nobom =~ s/(LE|BE)$//o;
152 open $fh, "<:encoding($utf_nobom)", $sfile or die "$sfile : $!";
153 my $cmp = join '' => <$fh>;
155 is($str, $cmp, "<:encoding($utf_nobom) eq $utf");
156 unlink $sfile; $seq++;
159 for my $utf_nobom (qw/UTF-16 UTF-32/){
160 my $utf = $utf_nobom . 'BE';
161 my $sfile = File::Spec->catfile($dir,".${utf_nobom}_${seq}_$$");
162 my $bomed = $bom{$utf} . encode($utf, $str);
163 open $fh, ">:encoding($utf_nobom)", $sfile or die "$sfile : $!";
166 open my $fh, "<:bytes", $sfile or die "$sfile : $!";
167 read $fh, my $cmp, -s $sfile;
170 ok($bomed eq $cmp, ">:encoding($utf_nobom) eq $utf");
171 unlink $sfile; $seq++;
176 open my $fh, ">", $_[0] or die "$_[0]: $!";