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