Remove pseudo-hashes (complete)
[p5sagit/p5-mst-13.2.git] / ext / Encode / t / Encode.t
CommitLineData
656753f8 1BEGIN {
037b88d6 2 if ($ENV{'PERL_CORE'}){
3 chdir 't';
4 unshift @INC, '../lib';
5 }
5a814814 6 require Config; import Config;
bb50757b 7 if ($Config{'extensions'} !~ /\bEncode\b/) {
5a814814 8 print "1..0 # Skip: Encode was not built\n";
9 exit 0;
10 }
656753f8 11}
0e567a6c 12use strict;
656753f8 13use Test;
2eebba1d 14use Encode qw(from_to encode decode
15 encode_utf8 decode_utf8
16 find_encoding is_utf8);
656753f8 17use charnames qw(greek);
51ef4e11 18my @encodings = grep(/iso-?8859/,Encode::encodings());
656753f8 19my $n = 2;
f90497d6 20my @character_set = ('0'..'9', 'A'..'Z', 'a'..'z');
21my @source = qw(ascii iso8859-1 cp1250);
22my @destiny = qw(cp1047 cp37 posix-bc);
23my @ebcdic_sets = qw(cp1047 cp37 posix-bc);
2eebba1d 24plan test => 38+$n*@encodings + 2*@source*@destiny*@character_set + 2*@ebcdic_sets*256 + 6;
656753f8 25my $str = join('',map(chr($_),0x20..0x7E));
26my $cpy = $str;
27ok(length($str),from_to($cpy,'iso8859-1','Unicode'),"Length Wrong");
28ok($cpy,$str,"ASCII mangled by translating from iso8859-1 to Unicode");
29$cpy = $str;
30ok(from_to($cpy,'Unicode','iso8859-1'),length($str),"Length wrong");
31ok($cpy,$str,"ASCII mangled by translating from Unicode to iso8859-1");
32
33$str = join('',map(chr($_),0xa0..0xff));
87714904 34$cpy = $str;
656753f8 35ok(length($str),from_to($cpy,'iso8859-1','Unicode'),"Length Wrong");
36
37my $sym = Encode->getEncoding('symbol');
7d85a32c 38my $uni = $sym->decode(encode(ascii => 'a'));
f90497d6 39ok("\N{alpha}",substr($uni,0,1),"alpha does not map to symbol 'a'");
50d26985 40$str = $sym->encode("\N{Beta}");
7d85a32c 41ok("B",decode(ascii => substr($str,0,1)),"Symbol 'B' does not map to Beta");
656753f8 42
43foreach my $enc (qw(symbol dingbats ascii),@encodings)
44 {
45 my $tab = Encode->getEncoding($enc);
46 ok(1,defined($tab),"Could not load $enc");
47 $str = join('',map(chr($_),0x20..0x7E));
50d26985 48 $uni = $tab->decode($str);
49 $cpy = $tab->encode($uni);
656753f8 50 ok($cpy,$str,"$enc mangled translating to Unicode and back");
51 }
52
f90497d6 53# On ASCII based machines see if we can map several codepoints from
54# three distinct ASCII sets to three distinct EBCDIC coded character sets.
a12c0f56 55# On EBCDIC machines see if we can map from three EBCDIC sets to three
f90497d6 56# distinct ASCII sets.
57
58my @expectation = (240..249, 193..201,209..217,226..233, 129..137,145..153,162..169);
59if (ord('A') != 65) {
60 my @temp = @destiny;
61 @destiny = @source;
62 @source = @temp;
63 undef(@temp);
64 @expectation = (48..57, 65..90, 97..122);
65}
66
67foreach my $to (@destiny)
68 {
69 foreach my $from (@source)
70 {
71 my @expected = @expectation;
72 foreach my $chr (@character_set)
73 {
74 my $native_chr = $chr;
75 my $cpy = $chr;
76 my $rc = from_to($cpy,$from,$to);
77 ok(1,$rc,"Could not translate from $from to $to");
78 ok(ord($cpy),shift(@expected),"mangled translating $native_chr from $from to $to");
79 }
80 }
81 }
82
83# On either ASCII or EBCDIC machines ensure we can take the full one
84# byte repetoire to EBCDIC sets and back.
85
86my $enc_as = 'iso8859-1';
87foreach my $enc_eb (@ebcdic_sets)
88 {
89 foreach my $ord (0..255)
90 {
91 $str = chr($ord);
92 my $rc = from_to($str,$enc_as,$enc_eb);
93 $rc += from_to($str,$enc_eb,$enc_as);
94 ok($rc,2,"return code for $ord $enc_eb -> $enc_as -> $enc_eb was not obtained");
95 ok($ord,ord($str),"$enc_as mangled translating $ord to $enc_eb and back");
96 }
97 }
98
51ef4e11 99my $mime = find_encoding('iso-8859-2');
100ok(defined($mime),1,"Cannot find MIME-ish'iso-8859-2'");
101my $x11 = find_encoding('iso8859-2');
102ok(defined($x11),1,"Cannot find X11-ish 'iso8859-2'");
103ok($mime,$x11,"iso8598-2 and iso-8859-2 not same");
104my $spc = find_encoding('iso 8859-2');
105ok(defined($spc),1,"Cannot find 'iso 8859-2'");
106ok($spc,$mime,"iso 8859-2 and iso-8859-2 not same");
107
4411f3b6 108for my $i (256,128,129,256)
a12c0f56 109 {
110 my $c = chr($i);
111 my $s = "$c\n".sprintf("%02X",$i);
1b026014 112 ok(utf8::valid($s),1,"concat of $i botched");
113 utf8::upgrade($s);
114 ok(utf8::valid($s),1,"concat of $i botched");
a12c0f56 115 }
116
4411f3b6 117# Spot check a few points in/out of utf8
ed46d110 118for my $i (ord('A'),128,256,0x20AC)
4411f3b6 119 {
120 my $c = chr($i);
121 my $o = encode_utf8($c);
122 ok(decode_utf8($o),$c,"decode_utf8 not inverse of encode_utf8 for $i");
123 ok(encode('utf8',$c),$o,"utf8 encode by name broken for $i");
124 ok(decode('utf8',$o),$c,"utf8 decode by name broken for $i");
125 }
126
127
2eebba1d 128# is_utf8
129
130ok( is_utf8("\x{100}"));
131ok(! is_utf8("a"));
132ok(! is_utf8(""));
133"\x{100}" =~ /(.)/;
134ok( is_utf8($1)); # ID 20011127.151
135$a = $1;
136ok( is_utf8($a));
137$a = "\x{100}";
138chop $a;
139ok( is_utf8($a)); # weird but true: an empty UTF-8 string
140