Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Objects/CheckIndex.java
41149 views
1
/*
2
* Copyright (c) 2015, 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
* @summary Objects.checkIndex/jdk.internal.util.Preconditions.checkIndex tests for int values
27
* @run testng CheckIndex
28
* @bug 8135248 8142493 8155794
29
* @modules java.base/jdk.internal.util
30
*/
31
32
import jdk.internal.util.Preconditions;
33
import org.testng.annotations.DataProvider;
34
import org.testng.annotations.Test;
35
36
import java.util.ArrayList;
37
import java.util.List;
38
import java.util.Objects;
39
import java.util.function.BiConsumer;
40
import java.util.function.BiFunction;
41
import java.util.function.IntSupplier;
42
43
import static org.testng.Assert.*;
44
45
public class CheckIndex {
46
47
static class AssertingOutOfBoundsException extends RuntimeException {
48
public AssertingOutOfBoundsException(String message) {
49
super(message);
50
}
51
}
52
53
static BiFunction<String, List<Number>, AssertingOutOfBoundsException> assertingOutOfBounds(
54
String message, String expCheckKind, Integer... expArgs) {
55
return (checkKind, args) -> {
56
assertEquals(checkKind, expCheckKind);
57
assertEquals(args, List.of(expArgs));
58
try {
59
args.clear();
60
fail("Out of bounds List<Integer> argument should be unmodifiable");
61
} catch (Exception e) {
62
}
63
return new AssertingOutOfBoundsException(message);
64
};
65
}
66
67
static BiFunction<String, List<Number>, AssertingOutOfBoundsException> assertingOutOfBoundsReturnNull(
68
String expCheckKind, Integer... expArgs) {
69
return (checkKind, args) -> {
70
assertEquals(checkKind, expCheckKind);
71
assertEquals(args, List.of(expArgs));
72
return null;
73
};
74
}
75
76
static final int[] VALUES = {0, 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, -1, Integer.MIN_VALUE + 1, Integer.MIN_VALUE};
77
78
@DataProvider
79
static Object[][] checkIndexProvider() {
80
List<Object[]> l = new ArrayList<>();
81
for (int index : VALUES) {
82
for (int length : VALUES) {
83
boolean withinBounds = index >= 0 &&
84
length >= 0 &&
85
index < length;
86
l.add(new Object[]{index, length, withinBounds});
87
}
88
}
89
return l.toArray(Object[][]::new);
90
}
91
92
@Test(dataProvider = "checkIndexProvider")
93
public void testCheckIndex(int index, int length, boolean withinBounds) {
94
String expectedMessage = withinBounds
95
? null
96
: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).
97
apply("checkIndex", List.of(index, length)).getMessage();
98
99
BiConsumer<Class<? extends RuntimeException>, IntSupplier> checker = (ec, s) -> {
100
try {
101
int rIndex = s.getAsInt();
102
if (!withinBounds)
103
fail(String.format(
104
"Index %d is out of bounds of [0, %d), but was reported to be within bounds", index, length));
105
assertEquals(rIndex, index);
106
}
107
catch (RuntimeException e) {
108
assertTrue(ec.isInstance(e));
109
if (withinBounds)
110
fail(String.format(
111
"Index %d is within bounds of [0, %d), but was reported to be out of bounds", index, length));
112
else
113
assertEquals(e.getMessage(), expectedMessage);
114
}
115
};
116
117
checker.accept(AssertingOutOfBoundsException.class,
118
() -> Preconditions.checkIndex(index, length,
119
assertingOutOfBounds(expectedMessage, "checkIndex", index, length)));
120
checker.accept(IndexOutOfBoundsException.class,
121
() -> Preconditions.checkIndex(index, length,
122
assertingOutOfBoundsReturnNull("checkIndex", index, length)));
123
checker.accept(IndexOutOfBoundsException.class,
124
() -> Preconditions.checkIndex(index, length, null));
125
checker.accept(IndexOutOfBoundsException.class,
126
() -> Objects.checkIndex(index, length));
127
checker.accept(ArrayIndexOutOfBoundsException.class,
128
() -> Preconditions.checkIndex(index, length,
129
Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));
130
checker.accept(StringIndexOutOfBoundsException.class,
131
() -> Preconditions.checkIndex(index, length,
132
Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));
133
}
134
135
136
@DataProvider
137
static Object[][] checkFromToIndexProvider() {
138
List<Object[]> l = new ArrayList<>();
139
for (int fromIndex : VALUES) {
140
for (int toIndex : VALUES) {
141
for (int length : VALUES) {
142
boolean withinBounds = fromIndex >= 0 &&
143
toIndex >= 0 &&
144
length >= 0 &&
145
fromIndex <= toIndex &&
146
toIndex <= length;
147
l.add(new Object[]{fromIndex, toIndex, length, withinBounds});
148
}
149
}
150
}
151
return l.toArray(Object[][]::new);
152
}
153
154
@Test(dataProvider = "checkFromToIndexProvider")
155
public void testCheckFromToIndex(int fromIndex, int toIndex, int length, boolean withinBounds) {
156
String expectedMessage = withinBounds
157
? null
158
: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).
159
apply("checkFromToIndex", List.of(fromIndex, toIndex, length)).getMessage();
160
161
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> {
162
try {
163
int rIndex = s.getAsInt();
164
if (!withinBounds)
165
fail(String.format(
166
"Range [%d, %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, toIndex, length));
167
assertEquals(rIndex, fromIndex);
168
}
169
catch (RuntimeException e) {
170
assertTrue(ec.isInstance(e));
171
if (withinBounds)
172
fail(String.format(
173
"Range [%d, %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, toIndex, length));
174
else
175
assertEquals(e.getMessage(), expectedMessage);
176
}
177
};
178
179
check.accept(AssertingOutOfBoundsException.class,
180
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,
181
assertingOutOfBounds(expectedMessage, "checkFromToIndex", fromIndex, toIndex, length)));
182
check.accept(IndexOutOfBoundsException.class,
183
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,
184
assertingOutOfBoundsReturnNull("checkFromToIndex", fromIndex, toIndex, length)));
185
check.accept(IndexOutOfBoundsException.class,
186
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, null));
187
check.accept(IndexOutOfBoundsException.class,
188
() -> Objects.checkFromToIndex(fromIndex, toIndex, length));
189
check.accept(ArrayIndexOutOfBoundsException.class,
190
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,
191
Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));
192
check.accept(StringIndexOutOfBoundsException.class,
193
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length,
194
Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));
195
}
196
197
198
@DataProvider
199
static Object[][] checkFromIndexSizeProvider() {
200
List<Object[]> l = new ArrayList<>();
201
for (int fromIndex : VALUES) {
202
for (int size : VALUES) {
203
for (int length : VALUES) {
204
// Explicitly convert to long
205
long lFromIndex = fromIndex;
206
long lSize = size;
207
long lLength = length;
208
// Avoid overflow
209
long lToIndex = lFromIndex + lSize;
210
211
boolean withinBounds = lFromIndex >= 0L &&
212
lSize >= 0L &&
213
lLength >= 0L &&
214
lFromIndex <= lToIndex &&
215
lToIndex <= lLength;
216
l.add(new Object[]{fromIndex, size, length, withinBounds});
217
}
218
}
219
}
220
return l.toArray(Object[][]::new);
221
}
222
223
@Test(dataProvider = "checkFromIndexSizeProvider")
224
public void testCheckFromIndexSize(int fromIndex, int size, int length, boolean withinBounds) {
225
String expectedMessage = withinBounds
226
? null
227
: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new).
228
apply("checkFromIndexSize", List.of(fromIndex, size, length)).getMessage();
229
230
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> {
231
try {
232
int rIndex = s.getAsInt();
233
if (!withinBounds)
234
fail(String.format(
235
"Range [%d, %d + %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, fromIndex, size, length));
236
assertEquals(rIndex, fromIndex);
237
}
238
catch (RuntimeException e) {
239
assertTrue(ec.isInstance(e));
240
if (withinBounds)
241
fail(String.format(
242
"Range [%d, %d + %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, fromIndex, size, length));
243
else
244
assertEquals(e.getMessage(), expectedMessage);
245
}
246
};
247
248
check.accept(AssertingOutOfBoundsException.class,
249
() -> Preconditions.checkFromIndexSize(fromIndex, size, length,
250
assertingOutOfBounds(expectedMessage, "checkFromIndexSize", fromIndex, size, length)));
251
check.accept(IndexOutOfBoundsException.class,
252
() -> Preconditions.checkFromIndexSize(fromIndex, size, length,
253
assertingOutOfBoundsReturnNull("checkFromIndexSize", fromIndex, size, length)));
254
check.accept(IndexOutOfBoundsException.class,
255
() -> Preconditions.checkFromIndexSize(fromIndex, size, length, null));
256
check.accept(IndexOutOfBoundsException.class,
257
() -> Objects.checkFromIndexSize(fromIndex, size, length));
258
check.accept(ArrayIndexOutOfBoundsException.class,
259
() -> Preconditions.checkFromIndexSize(fromIndex, size, length,
260
Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new)));
261
check.accept(StringIndexOutOfBoundsException.class,
262
() -> Preconditions.checkFromIndexSize(fromIndex, size, length,
263
Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new)));
264
}
265
266
@Test
267
public void uniqueMessagesForCheckKinds() {
268
BiFunction<String, List<Number>, IndexOutOfBoundsException> f =
269
Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new);
270
271
List<String> messages = new ArrayList<>();
272
// Exact arguments
273
messages.add(f.apply("checkIndex", List.of(-1, 0)).getMessage());
274
messages.add(f.apply("checkFromToIndex", List.of(-1, 0, 0)).getMessage());
275
messages.add(f.apply("checkFromIndexSize", List.of(-1, 0, 0)).getMessage());
276
// Unknown check kind
277
messages.add(f.apply("checkUnknown", List.of(-1, 0, 0)).getMessage());
278
// Known check kind with more arguments
279
messages.add(f.apply("checkIndex", List.of(-1, 0, 0)).getMessage());
280
messages.add(f.apply("checkFromToIndex", List.of(-1, 0, 0, 0)).getMessage());
281
messages.add(f.apply("checkFromIndexSize", List.of(-1, 0, 0, 0)).getMessage());
282
// Known check kind with fewer arguments
283
messages.add(f.apply("checkIndex", List.of(-1)).getMessage());
284
messages.add(f.apply("checkFromToIndex", List.of(-1, 0)).getMessage());
285
messages.add(f.apply("checkFromIndexSize", List.of(-1, 0)).getMessage());
286
// Null arguments
287
messages.add(f.apply(null, null).getMessage());
288
messages.add(f.apply("checkNullArguments", null).getMessage());
289
messages.add(f.apply(null, List.of(-1)).getMessage());
290
291
assertEquals(messages.size(), messages.stream().distinct().count());
292
}
293
}
294
295