Path: blob/master/test/jdk/sun/nio/cs/SurrogateGB18030Test.java
41152 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 489645425@summary Check GB18030 surrogate encoding/decoding handling26*/2728import java.nio.*;29import java.nio.charset.*;3031public class SurrogateGB18030Test {32public static void main(String[] args) throws Exception {33SurrogateGB18030Test test = new SurrogateGB18030Test();3435test.roundtripTest();3637/**38* Valid Surrogate pair and 4 byte GB18030 representation39*/4041String inputString = "\uD800\uDC00";4243byte[] expectedBytes = { (byte)0x90,44(byte)0x30,45(byte)0x81,46(byte)0x3047};48test.encodeTest(inputString, expectedBytes);4950/**51* Vice-versa : check that 4 byte GB18030 value encodes correctly52*/5354String expectedStr = "\uDBFF\uDFFF";5556byte[] inputBytes = { (byte)0xe3,57(byte)0x32,58(byte)0x9a,59(byte)0x3560};616263test.decodeTest(inputBytes, expectedStr);6465}6667private void roundtripTest() throws Exception68{69byte[] ba;70char[] pair = new char[2];71for (char high = '\ud800'; high <= '\udbff'; high++) {72for (char low = '\udc00'; low <= '\udfff'; low++) {73pair[0] = high;74pair[1] = low;75String s = new String(pair);76if (!s.equals(new String(s.getBytes("gb18030"), "gb18030")))77throw new Exception ("GB18030 roundtrip failure");78}79}8081}8283private void encodeTest(String inputString, byte[] expectedBytes)84throws Exception85{86byte[] encoded = inputString.getBytes("GB18030");8788CharBuffer cb = CharBuffer.wrap(inputString.toCharArray());89ByteBuffer bb = ByteBuffer.allocate(4);9091CharsetEncoder encoder = Charset.forName("GB18030").newEncoder();92encoder.encode(cb, bb, true);9394bb.flip();95for (int i = 0 ; i < expectedBytes.length; i++) {96if (encoded[i] != expectedBytes[i]97|| bb.get() != expectedBytes[i])98throw new Exception ("GB18030 encode failure");99}100}101102private void decodeTest(byte[] inputBytes, String expectedStr)103throws Exception104{105String s2 = new String(inputBytes, "GB18030");106107CharsetDecoder decoder = Charset.forName("GB18030").newDecoder();108109ByteBuffer bb = ByteBuffer.wrap(inputBytes);110CharBuffer cb = CharBuffer.allocate(2);111decoder.decode(bb, cb, true);112113cb.flip();114for (int i = 0 ; i < expectedStr.length(); i++) {115if (expectedStr.charAt(i) != cb.get()116|| s2.charAt(i) != expectedStr.charAt(i))117throw new Exception ("GB18030 encode failure");118}119}120}121122123