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.42 by jsr166, Sun Oct 16 22:13:15 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;
# Line 25 | Line 27 | public class ArrayDequeTest extends JSR1
27      public static Test suite() {
28          class Implementation implements CollectionImplementation {
29              public Class<?> klazz() { return ArrayDeque.class; }
30 <            public Collection emptyCollection() { return new ArrayDeque(); }
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; }
# Line 38 | Line 40 | public class ArrayDequeTest extends JSR1
40       * Returns a new deque of given size containing consecutive
41       * Integers 0 ... n - 1.
42       */
43 <    private ArrayDeque<Integer> populatedDeque(int n) {
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>(n - 1); break;
53 <        case 4: q = new ArrayDeque<Integer>(n); break;
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          }
# Line 59 | Line 61 | public class ArrayDequeTest extends JSR1
61              break;
62          case 1:
63              q.addLast(42);
64 <            assertEquals((Integer) 42, q.removeLast());
64 >            assertEquals((Integer) 42, q.removeFirst());
65              break;
66          case 2: /* do nothing */ break;
67          default: throw new AssertionError();
# Line 71 | Line 73 | public class ArrayDequeTest extends JSR1
73          else
74              for (int i = n; --i >= 0; )
75                  q.addFirst((Integer) i);
74        assertFalse(q.isEmpty());
76          assertEquals(n, q.size());
77 <        assertEquals((Integer) 0, q.peekFirst());
78 <        assertEquals((Integer) (n - 1), q.peekLast());
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 676 | 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++) {
725 >        int added = size * 2;
726 >        for (int i = 0; i < added; i++) {
727              checkToArray(q);
728 <            assertEquals(i, q.poll());
729 <            q.addLast(SIZE + i);
728 >            assertEquals((Integer) i, q.poll());
729 >            q.addLast(size + i);
730          }
706        for (int i = 0; i < SIZE; i++) {
707            checkToArray(q);
708            assertEquals(SIZE + i, q.poll());
709        }
710    }
711
712    void checkToArray2(ArrayDeque q) {
713        int size = q.size();
714        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
715        Integer[] a2 = new Integer[size];
716        Integer[] a3 = new Integer[size + 2];
717        if (size > 0) Arrays.fill(a1, 42);
718        Arrays.fill(a2, 42);
719        Arrays.fill(a3, 42);
720        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
721        Integer[] b2 = (Integer[]) q.toArray(a2);
722        Integer[] b3 = (Integer[]) q.toArray(a3);
723        assertSame(a2, b2);
724        assertSame(a3, b3);
725        Iterator it = q.iterator();
731          for (int i = 0; i < size; i++) {
732 <            Integer x = (Integer) it.next();
733 <            assertSame(b1[i], x);
729 <            assertEquals(b1[0] + i, (int) x);
730 <            assertSame(b2[i], x);
731 <            assertSame(b3[i], x);
732 <        }
733 <        assertNull(a3[size]);
734 <        assertEquals(42, (int) a3[size + 1]);
735 <        if (size > 0) {
736 <            assertNotSame(a1, b1);
737 <            assertEquals(size, b1.length);
738 <            for (int i = 0; i < a1.length; i++) {
739 <                assertEquals(42, (int) a1[i]);
740 <            }
741 <        }
742 <    }
743 <
744 <    /**
745 <     * toArray(a) contains all elements in FIFO order
746 <     */
747 <    public void testToArray2() {
748 <        ArrayDeque q = new ArrayDeque();
749 <        for (int i = 0; i < SIZE; i++) {
750 <            checkToArray2(q);
751 <            q.addLast(i);
752 <        }
753 <        // Provoke wraparound
754 <        for (int i = 0; i < SIZE; i++) {
755 <            checkToArray2(q);
756 <            assertEquals(i, q.poll());
757 <            q.addLast(SIZE + i);
758 <        }
759 <        for (int i = 0; i < SIZE; i++) {
760 <            checkToArray2(q);
761 <            assertEquals(SIZE + i, q.poll());
732 >            checkToArray(q);
733 >            assertEquals((Integer) (added + i), q.poll());
734          }
735      }
736  
# Line 777 | 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 940 | 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