Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/StringBuilder/HugeCapacity.java
41149 views
1
/*
2
* Copyright (c) 2016, 2020, 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
* @bug 8149330 8218227
27
* @summary Capacity should not get close to Integer.MAX_VALUE unless
28
* necessary
29
* @requires (sun.arch.data.model == "64" & os.maxMemory >= 6G)
30
* @run main/othervm -Xms5G -Xmx5G -XX:+CompactStrings HugeCapacity true
31
* @run main/othervm -Xms5G -Xmx5G -XX:-CompactStrings HugeCapacity false
32
*/
33
34
public class HugeCapacity {
35
private static int failures = 0;
36
37
public static void main(String[] args) {
38
if (args.length == 0) {
39
throw new IllegalArgumentException("Need the argument");
40
}
41
boolean isCompact = Boolean.parseBoolean(args[0]);
42
43
testLatin1(isCompact);
44
testUtf16();
45
testHugeInitialString();
46
testHugeInitialCharSequence();
47
if (failures > 0) {
48
throw new RuntimeException(failures + " tests failed");
49
}
50
}
51
52
private static void testLatin1(boolean isCompact) {
53
try {
54
int divisor = isCompact ? 2 : 4;
55
StringBuilder sb = new StringBuilder();
56
sb.ensureCapacity(Integer.MAX_VALUE / divisor);
57
sb.ensureCapacity(Integer.MAX_VALUE / divisor + 1);
58
} catch (OutOfMemoryError oom) {
59
oom.printStackTrace();
60
failures++;
61
}
62
}
63
64
private static void testUtf16() {
65
try {
66
StringBuilder sb = new StringBuilder();
67
sb.append('\u042b');
68
sb.ensureCapacity(Integer.MAX_VALUE / 4);
69
sb.ensureCapacity(Integer.MAX_VALUE / 4 + 1);
70
} catch (OutOfMemoryError oom) {
71
oom.printStackTrace();
72
failures++;
73
}
74
}
75
76
private static void testHugeInitialString() {
77
try {
78
String str = "Z".repeat(Integer.MAX_VALUE - 8);
79
StringBuilder sb = new StringBuilder(str);
80
} catch (OutOfMemoryError ignore) {
81
} catch (Throwable unexpected) {
82
unexpected.printStackTrace();
83
failures++;
84
}
85
}
86
87
private static void testHugeInitialCharSequence() {
88
try {
89
CharSequence seq = new MyHugeCharSeq();
90
StringBuilder sb = new StringBuilder(seq);
91
} catch (OutOfMemoryError ignore) {
92
} catch (Throwable unexpected) {
93
unexpected.printStackTrace();
94
failures++;
95
}
96
}
97
98
private static class MyHugeCharSeq implements CharSequence {
99
public char charAt(int i) {
100
throw new UnsupportedOperationException();
101
}
102
public int length() { return Integer.MAX_VALUE; }
103
public CharSequence subSequence(int st, int e) {
104
throw new UnsupportedOperationException();
105
}
106
public String toString() { return ""; }
107
}
108
}
109
110