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.25 by jsr166, Tue Nov 15 00:08:25 2016 UTC vs.
Revision 1.49 by jsr166, Mon Mar 26 21:06:51 2018 UTC

# Line 12 | Line 12 | import java.util.ArrayList;
12   import java.util.Arrays;
13   import java.util.Collection;
14   import java.util.Collections;
15 + import java.util.ConcurrentModificationException;
16   import java.util.Deque;
17   import java.util.HashSet;
18   import java.util.Iterator;
19   import java.util.List;
20   import java.util.NoSuchElementException;
21   import java.util.Queue;
22 + import java.util.Set;
23   import java.util.Spliterator;
24   import java.util.concurrent.BlockingDeque;
25   import java.util.concurrent.BlockingQueue;
26 + import java.util.concurrent.ConcurrentLinkedQueue;
27   import java.util.concurrent.CountDownLatch;
28   import java.util.concurrent.Executors;
29   import java.util.concurrent.ExecutorService;
30   import java.util.concurrent.Future;
31 + import java.util.concurrent.Phaser;
32   import java.util.concurrent.ThreadLocalRandom;
33   import java.util.concurrent.atomic.AtomicBoolean;
34   import java.util.concurrent.atomic.AtomicLong;
35   import java.util.concurrent.atomic.AtomicReference;
36   import java.util.function.Consumer;
37   import java.util.function.Predicate;
38 + import java.util.stream.Collectors;
39  
40   import junit.framework.Test;
41  
# Line 55 | Line 60 | public class Collection8Test extends JSR
60  
61      Object bomb() {
62          return new Object() {
63 <                public boolean equals(Object x) { throw new AssertionError(); }
64 <                public int hashCode() { throw new AssertionError(); }
65 <            };
63 >            @Override public boolean equals(Object x) { throw new AssertionError(); }
64 >            @Override public int hashCode() { throw new AssertionError(); }
65 >            @Override public String toString() { throw new AssertionError(); }
66 >        };
67      }
68  
69      /** Checks properties of empty collections. */
# Line 88 | Line 94 | public class Collection8Test extends JSR
94          assertTrue(c.isEmpty());
95          assertEquals(0, c.size());
96          assertEquals("[]", c.toString());
97 +        if (c instanceof List<?>) {
98 +            List x = (List) c;
99 +            assertEquals(1, x.hashCode());
100 +            assertEquals(x, Collections.emptyList());
101 +            assertEquals(Collections.emptyList(), x);
102 +            assertEquals(-1, x.indexOf(impl.makeElement(86)));
103 +            assertEquals(-1, x.lastIndexOf(impl.makeElement(99)));
104 +            assertThrows(
105 +                IndexOutOfBoundsException.class,
106 +                () -> x.get(0),
107 +                () -> x.set(0, impl.makeElement(42)));
108 +        }
109 +        else if (c instanceof Set<?>) {
110 +            assertEquals(0, c.hashCode());
111 +            assertEquals(c, Collections.emptySet());
112 +            assertEquals(Collections.emptySet(), c);
113 +        }
114          {
115              Object[] a = c.toArray();
116              assertEquals(0, a.length);
# Line 136 | Line 159 | public class Collection8Test extends JSR
159          }
160          if (c instanceof BlockingQueue) {
161              BlockingQueue q = (BlockingQueue) c;
162 <            assertNull(q.poll(0L, MILLISECONDS));
162 >            assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
163          }
164          if (c instanceof BlockingDeque) {
165              BlockingDeque q = (BlockingDeque) c;
166 <            assertNull(q.pollFirst(0L, MILLISECONDS));
167 <            assertNull(q.pollLast(0L, MILLISECONDS));
166 >            assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit()));
167 >            assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit()));
168          }
169      }
170  
# Line 248 | Line 271 | public class Collection8Test extends JSR
271                  () -> d.pop(),
272                  () -> d.descendingIterator().next());
273          }
274 +        if (c instanceof List) {
275 +            List x = (List) c;
276 +            assertThrows(
277 +                NoSuchElementException.class,
278 +                () -> x.iterator().next(),
279 +                () -> x.listIterator().next(),
280 +                () -> x.listIterator(0).next(),
281 +                () -> x.listIterator().previous(),
282 +                () -> x.listIterator(0).previous());
283 +        }
284      }
285  
286      public void testRemoveIf() {
# Line 302 | Line 335 | public class Collection8Test extends JSR
335              switch (rnd.nextInt(4)) {
336              case 0: survivors.addAll(c); break;
337              case 1: survivors.addAll(Arrays.asList(c.toArray())); break;
338 <            case 2: c.forEach(e -> survivors.add(e)); break;
338 >            case 2: c.forEach(survivors::add); break;
339              case 3: for (Object e : c) survivors.add(e); break;
340              }
341              assertTrue(orig.containsAll(accepts));
# Line 339 | Line 372 | public class Collection8Test extends JSR
372      }
373  
374      /**
375 +     * All elements removed in the middle of CONCURRENT traversal.
376 +     */
377 +    public void testElementRemovalDuringTraversal() {
378 +        Collection c = impl.emptyCollection();
379 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
380 +        int n = rnd.nextInt(6);
381 +        ArrayList copy = new ArrayList();
382 +        for (int i = 0; i < n; i++) {
383 +            Object x = impl.makeElement(i);
384 +            copy.add(x);
385 +            c.add(x);
386 +        }
387 +        ArrayList iterated = new ArrayList();
388 +        ArrayList spliterated = new ArrayList();
389 +        Spliterator s = c.spliterator();
390 +        Iterator it = c.iterator();
391 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
392 +            assertTrue(s.tryAdvance(spliterated::add));
393 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
394 +            iterated.add(it.next());
395 +        }
396 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
397 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
398 +            c.clear();          // TODO: many more removal methods
399 +            if (testImplementationDetails
400 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
401 +                if (rnd.nextBoolean())
402 +                    assertFalse(s.tryAdvance(alwaysThrows));
403 +                else
404 +                    s.forEachRemaining(alwaysThrows);
405 +            }
406 +            if (it.hasNext()) iterated.add(it.next());
407 +            if (rnd.nextBoolean()) assertIteratorExhausted(it);
408 +        }
409 +        assertTrue(copy.containsAll(iterated));
410 +        assertTrue(copy.containsAll(spliterated));
411 +    }
412 +
413 +    /**
414 +     * Some elements randomly disappear in the middle of traversal.
415 +     */
416 +    public void testRandomElementRemovalDuringTraversal() {
417 +        Collection c = impl.emptyCollection();
418 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
419 +        int n = rnd.nextInt(6);
420 +        ArrayList copy = new ArrayList();
421 +        for (int i = 0; i < n; i++) {
422 +            Object x = impl.makeElement(i);
423 +            copy.add(x);
424 +            c.add(x);
425 +        }
426 +        ArrayList iterated = new ArrayList();
427 +        ArrayList spliterated = new ArrayList();
428 +        ArrayList removed = new ArrayList();
429 +        Spliterator s = c.spliterator();
430 +        Iterator it = c.iterator();
431 +        if (! (s.hasCharacteristics(Spliterator.CONCURRENT) ||
432 +               s.hasCharacteristics(Spliterator.IMMUTABLE)))
433 +            return;
434 +        for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
435 +            assertTrue(s.tryAdvance(e -> {}));
436 +            if (rnd.nextBoolean()) assertTrue(it.hasNext());
437 +            it.next();
438 +        }
439 +        Consumer alwaysThrows = e -> { throw new AssertionError(); };
440 +        // TODO: many more removal methods
441 +        if (rnd.nextBoolean()) {
442 +            for (Iterator z = c.iterator(); z.hasNext(); ) {
443 +                Object e = z.next();
444 +                if (rnd.nextBoolean()) {
445 +                    try {
446 +                        z.remove();
447 +                    } catch (UnsupportedOperationException ok) { return; }
448 +                    removed.add(e);
449 +                }
450 +            }
451 +        } else {
452 +            Predicate randomlyRemove = e -> {
453 +                if (rnd.nextBoolean()) { removed.add(e); return true; }
454 +                else return false;
455 +            };
456 +            c.removeIf(randomlyRemove);
457 +        }
458 +        s.forEachRemaining(spliterated::add);
459 +        while (it.hasNext())
460 +            iterated.add(it.next());
461 +        assertTrue(copy.containsAll(iterated));
462 +        assertTrue(copy.containsAll(spliterated));
463 +        assertTrue(copy.containsAll(removed));
464 +        if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
465 +            ArrayList iteratedAndRemoved = new ArrayList(iterated);
466 +            ArrayList spliteratedAndRemoved = new ArrayList(spliterated);
467 +            iteratedAndRemoved.retainAll(removed);
468 +            spliteratedAndRemoved.retainAll(removed);
469 +            assertTrue(iteratedAndRemoved.size() <= 1);
470 +            assertTrue(spliteratedAndRemoved.size() <= 1);
471 +            if (testImplementationDetails
472 +                && !(c instanceof java.util.concurrent.ArrayBlockingQueue))
473 +                assertTrue(spliteratedAndRemoved.isEmpty());
474 +        }
475 +    }
476 +
477 +    /**
478       * Various ways of traversing a collection yield same elements
479       */
480 <    public void testIteratorEquivalence() {
480 >    public void testTraversalEquivalence() {
481          Collection c = impl.emptyCollection();
482          ThreadLocalRandom rnd = ThreadLocalRandom.current();
483          int n = rnd.nextInt(6);
# Line 350 | Line 486 | public class Collection8Test extends JSR
486          ArrayList iteratedForEachRemaining = new ArrayList();
487          ArrayList tryAdvanced = new ArrayList();
488          ArrayList spliterated = new ArrayList();
489 +        ArrayList splitonced = new ArrayList();
490          ArrayList forEached = new ArrayList();
491 +        ArrayList streamForEached = new ArrayList();
492 +        ConcurrentLinkedQueue parallelStreamForEached = new ConcurrentLinkedQueue();
493          ArrayList removeIfed = new ArrayList();
494          for (Object x : c) iterated.add(x);
495 <        c.iterator().forEachRemaining(e -> iteratedForEachRemaining.add(e));
495 >        c.iterator().forEachRemaining(iteratedForEachRemaining::add);
496          for (Spliterator s = c.spliterator();
497 <             s.tryAdvance(e -> tryAdvanced.add(e)); ) {}
498 <        c.spliterator().forEachRemaining(e -> spliterated.add(e));
499 <        c.forEach(e -> forEached.add(e));
497 >             s.tryAdvance(tryAdvanced::add); ) {}
498 >        c.spliterator().forEachRemaining(spliterated::add);
499 >        {                       // trySplit returns "strict prefix"
500 >            Spliterator s1 = c.spliterator(), s2 = s1.trySplit();
501 >            if (s2 != null) s2.forEachRemaining(splitonced::add);
502 >            s1.forEachRemaining(splitonced::add);
503 >        }
504 >        c.forEach(forEached::add);
505 >        c.stream().forEach(streamForEached::add);
506 >        c.parallelStream().forEach(parallelStreamForEached::add);
507          c.removeIf(e -> { removeIfed.add(e); return false; });
508          boolean ordered =
509              c.spliterator().hasCharacteristics(Spliterator.ORDERED);
510          if (c instanceof List || c instanceof Deque)
511              assertTrue(ordered);
512 +        HashSet cset = new HashSet(c);
513 +        assertEquals(cset, new HashSet(parallelStreamForEached));
514          if (ordered) {
515              assertEquals(iterated, iteratedForEachRemaining);
516              assertEquals(iterated, tryAdvanced);
517              assertEquals(iterated, spliterated);
518 +            assertEquals(iterated, splitonced);
519              assertEquals(iterated, forEached);
520 +            assertEquals(iterated, streamForEached);
521              assertEquals(iterated, removeIfed);
522          } else {
373            HashSet cset = new HashSet(c);
523              assertEquals(cset, new HashSet(iterated));
524              assertEquals(cset, new HashSet(iteratedForEachRemaining));
525              assertEquals(cset, new HashSet(tryAdvanced));
526              assertEquals(cset, new HashSet(spliterated));
527 +            assertEquals(cset, new HashSet(splitonced));
528              assertEquals(cset, new HashSet(forEached));
529 +            assertEquals(cset, new HashSet(streamForEached));
530              assertEquals(cset, new HashSet(removeIfed));
531          }
532          if (c instanceof Deque) {
# Line 394 | Line 545 | public class Collection8Test extends JSR
545      }
546  
547      /**
548 +     * Iterator.forEachRemaining has same behavior as Iterator's
549 +     * default implementation.
550 +     */
551 +    public void testForEachRemainingConsistentWithDefaultImplementation() {
552 +        Collection c = impl.emptyCollection();
553 +        if (!testImplementationDetails
554 +            || c.getClass() == java.util.LinkedList.class)
555 +            return;
556 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
557 +        int n = 1 + rnd.nextInt(3);
558 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
559 +        ArrayList iterated = new ArrayList();
560 +        ArrayList iteratedForEachRemaining = new ArrayList();
561 +        Iterator it1 = c.iterator();
562 +        Iterator it2 = c.iterator();
563 +        assertTrue(it1.hasNext());
564 +        assertTrue(it2.hasNext());
565 +        c.clear();
566 +        Object r1, r2;
567 +        try {
568 +            while (it1.hasNext()) iterated.add(it1.next());
569 +            r1 = iterated;
570 +        } catch (ConcurrentModificationException ex) {
571 +            r1 = ConcurrentModificationException.class;
572 +            assertFalse(impl.isConcurrent());
573 +        }
574 +        try {
575 +            it2.forEachRemaining(iteratedForEachRemaining::add);
576 +            r2 = iteratedForEachRemaining;
577 +        } catch (ConcurrentModificationException ex) {
578 +            r2 = ConcurrentModificationException.class;
579 +            assertFalse(impl.isConcurrent());
580 +        }
581 +        assertEquals(r1, r2);
582 +    }
583 +
584 +    /**
585       * Calling Iterator#remove() after Iterator#forEachRemaining
586       * should (maybe) remove last element
587       */
# Line 532 | Line 720 | public class Collection8Test extends JSR
720          assertTrue(found.isEmpty());
721      }
722  
723 <    public void testForEachConcurrentStressTest() throws Throwable {
723 >    /** TODO: promote to a common utility */
724 >    static <T> T chooseOne(T ... ts) {
725 >        return ts[ThreadLocalRandom.current().nextInt(ts.length)];
726 >    }
727 >
728 >    /** TODO: more random adders and removers */
729 >    static <E> Runnable adderRemover(Collection<E> c, E e) {
730 >        return chooseOne(
731 >            () -> {
732 >                assertTrue(c.add(e));
733 >                assertTrue(c.contains(e));
734 >                assertTrue(c.remove(e));
735 >                assertFalse(c.contains(e));
736 >            },
737 >            () -> {
738 >                assertTrue(c.add(e));
739 >                assertTrue(c.contains(e));
740 >                assertTrue(c.removeIf(x -> x == e));
741 >                assertFalse(c.contains(e));
742 >            },
743 >            () -> {
744 >                assertTrue(c.add(e));
745 >                assertTrue(c.contains(e));
746 >                for (Iterator it = c.iterator();; )
747 >                    if (it.next() == e) {
748 >                        try { it.remove(); }
749 >                        catch (UnsupportedOperationException ok) {
750 >                            c.remove(e);
751 >                        }
752 >                        break;
753 >                    }
754 >                assertFalse(c.contains(e));
755 >            });
756 >    }
757 >
758 >    /**
759 >     * Concurrent Spliterators, once exhausted, stay exhausted.
760 >     */
761 >    public void testStickySpliteratorExhaustion() throws Throwable {
762          if (!impl.isConcurrent()) return;
763 +        if (!testImplementationDetails) return;
764 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
765 +        final Consumer alwaysThrows = e -> { throw new AssertionError(); };
766          final Collection c = impl.emptyCollection();
767 <        final long testDurationMillis = timeoutMillis();
767 >        final Spliterator s = c.spliterator();
768 >        if (rnd.nextBoolean()) {
769 >            assertFalse(s.tryAdvance(alwaysThrows));
770 >        } else {
771 >            s.forEachRemaining(alwaysThrows);
772 >        }
773 >        final Object one = impl.makeElement(1);
774 >        // Spliterator should not notice added element
775 >        c.add(one);
776 >        if (rnd.nextBoolean()) {
777 >            assertFalse(s.tryAdvance(alwaysThrows));
778 >        } else {
779 >            s.forEachRemaining(alwaysThrows);
780 >        }
781 >    }
782 >
783 >    /**
784 >     * Motley crew of threads concurrently randomly hammer the collection.
785 >     */
786 >    public void testDetectRaces() throws Throwable {
787 >        if (!impl.isConcurrent()) return;
788 >        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
789 >        final Collection c = impl.emptyCollection();
790 >        final long testDurationMillis
791 >            = expensiveTests ? LONG_DELAY_MS : timeoutMillis();
792          final AtomicBoolean done = new AtomicBoolean(false);
793 <        final Object elt = impl.makeElement(1);
794 <        final Future<?> f1, f2;
793 >        final Object one = impl.makeElement(1);
794 >        final Object two = impl.makeElement(2);
795 >        final Consumer checkSanity = x -> assertTrue(x == one || x == two);
796 >        final Consumer<Object[]> checkArraySanity = array -> {
797 >            // assertTrue(array.length <= 2); // duplicates are permitted
798 >            for (Object x : array) assertTrue(x == one || x == two);
799 >        };
800 >        final Object[] emptyArray =
801 >            (Object[]) java.lang.reflect.Array.newInstance(one.getClass(), 0);
802 >        final List<Future<?>> futures;
803 >        final Phaser threadsStarted = new Phaser(1); // register this thread
804 >        final Runnable[] frobbers = {
805 >            () -> c.forEach(checkSanity),
806 >            () -> c.stream().forEach(checkSanity),
807 >            () -> c.parallelStream().forEach(checkSanity),
808 >            () -> c.spliterator().trySplit(),
809 >            () -> {
810 >                Spliterator s = c.spliterator();
811 >                s.tryAdvance(checkSanity);
812 >                s.trySplit();
813 >            },
814 >            () -> {
815 >                Spliterator s = c.spliterator();
816 >                do {} while (s.tryAdvance(checkSanity));
817 >            },
818 >            () -> { for (Object x : c) checkSanity.accept(x); },
819 >            () -> checkArraySanity.accept(c.toArray()),
820 >            () -> checkArraySanity.accept(c.toArray(emptyArray)),
821 >            () -> {
822 >                Object[] a = new Object[5];
823 >                Object three = impl.makeElement(3);
824 >                Arrays.fill(a, 0, a.length, three);
825 >                Object[] x = c.toArray(a);
826 >                if (x == a)
827 >                    for (int i = 0; i < a.length && a[i] != null; i++)
828 >                        checkSanity.accept(a[i]);
829 >                    // A careful reading of the spec does not support:
830 >                    // for (i++; i < a.length; i++) assertSame(three, a[i]);
831 >                else
832 >                    checkArraySanity.accept(x);
833 >                },
834 >            adderRemover(c, one),
835 >            adderRemover(c, two),
836 >        };
837 >        final List<Runnable> tasks =
838 >            Arrays.stream(frobbers)
839 >            .filter(task -> rnd.nextBoolean()) // random subset
840 >            .map(task -> (Runnable) () -> {
841 >                     threadsStarted.arriveAndAwaitAdvance();
842 >                     while (!done.get())
843 >                         task.run();
844 >                 })
845 >            .collect(Collectors.toList());
846          final ExecutorService pool = Executors.newCachedThreadPool();
847          try (PoolCleaner cleaner = cleaner(pool, done)) {
848 <            final CountDownLatch threadsStarted = new CountDownLatch(2);
849 <            Runnable checkElt = () -> {
850 <                threadsStarted.countDown();
851 <                while (!done.get())
852 <                    c.forEach(x -> assertSame(x, elt)); };
549 <            Runnable addRemove = () -> {
550 <                threadsStarted.countDown();
551 <                while (!done.get()) {
552 <                    assertTrue(c.add(elt));
553 <                    assertTrue(c.remove(elt));
554 <                }};
555 <            f1 = pool.submit(checkElt);
556 <            f2 = pool.submit(addRemove);
848 >            threadsStarted.bulkRegister(tasks.size());
849 >            futures = tasks.stream()
850 >                .map(pool::submit)
851 >                .collect(Collectors.toList());
852 >            threadsStarted.arriveAndDeregister();
853              Thread.sleep(testDurationMillis);
854          }
855 <        assertNull(f1.get(0L, MILLISECONDS));
856 <        assertNull(f2.get(0L, MILLISECONDS));
855 >        for (Future future : futures)
856 >            assertNull(future.get(0L, MILLISECONDS));
857 >    }
858 >
859 >    /**
860 >     * Spliterators are either IMMUTABLE or truly late-binding or, if
861 >     * concurrent, use the same "late-binding style" of returning
862 >     * elements added between creation and first use.
863 >     */
864 >    public void testLateBindingStyle() {
865 >        if (!testImplementationDetails) return;
866 >        if (impl.klazz() == ArrayList.class) return; // for jdk8
867 >        // Immutable (snapshot) spliterators are exempt
868 >        if (impl.emptyCollection().spliterator()
869 >            .hasCharacteristics(Spliterator.IMMUTABLE))
870 >            return;
871 >        final Object one = impl.makeElement(1);
872 >        {
873 >            final Collection c = impl.emptyCollection();
874 >            final Spliterator split = c.spliterator();
875 >            c.add(one);
876 >            assertTrue(split.tryAdvance(e -> { assertSame(e, one); }));
877 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
878 >            assertTrue(c.contains(one));
879 >        }
880 >        {
881 >            final AtomicLong count = new AtomicLong(0);
882 >            final Collection c = impl.emptyCollection();
883 >            final Spliterator split = c.spliterator();
884 >            c.add(one);
885 >            split.forEachRemaining(
886 >                e -> { assertSame(e, one); count.getAndIncrement(); });
887 >            assertEquals(1L, count.get());
888 >            assertFalse(split.tryAdvance(e -> { throw new AssertionError(); }));
889 >            assertTrue(c.contains(one));
890 >        }
891 >    }
892 >
893 >    /**
894 >     * Spliterator.getComparator throws IllegalStateException iff the
895 >     * spliterator does not report SORTED.
896 >     */
897 >    public void testGetComparator_IllegalStateException() {
898 >        Collection c = impl.emptyCollection();
899 >        Spliterator s = c.spliterator();
900 >        boolean reportsSorted = s.hasCharacteristics(Spliterator.SORTED);
901 >        try {
902 >            s.getComparator();
903 >            assertTrue(reportsSorted);
904 >        } catch (IllegalStateException ex) {
905 >            assertFalse(reportsSorted);
906 >        }
907      }
908  
909   //     public void testCollection8DebugFail() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines