Upgrade to Encode 1.34, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / t / Unicode.t
1 #
2 # $Id: Unicode.t,v 1.6 2002/04/12 20:23:05 dankogai Exp dankogai $
3 #
4 # This script is written entirely in ASCII, even though quoted literals
5 # do include non-BMP unicode characters -- Are you happy, jhi?
6 #
7
8 BEGIN {
9     require Config; import Config;
10     if ($Config{'extensions'} !~ /\bEncode\b/) {
11       print "1..0 # Skip: Encode was not built\n";
12       exit 0;
13     }
14 # should work without perlio
15 #     unless (find PerlIO::Layer 'perlio') {
16 #       print "1..0 # Skip: PerlIO was not built\n";
17 #       exit 0;
18 #     }
19 # should work on EBCDIC
20 #    if (ord("A") == 193) {
21 #       print "1..0 # Skip: EBCDIC\n";
22 #       exit 0;
23 #    }
24     $| = 1;
25 }
26
27 use strict;
28 #use Test::More 'no_plan';
29 use Test::More tests => 22;
30 use Encode qw(encode decode);
31
32 #
33 # see
34 # http://www.unicode.org/unicode/reports/tr19/
35 #
36
37 my $dankogai   = "\x{5c0f}\x{98fc}\x{3000}\x{5f3e}";
38 my $nasty      = "$dankogai\x{1abcd}";
39 my $fallback   = "$dankogai\x{fffd}";
40
41 #hi: (0x1abcd - 0x10000) / 0x400 + 0xD800 = 0xd82a
42 #lo: (0x1abcd - 0x10000) % 0x400 + 0xDC00 = 0xdfcd
43
44 my $n_16be = 
45     pack("C*", map {hex($_)} qw<5c 0f 98 fc 30 00 5f 3e  d8 2a df cd>);
46 my $n_16le =
47     pack("C*", map {hex($_)} qw<0f 5c fc 98 00 30 3e 5f  2a d8 cd df>);
48 my $f_16be = 
49     pack("C*", map {hex($_)} qw<5c 0f 98 fc 30 00 5f 3e  ff fd>);
50 my $f_16le =
51     pack("C*", map {hex($_)} qw<0f 5c fc 98 00 30 3e 5f  fd ff>);
52 my $n_32be =
53     pack("C*", map {hex($_)} 
54          qw<00 00 5c 0f 00 00 98 fc 00 00 30 00 00 00 5f 3e  00 01 ab cd>);
55 my $n_32le = 
56     pack("C*", map {hex($_)} 
57          qw<0f 5c 00 00 fc 98 00 00 00 30 00 00 3e 5f 00 00  cd ab 01 00>);
58
59 my $n_16bb = pack('n', 0xFeFF) . $n_16be;
60 my $n_16lb = pack('v', 0xFeFF) . $n_16le;
61 my $n_32bb = pack('N', 0xFeFF) . $n_32be;
62 my $n_32lb = pack('V', 0xFeFF) . $n_32le;
63
64 is($n_16be, encode('UTF-16BE', $nasty),  qq{encode UTF-16BE});
65 is($n_16le, encode('UTF-16LE', $nasty),  qq{encode UTF-16LE});
66 is($n_32be, encode('UTF-32BE', $nasty),  qq{encode UTF-32BE});
67 is($n_32le, encode('UTF-32LE', $nasty),  qq{encode UTF-16LE});
68
69 is($nasty,  decode('UTF-16BE', $n_16be), qq{decode UTF-16BE});
70 is($nasty,  decode('UTF-16LE', $n_16le), qq{decode UTF-16LE});
71 is($nasty,  decode('UTF-32BE', $n_32be), qq{decode UTF-32BE});
72 is($nasty,  decode('UTF-32LE', $n_32le), qq{decode UTF-32LE});
73
74 is($n_16bb, encode('UTF-16',   $nasty),  qq{encode UTF-16});
75 is($n_32bb, encode('UTF-32',   $nasty),  qq{encode UTF-32});
76 is($nasty,  decode('UTF-16',   $n_16bb), qq{decode UTF-16, bom=be});
77 is($nasty,  decode('UTF-16',   $n_16lb), qq{decode UTF-16, bom=le});
78 is($nasty,  decode('UTF-32',   $n_32bb), qq{decode UTF-32, bom=be});
79 is($nasty,  decode('UTF-32',   $n_32lb), qq{decode UTF-32, bom=le});
80
81 is(decode('UCS-2BE', $n_16be), $fallback, "decode UCS-2BE: fallback");
82 is(decode('UCS-2LE', $n_16le), $fallback, "decode UCS-2LE: fallback");
83 eval { decode('UCS-2BE', $n_16be, 1) }; 
84 ok($@=~/^UCS-2BE:/, "decode UCS-2BE: exception");
85 eval { decode('UCS-2LE', $n_16le, 1) }; 
86 ok($@=~/^UCS-2LE:/, "decode UCS-2LE: exception");
87 is(encode('UCS-2BE', $nasty), $f_16be, "encode UCS-2BE: fallback");
88 is(encode('UCS-2LE', $nasty), $f_16le, "encode UCS-2LE: fallback");
89 eval { encode('UCS-2BE', $nasty, 1) }; 
90 ok($@=~/^UCS-2BE:/, "encode UCS-2BE: exception");
91 eval { encode('UCS-2LE', $nasty, 1) }; 
92 ok($@=~/^UCS-2LE:/, "encode UCS-2LE: exception");
93
94 1;
95 __END__