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.38 by jsr166, Sun May 24 01:42:14 2015 UTC vs.
Revision 1.54 by jsr166, Sun Nov 6 15:59:52 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 >        // capacity slop 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 529 | Line 572 | public class ArrayDequeTest extends JSR1
572       * removeFirstOccurrence(x) removes x and returns true if present
573       */
574      public void testRemoveFirstOccurrence() {
575 <        ArrayDeque q = populatedDeque(SIZE);
575 >        Deque<Integer> q = populatedDeque(SIZE);
576 >        assertFalse(q.removeFirstOccurrence(null));
577          for (int i = 1; i < SIZE; i += 2) {
578 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
578 >            assertTrue(q.removeFirstOccurrence(i));
579 >            assertFalse(q.contains(i));
580          }
581          for (int i = 0; i < SIZE; i += 2) {
582 <            assertTrue(q.removeFirstOccurrence(new Integer(i)));
583 <            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
582 >            assertTrue(q.removeFirstOccurrence(i));
583 >            assertFalse(q.removeFirstOccurrence(i + 1));
584 >            assertFalse(q.contains(i));
585 >            assertFalse(q.contains(i + 1));
586          }
587          assertTrue(q.isEmpty());
588 +        assertFalse(q.removeFirstOccurrence(null));
589 +        assertFalse(q.removeFirstOccurrence(42));
590 +        q = new ArrayDeque();
591 +        assertFalse(q.removeFirstOccurrence(null));
592 +        assertFalse(q.removeFirstOccurrence(42));
593      }
594  
595      /**
596       * removeLastOccurrence(x) removes x and returns true if present
597       */
598      public void testRemoveLastOccurrence() {
599 <        ArrayDeque q = populatedDeque(SIZE);
599 >        Deque<Integer> q = populatedDeque(SIZE);
600 >        assertFalse(q.removeLastOccurrence(null));
601          for (int i = 1; i < SIZE; i += 2) {
602 <            assertTrue(q.removeLastOccurrence(new Integer(i)));
602 >            assertTrue(q.removeLastOccurrence(i));
603 >            assertFalse(q.contains(i));
604          }
605          for (int i = 0; i < SIZE; i += 2) {
606 <            assertTrue(q.removeLastOccurrence(new Integer(i)));
607 <            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
606 >            assertTrue(q.removeLastOccurrence(i));
607 >            assertFalse(q.removeLastOccurrence(i + 1));
608 >            assertFalse(q.contains(i));
609 >            assertFalse(q.contains(i + 1));
610          }
611          assertTrue(q.isEmpty());
612 +        assertFalse(q.removeLastOccurrence(null));
613 +        assertFalse(q.removeLastOccurrence(42));
614 +        q = new ArrayDeque();
615 +        assertFalse(q.removeLastOccurrence(null));
616 +        assertFalse(q.removeLastOccurrence(42));
617      }
618  
619      /**
# Line 625 | Line 686 | public class ArrayDequeTest extends JSR1
686          }
687      }
688  
689 <    void checkToArray(ArrayDeque q) {
689 >    void checkToArray(ArrayDeque<Integer> q) {
690          int size = q.size();
691 <        Object[] o = q.toArray();
692 <        assertEquals(size, o.length);
691 >        Object[] a1 = q.toArray();
692 >        assertEquals(size, a1.length);
693 >        Integer[] a2 = q.toArray(new Integer[0]);
694 >        assertEquals(size, a2.length);
695 >        Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]);
696 >        assertEquals(size, a3.length);
697 >        Integer[] a4 = new Integer[size];
698 >        assertSame(a4, q.toArray(a4));
699 >        Integer[] a5 = new Integer[size + 1];
700 >        Arrays.fill(a5, 42);
701 >        assertSame(a5, q.toArray(a5));
702 >        Integer[] a6 = new Integer[size + 2];
703 >        Arrays.fill(a6, 42);
704 >        assertSame(a6, q.toArray(a6));
705 >        Object[][] as = { a1, a2, a3, a4, a5, a6 };
706 >        for (Object[] a : as) {
707 >            if (a.length > size) assertNull(a[size]);
708 >            if (a.length > size + 1) assertEquals(42, a[size + 1]);
709 >        }
710          Iterator it = q.iterator();
711 +        Integer s = q.peekFirst();
712          for (int i = 0; i < size; i++) {
713              Integer x = (Integer) it.next();
714 <            assertEquals((Integer)o[0] + i, (int) x);
715 <            assertSame(o[i], x);
714 >            assertEquals(s + i, (int) x);
715 >            for (Object[] a : as)
716 >                assertSame(a1[i], x);
717          }
718      }
719  
720      /**
721 <     * toArray() contains all elements in FIFO order
721 >     * toArray() and toArray(a) contain all elements in FIFO order
722       */
723      public void testToArray() {
724 <        ArrayDeque q = new ArrayDeque();
725 <        for (int i = 0; i < SIZE; i++) {
724 >        final int size = ThreadLocalRandom.current().nextInt(10);
725 >        ArrayDeque<Integer> q = new ArrayDeque<>(size);
726 >        for (int i = 0; i < size; i++) {
727              checkToArray(q);
728              q.addLast(i);
729          }
730          // Provoke wraparound
731 <        for (int i = 0; i < SIZE; i++) {
732 <            checkToArray(q);
652 <            assertEquals(i, q.poll());
653 <            q.addLast(SIZE + i);
654 <        }
655 <        for (int i = 0; i < SIZE; i++) {
731 >        int added = size * 2;
732 >        for (int i = 0; i < added; i++) {
733              checkToArray(q);
734 <            assertEquals(SIZE + i, q.poll());
734 >            assertEquals((Integer) i, q.poll());
735 >            q.addLast(size + i);
736          }
659    }
660
661    void checkToArray2(ArrayDeque q) {
662        int size = q.size();
663        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
664        Integer[] a2 = new Integer[size];
665        Integer[] a3 = new Integer[size + 2];
666        if (size > 0) Arrays.fill(a1, 42);
667        Arrays.fill(a2, 42);
668        Arrays.fill(a3, 42);
669        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
670        Integer[] b2 = (Integer[]) q.toArray(a2);
671        Integer[] b3 = (Integer[]) q.toArray(a3);
672        assertSame(a2, b2);
673        assertSame(a3, b3);
674        Iterator it = q.iterator();
737          for (int i = 0; i < size; i++) {
738 <            Integer x = (Integer) it.next();
739 <            assertSame(b1[i], x);
678 <            assertEquals(b1[0] + i, (int) x);
679 <            assertSame(b2[i], x);
680 <            assertSame(b3[i], x);
681 <        }
682 <        assertNull(a3[size]);
683 <        assertEquals(42, (int) a3[size + 1]);
684 <        if (size > 0) {
685 <            assertNotSame(a1, b1);
686 <            assertEquals(size, b1.length);
687 <            for (int i = 0; i < a1.length; i++) {
688 <                assertEquals(42, (int) a1[i]);
689 <            }
690 <        }
691 <    }
692 <
693 <    /**
694 <     * toArray(a) contains all elements in FIFO order
695 <     */
696 <    public void testToArray2() {
697 <        ArrayDeque q = new ArrayDeque();
698 <        for (int i = 0; i < SIZE; i++) {
699 <            checkToArray2(q);
700 <            q.addLast(i);
701 <        }
702 <        // Provoke wraparound
703 <        for (int i = 0; i < SIZE; i++) {
704 <            checkToArray2(q);
705 <            assertEquals(i, q.poll());
706 <            q.addLast(SIZE + i);
707 <        }
708 <        for (int i = 0; i < SIZE; i++) {
709 <            checkToArray2(q);
710 <            assertEquals(SIZE + i, q.poll());
738 >            checkToArray(q);
739 >            assertEquals((Integer) (added + i), q.poll());
740          }
741      }
742  
# Line 726 | Line 755 | public class ArrayDequeTest extends JSR1
755      /**
756       * toArray(incompatible array type) throws ArrayStoreException
757       */
758 <    public void testToArray1_BadArg() {
758 >    public void testToArray_incompatibleArrayType() {
759          ArrayDeque l = new ArrayDeque();
760          l.add(new Integer(5));
761          try {
762              l.toArray(new String[10]);
763              shouldThrow();
764          } catch (ArrayStoreException success) {}
765 +        try {
766 +            l.toArray(new String[0]);
767 +            shouldThrow();
768 +        } catch (ArrayStoreException success) {}
769      }
770  
771      /**
# Line 889 | Line 922 | public class ArrayDequeTest extends JSR1
922  
923          assertNotSame(y, x);
924          assertEquals(x.size(), y.size());
925 +        assertEquals(x.toString(), y.toString());
926 +        assertEquals(Arrays.toString(x.toArray()), Arrays.toString(y.toArray()));
927 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
928 +        while (!x.isEmpty()) {
929 +            assertFalse(y.isEmpty());
930 +            assertEquals(x.remove(), y.remove());
931 +        }
932 +        assertTrue(y.isEmpty());
933 +    }
934 +
935 +    /**
936 +     * A cloned deque has same elements in same order
937 +     */
938 +    public void testClone() throws Exception {
939 +        ArrayDeque<Integer> x = populatedDeque(SIZE);
940 +        ArrayDeque<Integer> y = x.clone();
941 +
942 +        assertNotSame(y, x);
943 +        assertEquals(x.size(), y.size());
944          assertEquals(x.toString(), y.toString());
945          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
946          while (!x.isEmpty()) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines