8 |
|
import java.util.ArrayDeque; |
9 |
|
import java.util.Arrays; |
10 |
|
import java.util.Collection; |
11 |
– |
import java.util.Collections; |
11 |
|
import java.util.Deque; |
12 |
|
import java.util.Iterator; |
13 |
|
import java.util.NoSuchElementException; |
14 |
|
import java.util.Queue; |
15 |
|
import java.util.Random; |
17 |
– |
import java.util.Spliterator; |
16 |
|
import java.util.concurrent.ThreadLocalRandom; |
17 |
|
|
18 |
|
import junit.framework.Test; |
21 |
– |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class ArrayDequeTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
40 |
|
*/ |
41 |
|
private static ArrayDeque<Integer> populatedDeque(int n) { |
42 |
|
// Randomize various aspects of memory layout, including |
43 |
< |
// filled-to-capacity and wraparound. |
43 |
> |
// capacity slop and wraparound. |
44 |
|
final ArrayDeque<Integer> q; |
45 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
46 |
|
switch (rnd.nextInt(6)) { |
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)); |
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)); |
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(a[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); |
708 |
< |
assertEquals(i, q.poll()); |
709 |
< |
q.addLast(SIZE + i); |
710 |
< |
} |
711 |
< |
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 |
|
} |
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(); |
735 |
|
for (int i = 0; i < size; i++) { |
736 |
< |
Integer x = (Integer) it.next(); |
737 |
< |
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()); |
736 |
> |
checkToArray(q); |
737 |
> |
assertEquals((Integer) (added + i), q.poll()); |
738 |
|
} |
739 |
|
} |
740 |
|
|
745 |
|
ArrayDeque l = new ArrayDeque(); |
746 |
|
l.add(new Object()); |
747 |
|
try { |
748 |
< |
l.toArray(null); |
748 |
> |
l.toArray((Object[])null); |
749 |
|
shouldThrow(); |
750 |
|
} catch (NullPointerException success) {} |
751 |
|
} |
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 |
|
/** |
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); |
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()); |
965 |
|
} |
966 |
|
} |
967 |
|
|
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='-Xms22g -Xmx22g' -Djsr166.testImplementationDetails=true -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDequeTest -Djsr166.methodFilter=testHuge tck |
1023 |
– |
*/ |
1024 |
– |
public void testHuge() { |
1025 |
– |
if (! (testImplementationDetails |
1026 |
– |
&& expensiveTests |
1027 |
– |
&& Runtime.getRuntime().maxMemory() > 21_000_000_000L)) |
1028 |
– |
return; |
1029 |
– |
|
1030 |
– |
ArrayDeque q; |
1031 |
– |
final int maxSize = Integer.MAX_VALUE - 8; |
1032 |
– |
|
1033 |
– |
assertThrows(OutOfMemoryError.class, |
1034 |
– |
() -> new ArrayDeque<>(Integer.MAX_VALUE)); |
1035 |
– |
|
1036 |
– |
{ |
1037 |
– |
q = new ArrayDeque<>(maxSize); |
1038 |
– |
q = null; |
1039 |
– |
} |
1040 |
– |
|
1041 |
– |
{ |
1042 |
– |
q = new ArrayDeque(); |
1043 |
– |
assertTrue(q.addAll(Collections.nCopies(maxSize - 2, (Integer) 42))); |
1044 |
– |
assertEquals((Integer) 42, q.peekFirst()); |
1045 |
– |
assertEquals((Integer) 42, q.peekLast()); |
1046 |
– |
assertEquals(maxSize - 2, q.size()); |
1047 |
– |
q.addFirst((Integer) 0); |
1048 |
– |
q.addLast((Integer) 1); |
1049 |
– |
assertEquals((Integer) 0, q.peekFirst()); |
1050 |
– |
assertEquals((Integer) 1, q.peekLast()); |
1051 |
– |
assertEquals(maxSize, q.size()); |
1052 |
– |
q = null; |
1053 |
– |
} |
1054 |
– |
} |
1055 |
– |
|
968 |
|
} |