1BEGIN { 2 if ($ENV{'PERL_CORE'}){ 3 chdir 't'; 4 unshift @INC, '../lib'; 5 } 6 if (ord("A") == 193) { 7 print "1..0 # Skip: EBCDIC\n"; 8 exit 0; 9 } 10 require Config; import Config; 11 if ($Config{'extensions'} !~ /\bEncode\b/) { 12 print "1..0 # Skip: Encode was not built\n"; 13 exit 0; 14 } 15} 16use strict; 17use Test::More; 18use Encode qw(from_to encode decode 19 encode_utf8 decode_utf8 20 find_encoding is_utf8); 21use charnames qw(greek); 22my @encodings = grep(/iso-?8859/,Encode::encodings()); 23my $n = 2; 24my @character_set = ('0'..'9', 'A'..'Z', 'a'..'z'); 25my @source = qw(ascii iso8859-1 cp1250); 26my @destiny = qw(cp1047 cp37 posix-bc); 27my @ebcdic_sets = qw(cp1047 cp37 posix-bc); 28plan tests => 38+$n*@encodings + 2*@source*@destiny*@character_set + 2*@ebcdic_sets*256 + 6 + 3 + 3*8 + 2; 29 30my $str = join('',map(chr($_),0x20..0x7E)); 31my $cpy = $str; 32is length($str),from_to($cpy,'iso8859-1','Unicode'),"Length Wrong"; 33is $cpy,$str,"ASCII mangled by translating from iso8859-1 to Unicode"; 34$cpy = $str; 35is from_to($cpy,'Unicode','iso8859-1'),length($str),"Length wrong"; 36is $cpy,$str,"ASCII mangled by translating from Unicode to iso8859-1"; 37 38$str = join('',map(chr($_),0xa0..0xff)); 39$cpy = $str; 40is length($str),from_to($cpy,'iso8859-1','Unicode'),"Length Wrong"; 41 42my $sym = Encode->getEncoding('symbol'); 43my $uni = $sym->decode(encode(ascii => 'a')); 44is "\N{alpha}",substr($uni,0,1),"alpha does not map to symbol 'a'"; 45$str = $sym->encode("\N{Beta}"); 46is "B",decode(ascii => substr($str,0,1)),"Symbol 'B' does not map to Beta"; 47 48foreach my $enc (qw(symbol dingbats ascii),@encodings) 49 { 50 my $tab = Encode->getEncoding($enc); 51 is 1,defined($tab),"Could not load $enc"; 52 $str = join('',map(chr($_),0x20..0x7E)); 53 $uni = $tab->decode($str); 54 $cpy = $tab->encode($uni); 55 is $cpy,$str,"$enc mangled translating to Unicode and back"; 56 } 57 58# On ASCII based machines see if we can map several codepoints from 59# three distinct ASCII sets to three distinct EBCDIC coded character sets. 60# On EBCDIC machines see if we can map from three EBCDIC sets to three 61# distinct ASCII sets. 62 63my @expectation = (240..249, 193..201,209..217,226..233, 129..137,145..153,162..169); 64if (ord('A') != 65) { 65 my @temp = @destiny; 66 @destiny = @source; 67 @source = @temp; 68 undef(@temp); 69 @expectation = (48..57, 65..90, 97..122); 70} 71 72foreach my $to (@destiny) 73 { 74 foreach my $from (@source) 75 { 76 my @expected = @expectation; 77 foreach my $chr (@character_set) 78 { 79 my $native_chr = $chr; 80 my $cpy = $chr; 81 my $rc = from_to($cpy,$from,$to); 82 is 1,$rc,"Could not translate from $from to $to"; 83 is ord($cpy),shift(@expected),"mangled translating $native_chr from $from to $to"; 84 } 85 } 86 } 87 88# On either ASCII or EBCDIC machines ensure we can take the full one 89# byte repetoire to EBCDIC sets and back. 90 91my $enc_as = 'iso8859-1'; 92foreach my $enc_eb (@ebcdic_sets) 93 { 94 foreach my $ord (0..255) 95 { 96 $str = chr($ord); 97 my $rc = from_to($str,$enc_as,$enc_eb); 98 $rc += from_to($str,$enc_eb,$enc_as); 99 is $rc,2,"return code for $ord $enc_eb -> $enc_as -> $enc_eb was not obtained"; 100 is $ord,ord($str),"$enc_as mangled translating $ord to $enc_eb and back"; 101 } 102 } 103 104my $mime = find_encoding('iso-8859-2'); 105is defined($mime),1,"Cannot find MIME-ish'iso-8859-2'"; 106my $x11 = find_encoding('iso8859-2'); 107is defined($x11),1,"Cannot find X11-ish 'iso8859-2'"; 108is $mime,$x11,"iso8598-2 and iso-8859-2 not same"; 109my $spc = find_encoding('iso 8859-2'); 110is defined($spc),1,"Cannot find 'iso 8859-2'"; 111is $spc,$mime,"iso 8859-2 and iso-8859-2 not same"; 112 113for my $i (256,128,129,256) 114 { 115 my $c = chr($i); 116 my $s = "$c\n".sprintf("%02X",$i); 117 is utf8::valid($s),1,"concat of $i botched"; 118 utf8::upgrade($s); 119 is utf8::valid($s),1,"concat of $i botched"; 120 } 121 122# Spot check a few points in/out of utf8 123for my $i (ord('A'),128,256,0x20AC) 124 { 125 my $c = chr($i); 126 my $o = encode_utf8($c); 127 is decode_utf8($o),$c,"decode_utf8 not inverse of encode_utf8 for $i"; 128 is encode('utf8',$c),$o,"utf8 encode by name broken for $i"; 129 is decode('utf8',$o),$c,"utf8 decode by name broken for $i"; 130 } 131 132 133# is_utf8 134 135ok( is_utf8("\x{100}")); 136ok(! is_utf8("a")); 137ok(! is_utf8("")); 138"\x{100}" =~ /(.)/; 139ok( is_utf8($1)); # ID 20011127.151 140$a = $1; 141ok( is_utf8($a)); 142$a = "\x{100}"; 143chop $a; 144ok( is_utf8($a)); # weird but true: an empty UTF-8 string 145 146# non-string arguments 147package Encode::Dummy; 148use overload q("") => sub { $_[0]->[0] }; 149sub new { my $class = shift; bless [ @_ ] => $class } 150package main; 151ok(decode(latin1 => Encode::Dummy->new("foobar")), "foobar"); 152ok(encode(utf8 => Encode::Dummy->new("foobar")), "foobar"); 153 154# RT#91569 155# decode_utf8 with non-string arguments 156ok(decode_utf8(*1), "*main::1"); 157 158# hash keys 159foreach my $name ("UTF-16LE", "UTF-8", "Latin1") { 160 my $key = (keys %{{ "whatever\x{CA}" => '' }})[0]; 161 my $kopy = $key; 162 encode($name, $kopy, Encode::FB_CROAK); 163 is $key, "whatever\x{CA}", "encode $name with shared hash key scalars"; 164 undef $key; 165 $key = (keys %{{ "whatever\x{CA}" => '' }})[0]; 166 $kopy = $key; 167 encode($name, $kopy, Encode::FB_CROAK | Encode::LEAVE_SRC); 168 is $key, "whatever\x{CA}", "encode $name with LEAVE_SRC and shared hash key scalars"; 169 undef $key; 170 $key = (keys %{{ "whatever" => '' }})[0]; 171 $kopy = $key; 172 decode($name, $kopy, Encode::FB_CROAK); 173 is $key, "whatever", "decode $name with shared hash key scalars"; 174 undef $key; 175 $key = (keys %{{ "whatever" => '' }})[0]; 176 $kopy = $key; 177 decode($name, $kopy, Encode::FB_CROAK | Encode::LEAVE_SRC); 178 is $key, "whatever", "decode $name with LEAVE_SRC and shared hash key scalars"; 179 180 my $enc = find_encoding($name); 181 undef $key; 182 $key = (keys %{{ "whatever\x{CA}" => '' }})[0]; 183 $kopy = $key; 184 $enc->encode($kopy, Encode::FB_CROAK); 185 is $key, "whatever\x{CA}", "encode obj $name with shared hash key scalars"; 186 undef $key; 187 $key = (keys %{{ "whatever\x{CA}" => '' }})[0]; 188 $kopy = $key; 189 $enc->encode($kopy, Encode::FB_CROAK | Encode::LEAVE_SRC); 190 is $key, "whatever\x{CA}", "encode obj $name with LEAVE_SRC and shared hash key scalars"; 191 undef $key; 192 $key = (keys %{{ "whatever" => '' }})[0]; 193 $kopy = $key; 194 $enc->decode($kopy, Encode::FB_CROAK); 195 is $key, "whatever", "decode obj $name with shared hash key scalars"; 196 undef $key; 197 $key = (keys %{{ "whatever" => '' }})[0]; 198 $kopy = $key; 199 $enc->decode($kopy, Encode::FB_CROAK | Encode::LEAVE_SRC); 200 is $key, "whatever", "decode obj $name with LEAVE_SRC and shared hash key scalars"; 201} 202 203my $latin1 = find_encoding('latin1'); 204my $orig = "\316"; 205$orig =~ /(.)/; 206is $latin1->encode($1), $orig, '[cpan #115168] passing magic regex globals to encode'; 207SKIP: { 208 skip "Perl Version ($]) is older than v5.16", 1 if $] < 5.016; 209 *a = $orig; 210 is $latin1->encode(*a), '*main::'.$orig, '[cpan #115168] passing typeglobs to encode'; 211} 212