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 |
|
|
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. |
46 |
|
final ArrayDeque<Integer> q; |
680 |
|
} |
681 |
|
} |
682 |
|
|
683 |
< |
void checkToArray(ArrayDeque q) { |
683 |
> |
void checkToArray(ArrayDeque<Integer> q) { |
684 |
|
int size = q.size(); |
685 |
< |
Object[] o = q.toArray(); |
686 |
< |
assertEquals(size, o.length); |
685 |
> |
Object[] a1 = q.toArray(); |
686 |
> |
assertEquals(size, a1.length); |
687 |
> |
Integer[] a2 = q.toArray(new Integer[0]); |
688 |
> |
assertEquals(size, a2.length); |
689 |
> |
Integer[] a3 = q.toArray(new Integer[Math.max(0, size - 1)]); |
690 |
> |
assertEquals(size, a3.length); |
691 |
> |
Integer[] a4 = new Integer[size]; |
692 |
> |
assertSame(a4, q.toArray(a4)); |
693 |
> |
Integer[] a5 = new Integer[size + 1]; |
694 |
> |
Arrays.fill(a5, 42); |
695 |
> |
assertSame(a5, q.toArray(a5)); |
696 |
> |
Integer[] a6 = new Integer[size + 2]; |
697 |
> |
Arrays.fill(a6, 42); |
698 |
> |
assertSame(a6, q.toArray(a6)); |
699 |
> |
Object[][] as = { a1, a2, a3, a4, a5, a6 }; |
700 |
> |
for (Object[] a : as) { |
701 |
> |
if (a.length > size) assertNull(a[size]); |
702 |
> |
if (a.length > size + 1) assertEquals(42, a[size + 1]); |
703 |
> |
} |
704 |
|
Iterator it = q.iterator(); |
705 |
+ |
Integer s = q.peekFirst(); |
706 |
|
for (int i = 0; i < size; i++) { |
707 |
|
Integer x = (Integer) it.next(); |
708 |
< |
assertEquals((Integer)o[0] + i, (int) x); |
709 |
< |
assertSame(o[i], x); |
708 |
> |
assertEquals(s + i, (int) x); |
709 |
> |
for (Object[] a : as) |
710 |
> |
assertSame(a1[i], x); |
711 |
|
} |
712 |
|
} |
713 |
|
|
714 |
|
/** |
715 |
< |
* toArray() contains all elements in FIFO order |
715 |
> |
* toArray() and toArray(a) contain all elements in FIFO order |
716 |
|
*/ |
717 |
|
public void testToArray() { |
718 |
< |
ArrayDeque q = new ArrayDeque(); |
719 |
< |
for (int i = 0; i < SIZE; i++) { |
718 |
> |
final int size = ThreadLocalRandom.current().nextInt(10); |
719 |
> |
ArrayDeque<Integer> q = new ArrayDeque<>(size); |
720 |
> |
for (int i = 0; i < size; i++) { |
721 |
|
checkToArray(q); |
722 |
|
q.addLast(i); |
723 |
|
} |
724 |
|
// Provoke wraparound |
725 |
< |
for (int i = 0; i < SIZE; i++) { |
726 |
< |
checkToArray(q); |
706 |
< |
assertEquals(i, q.poll()); |
707 |
< |
q.addLast(SIZE + i); |
708 |
< |
} |
709 |
< |
for (int i = 0; i < SIZE; i++) { |
725 |
> |
int added = size * 2; |
726 |
> |
for (int i = 0; i < added; i++) { |
727 |
|
checkToArray(q); |
728 |
< |
assertEquals(SIZE + i, q.poll()); |
728 |
> |
assertEquals((Integer) i, q.poll()); |
729 |
> |
q.addLast(size + i); |
730 |
|
} |
713 |
– |
} |
714 |
– |
|
715 |
– |
void checkToArray2(ArrayDeque q) { |
716 |
– |
int size = q.size(); |
717 |
– |
Integer[] a1 = (size == 0) ? null : new Integer[size - 1]; |
718 |
– |
Integer[] a2 = new Integer[size]; |
719 |
– |
Integer[] a3 = new Integer[size + 2]; |
720 |
– |
if (size > 0) Arrays.fill(a1, 42); |
721 |
– |
Arrays.fill(a2, 42); |
722 |
– |
Arrays.fill(a3, 42); |
723 |
– |
Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1); |
724 |
– |
Integer[] b2 = (Integer[]) q.toArray(a2); |
725 |
– |
Integer[] b3 = (Integer[]) q.toArray(a3); |
726 |
– |
assertSame(a2, b2); |
727 |
– |
assertSame(a3, b3); |
728 |
– |
Iterator it = q.iterator(); |
731 |
|
for (int i = 0; i < size; i++) { |
732 |
< |
Integer x = (Integer) it.next(); |
733 |
< |
assertSame(b1[i], x); |
732 |
< |
assertEquals(b1[0] + i, (int) x); |
733 |
< |
assertSame(b2[i], x); |
734 |
< |
assertSame(b3[i], x); |
735 |
< |
} |
736 |
< |
assertNull(a3[size]); |
737 |
< |
assertEquals(42, (int) a3[size + 1]); |
738 |
< |
if (size > 0) { |
739 |
< |
assertNotSame(a1, b1); |
740 |
< |
assertEquals(size, b1.length); |
741 |
< |
for (int i = 0; i < a1.length; i++) { |
742 |
< |
assertEquals(42, (int) a1[i]); |
743 |
< |
} |
744 |
< |
} |
745 |
< |
} |
746 |
< |
|
747 |
< |
/** |
748 |
< |
* toArray(a) contains all elements in FIFO order |
749 |
< |
*/ |
750 |
< |
public void testToArray2() { |
751 |
< |
ArrayDeque q = new ArrayDeque(); |
752 |
< |
for (int i = 0; i < SIZE; i++) { |
753 |
< |
checkToArray2(q); |
754 |
< |
q.addLast(i); |
755 |
< |
} |
756 |
< |
// Provoke wraparound |
757 |
< |
for (int i = 0; i < SIZE; i++) { |
758 |
< |
checkToArray2(q); |
759 |
< |
assertEquals(i, q.poll()); |
760 |
< |
q.addLast(SIZE + i); |
761 |
< |
} |
762 |
< |
for (int i = 0; i < SIZE; i++) { |
763 |
< |
checkToArray2(q); |
764 |
< |
assertEquals(SIZE + i, q.poll()); |
732 |
> |
checkToArray(q); |
733 |
> |
assertEquals((Integer) (added + i), q.poll()); |
734 |
|
} |
735 |
|
} |
736 |
|
|
749 |
|
/** |
750 |
|
* toArray(incompatible array type) throws ArrayStoreException |
751 |
|
*/ |
752 |
< |
public void testToArray1_BadArg() { |
752 |
> |
public void testToArray_incompatibleArrayType() { |
753 |
|
ArrayDeque l = new ArrayDeque(); |
754 |
|
l.add(new Integer(5)); |
755 |
|
try { |
756 |
|
l.toArray(new String[10]); |
757 |
|
shouldThrow(); |
758 |
|
} catch (ArrayStoreException success) {} |
759 |
+ |
try { |
760 |
+ |
l.toArray(new String[0]); |
761 |
+ |
shouldThrow(); |
762 |
+ |
} catch (ArrayStoreException success) {} |
763 |
|
} |
764 |
|
|
765 |
|
/** |
960 |
|
} |
961 |
|
} |
962 |
|
|
990 |
– |
/** |
991 |
– |
* Handle capacities near Integer.MAX_VALUE. |
992 |
– |
* ant -Dvmoptions=-Xmx24g -Djsr166.expensiveTests=true -Djsr166.tckTestClass=ArrayDequeTest -Djsr166.methodFilter=testHuge tck |
993 |
– |
*/ |
994 |
– |
public void testHuge() { |
995 |
– |
if (! (testImplementationDetails |
996 |
– |
&& expensiveTests |
997 |
– |
&& Runtime.getRuntime().freeMemory() > 21_000_000_000L)) |
998 |
– |
return; |
999 |
– |
int maxSize = Integer.MAX_VALUE - 8; |
1000 |
– |
ArrayDeque<Integer> q; |
1001 |
– |
|
1002 |
– |
q = new ArrayDeque<>(maxSize); |
1003 |
– |
|
1004 |
– |
assertThrows(OutOfMemoryError.class, |
1005 |
– |
() -> new ArrayDeque<>(Integer.MAX_VALUE)); |
1006 |
– |
|
1007 |
– |
q = populatedDeque(0); |
1008 |
– |
q.addAll(Collections.nCopies(maxSize - 2, (Integer) 42)); |
1009 |
– |
assertEquals((Integer) 42, q.peekFirst()); |
1010 |
– |
assertEquals((Integer) 42, q.peekLast()); |
1011 |
– |
assertEquals(maxSize - 2, q.size()); |
1012 |
– |
q.addFirst((Integer) 0); |
1013 |
– |
q.addLast((Integer) 1); |
1014 |
– |
assertEquals((Integer) 0, q.peekFirst()); |
1015 |
– |
assertEquals((Integer) 1, q.peekLast()); |
1016 |
– |
assertEquals(maxSize, q.size()); |
1017 |
– |
} |
1018 |
– |
|
963 |
|
} |