1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8232161
27  * @summary Those test data confirm the preferred b2c irreversible mappings defined in MS950.nr file.
28  */
29 
30 import java.nio.charset.Charset;
31 import java.nio.CharBuffer;
32 import java.nio.ByteBuffer;
33 import java.util.Arrays;
34 
35 public class TestMS950 {
36     // Data is listed from make/data/charsetmapping/MS950.map
37     private final static String[] MS950B2C = new String[] {
38         "0xF9FA  0x256D",
39         "0xF9FB  0x256E",
40         "0xF9FC  0x2570",
41         "0xF9FD  0x256F",
42         "0xA2CC  0x5341",
43         "0xA2CE  0x5345",
44         "0xF9F9  0x2550",
45         "0xF9E9  0x255E",
46         "0xF9EA  0x256A",
47         "0xF9EB  0x2561",
48         "0xA27E  0x256D",
49         "0xA2A1  0x256E",
50         "0xA2A2  0x2570",
51         "0xA2A3  0x256F",
52         "0xA451  0x5341",
53         "0xA4CA  0x5345",
54         "0xA2A4  0x2550",
55         "0xA2A5  0x255E",
56         "0xA2A6  0x256A",
57         "0xA2A7  0x2561",
58     };
59 
60     // Data is listed from MS950.map
61     // Col1 should be in MS950.nr
62     // (Only check col2 and col3)
63     private final static String[] MS950C2B= new String[] {
64         "0xF9FA -> u256D -> 0xA27E",
65         "0xF9FB -> u256E -> 0xA2A1",
66         "0xF9FC -> u2570 -> 0xA2A2",
67         "0xF9FD -> u256F -> 0xA2A3",
68         "0xA2CC -> u5341 -> 0xA451",
69         "0xA2CE -> u5345 -> 0xA4CA",
70         "0xA2A4 -> u2550 -> 0xF9F9",
71         "0xA2A5 -> u255E -> 0xF9E9",
72         "0xA2A6 -> u256A -> 0xF9EA",
73         "0xA2A7 -> u2561 -> 0xF9EB",
74     };
75 
76     // Convert Hex string to byte array
hex2ba(String s)77     private static byte[] hex2ba(String s) {
78         byte[] ba;
79         if (s.startsWith("0x")) {
80             s = s.substring(2);
81         }
82         try {
83             ByteBuffer bb = ByteBuffer.allocate((int)(s.length()/2));
84             StringBuilder sb = new StringBuilder(s.substring(0, bb.limit() * 2));
85             while (sb.length() > 0) {
86                 bb.put((byte)Integer.parseInt(sb.substring(0, 2), 16));
87                 sb.delete(0, 2);
88             }
89             ba = bb.array();
90         } catch (NumberFormatException nfe) {
91             ba = new byte[0];
92         }
93         return ba;
94     }
95 
96     // Convert Hex string to string
hex2s(String s)97     private static String hex2s(String s) {
98         char[] ca;
99         if (s.startsWith("0x")) {
100             s = s.substring(2);
101         } else if (s.startsWith("u")) {
102             s = s.substring(1);
103         }
104         try {
105             CharBuffer cb = CharBuffer.allocate((int)(s.length()/4));
106             StringBuilder sb = new StringBuilder(s.substring(0, cb.limit() * 4));
107             while (sb.length() > 0) {
108                 cb.put((char)Integer.parseInt(sb.substring(0, 4), 16));
109                 sb.delete(0,4);
110             }
111             ca = cb.array();
112         } catch (NumberFormatException nfe) {
113             ca = new char[0];
114         }
115         return new String(ca);
116     }
117 
main(String[] args)118     public static void main(String[] args) throws Exception {
119         Charset cs = Charset.forName("MS950");
120         int diffs = 0;
121         // Check b2c side: Duplicated entries
122         for(int i = 0; i < MS950B2C.length; ++i) {
123             String[] sa = MS950B2C[i].split("\\s+");
124             String s = new String(hex2ba(sa[0]), cs);
125             if (!s.equals(hex2s(sa[1]))) {
126                 ++diffs;
127                 System.out.printf("b2c: %s, expected:%s, result:0x", sa[0], sa[1]);
128                 for (char ch : s.toCharArray()) {
129                     System.out.printf("%04X", (int)ch);
130                 }
131                 System.out.println();
132             }
133         }
134         // Check c2b side: round-trip entries
135         for(int i = 0; i < MS950C2B.length; ++i) {
136             String[] sa = MS950C2B[i].split("\\s+->\\s+");
137             byte[] ba = hex2s(sa[1]).getBytes(cs);
138             if (!Arrays.equals(ba, hex2ba(sa[2]))) {
139                 ++diffs;
140                 System.out.printf("c2b: %s, expected:%s, result:0x", sa[1], sa[2]);
141                 for (byte b : ba) {
142                     System.out.printf("%02X", (int)b & 0xFF);
143                 }
144                 System.out.println();
145             }
146         }
147         if (diffs > 0) {
148             throw new Exception("Failed");
149         }
150     }
151 }
152