14 |
|
public static void main(String[] args) { |
15 |
|
junit.textui.TestRunner.run (suite()); |
16 |
|
} |
17 |
– |
|
17 |
|
public static Test suite() { |
18 |
|
return new TestSuite(PriorityBlockingQueueTest.class); |
19 |
|
} |
31 |
|
} |
32 |
|
} |
33 |
|
|
35 |
– |
|
34 |
|
/** |
35 |
|
* Create a queue of given size containing consecutive |
36 |
|
* Integers 0 ... n. |
48 |
|
return q; |
49 |
|
} |
50 |
|
|
51 |
< |
public void testConstructor1(){ |
51 |
> |
/** |
52 |
> |
* A new queue has unbounded capacity |
53 |
> |
*/ |
54 |
> |
public void testConstructor1() { |
55 |
|
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
56 |
|
} |
57 |
|
|
58 |
< |
public void testConstructor2(){ |
58 |
> |
/** |
59 |
> |
* Constructor throws IAE if capacity argument nonpositive |
60 |
> |
*/ |
61 |
> |
public void testConstructor2() { |
62 |
|
try { |
63 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
64 |
< |
fail("Cannot make zero-sized"); |
64 |
> |
shouldThrow(); |
65 |
|
} |
66 |
|
catch (IllegalArgumentException success) {} |
67 |
|
} |
68 |
|
|
69 |
< |
public void testConstructor3(){ |
70 |
< |
|
69 |
> |
/** |
70 |
> |
* Initializing from null Collection throws NPE |
71 |
> |
*/ |
72 |
> |
public void testConstructor3() { |
73 |
|
try { |
74 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
75 |
< |
fail("Cannot make from null collection"); |
75 |
> |
shouldThrow(); |
76 |
|
} |
77 |
|
catch (NullPointerException success) {} |
78 |
|
} |
79 |
|
|
80 |
< |
public void testConstructor4(){ |
80 |
> |
/** |
81 |
> |
* Initializing from Collection of null elements throws NPE |
82 |
> |
*/ |
83 |
> |
public void testConstructor4() { |
84 |
|
try { |
85 |
|
Integer[] ints = new Integer[SIZE]; |
86 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
87 |
< |
fail("Cannot make with null elements"); |
87 |
> |
shouldThrow(); |
88 |
|
} |
89 |
|
catch (NullPointerException success) {} |
90 |
|
} |
91 |
|
|
92 |
< |
public void testConstructor5(){ |
92 |
> |
/** |
93 |
> |
* Initializing from Collection with some null elements throws NPE |
94 |
> |
*/ |
95 |
> |
public void testConstructor5() { |
96 |
|
try { |
97 |
|
Integer[] ints = new Integer[SIZE]; |
98 |
|
for (int i = 0; i < SIZE-1; ++i) |
99 |
|
ints[i] = new Integer(i); |
100 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
101 |
< |
fail("Cannot make with null elements"); |
101 |
> |
shouldThrow(); |
102 |
|
} |
103 |
|
catch (NullPointerException success) {} |
104 |
|
} |
105 |
|
|
106 |
< |
public void testConstructor6(){ |
106 |
> |
/** |
107 |
> |
* Queue contains all elements of collection used to initialize |
108 |
> |
*/ |
109 |
> |
public void testConstructor6() { |
110 |
|
try { |
111 |
|
Integer[] ints = new Integer[SIZE]; |
112 |
|
for (int i = 0; i < SIZE; ++i) |
118 |
|
finally {} |
119 |
|
} |
120 |
|
|
121 |
< |
public void testConstructor7(){ |
121 |
> |
/** |
122 |
> |
* The comparator used in constructor is used |
123 |
> |
*/ |
124 |
> |
public void testConstructor7() { |
125 |
|
try { |
126 |
|
MyReverseComparator cmp = new MyReverseComparator(); |
127 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
136 |
|
finally {} |
137 |
|
} |
138 |
|
|
139 |
+ |
/** |
140 |
+ |
* isEmpty is true before add, false after |
141 |
+ |
*/ |
142 |
|
public void testEmpty() { |
143 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
144 |
|
assertTrue(q.isEmpty()); |
145 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
146 |
< |
q.add(new Integer(1)); |
146 |
> |
q.add(one); |
147 |
|
assertFalse(q.isEmpty()); |
148 |
< |
q.add(new Integer(2)); |
148 |
> |
q.add(two); |
149 |
|
q.remove(); |
150 |
|
q.remove(); |
151 |
|
assertTrue(q.isEmpty()); |
152 |
|
} |
153 |
|
|
154 |
< |
public void testRemainingCapacity(){ |
154 |
> |
/** |
155 |
> |
* remainingCapacity does not change when elementa added or removed, |
156 |
> |
* but size does |
157 |
> |
*/ |
158 |
> |
public void testRemainingCapacity() { |
159 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
160 |
|
for (int i = 0; i < SIZE; ++i) { |
161 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
169 |
|
} |
170 |
|
} |
171 |
|
|
172 |
< |
public void testOfferNull(){ |
172 |
> |
/** |
173 |
> |
* offer(null) throws NPE |
174 |
> |
*/ |
175 |
> |
public void testOfferNull() { |
176 |
|
try { |
177 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
178 |
|
q.offer(null); |
179 |
< |
fail("should throw NPE"); |
179 |
> |
shouldThrow(); |
180 |
|
} catch (NullPointerException success) { } |
181 |
|
} |
182 |
|
|
183 |
+ |
/** |
184 |
+ |
* add(null) throws NPE |
185 |
+ |
*/ |
186 |
+ |
public void testAddNull() { |
187 |
+ |
try { |
188 |
+ |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
189 |
+ |
q.add(null); |
190 |
+ |
shouldThrow(); |
191 |
+ |
} catch (NullPointerException success) { } |
192 |
+ |
} |
193 |
+ |
|
194 |
+ |
/** |
195 |
+ |
* Offer of comparable element succeeds |
196 |
+ |
*/ |
197 |
|
public void testOffer() { |
198 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
199 |
< |
assertTrue(q.offer(new Integer(0))); |
200 |
< |
assertTrue(q.offer(new Integer(1))); |
199 |
> |
assertTrue(q.offer(zero)); |
200 |
> |
assertTrue(q.offer(one)); |
201 |
|
} |
202 |
|
|
203 |
+ |
/** |
204 |
+ |
* Offer of non-Comparable throws CCE |
205 |
+ |
*/ |
206 |
|
public void testOfferNonComparable() { |
207 |
|
try { |
208 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
209 |
|
q.offer(new Object()); |
210 |
|
q.offer(new Object()); |
211 |
|
q.offer(new Object()); |
212 |
< |
fail("should throw CCE"); |
212 |
> |
shouldThrow(); |
213 |
|
} |
214 |
|
catch(ClassCastException success) {} |
215 |
|
} |
216 |
|
|
217 |
< |
public void testAdd(){ |
217 |
> |
/** |
218 |
> |
* add of comparable succeeds |
219 |
> |
*/ |
220 |
> |
public void testAdd() { |
221 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
222 |
|
for (int i = 0; i < SIZE; ++i) { |
223 |
|
assertEquals(i, q.size()); |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
< |
public void testAddAll1(){ |
228 |
> |
/** |
229 |
> |
* addAll(null) throws NPE |
230 |
> |
*/ |
231 |
> |
public void testAddAll1() { |
232 |
|
try { |
233 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
234 |
|
q.addAll(null); |
235 |
< |
fail("Cannot add null collection"); |
235 |
> |
shouldThrow(); |
236 |
|
} |
237 |
|
catch (NullPointerException success) {} |
238 |
|
} |
239 |
< |
public void testAddAll2(){ |
239 |
> |
|
240 |
> |
/** |
241 |
> |
* addAll(this) throws IAE |
242 |
> |
*/ |
243 |
> |
public void testAddAllSelf() { |
244 |
> |
try { |
245 |
> |
PriorityBlockingQueue q = populatedQueue(SIZE); |
246 |
> |
q.addAll(q); |
247 |
> |
shouldThrow(); |
248 |
> |
} |
249 |
> |
catch (IllegalArgumentException success) {} |
250 |
> |
} |
251 |
> |
|
252 |
> |
/** |
253 |
> |
* addAll of a collection with null elements throws NPE |
254 |
> |
*/ |
255 |
> |
public void testAddAll2() { |
256 |
|
try { |
257 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
258 |
|
Integer[] ints = new Integer[SIZE]; |
259 |
|
q.addAll(Arrays.asList(ints)); |
260 |
< |
fail("Cannot add null elements"); |
260 |
> |
shouldThrow(); |
261 |
|
} |
262 |
|
catch (NullPointerException success) {} |
263 |
|
} |
264 |
< |
public void testAddAll3(){ |
264 |
> |
/** |
265 |
> |
* addAll of a collection with any null elements throws NPE after |
266 |
> |
* possibly adding some elements |
267 |
> |
*/ |
268 |
> |
public void testAddAll3() { |
269 |
|
try { |
270 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
271 |
|
Integer[] ints = new Integer[SIZE]; |
272 |
|
for (int i = 0; i < SIZE-1; ++i) |
273 |
|
ints[i] = new Integer(i); |
274 |
|
q.addAll(Arrays.asList(ints)); |
275 |
< |
fail("Cannot add null elements"); |
275 |
> |
shouldThrow(); |
276 |
|
} |
277 |
|
catch (NullPointerException success) {} |
278 |
|
} |
279 |
|
|
280 |
< |
public void testAddAll5(){ |
280 |
> |
/** |
281 |
> |
* Queue contains all elements of successful addAll |
282 |
> |
*/ |
283 |
> |
public void testAddAll5() { |
284 |
|
try { |
285 |
|
Integer[] empty = new Integer[0]; |
286 |
|
Integer[] ints = new Integer[SIZE]; |
295 |
|
finally {} |
296 |
|
} |
297 |
|
|
298 |
+ |
/** |
299 |
+ |
* put(null) throws NPE |
300 |
+ |
*/ |
301 |
|
public void testPutNull() { |
302 |
|
try { |
303 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
304 |
|
q.put(null); |
305 |
< |
fail("put should throw NPE"); |
305 |
> |
shouldThrow(); |
306 |
|
} |
307 |
|
catch (NullPointerException success){ |
308 |
|
} |
309 |
|
} |
310 |
|
|
311 |
+ |
/** |
312 |
+ |
* all elements successfully put are contained |
313 |
+ |
*/ |
314 |
|
public void testPut() { |
315 |
|
try { |
316 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
325 |
|
} |
326 |
|
} |
327 |
|
|
328 |
+ |
/** |
329 |
+ |
* put doesn't block waiting for take |
330 |
+ |
*/ |
331 |
|
public void testPutWithTake() { |
332 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
333 |
|
Thread t = new Thread(new Runnable() { |
334 |
< |
public void run(){ |
334 |
> |
public void run() { |
335 |
|
int added = 0; |
336 |
|
try { |
337 |
|
q.put(new Integer(0)); |
354 |
|
t.interrupt(); |
355 |
|
t.join(); |
356 |
|
} catch (Exception e){ |
357 |
< |
fail("Unexpected exception"); |
357 |
> |
unexpectedException(); |
358 |
|
} |
359 |
|
} |
360 |
|
|
361 |
+ |
/** |
362 |
+ |
* timed offer does not time out |
363 |
+ |
*/ |
364 |
|
public void testTimedOffer() { |
365 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
366 |
|
Thread t = new Thread(new Runnable() { |
367 |
< |
public void run(){ |
367 |
> |
public void run() { |
368 |
|
try { |
369 |
|
q.put(new Integer(0)); |
370 |
|
q.put(new Integer(0)); |
380 |
|
t.interrupt(); |
381 |
|
t.join(); |
382 |
|
} catch (Exception e){ |
383 |
< |
fail("Unexpected exception"); |
383 |
> |
unexpectedException(); |
384 |
|
} |
385 |
|
} |
386 |
|
|
387 |
< |
public void testTake(){ |
387 |
> |
/** |
388 |
> |
* take retrieves elements in priority order |
389 |
> |
*/ |
390 |
> |
public void testTake() { |
391 |
|
try { |
392 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
393 |
|
for (int i = 0; i < SIZE; ++i) { |
394 |
|
assertEquals(i, ((Integer)q.take()).intValue()); |
395 |
|
} |
396 |
|
} catch (InterruptedException e){ |
397 |
< |
fail("Unexpected exception"); |
397 |
> |
unexpectedException(); |
398 |
|
} |
399 |
|
} |
400 |
|
|
401 |
+ |
/** |
402 |
+ |
* take blocks interruptibly when empty |
403 |
+ |
*/ |
404 |
|
public void testTakeFromEmpty() { |
405 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
406 |
|
Thread t = new Thread(new Runnable() { |
407 |
< |
public void run(){ |
407 |
> |
public void run() { |
408 |
|
try { |
409 |
|
q.take(); |
410 |
< |
threadFail("Should block"); |
410 |
> |
threadShouldThrow(); |
411 |
|
} catch (InterruptedException success){ } |
412 |
|
} |
413 |
|
}); |
417 |
|
t.interrupt(); |
418 |
|
t.join(); |
419 |
|
} catch (Exception e){ |
420 |
< |
fail("Unexpected exception"); |
420 |
> |
unexpectedException(); |
421 |
|
} |
422 |
|
} |
423 |
|
|
424 |
< |
public void testBlockingTake(){ |
424 |
> |
/** |
425 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
426 |
> |
*/ |
427 |
> |
public void testBlockingTake() { |
428 |
|
Thread t = new Thread(new Runnable() { |
429 |
|
public void run() { |
430 |
|
try { |
433 |
|
threadAssertEquals(i, ((Integer)q.take()).intValue()); |
434 |
|
} |
435 |
|
q.take(); |
436 |
< |
threadFail("take should block"); |
436 |
> |
threadShouldThrow(); |
437 |
|
} catch (InterruptedException success){ |
438 |
|
} |
439 |
|
}}); |
444 |
|
t.join(); |
445 |
|
} |
446 |
|
catch (InterruptedException ie) { |
447 |
< |
fail("Unexpected exception"); |
447 |
> |
unexpectedException(); |
448 |
|
} |
449 |
|
} |
450 |
|
|
451 |
|
|
452 |
< |
public void testPoll(){ |
452 |
> |
/** |
453 |
> |
* poll succeeds unless empty |
454 |
> |
*/ |
455 |
> |
public void testPoll() { |
456 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
457 |
|
for (int i = 0; i < SIZE; ++i) { |
458 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
460 |
|
assertNull(q.poll()); |
461 |
|
} |
462 |
|
|
463 |
+ |
/** |
464 |
+ |
* timed pool with zero timeout succeeds when non-empty, else times out |
465 |
+ |
*/ |
466 |
|
public void testTimedPoll0() { |
467 |
|
try { |
468 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
471 |
|
} |
472 |
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
473 |
|
} catch (InterruptedException e){ |
474 |
< |
fail("Unexpected exception"); |
474 |
> |
unexpectedException(); |
475 |
|
} |
476 |
|
} |
477 |
|
|
478 |
+ |
/** |
479 |
+ |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
480 |
+ |
*/ |
481 |
|
public void testTimedPoll() { |
482 |
|
try { |
483 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
486 |
|
} |
487 |
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
488 |
|
} catch (InterruptedException e){ |
489 |
< |
fail("Unexpected exception"); |
489 |
> |
unexpectedException(); |
490 |
|
} |
491 |
|
} |
492 |
|
|
493 |
< |
public void testInterruptedTimedPoll(){ |
493 |
> |
/** |
494 |
> |
* Interrupted timed poll throws InterruptedException instead of |
495 |
> |
* returning timeout status |
496 |
> |
*/ |
497 |
> |
public void testInterruptedTimedPoll() { |
498 |
|
Thread t = new Thread(new Runnable() { |
499 |
|
public void run() { |
500 |
|
try { |
513 |
|
t.join(); |
514 |
|
} |
515 |
|
catch (InterruptedException ie) { |
516 |
< |
fail("Unexpected exception"); |
516 |
> |
unexpectedException(); |
517 |
|
} |
518 |
|
} |
519 |
|
|
520 |
< |
public void testTimedPollWithOffer(){ |
520 |
> |
/** |
521 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
522 |
> |
* on interruption throws |
523 |
> |
*/ |
524 |
> |
public void testTimedPollWithOffer() { |
525 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
526 |
|
Thread t = new Thread(new Runnable() { |
527 |
< |
public void run(){ |
527 |
> |
public void run() { |
528 |
|
try { |
529 |
|
threadAssertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
530 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
531 |
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
532 |
< |
threadFail("Should block"); |
532 |
> |
threadShouldThrow(); |
533 |
|
} catch (InterruptedException success) { } |
534 |
|
} |
535 |
|
}); |
540 |
|
t.interrupt(); |
541 |
|
t.join(); |
542 |
|
} catch (Exception e){ |
543 |
< |
fail("Unexpected exception"); |
543 |
> |
unexpectedException(); |
544 |
|
} |
545 |
|
} |
546 |
|
|
547 |
|
|
548 |
< |
public void testPeek(){ |
548 |
> |
/** |
549 |
> |
* peek returns next element, or null if empty |
550 |
> |
*/ |
551 |
> |
public void testPeek() { |
552 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
553 |
|
for (int i = 0; i < SIZE; ++i) { |
554 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
559 |
|
assertNull(q.peek()); |
560 |
|
} |
561 |
|
|
562 |
< |
public void testElement(){ |
562 |
> |
/** |
563 |
> |
* element returns next element, or throws NSEE if empty |
564 |
> |
*/ |
565 |
> |
public void testElement() { |
566 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
567 |
|
for (int i = 0; i < SIZE; ++i) { |
568 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
570 |
|
} |
571 |
|
try { |
572 |
|
q.element(); |
573 |
< |
fail("no such element"); |
573 |
> |
shouldThrow(); |
574 |
|
} |
575 |
|
catch (NoSuchElementException success) {} |
576 |
|
} |
577 |
|
|
578 |
< |
public void testRemove(){ |
578 |
> |
/** |
579 |
> |
* remove removes next element, or throws NSEE if empty |
580 |
> |
*/ |
581 |
> |
public void testRemove() { |
582 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
583 |
|
for (int i = 0; i < SIZE; ++i) { |
584 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
585 |
|
} |
586 |
|
try { |
587 |
|
q.remove(); |
588 |
< |
fail("remove should throw"); |
588 |
> |
shouldThrow(); |
589 |
|
} catch (NoSuchElementException success){ |
590 |
|
} |
591 |
|
} |
592 |
|
|
593 |
< |
public void testRemoveElement(){ |
593 |
> |
/** |
594 |
> |
* remove(x) removes x and returns true if present |
595 |
> |
*/ |
596 |
> |
public void testRemoveElement() { |
597 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
598 |
|
for (int i = 1; i < SIZE; i+=2) { |
599 |
|
assertTrue(q.remove(new Integer(i))); |
605 |
|
assertTrue(q.isEmpty()); |
606 |
|
} |
607 |
|
|
608 |
< |
public void testContains(){ |
608 |
> |
/** |
609 |
> |
* contains(x) reports true when elements added but not yet removed |
610 |
> |
*/ |
611 |
> |
public void testContains() { |
612 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
613 |
|
for (int i = 0; i < SIZE; ++i) { |
614 |
|
assertTrue(q.contains(new Integer(i))); |
617 |
|
} |
618 |
|
} |
619 |
|
|
620 |
< |
public void testClear(){ |
620 |
> |
/** |
621 |
> |
* clear removes all elements |
622 |
> |
*/ |
623 |
> |
public void testClear() { |
624 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
625 |
|
q.clear(); |
626 |
|
assertTrue(q.isEmpty()); |
632 |
|
assertTrue(q.isEmpty()); |
633 |
|
} |
634 |
|
|
635 |
< |
public void testContainsAll(){ |
635 |
> |
/** |
636 |
> |
* containsAll(c) is true when c contains a subset of elements |
637 |
> |
*/ |
638 |
> |
public void testContainsAll() { |
639 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
640 |
|
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
641 |
|
for (int i = 0; i < SIZE; ++i) { |
646 |
|
assertTrue(p.containsAll(q)); |
647 |
|
} |
648 |
|
|
649 |
< |
public void testRetainAll(){ |
649 |
> |
/** |
650 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
651 |
> |
*/ |
652 |
> |
public void testRetainAll() { |
653 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
654 |
|
PriorityBlockingQueue p = populatedQueue(SIZE); |
655 |
|
for (int i = 0; i < SIZE; ++i) { |
665 |
|
} |
666 |
|
} |
667 |
|
|
668 |
< |
public void testRemoveAll(){ |
668 |
> |
/** |
669 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
670 |
> |
*/ |
671 |
> |
public void testRemoveAll() { |
672 |
|
for (int i = 1; i < SIZE; ++i) { |
673 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
674 |
|
PriorityBlockingQueue p = populatedQueue(i); |
681 |
|
} |
682 |
|
} |
683 |
|
|
684 |
< |
public void testToArray(){ |
684 |
> |
/** |
685 |
> |
* toArray contains all elements |
686 |
> |
*/ |
687 |
> |
public void testToArray() { |
688 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
689 |
|
Object[] o = q.toArray(); |
690 |
|
Arrays.sort(o); |
692 |
|
for(int i = 0; i < o.length; i++) |
693 |
|
assertEquals(o[i], q.take()); |
694 |
|
} catch (InterruptedException e){ |
695 |
< |
fail("Unexpected exception"); |
695 |
> |
unexpectedException(); |
696 |
|
} |
697 |
|
} |
698 |
|
|
699 |
< |
public void testToArray2(){ |
699 |
> |
/** |
700 |
> |
* toArray(a) contains all elements |
701 |
> |
*/ |
702 |
> |
public void testToArray2() { |
703 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
704 |
|
Integer[] ints = new Integer[SIZE]; |
705 |
|
ints = (Integer[])q.toArray(ints); |
708 |
|
for(int i = 0; i < ints.length; i++) |
709 |
|
assertEquals(ints[i], q.take()); |
710 |
|
} catch (InterruptedException e){ |
711 |
< |
fail("Unexpected exception"); |
711 |
> |
unexpectedException(); |
712 |
|
} |
713 |
|
} |
714 |
+ |
|
715 |
+ |
/** |
716 |
+ |
* toArray(null) throws NPE |
717 |
+ |
*/ |
718 |
+ |
public void testToArray_BadArg() { |
719 |
+ |
try { |
720 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
721 |
+ |
Object o[] = q.toArray(null); |
722 |
+ |
shouldThrow(); |
723 |
+ |
} catch(NullPointerException success){} |
724 |
+ |
} |
725 |
+ |
|
726 |
+ |
/** |
727 |
+ |
* toArray with incompatable array type throws CCE |
728 |
+ |
*/ |
729 |
+ |
public void testToArray1_BadArg() { |
730 |
+ |
try { |
731 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
732 |
+ |
Object o[] = q.toArray(new String[10] ); |
733 |
+ |
shouldThrow(); |
734 |
+ |
} catch(ArrayStoreException success){} |
735 |
+ |
} |
736 |
|
|
737 |
< |
public void testIterator(){ |
737 |
> |
/** |
738 |
> |
* iterator iterates through all elements |
739 |
> |
*/ |
740 |
> |
public void testIterator() { |
741 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
742 |
|
int i = 0; |
743 |
|
Iterator it = q.iterator(); |
748 |
|
assertEquals(i, SIZE); |
749 |
|
} |
750 |
|
|
751 |
+ |
/** |
752 |
+ |
* iterator.remove removes current element |
753 |
+ |
*/ |
754 |
|
public void testIteratorRemove () { |
582 |
– |
|
755 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
584 |
– |
|
756 |
|
q.add(new Integer(2)); |
757 |
|
q.add(new Integer(1)); |
758 |
|
q.add(new Integer(3)); |
768 |
|
} |
769 |
|
|
770 |
|
|
771 |
< |
public void testToString(){ |
771 |
> |
/** |
772 |
> |
* toString contains toStrings of elements |
773 |
> |
*/ |
774 |
> |
public void testToString() { |
775 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
776 |
|
String s = q.toString(); |
777 |
|
for (int i = 0; i < SIZE; ++i) { |
779 |
|
} |
780 |
|
} |
781 |
|
|
782 |
+ |
/** |
783 |
+ |
* offer transfers elements across Executor tasks |
784 |
+ |
*/ |
785 |
|
public void testPollInExecutor() { |
609 |
– |
|
786 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
611 |
– |
|
787 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
613 |
– |
|
788 |
|
executor.execute(new Runnable() { |
789 |
|
public void run() { |
790 |
|
threadAssertNull(q.poll()); |
793 |
|
threadAssertTrue(q.isEmpty()); |
794 |
|
} |
795 |
|
catch (InterruptedException e) { |
796 |
< |
threadFail("should not be interrupted"); |
796 |
> |
threadUnexpectedException(); |
797 |
|
} |
798 |
|
} |
799 |
|
}); |
805 |
|
q.put(new Integer(1)); |
806 |
|
} |
807 |
|
catch (InterruptedException e) { |
808 |
< |
threadFail("should not be interrupted"); |
808 |
> |
threadUnexpectedException(); |
809 |
|
} |
810 |
|
} |
811 |
|
}); |
812 |
|
|
813 |
|
joinPool(executor); |
640 |
– |
|
814 |
|
} |
815 |
|
|
816 |
+ |
/** |
817 |
+ |
* A deserialized serialized queue has same elements |
818 |
+ |
*/ |
819 |
|
public void testSerialization() { |
820 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
821 |
|
try { |
831 |
|
while (!q.isEmpty()) |
832 |
|
assertEquals(q.remove(), r.remove()); |
833 |
|
} catch(Exception e){ |
834 |
< |
fail("unexpected exception"); |
834 |
> |
unexpectedException(); |
835 |
|
} |
836 |
|
} |
837 |
|
|
838 |
+ |
/** |
839 |
+ |
* drainTo(null) throws NPE |
840 |
+ |
*/ |
841 |
+ |
public void testDrainToNull() { |
842 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
843 |
+ |
try { |
844 |
+ |
q.drainTo(null); |
845 |
+ |
shouldThrow(); |
846 |
+ |
} catch(NullPointerException success) { |
847 |
+ |
} |
848 |
+ |
} |
849 |
+ |
|
850 |
+ |
/** |
851 |
+ |
* drainTo(this) throws IAE |
852 |
+ |
*/ |
853 |
+ |
public void testDrainToSelf() { |
854 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
855 |
+ |
try { |
856 |
+ |
q.drainTo(q); |
857 |
+ |
shouldThrow(); |
858 |
+ |
} catch(IllegalArgumentException success) { |
859 |
+ |
} |
860 |
+ |
} |
861 |
+ |
|
862 |
+ |
/** |
863 |
+ |
* drainTo(c) empties queue into another collection c |
864 |
+ |
*/ |
865 |
+ |
public void testDrainTo() { |
866 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
867 |
+ |
ArrayList l = new ArrayList(); |
868 |
+ |
q.drainTo(l); |
869 |
+ |
assertEquals(q.size(), 0); |
870 |
+ |
assertEquals(l.size(), SIZE); |
871 |
+ |
for (int i = 0; i < SIZE; ++i) |
872 |
+ |
assertEquals(l.get(i), new Integer(i)); |
873 |
+ |
} |
874 |
+ |
|
875 |
+ |
/** |
876 |
+ |
* drainTo empties queue |
877 |
+ |
*/ |
878 |
+ |
public void testDrainToWithActivePut() { |
879 |
+ |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
880 |
+ |
Thread t = new Thread(new Runnable() { |
881 |
+ |
public void run() { |
882 |
+ |
q.put(new Integer(SIZE+1)); |
883 |
+ |
} |
884 |
+ |
}); |
885 |
+ |
try { |
886 |
+ |
t.start(); |
887 |
+ |
ArrayList l = new ArrayList(); |
888 |
+ |
q.drainTo(l); |
889 |
+ |
assertTrue(l.size() >= SIZE); |
890 |
+ |
for (int i = 0; i < SIZE; ++i) |
891 |
+ |
assertEquals(l.get(i), new Integer(i)); |
892 |
+ |
t.join(); |
893 |
+ |
assertTrue(q.size() + l.size() == SIZE+1); |
894 |
+ |
} catch(Exception e){ |
895 |
+ |
unexpectedException(); |
896 |
+ |
} |
897 |
+ |
} |
898 |
+ |
|
899 |
+ |
/** |
900 |
+ |
* drainTo(null, n) throws NPE |
901 |
+ |
*/ |
902 |
+ |
public void testDrainToNullN() { |
903 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
904 |
+ |
try { |
905 |
+ |
q.drainTo(null, 0); |
906 |
+ |
shouldThrow(); |
907 |
+ |
} catch(NullPointerException success) { |
908 |
+ |
} |
909 |
+ |
} |
910 |
+ |
|
911 |
+ |
/** |
912 |
+ |
* drainTo(this, n) throws IAE |
913 |
+ |
*/ |
914 |
+ |
public void testDrainToSelfN() { |
915 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
916 |
+ |
try { |
917 |
+ |
q.drainTo(q, 0); |
918 |
+ |
shouldThrow(); |
919 |
+ |
} catch(IllegalArgumentException success) { |
920 |
+ |
} |
921 |
+ |
} |
922 |
+ |
|
923 |
+ |
/** |
924 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
925 |
+ |
*/ |
926 |
+ |
public void testDrainToN() { |
927 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
928 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
929 |
+ |
ArrayList l = new ArrayList(); |
930 |
+ |
q.drainTo(l, i); |
931 |
+ |
int k = (i < SIZE)? i : SIZE; |
932 |
+ |
assertEquals(q.size(), SIZE-k); |
933 |
+ |
assertEquals(l.size(), k); |
934 |
+ |
for (int j = 0; j < k; ++j) |
935 |
+ |
assertTrue(l.contains(new Integer(j))); |
936 |
+ |
} |
937 |
+ |
} |
938 |
+ |
|
939 |
+ |
|
940 |
|
} |