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 import java.util.Arrays;
25 
26 import org.testng.annotations.DataProvider;
27 import org.testng.annotations.Test;
28 
29 import static org.testng.Assert.assertTrue;
30 
31 /*
32  * @test
33  * @bug 8077559
34  * @summary Tests Compact String. This one is for String.getChars.
35  * @run testng/othervm -XX:+CompactStrings GetChars
36  * @run testng/othervm -XX:-CompactStrings GetChars
37  */
38 
39 public class GetChars extends CompactString {
40 
41     @DataProvider
provider()42     public Object[][] provider() {
43         return new Object[][] {
44 
45                 new Object[] { STRING_EMPTY, 0, STRING_EMPTY.length(),
46                         new char[STRING_EMPTY.length()], 0, CHAR_ARRAY_EMPTY },
47                 new Object[] { STRING_L1, 0, STRING_L1.length(),
48                         new char[STRING_L1.length()], 0, CHAR_ARRAY_L1 },
49                 new Object[] { STRING_L2, 0, STRING_L2.length(),
50                         new char[STRING_L2.length()], 0, CHAR_ARRAY_L2 },
51                 new Object[] { STRING_L4, 0, STRING_L4.length(),
52                         new char[STRING_L4.length()], 0, CHAR_ARRAY_L4 },
53                 new Object[] { STRING_LLONG, 0, STRING_LLONG.length(),
54                         new char[STRING_LLONG.length()], 0, CHAR_ARRAY_LLONG },
55                 new Object[] { STRING_U1, 0, STRING_U1.length(),
56                         new char[STRING_U1.length()], 0, CHAR_ARRAY_U1 },
57                 new Object[] { STRING_U2, 0, STRING_U2.length(),
58                         new char[STRING_U2.length()], 0, CHAR_ARRAY_U2 },
59                 new Object[] { STRING_M12, 0, STRING_M12.length(),
60                         new char[STRING_M12.length()], 0, CHAR_ARRAY_M12 },
61                 new Object[] { STRING_M11, 0, STRING_M11.length(),
62                         new char[STRING_M11.length()], 0, CHAR_ARRAY_M11 },
63                 new Object[] { STRING_UDUPLICATE, 0,
64                         STRING_UDUPLICATE.length(),
65                         new char[STRING_UDUPLICATE.length()], 0,
66                         CHAR_ARRAY_UDUPLICATE },
67                 new Object[] { STRING_MDUPLICATE1, 0,
68                         STRING_MDUPLICATE1.length(),
69                         new char[STRING_MDUPLICATE1.length()], 0,
70                         CHAR_ARRAY_MDUPLICATE1 }, };
71     }
72 
73     @Test(dataProvider = "provider")
testGetChars(String str, int srcBegin, int srcEnd, char[] dst, int dstBegin, char[] expected)74     public void testGetChars(String str, int srcBegin, int srcEnd, char[] dst,
75             int dstBegin, char[] expected) {
76         map.get(str)
77                 .forEach(
78                         (source, data) -> {
79                             data.getChars(srcBegin, srcEnd, dst, dstBegin);
80                             assertTrue(
81                                     Arrays.equals(dst, expected),
82                                     String.format(
83                                             "testing String(%s).getChars(%d, %d, %s, %d), source : %s, ",
84                                             escapeNonASCIIs(data), srcBegin,
85                                             srcEnd, escapeNonASCIIs(Arrays
86                                                     .toString(dst)), dstBegin,
87                                             source));
88                         });
89     }
90 
91 }
92