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.42 by jsr166, Mon Dec 12 20:49:34 2016 UTC vs.
Revision 1.51 by jsr166, Thu Apr 5 03:36:54 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;
# Line 19 | Line 20 | 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;
# Line 59 | 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. */
# Line 92 | 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 140 | 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 252 | 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 541 | Line 571 | public class Collection8Test extends JSR
571          } catch (ConcurrentModificationException ex) {
572              r1 = ConcurrentModificationException.class;
573              assertFalse(impl.isConcurrent());
544        } catch (UnsupportedOperationException ex) {
545            r1 = UnsupportedOperationException.class;
574          }
575          try {
576              it2.forEachRemaining(iteratedForEachRemaining::add);
577              r2 = iteratedForEachRemaining;
578          } catch (ConcurrentModificationException ex) {
579              r2 = ConcurrentModificationException.class;
580 <        } catch (UnsupportedOperationException ex) {
553 <            r2 = UnsupportedOperationException.class;
580 >            assertFalse(impl.isConcurrent());
581          }
555        if (!r1.equals(r2)) System.err.println(impl.klazz());
582          assertEquals(r1, r2);
583      }
584  
# Line 731 | Line 757 | public class Collection8Test extends JSR
757      }
758  
759      /**
760 +     * Concurrent Spliterators, once exhausted, stay exhausted.
761 +     */
762 +    public void testStickySpliteratorExhaustion() throws Throwable {
763 +        if (!impl.isConcurrent()) return;
764 +        if (!testImplementationDetails) return;
765 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
766 +        final Consumer alwaysThrows = e -> { throw new AssertionError(); };
767 +        final Collection c = impl.emptyCollection();
768 +        final Spliterator s = c.spliterator();
769 +        if (rnd.nextBoolean()) {
770 +            assertFalse(s.tryAdvance(alwaysThrows));
771 +        } else {
772 +            s.forEachRemaining(alwaysThrows);
773 +        }
774 +        final Object one = impl.makeElement(1);
775 +        // Spliterator should not notice added element
776 +        c.add(one);
777 +        if (rnd.nextBoolean()) {
778 +            assertFalse(s.tryAdvance(alwaysThrows));
779 +        } else {
780 +            s.forEachRemaining(alwaysThrows);
781 +        }
782 +    }
783 +
784 +    /**
785       * Motley crew of threads concurrently randomly hammer the collection.
786       */
787      public void testDetectRaces() throws Throwable {
# Line 856 | Line 907 | public class Collection8Test extends JSR
907          }
908      }
909  
910 +    public void testCollectionCopies() throws Exception {
911 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
912 +        Collection c = impl.emptyCollection();
913 +        for (int n = rnd.nextInt(4); n--> 0; )
914 +            c.add(impl.makeElement(rnd.nextInt()));
915 +        assertEquals(c, c);
916 +        if (c instanceof List)
917 +            assertCollectionsEquals(c, new ArrayList(c));
918 +        else if (c instanceof Set)
919 +            assertCollectionsEquals(c, new HashSet(c));
920 +        else if (c instanceof Deque)
921 +            assertCollectionsEquivalent(c, new ArrayDeque(c));
922 +
923 +        Collection clone = cloneableClone(c);
924 +        if (clone != null) {
925 +            assertSame(c.getClass(), clone.getClass());
926 +            assertCollectionsEquivalent(c, clone);
927 +        }
928 +        try {
929 +            Collection serialClone = serialClonePossiblyFailing(c);
930 +            assertSame(c.getClass(), serialClone.getClass());
931 +            assertCollectionsEquivalent(c, serialClone);
932 +        } catch (java.io.NotSerializableException acceptable) {}
933 +    }
934 +
935   //     public void testCollection8DebugFail() {
936   //         fail(impl.klazz().getSimpleName());
937   //     }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines