ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/Collection8Test.java
(Generate patch)

Comparing jsr166/src/test/tck/Collection8Test.java (file contents):
Revision 1.22 by jsr166, Sun Nov 13 02:10:10 2016 UTC vs.
Revision 1.54 by jsr166, Sun May 6 22:33:06 2018 UTC

# Line 8 | Line 8
8   import static java.util.concurrent.TimeUnit.HOURS;
9   import static java.util.concurrent.TimeUnit.MILLISECONDS;
10  
11 + import java.util.ArrayDeque;
12   import java.util.ArrayList;
13   import java.util.Arrays;
14   import java.util.Collection;
15   import java.util.Collections;
16 + import java.util.ConcurrentModificationException;
17   import java.util.Deque;
18   import java.util.HashSet;
19   import java.util.Iterator;
20   import java.util.List;
21   import java.util.NoSuchElementException;
22   import java.util.Queue;
23 + import java.util.Set;
24   import java.util.Spliterator;
25   import java.util.concurrent.BlockingDeque;
26   import java.util.concurrent.BlockingQueue;
27 + import java.util.concurrent.ConcurrentLinkedQueue;
28   import java.util.concurrent.CountDownLatch;
29   import java.util.concurrent.Executors;
30   import java.util.concurrent.ExecutorService;
31   import java.util.concurrent.Future;
32 + import java.util.concurrent.Phaser;
33   import java.util.concurrent.ThreadLocalRandom;
34   import java.util.concurrent.atomic.AtomicBoolean;
35   import java.util.concurrent.atomic.AtomicLong;
36   import java.util.concurrent.atomic.AtomicReference;
37   import java.util.function.Consumer;
38   import java.util.function.Predicate;
39 + import java.util.stream.Collectors;
40  
41   import junit.framework.Test;
42  
# Line 55 | Line 61 | public class Collection8Test extends JSR
61  
62      Object bomb() {
63          return new Object() {
64 <                public boolean equals(Object x) { throw new AssertionError(); }
65 <                public int hashCode() { throw new AssertionError(); }
66 <            };
64 >            @Override public boolean equals(Object x) { throw new AssertionError(); }
65 >            @Override public int hashCode() { throw new AssertionError(); }
66 >            @Override public String toString() { throw new AssertionError(); }
67 >        };
68      }
69  
70      /** Checks properties of empty collections. */
71 <    public void testEmptyMeansEmpty() throws InterruptedException {
71 >    public void testEmptyMeansEmpty() throws Throwable {
72          Collection c = impl.emptyCollection();
73          emptyMeansEmpty(c);
74  
75 <        if (c instanceof java.io.Serializable)
76 <            emptyMeansEmpty(serialClone(c));
75 >        if (c instanceof java.io.Serializable) {
76 >            try {
77 >                emptyMeansEmpty(serialClonePossiblyFailing(c));
78 >            } catch (java.io.NotSerializableException ex) {
79 >                // excusable when we have a serializable wrapper around
80 >                // a non-serializable collection, as can happen with:
81 >                // Vector.subList() => wrapped AbstractList$RandomAccessSubList
82 >                if (testImplementationDetails
83 >                    && (! c.getClass().getName().matches(
84 >                                "java.util.Collections.*")))
85 >                    throw ex;
86 >            }
87 >        }
88  
89          Collection clone = cloneableClone(c);
90          if (clone != null)
# Line 77 | Line 95 | public class Collection8Test extends JSR
95          assertTrue(c.isEmpty());
96          assertEquals(0, c.size());
97          assertEquals("[]", c.toString());
98 +        if (c instanceof List<?>) {
99 +            List x = (List) c;
100 +            assertEquals(1, x.hashCode());
101 +            assertEquals(x, Collections.emptyList());
102 +            assertEquals(Collections.emptyList(), x);
103 +            assertEquals(-1, x.indexOf(impl.makeElement(86)));
104 +            assertEquals(-1, x.lastIndexOf(impl.makeElement(99)));
105 +            assertThrows(
106 +                IndexOutOfBoundsException.class,
107 +                () -> x.get(0),
108 +                () -> x.set(0, impl.makeElement(42)));
109 +        }
110 +        else if (c instanceof Set<?>) {
111 +            assertEquals(0, c.hashCode());
112 +            assertEquals(c, Collections.emptySet());
113 +            assertEquals(Collections.emptySet(), c);
114 +        }
115          {
116              Object[] a = c.toArray();
117              assertEquals(0, a.length);
# Line 125 | Line 160 | public class Collection8Test extends JSR
160          }
161          if (c instanceof BlockingQueue) {
162              BlockingQueue q = (BlockingQueue) c;
163 <            assertNull(q.poll(0L, MILLISECONDS));
163 >            assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
164          }
165          if (c instanceof BlockingDeque) {
166              BlockingDeque q = (BlockingDeque) c;
167 <            assertNull(q.pollFirst(0L, MILLISECONDS));
168 <            assertNull(q.pollLast(0L, MILLISECONDS));
167 >            assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit()));
168 >            assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit()));
169          }
170      }
171  
# Line 237 | Line 272 | public class Collection8Test extends JSR
272                  () -> d.pop(),
273                  () -> d.descendingIterator().next());
274          }
275 +        if (c instanceof List) {
276 +            List x = (List) c;
277 +            assertThrows(
278 +                NoSuchElementException.class,
279 +                () -> x.iterator().next(),
280 +                () -> x.listIterator().next(),
281 +                () -> x.listIterator(0).next(),
282 +                () -> x.listIterator().previous(),
283 +                () -> x.listIterator(0).previous());
284 +        }
285      }
286  
287      public void testRemoveIf() {
# Line 291 | Line 336 | public class Collection8Test extends JSR
336              switch (rnd.nextInt(4)) {
337              case 0: survivors.addAll(c); break;
338              case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
339 <            case 2: c.forEach(e -> survivors.add(e)); break;
339 >            case 2: c.forEach(survivors::add); break;
340              case 3: for (Object e : c) survivors.add(e); break;
341              }
342              assertTrue(orig.containsAll(accepts));
# Line 314 | Line 359 | public class Collection8Test extends JSR
359              }
360          } catch (Throwable ex) {
361              System.err.println(impl.klazz());
362 <            System.err.printf("c=%s%n", c);
362 >            // c is at risk of corruption if we got here, so be lenient
363 >            try { System.err.printf("c=%s%n", c); }
364 >            catch (Throwable t) { t.printStackTrace(); }
365              System.err.printf("n=%d%n", n);
366              System.err.printf("orig=%s%n", orig);
367              System.err.printf("accepts=%s%n", accepts);
# Line 326 | Line 373 | public class Collection8Test extends JSR
373      }
374  
375      /**
376 +     * All elements removed in the middle of CONCURRENT traversal.
377 +     */
378 +    public void testElementRemovalDuringTraversal() {
379 +        Collection c = impl.emptyCollection();
380 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
381 +        int n = rnd.nextInt(6);
382 +        ArrayList copy = new ArrayList();
383 +        for (int i = 0; i < n; i++) {
384 +            Object x = impl.makeElement(i);
385 +            copy.add(x);
386 +            c.add(x);
387 +        }
388 +        ArrayList iterated = new ArrayList();
389 +        ArrayList spliterated = new ArrayList();
390 +        Spliterator s = c.spliterator();
391 +        Iterator it = c.iterator();
392 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
393 +            assertTrue(s.tryAdvance(spliterated::add));
394 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
395 +            iterated.add(it.next());
396 +        }
397 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
398 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
399 +            c.clear();          // TODO: many more removal methods
400 +            if (testImplementationDetails
401 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
402 +                if (rnd.nextBoolean())
403 +                    assertFalse(s.tryAdvance(alwaysThrows));
404 +                else
405 +                    s.forEachRemaining(alwaysThrows);
406 +            }
407 +            if (it.hasNext()) iterated.add(it.next());
408 +            if (rnd.nextBoolean()) assertIteratorExhausted(it);
409 +        }
410 +        assertTrue(copy.containsAll(iterated));
411 +        assertTrue(copy.containsAll(spliterated));
412 +    }
413 +
414 +    /**
415 +     * Some elements randomly disappear in the middle of traversal.
416 +     */
417 +    public void testRandomElementRemovalDuringTraversal() {
418 +        Collection c = impl.emptyCollection();
419 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
420 +        int n = rnd.nextInt(6);
421 +        ArrayList copy = new ArrayList();
422 +        for (int i = 0; i < n; i++) {
423 +            Object x = impl.makeElement(i);
424 +            copy.add(x);
425 +            c.add(x);
426 +        }
427 +        ArrayList iterated = new ArrayList();
428 +        ArrayList spliterated = new ArrayList();
429 +        ArrayList removed = new ArrayList();
430 +        Spliterator s = c.spliterator();
431 +        Iterator it = c.iterator();
432 +        if (! (s.hasCharacteristics(Spliterator.CONCURRENT) ||
433 +               s.hasCharacteristics(Spliterator.IMMUTABLE)))
434 +            return;
435 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
436 +            assertTrue(s.tryAdvance(e -> {}));
437 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
438 +            it.next();
439 +        }
440 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
441 +        // TODO: many more removal methods
442 +        if (rnd.nextBoolean()) {
443 +            for (Iterator z = c.iterator(); z.hasNext(); ) {
444 +                Object e = z.next();
445 +                if (rnd.nextBoolean()) {
446 +                    try {
447 +                        z.remove();
448 +                    } catch (UnsupportedOperationException ok) { return; }
449 +                    removed.add(e);
450 +                }
451 +            }
452 +        } else {
453 +            Predicate randomlyRemove = e -> {
454 +                if (rnd.nextBoolean()) { removed.add(e); return true; }
455 +                else return false;
456 +            };
457 +            c.removeIf(randomlyRemove);
458 +        }
459 +        s.forEachRemaining(spliterated::add);
460 +        while (it.hasNext())
461 +            iterated.add(it.next());
462 +        assertTrue(copy.containsAll(iterated));
463 +        assertTrue(copy.containsAll(spliterated));
464 +        assertTrue(copy.containsAll(removed));
465 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
466 +            ArrayList iteratedAndRemoved = new ArrayList(iterated);
467 +            ArrayList spliteratedAndRemoved = new ArrayList(spliterated);
468 +            iteratedAndRemoved.retainAll(removed);
469 +            spliteratedAndRemoved.retainAll(removed);
470 +            assertTrue(iteratedAndRemoved.size() <= 1);
471 +            assertTrue(spliteratedAndRemoved.size() <= 1);
472 +            if (testImplementationDetails
473 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue))
474 +                assertTrue(spliteratedAndRemoved.isEmpty());
475 +        }
476 +    }
477 +
478 +    /**
479       * Various ways of traversing a collection yield same elements
480       */
481 <    public void testIteratorEquivalence() {
481 >    public void testTraversalEquivalence() {
482          Collection c = impl.emptyCollection();
483          ThreadLocalRandom rnd = ThreadLocalRandom.current();
484          int n = rnd.nextInt(6);
# Line 337 | Line 487 | public class Collection8Test extends JSR
487          ArrayList iteratedForEachRemaining = new ArrayList();
488          ArrayList tryAdvanced = new ArrayList();
489          ArrayList spliterated = new ArrayList();
490 +        ArrayList splitonced = new ArrayList();
491          ArrayList forEached = new ArrayList();
492 +        ArrayList streamForEached = new ArrayList();
493 +        ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue();
494          ArrayList removeIfed = new ArrayList();
495          for (Object x : c) iterated.add(x);
496 <        c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e));
496 >        c.iterator().forEachRemaining(iteratedForEachRemaining::add);
497          for (Spliterator s = c.spliterator();
498 <             s.tryAdvance(e -> tryAdvanced.add(e)); ) {}
499 <        c.spliterator().forEachRemaining(e -> spliterated.add(e));
500 <        c.forEach(e -> forEached.add(e));
498 >             s.tryAdvance(tryAdvanced::add); ) {}
499 >        c.spliterator().forEachRemaining(spliterated::add);
500 >        {                       // trySplit returns "strict prefix"
501 >            Spliterator s1 = c.spliterator(), s2 = s1.trySplit();
502 >            if (s2 != null) s2.forEachRemaining(splitonced::add);
503 >            s1.forEachRemaining(splitonced::add);
504 >        }
505 >        c.forEach(forEached::add);
506 >        c.stream().forEach(streamForEached::add);
507 >        c.parallelStream().forEach(parallelStreamForEached::add);
508          c.removeIf(e -> { removeIfed.add(e); return false; });
509          boolean ordered =
510              c.spliterator().hasCharacteristics(Spliterator.ORDERED);
511          if (c instanceof List || c instanceof Deque)
512              assertTrue(ordered);
513 +        HashSet cset = new HashSet(c);
514 +        assertEquals(cset, new HashSet(parallelStreamForEached));
515          if (ordered) {
516              assertEquals(iterated, iteratedForEachRemaining);
517              assertEquals(iterated, tryAdvanced);
518              assertEquals(iterated, spliterated);
519 +            assertEquals(iterated, splitonced);
520              assertEquals(iterated, forEached);
521 +            assertEquals(iterated, streamForEached);
522              assertEquals(iterated, removeIfed);
523          } else {
360            HashSet cset = new HashSet(c);
524              assertEquals(cset, new HashSet(iterated));
525              assertEquals(cset, new HashSet(iteratedForEachRemaining));
526              assertEquals(cset, new HashSet(tryAdvanced));
527              assertEquals(cset, new HashSet(spliterated));
528 +            assertEquals(cset, new HashSet(splitonced));
529              assertEquals(cset, new HashSet(forEached));
530 +            assertEquals(cset, new HashSet(streamForEached));
531              assertEquals(cset, new HashSet(removeIfed));
532          }
533          if (c instanceof Deque) {
# Line 381 | Line 546 | public class Collection8Test extends JSR
546      }
547  
548      /**
549 +     * Iterator.forEachRemaining has same behavior as Iterator's
550 +     * default implementation.
551 +     */
552 +    public void testForEachRemainingConsistentWithDefaultImplementation() {
553 +        Collection c = impl.emptyCollection();
554 +        if (!testImplementationDetails
555 +            || c.getClass() == java.util.LinkedList.class)
556 +            return;
557 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
558 +        int n = 1 + rnd.nextInt(3);
559 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
560 +        ArrayList iterated = new ArrayList();
561 +        ArrayList iteratedForEachRemaining = new ArrayList();
562 +        Iterator it1 = c.iterator();
563 +        Iterator it2 = c.iterator();
564 +        assertTrue(it1.hasNext());
565 +        assertTrue(it2.hasNext());
566 +        c.clear();
567 +        Object r1, r2;
568 +        try {
569 +            while (it1.hasNext()) iterated.add(it1.next());
570 +            r1 = iterated;
571 +        } catch (ConcurrentModificationException ex) {
572 +            r1 = ConcurrentModificationException.class;
573 +            assertFalse(impl.isConcurrent());
574 +        }
575 +        try {
576 +            it2.forEachRemaining(iteratedForEachRemaining::add);
577 +            r2 = iteratedForEachRemaining;
578 +        } catch (ConcurrentModificationException ex) {
579 +            r2 = ConcurrentModificationException.class;
580 +            assertFalse(impl.isConcurrent());
581 +        }
582 +        assertEquals(r1, r2);
583 +    }
584 +
585 +    /**
586       * Calling Iterator#remove() after Iterator#forEachRemaining
587       * should (maybe) remove last element
588       */
589      public void testRemoveAfterForEachRemaining() {
590          Collection c = impl.emptyCollection();
591          ThreadLocalRandom rnd = ThreadLocalRandom.current();
592 <        {
592 >        ArrayList copy = new ArrayList();
593 >        boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED);
594 >        testCollection: {
595              int n = 3 + rnd.nextInt(2);
596 <            for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
596 >            for (int i = 0; i < n; i++) {
597 >                Object x = impl.makeElement(i);
598 >                c.add(x);
599 >                copy.add(x);
600 >            }
601              Iterator it = c.iterator();
602 <            assertTrue(it.hasNext());
603 <            assertEquals(impl.makeElement(0), it.next());
604 <            assertTrue(it.hasNext());
605 <            assertEquals(impl.makeElement(1), it.next());
606 <            it.forEachRemaining(e -> assertTrue(c.contains(e)));
602 >            if (ordered) {
603 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
604 >                assertEquals(impl.makeElement(0), it.next());
605 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
606 >                assertEquals(impl.makeElement(1), it.next());
607 >            } else {
608 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
609 >                assertTrue(copy.contains(it.next()));
610 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
611 >                assertTrue(copy.contains(it.next()));
612 >            }
613 >            if (rnd.nextBoolean()) assertTrue(it.hasNext());
614 >            it.forEachRemaining(
615 >                e -> {
616 >                    assertTrue(c.contains(e));
617 >                    assertTrue(copy.contains(e));});
618              if (testImplementationDetails) {
619                  if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
620                      assertIteratorExhausted(it);
621                  } else {
622 <                    it.remove();
622 >                    try { it.remove(); }
623 >                    catch (UnsupportedOperationException ok) {
624 >                        break testCollection;
625 >                    }
626                      assertEquals(n - 1, c.size());
627 <                    for (int i = 0; i < n - 1; i++)
628 <                        assertTrue(c.contains(impl.makeElement(i)));
629 <                    assertFalse(c.contains(impl.makeElement(n - 1)));
627 >                    if (ordered) {
628 >                        for (int i = 0; i < n - 1; i++)
629 >                            assertTrue(c.contains(impl.makeElement(i)));
630 >                        assertFalse(c.contains(impl.makeElement(n - 1)));
631 >                    }
632                  }
633              }
634          }
635          if (c instanceof Deque) {
636              Deque d = (Deque) impl.emptyCollection();
637 +            assertTrue(ordered);
638              int n = 3 + rnd.nextInt(2);
639              for (int i = 0; i < n; i++) d.add(impl.makeElement(i));
640              Iterator it = d.descendingIterator();
# Line 516 | Line 741 | public class Collection8Test extends JSR
741          assertTrue(found.isEmpty());
742      }
743  
744 <    public void testForEachConcurrentStressTest() throws Throwable {
744 >    /** TODO: promote to a common utility */
745 >    static <T> T chooseOne(T ... ts) {
746 >        return ts[ThreadLocalRandom.current().nextInt(ts.length)];
747 >    }
748 >
749 >    /** TODO: more random adders and removers */
750 >    static <E> Runnable adderRemover(Collection<E> c, E e) {
751 >        return chooseOne(
752 >            () -> {
753 >                assertTrue(c.add(e));
754 >                assertTrue(c.contains(e));
755 >                assertTrue(c.remove(e));
756 >                assertFalse(c.contains(e));
757 >            },
758 >            () -> {
759 >                assertTrue(c.add(e));
760 >                assertTrue(c.contains(e));
761 >                assertTrue(c.removeIf(x -> x == e));
762 >                assertFalse(c.contains(e));
763 >            },
764 >            () -> {
765 >                assertTrue(c.add(e));
766 >                assertTrue(c.contains(e));
767 >                for (Iterator it = c.iterator();; )
768 >                    if (it.next() == e) {
769 >                        try { it.remove(); }
770 >                        catch (UnsupportedOperationException ok) {
771 >                            c.remove(e);
772 >                        }
773 >                        break;
774 >                    }
775 >                assertFalse(c.contains(e));
776 >            });
777 >    }
778 >
779 >    /**
780 >     * Concurrent Spliterators, once exhausted, stay exhausted.
781 >     */
782 >    public void testStickySpliteratorExhaustion() throws Throwable {
783          if (!impl.isConcurrent()) return;
784 +        if (!testImplementationDetails) return;
785 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
786 +        final Consumer alwaysThrows = e -> { throw new AssertionError(); };
787          final Collection c = impl.emptyCollection();
788 <        final long testDurationMillis = timeoutMillis();
788 >        final Spliterator s = c.spliterator();
789 >        if (rnd.nextBoolean()) {
790 >            assertFalse(s.tryAdvance(alwaysThrows));
791 >        } else {
792 >            s.forEachRemaining(alwaysThrows);
793 >        }
794 >        final Object one = impl.makeElement(1);
795 >        // Spliterator should not notice added element
796 >        c.add(one);
797 >        if (rnd.nextBoolean()) {
798 >            assertFalse(s.tryAdvance(alwaysThrows));
799 >        } else {
800 >            s.forEachRemaining(alwaysThrows);
801 >        }
802 >    }
803 >
804 >    /**
805 >     * Motley crew of threads concurrently randomly hammer the collection.
806 >     */
807 >    public void testDetectRaces() throws Throwable {
808 >        if (!impl.isConcurrent()) return;
809 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
810 >        final Collection c = impl.emptyCollection();
811 >        final long testDurationMillis
812 >            = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
813          final AtomicBoolean done = new AtomicBoolean(false);
814 <        final Object elt = impl.makeElement(1);
815 <        final Future<?> f1, f2;
814 >        final Object one = impl.makeElement(1);
815 >        final Object two = impl.makeElement(2);
816 >        final Consumer checkSanity = x -> assertTrue(x == one || x == two);
817 >        final Consumer<Object[]> checkArraySanity = array -> {
818 >            // assertTrue(array.length <= 2); // duplicates are permitted
819 >            for (Object x : array) assertTrue(x == one || x == two);
820 >        };
821 >        final Object[] emptyArray =
822 >            (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
823 >        final List<Future<?>> futures;
824 >        final Phaser threadsStarted = new Phaser(1); // register this thread
825 >        final Runnable[] frobbers = {
826 >            () -> c.forEach(checkSanity),
827 >            () -> c.stream().forEach(checkSanity),
828 >            () -> c.parallelStream().forEach(checkSanity),
829 >            () -> c.spliterator().trySplit(),
830 >            () -> {
831 >                Spliterator s = c.spliterator();
832 >                s.tryAdvance(checkSanity);
833 >                s.trySplit();
834 >            },
835 >            () -> {
836 >                Spliterator s = c.spliterator();
837 >                do {} while (s.tryAdvance(checkSanity));
838 >            },
839 >            () -> { for (Object x : c) checkSanity.accept(x); },
840 >            () -> checkArraySanity.accept(c.toArray()),
841 >            () -> checkArraySanity.accept(c.toArray(emptyArray)),
842 >            () -> {
843 >                Object[] a = new Object[5];
844 >                Object three = impl.makeElement(3);
845 >                Arrays.fill(a, 0, a.length, three);
846 >                Object[] x = c.toArray(a);
847 >                if (x == a)
848 >                    for (int i = 0; i < a.length && a[i] != null; i++)
849 >                        checkSanity.accept(a[i]);
850 >                    // A careful reading of the spec does not support:
851 >                    // for (i++; i < a.length; i++) assertSame(three, a[i]);
852 >                else
853 >                    checkArraySanity.accept(x);
854 >                },
855 >            adderRemover(c, one),
856 >            adderRemover(c, two),
857 >        };
858 >        final List<Runnable> tasks =
859 >            Arrays.stream(frobbers)
860 >            .filter(task -> rnd.nextBoolean()) // random subset
861 >            .map(task -> (Runnable) () -> {
862 >                     threadsStarted.arriveAndAwaitAdvance();
863 >                     while (!done.get())
864 >                         task.run();
865 >                 })
866 >            .collect(Collectors.toList());
867          final ExecutorService pool = Executors.newCachedThreadPool();
868          try (PoolCleaner cleaner = cleaner(pool, done)) {
869 <            final CountDownLatch threadsStarted = new CountDownLatch(2);
870 <            Runnable checkElt = () -> {
871 <                threadsStarted.countDown();
872 <                while (!done.get())
873 <                    c.forEach(x -> assertSame(x, elt)); };
533 <            Runnable addRemove = () -> {
534 <                threadsStarted.countDown();
535 <                while (!done.get()) {
536 <                    assertTrue(c.add(elt));
537 <                    assertTrue(c.remove(elt));
538 <                }};
539 <            f1 = pool.submit(checkElt);
540 <            f2 = pool.submit(addRemove);
869 >            threadsStarted.bulkRegister(tasks.size());
870 >            futures = tasks.stream()
871 >                .map(pool::submit)
872 >                .collect(Collectors.toList());
873 >            threadsStarted.arriveAndDeregister();
874              Thread.sleep(testDurationMillis);
875          }
876 <        assertNull(f1.get(0L, MILLISECONDS));
877 <        assertNull(f2.get(0L, MILLISECONDS));
876 >        for (Future future : futures)
877 >            assertNull(future.get(0L, MILLISECONDS));
878 >    }
879 >
880 >    /**
881 >     * Spliterators are either IMMUTABLE or truly late-binding or, if
882 >     * concurrent, use the same "late-binding style" of returning
883 >     * elements added between creation and first use.
884 >     */
885 >    public void testLateBindingStyle() {
886 >        if (!testImplementationDetails) return;
887 >        if (impl.klazz() == ArrayList.class) return; // for jdk8
888 >        // Immutable (snapshot) spliterators are exempt
889 >        if (impl.emptyCollection().spliterator()
890 >            .hasCharacteristics(Spliterator.IMMUTABLE))
891 >            return;
892 >        final Object one = impl.makeElement(1);
893 >        {
894 >            final Collection c = impl.emptyCollection();
895 >            final Spliterator split = c.spliterator();
896 >            c.add(one);
897 >            assertTrue(split.tryAdvance(e -> { assertSame(e, one); }));
898 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
899 >            assertTrue(c.contains(one));
900 >        }
901 >        {
902 >            final AtomicLong count = new AtomicLong(0);
903 >            final Collection c = impl.emptyCollection();
904 >            final Spliterator split = c.spliterator();
905 >            c.add(one);
906 >            split.forEachRemaining(
907 >                e -> { assertSame(e, one); count.getAndIncrement(); });
908 >            assertEquals(1L, count.get());
909 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
910 >            assertTrue(c.contains(one));
911 >        }
912 >    }
913 >
914 >    /**
915 >     * Spliterator.getComparator throws IllegalStateException iff the
916 >     * spliterator does not report SORTED.
917 >     */
918 >    public void testGetComparator_IllegalStateException() {
919 >        Collection c = impl.emptyCollection();
920 >        Spliterator s = c.spliterator();
921 >        boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED);
922 >        try {
923 >            s.getComparator();
924 >            assertTrue(reportsSorted);
925 >        } catch (IllegalStateException ex) {
926 >            assertFalse(reportsSorted);
927 >        }
928 >    }
929 >
930 >    public void testCollectionCopies() throws Exception {
931 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
932 >        Collection c = impl.emptyCollection();
933 >        for (int n = rnd.nextInt(4); n--> 0; )
934 >            c.add(impl.makeElement(rnd.nextInt()));
935 >        assertEquals(c, c);
936 >        if (c instanceof List)
937 >            assertCollectionsEquals(c, new ArrayList(c));
938 >        else if (c instanceof Set)
939 >            assertCollectionsEquals(c, new HashSet(c));
940 >        else if (c instanceof Deque)
941 >            assertCollectionsEquivalent(c, new ArrayDeque(c));
942 >
943 >        Collection clone = cloneableClone(c);
944 >        if (clone != null) {
945 >            assertSame(c.getClass(), clone.getClass());
946 >            assertCollectionsEquivalent(c, clone);
947 >        }
948 >        try {
949 >            Collection serialClone = serialClonePossiblyFailing(c);
950 >            assertSame(c.getClass(), serialClone.getClass());
951 >            assertCollectionsEquivalent(c, serialClone);
952 >        } catch (java.io.NotSerializableException acceptable) {}
953 >    }
954 >
955 >    public void testReplaceAllIsNotStructuralModification() {
956 >        Collection c = impl.emptyCollection();
957 >        if (!(c instanceof List))
958 >            return;
959 >        List list = (List) c;
960 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
961 >        for (int n = rnd.nextInt(2, 10); n--> 0; )
962 >            list.add(impl.makeElement(rnd.nextInt()));
963 >        ArrayList copy = new ArrayList(list);
964 >        int size = list.size(), half = size / 2;
965 >        Iterator it = list.iterator();
966 >        for (int i = 0; i < half; i++)
967 >            assertEquals(it.next(), copy.get(i));
968 >        list.replaceAll(n -> n);
969 >        // ConcurrentModificationException must not be thrown here.
970 >        for (int i = half; i < size; i++)
971 >            assertEquals(it.next(), copy.get(i));
972      }
973  
974   //     public void testCollection8DebugFail() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines