25 |
|
/** Sample Comparator */ |
26 |
|
static class MyReverseComparator implements Comparator { |
27 |
|
public int compare(Object x, Object y) { |
28 |
< |
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; |
28 |
> |
return ((Comparable)y).compareTo(x); |
29 |
|
} |
30 |
|
} |
31 |
|
|
328 |
|
public void realRun() { |
329 |
|
q.put(new Integer(0)); |
330 |
|
q.put(new Integer(0)); |
331 |
< |
threadAssertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
332 |
< |
threadAssertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
331 |
> |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
332 |
> |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
333 |
|
}}); |
334 |
|
|
335 |
|
t.start(); |
344 |
|
public void testTake() throws InterruptedException { |
345 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
346 |
|
for (int i = 0; i < SIZE; ++i) { |
347 |
< |
assertEquals(i, ((Integer)q.take()).intValue()); |
347 |
> |
assertEquals(i, q.take()); |
348 |
|
} |
349 |
|
} |
350 |
|
|
368 |
|
* Take removes existing elements until empty, then blocks interruptibly |
369 |
|
*/ |
370 |
|
public void testBlockingTake() throws InterruptedException { |
371 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
371 |
> |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
372 |
> |
Thread t = new Thread(new CheckedRunnable() { |
373 |
|
public void realRun() throws InterruptedException { |
377 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
374 |
|
for (int i = 0; i < SIZE; ++i) { |
375 |
< |
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
375 |
> |
assertEquals(i, q.take()); |
376 |
|
} |
377 |
< |
q.take(); |
377 |
> |
try { |
378 |
> |
q.take(); |
379 |
> |
shouldThrow(); |
380 |
> |
} catch (InterruptedException success) {} |
381 |
|
}}); |
382 |
|
|
383 |
|
t.start(); |
393 |
|
public void testPoll() { |
394 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
395 |
|
for (int i = 0; i < SIZE; ++i) { |
396 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
396 |
> |
assertEquals(i, q.poll()); |
397 |
|
} |
398 |
|
assertNull(q.poll()); |
399 |
|
} |
404 |
|
public void testTimedPoll0() throws InterruptedException { |
405 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
406 |
|
for (int i = 0; i < SIZE; ++i) { |
407 |
< |
assertEquals(i, ((Integer)q.poll(0, MILLISECONDS)).intValue()); |
407 |
> |
assertEquals(i, q.poll(0, MILLISECONDS)); |
408 |
|
} |
409 |
|
assertNull(q.poll(0, MILLISECONDS)); |
410 |
|
} |
415 |
|
public void testTimedPoll() throws InterruptedException { |
416 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
417 |
|
for (int i = 0; i < SIZE; ++i) { |
418 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
418 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
419 |
|
} |
420 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
421 |
|
} |
429 |
|
public void realRun() throws InterruptedException { |
430 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
431 |
|
for (int i = 0; i < SIZE; ++i) { |
432 |
< |
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, MILLISECONDS)).intValue()); |
432 |
> |
assertEquals(i, q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
433 |
|
} |
434 |
|
try { |
435 |
< |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
435 |
> |
q.poll(SMALL_DELAY_MS, MILLISECONDS); |
436 |
|
shouldThrow(); |
437 |
|
} catch (InterruptedException success) {} |
438 |
|
}}); |
452 |
|
Thread t = new Thread(new CheckedRunnable() { |
453 |
|
public void realRun() throws InterruptedException { |
454 |
|
assertNull(q.poll(SHORT_DELAY_MS, MILLISECONDS)); |
455 |
< |
assertEquals(0, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
455 |
> |
assertSame(zero, q.poll(MEDIUM_DELAY_MS, MILLISECONDS)); |
456 |
|
try { |
457 |
|
q.poll(LONG_DELAY_MS, MILLISECONDS); |
458 |
< |
threadShouldThrow(); |
458 |
> |
shouldThrow(); |
459 |
|
} catch (InterruptedException success) {} |
460 |
|
}}); |
461 |
|
|
462 |
|
t.start(); |
463 |
|
Thread.sleep(SMALL_DELAY_MS); |
464 |
< |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
464 |
> |
assertTrue(q.offer(zero, SHORT_DELAY_MS, MILLISECONDS)); |
465 |
|
t.interrupt(); |
466 |
|
t.join(); |
467 |
|
} |
473 |
|
public void testPeek() { |
474 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
475 |
|
for (int i = 0; i < SIZE; ++i) { |
476 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
477 |
< |
q.poll(); |
476 |
> |
assertEquals(i, q.peek()); |
477 |
> |
assertEquals(i, q.poll()); |
478 |
|
assertTrue(q.peek() == null || |
479 |
< |
i != ((Integer)q.peek()).intValue()); |
479 |
> |
!q.peek().equals(i)); |
480 |
|
} |
481 |
|
assertNull(q.peek()); |
482 |
|
} |
487 |
|
public void testElement() { |
488 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
489 |
|
for (int i = 0; i < SIZE; ++i) { |
490 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
491 |
< |
q.poll(); |
490 |
> |
assertEquals(i, q.element()); |
491 |
> |
assertEquals(i, q.poll()); |
492 |
|
} |
493 |
|
try { |
494 |
|
q.element(); |
502 |
|
public void testRemove() { |
503 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
504 |
|
for (int i = 0; i < SIZE; ++i) { |
505 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
505 |
> |
assertEquals(i, q.remove()); |
506 |
|
} |
507 |
|
try { |
508 |
|
q.remove(); |
628 |
|
* toArray(null) throws NPE |
629 |
|
*/ |
630 |
|
public void testToArray_BadArg() { |
631 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
632 |
|
try { |
633 |
– |
PriorityBlockingQueue q = populatedQueue(SIZE); |
633 |
|
Object o[] = q.toArray(null); |
634 |
|
shouldThrow(); |
635 |
|
} catch (NullPointerException success) {} |
639 |
|
* toArray with incompatible array type throws CCE |
640 |
|
*/ |
641 |
|
public void testToArray1_BadArg() { |
642 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
643 |
|
try { |
644 |
< |
PriorityBlockingQueue q = populatedQueue(SIZE); |
645 |
< |
Object o[] = q.toArray(new String[10] ); |
644 |
> |
Object o[] = q.toArray(new String[10]); |
645 |
|
shouldThrow(); |
646 |
|
} catch (ArrayStoreException success) {} |
647 |
|
} |