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