author | psandoz |
Tue, 17 Nov 2015 14:11:46 +0100 | |
changeset 33844 | c81ff6d8588d |
parent 32929 | 7e1bb9268b8a |
child 37345 | 9cb6e1141bdb |
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 { |
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 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
48 |
static BiFunction<Integer, Integer, AssertingOutOfBoundsException> assertingOutOfBounds( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
49 |
int expFromIndex, int expToIndexOrSizeOrLength) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
50 |
return (fromIndex, toIndexOrSizeorLength) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
51 |
assertEquals(fromIndex, Integer.valueOf(expFromIndex)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
52 |
assertEquals(toIndexOrSizeorLength, Integer.valueOf(expToIndexOrSizeOrLength)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
53 |
return new AssertingOutOfBoundsException(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
54 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
55 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
56 |
|
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
57 |
static BiFunction<Integer, Integer, AssertingOutOfBoundsException> assertingOutOfBoundsReturnNull( |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
58 |
int expFromIndex, int expToIndexOrSizeOrLength) { |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
59 |
return (fromIndex, toIndexOrSizeorLength) -> { |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
60 |
assertEquals(fromIndex, Integer.valueOf(expFromIndex)); |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
61 |
assertEquals(toIndexOrSizeorLength, Integer.valueOf(expToIndexOrSizeOrLength)); |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
62 |
return null; |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
63 |
}; |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
64 |
} |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
65 |
|
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
66 |
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
|
67 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
68 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
69 |
static Object[][] checkIndexProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
70 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
71 |
for (int index : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
72 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
73 |
boolean withinBounds = index >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
74 |
length >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
75 |
index < length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
76 |
l.add(new Object[]{index, length, withinBounds}); |
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 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
79 |
return l.toArray(new Object[0][0]); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
80 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
81 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
82 |
interface X { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
83 |
int apply(int a, int b, int c); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
84 |
} |
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 |
@Test(dataProvider = "checkIndexProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
87 |
public void testCheckIndex(int index, int length, boolean withinBounds) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
88 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
89 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
90 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
91 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
92 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
93 |
"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
|
94 |
assertEquals(rIndex, index); |
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 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
97 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
98 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
99 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
100 |
"Index %d is within bounds of [0, %d), but was reported to be out of bounds", index, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
101 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
102 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
103 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
104 |
check.accept(AssertingOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
105 |
() -> Objects.checkIndex(index, length, assertingOutOfBounds(index, length))); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
106 |
check.accept(IndexOutOfBoundsException.class, |
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
107 |
() -> Objects.checkIndex(index, length, assertingOutOfBoundsReturnNull(index, length))); |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
108 |
check.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
109 |
() -> Objects.checkIndex(index, length, null)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
110 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
111 |
() -> Objects.checkIndex(index, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
112 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
113 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
114 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
115 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
116 |
static Object[][] checkFromToIndexProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
117 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
118 |
for (int fromIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
119 |
for (int toIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
120 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
121 |
boolean withinBounds = fromIndex >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
122 |
toIndex >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
123 |
length >= 0 && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
124 |
fromIndex <= toIndex && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
125 |
toIndex <= length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
126 |
l.add(new Object[]{fromIndex, toIndex, length, withinBounds}); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
127 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
128 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
129 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
130 |
return l.toArray(new Object[0][0]); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
131 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
132 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
133 |
@Test(dataProvider = "checkFromToIndexProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
134 |
public void testCheckFromToIndex(int fromIndex, int toIndex, int length, boolean withinBounds) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
135 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
136 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
137 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
138 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
139 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
140 |
"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
|
141 |
assertEquals(rIndex, fromIndex); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
142 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
143 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
144 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
145 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
146 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
147 |
"Range [%d, %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, toIndex, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
148 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
149 |
}; |
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 |
check.accept(AssertingOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
152 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, assertingOutOfBounds(fromIndex, toIndex))); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
153 |
check.accept(IndexOutOfBoundsException.class, |
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
154 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, assertingOutOfBoundsReturnNull(fromIndex, toIndex))); |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
155 |
check.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
156 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length, null)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
157 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
158 |
() -> Objects.checkFromToIndex(fromIndex, toIndex, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
159 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
160 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
161 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
162 |
@DataProvider |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
163 |
static Object[][] checkFromIndexSizeProvider() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
164 |
List<Object[]> l = new ArrayList<>(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
165 |
for (int fromIndex : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
166 |
for (int size : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
167 |
for (int length : VALUES) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
168 |
// Explicitly convert to long |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
169 |
long lFromIndex = fromIndex; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
170 |
long lSize = size; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
171 |
long lLength = length; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
172 |
// Avoid overflow |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
173 |
long lToIndex = lFromIndex + lSize; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
174 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
175 |
boolean withinBounds = lFromIndex >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
176 |
lSize >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
177 |
lLength >= 0L && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
178 |
lFromIndex <= lToIndex && |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
179 |
lToIndex <= lLength; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
180 |
l.add(new Object[]{fromIndex, size, length, withinBounds}); |
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 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
184 |
return l.toArray(new Object[0][0]); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
185 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
186 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
187 |
@Test(dataProvider = "checkFromIndexSizeProvider") |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
188 |
public void testCheckFromIndexSize(int fromIndex, int size, int length, boolean withinBounds) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
189 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
190 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
191 |
int rIndex = s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
192 |
if (!withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
193 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
194 |
"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
|
195 |
assertEquals(rIndex, fromIndex); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
196 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
197 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
198 |
assertTrue(ec.isInstance(e)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
199 |
if (withinBounds) |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
200 |
fail(String.format( |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
201 |
"Range [%d, %d + %d) is within bounds of [0, %d), but was reported to be out of bounds", fromIndex, fromIndex, size, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
202 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
203 |
}; |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
204 |
|
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
205 |
check.accept(AssertingOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
206 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, assertingOutOfBounds(fromIndex, size))); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
207 |
check.accept(IndexOutOfBoundsException.class, |
33844
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
208 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, assertingOutOfBoundsReturnNull(fromIndex, size))); |
c81ff6d8588d
8142493: Utility methods to check indexes and ranges doesn't specify behavior when function produces null
psandoz
parents:
32929
diff
changeset
|
209 |
check.accept(IndexOutOfBoundsException.class, |
32929
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
210 |
() -> Objects.checkFromIndexSize(fromIndex, size, length, null)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
211 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
212 |
() -> Objects.checkFromIndexSize(fromIndex, size, length)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
213 |
} |
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 |
@Test |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
216 |
public void checkIndexOutOfBoundsExceptionConstructors() { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
217 |
BiConsumer<Class<? extends RuntimeException>, IntSupplier> check = (ec, s) -> { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
218 |
try { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
219 |
s.getAsInt(); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
220 |
fail("Runtime exception expected"); |
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 |
catch (RuntimeException e) { |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
223 |
assertTrue(ec.isInstance(e)); |
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 |
}; |
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 |
check.accept(IndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
228 |
() -> Objects.checkIndex(1, 0, IndexOutOfBoundsException::new)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
229 |
check.accept(StringIndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
230 |
() -> Objects.checkIndex(1, 0, StringIndexOutOfBoundsException::new)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
231 |
check.accept(ArrayIndexOutOfBoundsException.class, |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
232 |
() -> Objects.checkIndex(1, 0, ArrayIndexOutOfBoundsException::new)); |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
233 |
} |
7e1bb9268b8a
8135248: Add utility methods to check indexes and ranges
psandoz
parents:
diff
changeset
|
234 |
} |