Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/String/StringRepeat.java
41152 views
1
/*
2
* Copyright (c) 2018, 2021, 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
* @summary This exercises String#repeat patterns and limits.
27
* @run main/othervm StringRepeat
28
*/
29
30
/*
31
* @test
32
* @summary This exercises String#repeat patterns with 16 * 1024 * 1024 repeats.
33
* @requires os.maxMemory >= 2G
34
* @run main/othervm -Xmx2g StringRepeat 16777216
35
*/
36
37
import java.nio.CharBuffer;
38
39
public class StringRepeat {
40
public static void main(String... args) {
41
if (args.length > 0) {
42
REPEATS = new int[args.length];
43
for (int i = 0; i < args.length; ++i) {
44
REPEATS[i] = Integer.parseInt(args[i]);
45
}
46
}
47
test1();
48
test2();
49
}
50
51
/*
52
* Default varitions of repeat count.
53
*/
54
static int[] REPEATS = {
55
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
56
32, 64, 128, 256, 512, 1024, 64 * 1024, 1024 * 1024
57
};
58
59
/*
60
* Varitions of Strings.
61
*/
62
static String[] STRINGS = new String[] {
63
"", "\0", " ", "a", "$", "\u2022",
64
"ab", "abc", "abcd", "abcde",
65
"The quick brown fox jumps over the lazy dog."
66
};
67
68
/*
69
* Repeat String function tests.
70
*/
71
static void test1() {
72
for (int repeat : REPEATS) {
73
for (String string : STRINGS) {
74
long limit = (long)string.length() * (long)repeat;
75
76
if ((long)(Integer.MAX_VALUE >> 1) <= limit) {
77
break;
78
}
79
80
verify(string.repeat(repeat), string, repeat);
81
}
82
}
83
}
84
85
/*
86
* Repeat String exception tests.
87
*/
88
static void test2() {
89
try {
90
"abc".repeat(-1);
91
throw new RuntimeException("No exception for negative repeat count");
92
} catch (IllegalArgumentException ex) {
93
// Correct
94
}
95
96
try {
97
"abc".repeat(Integer.MAX_VALUE - 1);
98
throw new RuntimeException("No exception for large repeat count");
99
} catch (OutOfMemoryError ex) {
100
// Correct
101
}
102
}
103
104
static String truncate(String string) {
105
if (string.length() < 80) {
106
return string;
107
}
108
return string.substring(0, 80) + "...";
109
}
110
111
/*
112
* Verify string repeat patterns.
113
*/
114
static void verify(String result, String string, int repeat) {
115
if (string.isEmpty() || repeat == 0) {
116
if (!result.isEmpty()) {
117
System.err.format("\"%s\".repeat(%d)%n", truncate(string), repeat);
118
System.err.format("Result \"%s\"%n", truncate(result));
119
System.err.format("Result expected to be empty, found string of length %d%n", result.length());
120
throw new RuntimeException();
121
}
122
} else {
123
int expected = 0;
124
int count = 0;
125
for (int offset = result.indexOf(string, expected);
126
0 <= offset;
127
offset = result.indexOf(string, expected)) {
128
count++;
129
if (offset != expected) {
130
System.err.format("\"%s\".repeat(%d)%n", truncate(string), repeat);
131
System.err.format("Result \"%s\"%n", truncate(result));
132
System.err.format("Repeat expected at %d, found at = %d%n", expected, offset);
133
throw new RuntimeException();
134
}
135
expected += string.length();
136
}
137
if (count != repeat) {
138
System.err.format("\"%s\".repeat(%d)%n", truncate(string), repeat);
139
System.err.format("Result \"%s\"%n", truncate(result));
140
System.err.format("Repeat count expected to be %d, found %d%n", repeat, count);
141
throw new RuntimeException();
142
}
143
}
144
}
145
}
146
147