Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/hotspot/jtreg/compiler/intrinsics/string/TestEncodeIntrinsics.java
64495 views
1
/*
2
* Copyright (c) 2013, 2021, 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
* @key randomness
27
* @bug 6896617 8274242
28
* @summary Verify potentially intrinsified encoders behave well before and after compilation
29
* @library /test/lib
30
*
31
* @run main/othervm/timeout=1200 --add-opens=java.base/sun.nio.cs=ALL-UNNAMED -Xbatch -Xmx256m compiler.intrinsics.string.TestEncodeIntrinsics
32
*/
33
34
package compiler.intrinsics.string;
35
36
import jdk.test.lib.Utils;
37
38
import java.lang.reflect.Method;
39
import java.nio.ByteBuffer;
40
import java.nio.CharBuffer;
41
import java.nio.charset.Charset;
42
import java.nio.charset.CharsetDecoder;
43
import java.nio.charset.CharsetEncoder;
44
import java.nio.charset.CodingErrorAction;
45
import java.util.Arrays;
46
import java.util.Random;
47
48
public class TestEncodeIntrinsics {
49
final static int SIZE = 256;
50
51
public static void main(String[] args) {
52
53
test("ISO-8859-1", false);
54
test("UTF-8", true);
55
test("US-ASCII", true);
56
test("CESU-8", true);
57
}
58
59
private static void test(String csn, boolean asciiOnly) {
60
try {
61
System.out.println("Testing " + csn);
62
Charset cs = Charset.forName(csn);
63
CharsetEncoder enc = cs.newEncoder();
64
enc.onMalformedInput(CodingErrorAction.REPLACE)
65
.onUnmappableCharacter(CodingErrorAction.REPLACE);
66
CharsetDecoder dec = cs.newDecoder();
67
dec.onMalformedInput(CodingErrorAction.REPLACE)
68
.onUnmappableCharacter(CodingErrorAction.REPLACE);
69
70
byte repl = (byte) '?';
71
enc.replaceWith(new byte[]{repl});
72
73
// Populate char[] with chars which can be encoded by ISO_8859_1 (<= 0xFF)
74
// - or ASCII (<= 0x7F) if requested
75
Random rnd = Utils.getRandomInstance();
76
int maxchar = asciiOnly ? 0x7F : 0xFF;
77
char[] a = new char[SIZE];
78
byte[] b = new byte[SIZE];
79
char[] at = new char[SIZE];
80
byte[] bt = new byte[SIZE];
81
for (int i = 0; i < SIZE; i++) {
82
char c = (char) rnd.nextInt(maxchar);
83
if (!enc.canEncode(c)) {
84
System.out.printf("Something wrong: can't encode c=%03x\n", (int) c);
85
System.exit(97);
86
}
87
a[i] = c;
88
b[i] = (byte) c;
89
at[i] = (char) -1;
90
bt[i] = (byte) -1;
91
}
92
93
Method encodeArray = null;
94
if (csn.equals("ISO-8859-1")) {
95
// Use internal API for tests
96
encodeArray = enc.getClass().getDeclaredMethod("encodeISOArray",
97
char[].class, int.class, byte[].class, int.class, int.class);
98
encodeArray.setAccessible(true);
99
if ((int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE || !Arrays.equals(b, bt)) {
100
System.out.println("Something wrong: ArrayEncoder.encode failed");
101
System.exit(97);
102
}
103
for (int i = 0; i < SIZE; i++) {
104
at[i] = (char) -1;
105
}
106
}
107
108
ByteBuffer bb = ByteBuffer.wrap(b);
109
CharBuffer ba = CharBuffer.wrap(a);
110
ByteBuffer bbt = ByteBuffer.wrap(bt);
111
CharBuffer bat = CharBuffer.wrap(at);
112
if (!enc.encode(ba, bbt, true).isUnderflow() || !Arrays.equals(b, bt)) {
113
System.out.println("Something wrong: Encoder.encode failed");
114
System.exit(97);
115
}
116
if (!dec.decode(bb, bat, true).isUnderflow() || !Arrays.equals(a, at)) {
117
System.out.println("Something wrong: Decoder.decode failed (a == at: " + !Arrays.equals(a, at) + ")");
118
System.exit(97);
119
}
120
for (int i = 0; i < SIZE; i++) {
121
at[i] = (char) -1;
122
bt[i] = (byte) -1;
123
}
124
125
// Warm up
126
boolean failed = false;
127
128
if (csn.equals("ISO-8859-1")) {
129
for (int i = 0; i < 10000; i++) {
130
failed |= (int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE;
131
}
132
for (int i = 0; i < 10000; i++) {
133
failed |= (int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE;
134
}
135
for (int i = 0; i < 10000; i++) {
136
failed |= (int) encodeArray.invoke(enc, a, 0, bt, 0, SIZE) != SIZE;
137
}
138
if (failed || !Arrays.equals(b, bt)) {
139
failed = true;
140
System.out.println("Failed: ISO_8859_1$Encoder.encode char[" + SIZE + "]");
141
}
142
}
143
144
for (int i = 0; i < SIZE; i++) {
145
at[i] = (char) -1;
146
bt[i] = (byte) -1;
147
}
148
149
boolean is_underflow = true;
150
for (int i = 0; i < 10000; i++) {
151
ba.clear();
152
bb.clear();
153
bat.clear();
154
bbt.clear();
155
boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();
156
boolean dec_res = dec.decode(bb, bat, true).isUnderflow();
157
is_underflow = is_underflow && enc_res && dec_res;
158
}
159
for (int i = 0; i < SIZE; i++) {
160
at[i] = (char) -1;
161
bt[i] = (byte) -1;
162
}
163
for (int i = 0; i < 10000; i++) {
164
ba.clear();
165
bb.clear();
166
bat.clear();
167
bbt.clear();
168
boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();
169
boolean dec_res = dec.decode(bb, bat, true).isUnderflow();
170
is_underflow = is_underflow && enc_res && dec_res;
171
}
172
for (int i = 0; i < SIZE; i++) {
173
at[i] = (char) -1;
174
bt[i] = (byte) -1;
175
}
176
for (int i = 0; i < 10000; i++) {
177
ba.clear();
178
bb.clear();
179
bat.clear();
180
bbt.clear();
181
boolean enc_res = enc.encode(ba, bbt, true).isUnderflow();
182
boolean dec_res = dec.decode(bb, bat, true).isUnderflow();
183
is_underflow = is_underflow && enc_res && dec_res;
184
}
185
if (!is_underflow) {
186
failed = true;
187
System.out.println("Failed: got a non-underflow");
188
}
189
if (!Arrays.equals(b, bt)) {
190
failed = true;
191
System.out.println("Failed: b != bt");
192
}
193
if (!Arrays.equals(a, at)) {
194
failed = true;
195
System.out.println("Failed: a != at");
196
}
197
198
// Test encoder with chars outside of the range the intrinsic deals with
199
System.out.println("Testing big char");
200
201
bt = new byte[SIZE + 10]; // add some spare room to deal with encoding multi-byte
202
ba = CharBuffer.wrap(a);
203
bbt = ByteBuffer.wrap(bt);
204
for (int i = 1; i <= SIZE; i++) {
205
for (int j = 0; j < i; j++) {
206
char bigChar = (char)((asciiOnly ? 0x7F : 0xFF) + 1 + rnd.nextInt(0x100));
207
char aOrig = a[j];
208
a[j] = bigChar;
209
// make sure to replace with a different byte
210
bt[j] = (byte)(bt[j] + 1);
211
ba.clear();
212
ba.limit(i);
213
bbt.clear();
214
if (!enc.encode(ba, bbt, true).isUnderflow()) {
215
failed = true;
216
System.out.println("Failed: encode char[" + i + "] to byte[" + i + "]: expected underflow");
217
}
218
if (bt[j] == b[j] && b[j] != repl) { // b[j] can be equal to repl; ignore
219
failed = true;
220
System.out.println("Failed: different byte expected at pos bt[" + j + "]");
221
}
222
if (!enc.canEncode(bigChar) && bt[j] != repl) {
223
failed = true;
224
System.out.println("Failed: encoded replace byte[" + j + "] (" + bt[j] + ") != " + repl);
225
}
226
227
// Check that all bytes prior to the replaced one was encoded properly
228
for (int k = 0; k < j; k++) {
229
if (bt[k] != b[k]) {
230
failed = true;
231
System.out.println("Failed: encoded byte[" + k + "] (" + bt[k] + ") != " + b[k]);
232
}
233
}
234
a[j] = aOrig; // Restore
235
}
236
}
237
238
if (failed) {
239
System.out.println("FAILED");
240
System.exit(97);
241
}
242
System.out.println("PASSED");
243
} catch (Exception e) {
244
e.printStackTrace();
245
System.out.println("FAILED");
246
System.exit(97);
247
}
248
}
249
}
250
251