Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/StringBuilder/Capacity.java
41149 views
1
/*
2
* Copyright (c) 2016, 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
27
* @summary Basic set of tests of capacity management
28
* @run testng Capacity
29
*/
30
31
import java.lang.reflect.Field;
32
import java.util.AbstractList;
33
import java.util.ArrayList;
34
import java.util.Collections;
35
import java.util.List;
36
import java.util.SplittableRandom;
37
38
import org.testng.annotations.Test;
39
import org.testng.annotations.DataProvider;
40
import static org.testng.Assert.*;
41
42
public class Capacity {
43
static final int DEFAULT_CAPACITY = 16;
44
45
private static int newCapacity(int oldCapacity,
46
int desiredCapacity)
47
{
48
return Math.max(oldCapacity * 2 + 2, desiredCapacity);
49
}
50
51
private static int nextNewCapacity(int oldCapacity) {
52
return newCapacity(oldCapacity, oldCapacity + 1);
53
}
54
55
@Test(dataProvider = "singleChar")
56
public void defaultCapacity(Character ch) {
57
StringBuilder sb = new StringBuilder();
58
assertEquals(sb.capacity(), DEFAULT_CAPACITY);
59
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
60
sb.append(ch);
61
assertEquals(sb.capacity(), DEFAULT_CAPACITY);
62
}
63
sb.append(ch);
64
assertEquals(sb.capacity(), nextNewCapacity(DEFAULT_CAPACITY));
65
}
66
67
@Test(dataProvider = "charCapacity")
68
public void explicitCapacity(Character ch, int initCapacity) {
69
StringBuilder sb = new StringBuilder(initCapacity);
70
assertEquals(sb.capacity(), initCapacity);
71
for (int i = 0; i < initCapacity; i++) {
72
sb.append(ch);
73
assertEquals(sb.capacity(), initCapacity);
74
}
75
sb.append(ch);
76
assertEquals(sb.capacity(), nextNewCapacity(initCapacity));
77
}
78
79
@Test(dataProvider = "singleChar")
80
public void sbFromString(Character ch) {
81
String s = "string " + ch;
82
int expectedCapacity = s.length() + DEFAULT_CAPACITY;
83
StringBuilder sb = new StringBuilder(s);
84
assertEquals(sb.capacity(), expectedCapacity);
85
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
86
sb.append(ch);
87
assertEquals(sb.capacity(), expectedCapacity);
88
}
89
sb.append(ch);
90
assertEquals(sb.capacity(), nextNewCapacity(expectedCapacity));
91
}
92
93
@Test(dataProvider = "singleChar")
94
public void sbFromCharSeq(Character ch) {
95
CharSequence cs = new MyCharSeq("char seq " + ch);
96
int expectedCapacity = cs.length() + DEFAULT_CAPACITY;
97
StringBuilder sb = new StringBuilder(cs);
98
assertEquals(sb.capacity(), expectedCapacity);
99
for (int i = 0; i < DEFAULT_CAPACITY; i++) {
100
sb.append(ch);
101
assertEquals(sb.capacity(), expectedCapacity);
102
}
103
sb.append(ch);
104
assertEquals(sb.capacity(), nextNewCapacity(expectedCapacity));
105
}
106
107
@Test(dataProvider = "charCapacity")
108
public void ensureCapacity(Character ch, int cap) {
109
StringBuilder sb = new StringBuilder(0);
110
assertEquals(sb.capacity(), 0);
111
sb.ensureCapacity(cap); // only has effect if cap > 0
112
int newCap = (cap == 0) ? 0 : newCapacity(0, cap);
113
assertEquals(sb.capacity(), newCap);
114
sb.ensureCapacity(newCap + 1);
115
assertEquals(sb.capacity(), nextNewCapacity(newCap));
116
sb.append(ch);
117
assertEquals(sb.capacity(), nextNewCapacity(newCap));
118
}
119
120
@Test(dataProvider = "negativeCapacity",
121
expectedExceptions = NegativeArraySizeException.class)
122
public void negativeInitialCapacity(int negCap) {
123
StringBuilder sb = new StringBuilder(negCap);
124
}
125
126
@Test(dataProvider = "negativeCapacity")
127
public void ensureNegativeCapacity(int negCap) {
128
StringBuilder sb = new StringBuilder();
129
sb.ensureCapacity(negCap);
130
assertEquals(sb.capacity(), DEFAULT_CAPACITY);
131
}
132
133
@Test(dataProvider = "charCapacity")
134
public void trimToSize(Character ch, int cap) {
135
StringBuilder sb = new StringBuilder(cap);
136
int halfOfCap = cap / 2;
137
for (int i = 0; i < halfOfCap; i++) {
138
sb.append(ch);
139
}
140
sb.trimToSize();
141
// according to the spec, capacity doesn't have to
142
// become exactly the size
143
assertTrue(sb.capacity() >= halfOfCap);
144
}
145
146
@DataProvider
147
public Object[][] singleChar() {
148
return new Object[][] { {'J'}, {'\u042b'} };
149
}
150
151
@DataProvider
152
public Object[][] charCapacity() {
153
return new Object[][] {
154
{'J', 0},
155
{'J', 1},
156
{'J', 15},
157
{'J', DEFAULT_CAPACITY},
158
{'J', 1024},
159
{'\u042b', 0},
160
{'\u042b', 1},
161
{'\u042b', 15},
162
{'\u042b', DEFAULT_CAPACITY},
163
{'\u042b', 1024},
164
};
165
}
166
167
@DataProvider
168
public Object[][] negativeCapacity() {
169
return new Object[][] { {-1}, {Integer.MIN_VALUE} };
170
}
171
172
private static class MyCharSeq implements CharSequence {
173
private CharSequence s;
174
public MyCharSeq(CharSequence s) { this.s = s; }
175
public char charAt(int i) { return s.charAt(i); }
176
public int length() { return s.length(); }
177
public CharSequence subSequence(int st, int e) {
178
return s.subSequence(st, e);
179
}
180
public String toString() { return s.toString(); }
181
}
182
}
183
184