591 |
|
} |
592 |
|
} |
593 |
|
|
594 |
+ |
void checkToArray(ArrayBlockingQueue q) { |
595 |
+ |
int size = q.size(); |
596 |
+ |
Object[] o = q.toArray(); |
597 |
+ |
assertEquals(size, o.length); |
598 |
+ |
Iterator it = q.iterator(); |
599 |
+ |
for (int i = 0; i < size; i++) { |
600 |
+ |
Integer x = (Integer) it.next(); |
601 |
+ |
assertEquals((Integer)o[0] + i, (int) x); |
602 |
+ |
assertSame(o[i], x); |
603 |
+ |
} |
604 |
+ |
} |
605 |
+ |
|
606 |
|
/** |
607 |
< |
* toArray contains all elements in FIFO order |
607 |
> |
* toArray() contains all elements in FIFO order |
608 |
|
*/ |
609 |
|
public void testToArray() { |
610 |
< |
ArrayBlockingQueue q = populatedQueue(SIZE); |
611 |
< |
Object[] o = q.toArray(); |
612 |
< |
for (int i = 0; i < o.length; i++) |
613 |
< |
assertSame(o[i], q.poll()); |
610 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
611 |
> |
for (int i = 0; i < SIZE; i++) { |
612 |
> |
checkToArray(q); |
613 |
> |
q.add(i); |
614 |
> |
} |
615 |
> |
// Provoke wraparound |
616 |
> |
for (int i = 0; i < SIZE; i++) { |
617 |
> |
checkToArray(q); |
618 |
> |
assertEquals(i, q.poll()); |
619 |
> |
checkToArray(q); |
620 |
> |
q.add(SIZE+i); |
621 |
> |
} |
622 |
> |
for (int i = 0; i < SIZE; i++) { |
623 |
> |
checkToArray(q); |
624 |
> |
assertEquals(SIZE+i, q.poll()); |
625 |
> |
} |
626 |
> |
} |
627 |
> |
|
628 |
> |
void checkToArray2(ArrayBlockingQueue q) { |
629 |
> |
int size = q.size(); |
630 |
> |
Integer[] a1 = size == 0 ? null : new Integer[size-1]; |
631 |
> |
Integer[] a2 = new Integer[size]; |
632 |
> |
Integer[] a3 = new Integer[size+2]; |
633 |
> |
if (size > 0) Arrays.fill(a1, 42); |
634 |
> |
Arrays.fill(a2, 42); |
635 |
> |
Arrays.fill(a3, 42); |
636 |
> |
Integer[] b1 = size == 0 ? null : (Integer[]) q.toArray(a1); |
637 |
> |
Integer[] b2 = (Integer[]) q.toArray(a2); |
638 |
> |
Integer[] b3 = (Integer[]) q.toArray(a3); |
639 |
> |
assertSame(a2, b2); |
640 |
> |
assertSame(a3, b3); |
641 |
> |
Iterator it = q.iterator(); |
642 |
> |
for (int i = 0; i < size; i++) { |
643 |
> |
Integer x = (Integer) it.next(); |
644 |
> |
assertSame(b1[i], x); |
645 |
> |
assertEquals(b1[0] + i, (int) x); |
646 |
> |
assertSame(b2[i], x); |
647 |
> |
assertSame(b3[i], x); |
648 |
> |
} |
649 |
> |
assertNull(a3[size]); |
650 |
> |
assertEquals(42, (int) a3[size+1]); |
651 |
> |
if (size > 0) { |
652 |
> |
assertNotSame(a1, b1); |
653 |
> |
assertEquals(size, b1.length); |
654 |
> |
for (int i = 0; i < a1.length; i++) { |
655 |
> |
assertEquals(42, (int) a1[i]); |
656 |
> |
} |
657 |
> |
} |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
|
* toArray(a) contains all elements in FIFO order |
662 |
|
*/ |
663 |
|
public void testToArray2() { |
664 |
< |
ArrayBlockingQueue<Integer> q = populatedQueue(SIZE); |
665 |
< |
Integer[] ints = new Integer[SIZE]; |
666 |
< |
Integer[] array = q.toArray(ints); |
667 |
< |
assertSame(ints, array); |
668 |
< |
for (int i = 0; i < ints.length; i++) |
669 |
< |
assertSame(ints[i], q.poll()); |
664 |
> |
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE); |
665 |
> |
for (int i = 0; i < SIZE; i++) { |
666 |
> |
checkToArray2(q); |
667 |
> |
q.add(i); |
668 |
> |
} |
669 |
> |
// Provoke wraparound |
670 |
> |
for (int i = 0; i < SIZE; i++) { |
671 |
> |
checkToArray2(q); |
672 |
> |
assertEquals(i, q.poll()); |
673 |
> |
checkToArray2(q); |
674 |
> |
q.add(SIZE+i); |
675 |
> |
} |
676 |
> |
for (int i = 0; i < SIZE; i++) { |
677 |
> |
checkToArray2(q); |
678 |
> |
assertEquals(SIZE+i, q.poll()); |
679 |
> |
} |
680 |
|
} |
681 |
|
|
682 |
|
/** |