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.53 by jsr166, Sun May 6 22:09:42 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 588 | Line 589 | public class Collection8Test extends JSR
589      public void testRemoveAfterForEachRemaining() {
590          Collection c = impl.emptyCollection();
591          ThreadLocalRandom rnd = ThreadLocalRandom.current();
592 +        ArrayList copy = new ArrayList();
593 +        boolean ordered = c.spliterator().hasCharacteristics(Spliterator.ORDERED);
594          testCollection: {
595              int n = 3 + rnd.nextInt(2);
596 <            for (int i = 0; i < n; i++) c.add(impl.makeElement(i));
596 >            for (int i = 0; i < n; i++) {
597 >                Object x = impl.makeElement(i);
598 >                c.add(x);
599 >                copy.add(x);
600 >            }
601              Iterator it = c.iterator();
602 <            assertTrue(it.hasNext());
603 <            assertEquals(impl.makeElement(0), it.next());
604 <            assertTrue(it.hasNext());
605 <            assertEquals(impl.makeElement(1), it.next());
606 <            it.forEachRemaining(e -> assertTrue(c.contains(e)));
602 >            if (ordered) {
603 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
604 >                assertEquals(impl.makeElement(0), it.next());
605 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
606 >                assertEquals(impl.makeElement(1), it.next());
607 >            } else {
608 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
609 >                assertTrue(copy.contains(it.next()));
610 >                if (rnd.nextBoolean()) assertTrue(it.hasNext());
611 >                assertTrue(copy.contains(it.next()));
612 >            }
613 >            it.forEachRemaining(
614 >                e -> {
615 >                    assertTrue(c.contains(e));
616 >                    assertTrue(copy.contains(e));});
617              if (testImplementationDetails) {
618                  if (c instanceof java.util.concurrent.ArrayBlockingQueue) {
619                      assertIteratorExhausted(it);
# Line 606 | Line 623 | public class Collection8Test extends JSR
623                          break testCollection;
624                      }
625                      assertEquals(n - 1, c.size());
626 <                    for (int i = 0; i < n - 1; i++)
627 <                        assertTrue(c.contains(impl.makeElement(i)));
628 <                    assertFalse(c.contains(impl.makeElement(n - 1)));
626 >                    if (ordered) {
627 >                        for (int i = 0; i < n - 1; i++)
628 >                            assertTrue(c.contains(impl.makeElement(i)));
629 >                        assertFalse(c.contains(impl.makeElement(n - 1)));
630 >                    }
631                  }
632              }
633          }
634          if (c instanceof Deque) {
635              Deque d = (Deque) impl.emptyCollection();
636 +            assertTrue(ordered);
637              int n = 3 + rnd.nextInt(2);
638              for (int i = 0; i < n; i++) d.add(impl.makeElement(i));
639              Iterator it = d.descendingIterator();
# Line 906 | Line 926 | public class Collection8Test extends JSR
926          }
927      }
928  
929 <    public void testObjectMethods() {
929 >    public void testCollectionCopies() throws Exception {
930          ThreadLocalRandom rnd = ThreadLocalRandom.current();
931          Collection c = impl.emptyCollection();
932 <        for (int n = rnd.nextInt(3); n--> 0; )
932 >        for (int n = rnd.nextInt(4); n--> 0; )
933              c.add(impl.makeElement(rnd.nextInt()));
934          assertEquals(c, c);
935 <        if (c instanceof List) {
936 <            List copy = new ArrayList(c);
937 <            assertEquals(copy, c);
938 <            assertEquals(c, copy);
939 <            assertEquals(copy.hashCode(), c.hashCode());
940 <        }
941 <        if (c instanceof Set) {
942 <            Set copy = new HashSet(c);
943 <            assertEquals(copy, c);
944 <            assertEquals(c, copy);
945 <            assertEquals(copy.hashCode(), c.hashCode());
935 >        if (c instanceof List)
936 >            assertCollectionsEquals(c, new ArrayList(c));
937 >        else if (c instanceof Set)
938 >            assertCollectionsEquals(c, new HashSet(c));
939 >        else if (c instanceof Deque)
940 >            assertCollectionsEquivalent(c, new ArrayDeque(c));
941 >
942 >        Collection clone = cloneableClone(c);
943 >        if (clone != null) {
944 >            assertSame(c.getClass(), clone.getClass());
945 >            assertCollectionsEquivalent(c, clone);
946          }
947 +        try {
948 +            Collection serialClone = serialClonePossiblyFailing(c);
949 +            assertSame(c.getClass(), serialClone.getClass());
950 +            assertCollectionsEquivalent(c, serialClone);
951 +        } catch (java.io.NotSerializableException acceptable) {}
952 +    }
953 +
954 +    public void testReplaceAllIsNotStructuralModification() {
955 +        Collection c = impl.emptyCollection();
956 +        if (!(c instanceof List))
957 +            return;
958 +        List list = (List) c;
959 +        ThreadLocalRandom rnd = ThreadLocalRandom.current();
960 +        for (int n = rnd.nextInt(2, 10); n--> 0; )
961 +            list.add(impl.makeElement(rnd.nextInt()));
962 +        ArrayList copy = new ArrayList(list);
963 +        int size = list.size(), half = size / 2;
964 +        Iterator it = list.iterator();
965 +        for (int i = 0; i < half; i++)
966 +            assertEquals(it.next(), copy.get(i));
967 +        list.replaceAll(n -> n);
968 +        // ConcurrentModificationException must not be thrown here.
969 +        for (int i = half; i < size; i++)
970 +            assertEquals(it.next(), copy.get(i));
971      }
972  
973   //     public void testCollection8DebugFail() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines