test/jdk/sun/nio/cs/TestCp834_SBCS.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 48688 08b5eb52ccfd
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10343
diff changeset
     2
 * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     4
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     8
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    13
 * accompanied this code).
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    14
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    21
 * questions.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    22
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    23
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    24
/* @test
48688
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    25
 * @bug 6379808 8166339
44115
bb4e971bf5d4 8176195: Fix misc module dependencies in jdk_core tests
xiaofeya
parents: 14342
diff changeset
    26
 * @summary Check all Cp933 SBCS characters are not supported in Cp834
bb4e971bf5d4 8176195: Fix misc module dependencies in jdk_core tests
xiaofeya
parents: 14342
diff changeset
    27
 * @modules jdk.charsets
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    28
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    29
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
import java.io.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
import java.nio.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
import java.nio.charset.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
public class TestCp834_SBCS {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
    public static void main(String args[]) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
        // The correctness of 1:1 mapping is Coverted by CoderTest.java
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
        // and TestConv.java, we only need to verify that SBCS characters
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
        // are not supported by this charset.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
        CharsetEncoder enc834 = Charset.forName("Cp834")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
                                       .newEncoder()
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
                                       .onUnmappableCharacter(CodingErrorAction.REPLACE)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
                                       .onMalformedInput(CodingErrorAction.REPLACE);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
        CharsetDecoder dec834 = Charset.forName("Cp834")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
                                       .newDecoder()
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
                                       .onUnmappableCharacter(CodingErrorAction.REPLACE)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
                                       .onMalformedInput(CodingErrorAction.REPLACE);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
        CharsetDecoder dec933 = Charset.forName("Cp933")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
                                       .newDecoder()
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
                                       .onUnmappableCharacter(CodingErrorAction.REPLACE)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
                                       .onMalformedInput(CodingErrorAction.REPLACE);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
        byte[] ba = new byte[1];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
        byte[] ba2 = new byte[2];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
        ByteBuffer dbb = ByteBuffer.allocateDirect(10);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
        char[] ca = new char[1];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
        char c;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
        for (int i = 0; i <= 0xff; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
            if (i != 0xe && i != 0xf) {   // no SI/SO
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
                ba[0] = (byte)i;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
                CharBuffer cb = dec933.decode(ByteBuffer.wrap(ba));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
                if ((c = cb.get()) != '\ufffd') {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
                    // OK, this is a SBCS character in Cp933
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
                    if (dec834.decode(ByteBuffer.wrap(ba)).get() != '\ufffd')
48688
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    65
                        throw new RuntimeException("SBCS is supported in IBM834 decoder");
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
                    if (enc834.canEncode(c))
48688
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    68
                        throw new RuntimeException("SBCS can be encoded in IBM834 encoder");
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
                    ca[0] = c;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
                    ByteBuffer bb = enc834.encode(CharBuffer.wrap(ca));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
                    if (bb.get() != (byte)0xfe || bb.get() != (byte)0xfe)
48688
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    73
                        throw new RuntimeException("SBCS is supported in IBM834 encoder");
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
        }
48688
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    77
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    78
        // 8166339: cp834 should handle unmappable bytes as dobule-byte pair.
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    79
        if (! new String("\ufffd".getBytes("cp834"), "cp834").equals("\ufffd")) {
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    80
            throw new RuntimeException("u+fffd roundtrip failed");
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    81
        }
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    82
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    83
        if (! new String("a".getBytes("cp834"), "cp834").equals("\ufffd") ||
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    84
            ! new String(new byte[] { 0x41, 0x40}, "cp834").equals("\ufffd")) {
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    85
            throw new RuntimeException("decoding unmappable don't return u+fffd");
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    86
        }
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    87
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    88
        CoderResult cr = Charset.forName("Cp834").newDecoder().decode(
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    89
            ByteBuffer.wrap(new byte[] { 0x41, 0x40}), CharBuffer.wrap(new char[2]), true);
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    90
        if (cr.isError() && cr.length() != 2) {
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    91
            throw new RuntimeException("decoding unmappable don't return unmmappable(2)");
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    92
        }
08b5eb52ccfd 8166339: Code conversion working behavior was changed for x-IBM834
sherman
parents: 47216
diff changeset
    93
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
}