--- jsr166/src/test/tck/CollectionTest.java 2016/10/17 02:47:09 1.5 +++ jsr166/src/test/tck/CollectionTest.java 2016/10/23 22:11:25 1.8 @@ -5,11 +5,15 @@ * http://creativecommons.org/publicdomain/zero/1.0/ */ +import java.util.ArrayList; import java.util.Collection; import java.util.Deque; import java.util.NoSuchElementException; import java.util.Queue; +import java.util.concurrent.ThreadLocalRandom; +import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; +import java.util.function.Predicate; import junit.framework.Test; @@ -41,11 +45,26 @@ public class CollectionTest extends JSR1 assertTrue(c.isEmpty()); assertEquals(0, c.size()); assertEquals("[]", c.toString()); - assertEquals(0, c.toArray().length); + { + Object[] a = c.toArray(); + assertEquals(0, a.length); + assertSame(Object[].class, a.getClass()); + } { Object[] a = new Object[0]; assertSame(a, c.toArray(a)); } + { + Integer[] a = new Integer[0]; + assertSame(a, c.toArray(a)); + } + { + Integer[] a = { 1, 2, 3}; + assertSame(a, c.toArray(a)); + assertNull(a[0]); + assertSame(2, a[1]); + assertSame(3, a[2]); + } assertIteratorExhausted(c.iterator()); Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; c.forEach(alwaysThrows); @@ -75,7 +94,8 @@ public class CollectionTest extends JSR1 () -> c.containsAll(null), () -> c.retainAll(null), () -> c.removeAll(null), - () -> c.removeIf(null)); + () -> c.removeIf(null), + () -> c.toArray(null)); if (!impl.permitsNulls()) { assertThrows( @@ -128,6 +148,49 @@ public class CollectionTest extends JSR1 } } + public void testRemoveIf() { + Collection c = impl.emptyCollection(); + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + int n = rnd.nextInt(6); + for (int i = 0; i < n; i++) c.add(impl.makeElement(i)); + AtomicReference threwAt = new AtomicReference(null); + ArrayList survivors = new ArrayList(c); + ArrayList accepts = new ArrayList(); + ArrayList rejects = new ArrayList(); + Predicate randomPredicate = (e) -> { + assertNull(threwAt.get()); + switch (rnd.nextInt(3)) { + case 0: accepts.add(e); return true; + case 1: rejects.add(e); return false; + case 2: threwAt.set(e); throw new ArithmeticException(); + default: throw new AssertionError(); + } + }; + try { + boolean modified = c.removeIf(randomPredicate); + if (!modified) { + assertNull(threwAt.get()); + assertEquals(n, rejects.size()); + assertEquals(0, accepts.size()); + } + } catch (ArithmeticException ok) {} + survivors.removeAll(accepts); + if (n - accepts.size() != c.size()) { + System.err.println(impl.klazz()); + System.err.println(c); + System.err.println(accepts); + System.err.println(rejects); + System.err.println(survivors); + System.err.println(threwAt.get()); + } + assertEquals(n - accepts.size(), c.size()); + assertTrue(c.containsAll(survivors)); + assertTrue(survivors.containsAll(rejects)); + for (Object x : accepts) assertFalse(c.contains(x)); + if (threwAt.get() == null) + assertEquals(accepts.size() + rejects.size(), n); + } + // public void testCollectionDebugFail() { // fail(impl.klazz().getSimpleName()); // }