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.41 by jsr166, Mon Dec 12 03:50:15 2016 UTC vs.
Revision 1.48 by jsr166, Sat Mar 24 14:46:18 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;
# Line 58 | 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 91 | 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 +        }
105 +        else if (c instanceof Set<?>) {
106 +            assertEquals(0, c.hashCode());
107 +            assertEquals(c, Collections.emptySet());
108 +            assertEquals(Collections.emptySet(), c);
109 +        }
110          {
111              Object[] a = c.toArray();
112              assertEquals(0, a.length);
# Line 139 | Line 155 | public class Collection8Test extends JSR
155          }
156          if (c instanceof BlockingQueue) {
157              BlockingQueue q = (BlockingQueue) c;
158 <            assertNull(q.poll(0L, MILLISECONDS));
158 >            assertNull(q.poll(randomExpiredTimeout(), randomTimeUnit()));
159          }
160          if (c instanceof BlockingDeque) {
161              BlockingDeque q = (BlockingDeque) c;
162 <            assertNull(q.pollFirst(0L, MILLISECONDS));
163 <            assertNull(q.pollLast(0L, MILLISECONDS));
162 >            assertNull(q.pollFirst(randomExpiredTimeout(), randomTimeUnit()));
163 >            assertNull(q.pollLast(randomExpiredTimeout(), randomTimeUnit()));
164          }
165      }
166  
# Line 251 | Line 267 | public class Collection8Test extends JSR
267                  () -> d.pop(),
268                  () -> d.descendingIterator().next());
269          }
270 +        if (c instanceof List) {
271 +            List x = (List) c;
272 +            assertThrows(
273 +                NoSuchElementException.class,
274 +                () -> x.iterator().next(),
275 +                () -> x.listIterator().next(),
276 +                () -> x.listIterator(0).next(),
277 +                () -> x.listIterator().previous(),
278 +                () -> x.listIterator(0).previous());
279 +        }
280      }
281  
282      public void testRemoveIf() {
# Line 515 | Line 541 | public class Collection8Test extends JSR
541      }
542  
543      /**
544 +     * Iterator.forEachRemaining has same behavior as Iterator's
545 +     * default implementation.
546 +     */
547 +    public void testForEachRemainingConsistentWithDefaultImplementation() {
548 +        Collection c = impl.emptyCollection();
549 +        if (!testImplementationDetails
550 +            || c.getClass() == java.util.LinkedList.class)
551 +            return;
552 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
553 +        int n = 1 + rnd.nextInt(3);
554 +        for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
555 +        ArrayList iterated = new ArrayList();
556 +        ArrayList iteratedForEachRemaining = new ArrayList();
557 +        Iterator it1 = c.iterator();
558 +        Iterator it2 = c.iterator();
559 +        assertTrue(it1.hasNext());
560 +        assertTrue(it2.hasNext());
561 +        c.clear();
562 +        Object r1, r2;
563 +        try {
564 +            while (it1.hasNext()) iterated.add(it1.next());
565 +            r1 = iterated;
566 +        } catch (ConcurrentModificationException ex) {
567 +            r1 = ConcurrentModificationException.class;
568 +            assertFalse(impl.isConcurrent());
569 +        }
570 +        try {
571 +            it2.forEachRemaining(iteratedForEachRemaining::add);
572 +            r2 = iteratedForEachRemaining;
573 +        } catch (ConcurrentModificationException ex) {
574 +            r2 = ConcurrentModificationException.class;
575 +            assertFalse(impl.isConcurrent());
576 +        }
577 +        assertEquals(r1, r2);
578 +    }
579 +
580 +    /**
581       * Calling Iterator#remove() after Iterator#forEachRemaining
582       * should (maybe) remove last element
583       */
# Line 689 | Line 752 | public class Collection8Test extends JSR
752      }
753  
754      /**
755 +     * Concurrent Spliterators, once exhausted, stay exhausted.
756 +     */
757 +    public void testStickySpliteratorExhaustion() throws Throwable {
758 +        if (!impl.isConcurrent()) return;
759 +        if (!testImplementationDetails) return;
760 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
761 +        final Consumer alwaysThrows = e -> { throw new AssertionError(); };
762 +        final Collection c = impl.emptyCollection();
763 +        final Spliterator s = c.spliterator();
764 +        if (rnd.nextBoolean()) {
765 +            assertFalse(s.tryAdvance(alwaysThrows));
766 +        } else {
767 +            s.forEachRemaining(alwaysThrows);
768 +        }
769 +        final Object one = impl.makeElement(1);
770 +        // Spliterator should not notice added element
771 +        c.add(one);
772 +        if (rnd.nextBoolean()) {
773 +            assertFalse(s.tryAdvance(alwaysThrows));
774 +        } else {
775 +            s.forEachRemaining(alwaysThrows);
776 +        }
777 +    }
778 +
779 +    /**
780       * Motley crew of threads concurrently randomly hammer the collection.
781       */
782      public void testDetectRaces() throws Throwable {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines