code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM437',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM437',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM437');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM437',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e4
0x85	0x00e0
0x86	0x00e5
0x87	0x00e7
0x88	0x00ea
0x89	0x00eb
0x8a	0x00e8
0x8b	0x00ef
0x8c	0x00ee
0x8d	0x00ec
0x8e	0x00c4
0x8f	0x00c5
0x90	0x00c9
0x91	0x00e6
0x92	0x00c6
0x93	0x00f4
0x94	0x00f6
0x95	0x00f2
0x96	0x00fb
0x97	0x00f9
0x98	0x00ff
0x99	0x00d6
0x9a	0x00dc
0x9b	0x00a2
0x9c	0x00a3
0x9d	0x00a5
0x9e	0x20a7
0x9f	0x0192
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00f1
0xa5	0x00d1
0xa6	0x00aa
0xa7	0x00ba
0xa8	0x00bf
0xa9	0x2310
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00a1
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x2561
0xb6	0x2562
0xb7	0x2556
0xb8	0x2555
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x255c
0xbe	0x255b
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x255e
0xc7	0x255f
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x2567
0xd0	0x2568
0xd1	0x2564
0xd2	0x2565
0xd3	0x2559
0xd4	0x2558
0xd5	0x2552
0xd6	0x2553
0xd7	0x256b
0xd8	0x256a
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x258c
0xde	0x2590
0xdf	0x2580
0xe0	0x03b1
0xe1	0x00df
0xe2	0x0393
0xe3	0x03c0
0xe4	0x03a3
0xe5	0x03c3
0xe6	0x00b5
0xe7	0x03c4
0xe8	0x03a6
0xe9	0x0398
0xea	0x03a9
0xeb	0x03b4
0xec	0x221e
0xed	0x03c6
0xee	0x03b5
0xef	0x2229
0xf0	0x2261
0xf1	0x00b1
0xf2	0x2265
0xf3	0x2264
0xf4	0x2320
0xf5	0x2321
0xf6	0x00f7
0xf7	0x2248
0xf8	0x00b0
0xf9	0x2219
0xfa	0x00b7
0xfb	0x221a
0xfc	0x207f
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM500',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM500',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM500');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x6f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM500',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x009c
0x05	0x0009
0x06	0x0086
0x07	0x007f
0x08	0x0097
0x09	0x008d
0x0a	0x008e
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x009d
0x15	0x0085
0x16	0x0008
0x17	0x0087
0x18	0x0018
0x19	0x0019
0x1a	0x0092
0x1b	0x008f
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0080
0x21	0x0081
0x22	0x0082
0x23	0x0083
0x24	0x0084
0x25	0x000a
0x26	0x0017
0x27	0x001b
0x28	0x0088
0x29	0x0089
0x2a	0x008a
0x2b	0x008b
0x2c	0x008c
0x2d	0x0005
0x2e	0x0006
0x2f	0x0007
0x30	0x0090
0x31	0x0091
0x32	0x0016
0x33	0x0093
0x34	0x0094
0x35	0x0095
0x36	0x0096
0x37	0x0004
0x38	0x0098
0x39	0x0099
0x3a	0x009a
0x3b	0x009b
0x3c	0x0014
0x3d	0x0015
0x3e	0x009e
0x3f	0x001a
0x40	0x0020
0x41	0x00a0
0x42	0x00e2
0x43	0x00e4
0x44	0x00e0
0x45	0x00e1
0x46	0x00e3
0x47	0x00e5
0x48	0x00e7
0x49	0x00f1
0x4a	0x005b
0x4b	0x002e
0x4c	0x003c
0x4d	0x0028
0x4e	0x002b
0x4f	0x0021
0x50	0x0026
0x51	0x00e9
0x52	0x00ea
0x53	0x00eb
0x54	0x00e8
0x55	0x00ed
0x56	0x00ee
0x57	0x00ef
0x58	0x00ec
0x59	0x00df
0x5a	0x005d
0x5b	0x0024
0x5c	0x002a
0x5d	0x0029
0x5e	0x003b
0x5f	0x005e
0x60	0x002d
0x61	0x002f
0x62	0x00c2
0x63	0x00c4
0x64	0x00c0
0x65	0x00c1
0x66	0x00c3
0x67	0x00c5
0x68	0x00c7
0x69	0x00d1
0x6a	0x00a6
0x6b	0x002c
0x6c	0x0025
0x6d	0x005f
0x6e	0x003e
0x6f	0x003f
0x70	0x00f8
0x71	0x00c9
0x72	0x00ca
0x73	0x00cb
0x74	0x00c8
0x75	0x00cd
0x76	0x00ce
0x77	0x00cf
0x78	0x00cc
0x79	0x0060
0x7a	0x003a
0x7b	0x0023
0x7c	0x0040
0x7d	0x0027
0x7e	0x003d
0x7f	0x0022
0x80	0x00d8
0x81	0x0061
0x82	0x0062
0x83	0x0063
0x84	0x0064
0x85	0x0065
0x86	0x0066
0x87	0x0067
0x88	0x0068
0x89	0x0069
0x8a	0x00ab
0x8b	0x00bb
0x8c	0x00f0
0x8d	0x00fd
0x8e	0x00fe
0x8f	0x00b1
0x90	0x00b0
0x91	0x006a
0x92	0x006b
0x93	0x006c
0x94	0x006d
0x95	0x006e
0x96	0x006f
0x97	0x0070
0x98	0x0071
0x99	0x0072
0x9a	0x00aa
0x9b	0x00ba
0x9c	0x00e6
0x9d	0x00b8
0x9e	0x00c6
0x9f	0x00a4
0xa0	0x00b5
0xa1	0x007e
0xa2	0x0073
0xa3	0x0074
0xa4	0x0075
0xa5	0x0076
0xa6	0x0077
0xa7	0x0078
0xa8	0x0079
0xa9	0x007a
0xaa	0x00a1
0xab	0x00bf
0xac	0x00d0
0xad	0x00dd
0xae	0x00de
0xaf	0x00ae
0xb0	0x00a2
0xb1	0x00a3
0xb2	0x00a5
0xb3	0x00b7
0xb4	0x00a9
0xb5	0x00a7
0xb6	0x00b6
0xb7	0x00bc
0xb8	0x00bd
0xb9	0x00be
0xba	0x00ac
0xbb	0x007c
0xbc	0x00af
0xbd	0x00a8
0xbe	0x00b4
0xbf	0x00d7
0xc0	0x007b
0xc1	0x0041
0xc2	0x0042
0xc3	0x0043
0xc4	0x0044
0xc5	0x0045
0xc6	0x0046
0xc7	0x0047
0xc8	0x0048
0xc9	0x0049
0xca	0x00ad
0xcb	0x00f4
0xcc	0x00f6
0xcd	0x00f2
0xce	0x00f3
0xcf	0x00f5
0xd0	0x007d
0xd1	0x004a
0xd2	0x004b
0xd3	0x004c
0xd4	0x004d
0xd5	0x004e
0xd6	0x004f
0xd7	0x0050
0xd8	0x0051
0xd9	0x0052
0xda	0x00b9
0xdb	0x00fb
0xdc	0x00fc
0xdd	0x00f9
0xde	0x00fa
0xdf	0x00ff
0xe0	0x005c
0xe1	0x00f7
0xe2	0x0053
0xe3	0x0054
0xe4	0x0055
0xe5	0x0056
0xe6	0x0057
0xe7	0x0058
0xe8	0x0059
0xe9	0x005a
0xea	0x00b2
0xeb	0x00d4
0xec	0x00d6
0xed	0x00d2
0xee	0x00d3
0xef	0x00d5
0xf0	0x0030
0xf1	0x0031
0xf2	0x0032
0xf3	0x0033
0xf4	0x0034
0xf5	0x0035
0xf6	0x0036
0xf7	0x0037
0xf8	0x0038
0xf9	0x0039
0xfa	0x00b3
0xfb	0x00db
0xfc	0x00dc
0xfd	0x00d9
0xfe	0x00da
0xff	0x009f

repo_name: mthl/texinfo

license: mit


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM850',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM850',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM850');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM850',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e4
0x85	0x00e0
0x86	0x00e5
0x87	0x00e7
0x88	0x00ea
0x89	0x00eb
0x8a	0x00e8
0x8b	0x00ef
0x8c	0x00ee
0x8d	0x00ec
0x8e	0x00c4
0x8f	0x00c5
0x90	0x00c9
0x91	0x00e6
0x92	0x00c6
0x93	0x00f4
0x94	0x00f6
0x95	0x00f2
0x96	0x00fb
0x97	0x00f9
0x98	0x00ff
0x99	0x00d6
0x9a	0x00dc
0x9b	0x00f8
0x9c	0x00a3
0x9d	0x00d8
0x9e	0x00d7
0x9f	0x0192
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00f1
0xa5	0x00d1
0xa6	0x00aa
0xa7	0x00ba
0xa8	0x00bf
0xa9	0x00ae
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00a1
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x00c1
0xb6	0x00c2
0xb7	0x00c0
0xb8	0x00a9
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x00a2
0xbe	0x00a5
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x00e3
0xc7	0x00c3
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x00a4
0xd0	0x00f0
0xd1	0x00d0
0xd2	0x00ca
0xd3	0x00cb
0xd4	0x00c8
0xd5	0x0131
0xd6	0x00cd
0xd7	0x00ce
0xd8	0x00cf
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x00a6
0xde	0x00cc
0xdf	0x2580
0xe0	0x00d3
0xe1	0x00df
0xe2	0x00d4
0xe3	0x00d2
0xe4	0x00f5
0xe5	0x00d5
0xe6	0x00b5
0xe7	0x00fe
0xe8	0x00de
0xe9	0x00da
0xea	0x00db
0xeb	0x00d9
0xec	0x00fd
0xed	0x00dd
0xee	0x00af
0xef	0x00b4
0xf0	0x00ad
0xf1	0x00b1
0xf2	0x2017
0xf3	0x00be
0xf4	0x00b6
0xf5	0x00a7
0xf6	0x00f7
0xf7	0x00b8
0xf8	0x00b0
0xf9	0x00a8
0xfa	0x00b7
0xfb	0x00b9
0xfc	0x00b3
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM851',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM851',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM851');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM851',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e4
0x85	0x00e0
0x86	0x0386
0x87	0x00e7
0x88	0x00ea
0x89	0x00eb
0x8a	0x00e8
0x8b	0x00ef
0x8c	0x00ee
0x8d	0x0388
0x8e	0x00c4
0x8f	0x0389
0x90	0x038a
0x92	0xfffd
0x92	0x038c
0x93	0x00f4
0x94	0x00f6
0x95	0x038e
0x96	0x00fb
0x97	0x00f9
0x98	0x038f
0x99	0x00d6
0x9a	0x00dc
0x9b	0x03ac
0x9c	0x00a3
0x9d	0x03ad
0x9e	0x03ae
0x9f	0x03af
0xa0	0x03ca
0xa1	0x0390
0xa2	0x03cc
0xa3	0x03cd
0xa4	0x0391
0xa5	0x0392
0xa6	0x0393
0xa7	0x0394
0xa8	0x0395
0xa9	0x0396
0xaa	0x0397
0xab	0x00bd
0xac	0x0398
0xad	0x0399
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x039a
0xb6	0x039b
0xb7	0x039d
0xb8	0x039c
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x039e
0xbe	0x039f
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x03a0
0xc7	0x03a1
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x03a3
0xd0	0x03a4
0xd1	0x03a5
0xd2	0x03a6
0xd3	0x03a7
0xd4	0x03a8
0xd5	0x03a9
0xd6	0x03b1
0xd7	0x03b2
0xd8	0x03b3
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x03b4
0xde	0x03b5
0xdf	0x2580
0xe0	0x03b6
0xe1	0x03b7
0xe2	0x03b8
0xe3	0x03b9
0xe4	0x03ba
0xe5	0x03bb
0xe6	0x03bc
0xe7	0x03bd
0xe8	0x03be
0xe9	0x03bf
0xea	0x03c0
0xeb	0x03c1
0xec	0x03c3
0xed	0x03c2
0xee	0x03c4
0xef	0x00b4
0xf0	0x00ad
0xf1	0x00b1
0xf2	0x03c5
0xf3	0x03c6
0xf4	0x03c7
0xf5	0x00a7
0xf6	0x03c8
0xf7	0x02db
0xf8	0x00b0
0xf9	0x00a8
0xfa	0x03c9
0xfb	0x03cb
0xfc	0x03b0
0xfd	0x03ce
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM852',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM852',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM852');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM852',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e4
0x85	0x016f
0x86	0x0107
0x87	0x00e7
0x88	0x0142
0x89	0x00eb
0x8a	0x0150
0x8b	0x0151
0x8c	0x00ee
0x8d	0x0179
0x8e	0x00c4
0x8f	0x0106
0x90	0x00c9
0x91	0x0139
0x92	0x013a
0x93	0x00f4
0x94	0x00f6
0x95	0x013d
0x96	0x013e
0x97	0x015a
0x98	0x015b
0x99	0x00d6
0x9a	0x00dc
0x9b	0x0164
0x9c	0x0165
0x9d	0x0141
0x9e	0x00d7
0x9f	0x010d
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x0104
0xa5	0x0105
0xa6	0x017d
0xa7	0x017e
0xa8	0x0118
0xa9	0x0119
0xaa	0x00ac
0xab	0x017a
0xac	0x010c
0xad	0x015f
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x00c1
0xb6	0x00c2
0xb7	0x011a
0xb8	0x015e
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x017b
0xbe	0x017c
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x0102
0xc7	0x0103
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x00a4
0xd0	0x0111
0xd1	0x0110
0xd2	0x010e
0xd3	0x00cb
0xd4	0x010f
0xd5	0x0147
0xd6	0x00cd
0xd7	0x00ce
0xd8	0x011b
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x0162
0xde	0x016e
0xdf	0x2580
0xe0	0x00d3
0xe1	0x00df
0xe2	0x00d4
0xe3	0x0143
0xe4	0x0144
0xe5	0x0148
0xe6	0x0160
0xe7	0x0161
0xe8	0x0154
0xe9	0x00da
0xea	0x0155
0xeb	0x0170
0xec	0x00fd
0xed	0x00dd
0xee	0x0163
0xef	0x00b4
0xf0	0x00ad
0xf1	0x02dd
0xf2	0x02db
0xf3	0x02c7
0xf4	0x02d8
0xf5	0x00a7
0xf6	0x00f7
0xf7	0x00b8
0xf8	0x00b0
0xf9	0x00a8
0xfa	0x02d9
0xfb	0x0171
0xfc	0x0158
0xfd	0x0159
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM855',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM855',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM855');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM855',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x0452
0x81	0x0402
0x82	0x0453
0x83	0x0403
0x84	0x0451
0x85	0x0401
0x86	0x0454
0x87	0x0404
0x88	0x0455
0x89	0x0405
0x8a	0x0456
0x8b	0x0406
0x8c	0x0457
0x8d	0x0407
0x8e	0x0458
0x8f	0x0408
0x90	0x0459
0x91	0x0409
0x92	0x045a
0x93	0x040a
0x94	0x045b
0x95	0x040b
0x96	0x045c
0x97	0x040c
0x98	0x045e
0x99	0x040e
0x9a	0x045f
0x9b	0x040f
0x9c	0x044e
0x9d	0x042e
0x9e	0x044a
0x9f	0x042a
0xa0	0x0430
0xa1	0x0410
0xa2	0x0431
0xa3	0x0411
0xa4	0x0446
0xa5	0x0426
0xa6	0x0434
0xa7	0x0414
0xa8	0x0435
0xa9	0x0415
0xaa	0x0444
0xab	0x0424
0xac	0x0433
0xad	0x0413
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x0445
0xb6	0x0425
0xb7	0x0438
0xb8	0x0418
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x0439
0xbe	0x0419
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x043a
0xc7	0x041a
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x00a4
0xd0	0x043b
0xd1	0x041b
0xd2	0x043c
0xd3	0x041c
0xd4	0x043d
0xd5	0x041d
0xd6	0x043e
0xd7	0x041e
0xd8	0x043f
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x041f
0xde	0x044f
0xdf	0x2580
0xe0	0x042f
0xe1	0x0440
0xe2	0x0420
0xe3	0x0441
0xe4	0x0421
0xe5	0x0442
0xe6	0x0422
0xe7	0x0443
0xe8	0x0423
0xe9	0x0436
0xea	0x0416
0xeb	0x0432
0xec	0x0412
0xed	0x044c
0xee	0x042c
0xef	0x2116
0xf0	0x00ad
0xf1	0x044b
0xf2	0x042b
0xf3	0x0437
0xf4	0x0417
0xf5	0x0448
0xf6	0x0428
0xf7	0x044d
0xf8	0x042d
0xf9	0x0449
0xfa	0x0429
0xfb	0x0447
0xfc	0x0427
0xfd	0x00a7
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM857',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM857',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM857');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM857',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e4
0x85	0x00e0
0x86	0x00e5
0x87	0x00e7
0x88	0x00ea
0x89	0x00eb
0x8a	0x00e8
0x8b	0x00ef
0x8c	0x00ee
0x8d	0x0131
0x8e	0x00c4
0x8f	0x00c5
0x90	0x00c9
0x91	0x00e6
0x92	0x00c6
0x93	0x00f4
0x94	0x00f6
0x95	0x00f2
0x96	0x00fb
0x97	0x00f9
0x98	0x0130
0x99	0x00d6
0x9a	0x00dc
0x9b	0x00f8
0x9c	0x00a3
0x9d	0x00d8
0x9e	0x015e
0x9f	0x015f
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00f1
0xa5	0x00d1
0xa6	0x011e
0xa7	0x011f
0xa8	0x00bf
0xa9	0x00ae
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00a1
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x00c1
0xb6	0x00c2
0xb7	0x00c0
0xb8	0x00a9
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x00a2
0xbe	0x00a5
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x00e3
0xc7	0x00c3
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x00a4
0xd0	0x00ba
0xd1	0x00aa
0xd2	0x00ca
0xd3	0x00cb
0xd4	0x00c8
0xd6	0xfffd
0xd6	0x00cd
0xd7	0x00ce
0xd8	0x00cf
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x00a6
0xde	0x00cc
0xdf	0x2580
0xe0	0x00d3
0xe1	0x00df
0xe2	0x00d4
0xe3	0x00d2
0xe4	0x00f5
0xe5	0x00d5
0xe6	0x00b5
0xe8	0xfffd
0xe8	0x00d7
0xe9	0x00da
0xea	0x00db
0xeb	0x00d9
0xec	0x00ec
0xed	0x00ff
0xee	0x00af
0xef	0x00b4
0xf0	0x00ad
0xf1	0x00b1
0xf3	0xfffd
0xf3	0x00be
0xf4	0x00b6
0xf5	0x00a7
0xf6	0x00f7
0xf7	0x00b8
0xf8	0x00b0
0xf9	0x00a8
0xfa	0x00b7
0xfb	0x00b9
0xfc	0x00b3
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM860',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM860',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM860');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM860',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e3
0x85	0x00e0
0x86	0x00c1
0x87	0x00e7
0x88	0x00ea
0x89	0x00ca
0x8a	0x00e8
0x8b	0x00cd
0x8c	0x00d4
0x8d	0x00ec
0x8e	0x00c3
0x8f	0x00c2
0x90	0x00c9
0x91	0x00c0
0x92	0x00c8
0x93	0x00f4
0x94	0x00f5
0x95	0x00f2
0x96	0x00da
0x97	0x00f9
0x98	0x00cc
0x99	0x00d5
0x9a	0x00dc
0x9b	0x00a2
0x9c	0x00a3
0x9d	0x00d9
0x9e	0x20a7
0x9f	0x00d3
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00f1
0xa5	0x00d1
0xa6	0x00aa
0xa7	0x00ba
0xa8	0x00bf
0xa9	0x00d2
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00a1
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x2561
0xb6	0x2562
0xb7	0x2556
0xb8	0x2555
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x255c
0xbe	0x255b
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x255e
0xc7	0x255f
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x2567
0xd0	0x2568
0xd1	0x2564
0xd2	0x2565
0xd3	0x2559
0xd4	0x2558
0xd5	0x2552
0xd6	0x2553
0xd7	0x256b
0xd8	0x256a
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x258c
0xde	0x2590
0xdf	0x2580
0xe0	0x03b1
0xe1	0x00df
0xe2	0x0393
0xe3	0x03c0
0xe4	0x03a3
0xe5	0x03c3
0xe6	0x00b5
0xe7	0x03c4
0xe8	0x03a6
0xe9	0x0398
0xea	0x03a9
0xeb	0x03b4
0xec	0x221e
0xed	0x03c6
0xee	0x03b5
0xef	0x2229
0xf0	0x2261
0xf1	0x00b1
0xf2	0x2265
0xf3	0x2264
0xf4	0x2320
0xf5	0x2321
0xf6	0x00f7
0xf7	0x2248
0xf8	0x00b0
0xf9	0x2219
0xfa	0x00b7
0xfb	0x221a
0xfc	0x207f
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM861',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM861',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM861');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM861',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e4
0x85	0x00e0
0x86	0x00e5
0x87	0x00e7
0x88	0x00ea
0x89	0x00eb
0x8a	0x00e8
0x8b	0x00d0
0x8c	0x00f0
0x8d	0x00de
0x8e	0x00c4
0x8f	0x00c5
0x90	0x00c9
0x91	0x00e6
0x92	0x00c6
0x93	0x00f4
0x94	0x00f6
0x95	0x00fe
0x96	0x00fb
0x97	0x00dd
0x98	0x00fd
0x99	0x00d6
0x9a	0x00dc
0x9b	0x00f8
0x9c	0x00a3
0x9d	0x00d8
0x9e	0x20a7
0x9f	0x0192
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00c1
0xa5	0x00cd
0xa6	0x00d3
0xa7	0x00da
0xa8	0x00bf
0xa9	0x2310
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00a1
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x2561
0xb6	0x2562
0xb7	0x2556
0xb8	0x2555
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x255c
0xbe	0x255b
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x255e
0xc7	0x255f
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x2567
0xd0	0x2568
0xd1	0x2564
0xd2	0x2565
0xd3	0x2559
0xd4	0x2558
0xd5	0x2552
0xd6	0x2553
0xd7	0x256b
0xd8	0x256a
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x258c
0xde	0x2590
0xdf	0x2580
0xe0	0x03b1
0xe1	0x00df
0xe2	0x0393
0xe3	0x03c0
0xe4	0x03a3
0xe5	0x03c3
0xe6	0x00b5
0xe7	0x03c4
0xe8	0x03a6
0xe9	0x0398
0xea	0x03a9
0xeb	0x03b4
0xec	0x221e
0xed	0x03c6
0xee	0x03b5
0xef	0x2229
0xf0	0x2261
0xf1	0x00b1
0xf2	0x2265
0xf3	0x2264
0xf4	0x2320
0xf5	0x2321
0xf6	0x00f7
0xf7	0x2248
0xf8	0x00b0
0xf9	0x2219
0xfa	0x00b7
0xfb	0x221a
0xfc	0x207f
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM862',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM862',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM862');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM862',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x05d0
0x81	0x05d1
0x82	0x05d2
0x83	0x05d3
0x84	0x05d4
0x85	0x05d5
0x86	0x05d6
0x87	0x05d7
0x88	0x05d8
0x89	0x05d9
0x8a	0x05da
0x8b	0x05db
0x8c	0x05dc
0x8d	0x05dd
0x8e	0x05de
0x8f	0x05df
0x90	0x05e0
0x91	0x05e1
0x92	0x05e2
0x93	0x05e3
0x94	0x05e4
0x95	0x05e5
0x96	0x05e6
0x97	0x05e7
0x98	0x05e8
0x99	0x05e9
0x9a	0x05ea
0x9b	0x00a2
0x9c	0x00a3
0x9d	0x00a5
0x9e	0x20a7
0x9f	0x0192
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00f1
0xa5	0x00d1
0xa6	0x00aa
0xa7	0x00ba
0xa8	0x00bf
0xa9	0x2310
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00a1
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x2561
0xb6	0x2562
0xb7	0x2556
0xb8	0x2555
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x255c
0xbe	0x255b
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x255e
0xc7	0x255f
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x2567
0xd0	0x2568
0xd1	0x2564
0xd2	0x2565
0xd3	0x2559
0xd4	0x2558
0xd5	0x2552
0xd6	0x2553
0xd7	0x256b
0xd8	0x256a
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x258c
0xde	0x2590
0xdf	0x2580
0xe0	0x03b1
0xe1	0x00df
0xe2	0x0393
0xe3	0x03c0
0xe4	0x03a3
0xe5	0x03c3
0xe6	0x00b5
0xe7	0x03c4
0xe8	0x03a6
0xe9	0x0398
0xea	0x03a9
0xeb	0x03b4
0xec	0x221e
0xed	0x03c6
0xee	0x03b5
0xef	0x2229
0xf0	0x2261
0xf1	0x00b1
0xf2	0x2265
0xf3	0x2264
0xf4	0x2320
0xf5	0x2321
0xf6	0x00f7
0xf7	0x2248
0xf8	0x00b0
0xf9	0x2219
0xfa	0x00b7
0xfb	0x221a
0xfc	0x207f
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM863',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM863',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM863');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM863',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00c2
0x85	0x00e0
0x86	0x00b6
0x87	0x00e7
0x88	0x00ea
0x89	0x00eb
0x8a	0x00e8
0x8b	0x00ef
0x8c	0x00ee
0x8d	0x2017
0x8e	0x00c0
0x8f	0x00a7
0x90	0x00c9
0x91	0x00c8
0x92	0x00ca
0x93	0x00f4
0x94	0x00cb
0x95	0x00cf
0x96	0x00fb
0x97	0x00f9
0x98	0x00a4
0x99	0x00d4
0x9a	0x00dc
0x9b	0x00a2
0x9c	0x00a3
0x9d	0x00d9
0x9e	0x00db
0x9f	0x0192
0xa0	0x00a6
0xa1	0x00b4
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00a8
0xa5	0x00b8
0xa6	0x00b3
0xa7	0x00af
0xa8	0x00ce
0xa9	0x2310
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00be
0xae	0x00ab
0xaf	0x00bb
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x2561
0xb6	0x2562
0xb7	0x2556
0xb8	0x2555
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x255c
0xbe	0x255b
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x255e
0xc7	0x255f
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x2567
0xd0	0x2568
0xd1	0x2564
0xd2	0x2565
0xd3	0x2559
0xd4	0x2558
0xd5	0x2552
0xd6	0x2553
0xd7	0x256b
0xd8	0x256a
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x258c
0xde	0x2590
0xdf	0x2580
0xe0	0x03b1
0xe1	0x00df
0xe2	0x0393
0xe3	0x03c0
0xe4	0x03a3
0xe5	0x03c3
0xe6	0x00b5
0xe7	0x03c4
0xe8	0x03a6
0xe9	0x0398
0xea	0x03a9
0xeb	0x03b4
0xec	0x221e
0xed	0x03c6
0xee	0x03b5
0xef	0x2229
0xf0	0x2261
0xf1	0x00b1
0xf2	0x2265
0xf3	0x2264
0xf4	0x2320
0xf5	0x2321
0xf6	0x00f7
0xf7	0x2248
0xf8	0x00b0
0xf9	0x2219
0xfa	0x00b7
0xfb	0x221a
0xfc	0x207f
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM864',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM864',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM864');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM864',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x066a
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00b0
0x81	0x00b7
0x82	0x2219
0x83	0x221a
0x84	0x2592
0x85	0x2500
0x86	0x2502
0x87	0x253c
0x88	0x2524
0x89	0x252c
0x8a	0x251c
0x8b	0x2534
0x8c	0x2510
0x8d	0x250c
0x8e	0x2514
0x8f	0x2518
0x90	0x03b2
0x91	0x221e
0x92	0x03c6
0x93	0x00b1
0x94	0x00bd
0x95	0x00bc
0x96	0x2248
0x97	0x00ab
0x98	0x00bb
0x99	0xfef7
0x9a	0xfef8
0x9d	0xfffd
0x9d	0xfffd
0x9d	0xfefb
0x9e	0xfefc
0xa0	0xfffd
0xa0	0x00a0
0xa1	0x00ad
0xa2	0xfe82
0xa3	0x00a3
0xa4	0x00a4
0xa5	0xfe84
0xa8	0xfffd
0xa8	0xfffd
0xa8	0xfe8e
0xa9	0xfe8f
0xaa	0xfe95
0xab	0xfe99
0xac	0x060c
0xad	0xfe9d
0xae	0xfea1
0xaf	0xfea5
0xb0	0x0660
0xb1	0x0661
0xb2	0x0662
0xb3	0x0663
0xb4	0x0664
0xb5	0x0665
0xb6	0x0666
0xb7	0x0667
0xb8	0x0668
0xb9	0x0669
0xba	0xfed1
0xbb	0x061b
0xbc	0xfeb1
0xbd	0xfeb5
0xbe	0xfeb9
0xbf	0x061f
0xc0	0x00a2
0xc1	0xfe80
0xc2	0xfe81
0xc3	0xfe83
0xc4	0xfe85
0xc5	0xfeca
0xc6	0xfe8b
0xc7	0xfe8d
0xc8	0xfe91
0xc9	0xfe93
0xca	0xfe97
0xcb	0xfe9b
0xcc	0xfe9f
0xcd	0xfea3
0xce	0xfea7
0xcf	0xfea9
0xd0	0xfeab
0xd1	0xfead
0xd2	0xfeaf
0xd3	0xfeb3
0xd4	0xfeb7
0xd5	0xfebb
0xd6	0xfebf
0xd7	0xfec1
0xd8	0xfec5
0xd9	0xfecb
0xda	0xfecf
0xdb	0x00a6
0xdc	0x00ac
0xdd	0x00f7
0xde	0x00d7
0xdf	0xfec9
0xe0	0x0640
0xe1	0xfed3
0xe2	0xfed7
0xe3	0xfedb
0xe4	0xfedf
0xe5	0xfee3
0xe6	0xfee7
0xe7	0xfeeb
0xe8	0xfeed
0xe9	0xfeef
0xea	0xfef3
0xeb	0xfebd
0xec	0xfecc
0xed	0xfece
0xee	0xfecd
0xef	0xfee1
0xf0	0xfe7d
0xf1	0x0651
0xf2	0xfee5
0xf3	0xfee9
0xf4	0xfeec
0xf5	0xfef0
0xf6	0xfef2
0xf7	0xfed0
0xf8	0xfed5
0xf9	0xfef5
0xfa	0xfef6
0xfb	0xfedd
0xfc	0xfed9
0xfd	0xfef1
0xfe	0x25a0
0xff	0xfffd


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM865',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM865',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM865');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM865',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x00c7
0x81	0x00fc
0x82	0x00e9
0x83	0x00e2
0x84	0x00e4
0x85	0x00e0
0x86	0x00e5
0x87	0x00e7
0x88	0x00ea
0x89	0x00eb
0x8a	0x00e8
0x8b	0x00ef
0x8c	0x00ee
0x8d	0x00ec
0x8e	0x00c4
0x8f	0x00c5
0x90	0x00c9
0x91	0x00e6
0x92	0x00c6
0x93	0x00f4
0x94	0x00f6
0x95	0x00f2
0x96	0x00fb
0x97	0x00f9
0x98	0x00ff
0x99	0x00d6
0x9a	0x00dc
0x9b	0x00f8
0x9c	0x00a3
0x9d	0x00d8
0x9e	0x20a7
0x9f	0x0192
0xa0	0x00e1
0xa1	0x00ed
0xa2	0x00f3
0xa3	0x00fa
0xa4	0x00f1
0xa5	0x00d1
0xa6	0x00aa
0xa7	0x00ba
0xa8	0x00bf
0xa9	0x2310
0xaa	0x00ac
0xab	0x00bd
0xac	0x00bc
0xad	0x00a1
0xae	0x00ab
0xaf	0x00a4
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x2561
0xb6	0x2562
0xb7	0x2556
0xb8	0x2555
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x255c
0xbe	0x255b
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x255e
0xc7	0x255f
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x2567
0xd0	0x2568
0xd1	0x2564
0xd2	0x2565
0xd3	0x2559
0xd4	0x2558
0xd5	0x2552
0xd6	0x2553
0xd7	0x256b
0xd8	0x256a
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x258c
0xde	0x2590
0xdf	0x2580
0xe0	0x03b1
0xe1	0x00df
0xe2	0x0393
0xe3	0x03c0
0xe4	0x03a3
0xe5	0x03c3
0xe6	0x00b5
0xe7	0x03c4
0xe8	0x03a6
0xe9	0x0398
0xea	0x03a9
0xeb	0x03b4
0xec	0x221e
0xed	0x03c6
0xee	0x03b5
0xef	0x2229
0xf0	0x2261
0xf1	0x00b1
0xf2	0x2265
0xf3	0x2264
0xf4	0x2320
0xf5	0x2321
0xf6	0x00f7
0xf7	0x2248
0xf8	0x00b0
0xf9	0x2219
0xfa	0x00b7
0xfb	0x221a
0xfc	0x207f
0xfd	0x00b2
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM866',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM866',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM866');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x3f"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM866',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x0410
0x81	0x0411
0x82	0x0412
0x83	0x0413
0x84	0x0414
0x85	0x0415
0x86	0x0416
0x87	0x0417
0x88	0x0418
0x89	0x0419
0x8a	0x041a
0x8b	0x041b
0x8c	0x041c
0x8d	0x041d
0x8e	0x041e
0x8f	0x041f
0x90	0x0420
0x91	0x0421
0x92	0x0422
0x93	0x0423
0x94	0x0424
0x95	0x0425
0x96	0x0426
0x97	0x0427
0x98	0x0428
0x99	0x0429
0x9a	0x042a
0x9b	0x042b
0x9c	0x042c
0x9d	0x042d
0x9e	0x042e
0x9f	0x042f
0xa0	0x0430
0xa1	0x0431
0xa2	0x0432
0xa3	0x0433
0xa4	0x0434
0xa5	0x0435
0xa6	0x0436
0xa7	0x0437
0xa8	0x0438
0xa9	0x0439
0xaa	0x043a
0xab	0x043b
0xac	0x043c
0xad	0x043d
0xae	0x043e
0xaf	0x043f
0xb0	0x2591
0xb1	0x2592
0xb2	0x2593
0xb3	0x2502
0xb4	0x2524
0xb5	0x2561
0xb6	0x2562
0xb7	0x2556
0xb8	0x2555
0xb9	0x2563
0xba	0x2551
0xbb	0x2557
0xbc	0x255d
0xbd	0x255c
0xbe	0x255b
0xbf	0x2510
0xc0	0x2514
0xc1	0x2534
0xc2	0x252c
0xc3	0x251c
0xc4	0x2500
0xc5	0x253c
0xc6	0x255e
0xc7	0x255f
0xc8	0x255a
0xc9	0x2554
0xca	0x2569
0xcb	0x2566
0xcc	0x2560
0xcd	0x2550
0xce	0x256c
0xcf	0x2567
0xd0	0x2568
0xd1	0x2564
0xd2	0x2565
0xd3	0x2559
0xd4	0x2558
0xd5	0x2552
0xd6	0x2553
0xd7	0x256b
0xd8	0x256a
0xd9	0x2518
0xda	0x250c
0xdb	0x2588
0xdc	0x2584
0xdd	0x258c
0xde	0x2590
0xdf	0x2580
0xe0	0x0440
0xe1	0x0441
0xe2	0x0442
0xe3	0x0443
0xe4	0x0444
0xe5	0x0445
0xe6	0x0446
0xe7	0x0447
0xe8	0x0448
0xe9	0x0449
0xea	0x044a
0xeb	0x044b
0xec	0x044c
0xed	0x044d
0xee	0x044e
0xef	0x044f
0xf0	0x0401
0xf1	0x0451
0xf2	0x0404
0xf3	0x0454
0xf4	0x0407
0xf5	0x0457
0xf6	0x040e
0xf7	0x045e
0xf8	0x00b0
0xf9	0x2219
0xfa	0x00b7
0xfb	0x221a
0xfc	0x2116
0xfd	0x00a4
0xfe	0x25a0
0xff	0x00a0


code:
#! /usr/local/bin/perl -w

# vim: tabstop=4
# vim: syntax=perl

use strict;

use Test;

BEGIN {
	plan tests => 7;
}

use Locale::Recode;

sub int2utf8;

my $local2ucs = {};
my $ucs2local = {};

while (<DATA>) {
	my ($code, $ucs, undef) = map { oct $_ } split /\s+/, $_;
	$local2ucs->{$code} = $ucs;
	$ucs2local->{$ucs} = $code unless $ucs == 0xfffd;
}

my $cd_int = Locale::Recode->new (from => 'IBM868',
				  to => 'INTERNAL');
ok !$cd_int->getError;

my $cd_utf8 = Locale::Recode->new (from => 'IBM868',
				   to => 'UTF-8');
ok !$cd_utf8->getError;

my $cd_rev = Locale::Recode->new (from => 'INTERNAL',
				  to => 'IBM868');
ok !$cd_rev->getError;

# Convert into internal representation.
my $result_int = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_int->recode ($outbuf);
    unless ($result && $outbuf->[0] == $ucs) {
	$result_int = 0;
	last;
    }
}
ok $result_int;

# Convert to UTF-8.
my $result_utf8 = 1;
while (my ($code, $ucs) = each %$local2ucs) {
    my $outbuf = chr $code;
    my $result = $cd_utf8->recode ($outbuf);
    unless ($result && $outbuf eq int2utf8 $ucs) {
        $result_utf8 = 0;
        last;
    }
}
ok $result_utf8;

# Convert from internal representation.
my $result_rev = 1;
while (my ($ucs, $code) = each %$ucs2local) {
    my $outbuf = [ $ucs ];
    my $result = $cd_rev->recode ($outbuf);
    unless ($result && $code == ord $outbuf) {
        $result_int = 0;
        last;
    }
}
ok $result_int;

# Check handling of unknown characters.
my $test_string1 = [ unpack 'c*', ' Supergirl ' ];
$test_string1->[0] = 0xad0be;
$test_string1->[-1] = 0xbeefbabe;
my $test_string2 = [ unpack 'c*', 'Supergirl' ];

my $unknown = "\x8c"; # Unknown character!

$cd_rev = Locale::Recode->new (from => 'INTERNAL',
		               to => 'IBM868',
				)
&& $cd_rev->recode ($test_string1)
&& $cd_rev->recode ($test_string2)
&& ($test_string2 = $unknown . $test_string2 . $unknown);

ok $test_string1 eq $test_string2;

sub int2utf8
{
    my $ucs4 = shift;

    if ($ucs4 <= 0x7f) {
	return chr $ucs4;
    } elsif ($ucs4 <= 0x7ff) {
	return pack ("C2",
		     (0xc0 | (($ucs4 >> 6) & 0x1f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0xffff) {
	return pack ("C3",
		     (0xe0 | (($ucs4 >> 12) & 0xf)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x1fffff) {
	return pack ("C4",
		     (0xf0 | (($ucs4 >> 18) & 0x7)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } elsif ($ucs4 <= 0x3ffffff) {
	return pack ("C5",
		     (0xf0 | (($ucs4 >> 24) & 0x3)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    } else {
	return pack ("C6",
		     (0xf0 | (($ucs4 >> 30) & 0x3)),
		     (0x80 | (($ucs4 >> 24) & 0x1)),
		     (0x80 | (($ucs4 >> 18) & 0x3f)),
		     (0x80 | (($ucs4 >> 12) & 0x3f)),
		     (0x80 | (($ucs4 >> 6) & 0x3f)),
		     (0x80 | ($ucs4 & 0x3f)));
    }
}

#Local Variables:
#mode: perl
#perl-indent-level: 4
#perl-continued-statement-offset: 4
#perl-continued-brace-offset: 0
#perl-brace-offset: -4
#perl-brace-imaginary-offset: 0
#perl-label-offset: -4
#tab-width: 4
#End:

__DATA__
0x00	0x0000
0x01	0x0001
0x02	0x0002
0x03	0x0003
0x04	0x0004
0x05	0x0005
0x06	0x0006
0x07	0x0007
0x08	0x0008
0x09	0x0009
0x0a	0x000a
0x0b	0x000b
0x0c	0x000c
0x0d	0x000d
0x0e	0x000e
0x0f	0x000f
0x10	0x0010
0x11	0x0011
0x12	0x0012
0x13	0x0013
0x14	0x0014
0x15	0x0015
0x16	0x0016
0x17	0x0017
0x18	0x0018
0x19	0x0019
0x1a	0x001a
0x1b	0x001b
0x1c	0x001c
0x1d	0x001d
0x1e	0x001e
0x1f	0x001f
0x20	0x0020
0x21	0x0021
0x22	0x0022
0x23	0x0023
0x24	0x0024
0x25	0x0025
0x26	0x0026
0x27	0x0027
0x28	0x0028
0x29	0x0029
0x2a	0x002a
0x2b	0x002b
0x2c	0x002c
0x2d	0x002d
0x2e	0x002e
0x2f	0x002f
0x30	0x0030
0x31	0x0031
0x32	0x0032
0x33	0x0033
0x34	0x0034
0x35	0x0035
0x36	0x0036
0x37	0x0037
0x38	0x0038
0x39	0x0039
0x3a	0x003a
0x3b	0x003b
0x3c	0x003c
0x3d	0x003d
0x3e	0x003e
0x3f	0x003f
0x40	0x0040
0x41	0x0041
0x42	0x0042
0x43	0x0043
0x44	0x0044
0x45	0x0045
0x46	0x0046
0x47	0x0047
0x48	0x0048
0x49	0x0049
0x4a	0x004a
0x4b	0x004b
0x4c	0x004c
0x4d	0x004d
0x4e	0x004e
0x4f	0x004f
0x50	0x0050
0x51	0x0051
0x52	0x0052
0x53	0x0053
0x54	0x0054
0x55	0x0055
0x56	0x0056
0x57	0x0057
0x58	0x0058
0x59	0x0059
0x5a	0x005a
0x5b	0x005b
0x5c	0x005c
0x5d	0x005d
0x5e	0x005e
0x5f	0x005f
0x60	0x0060
0x61	0x0061
0x62	0x0062
0x63	0x0063
0x64	0x0064
0x65	0x0065
0x66	0x0066
0x67	0x0067
0x68	0x0068
0x69	0x0069
0x6a	0x006a
0x6b	0x006b
0x6c	0x006c
0x6d	0x006d
0x6e	0x006e
0x6f	0x006f
0x70	0x0070
0x71	0x0071
0x72	0x0072
0x73	0x0073
0x74	0x0074
0x75	0x0075
0x76	0x0076
0x77	0x0077
0x78	0x0078
0x79	0x0079
0x7a	0x007a
0x7b	0x007b
0x7c	0x007c
0x7d	0x007d
0x7e	0x007e
0x7f	0x007f
0x80	0x0660
0x81	0x0661
0x82	0x0662
0x83	0x0663
0x84	0x0664
0x85	0x0665
0x86	0x0666
0x87	0x0667
0x88	0x0668
0x89	0x0669
0x8a	0x060c
0x8b	0x061b
0x8c	0x061f
0x8d	0x0622
0x8e	0x0627
0x8f	0xfe8e
0x90	0xe016
0x91	0x0628
0x92	0xfe91
0x93	0x067e
0x95	0xfffd
0x95	0x0629
0x96	0x062a
0x97	0xfe97
0x9a	0xfffd
0x9a	0xfffd
0x9a	0x062b
0x9b	0xfe9b
0x9c	0x062c
0x9d	0xfe9f
0xa0	0xfffd
0xa0	0xfffd
0xa0	0x062d
0xa1	0xfea3
0xa2	0x062e
0xa3	0xfea7
0xa4	0x062f
0xa6	0xfffd
0xa6	0x0630
0xa7	0x0631
0xa9	0xfffd
0xa9	0x0632
0xab	0xfffd
0xab	0x0633
0xac	0xfeb3
0xad	0x0634
0xae	0x00ab
0xaf	0x00bb
0xb0	0xfeb7
0xb1	0x0635
0xb2	0x2591
0xb3	0x2592
0xb4	0x2593
0xb5	0x2502
0xb6	0x2524
0xb7	0xfebb
0xb8	0x0636
0xb9	0xfebf
0xba	0x0637
0xbb	0x2563
0xbc	0x2551
0xbd	0x2557
0xbe	0x255d
0xbf	0x0638
0xc0	0x0639
0xc1	0x2510
0xc2	0x2514
0xc3	0x2534
0xc4	0x252c
0xc5	0x251c
0xc6	0x2500
0xc7	0x253c
0xc8	0xfeca
0xc9	0xfecb
0xca	0x255a
0xcb	0x2554
0xcc	0x2569
0xcd	0x2566
0xce	0x2560
0xcf	0x2550
0xd0	0x256c
0xd1	0xfecc
0xd2	0x063a
0xd3	0xfece
0xd4	0xfecf
0xd5	0xfed0
0xd6	0x0641
0xd7	0xfed3
0xd8	0x0642
0xd9	0xfed7
0xda	0xfeda
0xdb	0x2518
0xdc	0x250c
0xdd	0x2588
0xde	0x2580
0xdf	0xfedb
0xe1	0xfffd
0xe1	0x2584
0xe3	0xfffd
0xe3	0x0644
0xe4	0xfede
0xe5	0xfee0
0xe6	0x0645
0xe7	0xfee3
0xe9	0xfffd
0xe9	0x0646
0xea	0xfee7
0xec	0xfffd
0xec	0x0648
0xf1	0xfffd
0xf1	0xfffd
0xf1	0xfffd
0xf1	0xfffd
0xf1	0x0621
0xf2	0x00ad
0xfb	0xfffd
0xfb	0xfffd
0xfb	0xfffd
0xfb	0xfffd
0xfb	0xfffd
0xfb	0xfffd
0xfb	0xfffd
0xfb	0xfffd
0xfb	0x0651
0xfc	0xfe7d
0xfe	0xfffd
0xfe	0x25a0
0xff	0x00a0