52 |
52 |
53 //Array size much greater than MIN_PARTITION |
53 //Array size much greater than MIN_PARTITION |
54 private final static int LARGE_ARRAY_SIZE = 1 << 12; |
54 private final static int LARGE_ARRAY_SIZE = 1 << 12; |
55 |
55 |
56 private final static int[] ARRAY_SIZE_COLLECTION = new int[]{ |
56 private final static int[] ARRAY_SIZE_COLLECTION = new int[]{ |
57 SMALL_ARRAY_SIZE, THRESHOLD_ARRAY_SIZE,MEDIUM_ARRAY_SIZE, LARGE_ARRAY_SIZE}; |
57 SMALL_ARRAY_SIZE, |
|
58 THRESHOLD_ARRAY_SIZE, |
|
59 MEDIUM_ARRAY_SIZE, |
|
60 LARGE_ARRAY_SIZE |
|
61 }; |
58 |
62 |
59 @DataProvider |
63 @DataProvider |
60 public static Object[][] intSet(){ |
64 public static Object[][] intSet(){ |
61 return genericData(size -> IntStream.range(0, size).toArray(), new IntBinaryOperator[]{Integer::sum, Integer::min}); |
65 return genericData(size -> IntStream.range(0, size).toArray(), |
|
66 new IntBinaryOperator[]{ |
|
67 Integer::sum, |
|
68 Integer::min}); |
62 } |
69 } |
63 |
70 |
64 @DataProvider |
71 @DataProvider |
65 public static Object[][] longSet(){ |
72 public static Object[][] longSet(){ |
66 return genericData(size -> LongStream.range(0, size).toArray(), new LongBinaryOperator[]{Long::sum, Long::min}); |
73 return genericData(size -> LongStream.range(0, size).toArray(), |
|
74 new LongBinaryOperator[]{ |
|
75 Long::sum, |
|
76 Long::min}); |
67 } |
77 } |
68 |
78 |
69 @DataProvider |
79 @DataProvider |
70 public static Object[][] doubleSet(){ |
80 public static Object[][] doubleSet(){ |
71 return genericData(size -> IntStream.range(0, size).mapToDouble(i -> (double)i).toArray(), |
81 return genericData(size -> IntStream.range(0, size).mapToDouble(i -> (double)i).toArray(), |
72 new DoubleBinaryOperator[]{Double::sum, Double::min}); |
82 new DoubleBinaryOperator[]{ |
|
83 Double::sum, |
|
84 Double::min}); |
73 } |
85 } |
74 |
86 |
75 @DataProvider |
87 @DataProvider |
76 public static Object[][] stringSet(){ |
88 public static Object[][] stringSet(){ |
77 Function<Integer, String[]> stringsFunc = size -> |
89 Function<Integer, String[]> stringsFunc = size -> |
78 IntStream.range(0, size).mapToObj(Integer::toString).toArray(String[]::new); |
90 IntStream.range(0, size).mapToObj(Integer::toString).toArray(String[]::new); |
79 BinaryOperator<String> cancatBop = String::concat; |
91 BinaryOperator<String> concat = String::concat; |
80 return genericData(stringsFunc, new BinaryOperator[]{cancatBop}); |
92 return genericData(stringsFunc, |
|
93 (BinaryOperator<String>[]) new BinaryOperator[]{ |
|
94 concat }); |
81 } |
95 } |
82 |
96 |
83 private static <T, OPS> Object[][] genericData(Function<Integer, T> generateFunc, OPS[] ops) { |
97 private static <T, OPS> Object[][] genericData(Function<Integer, T> generateFunc, OPS[] ops) { |
84 //test arrays which size is equals n-1, n, n+1, test random data |
98 //test arrays which size is equals n-1, n, n+1, test random data |
85 Object[][] data = new Object[ARRAY_SIZE_COLLECTION.length * 3 * ops.length][4]; |
99 Object[][] data = new Object[ARRAY_SIZE_COLLECTION.length * 3 * ops.length][4]; |
159 |
173 |
160 String[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex); |
174 String[] parallelRangeResult = Arrays.copyOfRange(data, fromIndex, toIndex); |
161 Arrays.parallelPrefix(parallelRangeResult, op); |
175 Arrays.parallelPrefix(parallelRangeResult, op); |
162 assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex)); |
176 assertEquals(parallelRangeResult, Arrays.copyOfRange(sequentialResult, fromIndex, toIndex)); |
163 } |
177 } |
|
178 |
|
179 @Test |
|
180 public void testNPEs() { |
|
181 // null array |
|
182 assertThrows( () -> Arrays.parallelPrefix((int[]) null, Integer::max), |
|
183 NullPointerException.class, "should throw NPE"); |
|
184 assertThrows( () -> Arrays.parallelPrefix((long []) null, Long::max), |
|
185 NullPointerException.class, "should throw NPE"); |
|
186 assertThrows( () -> Arrays.parallelPrefix((double []) null, Double::max), |
|
187 NullPointerException.class, "should throw NPE"); |
|
188 assertThrows( () -> Arrays.parallelPrefix((String []) null, String::concat), |
|
189 NullPointerException.class, "should throw NPE"); |
|
190 |
|
191 // null array w/ range |
|
192 assertThrows( () -> Arrays.parallelPrefix((int[]) null, 0, 0, Integer::max), |
|
193 NullPointerException.class, "should throw NPE"); |
|
194 assertThrows( () -> Arrays.parallelPrefix((long []) null, 0, 0, Long::max), |
|
195 NullPointerException.class, "should throw NPE"); |
|
196 assertThrows( () -> Arrays.parallelPrefix((double []) null, 0, 0, Double::max), |
|
197 NullPointerException.class, "should throw NPE"); |
|
198 assertThrows( () -> Arrays.parallelPrefix((String []) null, 0, 0, String::concat), |
|
199 NullPointerException.class, "should throw NPE"); |
|
200 |
|
201 // null op |
|
202 assertThrows( () -> Arrays.parallelPrefix(new int[] {}, null), |
|
203 NullPointerException.class, "should throw NPE"); |
|
204 assertThrows( () -> Arrays.parallelPrefix(new long[] {}, null), |
|
205 NullPointerException.class, "should throw NPE"); |
|
206 assertThrows( () -> Arrays.parallelPrefix(new double[] {}, null), |
|
207 NullPointerException.class, "should throw NPE"); |
|
208 assertThrows( () -> Arrays.parallelPrefix(new String[] {}, null), |
|
209 NullPointerException.class, "should throw NPE"); |
|
210 |
|
211 // null op w/ range |
|
212 assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 0, 0, null), |
|
213 NullPointerException.class, "should throw NPE"); |
|
214 assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 0, 0, null), |
|
215 NullPointerException.class, "should throw NPE"); |
|
216 assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 0, 0, null), |
|
217 NullPointerException.class, "should throw NPE"); |
|
218 assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 0, 0, null), |
|
219 NullPointerException.class, "should throw NPE"); |
|
220 } |
|
221 |
|
222 @Test |
|
223 public void testIAEs() { |
|
224 assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 1, 0, Integer::max), |
|
225 IllegalArgumentException.class, "should throw IAE"); |
|
226 assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 1, 0, Long::max), |
|
227 IllegalArgumentException.class, "should throw IAE"); |
|
228 assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 1, 0, Double::max), |
|
229 IllegalArgumentException.class, "should throw IAE"); |
|
230 assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 1, 0, String::concat), |
|
231 IllegalArgumentException.class, "should throw IAE"); |
|
232 } |
|
233 |
|
234 @Test |
|
235 public void testAIOBEs() { |
|
236 // bad "fromIndex" |
|
237 assertThrows( () -> Arrays.parallelPrefix(new int[] {}, -1, 0, Integer::max), |
|
238 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
239 assertThrows( () -> Arrays.parallelPrefix(new long[] {}, -1, 0, Long::max), |
|
240 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
241 assertThrows( () -> Arrays.parallelPrefix(new double[] {}, -1, 0, Double::max), |
|
242 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
243 assertThrows( () -> Arrays.parallelPrefix(new String[] {}, -1, 0, String::concat), |
|
244 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
245 |
|
246 // bad "toIndex" |
|
247 assertThrows( () -> Arrays.parallelPrefix(new int[] {}, 0, 1, Integer::max), |
|
248 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
249 assertThrows( () -> Arrays.parallelPrefix(new long[] {}, 0, 1, Long::max), |
|
250 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
251 assertThrows( () -> Arrays.parallelPrefix(new double[] {}, 0, 1, Double::max), |
|
252 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
253 assertThrows( () -> Arrays.parallelPrefix(new String[] {}, 0, 1, String::concat), |
|
254 ArrayIndexOutOfBoundsException.class, "should throw AIOBE"); |
|
255 } |
|
256 |
|
257 // "library" code |
|
258 |
|
259 public interface Thrower<T extends Throwable> { |
|
260 |
|
261 public void run() throws T; |
|
262 } |
|
263 |
|
264 |
|
265 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable) { |
|
266 assertThrows(thrower, throwable, null); |
|
267 } |
|
268 |
|
269 public static <T extends Throwable> void assertThrows(Thrower<T> thrower, Class<T> throwable, String message) { |
|
270 Throwable thrown; |
|
271 try { |
|
272 thrower.run(); |
|
273 thrown = null; |
|
274 } catch (Throwable caught) { |
|
275 thrown = caught; |
|
276 } |
|
277 |
|
278 assertInstance(thrown, throwable, |
|
279 ((null != message) ? message : "") + |
|
280 " Failed to throw " + throwable.getCanonicalName()); |
|
281 } |
|
282 |
|
283 public static <T extends Throwable> void assertThrows(Class<T> throwable, String message, Thrower<T>... throwers) { |
|
284 for(Thrower<T> thrower : throwers) { |
|
285 assertThrows(thrower, throwable, message); |
|
286 } |
|
287 } |
|
288 |
|
289 public static void assertInstance(Object actual, Class<?> expected) { |
|
290 assertInstance(expected.isInstance(actual), null); |
|
291 } |
|
292 |
|
293 public static void assertInstance(Object actual, Class<?> expected, String message) { |
|
294 assertTrue(expected.isInstance(actual), message); |
|
295 } |
164 } |
296 } |
165 |
297 |