--- jsr166/src/test/tck/Collection8Test.java 2016/11/15 23:08:30 1.27 +++ jsr166/src/test/tck/Collection8Test.java 2016/11/23 00:25:16 1.30 @@ -304,7 +304,7 @@ public class Collection8Test extends JSR 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 2: c.forEach(survivors::add); break; case 3: for (Object e : c) survivors.add(e); break; } assertTrue(orig.containsAll(accepts)); @@ -355,11 +355,11 @@ public class Collection8Test extends JSR ArrayList forEached = new ArrayList(); ArrayList removeIfed = new ArrayList(); for (Object x : c) iterated.add(x); - c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e)); + c.iterator().forEachRemaining(iteratedForEachRemaining::add); for (Spliterator s = c.spliterator(); - s.tryAdvance(e -> tryAdvanced.add(e)); ) {} - c.spliterator().forEachRemaining(e -> spliterated.add(e)); - c.forEach(e -> forEached.add(e)); + s.tryAdvance(tryAdvanced::add); ) {} + c.spliterator().forEachRemaining(spliterated::add); + c.forEach(forEached::add); c.removeIf(e -> { removeIfed.add(e); return false; }); boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED); @@ -545,6 +545,8 @@ public class Collection8Test extends JSR final AtomicBoolean done = new AtomicBoolean(false); final Object one = impl.makeElement(1); final Object two = impl.makeElement(2); + final Object[] emptyArray = + (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0); final List> futures; final Phaser threadsStarted = new Phaser(1); // register this thread final Runnable[] frobbers = { @@ -564,6 +566,12 @@ public class Collection8Test extends JSR for (Object x : c) assertTrue(x == one || x == two); }, () -> { + for (Object x : c.toArray()) assertTrue(x == one || x == two); + }, + () -> { + for (Object x : c.toArray(emptyArray)) assertTrue(x == one || x == two); + }, + () -> { assertTrue(c.add(one)); assertTrue(c.contains(one)); assertTrue(c.remove(one)); @@ -589,7 +597,7 @@ public class Collection8Test extends JSR try (PoolCleaner cleaner = cleaner(pool, done)) { threadsStarted.bulkRegister(tasks.size()); futures = tasks.stream() - .map(task -> pool.submit(task)) + .map(pool::submit) .collect(Collectors.toList()); threadsStarted.arriveAndDeregister(); Thread.sleep(testDurationMillis); @@ -598,6 +606,39 @@ public class Collection8Test extends JSR assertNull(future.get(0L, MILLISECONDS)); } + /** + * Spliterators are either IMMUTABLE or truly late-binding or, if + * concurrent, use the same "late-binding style" of returning + * elements added between creation and first use. + */ + public void testLateBindingStyle() { + if (!testImplementationDetails) return; + // Immutable (snapshot) spliterators are exempt + if (impl.emptyCollection().spliterator() + .hasCharacteristics(Spliterator.IMMUTABLE)) + return; + final Object one = impl.makeElement(1); + { + final Collection c = impl.emptyCollection(); + final Spliterator split = c.spliterator(); + c.add(one); + assertTrue(split.tryAdvance(e -> { assertSame(e, one); })); + assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); + assertTrue(c.contains(one)); + } + { + final AtomicLong count = new AtomicLong(0); + final Collection c = impl.emptyCollection(); + final Spliterator split = c.spliterator(); + c.add(one); + split.forEachRemaining( + e -> { assertSame(e, one); count.getAndIncrement(); }); + assertEquals(1L, count.get()); + assertFalse(split.tryAdvance(e -> { throw new AssertionError(); })); + assertTrue(c.contains(one)); + } + } + // public void testCollection8DebugFail() { // fail(impl.klazz().getSimpleName()); // }