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.35 by jsr166, Fri May 15 18:21:19 2015 UTC vs.
Revision 1.47 by jsr166, Mon Oct 17 15:31:19 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.Spliterator;
18 + import java.util.concurrent.ThreadLocalRandom;
19  
20   import junit.framework.Test;
21   import junit.framework.TestSuite;
# Line 22 | Line 26 | public class ArrayDequeTest extends JSR1
26      }
27  
28      public static Test suite() {
29 <        return new TestSuite(ArrayDequeTest.class);
29 >        class Implementation implements CollectionImplementation {
30 >            public Class<?> klazz() { return ArrayDeque.class; }
31 >            public Collection emptyCollection() { return populatedDeque(0); }
32 >            public Object makeElement(int i) { return i; }
33 >            public boolean isConcurrent() { return false; }
34 >            public boolean permitsNulls() { return false; }
35 >        }
36 >        return newTestSuite(ArrayDequeTest.class,
37 >                            CollectionTest.testSuite(new Implementation()));
38      }
39  
40      /**
41       * Returns a new deque of given size containing consecutive
42 <     * Integers 0 ... n.
42 >     * Integers 0 ... n - 1.
43       */
44 <    private ArrayDeque<Integer> populatedDeque(int n) {
45 <        ArrayDeque<Integer> q = new ArrayDeque<Integer>();
44 >    private static ArrayDeque<Integer> populatedDeque(int n) {
45 >        // Randomize various aspects of memory layout, including
46 >        // filled-to-capacity and wraparound.
47 >        final ArrayDeque<Integer> q;
48 >        ThreadLocalRandom rnd = ThreadLocalRandom.current();
49 >        switch (rnd.nextInt(6)) {
50 >        case 0: q = new ArrayDeque<Integer>();      break;
51 >        case 1: q = new ArrayDeque<Integer>(0);     break;
52 >        case 2: q = new ArrayDeque<Integer>(1);     break;
53 >        case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break;
54 >        case 4: q = new ArrayDeque<Integer>(n);     break;
55 >        case 5: q = new ArrayDeque<Integer>(n + 1); break;
56 >        default: throw new AssertionError();
57 >        }
58 >        switch (rnd.nextInt(3)) {
59 >        case 0:
60 >            q.addFirst(42);
61 >            assertEquals((Integer) 42, q.removeLast());
62 >            break;
63 >        case 1:
64 >            q.addLast(42);
65 >            assertEquals((Integer) 42, q.removeFirst());
66 >            break;
67 >        case 2: /* do nothing */ break;
68 >        default: throw new AssertionError();
69 >        }
70          assertTrue(q.isEmpty());
71 <        for (int i = 0; i < n; ++i)
72 <            assertTrue(q.offerLast(new Integer(i)));
73 <        assertFalse(q.isEmpty());
71 >        if (rnd.nextBoolean())
72 >            for (int i = 0; i < n; i++)
73 >                assertTrue(q.offerLast((Integer) i));
74 >        else
75 >            for (int i = n; --i >= 0; )
76 >                q.addFirst((Integer) i);
77          assertEquals(n, q.size());
78 +        if (n > 0) {
79 +            assertFalse(q.isEmpty());
80 +            assertEquals((Integer) 0, q.peekFirst());
81 +            assertEquals((Integer) (n - 1), q.peekLast());
82 +        }
83          return q;
84      }
85  
# Line 71 | Line 115 | public class ArrayDequeTest extends JSR1
115       */
116      public void testConstructor5() {
117          Integer[] ints = new Integer[SIZE];
118 <        for (int i = 0; i < SIZE-1; ++i)
118 >        for (int i = 0; i < SIZE - 1; ++i)
119              ints[i] = new Integer(i);
120          try {
121              new ArrayDeque(Arrays.asList(ints));
# Line 111 | Line 155 | public class ArrayDequeTest extends JSR1
155      public void testSize() {
156          ArrayDeque q = populatedDeque(SIZE);
157          for (int i = 0; i < SIZE; ++i) {
158 <            assertEquals(SIZE-i, q.size());
158 >            assertEquals(SIZE - i, q.size());
159              q.removeFirst();
160          }
161          for (int i = 0; i < SIZE; ++i) {
# Line 316 | Line 360 | public class ArrayDequeTest extends JSR1
360      public void testAddAll3() {
361          ArrayDeque q = new ArrayDeque();
362          Integer[] ints = new Integer[SIZE];
363 <        for (int i = 0; i < SIZE-1; ++i)
363 >        for (int i = 0; i < SIZE - 1; ++i)
364              ints[i] = new Integer(i);
365          try {
366              q.addAll(Arrays.asList(ints));
# Line 355 | Line 399 | public class ArrayDequeTest extends JSR1
399       */
400      public void testPollLast() {
401          ArrayDeque q = populatedDeque(SIZE);
402 <        for (int i = SIZE-1; i >= 0; --i) {
402 >        for (int i = SIZE - 1; i >= 0; --i) {
403              assertEquals(i, q.pollLast());
404          }
405          assertNull(q.pollLast());
# Line 395 | Line 439 | public class ArrayDequeTest extends JSR1
439              assertTrue(q.contains(i));
440              assertTrue(q.remove(i));
441              assertFalse(q.contains(i));
442 <            assertTrue(q.contains(i-1));
442 >            assertTrue(q.contains(i - 1));
443          }
444          for (int i = 0; i < SIZE; i += 2) {
445              assertTrue(q.contains(i));
446              assertTrue(q.remove(i));
447              assertFalse(q.contains(i));
448 <            assertFalse(q.remove(i+1));
449 <            assertFalse(q.contains(i+1));
448 >            assertFalse(q.remove(i + 1));
449 >            assertFalse(q.contains(i + 1));
450          }
451          assertTrue(q.isEmpty());
452      }
# Line 440 | Line 484 | public class ArrayDequeTest extends JSR1
484       */
485      public void testPeekLast() {
486          ArrayDeque q = populatedDeque(SIZE);
487 <        for (int i = SIZE-1; i >= 0; --i) {
487 >        for (int i = SIZE - 1; i >= 0; --i) {
488              assertEquals(i, q.peekLast());
489              assertEquals(i, q.pollLast());
490              assertTrue(q.peekLast() == null ||
# Line 484 | Line 528 | public class ArrayDequeTest extends JSR1
528       */
529      public void testLastElement() {
530          ArrayDeque q = populatedDeque(SIZE);
531 <        for (int i = SIZE-1; i >= 0; --i) {
531 >        for (int i = SIZE - 1; i >= 0; --i) {
532              assertEquals(i, q.getLast());
533              assertEquals(i, q.pollLast());
534          }
# Line 530 | Line 574 | public class ArrayDequeTest extends JSR1
574       */
575      public void testRemoveFirstOccurrence() {
576          ArrayDeque q = populatedDeque(SIZE);
577 +        assertFalse(q.removeFirstOccurrence(null));
578          for (int i = 1; i < SIZE; i += 2) {
579              assertTrue(q.removeFirstOccurrence(new Integer(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)));
583 >            assertFalse(q.removeFirstOccurrence(new Integer(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      /**
# Line 545 | Line 595 | public class ArrayDequeTest extends JSR1
595       */
596      public void testRemoveLastOccurrence() {
597          ArrayDeque q = populatedDeque(SIZE);
598 +        assertFalse(q.removeLastOccurrence(null));
599          for (int i = 1; i < SIZE; i += 2) {
600              assertTrue(q.removeLastOccurrence(new Integer(i)));
601          }
602          for (int i = 0; i < SIZE; i += 2) {
603              assertTrue(q.removeLastOccurrence(new Integer(i)));
604 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
604 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
605          }
606          assertTrue(q.isEmpty());
607 +        assertFalse(q.removeLastOccurrence(null));
608 +        assertFalse(q.removeLastOccurrence(42));
609 +        q = new ArrayDeque();
610 +        assertFalse(q.removeLastOccurrence(null));
611 +        assertFalse(q.removeLastOccurrence(42));
612      }
613  
614      /**
# Line 605 | Line 661 | public class ArrayDequeTest extends JSR1
661              boolean changed = q.retainAll(p);
662              assertEquals(changed, (i > 0));
663              assertTrue(q.containsAll(p));
664 <            assertEquals(SIZE-i, q.size());
664 >            assertEquals(SIZE - i, q.size());
665              p.removeFirst();
666          }
667      }
# Line 618 | Line 674 | public class ArrayDequeTest extends JSR1
674              ArrayDeque q = populatedDeque(SIZE);
675              ArrayDeque p = populatedDeque(i);
676              assertTrue(q.removeAll(p));
677 <            assertEquals(SIZE-i, q.size());
677 >            assertEquals(SIZE - i, q.size());
678              for (int j = 0; j < i; ++j) {
679                  assertFalse(q.contains(p.removeFirst()));
680              }
# Line 650 | Line 706 | public class ArrayDequeTest extends JSR1
706          for (int i = 0; i < SIZE; i++) {
707              checkToArray(q);
708              assertEquals(i, q.poll());
709 <            q.addLast(SIZE+i);
709 >            q.addLast(SIZE + i);
710          }
711          for (int i = 0; i < SIZE; i++) {
712              checkToArray(q);
713 <            assertEquals(SIZE+i, q.poll());
713 >            assertEquals(SIZE + i, q.poll());
714          }
715      }
716  
717      void checkToArray2(ArrayDeque q) {
718          int size = q.size();
719 <        Integer[] a1 = size == 0 ? null : new Integer[size-1];
719 >        Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
720          Integer[] a2 = new Integer[size];
721 <        Integer[] a3 = new Integer[size+2];
721 >        Integer[] a3 = new Integer[size + 2];
722          if (size > 0) Arrays.fill(a1, 42);
723          Arrays.fill(a2, 42);
724          Arrays.fill(a3, 42);
725 <        Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1);
725 >        Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
726          Integer[] b2 = (Integer[]) q.toArray(a2);
727          Integer[] b3 = (Integer[]) q.toArray(a3);
728          assertSame(a2, b2);
# Line 680 | Line 736 | public class ArrayDequeTest extends JSR1
736              assertSame(b3[i], x);
737          }
738          assertNull(a3[size]);
739 <        assertEquals(42, (int) a3[size+1]);
739 >        assertEquals(42, (int) a3[size + 1]);
740          if (size > 0) {
741              assertNotSame(a1, b1);
742              assertEquals(size, b1.length);
# Line 703 | Line 759 | public class ArrayDequeTest extends JSR1
759          for (int i = 0; i < SIZE; i++) {
760              checkToArray2(q);
761              assertEquals(i, q.poll());
762 <            q.addLast(SIZE+i);
762 >            q.addLast(SIZE + i);
763          }
764          for (int i = 0; i < SIZE; i++) {
765              checkToArray2(q);
766 <            assertEquals(SIZE+i, q.poll());
766 >            assertEquals(SIZE + i, q.poll());
767          }
768      }
769  
# Line 781 | Line 837 | public class ArrayDequeTest extends JSR1
837          final Random rng = new Random();
838          for (int iters = 0; iters < 100; ++iters) {
839              int max = rng.nextInt(5) + 2;
840 <            int split = rng.nextInt(max-1) + 1;
840 >            int split = rng.nextInt(max - 1) + 1;
841              for (int j = 1; j <= max; ++j)
842                  q.add(new Integer(j));
843              Iterator it = q.iterator();
844              for (int j = 1; j <= split; ++j)
845                  assertEquals(it.next(), new Integer(j));
846              it.remove();
847 <            assertEquals(it.next(), new Integer(split+1));
847 >            assertEquals(it.next(), new Integer(split + 1));
848              for (int j = 1; j <= split; ++j)
849                  q.remove(new Integer(j));
850              it = q.iterator();
851 <            for (int j = split+1; j <= max; ++j) {
851 >            for (int j = split + 1; j <= max; ++j) {
852                  assertEquals(it.next(), new Integer(j));
853                  it.remove();
854              }
# Line 849 | Line 905 | public class ArrayDequeTest extends JSR1
905          final Random rng = new Random();
906          for (int iters = 0; iters < 100; ++iters) {
907              int max = rng.nextInt(5) + 2;
908 <            int split = rng.nextInt(max-1) + 1;
908 >            int split = rng.nextInt(max - 1) + 1;
909              for (int j = max; j >= 1; --j)
910                  q.add(new Integer(j));
911              Iterator it = q.descendingIterator();
912              for (int j = 1; j <= split; ++j)
913                  assertEquals(it.next(), new Integer(j));
914              it.remove();
915 <            assertEquals(it.next(), new Integer(split+1));
915 >            assertEquals(it.next(), new Integer(split + 1));
916              for (int j = 1; j <= split; ++j)
917                  q.remove(new Integer(j));
918              it = q.descendingIterator();
919 <            for (int j = split+1; j <= max; ++j) {
919 >            for (int j = split + 1; j <= max; ++j) {
920                  assertEquals(it.next(), new Integer(j));
921                  it.remove();
922              }
# Line 899 | Line 955 | public class ArrayDequeTest extends JSR1
955      }
956  
957      /**
958 +     * A cloned deque has same elements in same order
959 +     */
960 +    public void testClone() throws Exception {
961 +        ArrayDeque<Integer> x = populatedDeque(SIZE);
962 +        ArrayDeque<Integer> y = x.clone();
963 +
964 +        assertNotSame(y, x);
965 +        assertEquals(x.size(), y.size());
966 +        assertEquals(x.toString(), y.toString());
967 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
968 +        while (!x.isEmpty()) {
969 +            assertFalse(y.isEmpty());
970 +            assertEquals(x.remove(), y.remove());
971 +        }
972 +        assertTrue(y.isEmpty());
973 +    }
974 +
975 +    /**
976       * remove(null), contains(null) always return false
977       */
978      public void testNeverContainsNull() {
# Line 915 | Line 989 | public class ArrayDequeTest extends JSR1
989          }
990      }
991  
992 +    /**
993 +     * Spliterator characteristics are as advertised
994 +     */
995 +    public void testSpliterator_characteristics() {
996 +        ArrayDeque q = new ArrayDeque();
997 +        Spliterator s = q.spliterator();
998 +        int characteristics = s.characteristics();
999 +        int required = Spliterator.NONNULL
1000 +            | Spliterator.ORDERED
1001 +            | Spliterator.SIZED
1002 +            | Spliterator.SUBSIZED;
1003 +        assertEquals(required, characteristics & required);
1004 +        assertEquals(0, characteristics
1005 +                     & (Spliterator.CONCURRENT
1006 +                        | Spliterator.DISTINCT
1007 +                        | Spliterator.IMMUTABLE
1008 +                        | Spliterator.SORTED));
1009 +    }
1010 +
1011 +    /**
1012 +     * Spliterator.getComparator always throws IllegalStateException
1013 +     */
1014 +    public void testSpliterator_getComparator() {
1015 +        assertThrows(IllegalStateException.class,
1016 +                     () -> new ArrayDeque().spliterator().getComparator());
1017 +    }
1018 +
1019 +    /**
1020 +     * Handle capacities near Integer.MAX_VALUE.
1021 +     * ant -Dvmoptions=-Xmx24g -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDequeTest -Djsr166.methodFilter=testHuge tck
1022 +     */
1023 +    public void testHuge() {
1024 +        if (! (testImplementationDetails
1025 +               && expensiveTests
1026 +               && Runtime.getRuntime().freeMemory() > 21_000_000_000L))
1027 +            return;
1028 +        int maxSize = Integer.MAX_VALUE - 8;
1029 +        ArrayDeque<Integer> q;
1030 +
1031 +        q = new ArrayDeque<>(maxSize);
1032 +
1033 +        assertThrows(OutOfMemoryError.class,
1034 +                     () -> new ArrayDeque<>(Integer.MAX_VALUE));
1035 +
1036 +        q = populatedDeque(0);
1037 +        q.addAll(Collections.nCopies(maxSize - 2, (Integer) 42));
1038 +        assertEquals((Integer) 42, q.peekFirst());
1039 +        assertEquals((Integer) 42, q.peekLast());
1040 +        assertEquals(maxSize - 2, q.size());
1041 +        q.addFirst((Integer) 0);
1042 +        q.addLast((Integer) 1);
1043 +        assertEquals((Integer) 0, q.peekFirst());
1044 +        assertEquals((Integer) 1, q.peekLast());
1045 +        assertEquals(maxSize, q.size());
1046 +    }
1047 +
1048   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines