xref: /openbsd/gnu/usr.bin/perl/cpan/Encode/t/Encode.t (revision a6445c1d)
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;
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 test => 38+$n*@encodings + 2*@source*@destiny*@character_set + 2*@ebcdic_sets*256 + 6 + 5;
29my $str = join('',map(chr($_),0x20..0x7E));
30my $cpy = $str;
31ok(length($str),from_to($cpy,'iso8859-1','Unicode'),"Length Wrong");
32ok($cpy,$str,"ASCII mangled by translating from iso8859-1 to Unicode");
33$cpy = $str;
34ok(from_to($cpy,'Unicode','iso8859-1'),length($str),"Length wrong");
35ok($cpy,$str,"ASCII mangled by translating from Unicode to iso8859-1");
36
37$str = join('',map(chr($_),0xa0..0xff));
38$cpy = $str;
39ok(length($str),from_to($cpy,'iso8859-1','Unicode'),"Length Wrong");
40
41my $sym = Encode->getEncoding('symbol');
42my $uni = $sym->decode(encode(ascii => 'a'));
43ok("\N{alpha}",substr($uni,0,1),"alpha does not map to symbol 'a'");
44$str = $sym->encode("\N{Beta}");
45ok("B",decode(ascii => substr($str,0,1)),"Symbol 'B' does not map to Beta");
46
47foreach my $enc (qw(symbol dingbats ascii),@encodings)
48 {
49  my $tab = Encode->getEncoding($enc);
50  ok(1,defined($tab),"Could not load $enc");
51  $str = join('',map(chr($_),0x20..0x7E));
52  $uni = $tab->decode($str);
53  $cpy = $tab->encode($uni);
54  ok($cpy,$str,"$enc mangled translating to Unicode and back");
55 }
56
57# On ASCII based machines see if we can map several codepoints from
58# three distinct ASCII sets to three distinct EBCDIC coded character sets.
59# On EBCDIC machines see if we can map from three EBCDIC sets to three
60# distinct ASCII sets.
61
62my @expectation = (240..249, 193..201,209..217,226..233, 129..137,145..153,162..169);
63if (ord('A') != 65) {
64    my @temp = @destiny;
65    @destiny = @source;
66    @source = @temp;
67    undef(@temp);
68    @expectation = (48..57, 65..90, 97..122);
69}
70
71foreach my $to (@destiny)
72 {
73  foreach my $from (@source)
74   {
75    my @expected = @expectation;
76    foreach my $chr (@character_set)
77     {
78      my $native_chr = $chr;
79      my $cpy = $chr;
80      my $rc = from_to($cpy,$from,$to);
81      ok(1,$rc,"Could not translate from $from to $to");
82      ok(ord($cpy),shift(@expected),"mangled translating $native_chr from $from to $to");
83     }
84   }
85 }
86
87# On either ASCII or EBCDIC machines ensure we can take the full one
88# byte repetoire to EBCDIC sets and back.
89
90my $enc_as = 'iso8859-1';
91foreach my $enc_eb (@ebcdic_sets)
92 {
93  foreach my $ord (0..255)
94   {
95    $str = chr($ord);
96    my $rc = from_to($str,$enc_as,$enc_eb);
97    $rc += from_to($str,$enc_eb,$enc_as);
98    ok($rc,2,"return code for $ord $enc_eb -> $enc_as -> $enc_eb was not obtained");
99    ok($ord,ord($str),"$enc_as mangled translating $ord to $enc_eb and back");
100   }
101 }
102
103my $mime = find_encoding('iso-8859-2');
104ok(defined($mime),1,"Cannot find MIME-ish'iso-8859-2'");
105my $x11 = find_encoding('iso8859-2');
106ok(defined($x11),1,"Cannot find X11-ish 'iso8859-2'");
107ok($mime,$x11,"iso8598-2 and iso-8859-2 not same");
108my $spc = find_encoding('iso 8859-2');
109ok(defined($spc),1,"Cannot find 'iso 8859-2'");
110ok($spc,$mime,"iso 8859-2 and iso-8859-2 not same");
111
112for my $i (256,128,129,256)
113 {
114  my $c = chr($i);
115  my $s = "$c\n".sprintf("%02X",$i);
116  ok(utf8::valid($s),1,"concat of $i botched");
117  utf8::upgrade($s);
118  ok(utf8::valid($s),1,"concat of $i botched");
119 }
120
121# Spot check a few points in/out of utf8
122for my $i (ord('A'),128,256,0x20AC)
123 {
124  my $c = chr($i);
125  my $o = encode_utf8($c);
126  ok(decode_utf8($o),$c,"decode_utf8 not inverse of encode_utf8 for $i");
127  ok(encode('utf8',$c),$o,"utf8 encode by name broken for $i");
128  ok(decode('utf8',$o),$c,"utf8 decode by name broken for $i");
129 }
130
131
132# is_utf8
133
134ok(  is_utf8("\x{100}"));
135ok(! is_utf8("a"));
136ok(! is_utf8(""));
137"\x{100}" =~ /(.)/;
138ok(  is_utf8($1)); # ID 20011127.151
139$a = $1;
140ok(  is_utf8($a));
141$a = "\x{100}";
142chop $a;
143ok(  is_utf8($a)); # weird but true: an empty UTF-8 string
144
145# non-string arguments
146package Encode::Dummy;
147use overload q("") => sub { $_[0]->[0] };
148sub new { my $class = shift; bless [ @_  ] => $class }
149package main;
150ok(decode(latin1 => Encode::Dummy->new("foobar")), "foobar");
151ok(encode(utf8   => Encode::Dummy->new("foobar")), "foobar");
152
153# RT#91569
154# decode_utf8 with non-string arguments
155ok(decode_utf8(*1), "*main::1");
156
157# hash keys
158my $key = (keys %{{ "whatever\x{100}" => '' }})[0];
159my $kopy = $key;
160encode("UTF-16LE", $kopy, Encode::FB_CROAK);
161ok $key, "whatever\x{100}", 'encode with shared hash key scalars';
162undef $key;
163$key = (keys %{{ "whatever" => '' }})[0];
164$kopy = $key;
165decode("UTF-16LE", $kopy, Encode::FB_CROAK);
166ok $key, "whatever", 'decode with shared hash key scalars';
167