--- jsr166/src/test/tck/Collection8Test.java 2016/10/31 20:08:40 1.7 +++ jsr166/src/test/tck/Collection8Test.java 2016/11/06 03:35:25 1.14 @@ -17,6 +17,8 @@ import java.util.List; import java.util.NoSuchElementException; import java.util.Queue; import java.util.Spliterator; +import java.util.concurrent.BlockingDeque; +import java.util.concurrent.BlockingQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; @@ -49,9 +51,27 @@ public class Collection8Test extends JSR impl); } + Object bomb() { + return new Object() { + public boolean equals(Object x) { throw new AssertionError(); } + public int hashCode() { throw new AssertionError(); } + }; + } + /** Checks properties of empty collections. */ - public void testEmptyMeansEmpty() { + public void testEmptyMeansEmpty() throws InterruptedException { Collection c = impl.emptyCollection(); + emptyMeansEmpty(c); + + if (c instanceof java.io.Serializable) + emptyMeansEmpty(serialClone(c)); + + Collection clone = cloneableClone(c); + if (clone != null) + emptyMeansEmpty(clone); + } + + void emptyMeansEmpty(Collection c) throws InterruptedException { assertTrue(c.isEmpty()); assertEquals(0, c.size()); assertEquals("[]", c.toString()); @@ -81,22 +101,38 @@ public class Collection8Test extends JSR c.iterator().forEachRemaining(alwaysThrows); c.spliterator().forEachRemaining(alwaysThrows); assertFalse(c.spliterator().tryAdvance(alwaysThrows)); - if (Queue.class.isAssignableFrom(impl.klazz())) { + if (c.spliterator().hasCharacteristics(Spliterator.SIZED)) + assertEquals(0, c.spliterator().estimateSize()); + assertFalse(c.contains(bomb())); + assertFalse(c.remove(bomb())); + if (c instanceof Queue) { Queue q = (Queue) c; assertNull(q.peek()); assertNull(q.poll()); } - if (Deque.class.isAssignableFrom(impl.klazz())) { + if (c instanceof Deque) { Deque d = (Deque) c; assertNull(d.peekFirst()); assertNull(d.peekLast()); assertNull(d.pollFirst()); assertNull(d.pollLast()); assertIteratorExhausted(d.descendingIterator()); + d.descendingIterator().forEachRemaining(alwaysThrows); + assertFalse(d.removeFirstOccurrence(bomb())); + assertFalse(d.removeLastOccurrence(bomb())); + } + if (c instanceof BlockingQueue) { + BlockingQueue q = (BlockingQueue) c; + assertNull(q.poll(0L, MILLISECONDS)); + } + if (c instanceof BlockingDeque) { + BlockingDeque q = (BlockingDeque) c; + assertNull(q.pollFirst(0L, MILLISECONDS)); + assertNull(q.pollLast(0L, MILLISECONDS)); } } - public void testNullPointerExceptions() { + public void testNullPointerExceptions() throws InterruptedException { Collection c = impl.emptyCollection(); assertThrows( NullPointerException.class, @@ -116,15 +152,13 @@ public class Collection8Test extends JSR NullPointerException.class, () -> c.add(null)); } - if (!impl.permitsNulls() - && Queue.class.isAssignableFrom(impl.klazz())) { + if (!impl.permitsNulls() && c instanceof Queue) { Queue q = (Queue) c; assertThrows( NullPointerException.class, () -> q.offer(null)); } - if (!impl.permitsNulls() - && Deque.class.isAssignableFrom(impl.klazz())) { + if (!impl.permitsNulls() && c instanceof Deque) { Deque d = (Deque) c; assertThrows( NullPointerException.class, @@ -135,6 +169,31 @@ public class Collection8Test extends JSR () -> d.push(null), () -> d.descendingIterator().forEachRemaining(null)); } + if (!impl.permitsNulls() && c instanceof BlockingQueue) { + BlockingQueue q = (BlockingQueue) c; + assertThrows( + NullPointerException.class, + () -> { + try { q.offer(null, 1L, MILLISECONDS); } + catch (InterruptedException ex) { + throw new AssertionError(ex); + }}); + } + if (!impl.permitsNulls() && c instanceof BlockingDeque) { + BlockingDeque q = (BlockingDeque) c; + assertThrows( + NullPointerException.class, + () -> { + try { q.offerFirst(null, 1L, MILLISECONDS); } + catch (InterruptedException ex) { + throw new AssertionError(ex); + }}, + () -> { + try { q.offerLast(null, 1L, MILLISECONDS); } + catch (InterruptedException ex) { + throw new AssertionError(ex); + }}); + } } public void testNoSuchElementExceptions() { @@ -143,14 +202,14 @@ public class Collection8Test extends JSR NoSuchElementException.class, () -> c.iterator().next()); - if (Queue.class.isAssignableFrom(impl.klazz())) { + if (c instanceof Queue) { Queue q = (Queue) c; assertThrows( NoSuchElementException.class, () -> q.element(), () -> q.remove()); } - if (Deque.class.isAssignableFrom(impl.klazz())) { + if (c instanceof Deque) { Deque d = (Deque) c; assertThrows( NoSuchElementException.class, @@ -182,6 +241,7 @@ public class Collection8Test extends JSR } }; try { + assertFalse(survivors.contains(null)); try { boolean modified = c.removeIf(randomPredicate); if (!modified) { @@ -203,7 +263,7 @@ public class Collection8Test extends JSR System.err.printf("n=%d%n", n); System.err.printf("accepts=%s%n", accepts); System.err.printf("rejects=%s%n", rejects); - System.err.println(survivors); + System.err.printf("survivors=%s%n", survivors); System.err.printf("threw=%s%n", threwAt.get()); throw ex; } @@ -219,26 +279,35 @@ public class Collection8Test extends JSR for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); ArrayList iterated = new ArrayList(); ArrayList iteratedForEachRemaining = new ArrayList(); + ArrayList tryAdvanced = new ArrayList(); ArrayList spliterated = new ArrayList(); - ArrayList foreached = new ArrayList(); + ArrayList forEached = new ArrayList(); + ArrayList removeIfed = new ArrayList(); for (Object x : c) iterated.add(x); c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); + for (Spliterator s = c.spliterator(); + s.tryAdvance(e -> tryAdvanced.add(e)); ) {} c.spliterator().forEachRemaining(e -> spliterated.add(e)); - c.forEach(e -> foreached.add(e)); + c.forEach(e -> forEached.add(e)); + c.removeIf(e -> { removeIfed.add(e); return false; }); boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED); if (c instanceof List || c instanceof Deque) assertTrue(ordered); if (ordered) { assertEquals(iterated, iteratedForEachRemaining); + assertEquals(iterated, tryAdvanced); assertEquals(iterated, spliterated); - assertEquals(iterated, foreached); + assertEquals(iterated, forEached); + assertEquals(iterated, removeIfed); } else { HashSet cset = new HashSet(c); assertEquals(cset, new HashSet(iterated)); assertEquals(cset, new HashSet(iteratedForEachRemaining)); + assertEquals(cset, new HashSet(tryAdvanced)); assertEquals(cset, new HashSet(spliterated)); - assertEquals(cset, new HashSet(foreached)); + assertEquals(cset, new HashSet(forEached)); + assertEquals(cset, new HashSet(removeIfed)); } if (c instanceof Deque) { Deque d = (Deque) c;