1 /* |
|
2 * Copyright (c) 2009, 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 1234567 |
|
27 * @summary Test updated X11 charsets |
|
28 * @build X11GB2312_OLD X11GBK_OLD X11KSC5601_OLD |
|
29 */ |
|
30 |
|
31 import java.nio.charset.*; |
|
32 import java.nio.*; |
|
33 import java.util.*; |
|
34 |
|
35 public class TestX11CS { |
|
36 |
|
37 static char[] decode(byte[] bb, Charset cs) |
|
38 throws Exception { |
|
39 CharsetDecoder dec = cs.newDecoder(); |
|
40 ByteBuffer bbf = ByteBuffer.wrap(bb); |
|
41 CharBuffer cbf = CharBuffer.allocate(bb.length); |
|
42 CoderResult cr = dec.decode(bbf, cbf, true); |
|
43 if (cr != CoderResult.UNDERFLOW) { |
|
44 System.out.println("DEC-----------------"); |
|
45 int pos = bbf.position(); |
|
46 System.out.printf(" cr=%s, bbf.pos=%d, bb[pos]=%x,%x,%x,%x%n", |
|
47 cr.toString(), pos, |
|
48 bb[pos++]&0xff, bb[pos++]&0xff,bb[pos++]&0xff, bb[pos++]&0xff); |
|
49 throw new RuntimeException("Decoding err: " + cs.name()); |
|
50 } |
|
51 char[] cc = new char[cbf.position()]; |
|
52 cbf.flip(); cbf.get(cc); |
|
53 return cc; |
|
54 |
|
55 } |
|
56 |
|
57 static byte[] encode(char[] cc, Charset cs) |
|
58 throws Exception { |
|
59 ByteBuffer bbf = ByteBuffer.allocate(cc.length * 4); |
|
60 CharBuffer cbf = CharBuffer.wrap(cc); |
|
61 CharsetEncoder enc = cs.newEncoder(); |
|
62 |
|
63 CoderResult cr = enc.encode(cbf, bbf, true); |
|
64 if (cr != CoderResult.UNDERFLOW) { |
|
65 System.out.println("ENC-----------------"); |
|
66 int pos = cbf.position(); |
|
67 System.out.printf(" cr=%s, cbf.pos=%d, cc[pos]=%x%n", |
|
68 cr.toString(), pos, cc[pos]&0xffff); |
|
69 throw new RuntimeException("Encoding err: " + cs.name()); |
|
70 } |
|
71 byte[] bb = new byte[bbf.position()]; |
|
72 bbf.flip(); bbf.get(bb); |
|
73 return bb; |
|
74 } |
|
75 |
|
76 static char[] getChars(Charset newCS, Charset oldCS) { |
|
77 CharsetEncoder enc = oldCS.newEncoder(); |
|
78 CharsetEncoder encNew = newCS.newEncoder(); |
|
79 char[] cc = new char[0x10000]; |
|
80 int pos = 0; |
|
81 int i = 0; |
|
82 while (i < 0x10000) { |
|
83 if (enc.canEncode((char)i) != encNew.canEncode((char)i)) { |
|
84 System.out.printf(" Err i=%x%n", i); |
|
85 //throw new RuntimeException("canEncode() err!"); |
|
86 } |
|
87 if (enc.canEncode((char)i)) { |
|
88 cc[pos++] = (char)i; |
|
89 } |
|
90 i++; |
|
91 } |
|
92 return Arrays.copyOf(cc, pos); |
|
93 } |
|
94 |
|
95 static void compare(Charset newCS, Charset oldCS) throws Exception { |
|
96 System.out.printf(" Diff <%s> <%s>...%n", newCS.name(), oldCS.name()); |
|
97 char[] cc = getChars(newCS, oldCS); |
|
98 |
|
99 byte[] bb1 = encode(cc, newCS); |
|
100 byte[] bb2 = encode(cc, oldCS); |
|
101 |
|
102 if (!Arrays.equals(bb1, bb2)) { |
|
103 System.out.printf(" encoding failed!%n"); |
|
104 } |
|
105 char[] cc1 = decode(bb1, newCS); |
|
106 char[] cc2 = decode(bb1, oldCS); |
|
107 if (!Arrays.equals(cc1, cc2)) { |
|
108 for (int i = 0; i < cc1.length; i++) { |
|
109 if (cc1[i] != cc2[i]) { |
|
110 System.out.printf("i=%d, cc1=%x cc2=%x, bb=<%x%x>%n", |
|
111 i, |
|
112 cc1[i]&0xffff, cc2[i]&0xffff, |
|
113 bb1[i*2]&0xff, bb1[i*2+1]&0xff); |
|
114 } |
|
115 |
|
116 } |
|
117 |
|
118 System.out.printf(" decoding failed%n"); |
|
119 } |
|
120 } |
|
121 |
|
122 public static void main(String[] args) throws Exception { |
|
123 compare(new sun.awt.motif.X11GBK(), |
|
124 new X11GBK_OLD()); |
|
125 |
|
126 compare(new sun.awt.motif.X11GB2312(), |
|
127 new X11GB2312_OLD()); |
|
128 |
|
129 compare(new sun.awt.motif.X11KSC5601(), |
|
130 new X11KSC5601_OLD()); |
|
131 |
|
132 } |
|
133 } |
|