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.43 by jsr166, Thu Dec 15 01:21:22 2016 UTC vs.
Revision 1.60 by jsr166, Mon Dec 16 21:22:33 2019 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 162 | Line 182 | public class Collection8Test extends JSR
182              () -> c.iterator().forEachRemaining(null),
183              () -> c.spliterator().forEachRemaining(null),
184              () -> c.spliterator().tryAdvance(null),
185 <            () -> c.toArray(null));
185 >            () -> c.toArray((Object[])null));
186  
187          if (!impl.permitsNulls()) {
188              assertThrows(
# Line 190 | Line 210 | public class Collection8Test extends JSR
210              BlockingQueue q = (BlockingQueue) c;
211              assertThrows(
212                  NullPointerException.class,
213 <                () -> {
214 <                    try { q.offer(null, 1L, HOURS); }
195 <                    catch (InterruptedException ex) {
196 <                        throw new AssertionError(ex);
197 <                    }},
198 <                () -> {
199 <                    try { q.put(null); }
200 <                    catch (InterruptedException ex) {
201 <                        throw new AssertionError(ex);
202 <                    }});
213 >                () -> q.offer(null, 1L, HOURS),
214 >                () -> q.put(null));
215          }
216          if (c instanceof BlockingDeque) {
217              BlockingDeque q = (BlockingDeque) c;
218              assertThrows(
219                  NullPointerException.class,
220 <                () -> {
221 <                    try { q.offerFirst(null, 1L, HOURS); }
222 <                    catch (InterruptedException ex) {
223 <                        throw new AssertionError(ex);
212 <                    }},
213 <                () -> {
214 <                    try { q.offerLast(null, 1L, HOURS); }
215 <                    catch (InterruptedException ex) {
216 <                        throw new AssertionError(ex);
217 <                    }},
218 <                () -> {
219 <                    try { q.putFirst(null); }
220 <                    catch (InterruptedException ex) {
221 <                        throw new AssertionError(ex);
222 <                    }},
223 <                () -> {
224 <                    try { q.putLast(null); }
225 <                    catch (InterruptedException ex) {
226 <                        throw new AssertionError(ex);
227 <                    }});
220 >                () -> q.offerFirst(null, 1L, HOURS),
221 >                () -> q.offerLast(null, 1L, HOURS),
222 >                () -> q.putFirst(null),
223 >                () -> q.putLast(null));
224          }
225      }
226  
# Line 252 | Line 248 | public class Collection8Test extends JSR
248                  () -> d.pop(),
249                  () -> d.descendingIterator().next());
250          }
251 +        if (c instanceof List) {
252 +            List x = (List) c;
253 +            assertThrows(
254 +                NoSuchElementException.class,
255 +                () -> x.iterator().next(),
256 +                () -> x.listIterator().next(),
257 +                () -> x.listIterator(0).next(),
258 +                () -> x.listIterator().previous(),
259 +                () -> x.listIterator(0).previous());
260 +        }
261      }
262  
263      public void testRemoveIf() {
# Line 407 | Line 413 | public class Collection8Test extends JSR
413              if (rnd.nextBoolean()) assertTrue(it.hasNext());
414              it.next();
415          }
410        Consumer alwaysThrows = e -> { throw new AssertionError(); };
416          // TODO: many more removal methods
417          if (rnd.nextBoolean()) {
418              for (Iterator z = c.iterator(); z.hasNext(); ) {
# Line 541 | Line 546 | public class Collection8Test extends JSR
546          } catch (ConcurrentModificationException ex) {
547              r1 = ConcurrentModificationException.class;
548              assertFalse(impl.isConcurrent());
544        } catch (UnsupportedOperationException ex) {
545            r1 = UnsupportedOperationException.class;
549          }
550          try {
551              it2.forEachRemaining(iteratedForEachRemaining::add);
552              r2 = iteratedForEachRemaining;
553          } catch (ConcurrentModificationException ex) {
554              r2 = ConcurrentModificationException.class;
555 <        } catch (UnsupportedOperationException ex) {
553 <            r2 = UnsupportedOperationException.class;
555 >            assertFalse(impl.isConcurrent());
556          }
557          assertEquals(r1, r2);
558      }
# Line 562 | Line 564 | public class Collection8Test extends JSR
564      public void testRemoveAfterForEachRemaining() {
565          Collection c = impl.emptyCollection();
566          ThreadLocalRandom rnd = ThreadLocalRandom.current();
567 +        ArrayList copy = new ArrayList();
568 +        boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED);
569          testCollection: {
570              int n = 3 + rnd.nextInt(2);
571 <            for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
571 >            for (int i = 0; i < n; i++) {
572 >                Object x = impl.makeElement(i);
573 >                c.add(x);
574 >                copy.add(x);
575 >            }
576              Iterator it = c.iterator();
577 <            assertTrue(it.hasNext());
578 <            assertEquals(impl.makeElement(0), it.next());
579 <            assertTrue(it.hasNext());
580 <            assertEquals(impl.makeElement(1), it.next());
581 <            it.forEachRemaining(e -> assertTrue(c.contains(e)));
577 >            if (ordered) {
578 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
579 >                assertEquals(impl.makeElement(0), it.next());
580 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
581 >                assertEquals(impl.makeElement(1), it.next());
582 >            } else {
583 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
584 >                assertTrue(copy.contains(it.next()));
585 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
586 >                assertTrue(copy.contains(it.next()));
587 >            }
588 >            if (rnd.nextBoolean()) assertTrue(it.hasNext());
589 >            it.forEachRemaining(
590 >                e -> {
591 >                    assertTrue(c.contains(e));
592 >                    assertTrue(copy.contains(e));});
593              if (testImplementationDetails) {
594                  if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
595                      assertIteratorExhausted(it);
# Line 580 | Line 599 | public class Collection8Test extends JSR
599                          break testCollection;
600                      }
601                      assertEquals(n - 1, c.size());
602 <                    for (int i = 0; i < n - 1; i++)
603 <                        assertTrue(c.contains(impl.makeElement(i)));
604 <                    assertFalse(c.contains(impl.makeElement(n - 1)));
602 >                    if (ordered) {
603 >                        for (int i = 0; i < n - 1; i++)
604 >                            assertTrue(c.contains(impl.makeElement(i)));
605 >                        assertFalse(c.contains(impl.makeElement(n - 1)));
606 >                    }
607                  }
608              }
609          }
610          if (c instanceof Deque) {
611              Deque d = (Deque) impl.emptyCollection();
612 +            assertTrue(ordered);
613              int n = 3 + rnd.nextInt(2);
614              for (int i = 0; i < n; i++) d.add(impl.makeElement(i));
615              Iterator it = d.descendingIterator();
# Line 611 | Line 633 | public class Collection8Test extends JSR
633       */
634      public void testStreamForEach() throws Throwable {
635          final Collection c = impl.emptyCollection();
614        final AtomicLong count = new AtomicLong(0L);
636          final Object x = impl.makeElement(1);
637          final Object y = impl.makeElement(2);
638          final ArrayList found = new ArrayList();
# Line 669 | Line 690 | public class Collection8Test extends JSR
690       */
691      public void testForEach() throws Throwable {
692          final Collection c = impl.emptyCollection();
672        final AtomicLong count = new AtomicLong(0L);
693          final Object x = impl.makeElement(1);
694          final Object y = impl.makeElement(2);
695          final ArrayList found = new ArrayList();
# Line 730 | Line 750 | public class Collection8Test extends JSR
750      }
751  
752      /**
753 +     * Concurrent Spliterators, once exhausted, stay exhausted.
754 +     */
755 +    public void testStickySpliteratorExhaustion() throws Throwable {
756 +        if (!impl.isConcurrent()) return;
757 +        if (!testImplementationDetails) return;
758 +        final ThreadLocalRandom rnd = ThreadLocalRandom.current();
759 +        final Consumer alwaysThrows = e -> { throw new AssertionError(); };
760 +        final Collection c = impl.emptyCollection();
761 +        final Spliterator s = c.spliterator();
762 +        if (rnd.nextBoolean()) {
763 +            assertFalse(s.tryAdvance(alwaysThrows));
764 +        } else {
765 +            s.forEachRemaining(alwaysThrows);
766 +        }
767 +        final Object one = impl.makeElement(1);
768 +        // Spliterator should not notice added element
769 +        c.add(one);
770 +        if (rnd.nextBoolean()) {
771 +            assertFalse(s.tryAdvance(alwaysThrows));
772 +        } else {
773 +            s.forEachRemaining(alwaysThrows);
774 +        }
775 +    }
776 +
777 +    /**
778       * Motley crew of threads concurrently randomly hammer the collection.
779       */
780      public void testDetectRaces() throws Throwable {
# Line 855 | Line 900 | public class Collection8Test extends JSR
900          }
901      }
902  
903 +    public void testCollectionCopies() throws Exception {
904 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
905 +        Collection c = impl.emptyCollection();
906 +        for (int n = rnd.nextInt(4); n--> 0; )
907 +            c.add(impl.makeElement(rnd.nextInt()));
908 +        assertEquals(c, c);
909 +        if (c instanceof List)
910 +            assertCollectionsEquals(c, new ArrayList(c));
911 +        else if (c instanceof Set)
912 +            assertCollectionsEquals(c, new HashSet(c));
913 +        else if (c instanceof Deque)
914 +            assertCollectionsEquivalent(c, new ArrayDeque(c));
915 +
916 +        Collection clone = cloneableClone(c);
917 +        if (clone != null) {
918 +            assertSame(c.getClass(), clone.getClass());
919 +            assertCollectionsEquivalent(c, clone);
920 +        }
921 +        try {
922 +            Collection serialClone = serialClonePossiblyFailing(c);
923 +            assertSame(c.getClass(), serialClone.getClass());
924 +            assertCollectionsEquivalent(c, serialClone);
925 +        } catch (java.io.NotSerializableException acceptable) {}
926 +    }
927 +
928 +    /**
929 +     * TODO: move out of limbo
930 +     * 8203662: remove increment of modCount from ArrayList and Vector replaceAll()
931 +     */
932 +    public void DISABLED_testReplaceAllIsNotStructuralModification() {
933 +        Collection c = impl.emptyCollection();
934 +        if (!(c instanceof List))
935 +            return;
936 +        List list = (List) c;
937 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
938 +        for (int n = rnd.nextInt(2, 10); n--> 0; )
939 +            list.add(impl.makeElement(rnd.nextInt()));
940 +        ArrayList copy = new ArrayList(list);
941 +        int size = list.size(), half = size / 2;
942 +        Iterator it = list.iterator();
943 +        for (int i = 0; i < half; i++)
944 +            assertEquals(it.next(), copy.get(i));
945 +        list.replaceAll(n -> n);
946 +        // ConcurrentModificationException must not be thrown here.
947 +        for (int i = half; i < size; i++)
948 +            assertEquals(it.next(), copy.get(i));
949 +    }
950 +
951   //     public void testCollection8DebugFail() {
952   //         fail(impl.klazz().getSimpleName());
953   //     }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines