--- jsr166/src/test/tck/Collection8Test.java 2018/04/04 03:35:13 1.50 +++ jsr166/src/test/tck/Collection8Test.java 2018/05/05 18:29:53 1.52 @@ -8,6 +8,7 @@ import static java.util.concurrent.TimeUnit.HOURS; import static java.util.concurrent.TimeUnit.MILLISECONDS; +import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -906,24 +907,48 @@ public class Collection8Test extends JSR } } - public void testObjectMethods() { + public void testCollectionCopies() throws Exception { ThreadLocalRandom rnd = ThreadLocalRandom.current(); Collection c = impl.emptyCollection(); - for (int n = rnd.nextInt(3); n--> 0; ) + for (int n = rnd.nextInt(4); n--> 0; ) c.add(impl.makeElement(rnd.nextInt())); assertEquals(c, c); - if (c instanceof List) { - List copy = new ArrayList(c); - assertEquals(copy, c); - assertEquals(c, copy); - assertEquals(copy.hashCode(), c.hashCode()); - } - if (c instanceof Set) { - Set copy = new HashSet(c); - assertEquals(copy, c); - assertEquals(c, copy); - assertEquals(copy.hashCode(), c.hashCode()); + if (c instanceof List) + assertCollectionsEquals(c, new ArrayList(c)); + else if (c instanceof Set) + assertCollectionsEquals(c, new HashSet(c)); + else if (c instanceof Deque) + assertCollectionsEquivalent(c, new ArrayDeque(c)); + + Collection clone = cloneableClone(c); + if (clone != null) { + assertSame(c.getClass(), clone.getClass()); + assertCollectionsEquivalent(c, clone); } + try { + Collection serialClone = serialClonePossiblyFailing(c); + assertSame(c.getClass(), serialClone.getClass()); + assertCollectionsEquivalent(c, serialClone); + } catch (java.io.NotSerializableException acceptable) {} + } + + public void testReplaceAllIsNotStructuralModification() { + Collection c = impl.emptyCollection(); + if (!(c instanceof List)) + return; + List list = (List) c; + ThreadLocalRandom rnd = ThreadLocalRandom.current(); + for (int n = rnd.nextInt(2, 10); n--> 0; ) + list.add(impl.makeElement(rnd.nextInt())); + ArrayList copy = new ArrayList(list); + int size = list.size(), half = size / 2; + Iterator it = list.iterator(); + for (int i = 0; i < half; i++) + assertEquals(it.next(), copy.get(i)); + list.replaceAll(n -> n); + // ConcurrentModificationException must not be thrown here. + for (int i = half; i < size; i++) + assertEquals(it.next(), copy.get(i)); } // public void testCollection8DebugFail() {