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. |
49 |
|
} |
50 |
|
|
51 |
|
/** |
52 |
< |
* |
52 |
> |
* A new queue has unbounded capacity |
53 |
|
*/ |
54 |
|
public void testConstructor1() { |
55 |
|
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
56 |
|
} |
57 |
|
|
58 |
|
/** |
59 |
< |
* |
59 |
> |
* Constructor throws IAE if capacity argument nonpositive |
60 |
|
*/ |
61 |
|
public void testConstructor2() { |
62 |
|
try { |
67 |
|
} |
68 |
|
|
69 |
|
/** |
70 |
< |
* |
70 |
> |
* Initializing from null Collection throws NPE |
71 |
|
*/ |
72 |
|
public void testConstructor3() { |
75 |
– |
|
73 |
|
try { |
74 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
75 |
|
shouldThrow(); |
78 |
|
} |
79 |
|
|
80 |
|
/** |
81 |
< |
* |
81 |
> |
* Initializing from Collection of null elements throws NPE |
82 |
|
*/ |
83 |
|
public void testConstructor4() { |
84 |
|
try { |
90 |
|
} |
91 |
|
|
92 |
|
/** |
93 |
< |
* |
93 |
> |
* Initializing from Collection with some null elements throws NPE |
94 |
|
*/ |
95 |
|
public void testConstructor5() { |
96 |
|
try { |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* |
107 |
> |
* Queue contains all elements of collection used to initialize |
108 |
|
*/ |
109 |
|
public void testConstructor6() { |
110 |
|
try { |
119 |
|
} |
120 |
|
|
121 |
|
/** |
122 |
< |
* |
122 |
> |
* The comparator used in constructor is used |
123 |
|
*/ |
124 |
|
public void testConstructor7() { |
125 |
|
try { |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
< |
* |
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 |
|
/** |
155 |
< |
* |
155 |
> |
* remainingCapacity does not change when elementa added or removed, |
156 |
> |
* but size does |
157 |
|
*/ |
158 |
|
public void testRemainingCapacity() { |
159 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* |
173 |
> |
* offer(null) throws NPE |
174 |
|
*/ |
175 |
|
public void testOfferNull() { |
176 |
|
try { |
181 |
|
} |
182 |
|
|
183 |
|
/** |
184 |
< |
* |
184 |
> |
* Offer of comparable element succeeds |
185 |
|
*/ |
186 |
|
public void testOffer() { |
187 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
188 |
< |
assertTrue(q.offer(new Integer(0))); |
189 |
< |
assertTrue(q.offer(new Integer(1))); |
188 |
> |
assertTrue(q.offer(zero)); |
189 |
> |
assertTrue(q.offer(one)); |
190 |
|
} |
191 |
|
|
192 |
|
/** |
193 |
< |
* |
193 |
> |
* Offer of non-Comparable throws CCE |
194 |
|
*/ |
195 |
|
public void testOfferNonComparable() { |
196 |
|
try { |
204 |
|
} |
205 |
|
|
206 |
|
/** |
207 |
< |
* |
207 |
> |
* add of comparable succeeds |
208 |
|
*/ |
209 |
|
public void testAdd() { |
210 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
< |
* |
218 |
> |
* addAll(null) throws NPE |
219 |
|
*/ |
220 |
|
public void testAddAll1() { |
221 |
|
try { |
226 |
|
catch (NullPointerException success) {} |
227 |
|
} |
228 |
|
/** |
229 |
< |
* |
229 |
> |
* addAll of a collection with null elements throws NPE |
230 |
|
*/ |
231 |
|
public void testAddAll2() { |
232 |
|
try { |
238 |
|
catch (NullPointerException success) {} |
239 |
|
} |
240 |
|
/** |
241 |
< |
* |
241 |
> |
* addAll of a collection with any null elements throws NPE after |
242 |
> |
* possibly adding some elements |
243 |
|
*/ |
244 |
|
public void testAddAll3() { |
245 |
|
try { |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* |
257 |
> |
* Queue contains all elements of successful addAll |
258 |
|
*/ |
259 |
|
public void testAddAll5() { |
260 |
|
try { |
272 |
|
} |
273 |
|
|
274 |
|
/** |
275 |
< |
* |
275 |
> |
* put(null) throws NPE |
276 |
|
*/ |
277 |
|
public void testPutNull() { |
278 |
|
try { |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* |
288 |
> |
* all elements successfully put are contained |
289 |
|
*/ |
290 |
|
public void testPut() { |
291 |
|
try { |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
< |
* |
305 |
> |
* put doesn't block waiting for take |
306 |
|
*/ |
307 |
|
public void testPutWithTake() { |
308 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* |
338 |
> |
* timed offer does not time out |
339 |
|
*/ |
340 |
|
public void testTimedOffer() { |
341 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
361 |
|
} |
362 |
|
|
363 |
|
/** |
364 |
< |
* |
364 |
> |
* take retrieves elements in priority order |
365 |
|
*/ |
366 |
|
public void testTake() { |
367 |
|
try { |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* |
378 |
> |
* take blocks interruptibly when empty |
379 |
|
*/ |
380 |
|
public void testTakeFromEmpty() { |
381 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
398 |
|
} |
399 |
|
|
400 |
|
/** |
401 |
< |
* |
401 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
402 |
|
*/ |
403 |
|
public void testBlockingTake() { |
404 |
|
Thread t = new Thread(new Runnable() { |
426 |
|
|
427 |
|
|
428 |
|
/** |
429 |
< |
* |
429 |
> |
* poll succeeds unless empty |
430 |
|
*/ |
431 |
|
public void testPoll() { |
432 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
< |
* |
440 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
441 |
|
*/ |
442 |
|
public void testTimedPoll0() { |
443 |
|
try { |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
< |
* |
455 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
456 |
|
*/ |
457 |
|
public void testTimedPoll() { |
458 |
|
try { |
467 |
|
} |
468 |
|
|
469 |
|
/** |
470 |
< |
* |
470 |
> |
* Interrupted timed poll throws InterruptedException instead of |
471 |
> |
* returning timeout status |
472 |
|
*/ |
473 |
|
public void testInterruptedTimedPoll() { |
474 |
|
Thread t = new Thread(new Runnable() { |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
< |
* |
497 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
498 |
> |
* on interruption throws |
499 |
|
*/ |
500 |
|
public void testTimedPollWithOffer() { |
501 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
522 |
|
|
523 |
|
|
524 |
|
/** |
525 |
< |
* |
525 |
> |
* peek returns next element, or null if empty |
526 |
|
*/ |
527 |
|
public void testPeek() { |
528 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
536 |
|
} |
537 |
|
|
538 |
|
/** |
539 |
< |
* |
539 |
> |
* element returns next element, or throws NSEE if empty |
540 |
|
*/ |
541 |
|
public void testElement() { |
542 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
< |
* |
555 |
> |
* remove removes next element, or throws NSEE if empty |
556 |
|
*/ |
557 |
|
public void testRemove() { |
558 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
< |
* |
570 |
> |
* remove(x) removes x and returns true if present |
571 |
|
*/ |
572 |
|
public void testRemoveElement() { |
573 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
582 |
|
} |
583 |
|
|
584 |
|
/** |
585 |
< |
* |
585 |
> |
* contains(x) reports true when elements added but not yet removed |
586 |
|
*/ |
587 |
|
public void testContains() { |
588 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
594 |
|
} |
595 |
|
|
596 |
|
/** |
597 |
< |
* |
597 |
> |
* clear removes all elements |
598 |
|
*/ |
599 |
|
public void testClear() { |
600 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
< |
* |
612 |
> |
* containsAll(c) is true when c contains a subset of elements |
613 |
|
*/ |
614 |
|
public void testContainsAll() { |
615 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
623 |
|
} |
624 |
|
|
625 |
|
/** |
626 |
< |
* |
626 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
627 |
|
*/ |
628 |
|
public void testRetainAll() { |
629 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
< |
* |
645 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
646 |
|
*/ |
647 |
|
public void testRemoveAll() { |
648 |
|
for (int i = 1; i < SIZE; ++i) { |
658 |
|
} |
659 |
|
|
660 |
|
/** |
661 |
< |
* |
661 |
> |
* toArray contains all elements |
662 |
|
*/ |
663 |
|
public void testToArray() { |
664 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
673 |
|
} |
674 |
|
|
675 |
|
/** |
676 |
< |
* |
676 |
> |
* toArray(a) contains all elements |
677 |
|
*/ |
678 |
|
public void testToArray2() { |
679 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
689 |
|
} |
690 |
|
|
691 |
|
/** |
692 |
< |
* |
692 |
> |
* iterator iterates through all elements |
693 |
|
*/ |
694 |
|
public void testIterator() { |
695 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
703 |
|
} |
704 |
|
|
705 |
|
/** |
706 |
< |
* |
706 |
> |
* iterator.remove removes current element |
707 |
|
*/ |
708 |
|
public void testIteratorRemove () { |
709 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
723 |
|
|
724 |
|
|
725 |
|
/** |
726 |
< |
* |
726 |
> |
* toString contains toStrings of elements |
727 |
|
*/ |
728 |
|
public void testToString() { |
729 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
734 |
|
} |
735 |
|
|
736 |
|
/** |
737 |
< |
* |
737 |
> |
* offer transfers elements across Executor tasks |
738 |
|
*/ |
739 |
|
public void testPollInExecutor() { |
739 |
– |
|
740 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
741 |
– |
|
741 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
743 |
– |
|
742 |
|
executor.execute(new Runnable() { |
743 |
|
public void run() { |
744 |
|
threadAssertNull(q.poll()); |
765 |
|
}); |
766 |
|
|
767 |
|
joinPool(executor); |
770 |
– |
|
768 |
|
} |
769 |
|
|
770 |
|
/** |
771 |
< |
* |
771 |
> |
* A deserialized serialized queue has same elements |
772 |
|
*/ |
773 |
|
public void testSerialization() { |
774 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |