27 |
|
public static Test suite() { |
28 |
|
class Implementation implements CollectionImplementation { |
29 |
|
public Class<?> klazz() { return ArrayDeque.class; } |
30 |
< |
public Collection emptyCollection() { return new ArrayDeque(); } |
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; } |
40 |
|
* Returns a new deque of given size containing consecutive |
41 |
|
* Integers 0 ... n - 1. |
42 |
|
*/ |
43 |
< |
private ArrayDeque<Integer> populatedDeque(int n) { |
43 |
> |
private static ArrayDeque<Integer> populatedDeque(int n) { |
44 |
|
// Randomize various aspects of memory layout, including |
45 |
< |
// filled-to-capacity and wraparound. |
45 |
> |
// capacity slop and wraparound. |
46 |
|
final ArrayDeque<Integer> q; |
47 |
|
ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
48 |
|
switch (rnd.nextInt(6)) { |
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)); |
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)); |
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 |
|
} |
710 |
– |
for (int i = 0; i < SIZE; i++) { |
711 |
– |
checkToArray(q); |
712 |
– |
assertEquals(SIZE + i, q.poll()); |
713 |
– |
} |
714 |
– |
} |
715 |
– |
|
716 |
– |
void checkToArray2(ArrayDeque q) { |
717 |
– |
int size = q.size(); |
718 |
– |
Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; |
719 |
– |
Integer[] a2 = new Integer[size]; |
720 |
– |
Integer[] a3 = new Integer[size + 2]; |
721 |
– |
if (size > 0) Arrays.fill(a1, 42); |
722 |
– |
Arrays.fill(a2, 42); |
723 |
– |
Arrays.fill(a3, 42); |
724 |
– |
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); |
725 |
– |
Integer[] b2 = (Integer[]) q.toArray(a2); |
726 |
– |
Integer[] b3 = (Integer[]) q.toArray(a3); |
727 |
– |
assertSame(a2, b2); |
728 |
– |
assertSame(a3, b3); |
729 |
– |
Iterator it = q.iterator(); |
737 |
|
for (int i = 0; i < size; i++) { |
738 |
< |
Integer x = (Integer) it.next(); |
739 |
< |
assertSame(b1[i], x); |
733 |
< |
assertEquals(b1[0] + i, (int) x); |
734 |
< |
assertSame(b2[i], x); |
735 |
< |
assertSame(b3[i], x); |
736 |
< |
} |
737 |
< |
assertNull(a3[size]); |
738 |
< |
assertEquals(42, (int) a3[size + 1]); |
739 |
< |
if (size > 0) { |
740 |
< |
assertNotSame(a1, b1); |
741 |
< |
assertEquals(size, b1.length); |
742 |
< |
for (int i = 0; i < a1.length; i++) { |
743 |
< |
assertEquals(42, (int) a1[i]); |
744 |
< |
} |
745 |
< |
} |
746 |
< |
} |
747 |
< |
|
748 |
< |
/** |
749 |
< |
* toArray(a) contains all elements in FIFO order |
750 |
< |
*/ |
751 |
< |
public void testToArray2() { |
752 |
< |
ArrayDeque q = new ArrayDeque(); |
753 |
< |
for (int i = 0; i < SIZE; i++) { |
754 |
< |
checkToArray2(q); |
755 |
< |
q.addLast(i); |
756 |
< |
} |
757 |
< |
// Provoke wraparound |
758 |
< |
for (int i = 0; i < SIZE; i++) { |
759 |
< |
checkToArray2(q); |
760 |
< |
assertEquals(i, q.poll()); |
761 |
< |
q.addLast(SIZE + i); |
762 |
< |
} |
763 |
< |
for (int i = 0; i < SIZE; i++) { |
764 |
< |
checkToArray2(q); |
765 |
< |
assertEquals(SIZE + i, q.poll()); |
738 |
> |
checkToArray(q); |
739 |
> |
assertEquals((Integer) (added + i), q.poll()); |
740 |
|
} |
741 |
|
} |
742 |
|
|
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 |
|
/** |
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()); |
967 |
|
} |
968 |
|
} |
969 |
|
|
991 |
– |
/** |
992 |
– |
* Handle capacities near Integer.MAX_VALUE. |
993 |
– |
* ant -Dvmoptions=-Xmx24g -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDequeTest -Djsr166.methodFilter=testHuge tck |
994 |
– |
*/ |
995 |
– |
public void testHuge() { |
996 |
– |
if (! (testImplementationDetails |
997 |
– |
&& expensiveTests |
998 |
– |
&& Runtime.getRuntime().freeMemory() > 21_000_000_000L)) |
999 |
– |
return; |
1000 |
– |
int maxSize = Integer.MAX_VALUE - 8; |
1001 |
– |
ArrayDeque<Integer> q; |
1002 |
– |
|
1003 |
– |
q = new ArrayDeque<>(maxSize); |
1004 |
– |
|
1005 |
– |
assertThrows(OutOfMemoryError.class, |
1006 |
– |
() -> new ArrayDeque<>(Integer.MAX_VALUE)); |
1007 |
– |
|
1008 |
– |
q = populatedDeque(0); |
1009 |
– |
q.addAll(Collections.nCopies(maxSize - 2, (Integer) 42)); |
1010 |
– |
assertEquals((Integer) 42, q.peekFirst()); |
1011 |
– |
assertEquals((Integer) 42, q.peekLast()); |
1012 |
– |
assertEquals(maxSize - 2, q.size()); |
1013 |
– |
q.addFirst((Integer) 0); |
1014 |
– |
q.addLast((Integer) 1); |
1015 |
– |
assertEquals((Integer) 0, q.peekFirst()); |
1016 |
– |
assertEquals((Integer) 1, q.peekLast()); |
1017 |
– |
assertEquals(maxSize, q.size()); |
1018 |
– |
} |
1019 |
– |
|
970 |
|
} |