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.48 by jsr166, Mon Oct 17 15:48:01 2016 UTC vs.
Revision 1.53 by jsr166, Thu Nov 3 15:45:56 2016 UTC

# Line 14 | Line 14 | import java.util.Iterator;
14   import java.util.NoSuchElementException;
15   import java.util.Queue;
16   import java.util.Random;
17 import java.util.Spliterator;
17   import java.util.concurrent.ThreadLocalRandom;
18  
19   import junit.framework.Test;
# Line 573 | 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));
# Line 594 | Line 596 | public class ArrayDequeTest extends JSR1
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));
# Line 681 | 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++) {
731 >        int added = size * 2;
732 >        for (int i = 0; i < added; i++) {
733              checkToArray(q);
734 <            assertEquals(i, q.poll());
735 <            q.addLast(SIZE + i);
734 >            assertEquals((Integer) i, q.poll());
735 >            q.addLast(size + i);
736          }
711        for (int i = 0; i < SIZE; i++) {
712            checkToArray(q);
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];
720        Integer[] a2 = new Integer[size];
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);
726        Integer[] b2 = (Integer[]) q.toArray(a2);
727        Integer[] b3 = (Integer[]) q.toArray(a3);
728        assertSame(a2, b2);
729        assertSame(a3, b3);
730        Iterator it = q.iterator();
737          for (int i = 0; i < size; i++) {
738 <            Integer x = (Integer) it.next();
739 <            assertSame(b1[i], x);
734 <            assertEquals(b1[0] + i, (int) x);
735 <            assertSame(b2[i], x);
736 <            assertSame(b3[i], x);
737 <        }
738 <        assertNull(a3[size]);
739 <        assertEquals(42, (int) a3[size + 1]);
740 <        if (size > 0) {
741 <            assertNotSame(a1, b1);
742 <            assertEquals(size, b1.length);
743 <            for (int i = 0; i < a1.length; i++) {
744 <                assertEquals(42, (int) a1[i]);
745 <            }
746 <        }
747 <    }
748 <
749 <    /**
750 <     * toArray(a) contains all elements in FIFO order
751 <     */
752 <    public void testToArray2() {
753 <        ArrayDeque q = new ArrayDeque();
754 <        for (int i = 0; i < SIZE; i++) {
755 <            checkToArray2(q);
756 <            q.addLast(i);
757 <        }
758 <        // Provoke wraparound
759 <        for (int i = 0; i < SIZE; i++) {
760 <            checkToArray2(q);
761 <            assertEquals(i, q.poll());
762 <            q.addLast(SIZE + i);
763 <        }
764 <        for (int i = 0; i < SIZE; i++) {
765 <            checkToArray2(q);
766 <            assertEquals(SIZE + i, q.poll());
738 >            checkToArray(q);
739 >            assertEquals((Integer) (added + i), q.poll());
740          }
741      }
742  
# Line 782 | 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 946 | Line 923 | public class ArrayDequeTest extends JSR1
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());
# Line 989 | Line 967 | public class ArrayDequeTest extends JSR1
967          }
968      }
969  
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        assertTrue(s.hasCharacteristics(required));
1005        assertEquals(0, characteristics
1006                     & (Spliterator.CONCURRENT
1007                        | Spliterator.DISTINCT
1008                        | Spliterator.IMMUTABLE
1009                        | Spliterator.SORTED));
1010    }
1011
1012    /**
1013     * Spliterator.getComparator always throws IllegalStateException
1014     */
1015    public void testSpliterator_getComparator() {
1016        assertThrows(IllegalStateException.class,
1017                     () -> new ArrayDeque().spliterator().getComparator());
1018    }
1019
1020    /**
1021     * Handle capacities near Integer.MAX_VALUE.
1022     * ant -Dvmoptions=-Xmx24g -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDequeTest -Djsr166.methodFilter=testHuge tck
1023     */
1024    public void testHuge() {
1025        if (! (testImplementationDetails
1026               && expensiveTests
1027               && Runtime.getRuntime().freeMemory() > 21_000_000_000L))
1028            return;
1029        int maxSize = Integer.MAX_VALUE - 8;
1030        ArrayDeque<Integer> q;
1031
1032        q = new ArrayDeque<>(maxSize);
1033
1034        assertThrows(OutOfMemoryError.class,
1035                     () -> new ArrayDeque<>(Integer.MAX_VALUE));
1036
1037        q = populatedDeque(0);
1038        assertTrue(q.addAll(Collections.nCopies(maxSize - 2, (Integer) 42)));
1039        assertEquals((Integer) 42, q.peekFirst());
1040        assertEquals((Integer) 42, q.peekLast());
1041        assertEquals(maxSize - 2, q.size());
1042        q.addFirst((Integer) 0);
1043        q.addLast((Integer) 1);
1044        assertEquals((Integer) 0, q.peekFirst());
1045        assertEquals((Integer) 1, q.peekLast());
1046        assertEquals(maxSize, q.size());
1047    }
1048
970   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines