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); |
707 |
< |
assertEquals(i, q.poll()); |
708 |
< |
q.addLast(SIZE + i); |
709 |
< |
} |
710 |
< |
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 |
|
} |
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(); |
731 |
|
for (int i = 0; i < size; i++) { |
732 |
< |
Integer x = (Integer) it.next(); |
733 |
< |
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()); |
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 |
|
/** |