author | psandoz |
Wed, 13 Apr 2016 15:05:50 +0200 | |
changeset 37345 | 9cb6e1141bdb |
parent 33844 | c81ff6d8588d |
child 38356 | 1e4ecca97792 |
permissions | -rw-r--r-- |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
1 |
/* |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
2 |
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
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 |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
26 |
* @summary IndexOutOfBoundsException check index tests |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
27 |
* @run testng CheckIndex |
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
28 |
* @bug 8135248 8142493 |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
29 |
*/ |
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 |
import org.testng.annotations.DataProvider; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
32 |
import org.testng.annotations.Test; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
33 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
34 |
import java.util.ArrayList; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
35 |
import java.util.List; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
36 |
import java.util.Objects; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
37 |
import java.util.function.BiConsumer; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
38 |
import java.util.function.BiFunction; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
39 |
import java.util.function.IntSupplier; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
40 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
41 |
import static org.testng.Assert.*; |
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 |
public class CheckIndex { |
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 |
static class AssertingOutOfBoundsException extends RuntimeException { |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
46 |
public AssertingOutOfBoundsException(String message) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
47 |
super(message); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
48 |
} |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
49 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
50 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
51 |
static BiFunction<String, List<Integer>, AssertingOutOfBoundsException> assertingOutOfBounds( |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
52 |
String message, String expCheckKind, Integer... expArgs) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
53 |
return (checkKind, args) -> { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
54 |
assertEquals(checkKind, expCheckKind); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
55 |
assertEquals(args, List.of(expArgs)); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
56 |
try { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
57 |
args.clear(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
58 |
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
|
59 |
} catch (Exception e) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
60 |
} |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
61 |
return new AssertingOutOfBoundsException(message); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
62 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
63 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
64 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
65 |
static BiFunction<String, List<Integer>, AssertingOutOfBoundsException> assertingOutOfBoundsReturnNull( |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
66 |
String expCheckKind, Integer... expArgs) { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
67 |
return (checkKind, args) -> { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
68 |
assertEquals(checkKind, expCheckKind); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
69 |
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
|
70 |
return null; |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
71 |
}; |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
72 |
} |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
73 |
|
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
74 |
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
|
75 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
76 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
77 |
static Object[][] checkIndexProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
78 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
79 |
for (int index : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
80 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
81 |
boolean withinBounds = index >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
82 |
length >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
83 |
index < length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
84 |
l.add(new Object[]{index, length, withinBounds}); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
85 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
86 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
87 |
return l.toArray(new Object[0][0]); |
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 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
90 |
interface X { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
91 |
int apply(int a, int b, int c); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
92 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
93 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
94 |
@Test(dataProvider = "checkIndexProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
95 |
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
|
96 |
String expectedMessage = withinBounds |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
97 |
? null |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
98 |
: Objects.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new). |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
99 |
apply("checkIndex", List.of(index, length)).getMessage(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
100 |
|
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
101 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> checker = (ec, s) -> { |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
102 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
103 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
104 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
105 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
106 |
"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
|
107 |
assertEquals(rIndex, index); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
108 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
109 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
110 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
111 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
112 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
113 |
"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
|
114 |
else |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
115 |
assertEquals(e.getMessage(), expectedMessage); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
116 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
117 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
118 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
119 |
checker.accept(AssertingOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
120 |
() -> Objects.checkIndex(index, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
121 |
assertingOutOfBounds(expectedMessage, "checkIndex", index, length))); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
122 |
checker.accept(IndexOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
123 |
() -> Objects.checkIndex(index, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
124 |
assertingOutOfBoundsReturnNull("checkIndex", index, length))); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
125 |
checker.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
126 |
() -> Objects.checkIndex(index, length, null)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
127 |
checker.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
128 |
() -> Objects.checkIndex(index, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
129 |
checker.accept(ArrayIndexOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
130 |
() -> Objects.checkIndex(index, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
131 |
Objects.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new))); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
132 |
checker.accept(StringIndexOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
133 |
() -> Objects.checkIndex(index, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
134 |
Objects.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
135 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
136 |
|
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 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
139 |
static Object[][] checkFromToIndexProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
140 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
141 |
for (int fromIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
142 |
for (int toIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
143 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
144 |
boolean withinBounds = fromIndex >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
145 |
toIndex >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
146 |
length >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
147 |
fromIndex <= toIndex && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
148 |
toIndex <= length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
149 |
l.add(new Object[]{fromIndex, toIndex, length, withinBounds}); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
150 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
151 |
} |
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 |
return l.toArray(new Object[0][0]); |
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 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
156 |
@Test(dataProvider = "checkFromToIndexProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
157 |
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
|
158 |
String expectedMessage = withinBounds |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
159 |
? null |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
160 |
: Objects.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new). |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
161 |
apply("checkFromToIndex", List.of(fromIndex, toIndex, length)).getMessage(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
162 |
|
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
163 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
164 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
165 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
166 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
167 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
168 |
"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
|
169 |
assertEquals(rIndex, fromIndex); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
170 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
171 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
172 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
173 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
174 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
175 |
"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
|
176 |
else |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
177 |
assertEquals(e.getMessage(), expectedMessage); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
178 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
179 |
}; |
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 |
check.accept(AssertingOutOfBoundsException.class, |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
182 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
183 |
assertingOutOfBounds(expectedMessage, "checkFromToIndex", fromIndex, toIndex, length))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
184 |
check.accept(IndexOutOfBoundsException.class, |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
185 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
186 |
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
|
187 |
check.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
188 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, null)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
189 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
190 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
191 |
check.accept(ArrayIndexOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
192 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
193 |
Objects.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new))); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
194 |
check.accept(StringIndexOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
195 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
196 |
Objects.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
197 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
198 |
|
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 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
201 |
static Object[][] checkFromIndexSizeProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
202 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
203 |
for (int fromIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
204 |
for (int size : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
205 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
206 |
// Explicitly convert to long |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
207 |
long lFromIndex = fromIndex; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
208 |
long lSize = size; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
209 |
long lLength = length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
210 |
// Avoid overflow |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
211 |
long lToIndex = lFromIndex + lSize; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
212 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
213 |
boolean withinBounds = lFromIndex >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
214 |
lSize >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
215 |
lLength >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
216 |
lFromIndex <= lToIndex && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
217 |
lToIndex <= lLength; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
218 |
l.add(new Object[]{fromIndex, size, length, withinBounds}); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
219 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
220 |
} |
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 |
return l.toArray(new Object[0][0]); |
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 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
225 |
@Test(dataProvider = "checkFromIndexSizeProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
226 |
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
|
227 |
String expectedMessage = withinBounds |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
228 |
? null |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
229 |
: Objects.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new). |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
230 |
apply("checkFromIndexSize", List.of(fromIndex, size, length)).getMessage(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
231 |
|
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
232 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
233 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
234 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
235 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
236 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
237 |
"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
|
238 |
assertEquals(rIndex, fromIndex); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
239 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
240 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
241 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
242 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
243 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
244 |
"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
|
245 |
else |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
246 |
assertEquals(e.getMessage(), expectedMessage); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
247 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
248 |
}; |
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 |
check.accept(AssertingOutOfBoundsException.class, |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
251 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
252 |
assertingOutOfBounds(expectedMessage, "checkFromIndexSize", fromIndex, size, length))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
253 |
check.accept(IndexOutOfBoundsException.class, |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
254 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
255 |
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
|
256 |
check.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
257 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, null)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
258 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
259 |
() -> Objects.checkFromIndexSize(fromIndex, size, length)); |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
260 |
check.accept(ArrayIndexOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
261 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
262 |
Objects.outOfBoundsExceptionFormatter(ArrayIndexOutOfBoundsException::new))); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
263 |
check.accept(StringIndexOutOfBoundsException.class, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
264 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
265 |
Objects.outOfBoundsExceptionFormatter(StringIndexOutOfBoundsException::new))); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
266 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
267 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
268 |
@Test |
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
269 |
public void uniqueMessagesForCheckKinds() { |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
270 |
BiFunction<String, List<Integer>, IndexOutOfBoundsException> f = |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
271 |
Objects.outOfBoundsExceptionFormatter(IndexOutOfBoundsException::new); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
272 |
|
37345
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
273 |
List<String> messages = new ArrayList<>(); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
274 |
// Exact arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
275 |
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
|
276 |
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
|
277 |
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
|
278 |
// Unknown check kind |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
279 |
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
|
280 |
// Known check kind with more arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
281 |
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
|
282 |
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
|
283 |
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
|
284 |
// Known check kind with fewer arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
285 |
messages.add(f.apply("checkIndex", List.of(-1)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
286 |
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
|
287 |
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
|
288 |
// Null arguments |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
289 |
messages.add(f.apply(null, null).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
290 |
messages.add(f.apply("checkNullArguments", null).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
291 |
messages.add(f.apply(null, List.of(-1)).getMessage()); |
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
292 |
|
9cb6e1141bdb
8146458: Improve exception reporting for Objects.checkIndex/checkFromToIndex/checkFromIndexSize
psandoz
parents:
33844
diff
changeset
|
293 |
assertEquals(messages.size(), messages.stream().distinct().count()); |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
294 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
295 |
} |