Integrate mainline
[p5sagit/p5-mst-13.2.git] / ext / Encode / t / Unicode.t
1 #
2 # $Id: Unicode.t,v 1.2 2002/04/07 17:22:31 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 $nasty      = "\x{004D}\x{0061}\x{1abcd}";
38 my $fallback   = "\x{004D}\x{0061}\x{fffd}";
39
40 #hi: (0x1abcd - 0x10000) / 0x400 + 0xD800 = 0xd82a
41 #lo: (0x1abcd - 0x10000) % 0x400 + 0xDC00 = 0xdfcd
42
43 my $n_16be =
44      pack("C*", map {hex($_)} qw<00 4D 00 61 d8 2a df cd>);
45 my $n_16le =
46      pack("C*", map {hex($_)} qw<4D 00 61 00 2a d8 cd df>);
47 my $f_16be =
48      pack("C*", map {hex($_)} qw<00 4D 00 61 ff fd>);
49 my $f_16le =
50      pack("C*", map {hex($_)} qw<4D 00 61 00 fd ff>);
51 my $n_32be =
52      pack("C*", map {hex($_)} qw<00 00 00 4D 00 00 00 61 00 01 ab cd>);
53 my $n_32le =
54      pack("C*", map {hex($_)} qw<4D 00 00 00 61 00 00 00 cd ab 01 00>);
55
56 my $n_16bb = pack('n', Encode::Unicode::BOM_BE)  . $n_16be;
57 my $n_16lb = pack('n', Encode::Unicode::BOM16LE) . $n_16le;
58 my $n_32bb = pack('N', Encode::Unicode::BOM_BE ) . $n_32be;
59 my $n_32lb = pack('N', Encode::Unicode::BOM32LE) . $n_32le;
60
61 is($n_16be, encode('UTF-16BE', $nasty),  qq{encode UTF-16BE});
62 is($n_16le, encode('UTF-16LE', $nasty),  qq{encode UTF-16LE});
63 is($n_32be, encode('UTF-32BE', $nasty),  qq{encode UTF-32BE});
64 is($n_32le, encode('UTF-32LE', $nasty),  qq{encode UTF-16LE});
65
66 is($nasty,  decode('UTF-16BE', $n_16be), qq{decode UTF-16BE});
67 is($nasty,  decode('UTF-16LE', $n_16le), qq{decode UTF-16LE});
68 is($nasty,  decode('UTF-32BE', $n_32be), qq{decode UTF-32BE});
69 is($nasty,  decode('UTF-32LE', $n_32le), qq{decode UTF-32LE});
70
71 is($n_16bb, encode('UTF-16',   $nasty),  qq{encode UTF-16});
72 is($n_32bb, encode('UTF-32',   $nasty),  qq{encode UTF-32});
73 is($nasty,  decode('UTF-16',   $n_16bb), qq{decode UTF-16, bom=be});
74 is($nasty,  decode('UTF-16',   $n_16lb), qq{decode UTF-16, bom=le});
75 is($nasty,  decode('UTF-32',   $n_32bb), qq{decode UTF-32, bom=be});
76 is($nasty,  decode('UTF-32',   $n_32lb), qq{decode UTF-32, bom=le});
77
78 is(decode('UCS-2BE', $n_16be), $fallback, "decode UCS-2BE: fallback");
79 is(decode('UCS-2LE', $n_16le), $fallback, "decode UCS-2LE: fallback");
80 eval { decode('UCS-2BE', $n_16be, 1) };
81 ok($@=~/^UCS-2BE:/, "decode UCS-2BE: exception");
82 eval { decode('UCS-2LE', $n_16le, 1) };
83 ok($@=~/^UCS-2LE:/, "decode UCS-2LE: exception");
84 is(encode('UCS-2BE', $nasty), $f_16be, "encode UCS-2BE: fallback");
85 is(encode('UCS-2LE', $nasty), $f_16le, "encode UCS-2LE: fallback");
86 eval { encode('UCS-2BE', $nasty, 1) };
87 ok($@=~/^UCS-2BE:/, "encode UCS-2BE: exception");
88 eval { encode('UCS-2LE', $nasty, 1) };
89 ok($@=~/^UCS-2LE:/, "encode UCS-2LE: exception");
90
91 1;
92 __END__