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.36 by jsr166, Sat May 23 00:53:08 2015 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 395 | Line 438 | public class ArrayDequeTest extends JSR1
438              assertTrue(q.contains(i));
439              assertTrue(q.remove(i));
440              assertFalse(q.contains(i));
441 <            assertTrue(q.contains(i-1));
441 >            assertTrue(q.contains(i - 1));
442          }
443          for (int i = 0; i < SIZE; i += 2) {
444              assertTrue(q.contains(i));
445              assertTrue(q.remove(i));
446              assertFalse(q.contains(i));
447 <            assertFalse(q.remove(i+1));
448 <            assertFalse(q.contains(i+1));
447 >            assertFalse(q.remove(i + 1));
448 >            assertFalse(q.contains(i + 1));
449          }
450          assertTrue(q.isEmpty());
451      }
# Line 530 | Line 573 | public class ArrayDequeTest extends JSR1
573       */
574      public void testRemoveFirstOccurrence() {
575          ArrayDeque q = populatedDeque(SIZE);
576 +        assertFalse(q.removeFirstOccurrence(null));
577          for (int i = 1; i < SIZE; i += 2) {
578              assertTrue(q.removeFirstOccurrence(new Integer(i)));
579          }
580          for (int i = 0; i < SIZE; i += 2) {
581              assertTrue(q.removeFirstOccurrence(new Integer(i)));
582 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
582 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
583          }
584          assertTrue(q.isEmpty());
585 +        assertFalse(q.removeFirstOccurrence(null));
586 +        assertFalse(q.removeFirstOccurrence(42));
587 +        q = new ArrayDeque();
588 +        assertFalse(q.removeFirstOccurrence(null));
589 +        assertFalse(q.removeFirstOccurrence(42));
590      }
591  
592      /**
# Line 545 | Line 594 | public class ArrayDequeTest extends JSR1
594       */
595      public void testRemoveLastOccurrence() {
596          ArrayDeque q = populatedDeque(SIZE);
597 +        assertFalse(q.removeLastOccurrence(null));
598          for (int i = 1; i < SIZE; i += 2) {
599              assertTrue(q.removeLastOccurrence(new Integer(i)));
600          }
601          for (int i = 0; i < SIZE; i += 2) {
602              assertTrue(q.removeLastOccurrence(new Integer(i)));
603 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
603 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
604          }
605          assertTrue(q.isEmpty());
606 +        assertFalse(q.removeLastOccurrence(null));
607 +        assertFalse(q.removeLastOccurrence(42));
608 +        q = new ArrayDeque();
609 +        assertFalse(q.removeLastOccurrence(null));
610 +        assertFalse(q.removeLastOccurrence(42));
611      }
612  
613      /**
# Line 625 | 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);
652 <            assertEquals(i, q.poll());
653 <            q.addLast(SIZE + i);
654 <        }
655 <        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          }
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();
731          for (int i = 0; i < size; i++) {
732 <            Integer x = (Integer) it.next();
733 <            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());
732 >            checkToArray(q);
733 >            assertEquals((Integer) (added + i), q.poll());
734          }
735      }
736  
# Line 726 | 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 781 | Line 808 | public class ArrayDequeTest extends JSR1
808          final Random rng = new Random();
809          for (int iters = 0; iters < 100; ++iters) {
810              int max = rng.nextInt(5) + 2;
811 <            int split = rng.nextInt(max-1) + 1;
811 >            int split = rng.nextInt(max - 1) + 1;
812              for (int j = 1; j <= max; ++j)
813                  q.add(new Integer(j));
814              Iterator it = q.iterator();
815              for (int j = 1; j <= split; ++j)
816                  assertEquals(it.next(), new Integer(j));
817              it.remove();
818 <            assertEquals(it.next(), new Integer(split+1));
818 >            assertEquals(it.next(), new Integer(split + 1));
819              for (int j = 1; j <= split; ++j)
820                  q.remove(new Integer(j));
821              it = q.iterator();
822 <            for (int j = split+1; j <= max; ++j) {
822 >            for (int j = split + 1; j <= max; ++j) {
823                  assertEquals(it.next(), new Integer(j));
824                  it.remove();
825              }
# Line 849 | Line 876 | public class ArrayDequeTest extends JSR1
876          final Random rng = new Random();
877          for (int iters = 0; iters < 100; ++iters) {
878              int max = rng.nextInt(5) + 2;
879 <            int split = rng.nextInt(max-1) + 1;
879 >            int split = rng.nextInt(max - 1) + 1;
880              for (int j = max; j >= 1; --j)
881                  q.add(new Integer(j));
882              Iterator it = q.descendingIterator();
883              for (int j = 1; j <= split; ++j)
884                  assertEquals(it.next(), new Integer(j));
885              it.remove();
886 <            assertEquals(it.next(), new Integer(split+1));
886 >            assertEquals(it.next(), new Integer(split + 1));
887              for (int j = 1; j <= split; ++j)
888                  q.remove(new Integer(j));
889              it = q.descendingIterator();
890 <            for (int j = split+1; j <= max; ++j) {
890 >            for (int j = split + 1; j <= max; ++j) {
891                  assertEquals(it.next(), new Integer(j));
892                  it.remove();
893              }
# Line 889 | 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