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 org.testng.annotations.DataProvider;
25 import org.testng.annotations.Test;
26 
27 import static org.testng.Assert.assertEquals;
28 
29 /*
30  * @test
31  * @bug 8077559
32  * @summary Tests Compact String. This one is for String.indexOf.
33  * @run testng/othervm -XX:+CompactStrings IndexOf
34  * @run testng/othervm -XX:-CompactStrings IndexOf
35  */
36 
37 public class IndexOf extends CompactString {
38 
39     @DataProvider
provider()40     public Object[][] provider() {
41         return new Object[][] {
42 
43                 new Object[] { STRING_EMPTY, (int) 'A', -1 },
44                 new Object[] { STRING_L1, (int) 'A', 0 },
45                 new Object[] { STRING_L2, (int) 'A', 0 },
46                 new Object[] { STRING_L2, (int) 'B', 1 },
47                 new Object[] { STRING_L4, (int) 'A', 0 },
48                 new Object[] { STRING_L4, (int) 'D', 3 },
49                 new Object[] { STRING_L4, (int) 'E', -1 },
50                 new Object[] { STRING_LLONG, (int) 'A', 0 },
51                 new Object[] { STRING_LLONG, (int) 'H', 7 },
52                 new Object[] { STRING_U1, (int) '\uFF21', 0 },
53                 new Object[] { STRING_U1, (int) 'A', -1 },
54                 new Object[] { STRING_U2, (int) '\uFF21', 0 },
55                 new Object[] { STRING_U2, (int) '\uFF22', 1 },
56                 new Object[] { STRING_M12, (int) '\uFF21', 0 },
57                 new Object[] { STRING_M12, (int) 'A', 1 },
58                 new Object[] { STRING_M11, (int) 'A', 0 },
59                 new Object[] { STRING_M11, (int) '\uFF21', 1 },
60                 new Object[] { STRING_UDUPLICATE, (int) '\uFF21', 0 },
61                 new Object[] { STRING_UDUPLICATE, (int) '\uFF22', 1 },
62                 new Object[] { STRING_SUPPLEMENTARY, 'A', 5 },
63                 new Object[] { STRING_SUPPLEMENTARY, '\uFF21', 4 },
64                 new Object[] { STRING_SUPPLEMENTARY,
65                         Character.toCodePoint('\uD801', '\uDC00'), 0 },
66                 new Object[] { STRING_SUPPLEMENTARY,
67                         Character.toCodePoint('\uD801', '\uDC01'), 2 }, };
68     }
69 
70     @Test(dataProvider = "provider")
testIndexOf(String str, int ch, int expected)71     public void testIndexOf(String str, int ch, int expected) {
72         map.get(str).forEach(
73                 (source, data) -> {
74                     assertEquals(data.indexOf(ch), expected, String.format(
75                             "testing String(%s).indexOf(%d), source : %s, ",
76                             escapeNonASCIIs(data), ch, source));
77                 });
78     }
79 
80     @DataProvider
provider2()81     public Object[][] provider2() {
82         return new Object[][] {
83 
84         new Object[] { STRING_EMPTY, (int) 'A', 0, -1 },
85                 new Object[] { STRING_L1, (int) 'A', 0, 0 },
86                 new Object[] { STRING_L1, (int) 'A', 1, -1 },
87                 new Object[] { STRING_L1, (int) 'B', 0, -1 },
88                 new Object[] { STRING_L2, (int) 'A', 0, 0 },
89                 new Object[] { STRING_L2, (int) 'A', 1, -1 },
90                 new Object[] { STRING_L2, (int) 'B', 0, 1 },
91                 new Object[] { STRING_L2, (int) 'B', 1, 1 },
92                 new Object[] { STRING_L4, (int) 'A', 0, 0 },
93                 new Object[] { STRING_L4, (int) 'D', 2, 3 },
94                 new Object[] { STRING_L4, (int) 'B', 2, -1 },
95                 new Object[] { STRING_LLONG, (int) 'A', 0, 0 },
96                 new Object[] { STRING_LLONG, (int) 'H', 5, 7 },
97                 new Object[] { STRING_U1, (int) '\uFF21', 0, 0 },
98                 new Object[] { STRING_U1, (int) 'A', 0, -1 },
99                 new Object[] { STRING_U2, (int) '\uFF21', 0, 0 },
100                 new Object[] { STRING_U2, (int) '\uFF22', 0, 1 },
101                 new Object[] { STRING_M12, (int) '\uFF21', 0, 0 },
102                 new Object[] { STRING_M12, (int) 'A', 1, 1 },
103                 new Object[] { STRING_M11, (int) 'A', 0, 0 },
104                 new Object[] { STRING_M11, (int) '\uFF21', 1, 1 },
105                 new Object[] { STRING_UDUPLICATE, (int) '\uFF21', 1, 2 },
106                 new Object[] { STRING_UDUPLICATE, (int) '\uFF22', 1, 1 }, };
107     }
108 
109     @Test(dataProvider = "provider2")
testIndexOf(String str, int ch, int fromIndex, int expected)110     public void testIndexOf(String str, int ch, int fromIndex, int expected) {
111         map.get(str)
112                 .forEach(
113                         (source, data) -> {
114                             assertEquals(
115                                     data.indexOf(ch, fromIndex),
116                                     expected,
117                                     String.format(
118                                             "testing String(%s).indexOf(%d, %d), source : %s, ",
119                                             escapeNonASCIIs(data), ch,
120                                             fromIndex, source));
121                         });
122     }
123 
124     @DataProvider
provider3()125     public Object[][] provider3() {
126         return new Object[][] {
127 
128                 new Object[] { STRING_EMPTY, "A", -1 },
129                 new Object[] { STRING_L1, "A", 0 },
130                 new Object[] { STRING_L1, "AB", -1 },
131                 new Object[] { STRING_L2, "A", 0 },
132                 new Object[] { STRING_L2, "B", 1 },
133                 new Object[] { STRING_L2, "AB", 0 },
134                 new Object[] { STRING_L2, "AC", -1 },
135                 new Object[] { STRING_L2, "ABC", -1 },
136                 new Object[] { STRING_L4, "ABCD", 0 },
137                 new Object[] { STRING_L4, "D", 3 },
138                 new Object[] { STRING_LLONG, "ABCDEFGH", 0 },
139                 new Object[] { STRING_LLONG, "EFGH", 4 },
140                 new Object[] { STRING_LLONG, "EFGHI", -1 },
141                 new Object[] { STRING_U1, "\uFF21", 0 },
142                 new Object[] { STRING_U1, "\uFF21A", -1 },
143                 new Object[] { STRING_U2, "\uFF21\uFF22", 0 },
144                 new Object[] { STRING_U2, "\uFF22", 1 },
145                 new Object[] { STRING_U2, "A\uFF22", -1 },
146                 new Object[] { STRING_M12, "\uFF21A", 0 },
147                 new Object[] { STRING_M12, "A", 1 },
148                 new Object[] { STRING_M12, "\uFF21\uFF21", -1 },
149                 new Object[] { STRING_M11, "A\uFF21", 0 },
150                 new Object[] { STRING_M11, "\uFF21", 1 },
151                 new Object[] { STRING_M11, "A", 0 },
152                 new Object[] {
153                         STRING_UDUPLICATE,
154                         "\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22",
155                         0 },
156                 new Object[] { STRING_UDUPLICATE,
157                         "\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21", 1 },
158                 new Object[] {
159                         STRING_UDUPLICATE,
160                         "\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21",
161                         -1 }, };
162     }
163 
164     @Test(dataProvider = "provider3")
testIndexOf(String str, String anotherString, int expected)165     public void testIndexOf(String str, String anotherString, int expected) {
166         map.get(str)
167                 .forEach(
168                         (source, data) -> {
169                             assertEquals(
170                                     data.indexOf(anotherString),
171                                     expected,
172                                     String.format(
173                                             "testing String(%s).indexOf(%s), source : %s, ",
174                                             escapeNonASCIIs(data),
175                                             escapeNonASCIIs(anotherString),
176                                             source));
177                         });
178     }
179 
180     @DataProvider
provider4()181     public Object[][] provider4() {
182         return new Object[][] {
183 
184                 new Object[] { STRING_EMPTY, "A", 0, -1 },
185                 new Object[] { STRING_L1, "A", 0, 0 },
186                 new Object[] { STRING_L1, "A", 1, -1 },
187                 new Object[] { STRING_L1, "AB", 0, -1 },
188                 new Object[] { STRING_L2, "A", 0, 0 },
189                 new Object[] { STRING_L2, "B", 0, 1 },
190                 new Object[] { STRING_L2, "AB", 0, 0 },
191                 new Object[] { STRING_L2, "AB", 1, -1 },
192                 new Object[] { STRING_L4, "ABCD", 0, 0 },
193                 new Object[] { STRING_L4, "BC", 0, 1 },
194                 new Object[] { STRING_L4, "A", 0, 0 },
195                 new Object[] { STRING_L4, "CD", 0, 2 },
196                 new Object[] { STRING_L4, "A", 2, -1 },
197                 new Object[] { STRING_L4, "ABCDE", 0, -1 },
198                 new Object[] { STRING_LLONG, "ABCDEFGH", 0, 0 },
199                 new Object[] { STRING_LLONG, "DEFGH", 0, 3 },
200                 new Object[] { STRING_LLONG, "A", 0, 0 },
201                 new Object[] { STRING_LLONG, "GHI", 0, -1 },
202                 new Object[] { STRING_U1, "\uFF21", 0, 0 },
203                 new Object[] { STRING_U1, "\uFF21A", 0, -1 },
204                 new Object[] { STRING_U2, "\uFF21\uFF22", 0, 0 },
205                 new Object[] { STRING_U2, "\uFF22", 0, 1 },
206                 new Object[] { STRING_U2, "\uFF21", 1, -1 },
207                 new Object[] { STRING_M12, "\uFF21A", 0, 0 },
208                 new Object[] { STRING_M12, "A", 1, 1 },
209                 new Object[] { STRING_M12, "\uFF21A", 1, -1 },
210                 new Object[] { STRING_M12, "\uFF21", 0, 0 },
211                 new Object[] { STRING_M11, "A\uFF21", 0, 0 },
212                 new Object[] { STRING_M11, "\uFF21", 1, 1 },
213                 new Object[] { STRING_M11, "A\uFF21", 1, -1 },
214                 new Object[] { STRING_M11, "A\uFF21A", 0, -1 },
215                 new Object[] {
216                         STRING_UDUPLICATE,
217                         "\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22",
218                         0, 0 },
219                 new Object[] {
220                         STRING_UDUPLICATE,
221                         "\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22",
222                         1, -1 },
223                 new Object[] {
224                         STRING_UDUPLICATE,
225                         "\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22",
226                         1, 1 },
227                 new Object[] { STRING_UDUPLICATE, "\uFF21\uFF22\uFF21\uFF22",
228                         4, 4 },
229                 new Object[] { STRING_UDUPLICATE, "\uFF21\uFF22\uFF21\uFF22",
230                         7, -1 }, };
231     }
232 
233     @Test(dataProvider = "provider4")
testIndexOf(String str, String anotherString, int fromIndex, int expected)234     public void testIndexOf(String str, String anotherString, int fromIndex,
235             int expected) {
236         map.get(str)
237                 .forEach(
238                         (source, data) -> {
239                             assertEquals(
240                                     data.indexOf(anotherString, fromIndex),
241                                     expected,
242                                     String.format(
243                                             "testing String(%s).indexOf(%s), source : %s, ",
244                                             escapeNonASCIIs(data),
245                                             escapeNonASCIIs(anotherString),
246                                             fromIndex, source));
247                         });
248     }
249 }
250