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.lastIndexOf.
33  * @run testng/othervm -XX:+CompactStrings LastIndexOf
34  * @run testng/othervm -XX:-CompactStrings LastIndexOf
35  */
36 
37 public class LastIndexOf 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_LLONG, (int) 'A', 0 },
50                 new Object[] { STRING_LLONG, (int) 'H', 7 },
51                 new Object[] { STRING_U1, (int) '\uFF21', 0 },
52                 new Object[] { STRING_U1, (int) 'B', -1 },
53                 new Object[] { STRING_U2, (int) '\uFF21', 0 },
54                 new Object[] { STRING_U2, (int) '\uFF22', 1 },
55                 new Object[] { STRING_M12, (int) '\uFF21', 0 },
56                 new Object[] { STRING_M12, (int) 'A', 1 },
57                 new Object[] { STRING_M11, (int) 'A', 0 },
58                 new Object[] { STRING_M11, (int) '\uFF21', 1 },
59                 new Object[] { STRING_UDUPLICATE, (int) '\uFF22', 9 },
60                 new Object[] { STRING_UDUPLICATE, (int) '\uFF21', 8 },
61                 new Object[] { STRING_SUPPLEMENTARY,
62                         Character.toCodePoint('\uD801', '\uDC01'), 2 }, };
63     }
64 
65     @Test(dataProvider = "provider")
testLastIndexOf(String str, int ch, int expected)66     public void testLastIndexOf(String str, int ch, int expected) {
67         map.get(str)
68                 .forEach(
69                         (source, data) -> {
70                             assertEquals(
71                                     data.lastIndexOf(ch),
72                                     expected,
73                                     String.format(
74                                             "testing String(%s).lastIndexOf(%d), source : %s, ",
75                                             escapeNonASCIIs(data), ch, source));
76                         });
77     }
78 
79     @DataProvider
provider2()80     public Object[][] provider2() {
81         return new Object[][] {
82 
83         new Object[] { STRING_EMPTY, (int) 'A', 0, -1 },
84                 new Object[] { STRING_L1, (int) 'A', 0, 0 },
85                 new Object[] { STRING_L1, (int) 'A', 1, 0 },
86                 new Object[] { STRING_L2, (int) 'A', 0, 0 },
87                 new Object[] { STRING_L2, (int) 'B', 1, 1 },
88                 new Object[] { STRING_L2, (int) 'B', 2, 1 },
89                 new Object[] { STRING_L4, (int) 'A', 0, 0 },
90                 new Object[] { STRING_L4, (int) 'C', 2, 2 },
91                 new Object[] { STRING_L4, (int) 'C', 1, -1 },
92                 new Object[] { STRING_LLONG, (int) 'A', 0, 0 },
93                 new Object[] { STRING_LLONG, (int) 'H', 7, 7 },
94                 new Object[] { STRING_LLONG, (int) 'H', 6, -1 },
95                 new Object[] { STRING_U1, (int) '\uFF21', 0, 0 },
96                 new Object[] { STRING_U1, (int) '\uFF21', 7, 0 },
97                 new Object[] { STRING_U2, (int) '\uFF21', 0, 0 },
98                 new Object[] { STRING_U2, (int) '\uFF22', 0, -1 },
99                 new Object[] { STRING_M12, (int) '\uFF21', 0, 0 },
100                 new Object[] { STRING_M12, (int) 'A', 1, 1 },
101                 new Object[] { STRING_M12, (int) 'A', 0, -1 },
102                 new Object[] { STRING_M11, (int) 'A', 0, 0 },
103                 new Object[] { STRING_M11, (int) '\uFF21', 1, 1 },
104                 new Object[] { STRING_M11, (int) '\uFF21', 0, -1 },
105                 new Object[] { STRING_UDUPLICATE, (int) '\uFF21', 5, 4 },
106                 new Object[] { STRING_UDUPLICATE, (int) '\uFF21', 6, 6 },
107                 new Object[] { STRING_UDUPLICATE, (int) '\uFF22', 5, 5 },
108                 new Object[] { STRING_UDUPLICATE, (int) '\uFF22', 6, 5 }, };
109     }
110 
111     @Test(dataProvider = "provider2")
testLastIndexOf(String str, int ch, int fromIndex, int expected)112     public void testLastIndexOf(String str, int ch, int fromIndex, int expected) {
113         map.get(str)
114                 .forEach(
115                         (source, data) -> {
116                             assertEquals(
117                                     data.lastIndexOf(ch, fromIndex),
118                                     expected,
119                                     String.format(
120                                             "testing String(%s).lastIndexOf(%d, %d), source : %s, ",
121                                             escapeNonASCIIs(data), ch,
122                                             fromIndex, source));
123                         });
124     }
125 
126     @DataProvider
provider3()127     public Object[][] provider3() {
128         return new Object[][] {
129 
130                 new Object[] { STRING_EMPTY, "A", -1 },
131                 new Object[] { STRING_L1, "A", 0 },
132                 new Object[] { STRING_L1, "AB", -1 },
133 
134                 new Object[] { STRING_L2, "AB", 0 },
135                 new Object[] { STRING_L2, "B", 1 },
136                 new Object[] { STRING_L4, "ABCD", 0 },
137                 new Object[] { STRING_L4, "B", 1 },
138                 new Object[] { STRING_LLONG, "ABCD", 0 },
139                 new Object[] { STRING_LLONG, "GH", 6 },
140                 new Object[] { STRING_U1, "\uFF21", 0 },
141                 new Object[] { STRING_U1, "\uFF22", -1 },
142                 new Object[] { STRING_U2, "\uFF21\uFF22", 0 },
143                 new Object[] { STRING_U2, "\uFF22", 1 },
144                 new Object[] { STRING_M12, "\uFF21A", 0 },
145                 new Object[] { STRING_M12, "A", 1 },
146                 new Object[] { STRING_M11, "A\uFF21", 0 },
147                 new Object[] { STRING_M11, "\uFF21", 1 },
148                 new Object[] { STRING_UDUPLICATE, "\uFF21\uFF22\uFF21\uFF22", 6 },
149                 new Object[] { STRING_UDUPLICATE, "\uFF21\uFF22", 8 }, };
150     }
151 
152     @Test(dataProvider = "provider3")
testLastIndexOf(String str, String anotherString, int expected)153     public void testLastIndexOf(String str, String anotherString, int expected) {
154         map.get(str)
155                 .forEach(
156                         (source, data) -> {
157                             assertEquals(
158                                     data.lastIndexOf(anotherString),
159                                     expected,
160                                     String.format(
161                                             "testing String(%s).lastIndexOf(%s), source : %s, ",
162                                             escapeNonASCIIs(data),
163                                             escapeNonASCIIs(anotherString),
164                                             source));
165                         });
166     }
167 
168     @DataProvider
provider4()169     public Object[][] provider4() {
170         return new Object[][] {
171 
172                 new Object[] { STRING_EMPTY, "A", 0, -1 },
173                 new Object[] { STRING_L2, "AB", 0, 0 },
174 
175                 new Object[] { STRING_L1, "AB", -1, -1 },
176 
177                 new Object[] { STRING_L2, "B", 1, 1 },
178                 new Object[] { STRING_L2, "B", 0, -1 },
179                 new Object[] { STRING_L4, "ABC", 3, 0 },
180                 new Object[] { STRING_L4, "ABC", 0, 0 },
181                 new Object[] { STRING_L4, "ABC", 1, 0 },
182                 new Object[] { STRING_L4, "BC", 1, 1 },
183                 new Object[] { STRING_L4, "BC", 0, -1 },
184                 new Object[] { STRING_LLONG, "ABCDEFGH", 0, 0 },
185                 new Object[] { STRING_LLONG, "EFGH", 7, 4 },
186                 new Object[] { STRING_LLONG, "EFGH", 3, -1 },
187                 new Object[] { STRING_U1, "\uFF21", 0, 0 },
188                 new Object[] { STRING_U1, "\uFF21", 7, 0 },
189                 new Object[] { STRING_U2, "\uFF21\uFF22", 0, 0 },
190                 new Object[] { STRING_U2, "\uFF21\uFF22", 1, 0 },
191                 new Object[] { STRING_M12, "\uFF21A", 0, 0 },
192                 new Object[] { STRING_M12, "A", 1, 1 },
193                 new Object[] { STRING_M12, "A", 0, -1 },
194                 new Object[] { STRING_M11, "A\uFF21", 0, 0 },
195                 new Object[] { STRING_M11, "A\uFF21", 1, 0 },
196                 new Object[] { STRING_M11, "\uFF21", 0, -1 },
197                 new Object[] {
198                         STRING_UDUPLICATE,
199                         "\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22",
200                         9, 0 },
201                 new Object[] {
202                         STRING_UDUPLICATE,
203                         "\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22\uFF21\uFF22",
204                         0, 0 },
205                 new Object[] { STRING_UDUPLICATE, "\uFF21\uFF22", 6, 6 },
206                 new Object[] { STRING_UDUPLICATE, "\uFF21\uFF22\uFF21", 6, 6 }, };
207     }
208 
209     @Test(dataProvider = "provider4")
testLastIndexOf(String str, String anotherString, int fromIndex, int expected)210     public void testLastIndexOf(String str, String anotherString,
211             int fromIndex, int expected) {
212         map.get(str)
213                 .forEach(
214                         (source, data) -> {
215                             assertEquals(
216                                     data.lastIndexOf(anotherString, fromIndex),
217                                     expected,
218                                     String.format(
219                                             "testing String(%s).lastIndexOf(%s, %d), source : %s, ",
220                                             escapeNonASCIIs(data),
221                                             escapeNonASCIIs(anotherString),
222                                             fromIndex, source));
223                         });
224     }
225 }
226