######################################################################
#
# 0117_cp932_vs_cp932x_test.t
#
# Copyright (c) 2018 INABA Hitoshi <ina.cpan@gmail.com> in a CPAN
######################################################################

use strict;
use FindBin;
use lib "$FindBin::Bin/../lib";

BEGIN {
    use vars qw(@test);

    use Jacode4e::RoundTrip;

    for my $row (0x81..0x9F, 0xE0..0xFC) {
        for my $cell (0x40..0x7E, 0x80..0xFC) {
            my $cp932_hex  = sprintf('%c%c', $row, $cell);
            my $cp932x_hex = sprintf('%c%c', $row, $cell);
            if ($cp932_hex =~ /^\x81\xAC$/) {
                push @test, [$cp932_hex,'cp932x','cp932',{'INPUT_LAYOUT'=>'D'}, $cp932x_hex],
            }
            elsif ($cp932_hex =~ /^\x9C\x5A$/) {
            }
            elsif ($cp932_hex =~ m'^(?:
    \x81\xBE|\x81\xBF|\x81\xCA|\x81\xDA|\x81\xDB|\x81\xDF|\x81\xE0|\x81\xE3|\x81\xE6|\x81\xE7|
    \x87\x54|\x87\x55|\x87\x56|\x87\x57|\x87\x58|\x87\x59|\x87\x5A|\x87\x5B|\x87\x5C|\x87\x5D|
    \x87\x82|\x87\x84|\x87\x8A|\x87\x90|\x87\x91|\x87\x92|\x87\x95|\x87\x96|\x87\x97|\x87\x9A|
    \x87\x9B|\x87\x9C|\xED\x40|\xED\x41|\xED\x42|\xED\x43|\xED\x44|\xED\x45|\xED\x46|\xED\x47|
    \xED\x48|\xED\x49|\xED\x4A|\xED\x4B|\xED\x4C|\xED\x4D|\xED\x4E|\xED\x4F|\xED\x50|\xED\x51|
    \xED\x52|\xED\x53|\xED\x54|\xED\x55|\xED\x56|\xED\x57|\xED\x58|\xED\x59|\xED\x5A|\xED\x5B|
    \xED\x5C|\xED\x5D|\xED\x5E|\xED\x5F|\xED\x60|\xED\x61|\xED\x62|\xED\x63|\xED\x64|\xED\x65|
    \xED\x66|\xED\x67|\xED\x68|\xED\x69|\xED\x6A|\xED\x6B|\xED\x6C|\xED\x6D|\xED\x6E|\xED\x6F|
    \xED\x70|\xED\x71|\xED\x72|\xED\x73|\xED\x74|\xED\x75|\xED\x76|\xED\x77|\xED\x78|\xED\x79|
    \xED\x7A|\xED\x7B|\xED\x7C|\xED\x7D|\xED\x7E|\xED\x80|\xED\x81|\xED\x82|\xED\x83|\xED\x84|
    \xED\x85|\xED\x86|\xED\x87|\xED\x88|\xED\x89|\xED\x8A|\xED\x8B|\xED\x8C|\xED\x8D|\xED\x8E|
    \xED\x8F|\xED\x90|\xED\x91|\xED\x92|\xED\x93|\xED\x94|\xED\x95|\xED\x96|\xED\x97|\xED\x98|
    \xED\x99|\xED\x9A|\xED\x9B|\xED\x9C|\xED\x9D|\xED\x9E|\xED\x9F|\xED\xA0|\xED\xA1|\xED\xA2|
    \xED\xA3|\xED\xA4|\xED\xA5|\xED\xA6|\xED\xA7|\xED\xA8|\xED\xA9|\xED\xAA|\xED\xAB|\xED\xAC|
    \xED\xAD|\xED\xAE|\xED\xAF|\xED\xB0|\xED\xB1|\xED\xB2|\xED\xB3|\xED\xB4|\xED\xB5|\xED\xB6|
    \xED\xB7|\xED\xB8|\xED\xB9|\xED\xBA|\xED\xBB|\xED\xBC|\xED\xBD|\xED\xBE|\xED\xBF|\xED\xC0|
    \xED\xC1|\xED\xC2|\xED\xC3|\xED\xC4|\xED\xC5|\xED\xC6|\xED\xC7|\xED\xC8|\xED\xC9|\xED\xCA|
    \xED\xCB|\xED\xCC|\xED\xCD|\xED\xCE|\xED\xCF|\xED\xD0|\xED\xD1|\xED\xD2|\xED\xD3|\xED\xD4|
    \xED\xD5|\xED\xD6|\xED\xD7|\xED\xD8|\xED\xD9|\xED\xDA|\xED\xDB|\xED\xDC|\xED\xDD|\xED\xDE|
    \xED\xDF|\xED\xE0|\xED\xE1|\xED\xE2|\xED\xE3|\xED\xE4|\xED\xE5|\xED\xE6|\xED\xE7|\xED\xE8|
    \xED\xE9|\xED\xEA|\xED\xEB|\xED\xEC|\xED\xED|\xED\xEE|\xED\xEF|\xED\xF0|\xED\xF1|\xED\xF2|
    \xED\xF3|\xED\xF4|\xED\xF5|\xED\xF6|\xED\xF7|\xED\xF8|\xED\xF9|\xED\xFA|\xED\xFB|\xED\xFC|
    \xEE\x40|\xEE\x41|\xEE\x42|\xEE\x43|\xEE\x44|\xEE\x45|\xEE\x46|\xEE\x47|\xEE\x48|\xEE\x49|
    \xEE\x4A|\xEE\x4B|\xEE\x4C|\xEE\x4D|\xEE\x4E|\xEE\x4F|\xEE\x50|\xEE\x51|\xEE\x52|\xEE\x53|
    \xEE\x54|\xEE\x55|\xEE\x56|\xEE\x57|\xEE\x58|\xEE\x59|\xEE\x5A|\xEE\x5B|\xEE\x5C|\xEE\x5D|
    \xEE\x5E|\xEE\x5F|\xEE\x60|\xEE\x61|\xEE\x62|\xEE\x63|\xEE\x64|\xEE\x65|\xEE\x66|\xEE\x67|
    \xEE\x68|\xEE\x69|\xEE\x6A|\xEE\x6B|\xEE\x6C|\xEE\x6D|\xEE\x6E|\xEE\x6F|\xEE\x70|\xEE\x71|
    \xEE\x72|\xEE\x73|\xEE\x74|\xEE\x75|\xEE\x76|\xEE\x77|\xEE\x78|\xEE\x79|\xEE\x7A|\xEE\x7B|
    \xEE\x7C|\xEE\x7D|\xEE\x7E|\xEE\x80|\xEE\x81|\xEE\x82|\xEE\x83|\xEE\x84|\xEE\x85|\xEE\x86|
    \xEE\x87|\xEE\x88|\xEE\x89|\xEE\x8A|\xEE\x8B|\xEE\x8C|\xEE\x8D|\xEE\x8E|\xEE\x8F|\xEE\x90|
    \xEE\x91|\xEE\x92|\xEE\x93|\xEE\x94|\xEE\x95|\xEE\x96|\xEE\x97|\xEE\x98|\xEE\x99|\xEE\x9A|
    \xEE\x9B|\xEE\x9C|\xEE\x9D|\xEE\x9E|\xEE\x9F|\xEE\xA0|\xEE\xA1|\xEE\xA2|\xEE\xA3|\xEE\xA4|
    \xEE\xA5|\xEE\xA6|\xEE\xA7|\xEE\xA8|\xEE\xA9|\xEE\xAA|\xEE\xAB|\xEE\xAC|\xEE\xAD|\x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C\x46|\xFC\x47|
    \xFC\x48|\xFC\x49|\xFC\x4A|\xFC\x4B
            )$'x) {
            }
            else {
                my $got = 
                $cp932_hex;
                if (Jacode4e::RoundTrip::convert(\$got,'cp932','cp932',{'INPUT_LAYOUT'=>'D'})) {
                    if ($got ne "\x81\xAC") {
                        push @test, [$cp932_hex,'cp932x','cp932',{'INPUT_LAYOUT'=>'D'}, $cp932x_hex],
                    }
                }
            }
        }
    }
    $|=1; print "1..",scalar(@test),"\n"; my $testno=1; sub ok { print $_[0]?'ok ':'not ok ',$testno++,$_[1]?" - $_[1]\n":"\n" }
}

use Jacode4e::RoundTrip;

for my $test (@test) {
    my($give,$OUTPUT_encoding,$INPUT_encoding,$option,$want) = @{$test};
    my $got = $give;
    my $return = Jacode4e::RoundTrip::convert(\$got,$OUTPUT_encoding,$INPUT_encoding,$option);

    my $option_content = '';
    if (defined $option) {
        $option_content .= qq{INPUT_LAYOUT=>$option->{'INPUT_LAYOUT'}}        if exists $option->{'INPUT_LAYOUT'};
        $option_content .= qq{OUTPUT_SHIFTING=>$option->{'OUTPUT_SHIFTING'}}  if exists $option->{'OUTPUT_SHIFTING'};
        $option_content .= qq{SPACE=>@{[uc unpack('H*',$option->{'SPACE'})]}} if exists $option->{'SPACE'};
        $option_content .= qq{GETA=>@{[uc unpack('H*',$option->{'GETA'})]}}   if exists $option->{'GETA'};
        $option_content = "{$option_content}";
    }

    ok(($return > 0) and ($got eq $want),
        sprintf(qq{$INPUT_encoding(%s) to $OUTPUT_encoding(%s), $option_content => return=$return,got=(%s)},
            uc unpack('H*',$give),
            uc unpack('H*',$want),
            uc unpack('H*',$got),
        )
    );
}

__END__
