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