13 |
|
import java.io.*; |
14 |
|
|
15 |
|
public class PriorityBlockingQueueTest extends JSR166TestCase { |
16 |
+ |
|
17 |
+ |
public static class Generic extends BlockingQueueTest { |
18 |
+ |
protected BlockingQueue emptyCollection() { |
19 |
+ |
return new PriorityBlockingQueue(); |
20 |
+ |
} |
21 |
+ |
} |
22 |
+ |
|
23 |
+ |
public static class InitialCapacity extends BlockingQueueTest { |
24 |
+ |
protected BlockingQueue emptyCollection() { |
25 |
+ |
return new PriorityBlockingQueue(20); |
26 |
+ |
} |
27 |
+ |
} |
28 |
+ |
|
29 |
|
public static void main(String[] args) { |
30 |
< |
junit.textui.TestRunner.run (suite()); |
30 |
> |
junit.textui.TestRunner.run(suite()); |
31 |
|
} |
32 |
+ |
|
33 |
|
public static Test suite() { |
34 |
< |
return new TestSuite(PriorityBlockingQueueTest.class); |
34 |
> |
return newTestSuite(PriorityBlockingQueueTest.class, |
35 |
> |
new Generic().testSuite(), |
36 |
> |
new InitialCapacity().testSuite()); |
37 |
|
} |
38 |
|
|
39 |
|
private static final int NOCAP = Integer.MAX_VALUE; |
41 |
|
/** Sample Comparator */ |
42 |
|
static class MyReverseComparator implements Comparator { |
43 |
|
public int compare(Object x, Object y) { |
44 |
< |
int i = ((Integer)x).intValue(); |
29 |
< |
int j = ((Integer)y).intValue(); |
30 |
< |
if (i < j) return 1; |
31 |
< |
if (i > j) return -1; |
32 |
< |
return 0; |
44 |
> |
return ((Comparable)y).compareTo(x); |
45 |
|
} |
46 |
|
} |
47 |
|
|
261 |
|
shouldThrow(); |
262 |
|
} catch (NullPointerException success) {} |
263 |
|
} |
264 |
+ |
|
265 |
|
/** |
266 |
|
* addAll of a collection with any null elements throws NPE after |
267 |
|
* possibly adding some elements |
361 |
|
public void testTake() throws InterruptedException { |
362 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
363 |
|
for (int i = 0; i < SIZE; ++i) { |
364 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
364 |
> |
assertEquals(i, q.take()); |
365 |
|
} |
366 |
|
} |
367 |
|
|
368 |
|
/** |
356 |
– |
* take blocks interruptibly when empty |
357 |
– |
*/ |
358 |
– |
public void testTakeFromEmpty() throws InterruptedException { |
359 |
– |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
360 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
361 |
– |
public void realRun() throws InterruptedException { |
362 |
– |
q.take(); |
363 |
– |
}}); |
364 |
– |
|
365 |
– |
t.start(); |
366 |
– |
Thread.sleep(SHORT_DELAY_MS); |
367 |
– |
t.interrupt(); |
368 |
– |
t.join(); |
369 |
– |
} |
370 |
– |
|
371 |
– |
/** |
369 |
|
* Take removes existing elements until empty, then blocks interruptibly |
370 |
|
*/ |
371 |
|
public void testBlockingTake() throws InterruptedException { |
372 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
372 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
373 |
> |
Thread t = new Thread(new CheckedRunnable() { |
374 |
|
public void realRun() throws InterruptedException { |
377 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
375 |
|
for (int i = 0; i < SIZE; ++i) { |
376 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
376 |
> |
assertEquals(i, q.take()); |
377 |
|
} |
378 |
< |
q.take(); |
378 |
> |
try { |
379 |
> |
q.take(); |
380 |
> |
shouldThrow(); |
381 |
> |
} catch (InterruptedException success) {} |
382 |
|
}}); |
383 |
|
|
384 |
|
t.start(); |
394 |
|
public void testPoll() { |
395 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
396 |
|
for (int i = 0; i < SIZE; ++i) { |
397 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
397 |
> |
assertEquals(i, q.poll()); |
398 |
|
} |
399 |
|
assertNull(q.poll()); |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* timed pool with zero timeout succeeds when non-empty, else times out |
403 |
> |
* timed poll with zero timeout succeeds when non-empty, else times out |
404 |
|
*/ |
405 |
|
public void testTimedPoll0() throws InterruptedException { |
406 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
407 |
|
for (int i = 0; i < SIZE; ++i) { |
408 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
408 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
409 |
|
} |
410 |
|
assertNull(q.poll(0, MILLISECONDS)); |
411 |
|
} |
412 |
|
|
413 |
|
/** |
414 |
< |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
414 |
> |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
415 |
|
*/ |
416 |
|
public void testTimedPoll() throws InterruptedException { |
417 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
418 |
|
for (int i = 0; i < SIZE; ++i) { |
419 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
419 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
420 |
|
} |
421 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
422 |
|
} |
430 |
|
public void realRun() throws InterruptedException { |
431 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
432 |
|
for (int i = 0; i < SIZE; ++i) { |
433 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
433 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
434 |
|
} |
435 |
|
try { |
436 |
|
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
445 |
|
} |
446 |
|
|
447 |
|
/** |
448 |
– |
* timed poll before a delayed offer fails; after offer succeeds; |
449 |
– |
* on interruption throws |
450 |
– |
*/ |
451 |
– |
public void testTimedPollWithOffer() throws InterruptedException { |
452 |
– |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
453 |
– |
Thread t = new Thread(new CheckedRunnable() { |
454 |
– |
public void realRun() throws InterruptedException { |
455 |
– |
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
456 |
– |
assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
457 |
– |
try { |
458 |
– |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
459 |
– |
shouldThrow(); |
460 |
– |
} catch (InterruptedException success) {} |
461 |
– |
}}); |
462 |
– |
|
463 |
– |
t.start(); |
464 |
– |
Thread.sleep(SMALL_DELAY_MS); |
465 |
– |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
466 |
– |
t.interrupt(); |
467 |
– |
t.join(); |
468 |
– |
} |
469 |
– |
|
470 |
– |
|
471 |
– |
/** |
448 |
|
* peek returns next element, or null if empty |
449 |
|
*/ |
450 |
|
public void testPeek() { |
451 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
452 |
|
for (int i = 0; i < SIZE; ++i) { |
453 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
454 |
< |
q.poll(); |
453 |
> |
assertEquals(i, q.peek()); |
454 |
> |
assertEquals(i, q.poll()); |
455 |
|
assertTrue(q.peek() == null || |
456 |
< |
i != ((Integer)q.peek()).intValue()); |
456 |
> |
!q.peek().equals(i)); |
457 |
|
} |
458 |
|
assertNull(q.peek()); |
459 |
|
} |
464 |
|
public void testElement() { |
465 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
466 |
|
for (int i = 0; i < SIZE; ++i) { |
467 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
468 |
< |
q.poll(); |
467 |
> |
assertEquals(i, q.element()); |
468 |
> |
assertEquals(i, q.poll()); |
469 |
|
} |
470 |
|
try { |
471 |
|
q.element(); |
479 |
|
public void testRemove() { |
480 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
481 |
|
for (int i = 0; i < SIZE; ++i) { |
482 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
482 |
> |
assertEquals(i, q.remove()); |
483 |
|
} |
484 |
|
try { |
485 |
|
q.remove(); |
579 |
|
} |
580 |
|
|
581 |
|
/** |
582 |
< |
* toArray contains all elements |
582 |
> |
* toArray contains all elements |
583 |
|
*/ |
584 |
|
public void testToArray() throws InterruptedException { |
585 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
605 |
|
* toArray(null) throws NPE |
606 |
|
*/ |
607 |
|
public void testToArray_BadArg() { |
608 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
609 |
|
try { |
633 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
610 |
|
Object o[] = q.toArray(null); |
611 |
|
shouldThrow(); |
612 |
|
} catch (NullPointerException success) {} |
616 |
|
* toArray with incompatible array type throws CCE |
617 |
|
*/ |
618 |
|
public void testToArray1_BadArg() { |
619 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
620 |
|
try { |
621 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
645 |
< |
Object o[] = q.toArray(new String[10] ); |
621 |
> |
Object o[] = q.toArray(new String[10]); |
622 |
|
shouldThrow(); |
623 |
|
} catch (ArrayStoreException success) {} |
624 |
|
} |
640 |
|
/** |
641 |
|
* iterator.remove removes current element |
642 |
|
*/ |
643 |
< |
public void testIteratorRemove () { |
643 |
> |
public void testIteratorRemove() { |
644 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
645 |
|
q.add(new Integer(2)); |
646 |
|
q.add(new Integer(1)); |
676 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
677 |
|
executor.execute(new CheckedRunnable() { |
678 |
|
public void realRun() throws InterruptedException { |
679 |
< |
threadAssertNull(q.poll()); |
680 |
< |
threadAssertTrue(null != q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
681 |
< |
threadAssertTrue(q.isEmpty()); |
679 |
> |
assertNull(q.poll()); |
680 |
> |
assertSame(one, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
681 |
> |
assertTrue(q.isEmpty()); |
682 |
|
}}); |
683 |
|
|
684 |
|
executor.execute(new CheckedRunnable() { |
685 |
|
public void realRun() throws InterruptedException { |
686 |
|
Thread.sleep(SMALL_DELAY_MS); |
687 |
< |
q.put(new Integer(1)); |
687 |
> |
q.put(one); |
688 |
|
}}); |
689 |
|
|
690 |
|
joinPool(executor); |
797 |
|
} |
798 |
|
|
799 |
|
/** |
800 |
< |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
800 |
> |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
801 |
|
*/ |
802 |
|
public void testDrainToN() { |
803 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE*2); |
806 |
|
assertTrue(q.offer(new Integer(j))); |
807 |
|
ArrayList l = new ArrayList(); |
808 |
|
q.drainTo(l, i); |
809 |
< |
int k = (i < SIZE)? i : SIZE; |
809 |
> |
int k = (i < SIZE) ? i : SIZE; |
810 |
|
assertEquals(l.size(), k); |
811 |
|
assertEquals(q.size(), SIZE-k); |
812 |
|
for (int j = 0; j < k; ++j) |