--- jsr166/src/test/tck/Collection8Test.java 2016/11/06 18:51:53 1.17 +++ jsr166/src/test/tck/Collection8Test.java 2016/11/06 22:42:10 1.19 @@ -9,6 +9,7 @@ import static java.util.concurrent.TimeU import static java.util.concurrent.TimeUnit.MILLISECONDS; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Deque; @@ -97,7 +98,7 @@ public class Collection8Test extends JSR assertSame(3, a[2]); } assertIteratorExhausted(c.iterator()); - Consumer alwaysThrows = (e) -> { throw new AssertionError(); }; + Consumer alwaysThrows = e -> { throw new AssertionError(); }; c.forEach(alwaysThrows); c.iterator().forEachRemaining(alwaysThrows); c.spliterator().forEachRemaining(alwaysThrows); @@ -244,10 +245,18 @@ public class Collection8Test extends JSR 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); + List orig = rnd.nextBoolean() + ? new ArrayList(c) + : Arrays.asList(c.toArray()); + + // Merely creating an iterator can change ArrayBlockingQueue behavior + Iterator it = rnd.nextBoolean() ? c.iterator() : null; + + ArrayList survivors = new ArrayList(); ArrayList accepts = new ArrayList(); ArrayList rejects = new ArrayList(); - Predicate randomPredicate = (e) -> { + + Predicate randomPredicate = e -> { assertNull(threwAt.get()); switch (rnd.nextInt(3)) { case 0: accepts.add(e); return true; @@ -259,27 +268,42 @@ public class Collection8Test extends JSR try { 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); - assertEquals(n - accepts.size(), c.size()); + assertNull(threwAt.get()); + assertEquals(modified, accepts.size() > 0); + assertEquals(modified, rejects.size() != n); + assertEquals(accepts.size() + rejects.size(), n); + assertEquals(rejects, Arrays.asList(c.toArray())); + } catch (ArithmeticException ok) { + assertNotNull(threwAt.get()); + assertTrue(c.contains(threwAt.get())); + } + if (it != null && impl.isConcurrent()) + // check for weakly consistent iterator + while (it.hasNext()) assertTrue(orig.contains(it.next())); + switch (rnd.nextInt(4)) { + case 0: survivors.addAll(c); break; + case 1: survivors.addAll(Arrays.asList(c.toArray())); break; + case 2: c.forEach(e -> survivors.add(e)); break; + case 3: for (Object e : c) survivors.add(e); break; + } + assertTrue(orig.containsAll(accepts)); + assertTrue(orig.containsAll(rejects)); + assertTrue(orig.containsAll(survivors)); + assertTrue(orig.containsAll(c)); + assertTrue(c.containsAll(rejects)); assertTrue(c.containsAll(survivors)); assertTrue(survivors.containsAll(rejects)); + assertEquals(n - accepts.size(), c.size()); for (Object x : accepts) assertFalse(c.contains(x)); - if (threwAt.get() == null) - assertEquals(accepts.size() + rejects.size(), n); } catch (Throwable ex) { System.err.println(impl.klazz()); System.err.printf("c=%s%n", c); System.err.printf("n=%d%n", n); + System.err.printf("orig=%s%n", orig); System.err.printf("accepts=%s%n", accepts); System.err.printf("rejects=%s%n", rejects); System.err.printf("survivors=%s%n", survivors); - System.err.printf("threw=%s%n", threwAt.get()); + System.err.printf("threwAt=%s%n", threwAt.get()); throw ex; } } @@ -354,7 +378,7 @@ public class Collection8Test extends JSR assertEquals(impl.makeElement(0), it.next()); assertTrue(it.hasNext()); assertEquals(impl.makeElement(1), it.next()); - it.forEachRemaining((e) -> {}); + it.forEachRemaining(e -> {}); it.remove(); assertEquals(n - 1, c.size()); for (int i = 0; i < n - 1; i++) @@ -370,7 +394,7 @@ public class Collection8Test extends JSR assertEquals(impl.makeElement(n - 1), it.next()); assertTrue(it.hasNext()); assertEquals(impl.makeElement(n - 2), it.next()); - it.forEachRemaining((e) -> {}); + it.forEachRemaining(e -> {}); it.remove(); assertEquals(n - 1, d.size()); for (int i = 1; i < n; i++) @@ -388,7 +412,7 @@ public class Collection8Test extends JSR final Object x = impl.makeElement(1); final Object y = impl.makeElement(2); final ArrayList found = new ArrayList(); - Consumer spy = (o) -> { found.add(o); }; + Consumer spy = o -> { found.add(o); }; c.stream().forEach(spy); assertTrue(found.isEmpty()); @@ -422,7 +446,7 @@ public class Collection8Test extends JSR Runnable checkElt = () -> { threadsStarted.countDown(); while (!done.get()) - c.stream().forEach((x) -> { assertSame(x, elt); }); }; + c.stream().forEach(x -> { assertSame(x, elt); }); }; Runnable addRemove = () -> { threadsStarted.countDown(); while (!done.get()) { @@ -446,7 +470,7 @@ public class Collection8Test extends JSR final Object x = impl.makeElement(1); final Object y = impl.makeElement(2); final ArrayList found = new ArrayList(); - Consumer spy = (o) -> { found.add(o); }; + Consumer spy = o -> { found.add(o); }; c.forEach(spy); assertTrue(found.isEmpty()); @@ -480,7 +504,7 @@ public class Collection8Test extends JSR Runnable checkElt = () -> { threadsStarted.countDown(); while (!done.get()) - c.forEach((x) -> { assertSame(x, elt); }); }; + c.forEach(x -> { assertSame(x, elt); }); }; Runnable addRemove = () -> { threadsStarted.countDown(); while (!done.get()) {