33663
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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 8054307
|
|
27 |
@summary test chars() and codePoints()
|
|
28 |
*/
|
|
29 |
|
|
30 |
import java.util.Arrays;
|
|
31 |
import java.util.Random;
|
|
32 |
|
|
33 |
public class Chars {
|
|
34 |
|
|
35 |
public static void main(String[] args) {
|
|
36 |
Random r = new Random();
|
|
37 |
for (int i = 0; i < 10; i++) {
|
|
38 |
int n = 100 + r.nextInt(100);
|
|
39 |
char[] cc = new char[n];
|
|
40 |
int[] ccExp = new int[n];
|
|
41 |
int[] cpExp = new int[n];
|
|
42 |
// latin1
|
|
43 |
for (int j = 0; j < n; j++) {
|
|
44 |
cc[j] = (char)(ccExp[j] = cpExp[j] = r.nextInt(0x80));
|
|
45 |
}
|
|
46 |
testChars(cc, ccExp);
|
|
47 |
testCPs(cc, cpExp);
|
|
48 |
|
|
49 |
// bmp without surrogates
|
|
50 |
for (int j = 0; j < n; j++) {
|
|
51 |
cc[j] = (char)(ccExp[j] = cpExp[j] = r.nextInt(0x8000));
|
|
52 |
}
|
|
53 |
testChars(cc, ccExp);
|
|
54 |
testCPs(cc, cpExp);
|
|
55 |
|
|
56 |
// bmp with surrogates
|
|
57 |
int k = 0;
|
|
58 |
for (int j = 0; j < n; j++) {
|
|
59 |
if (j % 9 == 5 && j + 1 < n) {
|
|
60 |
int cp = 0x10000 + r.nextInt(2000);
|
|
61 |
cpExp[k++] = cp;
|
|
62 |
Character.toChars(cp, cc, j);
|
|
63 |
ccExp[j] = cc[j];
|
|
64 |
ccExp[j + 1] = cc[j + 1];
|
|
65 |
j++;
|
|
66 |
} else {
|
|
67 |
cc[j] = (char)(ccExp[j] = cpExp[k++] = r.nextInt(0x8000));
|
|
68 |
}
|
|
69 |
}
|
|
70 |
cpExp = Arrays.copyOf(cpExp, k);
|
|
71 |
testChars(cc, ccExp);
|
|
72 |
testCPs(cc, cpExp);
|
|
73 |
}
|
|
74 |
}
|
|
75 |
|
|
76 |
static void testChars(char[] cc, int[] expected) {
|
|
77 |
String str = new String(cc);
|
|
78 |
if (!Arrays.equals(expected, str.chars().toArray())) {
|
|
79 |
throw new RuntimeException("chars/codePoints() failed!");
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
|
83 |
static void testCPs(char[] cc, int[] expected) {
|
|
84 |
String str = new String(cc);
|
|
85 |
if (!Arrays.equals(expected, str.codePoints().toArray())) {
|
|
86 |
throw new RuntimeException("chars/codePoints() failed!");
|
|
87 |
}
|
|
88 |
}
|
|
89 |
}
|