24 |
24 |
25 import java.util.function.*; |
25 import java.util.function.*; |
26 import java.util.stream.LambdaTestHelpers; |
26 import java.util.stream.LambdaTestHelpers; |
27 |
27 |
28 import static org.testng.Assert.*; |
28 import static org.testng.Assert.*; |
29 import static org.testng.Assert.assertEquals; |
|
30 import static org.testng.Assert.fail; |
|
31 |
29 |
32 /** |
30 /** |
33 * Assertion methods for spliterators, to be called from other tests |
31 * Assertion methods for spliterators, to be called from other tests |
34 */ |
32 */ |
35 public class SpliteratorTestHelper { |
33 public class SpliteratorTestHelper { |
222 S sp = s.get(); |
220 S sp = s.get(); |
223 // Have to check instances and use casts to avoid tripwire messages and |
221 // Have to check instances and use casts to avoid tripwire messages and |
224 // directly test the primitive methods |
222 // directly test the primitive methods |
225 if (sp instanceof Spliterator.OfInt) { |
223 if (sp instanceof Spliterator.OfInt) { |
226 Spliterator.OfInt psp = (Spliterator.OfInt) sp; |
224 Spliterator.OfInt psp = (Spliterator.OfInt) sp; |
227 executeAndCatch(NullPointerException.class, () -> psp.forEachRemaining((IntConsumer) null)); |
225 assertThrowsNPE(() -> psp.forEachRemaining((IntConsumer) null)); |
228 executeAndCatch(NullPointerException.class, () -> psp.tryAdvance((IntConsumer) null)); |
226 assertThrowsNPE(() -> psp.tryAdvance((IntConsumer) null)); |
229 } |
227 } |
230 else if (sp instanceof Spliterator.OfLong) { |
228 else if (sp instanceof Spliterator.OfLong) { |
231 Spliterator.OfLong psp = (Spliterator.OfLong) sp; |
229 Spliterator.OfLong psp = (Spliterator.OfLong) sp; |
232 executeAndCatch(NullPointerException.class, () -> psp.forEachRemaining((LongConsumer) null)); |
230 assertThrowsNPE(() -> psp.forEachRemaining((LongConsumer) null)); |
233 executeAndCatch(NullPointerException.class, () -> psp.tryAdvance((LongConsumer) null)); |
231 assertThrowsNPE(() -> psp.tryAdvance((LongConsumer) null)); |
234 } |
232 } |
235 else if (sp instanceof Spliterator.OfDouble) { |
233 else if (sp instanceof Spliterator.OfDouble) { |
236 Spliterator.OfDouble psp = (Spliterator.OfDouble) sp; |
234 Spliterator.OfDouble psp = (Spliterator.OfDouble) sp; |
237 executeAndCatch(NullPointerException.class, () -> psp.forEachRemaining((DoubleConsumer) null)); |
235 assertThrowsNPE(() -> psp.forEachRemaining((DoubleConsumer) null)); |
238 executeAndCatch(NullPointerException.class, () -> psp.tryAdvance((DoubleConsumer) null)); |
236 assertThrowsNPE(() -> psp.tryAdvance((DoubleConsumer) null)); |
239 } |
237 } |
240 else { |
238 else { |
241 executeAndCatch(NullPointerException.class, () -> sp.forEachRemaining(null)); |
239 assertThrowsNPE(() -> sp.forEachRemaining(null)); |
242 executeAndCatch(NullPointerException.class, () -> sp.tryAdvance(null)); |
240 assertThrowsNPE(() -> sp.tryAdvance(null)); |
243 } |
241 } |
244 } |
242 } |
245 |
243 |
246 private static <T, S extends Spliterator<T>> void testForEach( |
244 private static <T, S extends Spliterator<T>> void testForEach( |
247 Collection<T> exp, |
245 Collection<T> exp, |
651 else { |
649 else { |
652 LambdaTestHelpers.assertContentsUnordered(actual, expected); |
650 LambdaTestHelpers.assertContentsUnordered(actual, expected); |
653 } |
651 } |
654 } |
652 } |
655 |
653 |
656 public static void executeAndCatch(Class<? extends Exception> expected, Runnable r) { |
654 public static void assertThrowsNPE(ThrowingRunnable r) { |
657 Exception caught = null; |
655 assertThrows(NullPointerException.class, r); |
658 try { |
|
659 r.run(); |
|
660 } |
|
661 catch (Exception e) { |
|
662 caught = e; |
|
663 } |
|
664 |
|
665 assertNotNull(caught, |
|
666 String.format("No Exception was thrown, expected an Exception of %s to be thrown", |
|
667 expected.getName())); |
|
668 assertTrue(expected.isInstance(caught), |
|
669 String.format("Exception thrown %s not an instance of %s", |
|
670 caught.getClass().getName(), expected.getName())); |
|
671 } |
656 } |
672 |
657 |
673 public static<U> void mixedTraverseAndSplit(Consumer<U> b, Spliterator<U> splTop) { |
658 public static<U> void mixedTraverseAndSplit(Consumer<U> b, Spliterator<U> splTop) { |
674 Spliterator<U> spl1, spl2, spl3; |
659 Spliterator<U> spl1, spl2, spl3; |
675 splTop.tryAdvance(b); |
660 splTop.tryAdvance(b); |