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.57 by jsr166, Fri Jun 22 00:04:58 2018 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  
# Line 15 | Line 16 | import java.util.Random;
16   import java.util.concurrent.ThreadLocalRandom;
17  
18   import junit.framework.Test;
18 import junit.framework.TestSuite;
19  
20   public class ArrayDequeTest extends JSR166TestCase {
21      public static void main(String[] args) {
# Line 25 | Line 25 | public class ArrayDequeTest extends JSR1
25      public static Test suite() {
26          class Implementation implements CollectionImplementation {
27              public Class<?> klazz() { return ArrayDeque.class; }
28 <            public Collection emptyCollection() { return new ArrayDeque(); }
28 >            public Collection emptyCollection() { return populatedDeque(0); }
29              public Object makeElement(int i) { return i; }
30              public boolean isConcurrent() { return false; }
31              public boolean permitsNulls() { return false; }
# Line 38 | Line 38 | public class ArrayDequeTest extends JSR1
38       * Returns a new deque of given size containing consecutive
39       * Integers 0 ... n - 1.
40       */
41 <    private ArrayDeque<Integer> populatedDeque(int n) {
41 >    private static ArrayDeque<Integer> populatedDeque(int n) {
42          // Randomize various aspects of memory layout, including
43 <        // filled-to-capacity and wraparound.
43 >        // capacity slop and wraparound.
44          final ArrayDeque<Integer> q;
45          ThreadLocalRandom rnd = ThreadLocalRandom.current();
46          switch (rnd.nextInt(6)) {
47 <        case 0: q = new ArrayDeque<Integer>(); break;
48 <        case 1: q = new ArrayDeque<Integer>(0); break;
49 <        case 2: q = new ArrayDeque<Integer>(1); break;
50 <        case 3: q = new ArrayDeque<Integer>(n - 1); break;
51 <        case 4: q = new ArrayDeque<Integer>(n); break;
47 >        case 0: q = new ArrayDeque<Integer>();      break;
48 >        case 1: q = new ArrayDeque<Integer>(0);     break;
49 >        case 2: q = new ArrayDeque<Integer>(1);     break;
50 >        case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break;
51 >        case 4: q = new ArrayDeque<Integer>(n);     break;
52          case 5: q = new ArrayDeque<Integer>(n + 1); break;
53          default: throw new AssertionError();
54          }
# Line 59 | Line 59 | public class ArrayDequeTest extends JSR1
59              break;
60          case 1:
61              q.addLast(42);
62 <            assertEquals((Integer) 42, q.removeLast());
62 >            assertEquals((Integer) 42, q.removeFirst());
63              break;
64          case 2: /* do nothing */ break;
65          default: throw new AssertionError();
# Line 71 | Line 71 | public class ArrayDequeTest extends JSR1
71          else
72              for (int i = n; --i >= 0; )
73                  q.addFirst((Integer) i);
74        assertFalse(q.isEmpty());
74          assertEquals(n, q.size());
75 <        assertEquals((Integer) 0, q.peekFirst());
76 <        assertEquals((Integer) (n - 1), q.peekLast());
75 >        if (n > 0) {
76 >            assertFalse(q.isEmpty());
77 >            assertEquals((Integer) 0, q.peekFirst());
78 >            assertEquals((Integer) (n - 1), q.peekLast());
79 >        }
80          return q;
81      }
82  
# Line 568 | Line 570 | public class ArrayDequeTest extends JSR1
570       * removeFirstOccurrence(x) removes x and returns true if present
571       */
572      public void testRemoveFirstOccurrence() {
573 <        ArrayDeque q = populatedDeque(SIZE);
573 >        Deque<Integer> q = populatedDeque(SIZE);
574          assertFalse(q.removeFirstOccurrence(null));
575          for (int i = 1; i < SIZE; i += 2) {
576 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
576 >            assertTrue(q.removeFirstOccurrence(i));
577 >            assertFalse(q.contains(i));
578          }
579          for (int i = 0; i < SIZE; i += 2) {
580 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
581 <            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
580 >            assertTrue(q.removeFirstOccurrence(i));
581 >            assertFalse(q.removeFirstOccurrence(i + 1));
582 >            assertFalse(q.contains(i));
583 >            assertFalse(q.contains(i + 1));
584          }
585          assertTrue(q.isEmpty());
586          assertFalse(q.removeFirstOccurrence(null));
# Line 589 | Line 594 | public class ArrayDequeTest extends JSR1
594       * removeLastOccurrence(x) removes x and returns true if present
595       */
596      public void testRemoveLastOccurrence() {
597 <        ArrayDeque q = populatedDeque(SIZE);
597 >        Deque<Integer> q = populatedDeque(SIZE);
598          assertFalse(q.removeLastOccurrence(null));
599          for (int i = 1; i < SIZE; i += 2) {
600 <            assertTrue(q.removeLastOccurrence(new Integer(i)));
600 >            assertTrue(q.removeLastOccurrence(i));
601 >            assertFalse(q.contains(i));
602          }
603          for (int i = 0; i < SIZE; i += 2) {
604 <            assertTrue(q.removeLastOccurrence(new Integer(i)));
605 <            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
604 >            assertTrue(q.removeLastOccurrence(i));
605 >            assertFalse(q.removeLastOccurrence(i + 1));
606 >            assertFalse(q.contains(i));
607 >            assertFalse(q.contains(i + 1));
608          }
609          assertTrue(q.isEmpty());
610          assertFalse(q.removeLastOccurrence(null));
# Line 676 | Line 684 | public class ArrayDequeTest extends JSR1
684          }
685      }
686  
687 <    void checkToArray(ArrayDeque q) {
687 >    void checkToArray(ArrayDeque<Integer> q) {
688          int size = q.size();
689 <        Object[] o = q.toArray();
690 <        assertEquals(size, o.length);
689 >        Object[] a1 = q.toArray();
690 >        assertEquals(size, a1.length);
691 >        Integer[] a2 = q.toArray(new Integer[0]);
692 >        assertEquals(size, a2.length);
693 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
694 >        assertEquals(size, a3.length);
695 >        Integer[] a4 = new Integer[size];
696 >        assertSame(a4, q.toArray(a4));
697 >        Integer[] a5 = new Integer[size + 1];
698 >        Arrays.fill(a5, 42);
699 >        assertSame(a5, q.toArray(a5));
700 >        Integer[] a6 = new Integer[size + 2];
701 >        Arrays.fill(a6, 42);
702 >        assertSame(a6, q.toArray(a6));
703 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
704 >        for (Object[] a : as) {
705 >            if (a.length > size) assertNull(a[size]);
706 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
707 >        }
708          Iterator it = q.iterator();
709 +        Integer s = q.peekFirst();
710          for (int i = 0; i < size; i++) {
711              Integer x = (Integer) it.next();
712 <            assertEquals((Integer)o[0] + i, (int) x);
713 <            assertSame(o[i], x);
712 >            assertEquals(s + i, (int) x);
713 >            for (Object[] a : as)
714 >                assertSame(a1[i], x);
715          }
716      }
717  
718      /**
719 <     * toArray() contains all elements in FIFO order
719 >     * toArray() and toArray(a) contain all elements in FIFO order
720       */
721      public void testToArray() {
722 <        ArrayDeque q = new ArrayDeque();
723 <        for (int i = 0; i < SIZE; i++) {
722 >        final int size = ThreadLocalRandom.current().nextInt(10);
723 >        ArrayDeque<Integer> q = new ArrayDeque<>(size);
724 >        for (int i = 0; i < size; i++) {
725              checkToArray(q);
726              q.addLast(i);
727          }
728          // Provoke wraparound
729 <        for (int i = 0; i < SIZE; i++) {
730 <            checkToArray(q);
703 <            assertEquals(i, q.poll());
704 <            q.addLast(SIZE + i);
705 <        }
706 <        for (int i = 0; i < SIZE; i++) {
729 >        int added = size * 2;
730 >        for (int i = 0; i < added; i++) {
731              checkToArray(q);
732 <            assertEquals(SIZE + i, q.poll());
732 >            assertEquals((Integer) i, q.poll());
733 >            q.addLast(size + i);
734          }
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();
735          for (int i = 0; i < size; i++) {
736 <            Integer x = (Integer) it.next();
737 <            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());
736 >            checkToArray(q);
737 >            assertEquals((Integer) (added + i), q.poll());
738          }
739      }
740  
# Line 769 | Line 745 | public class ArrayDequeTest extends JSR1
745          ArrayDeque l = new ArrayDeque();
746          l.add(new Object());
747          try {
748 <            l.toArray(null);
748 >            l.toArray((Object[])null);
749              shouldThrow();
750          } catch (NullPointerException success) {}
751      }
# Line 777 | Line 753 | public class ArrayDequeTest extends JSR1
753      /**
754       * toArray(incompatible array type) throws ArrayStoreException
755       */
756 <    public void testToArray1_BadArg() {
756 >    public void testToArray_incompatibleArrayType() {
757          ArrayDeque l = new ArrayDeque();
758          l.add(new Integer(5));
759          try {
760              l.toArray(new String[10]);
761              shouldThrow();
762          } catch (ArrayStoreException success) {}
763 +        try {
764 +            l.toArray(new String[0]);
765 +            shouldThrow();
766 +        } catch (ArrayStoreException success) {}
767      }
768  
769      /**
# Line 932 | Line 912 | public class ArrayDequeTest extends JSR1
912      }
913  
914      /**
915 <     * A deserialized serialized deque has same elements in same order
915 >     * A deserialized/reserialized deque has same elements in same order
916       */
917      public void testSerialization() throws Exception {
918          Queue x = populatedDeque(SIZE);
# Line 940 | Line 920 | public class ArrayDequeTest extends JSR1
920  
921          assertNotSame(y, x);
922          assertEquals(x.size(), y.size());
923 +        assertEquals(x.toString(), y.toString());
924 +        assertEquals(Arrays.toString(x.toArray()), Arrays.toString(y.toArray()));
925 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
926 +        while (!x.isEmpty()) {
927 +            assertFalse(y.isEmpty());
928 +            assertEquals(x.remove(), y.remove());
929 +        }
930 +        assertTrue(y.isEmpty());
931 +    }
932 +
933 +    /**
934 +     * A cloned deque has same elements in same order
935 +     */
936 +    public void testClone() throws Exception {
937 +        ArrayDeque<Integer> x = populatedDeque(SIZE);
938 +        ArrayDeque<Integer> y = x.clone();
939 +
940 +        assertNotSame(y, x);
941 +        assertEquals(x.size(), y.size());
942          assertEquals(x.toString(), y.toString());
943          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
944          while (!x.isEmpty()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines