Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/StringBuffer/Supplementary.java
41149 views
1
/*
2
* Copyright (c) 2003, 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
*
26
* @test
27
* @bug 4533872 4915683 4985217 5017280 6937112
28
* @summary Unit tests for supplementary character support (JSR-204)
29
*/
30
31
import java.util.HexFormat;
32
33
public class Supplementary {
34
35
public static void main(String[] args) {
36
test1(); // Test for codePointAt(int index)
37
test2(); // Test for codePointBefore(int index)
38
test3(); // Test for reverse()
39
test4(); // Test for appendCodePoint(int codePoint)
40
test5(); // Test for codePointCount(int beginIndex, int endIndex)
41
test6(); // Test for offsetByCodePoints(int index, int offset)
42
}
43
44
/* Text strings which are used as input data.
45
* The comment above each text string means the index of each 16-bit char
46
* for convenience.
47
*/
48
static final String[] input = {
49
/* 111 1 111111 22222
50
0123 4 5678 9 012 3 456789 01234 */
51
"abc\uD800\uDC00def\uD800\uD800ab\uD800\uDC00cdefa\uDC00bcdef",
52
/* 1 1111 1111 1 222
53
0 12345 6789 0 1234 5678 9 012 */
54
"\uD800defg\uD800hij\uD800\uDC00klm\uDC00nop\uDC00\uD800rt\uDC00",
55
/* 11 1 1111 1 112 222
56
0 12345 6 78901 2 3456 7 890 123 */
57
"\uDC00abcd\uDBFF\uDFFFefgh\uD800\uDC009ik\uDC00\uDC00lm\uDC00no\uD800",
58
/* 111 111111 1 22 2
59
0 1 2345 678 9 012 345678 9 01 2 */
60
"\uD800\uDC00!#$\uD800%&\uD800\uDC00;+\uDC00<>;=^\uDC00\\@\uD800\uDC00",
61
62
// includes an undefined supplementary character in Unicode 4.0.0
63
/* 1 11 1 1111 1
64
0 1 2345 6 789 0 12 3 4567 8 */
65
"\uDB40\uDE00abc\uDE01\uDB40de\uDB40\uDE02f\uDB40\uDE03ghi\uDB40\uDE02",
66
};
67
68
69
/* Expected results for:
70
* test1(): for codePointAt()
71
*
72
* Each character in each array is the golden data for each text string
73
* in the above input data. For example, the first data in each array is
74
* for the first input string.
75
*/
76
static final int[][] golden1 = {
77
{'a', 0xD800, 0xDC00, 0x10000, 0xE0200}, // codePointAt(0)
78
{0xD800, 0x10000, 'g', 0xDC00, 0xE0202}, // codePointAt(9)
79
{'f', 0xDC00, 0xD800, 0xDC00, 0xDE02}, // codePointAt(length-1)
80
};
81
82
/*
83
* Test for codePointAt(int index) method
84
*/
85
static void test1() {
86
87
for (int i = 0; i < input.length; i++) {
88
StringBuffer sb = new StringBuffer(input[i]);
89
90
/*
91
* Normal case
92
*/
93
testCodePoint(At, sb, 0, golden1[0][i]);
94
testCodePoint(At, sb, 9, golden1[1][i]);
95
testCodePoint(At, sb, sb.length()-1, golden1[2][i]);
96
97
/*
98
* Abnormal case - verify that an exception is thrown.
99
*/
100
testCodePoint(At, sb, -1);
101
testCodePoint(At, sb, sb.length());
102
}
103
}
104
105
106
/* Expected results for:
107
* test2(): for codePointBefore()
108
*
109
* Each character in each array is the golden data for each text string
110
* in the above input data. For example, the first data in each array is
111
* for the first input string.
112
*/
113
static final int[][] golden2 = {
114
{'a', 0xD800, 0xDC00, 0xD800, 0xDB40}, // codePointBefore(1)
115
{0xD800, 'l', 0x10000, 0xDC00, 0xDB40}, // codePointBefore(13)
116
{'f', 0xDC00, 0xD800, 0x10000, 0xE0202}, // codePointBefore(length)
117
};
118
119
/*
120
* Test for codePointBefore(int index) method
121
*/
122
static void test2() {
123
124
for (int i = 0; i < input.length; i++) {
125
StringBuffer sb = new StringBuffer(input[i]);
126
127
/*
128
* Normal case
129
*/
130
testCodePoint(Before, sb, 1, golden2[0][i]);
131
testCodePoint(Before, sb, 13, golden2[1][i]);
132
testCodePoint(Before, sb, sb.length(), golden2[2][i]);
133
134
/*
135
* Abnormal case - verify that an exception is thrown.
136
*/
137
testCodePoint(Before, sb, 0);
138
testCodePoint(Before, sb, sb.length()+1);
139
}
140
}
141
142
143
/* Expected results for:
144
* test3(): for reverse()
145
*
146
* Unlike golden1 and golden2, each array is the golden data for each text
147
* string in the above input data. For example, the first array is for
148
* the first input string.
149
*/
150
static final String[] golden3 = {
151
"fedcb\uDC00afedc\uD800\uDC00ba\uD800\uD800fed\uD800\uDC00cba",
152
"\uDC00tr\uD800\uDC00pon\uDC00mlk\uD800\uDC00jih\uD800gfed\uD800",
153
"\uD800on\uDC00ml\uDC00\uDC00ki9\uD800\uDC00hgfe\uDBFF\uDFFFdcba\uDC00",
154
"\uD800\uDC00@\\\uDC00^=;><\uDC00+;\uD800\uDC00&%\uD800$#!\uD800\uDC00",
155
156
// includes an undefined supplementary character in Unicode 4.0.0
157
"\uDB40\uDE02ihg\uDB40\uDE03f\uDB40\uDE02ed\uDB40\uDE01cba\uDB40\uDE00",
158
};
159
160
// Additional input data & expected result for test3()
161
static final String[][] testdata1 = {
162
{"a\uD800\uDC00", "\uD800\uDC00a"},
163
{"a\uDC00\uD800", "\uD800\uDC00a"},
164
{"\uD800\uDC00a", "a\uD800\uDC00"},
165
{"\uDC00\uD800a", "a\uD800\uDC00"},
166
{"\uDC00\uD800\uD801", "\uD801\uD800\uDC00"},
167
{"\uDC00\uD800\uDC01", "\uD800\uDC01\uDC00"},
168
{"\uD801\uD800\uDC00", "\uD800\uDC00\uD801"},
169
{"\uD800\uDC01\uDC00", "\uDC00\uD800\uDC01"},
170
{"\uD800\uDC00\uDC01\uD801", "\uD801\uDC01\uD800\uDC00"},
171
};
172
173
/*
174
* Test for reverse() method
175
*/
176
static void test3() {
177
for (int i = 0; i < input.length; i++) {
178
StringBuffer sb = new StringBuffer(input[i]).reverse();
179
180
check(!golden3[i].equals(new String(sb)),
181
"reverse() for <" + toHexString(input[i]) + ">",
182
sb, golden3[i]);
183
}
184
185
for (int i = 0; i < testdata1.length; i++) {
186
StringBuffer sb = new StringBuffer(testdata1[i][0]).reverse();
187
188
check(!testdata1[i][1].equals(new String(sb)),
189
"reverse() for <" + toHexString(testdata1[i][0]) + ">",
190
sb, testdata1[i][1]);
191
}
192
}
193
194
/**
195
* Test for appendCodePoint() method
196
*/
197
static void test4() {
198
for (int i = 0; i < input.length; i++) {
199
String s = input[i];
200
StringBuffer sb = new StringBuffer();
201
int c;
202
for (int j = 0; j < s.length(); j += Character.charCount(c)) {
203
c = s.codePointAt(j);
204
StringBuffer rsb = sb.appendCodePoint(c);
205
check(sb != rsb, "appendCodePoint returned a wrong object");
206
int sbc = sb.codePointAt(j);
207
check(sbc != c, "appendCodePoint(j) != c", sbc, c);
208
}
209
check(!s.equals(sb.toString()),
210
"appendCodePoint() produced a wrong result with input["+i+"]");
211
}
212
213
// test exception
214
testAppendCodePoint(-1, IllegalArgumentException.class);
215
testAppendCodePoint(Character.MAX_CODE_POINT+1, IllegalArgumentException.class);
216
}
217
218
/**
219
* Test codePointCount(int, int)
220
*
221
* This test case assumes that
222
* Character.codePointCount(CharSequence, int, int) works
223
* correctly.
224
*/
225
static void test5() {
226
for (int i = 0; i < input.length; i++) {
227
String s = input[i];
228
StringBuffer sb = new StringBuffer(s);
229
int length = sb.length();
230
for (int j = 0; j <= length; j++) {
231
int result = sb.codePointCount(j, length);
232
int expected = Character.codePointCount(sb, j, length);
233
check(result != expected, "codePointCount(input["+i+"], "+j+", "+length+")",
234
result, expected);
235
}
236
for (int j = length; j >= 0; j--) {
237
int result = sb.codePointCount(0, j);
238
int expected = Character.codePointCount(sb, 0, j);
239
check(result != expected, "codePointCount(input["+i+"], 0, "+j+")",
240
result, expected);
241
}
242
243
// test exceptions
244
testCodePointCount(null, 0, 0, NullPointerException.class);
245
testCodePointCount(sb, -1, length, IndexOutOfBoundsException.class);
246
testCodePointCount(sb, 0, length+1, IndexOutOfBoundsException.class);
247
testCodePointCount(sb, length, length-1, IndexOutOfBoundsException.class);
248
}
249
}
250
251
/**
252
* Test offsetByCodePoints(int, int)
253
*
254
* This test case assumes that
255
* Character.codePointCount(CharSequence, int, int) works
256
* correctly.
257
*/
258
static void test6() {
259
for (int i = 0; i < input.length; i++) {
260
String s = input[i];
261
StringBuffer sb = new StringBuffer(s);
262
int length = s.length();
263
for (int j = 0; j <= length; j++) {
264
int nCodePoints = Character.codePointCount(sb, j, length);
265
int result = sb.offsetByCodePoints(j, nCodePoints);
266
check(result != length,
267
"offsetByCodePoints(input["+i+"], "+j+", "+nCodePoints+")",
268
result, length);
269
result = sb.offsetByCodePoints(length, -nCodePoints);
270
int expected = j;
271
if (j > 0 && j < length) {
272
int cp = sb.codePointBefore(j+1);
273
if (Character.isSupplementaryCodePoint(cp)) {
274
expected--;
275
}
276
}
277
check(result != expected,
278
"offsetByCodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
279
result, expected);
280
}
281
for (int j = length; j >= 0; j--) {
282
int nCodePoints = Character.codePointCount(sb, 0, j);
283
int result = sb.offsetByCodePoints(0, nCodePoints);
284
int expected = j;
285
if (j > 0 && j < length) {
286
int cp = sb.codePointAt(j-1);
287
if (Character.isSupplementaryCodePoint(cp)) {
288
expected++;
289
}
290
}
291
check(result != expected,
292
"offsetByCodePoints(input["+i+"], 0, "+nCodePoints+")",
293
result, expected);
294
result = sb.offsetByCodePoints(j, -nCodePoints);
295
check(result != 0,
296
"offsetBycodePoints(input["+i+"], "+j+", "+(-nCodePoints)+")",
297
result, 0);
298
}
299
300
// test exceptions
301
testOffsetByCodePoints(null, 0, 0, NullPointerException.class);
302
testOffsetByCodePoints(sb, -1, length, IndexOutOfBoundsException.class);
303
testOffsetByCodePoints(sb, 0, length+1, IndexOutOfBoundsException.class);
304
testOffsetByCodePoints(sb, 1, -2, IndexOutOfBoundsException.class);
305
testOffsetByCodePoints(sb, length, length-1, IndexOutOfBoundsException.class);
306
testOffsetByCodePoints(sb, length, -(length+1), IndexOutOfBoundsException.class);
307
}
308
}
309
310
311
static final boolean At = true, Before = false;
312
313
static void testCodePoint(boolean isAt, StringBuffer sb, int index, int expected) {
314
int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
315
316
check(c != expected,
317
"codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
318
+ sb + ">", c, expected);
319
}
320
321
static void testCodePoint(boolean isAt, StringBuffer sb, int index) {
322
boolean exceptionOccurred = false;
323
324
try {
325
int c = isAt ? sb.codePointAt(index) : sb.codePointBefore(index);
326
}
327
catch (StringIndexOutOfBoundsException e) {
328
exceptionOccurred = true;
329
}
330
check(!exceptionOccurred,
331
"codePoint" + (isAt ? "At" : "Before") + "(" + index + ") for <"
332
+ sb + "> should throw StringIndexOutOfBoundsPointerException.");
333
}
334
335
static void testAppendCodePoint(int codePoint, Class expectedException) {
336
try {
337
new StringBuffer().appendCodePoint(codePoint);
338
} catch (Exception e) {
339
if (expectedException.isInstance(e)) {
340
return;
341
}
342
throw new RuntimeException("Error: Unexpected exception", e);
343
}
344
check(true, "appendCodePoint(" + toHexString(codePoint) + ") didn't throw "
345
+ expectedException.getName());
346
}
347
348
static void testCodePointCount(StringBuffer sb, int beginIndex, int endIndex,
349
Class expectedException) {
350
try {
351
int n = sb.codePointCount(beginIndex, endIndex);
352
} catch (Exception e) {
353
if (expectedException.isInstance(e)) {
354
return;
355
}
356
throw new RuntimeException("Error: Unexpected exception", e);
357
}
358
check(true, "codePointCount() didn't throw " + expectedException.getName());
359
}
360
361
static void testOffsetByCodePoints(StringBuffer sb, int index, int offset,
362
Class expectedException) {
363
try {
364
int n = sb.offsetByCodePoints(index, offset);
365
} catch (Exception e) {
366
if (expectedException.isInstance(e)) {
367
return;
368
}
369
throw new RuntimeException("Error: Unexpected exception", e);
370
}
371
check(true, "offsetByCodePoints() didn't throw " + expectedException.getName());
372
}
373
374
static void check(boolean err, String msg) {
375
if (err) {
376
throw new RuntimeException("Error: " + msg);
377
}
378
}
379
380
static void check(boolean err, String s, int got, int expected) {
381
if (err) {
382
throw new RuntimeException("Error: " + s
383
+ " returned an unexpected value. got "
384
+ toHexString(got)
385
+ ", expected "
386
+ toHexString(expected));
387
}
388
}
389
390
static void check(boolean err, String s, StringBuffer got, String expected) {
391
if (err) {
392
throw new RuntimeException("Error: " + s
393
+ " returned an unexpected value. got <"
394
+ toHexString(new String(got))
395
+ ">, expected <"
396
+ toHexString(expected)
397
+ ">");
398
}
399
}
400
401
private static String toHexString(int c) {
402
return "0x" + Integer.toHexString(c);
403
}
404
405
private static String toHexString(String s) {
406
HexFormat format = HexFormat.of();
407
StringBuilder sb = new StringBuilder();
408
for (int i = 0; i < s.length(); i++) {
409
char c = s.charAt(i);
410
411
sb.append(" 0x");
412
sb.append(format.toHexDigits(c));
413
}
414
sb.append(' ');
415
return sb.toString();
416
}
417
}
418
419