Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/StringJoiner/StringJoinerOomUtf16Test.java
41149 views
1
/*
2
* Copyright (c) 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
* @test
25
* @bug 8265237
26
* @summary tests StringJoiner OOME when joining sub-max-length Strings
27
* @modules java.base/jdk.internal.util
28
* @requires vm.bits == "64" & os.maxMemory > 4G
29
* @run testng/othervm -Xmx4g -XX:+CompactStrings StringJoinerOomUtf16Test
30
*/
31
32
import org.testng.annotations.Test;
33
34
import static jdk.internal.util.ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
35
import static org.testng.Assert.fail;
36
37
import java.util.StringJoiner;
38
39
40
@Test(groups = {"unit","string","util","libs"})
41
public class StringJoinerOomUtf16Test {
42
43
// the sum of lengths of the following two strings is way less than
44
// SOFT_MAX_ARRAY_LENGTH, but the byte[] array holding the UTF16 representation
45
// would need to be bigger than Integer.MAX_VALUE...
46
private static final String HALF_MAX_LATIN1_STRING =
47
"*".repeat(SOFT_MAX_ARRAY_LENGTH >> 1);
48
private static final String OVERFLOW_UTF16_STRING =
49
"\u017D".repeat(((Integer.MAX_VALUE - SOFT_MAX_ARRAY_LENGTH) >> 1) + 1);
50
51
public void OOM1() {
52
try {
53
new StringJoiner("")
54
.add(HALF_MAX_LATIN1_STRING)
55
.add(OVERFLOW_UTF16_STRING)
56
.toString();
57
fail("Should have thrown OutOfMemoryError");
58
} catch (OutOfMemoryError ex) {
59
System.out.println("Expected: " + ex);
60
}
61
}
62
63
public void OOM2() {
64
try {
65
new StringJoiner(HALF_MAX_LATIN1_STRING)
66
.add("")
67
.add(OVERFLOW_UTF16_STRING)
68
.toString();
69
fail("Should have thrown OutOfMemoryError");
70
} catch (OutOfMemoryError ex) {
71
System.out.println("Expected: " + ex);
72
}
73
}
74
75
public void OOM3() {
76
try {
77
new StringJoiner(OVERFLOW_UTF16_STRING)
78
.add("")
79
.add(HALF_MAX_LATIN1_STRING)
80
.toString();
81
fail("Should have thrown OutOfMemoryError");
82
} catch (OutOfMemoryError ex) {
83
System.out.println("Expected: " + ex);
84
}
85
}
86
87
public void OOM4() {
88
try {
89
new StringJoiner("", HALF_MAX_LATIN1_STRING, OVERFLOW_UTF16_STRING)
90
.toString();
91
fail("Should have thrown OutOfMemoryError");
92
} catch (OutOfMemoryError ex) {
93
System.out.println("Expected: " + ex);
94
}
95
}
96
}
97
98
99