Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/nio/cs/FindOneCharEncoderBugs.java
41149 views
1
/*
2
* Copyright (c) 2008, 2014, 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 5058133 6233345 6381699 6381702 6381705 6381706
27
* @summary Check that all one-char sequences can be encoded by all charsets
28
* @run main/timeout=1200 FindOneCharEncoderBugs
29
* @author Martin Buchholz
30
*/
31
32
import java.util.*;
33
import java.nio.*;
34
import java.nio.charset.*;
35
36
public class FindOneCharEncoderBugs {
37
final static String[] brokenCharsets = {
38
// Delete the following lines when these charsets are fixed!
39
"x-IBM970",
40
"x-COMPOUND_TEXT", // Direct buffers not supported
41
};
42
43
private static boolean equals(byte[] ba, ByteBuffer bb) {
44
if (ba.length != bb.limit())
45
return false;
46
for (int i = 0; i < ba.length; i++)
47
if (ba[i] != bb.get(i))
48
return false;
49
return true;
50
}
51
52
private static String toString(byte[] bytes) {
53
final StringBuilder sb = new StringBuilder();
54
for (byte b : bytes) {
55
if (sb.length() != 0) sb.append(' ');
56
sb.append(String.format("%02x", (int)b));
57
}
58
return sb.toString();
59
}
60
61
private static String toString(ByteBuffer bb) {
62
final StringBuilder sb = new StringBuilder();
63
for (int i = 0; i < bb.limit(); i++) {
64
if (sb.length() != 0) sb.append(' ');
65
sb.append(String.format("%02x", (int)bb.get(i)));
66
}
67
return sb.toString();
68
}
69
70
private static ByteBuffer convert(Charset cs, char c, CharBuffer cb) throws Throwable {
71
cb.clear(); cb.put(c); cb.flip();
72
return cs.newEncoder()
73
.onUnmappableCharacter(CodingErrorAction.REPLACE)
74
.onMalformedInput(CodingErrorAction.REPLACE)
75
.encode(cb);
76
}
77
78
/** Returns a direct CharBuffer with the same capacity as ordinary CharBuffer ocb */
79
private static CharBuffer directCharBuffer(CharBuffer ocb) {
80
final CharBuffer dcb =
81
ByteBuffer.allocateDirect(ocb.capacity() * Character.SIZE / Byte.SIZE)
82
.asCharBuffer();
83
check(! ocb.isDirect());
84
check( dcb.isDirect());
85
equal(ocb.capacity(), dcb.capacity());
86
return dcb;
87
}
88
89
private static void testChar(byte[] expected, CharBuffer cb, Charset cs, char c) {
90
try {
91
final ByteBuffer bb = convert(cs, c, cb);
92
if (! equals(expected, bb))
93
fail("bytes differ charset=%s direct=%s char=\\u%04x%n%s%n%s",
94
cs, cb.isDirect(), (int)c,
95
toString(expected), toString(bb));
96
} catch (Throwable t) {
97
System.out.printf("Unexpected exception charset=%s direct=%s char=\\u%04x%n",
98
cs, cb.isDirect(), (int)c);
99
unexpected(t);
100
failed++;
101
}
102
}
103
104
private static void testCharset(Charset cs) throws Throwable {
105
if (! cs.canEncode())
106
return;
107
108
final String csn = cs.name();
109
110
for (String n : brokenCharsets)
111
if (csn.equals(n)) {
112
System.out.printf("Skipping possibly broken charset %s%n", csn);
113
return;
114
}
115
System.out.println(csn);
116
117
final char[] theChar = new char[1];
118
final CharBuffer ocb = CharBuffer.allocate(1);
119
final CharBuffer dcb = directCharBuffer(ocb);
120
final int maxFailuresPerCharset = 5;
121
final int failed0 = failed;
122
123
for (char c = '\u0000';
124
(c+1 != 0x10000) && (failed - failed0 < maxFailuresPerCharset);
125
c++) {
126
theChar[0] = c;
127
byte[] bytes = new String(theChar).getBytes(csn);
128
if (bytes.length == 0)
129
fail("Empty output?! charset=%s char=\\u%04x", cs, (int)c);
130
testChar(bytes, ocb, cs, c);
131
testChar(bytes, dcb, cs, c);
132
}
133
}
134
135
private static void realMain(String[] args) {
136
for (Charset cs : Charset.availableCharsets().values()) {
137
try { testCharset(cs); }
138
catch (Throwable t) { unexpected(t); }
139
}
140
}
141
142
//--------------------- Infrastructure ---------------------------
143
static volatile int passed = 0, failed = 0;
144
static void pass() {passed++;}
145
static void fail() {failed++; Thread.dumpStack();}
146
static void fail(String format, Object... args) {
147
System.out.println(String.format(format, args)); failed++;}
148
static void fail(String msg) {System.out.println(msg); fail();}
149
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
150
static void check(boolean cond) {if (cond) pass(); else fail();}
151
static void equal(Object x, Object y) {
152
if (x == null ? y == null : x.equals(y)) pass();
153
else fail(x + " not equal to " + y);}
154
public static void main(String[] args) throws Throwable {
155
try {realMain(args);} catch (Throwable t) {unexpected(t);}
156
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
157
if (failed > 0) throw new AssertionError("Some tests failed");}
158
private static abstract class CheckedThread extends Thread {
159
abstract void realRun() throws Throwable;
160
public void run() {
161
try {realRun();} catch (Throwable t) {unexpected(t);}}}
162
}
163
164