24 |
|
|
25 |
|
public static class Fair extends BlockingQueueTest { |
26 |
|
protected BlockingQueue emptyCollection() { |
27 |
< |
return new ArrayBlockingQueue(20, true); |
27 |
> |
return new ArrayBlockingQueue(SIZE, true); |
28 |
|
} |
29 |
|
} |
30 |
|
|
31 |
|
public static class NonFair extends BlockingQueueTest { |
32 |
|
protected BlockingQueue emptyCollection() { |
33 |
< |
return new ArrayBlockingQueue(20, false); |
33 |
> |
return new ArrayBlockingQueue(SIZE, false); |
34 |
|
} |
35 |
|
} |
36 |
|
|
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
< |
* Create a queue of given size containing consecutive |
48 |
> |
* Returns a new queue of given size containing consecutive |
49 |
|
* Integers 0 ... n. |
50 |
|
*/ |
51 |
|
private ArrayBlockingQueue<Integer> populatedQueue(int n) { |
322 |
|
}}); |
323 |
|
|
324 |
|
await(pleaseTake); |
325 |
< |
assertEquals(q.remainingCapacity(), 0); |
325 |
> |
assertEquals(0, q.remainingCapacity()); |
326 |
|
assertEquals(0, q.take()); |
327 |
|
|
328 |
|
await(pleaseInterrupt); |
329 |
|
assertThreadStaysAlive(t); |
330 |
|
t.interrupt(); |
331 |
|
awaitTermination(t); |
332 |
< |
assertEquals(q.remainingCapacity(), 0); |
332 |
> |
assertEquals(0, q.remainingCapacity()); |
333 |
|
} |
334 |
|
|
335 |
|
/** |
515 |
|
} |
516 |
|
|
517 |
|
/** |
518 |
– |
* remove(x) removes x and returns true if present |
519 |
– |
*/ |
520 |
– |
public void testRemoveElement() { |
521 |
– |
ArrayBlockingQueue q = populatedQueue(SIZE); |
522 |
– |
for (int i = 1; i < SIZE; i+=2) { |
523 |
– |
assertTrue(q.remove(new Integer(i))); |
524 |
– |
} |
525 |
– |
for (int i = 0; i < SIZE; i+=2) { |
526 |
– |
assertTrue(q.remove(new Integer(i))); |
527 |
– |
assertFalse(q.remove(new Integer(i+1))); |
528 |
– |
} |
529 |
– |
assertTrue(q.isEmpty()); |
530 |
– |
} |
531 |
– |
|
532 |
– |
/** |
518 |
|
* contains(x) reports true when elements added but not yet removed |
519 |
|
*/ |
520 |
|
public void testContains() { |
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 |
|
/** |
822 |
|
Queue x = populatedQueue(SIZE); |
823 |
|
Queue y = serialClone(x); |
824 |
|
|
825 |
< |
assertTrue(x != y); |
825 |
> |
assertNotSame(x, y); |
826 |
|
assertEquals(x.size(), y.size()); |
827 |
|
assertEquals(x.toString(), y.toString()); |
828 |
|
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
840 |
|
ArrayBlockingQueue q = populatedQueue(SIZE); |
841 |
|
ArrayList l = new ArrayList(); |
842 |
|
q.drainTo(l); |
843 |
< |
assertEquals(q.size(), 0); |
844 |
< |
assertEquals(l.size(), SIZE); |
843 |
> |
assertEquals(0, q.size()); |
844 |
> |
assertEquals(SIZE, l.size()); |
845 |
|
for (int i = 0; i < SIZE; ++i) |
846 |
|
assertEquals(l.get(i), new Integer(i)); |
847 |
|
q.add(zero); |
851 |
|
assertTrue(q.contains(one)); |
852 |
|
l.clear(); |
853 |
|
q.drainTo(l); |
854 |
< |
assertEquals(q.size(), 0); |
855 |
< |
assertEquals(l.size(), 2); |
854 |
> |
assertEquals(0, q.size()); |
855 |
> |
assertEquals(2, l.size()); |
856 |
|
for (int i = 0; i < 2; ++i) |
857 |
|
assertEquals(l.get(i), new Integer(i)); |
858 |
|
} |
888 |
|
ArrayList l = new ArrayList(); |
889 |
|
q.drainTo(l, i); |
890 |
|
int k = (i < SIZE) ? i : SIZE; |
891 |
< |
assertEquals(l.size(), k); |
892 |
< |
assertEquals(q.size(), SIZE-k); |
891 |
> |
assertEquals(k, l.size()); |
892 |
> |
assertEquals(SIZE-k, q.size()); |
893 |
|
for (int j = 0; j < k; ++j) |
894 |
|
assertEquals(l.get(j), new Integer(j)); |
895 |
|
while (q.poll() != null) ; |