41 |
|
* Create a queue of given size containing consecutive |
42 |
|
* Integers 0 ... n. |
43 |
|
*/ |
44 |
< |
private LinkedBlockingQueue populatedQueue(int n) { |
45 |
< |
LinkedBlockingQueue q = new LinkedBlockingQueue(n); |
44 |
> |
private LinkedBlockingQueue<Integer> populatedQueue(int n) { |
45 |
> |
LinkedBlockingQueue<Integer> q = |
46 |
> |
new LinkedBlockingQueue<Integer>(n); |
47 |
|
assertTrue(q.isEmpty()); |
48 |
|
for (int i = 0; i < n; i++) |
49 |
|
assertTrue(q.offer(new Integer(i))); |
382 |
|
} |
383 |
|
|
384 |
|
/** |
384 |
– |
* take blocks interruptibly when empty |
385 |
– |
*/ |
386 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
387 |
– |
final LinkedBlockingQueue q = new LinkedBlockingQueue(2); |
388 |
– |
Thread t = new ThreadShouldThrow(InterruptedException.class) { |
389 |
– |
public void realRun() throws InterruptedException { |
390 |
– |
q.take(); |
391 |
– |
}}; |
392 |
– |
|
393 |
– |
t.start(); |
394 |
– |
Thread.sleep(SHORT_DELAY_MS); |
395 |
– |
t.interrupt(); |
396 |
– |
t.join(); |
397 |
– |
} |
398 |
– |
|
399 |
– |
/** |
385 |
|
* Take removes existing elements until empty, then blocks interruptibly |
386 |
|
*/ |
387 |
|
public void testBlockingTake() throws InterruptedException { |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
418 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
419 |
|
*/ |
420 |
|
public void testTimedPoll0() throws InterruptedException { |
421 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
426 |
|
} |
427 |
|
|
428 |
|
/** |
429 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
429 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
430 |
|
*/ |
431 |
|
public void testTimedPoll() throws InterruptedException { |
432 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
608 |
|
} |
609 |
|
|
610 |
|
/** |
611 |
< |
* toArray contains all elements |
611 |
> |
* toArray contains all elements in FIFO order |
612 |
|
*/ |
613 |
< |
public void testToArray() throws InterruptedException { |
613 |
> |
public void testToArray() { |
614 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
615 |
|
Object[] o = q.toArray(); |
616 |
|
for (int i = 0; i < o.length; i++) |
617 |
< |
assertEquals(o[i], q.take()); |
617 |
> |
assertSame(o[i], q.poll()); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
< |
* toArray(a) contains all elements |
621 |
> |
* toArray(a) contains all elements in FIFO order |
622 |
|
*/ |
623 |
|
public void testToArray2() throws InterruptedException { |
624 |
< |
LinkedBlockingQueue q = populatedQueue(SIZE); |
624 |
> |
LinkedBlockingQueue<Integer> q = populatedQueue(SIZE); |
625 |
|
Integer[] ints = new Integer[SIZE]; |
626 |
< |
ints = (Integer[])q.toArray(ints); |
626 |
> |
Integer[] array = q.toArray(ints); |
627 |
> |
assertSame(ints, array); |
628 |
|
for (int i = 0; i < ints.length; i++) |
629 |
< |
assertEquals(ints[i], q.take()); |
629 |
> |
assertSame(ints[i], q.poll()); |
630 |
|
} |
631 |
|
|
632 |
|
/** |
633 |
< |
* toArray(null) throws NPE |
633 |
> |
* toArray(null) throws NullPointerException |
634 |
|
*/ |
635 |
< |
public void testToArray_BadArg() { |
635 |
> |
public void testToArray_NullArg() { |
636 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
637 |
|
try { |
638 |
< |
Object o[] = q.toArray(null); |
638 |
> |
q.toArray(null); |
639 |
|
shouldThrow(); |
640 |
|
} catch (NullPointerException success) {} |
641 |
|
} |
642 |
|
|
643 |
|
/** |
644 |
< |
* toArray with incompatible array type throws CCE |
644 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
645 |
|
*/ |
646 |
|
public void testToArray1_BadArg() { |
647 |
|
LinkedBlockingQueue q = populatedQueue(SIZE); |
648 |
|
try { |
649 |
< |
Object o[] = q.toArray(new String[10]); |
649 |
> |
q.toArray(new String[10]); |
650 |
|
shouldThrow(); |
651 |
|
} catch (ArrayStoreException success) {} |
652 |
|
} |
881 |
|
} |
882 |
|
|
883 |
|
/** |
884 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
884 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
885 |
|
*/ |
886 |
|
public void testDrainToN() { |
887 |
|
LinkedBlockingQueue q = new LinkedBlockingQueue(); |
890 |
|
assertTrue(q.offer(new Integer(j))); |
891 |
|
ArrayList l = new ArrayList(); |
892 |
|
q.drainTo(l, i); |
893 |
< |
int k = (i < SIZE)? i : SIZE; |
893 |
> |
int k = (i < SIZE) ? i : SIZE; |
894 |
|
assertEquals(l.size(), k); |
895 |
|
assertEquals(q.size(), SIZE-k); |
896 |
|
for (int j = 0; j < k; ++j) |