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.45 by jsr166, Mon Dec 26 19:54:46 2016 UTC vs.
Revision 1.59 by jsr166, Tue Apr 30 00:50:31 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 559 | Line 565 | public class Collection8Test extends JSR
565      public void testRemoveAfterForEachRemaining() {
566          Collection c = impl.emptyCollection();
567          ThreadLocalRandom rnd = ThreadLocalRandom.current();
568 +        ArrayList copy = new ArrayList();
569 +        boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED);
570          testCollection: {
571              int n = 3 + rnd.nextInt(2);
572 <            for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
572 >            for (int i = 0; i < n; i++) {
573 >                Object x = impl.makeElement(i);
574 >                c.add(x);
575 >                copy.add(x);
576 >            }
577              Iterator it = c.iterator();
578 <            assertTrue(it.hasNext());
579 <            assertEquals(impl.makeElement(0), it.next());
580 <            assertTrue(it.hasNext());
581 <            assertEquals(impl.makeElement(1), it.next());
582 <            it.forEachRemaining(e -> assertTrue(c.contains(e)));
578 >            if (ordered) {
579 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
580 >                assertEquals(impl.makeElement(0), it.next());
581 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
582 >                assertEquals(impl.makeElement(1), it.next());
583 >            } else {
584 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
585 >                assertTrue(copy.contains(it.next()));
586 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
587 >                assertTrue(copy.contains(it.next()));
588 >            }
589 >            if (rnd.nextBoolean()) assertTrue(it.hasNext());
590 >            it.forEachRemaining(
591 >                e -> {
592 >                    assertTrue(c.contains(e));
593 >                    assertTrue(copy.contains(e));});
594              if (testImplementationDetails) {
595                  if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
596                      assertIteratorExhausted(it);
# Line 577 | Line 600 | public class Collection8Test extends JSR
600                          break testCollection;
601                      }
602                      assertEquals(n - 1, c.size());
603 <                    for (int i = 0; i < n - 1; i++)
604 <                        assertTrue(c.contains(impl.makeElement(i)));
605 <                    assertFalse(c.contains(impl.makeElement(n - 1)));
603 >                    if (ordered) {
604 >                        for (int i = 0; i < n - 1; i++)
605 >                            assertTrue(c.contains(impl.makeElement(i)));
606 >                        assertFalse(c.contains(impl.makeElement(n - 1)));
607 >                    }
608                  }
609              }
610          }
611          if (c instanceof Deque) {
612              Deque d = (Deque) impl.emptyCollection();
613 +            assertTrue(ordered);
614              int n = 3 + rnd.nextInt(2);
615              for (int i = 0; i < n; i++) d.add(impl.makeElement(i));
616              Iterator it = d.descendingIterator();
# Line 877 | Line 903 | public class Collection8Test extends JSR
903          }
904      }
905  
906 +    public void testCollectionCopies() throws Exception {
907 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
908 +        Collection c = impl.emptyCollection();
909 +        for (int n = rnd.nextInt(4); n--> 0; )
910 +            c.add(impl.makeElement(rnd.nextInt()));
911 +        assertEquals(c, c);
912 +        if (c instanceof List)
913 +            assertCollectionsEquals(c, new ArrayList(c));
914 +        else if (c instanceof Set)
915 +            assertCollectionsEquals(c, new HashSet(c));
916 +        else if (c instanceof Deque)
917 +            assertCollectionsEquivalent(c, new ArrayDeque(c));
918 +
919 +        Collection clone = cloneableClone(c);
920 +        if (clone != null) {
921 +            assertSame(c.getClass(), clone.getClass());
922 +            assertCollectionsEquivalent(c, clone);
923 +        }
924 +        try {
925 +            Collection serialClone = serialClonePossiblyFailing(c);
926 +            assertSame(c.getClass(), serialClone.getClass());
927 +            assertCollectionsEquivalent(c, serialClone);
928 +        } catch (java.io.NotSerializableException acceptable) {}
929 +    }
930 +
931 +    /**
932 +     * TODO: move out of limbo
933 +     * 8203662: remove increment of modCount from ArrayList and Vector replaceAll()
934 +     */
935 +    public void DISABLED_testReplaceAllIsNotStructuralModification() {
936 +        Collection c = impl.emptyCollection();
937 +        if (!(c instanceof List))
938 +            return;
939 +        List list = (List) c;
940 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
941 +        for (int n = rnd.nextInt(2, 10); n--> 0; )
942 +            list.add(impl.makeElement(rnd.nextInt()));
943 +        ArrayList copy = new ArrayList(list);
944 +        int size = list.size(), half = size / 2;
945 +        Iterator it = list.iterator();
946 +        for (int i = 0; i < half; i++)
947 +            assertEquals(it.next(), copy.get(i));
948 +        list.replaceAll(n -> n);
949 +        // ConcurrentModificationException must not be thrown here.
950 +        for (int i = half; i < size; i++)
951 +            assertEquals(it.next(), copy.get(i));
952 +    }
953 +
954   //     public void testCollection8DebugFail() {
955   //         fail(impl.klazz().getSimpleName());
956   //     }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines