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.56 by jsr166, Fri Aug 4 03:30:21 2017 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 12 | Line 13 | import java.util.Iterator;
13   import java.util.NoSuchElementException;
14   import java.util.Queue;
15   import java.util.Random;
16 + import java.util.concurrent.ThreadLocalRandom;
17  
18   import junit.framework.Test;
17 import junit.framework.TestSuite;
19  
20   public class ArrayDequeTest extends JSR166TestCase {
21      public static void main(String[] args) {
# Line 22 | Line 23 | public class ArrayDequeTest extends JSR1
23      }
24  
25      public static Test suite() {
26 <        return new TestSuite(ArrayDequeTest.class);
26 >        class Implementation implements CollectionImplementation {
27 >            public Class<?> klazz() { return ArrayDeque.class; }
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; }
32 >        }
33 >        return newTestSuite(ArrayDequeTest.class,
34 >                            CollectionTest.testSuite(new Implementation()));
35      }
36  
37      /**
38       * Returns a new deque of given size containing consecutive
39 <     * Integers 0 ... n.
39 >     * Integers 0 ... n - 1.
40       */
41 <    private ArrayDeque<Integer> populatedDeque(int n) {
42 <        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
41 >    private static ArrayDeque<Integer> populatedDeque(int n) {
42 >        // Randomize various aspects of memory layout, including
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>(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 >        }
55 >        switch (rnd.nextInt(3)) {
56 >        case 0:
57 >            q.addFirst(42);
58 >            assertEquals((Integer) 42, q.removeLast());
59 >            break;
60 >        case 1:
61 >            q.addLast(42);
62 >            assertEquals((Integer) 42, q.removeFirst());
63 >            break;
64 >        case 2: /* do nothing */ break;
65 >        default: throw new AssertionError();
66 >        }
67          assertTrue(q.isEmpty());
68 <        for (int i = 0; i < n; ++i)
69 <            assertTrue(q.offerLast(new Integer(i)));
70 <        assertFalse(q.isEmpty());
68 >        if (rnd.nextBoolean())
69 >            for (int i = 0; i < n; i++)
70 >                assertTrue(q.offerLast((Integer) i));
71 >        else
72 >            for (int i = n; --i >= 0; )
73 >                q.addFirst((Integer) i);
74          assertEquals(n, q.size());
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 395 | Line 436 | public class ArrayDequeTest extends JSR1
436              assertTrue(q.contains(i));
437              assertTrue(q.remove(i));
438              assertFalse(q.contains(i));
439 <            assertTrue(q.contains(i-1));
439 >            assertTrue(q.contains(i - 1));
440          }
441          for (int i = 0; i < SIZE; i += 2) {
442              assertTrue(q.contains(i));
443              assertTrue(q.remove(i));
444              assertFalse(q.contains(i));
445 <            assertFalse(q.remove(i+1));
446 <            assertFalse(q.contains(i+1));
445 >            assertFalse(q.remove(i + 1));
446 >            assertFalse(q.contains(i + 1));
447          }
448          assertTrue(q.isEmpty());
449      }
# Line 529 | 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));
587 +        assertFalse(q.removeFirstOccurrence(42));
588 +        q = new ArrayDeque();
589 +        assertFalse(q.removeFirstOccurrence(null));
590 +        assertFalse(q.removeFirstOccurrence(42));
591      }
592  
593      /**
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));
611 +        assertFalse(q.removeLastOccurrence(42));
612 +        q = new ArrayDeque();
613 +        assertFalse(q.removeLastOccurrence(null));
614 +        assertFalse(q.removeLastOccurrence(42));
615      }
616  
617      /**
# Line 625 | 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);
652 <            assertEquals(i, q.poll());
653 <            q.addLast(SIZE + i);
654 <        }
655 <        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          }
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();
735          for (int i = 0; i < size; i++) {
736 <            Integer x = (Integer) it.next();
737 <            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());
736 >            checkToArray(q);
737 >            assertEquals((Integer) (added + i), q.poll());
738          }
739      }
740  
# Line 726 | 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 781 | Line 812 | public class ArrayDequeTest extends JSR1
812          final Random rng = new Random();
813          for (int iters = 0; iters < 100; ++iters) {
814              int max = rng.nextInt(5) + 2;
815 <            int split = rng.nextInt(max-1) + 1;
815 >            int split = rng.nextInt(max - 1) + 1;
816              for (int j = 1; j <= max; ++j)
817                  q.add(new Integer(j));
818              Iterator it = q.iterator();
819              for (int j = 1; j <= split; ++j)
820                  assertEquals(it.next(), new Integer(j));
821              it.remove();
822 <            assertEquals(it.next(), new Integer(split+1));
822 >            assertEquals(it.next(), new Integer(split + 1));
823              for (int j = 1; j <= split; ++j)
824                  q.remove(new Integer(j));
825              it = q.iterator();
826 <            for (int j = split+1; j <= max; ++j) {
826 >            for (int j = split + 1; j <= max; ++j) {
827                  assertEquals(it.next(), new Integer(j));
828                  it.remove();
829              }
# Line 849 | Line 880 | public class ArrayDequeTest extends JSR1
880          final Random rng = new Random();
881          for (int iters = 0; iters < 100; ++iters) {
882              int max = rng.nextInt(5) + 2;
883 <            int split = rng.nextInt(max-1) + 1;
883 >            int split = rng.nextInt(max - 1) + 1;
884              for (int j = max; j >= 1; --j)
885                  q.add(new Integer(j));
886              Iterator it = q.descendingIterator();
887              for (int j = 1; j <= split; ++j)
888                  assertEquals(it.next(), new Integer(j));
889              it.remove();
890 <            assertEquals(it.next(), new Integer(split+1));
890 >            assertEquals(it.next(), new Integer(split + 1));
891              for (int j = 1; j <= split; ++j)
892                  q.remove(new Integer(j));
893              it = q.descendingIterator();
894 <            for (int j = split+1; j <= max; ++j) {
894 >            for (int j = split + 1; j <= max; ++j) {
895                  assertEquals(it.next(), new Integer(j));
896                  it.remove();
897              }
# Line 881 | 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 889 | 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