author | prr |
Fri, 25 May 2018 12:12:24 -0700 | |
changeset 50347 | b2f046ae8eb6 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
1 |
/* |
40684
2e37c119dc2a
8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents:
38356
diff
changeset
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
4 |
* |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
8 |
* |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
13 |
* accompanied this code). |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
14 |
* |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
18 |
* |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
21 |
* questions. |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
22 |
*/ |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
23 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
24 |
/** |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
25 |
* @test |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
26 |
* @summary Objects.checkIndex/jdk.internal.util.Preconditions.checkIndex tests |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
27 |
* @run testng CheckIndex |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
28 |
* @bug 8135248 8142493 8155794 |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
29 |
* @modules java.base/jdk.internal.util |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
30 |
*/ |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
31 |
|
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
32 |
import jdk.internal.util.Preconditions; |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
33 |
import org.testng.annotations.DataProvider; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
34 |
import org.testng.annotations.Test; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
35 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
36 |
import java.util.ArrayList; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
37 |
import java.util.List; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
38 |
import java.util.Objects; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
39 |
import java.util.function.BiConsumer; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
40 |
import java.util.function.BiFunction; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
41 |
import java.util.function.IntSupplier; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
42 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
43 |
import static org.testng.Assert.*; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
44 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
45 |
public class CheckIndex { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
46 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
47 |
static class AssertingOutOfBoundsException extends RuntimeException { |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
48 |
public AssertingOutOfBoundsException(String message) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
49 |
super(message); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
50 |
} |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
51 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
52 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
53 |
static BiFunction<String, List<Integer>, AssertingOutOfBoundsException> assertingOutOfBounds( |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
54 |
String message, String expCheckKind, Integer... expArgs) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
55 |
return (checkKind, args) -> { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
56 |
assertEquals(checkKind, expCheckKind); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
57 |
assertEquals(args, List.of(expArgs)); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
58 |
try { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
59 |
args.clear(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
60 |
fail("Out of bounds List<Integer> argument should be unmodifiable"); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
61 |
} catch (Exception e) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
62 |
} |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
63 |
return new AssertingOutOfBoundsException(message); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
64 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
65 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
66 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
67 |
static BiFunction<String, List<Integer>, AssertingOutOfBoundsException> assertingOutOfBoundsReturnNull( |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
68 |
String expCheckKind, Integer... expArgs) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
69 |
return (checkKind, args) -> { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
70 |
assertEquals(checkKind, expCheckKind); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
71 |
assertEquals(args, List.of(expArgs)); |
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
72 |
return null; |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
73 |
}; |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
74 |
} |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
75 |
|
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
76 |
static final int[] VALUES = {0, 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE, -1, Integer.MIN_VALUE + 1, Integer.MIN_VALUE}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
77 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
78 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
79 |
static Object[][] checkIndexProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
80 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
81 |
for (int index : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
82 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
83 |
boolean withinBounds = index >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
84 |
length >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
85 |
index < length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
86 |
l.add(new Object[]{index, length, withinBounds}); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
87 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
88 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
89 |
return l.toArray(new Object[0][0]); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
90 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
91 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
92 |
interface X { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
93 |
int apply(int a, int b, int c); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
94 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
95 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
96 |
@Test(dataProvider = "checkIndexProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
97 |
public void testCheckIndex(int index, int length, boolean withinBounds) { |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
98 |
String expectedMessage = withinBounds |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
99 |
? null |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
100 |
: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new). |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
101 |
apply("checkIndex", List.of(index, length)).getMessage(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
102 |
|
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
103 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> checker = (ec, s) -> { |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
104 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
105 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
106 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
107 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
108 |
"Index %d is out of bounds of [0, %d), but was reported to be within bounds", index, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
109 |
assertEquals(rIndex, index); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
110 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
111 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
112 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
113 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
114 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
115 |
"Index %d is within bounds of [0, %d), but was reported to be out of bounds", index, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
116 |
else |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
117 |
assertEquals(e.getMessage(), expectedMessage); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
118 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
119 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
120 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
121 |
checker.accept(AssertingOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
122 |
() -> Preconditions.checkIndex(index, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
123 |
assertingOutOfBounds(expectedMessage, "checkIndex", index, length))); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
124 |
checker.accept(IndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
125 |
() -> Preconditions.checkIndex(index, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
126 |
assertingOutOfBoundsReturnNull("checkIndex", index, length))); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
127 |
checker.accept(IndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
128 |
() -> Preconditions.checkIndex(index, length, null)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
129 |
checker.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
130 |
() -> Objects.checkIndex(index, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
131 |
checker.accept(ArrayIndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
132 |
() -> Preconditions.checkIndex(index, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
133 |
Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new))); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
134 |
checker.accept(StringIndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
135 |
() -> Preconditions.checkIndex(index, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
136 |
Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
137 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
138 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
139 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
140 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
141 |
static Object[][] checkFromToIndexProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
142 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
143 |
for (int fromIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
144 |
for (int toIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
145 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
146 |
boolean withinBounds = fromIndex >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
147 |
toIndex >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
148 |
length >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
149 |
fromIndex <= toIndex && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
150 |
toIndex <= length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
151 |
l.add(new Object[]{fromIndex, toIndex, length, withinBounds}); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
152 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
153 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
154 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
155 |
return l.toArray(new Object[0][0]); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
156 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
157 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
158 |
@Test(dataProvider = "checkFromToIndexProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
159 |
public void testCheckFromToIndex(int fromIndex, int toIndex, int length, boolean withinBounds) { |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
160 |
String expectedMessage = withinBounds |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
161 |
? null |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
162 |
: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new). |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
163 |
apply("checkFromToIndex", List.of(fromIndex, toIndex, length)).getMessage(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
164 |
|
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
165 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
166 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
167 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
168 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
169 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
170 |
"Range [%d, %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, toIndex, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
171 |
assertEquals(rIndex, fromIndex); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
172 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
173 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
174 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
175 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
176 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
177 |
"Range [%d, %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, toIndex, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
178 |
else |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
179 |
assertEquals(e.getMessage(), expectedMessage); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
180 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
181 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
182 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
183 |
check.accept(AssertingOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
184 |
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
185 |
assertingOutOfBounds(expectedMessage, "checkFromToIndex", fromIndex, toIndex, length))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
186 |
check.accept(IndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
187 |
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
188 |
assertingOutOfBoundsReturnNull("checkFromToIndex", fromIndex, toIndex, length))); |
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
189 |
check.accept(IndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
190 |
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, null)); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
191 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
192 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
193 |
check.accept(ArrayIndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
194 |
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
195 |
Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new))); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
196 |
check.accept(StringIndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
197 |
() -> Preconditions.checkFromToIndex(fromIndex, toIndex, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
198 |
Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
199 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
200 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
201 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
202 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
203 |
static Object[][] checkFromIndexSizeProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
204 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
205 |
for (int fromIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
206 |
for (int size : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
207 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
208 |
// Explicitly convert to long |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
209 |
long lFromIndex = fromIndex; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
210 |
long lSize = size; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
211 |
long lLength = length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
212 |
// Avoid overflow |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
213 |
long lToIndex = lFromIndex + lSize; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
214 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
215 |
boolean withinBounds = lFromIndex >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
216 |
lSize >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
217 |
lLength >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
218 |
lFromIndex <= lToIndex && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
219 |
lToIndex <= lLength; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
220 |
l.add(new Object[]{fromIndex, size, length, withinBounds}); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
221 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
222 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
223 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
224 |
return l.toArray(new Object[0][0]); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
225 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
226 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
227 |
@Test(dataProvider = "checkFromIndexSizeProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
228 |
public void testCheckFromIndexSize(int fromIndex, int size, int length, boolean withinBounds) { |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
229 |
String expectedMessage = withinBounds |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
230 |
? null |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
231 |
: Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new). |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
232 |
apply("checkFromIndexSize", List.of(fromIndex, size, length)).getMessage(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
233 |
|
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
234 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
235 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
236 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
237 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
238 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
239 |
"Range [%d, %d + %d) is out of bounds of [0, %d), but was reported to be withing bounds", fromIndex, fromIndex, size, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
240 |
assertEquals(rIndex, fromIndex); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
241 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
242 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
243 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
244 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
245 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
246 |
"Range [%d, %d + %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, fromIndex, size, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
247 |
else |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
248 |
assertEquals(e.getMessage(), expectedMessage); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
249 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
250 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
251 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
252 |
check.accept(AssertingOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
253 |
() -> Preconditions.checkFromIndexSize(fromIndex, size, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
254 |
assertingOutOfBounds(expectedMessage, "checkFromIndexSize", fromIndex, size, length))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
255 |
check.accept(IndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
256 |
() -> Preconditions.checkFromIndexSize(fromIndex, size, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
257 |
assertingOutOfBoundsReturnNull("checkFromIndexSize", fromIndex, size, length))); |
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
258 |
check.accept(IndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
259 |
() -> Preconditions.checkFromIndexSize(fromIndex, size, length, null)); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
260 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
261 |
() -> Objects.checkFromIndexSize(fromIndex, size, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
262 |
check.accept(ArrayIndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
263 |
() -> Preconditions.checkFromIndexSize(fromIndex, size, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
264 |
Preconditions.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new))); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
265 |
check.accept(StringIndexOutOfBoundsException.class, |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
266 |
() -> Preconditions.checkFromIndexSize(fromIndex, size, length, |
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
267 |
Preconditions.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
268 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
269 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
270 |
@Test |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
271 |
public void uniqueMessagesForCheckKinds() { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
272 |
BiFunction<String, List<Integer>, IndexOutOfBoundsException> f = |
38356
1e4ecca97792
8155794: Move Objects.checkIndex BiFunction accepting methods to an internal package
psandoz
parents:
37345
diff
changeset
|
273 |
Preconditions.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
274 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
275 |
List<String> messages = new ArrayList<>(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
276 |
// Exact arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
277 |
messages.add(f.apply("checkIndex", List.of(-1, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
278 |
messages.add(f.apply("checkFromToIndex", List.of(-1, 0, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
279 |
messages.add(f.apply("checkFromIndexSize", List.of(-1, 0, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
280 |
// Unknown check kind |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
281 |
messages.add(f.apply("checkUnknown", List.of(-1, 0, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
282 |
// Known check kind with more arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
283 |
messages.add(f.apply("checkIndex", List.of(-1, 0, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
284 |
messages.add(f.apply("checkFromToIndex", List.of(-1, 0, 0, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
285 |
messages.add(f.apply("checkFromIndexSize", List.of(-1, 0, 0, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
286 |
// Known check kind with fewer arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
287 |
messages.add(f.apply("checkIndex", List.of(-1)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
288 |
messages.add(f.apply("checkFromToIndex", List.of(-1, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
289 |
messages.add(f.apply("checkFromIndexSize", List.of(-1, 0)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
290 |
// Null arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
291 |
messages.add(f.apply(null, null).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
292 |
messages.add(f.apply("checkNullArguments", null).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
293 |
messages.add(f.apply(null, List.of(-1)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
294 |
|
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
295 |
assertEquals(messages.size(), messages.stream().distinct().count()); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
296 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
297 |
} |