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