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