ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ArrayDequeTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ArrayDequeTest.java (file contents):
Revision 1.39 by jsr166, Mon Oct 10 14:18:06 2016 UTC vs.
Revision 1.52 by jsr166, Sun Oct 30 21:07:27 2016 UTC

# Line 1 | Line 1
1   /*
2 < * Written by Doug Lea with assistance from members of JCP JSR-166
3 < * Expert Group and released to the public domain, as explained at
2 > * Written by Doug Lea and Martin Buchholz with assistance from
3 > * members of JCP JSR-166 Expert Group and released to the public
4 > * domain, as explained at
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8   import java.util.ArrayDeque;
9   import java.util.Arrays;
10   import java.util.Collection;
11 + import java.util.Collections;
12   import java.util.Deque;
13   import java.util.Iterator;
14   import java.util.NoSuchElementException;
15   import java.util.Queue;
16   import java.util.Random;
17 + import java.util.concurrent.ThreadLocalRandom;
18  
19   import junit.framework.Test;
20   import junit.framework.TestSuite;
# Line 22 | Line 25 | public class ArrayDequeTest extends JSR1
25      }
26  
27      public static Test suite() {
28 <        return new TestSuite(ArrayDequeTest.class);
28 >        class Implementation implements CollectionImplementation {
29 >            public Class<?> klazz() { return ArrayDeque.class; }
30 >            public Collection emptyCollection() { return populatedDeque(0); }
31 >            public Object makeElement(int i) { return i; }
32 >            public boolean isConcurrent() { return false; }
33 >            public boolean permitsNulls() { return false; }
34 >        }
35 >        return newTestSuite(ArrayDequeTest.class,
36 >                            CollectionTest.testSuite(new Implementation()));
37      }
38  
39      /**
40       * Returns a new deque of given size containing consecutive
41 <     * Integers 0 ... n.
41 >     * Integers 0 ... n - 1.
42       */
43 <    private ArrayDeque<Integer> populatedDeque(int n) {
44 <        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
43 >    private static ArrayDeque<Integer> populatedDeque(int n) {
44 >        // Randomize various aspects of memory layout, including
45 >        // filled-to-capacity and wraparound.
46 >        final ArrayDeque<Integer> q;
47 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
48 >        switch (rnd.nextInt(6)) {
49 >        case 0: q = new ArrayDeque<Integer>();      break;
50 >        case 1: q = new ArrayDeque<Integer>(0);     break;
51 >        case 2: q = new ArrayDeque<Integer>(1);     break;
52 >        case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break;
53 >        case 4: q = new ArrayDeque<Integer>(n);     break;
54 >        case 5: q = new ArrayDeque<Integer>(n + 1); break;
55 >        default: throw new AssertionError();
56 >        }
57 >        switch (rnd.nextInt(3)) {
58 >        case 0:
59 >            q.addFirst(42);
60 >            assertEquals((Integer) 42, q.removeLast());
61 >            break;
62 >        case 1:
63 >            q.addLast(42);
64 >            assertEquals((Integer) 42, q.removeFirst());
65 >            break;
66 >        case 2: /* do nothing */ break;
67 >        default: throw new AssertionError();
68 >        }
69          assertTrue(q.isEmpty());
70 <        for (int i = 0; i < n; ++i)
71 <            assertTrue(q.offerLast(new Integer(i)));
72 <        assertFalse(q.isEmpty());
70 >        if (rnd.nextBoolean())
71 >            for (int i = 0; i < n; i++)
72 >                assertTrue(q.offerLast((Integer) i));
73 >        else
74 >            for (int i = n; --i >= 0; )
75 >                q.addFirst((Integer) i);
76          assertEquals(n, q.size());
77 +        if (n > 0) {
78 +            assertFalse(q.isEmpty());
79 +            assertEquals((Integer) 0, q.peekFirst());
80 +            assertEquals((Integer) (n - 1), q.peekLast());
81 +        }
82          return q;
83      }
84  
# Line 637 | Line 680 | public class ArrayDequeTest extends JSR1
680          }
681      }
682  
683 <    void checkToArray(ArrayDeque q) {
683 >    void checkToArray(ArrayDeque<Integer> q) {
684          int size = q.size();
685 <        Object[] o = q.toArray();
686 <        assertEquals(size, o.length);
685 >        Object[] a1 = q.toArray();
686 >        assertEquals(size, a1.length);
687 >        Integer[] a2 = q.toArray(new Integer[0]);
688 >        assertEquals(size, a2.length);
689 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
690 >        assertEquals(size, a3.length);
691 >        Integer[] a4 = new Integer[size];
692 >        assertSame(a4, q.toArray(a4));
693 >        Integer[] a5 = new Integer[size + 1];
694 >        Arrays.fill(a5, 42);
695 >        assertSame(a5, q.toArray(a5));
696 >        Integer[] a6 = new Integer[size + 2];
697 >        Arrays.fill(a6, 42);
698 >        assertSame(a6, q.toArray(a6));
699 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
700 >        for (Object[] a : as) {
701 >            if (a.length > size) assertNull(a[size]);
702 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
703 >        }
704          Iterator it = q.iterator();
705 +        Integer s = q.peekFirst();
706          for (int i = 0; i < size; i++) {
707              Integer x = (Integer) it.next();
708 <            assertEquals((Integer)o[0] + i, (int) x);
709 <            assertSame(o[i], x);
708 >            assertEquals(s + i, (int) x);
709 >            for (Object[] a : as)
710 >                assertSame(a1[i], x);
711          }
712      }
713  
714      /**
715 <     * toArray() contains all elements in FIFO order
715 >     * toArray() and toArray(a) contain all elements in FIFO order
716       */
717      public void testToArray() {
718 <        ArrayDeque q = new ArrayDeque();
719 <        for (int i = 0; i < SIZE; i++) {
718 >        final int size = ThreadLocalRandom.current().nextInt(10);
719 >        ArrayDeque<Integer> q = new ArrayDeque<>(size);
720 >        for (int i = 0; i < size; i++) {
721              checkToArray(q);
722              q.addLast(i);
723          }
724          // Provoke wraparound
725 <        for (int i = 0; i < SIZE; i++) {
726 <            checkToArray(q);
664 <            assertEquals(i, q.poll());
665 <            q.addLast(SIZE + i);
666 <        }
667 <        for (int i = 0; i < SIZE; i++) {
725 >        int added = size * 2;
726 >        for (int i = 0; i < added; i++) {
727              checkToArray(q);
728 <            assertEquals(SIZE + i, q.poll());
728 >            assertEquals((Integer) i, q.poll());
729 >            q.addLast(size + i);
730          }
671    }
672
673    void checkToArray2(ArrayDeque q) {
674        int size = q.size();
675        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
676        Integer[] a2 = new Integer[size];
677        Integer[] a3 = new Integer[size + 2];
678        if (size > 0) Arrays.fill(a1, 42);
679        Arrays.fill(a2, 42);
680        Arrays.fill(a3, 42);
681        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
682        Integer[] b2 = (Integer[]) q.toArray(a2);
683        Integer[] b3 = (Integer[]) q.toArray(a3);
684        assertSame(a2, b2);
685        assertSame(a3, b3);
686        Iterator it = q.iterator();
731          for (int i = 0; i < size; i++) {
732 <            Integer x = (Integer) it.next();
733 <            assertSame(b1[i], x);
690 <            assertEquals(b1[0] + i, (int) x);
691 <            assertSame(b2[i], x);
692 <            assertSame(b3[i], x);
693 <        }
694 <        assertNull(a3[size]);
695 <        assertEquals(42, (int) a3[size + 1]);
696 <        if (size > 0) {
697 <            assertNotSame(a1, b1);
698 <            assertEquals(size, b1.length);
699 <            for (int i = 0; i < a1.length; i++) {
700 <                assertEquals(42, (int) a1[i]);
701 <            }
702 <        }
703 <    }
704 <
705 <    /**
706 <     * toArray(a) contains all elements in FIFO order
707 <     */
708 <    public void testToArray2() {
709 <        ArrayDeque q = new ArrayDeque();
710 <        for (int i = 0; i < SIZE; i++) {
711 <            checkToArray2(q);
712 <            q.addLast(i);
713 <        }
714 <        // Provoke wraparound
715 <        for (int i = 0; i < SIZE; i++) {
716 <            checkToArray2(q);
717 <            assertEquals(i, q.poll());
718 <            q.addLast(SIZE + i);
719 <        }
720 <        for (int i = 0; i < SIZE; i++) {
721 <            checkToArray2(q);
722 <            assertEquals(SIZE + i, q.poll());
732 >            checkToArray(q);
733 >            assertEquals((Integer) (added + i), q.poll());
734          }
735      }
736  
# Line 738 | Line 749 | public class ArrayDequeTest extends JSR1
749      /**
750       * toArray(incompatible array type) throws ArrayStoreException
751       */
752 <    public void testToArray1_BadArg() {
752 >    public void testToArray_incompatibleArrayType() {
753          ArrayDeque l = new ArrayDeque();
754          l.add(new Integer(5));
755          try {
756              l.toArray(new String[10]);
757              shouldThrow();
758          } catch (ArrayStoreException success) {}
759 +        try {
760 +            l.toArray(new String[0]);
761 +            shouldThrow();
762 +        } catch (ArrayStoreException success) {}
763      }
764  
765      /**
# Line 901 | Line 916 | public class ArrayDequeTest extends JSR1
916  
917          assertNotSame(y, x);
918          assertEquals(x.size(), y.size());
919 +        assertEquals(x.toString(), y.toString());
920 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
921 +        while (!x.isEmpty()) {
922 +            assertFalse(y.isEmpty());
923 +            assertEquals(x.remove(), y.remove());
924 +        }
925 +        assertTrue(y.isEmpty());
926 +    }
927 +
928 +    /**
929 +     * A cloned deque has same elements in same order
930 +     */
931 +    public void testClone() throws Exception {
932 +        ArrayDeque<Integer> x = populatedDeque(SIZE);
933 +        ArrayDeque<Integer> y = x.clone();
934 +
935 +        assertNotSame(y, x);
936 +        assertEquals(x.size(), y.size());
937          assertEquals(x.toString(), y.toString());
938          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
939          while (!x.isEmpty()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines