Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/String/LiteralReplace.java
41152 views
1
/*
2
* Copyright (c) 2015, 2019, 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
/* @test
25
* @bug 8058779 8054307 8222955
26
* @library /test/lib
27
* @build jdk.test.lib.RandomFactory
28
* @run testng LiteralReplace
29
* @summary Basic tests of String.replace(CharSequence, CharSequence)
30
* @key randomness
31
*/
32
33
import java.util.ArrayList;
34
import java.util.Arrays;
35
import java.util.Iterator;
36
import java.util.regex.Matcher;
37
import java.util.regex.Pattern;
38
import java.util.Random;
39
40
import jdk.test.lib.RandomFactory;
41
42
import org.testng.annotations.Test;
43
import org.testng.annotations.DataProvider;
44
import static org.testng.Assert.fail;
45
46
public class LiteralReplace {
47
48
@Test(dataProvider="sourceTargetReplacementExpected")
49
public void testExpected(String source, String target,
50
String replacement, String expected)
51
{
52
String canonical = canonicalReplace(source, target, replacement);
53
if (!canonical.equals(expected)) {
54
fail("Canonical: " + canonical + " != " + expected);
55
}
56
test0(source, target, replacement, expected);
57
}
58
59
@Test(dataProvider="sourceTargetReplacement")
60
public void testCanonical(String source, String target,
61
String replacement)
62
{
63
String canonical = canonicalReplace(source, target, replacement);
64
test0(source, target, replacement, canonical);
65
}
66
67
private void test0(String source, String target, String replacement,
68
String expected)
69
{
70
String result = source.replace(target, replacement);
71
if (!result.equals(expected)) {
72
fail(result + " != " + expected);
73
}
74
}
75
76
@Test(dataProvider="sourceTargetReplacementWithNull",
77
expectedExceptions = {NullPointerException.class})
78
public void testNPE(String source, String target, String replacement) {
79
source.replace(target, replacement);
80
}
81
82
@Test(expectedExceptions = {OutOfMemoryError.class})
83
public void testOOM() {
84
"1".repeat(65537).replace("1", "2".repeat(65537));
85
}
86
87
@DataProvider
88
public static Object[][] sourceTargetReplacementExpected() {
89
return new Object[][] {
90
{"aaa", "aa", "b", "ba"},
91
{"abcdefgh", "def", "DEF", "abcDEFgh"},
92
{"abcdefgh", "123", "DEF", "abcdefgh"},
93
{"abcdefgh", "abcdefghi", "DEF", "abcdefgh"},
94
{"abcdefghabc", "abc", "DEF", "DEFdefghDEF"},
95
{"abcdefghdef", "def", "", "abcgh"},
96
{"abcdefgh", "", "_", "_a_b_c_d_e_f_g_h_"},
97
{"", "", "", ""},
98
{"", "a", "b", ""},
99
{"", "", "abc", "abc"},
100
{"abcdefgh", "abcdefgh", "abcdefgh", "abcdefgh"},
101
{"abcdefgh", "abcdefgh", "abcdefghi", "abcdefghi"},
102
{"abcdefgh", "abcdefgh", "", ""},
103
{"abcdabcd", "abcd", "", ""},
104
{"aaaaaaaaa", "aa", "_X_", "_X__X__X__X_a"},
105
{"aaaaaaaaa", "aa", "aaa", "aaaaaaaaaaaaa"},
106
{"aaaaaaaaa", "aa", "aa", "aaaaaaaaa"},
107
{"a.c.e.g.", ".", "-", "a-c-e-g-"},
108
{"abcdefgh", "[a-h]", "X", "abcdefgh"},
109
{"aa+", "a+", "", "a"},
110
{"^abc$", "abc", "x", "^x$"},
111
{"abc", "b", "_", "a_c"},
112
{"abc", "bc", "_", "a_"},
113
{"abc".repeat(65537) + "end", "b", "_XYZ_", "a_XYZ_c".repeat(65537) + "end"},
114
{"abc".repeat(65537) + "end", "a", "_", "_bc".repeat(65537) + "end"},
115
{"a".repeat(65537), "a", "", ""},
116
{"ab".repeat(65537), "a", "", "b".repeat(65537)},
117
{"ab".repeat(65537), "ab", "", ""},
118
{"b" + "ab".repeat(65537), "ab", "", "b"},
119
120
// more with non-latin1 characters
121
{"\u4e00\u4e00\u4e00",
122
"\u4e00\u4e00",
123
"\u4e01",
124
"\u4e01\u4e00"},
125
126
{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",
127
"\u4e03\u4e04\u4e05",
128
"\u4e10\u4e11\u4e12",
129
"\u4e00\u4e01\u4e02\u4e10\u4e11\u4e12\u4e06\u4e07\u4e08"},
130
131
{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",
132
"ABC",
133
"\u4e10\u4e11\u4e12",
134
"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08"},
135
136
{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08",
137
"\u4e02\u4e03",
138
"\u4e12\u4e13",
139
"\u4e00\u4e01\u4e12\u4e13\u4e04\u4e12\u4e13\u4e07\u4e08"},
140
141
{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08",
142
"\u4e02\u4e03",
143
"ab",
144
"\u4e00\u4e01ab\u4e04ab\u4e07\u4e08"},
145
146
{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07",
147
"",
148
"_",
149
"_\u4e00_\u4e01_\u4e02_\u4e03_\u4e04_\u4e05_\u4e06_\u4e07_"},
150
{"^\u4e00\u4e01\u4e02$",
151
"\u4e00\u4e01\u4e02",
152
"\u4e03",
153
"^\u4e03$"},
154
155
{"", "\u4e00", "\u4e01", ""},
156
{"", "", "\u4e00\u4e01\u4e02", "\u4e00\u4e01\u4e02"},
157
158
{"^\u4e00\u4e01\u4e02$",
159
"\u4e00\u4e01\u4e02",
160
"X",
161
"^X$"},
162
163
{"abcdefgh",
164
"def",
165
"\u4e01",
166
"abc\u4e01gh"},
167
168
{"abcdefgh",
169
"def",
170
"\u4e01\u4e02",
171
"abc\u4e01\u4e02gh"},
172
173
{"abcdefabcgh",
174
"abc",
175
"\u4e01\u4e02",
176
"\u4e01\u4e02def\u4e01\u4e02gh"},
177
178
{"abcdefabcghabc",
179
"abc",
180
"\u4e01\u4e02",
181
"\u4e01\u4e02def\u4e01\u4e02gh\u4e01\u4e02"},
182
183
{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
184
"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
185
"abcd",
186
"abcd"},
187
188
{"\u4e00\u4e01",
189
"\u4e00\u4e01",
190
"abcdefg",
191
"abcdefg"},
192
193
{"\u4e00\u4e01xyz",
194
"\u4e00\u4e01",
195
"abcdefg",
196
"abcdefgxyz"},
197
198
{"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",
199
"\u4e00\u4e00",
200
"\u4e00\u4e00\u4e00",
201
"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00"},
202
203
{"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",
204
"\u4e00\u4e00\u4e00",
205
"\u4e00\u4e00",
206
"\u4e00\u4e00\u4e00\u4e00"},
207
208
{"\u4e00.\u4e01.\u4e02.\u4e03.\u4e04.",
209
".",
210
"-",
211
"\u4e00-\u4e01-\u4e02-\u4e03-\u4e04-"},
212
213
{"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",
214
"\u4e00",
215
"",
216
""},
217
218
{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
219
"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
220
"",
221
""},
222
223
{"\u4e01\u4e02\u4e03", "\u4e02", "\u4e02", "\u4e01\u4e02\u4e03"},
224
{"\u4e01\u4e02\u4e03", "\u4e02", "\u4e04", "\u4e01\u4e04\u4e03"},
225
{"\u4e01\u4e02\u4e03", "\u4e02", "_", "\u4e01_\u4e03"},
226
{"a\u4e02c", "\u4e02", "_", "a_c"},
227
{"\u4e01@\u4e03", "@", "_", "\u4e01_\u4e03"},
228
{"\u4e01@\u4e03", "@", "\u4e02", "\u4e01\u4e02\u4e03"},
229
{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e02\u4e03", "\u4e01\u4e02\u4e03"},
230
{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e04\u4e05", "\u4e01\u4e04\u4e05"},
231
{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e06", "\u4e01\u4e06"},
232
{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "<>", "\u4e01<>"},
233
{"@\u4e02\u4e03", "\u4e02\u4e03", "<>", "@<>"},
234
{"\u4e01@@", "\u4e01@", "", "@"},
235
{"\u4e01@@", "\u4e01@", "#", "#@"},
236
{"\u4e01@@", "\u4e01@", "\u4e09", "\u4e09@"},
237
{"\u4e01@@", "\u4e01@", "#\u4e09", "#\u4e09@"},
238
{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e02", "\u4e01\u4e02\u4e03".repeat(32771) + "end"},
239
{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e04", "\u4e01\u4e04\u4e03".repeat(32771) + "end"},
240
{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e04\u4e05", "\u4e01\u4e04\u4e05\u4e03".repeat(32771) + "end"},
241
{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "_", "\u4e01_\u4e03".repeat(32771) + "end"},
242
{"\u4e01_\u4e03".repeat(32771) + "end", "_", "_", "\u4e01_\u4e03".repeat(32771) + "end"},
243
{"\u4e01_\u4e03".repeat(32771) + "end", "_", "\u4e06", "\u4e01\u4e06\u4e03".repeat(32771) + "end"},
244
{"\u4e01_\u4e03".repeat(32771) + "end", "_", "\u4e06\u4e06", "\u4e01\u4e06\u4e06\u4e03".repeat(32771) + "end"},
245
{"X_Y".repeat(32771) + "end", "_", "\u4e07", "X\u4e07Y".repeat(32771) + "end"},
246
{"X_Y".repeat(32771) + "end", "_", "\u4e07\u4e08", "X\u4e07\u4e08Y".repeat(32771) + "end"},
247
{"X_Y".repeat(32771) + "end", "_", ".\u4e08.", "X.\u4e08.Y".repeat(32771) + "end"},
248
{"\u4e0a".repeat(32771), "\u4e0a", "", ""},
249
{"\u4e0a\u4e0b".repeat(32771), "\u4e0a", "", "\u4e0b".repeat(32771)},
250
{"\u4e0a\u4e0b".repeat(32771), "\u4e0b", "", "\u4e0a".repeat(32771)},
251
{"\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", ""},
252
{"\u4e0b" + "\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", "\u4e0b"},
253
{"\u4e0a\u4e0b".repeat(32771) + "\u4e0a", "\u4e0a\u4e0b", "", "\u4e0a"},
254
{"\u4e0b" + "\u4e0a\u4e0b".repeat(32771) + "\u4e0a", "\u4e0a\u4e0b", "", "\u4e0b\u4e0a"},
255
{"b" + "\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", "b"},
256
{"\u4e0a\u4e0b".repeat(32771) + "a", "\u4e0a\u4e0b", "", "a"},
257
{"b" + "\u4e0a\u4e0b".repeat(32771) + "a", "\u4e0a\u4e0b", "", "ba"},
258
};
259
}
260
261
@DataProvider
262
public static Iterator<Object[]> sourceTargetReplacement() {
263
ArrayList<Object[]> list = new ArrayList<>();
264
for (int maxSrcLen = 1; maxSrcLen <= (1 << 10); maxSrcLen <<= 1) {
265
for (int maxTrgLen = 1; maxTrgLen <= (1 << 10); maxTrgLen <<= 1) {
266
for (int maxPrlLen = 1; maxPrlLen <= (1 << 10); maxPrlLen <<= 1) {
267
list.add(makeArray(makeRandomString(maxSrcLen),
268
makeRandomString(maxTrgLen),
269
makeRandomString(maxPrlLen)));
270
271
String source = makeRandomString(maxSrcLen);
272
list.add(makeArray(source,
273
mekeRandomSubstring(source, maxTrgLen),
274
makeRandomString(maxPrlLen)));
275
}
276
}
277
}
278
return list.iterator();
279
}
280
281
@DataProvider
282
public static Iterator<Object[]> sourceTargetReplacementWithNull() {
283
ArrayList<Object[]> list = new ArrayList<>();
284
Object[] arr = {null, "", "a", "b", "string", "str", "ababstrstr"};
285
for (int i = 0; i < arr.length; ++i) {
286
for (int j = 0; j < arr.length; ++j) {
287
for (int k = 0; k < arr.length; ++k) {
288
if (arr[i] != null && (arr[j] == null || arr[k] == null)) {
289
list.add(makeArray(arr[i], arr[j], arr[k]));
290
}
291
}
292
}
293
}
294
return list.iterator();
295
}
296
297
// utilities
298
299
/**
300
* How the String.replace(CharSequence, CharSequence) used to be implemented
301
*/
302
private static String canonicalReplace(String source, String target, String replacement) {
303
return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(
304
source).replaceAll(Matcher.quoteReplacement(replacement.toString()));
305
}
306
307
private static final Random random = RandomFactory.getRandom();
308
309
private static final char[] CHARS = ("qwertyuiop[]12345678" +
310
"90-=\\`asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{" +
311
"}ASDFGHJKL:\"ZXCVBNM<>?\n\r\t\u0444\u044B\u0432\u0430").toCharArray();
312
313
private static String makeRandomString(int maxLen) {
314
int len = random.nextInt(maxLen);
315
char[] buf = new char[len];
316
for (int i = 0; i < len; ++i) {
317
buf[i] = CHARS[random.nextInt(CHARS.length)];
318
}
319
return new String(buf);
320
}
321
322
private static String mekeRandomSubstring(String source, int maxLen) {
323
if (source.isEmpty()) {
324
return source;
325
}
326
int pos = random.nextInt(source.length());
327
int len = Integer.min(source.length() - pos,
328
random.nextInt(maxLen));
329
return source.substring(pos, pos + len);
330
}
331
332
private static Object[] makeArray(Object... array) {
333
return array;
334
}
335
}
336
337