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.50 by jsr166, Wed Apr 4 03:35:13 2018 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 181 | 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 209 | 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); }
214 <                    catch (InterruptedException ex) {
215 <                        throw new AssertionError(ex);
216 <                    }},
217 <                () -> {
218 <                    try { q.put(null); }
219 <                    catch (InterruptedException ex) {
220 <                        throw new AssertionError(ex);
221 <                    }});
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);
231 <                    }},
232 <                () -> {
233 <                    try { q.offerLast(null, 1L, HOURS); }
234 <                    catch (InterruptedException ex) {
235 <                        throw new AssertionError(ex);
236 <                    }},
237 <                () -> {
238 <                    try { q.putFirst(null); }
239 <                    catch (InterruptedException ex) {
240 <                        throw new AssertionError(ex);
241 <                    }},
242 <                () -> {
243 <                    try { q.putLast(null); }
244 <                    catch (InterruptedException ex) {
245 <                        throw new AssertionError(ex);
246 <                    }});
220 >                () -> q.offerFirst(null, 1L, HOURS),
221 >                () -> q.offerLast(null, 1L, HOURS),
222 >                () -> q.putFirst(null),
223 >                () -> q.putLast(null));
224          }
225      }
226  
# Line 588 | 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 606 | 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 906 | Line 903 | public class Collection8Test extends JSR
903          }
904      }
905  
906 <    public void testObjectMethods() {
906 >    public void testCollectionCopies() throws Exception {
907          ThreadLocalRandom rnd = ThreadLocalRandom.current();
908          Collection c = impl.emptyCollection();
909 <        for (int n = rnd.nextInt(3); n--> 0; )
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 <            List copy = new ArrayList(c);
914 <            assertEquals(copy, c);
915 <            assertEquals(c, copy);
916 <            assertEquals(copy.hashCode(), c.hashCode());
917 <        }
918 <        if (c instanceof Set) {
919 <            Set copy = new HashSet(c);
920 <            assertEquals(copy, c);
921 <            assertEquals(c, copy);
922 <            assertEquals(copy.hashCode(), c.hashCode());
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() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines