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 |
> |
* 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 |
< |
* |
204 |
> |
* Offer of non-Comparable throws CCE |
205 |
|
*/ |
206 |
|
public void testOfferNonComparable() { |
207 |
|
try { |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
< |
* |
218 |
> |
* add of comparable succeeds |
219 |
|
*/ |
220 |
|
public void testAdd() { |
221 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
226 |
|
} |
227 |
|
|
228 |
|
/** |
229 |
< |
* |
229 |
> |
* addAll(null) throws NPE |
230 |
|
*/ |
231 |
|
public void testAddAll1() { |
232 |
|
try { |
236 |
|
} |
237 |
|
catch (NullPointerException success) {} |
238 |
|
} |
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 |
< |
* |
253 |
> |
* addAll of a collection with null elements throws NPE |
254 |
|
*/ |
255 |
|
public void testAddAll2() { |
256 |
|
try { |
262 |
|
catch (NullPointerException success) {} |
263 |
|
} |
264 |
|
/** |
265 |
< |
* |
265 |
> |
* addAll of a collection with any null elements throws NPE after |
266 |
> |
* possibly adding some elements |
267 |
|
*/ |
268 |
|
public void testAddAll3() { |
269 |
|
try { |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* |
281 |
> |
* Queue contains all elements of successful addAll |
282 |
|
*/ |
283 |
|
public void testAddAll5() { |
284 |
|
try { |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* |
299 |
> |
* put(null) throws NPE |
300 |
|
*/ |
301 |
|
public void testPutNull() { |
302 |
|
try { |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* |
312 |
> |
* all elements successfully put are contained |
313 |
|
*/ |
314 |
|
public void testPut() { |
315 |
|
try { |
326 |
|
} |
327 |
|
|
328 |
|
/** |
329 |
< |
* |
329 |
> |
* put doesn't block waiting for take |
330 |
|
*/ |
331 |
|
public void testPutWithTake() { |
332 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
359 |
|
} |
360 |
|
|
361 |
|
/** |
362 |
< |
* |
362 |
> |
* timed offer does not time out |
363 |
|
*/ |
364 |
|
public void testTimedOffer() { |
365 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
385 |
|
} |
386 |
|
|
387 |
|
/** |
388 |
< |
* |
388 |
> |
* take retrieves elements in priority order |
389 |
|
*/ |
390 |
|
public void testTake() { |
391 |
|
try { |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
< |
* |
402 |
> |
* take blocks interruptibly when empty |
403 |
|
*/ |
404 |
|
public void testTakeFromEmpty() { |
405 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
< |
* |
425 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
426 |
|
*/ |
427 |
|
public void testBlockingTake() { |
428 |
|
Thread t = new Thread(new Runnable() { |
450 |
|
|
451 |
|
|
452 |
|
/** |
453 |
< |
* |
453 |
> |
* poll succeeds unless empty |
454 |
|
*/ |
455 |
|
public void testPoll() { |
456 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
< |
* |
464 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
465 |
|
*/ |
466 |
|
public void testTimedPoll0() { |
467 |
|
try { |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* |
479 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
480 |
|
*/ |
481 |
|
public void testTimedPoll() { |
482 |
|
try { |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
< |
* |
494 |
> |
* Interrupted timed poll throws InterruptedException instead of |
495 |
> |
* returning timeout status |
496 |
|
*/ |
497 |
|
public void testInterruptedTimedPoll() { |
498 |
|
Thread t = new Thread(new Runnable() { |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
< |
* |
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); |
546 |
|
|
547 |
|
|
548 |
|
/** |
549 |
< |
* |
549 |
> |
* peek returns next element, or null if empty |
550 |
|
*/ |
551 |
|
public void testPeek() { |
552 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
560 |
|
} |
561 |
|
|
562 |
|
/** |
563 |
< |
* |
563 |
> |
* element returns next element, or throws NSEE if empty |
564 |
|
*/ |
565 |
|
public void testElement() { |
566 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
576 |
|
} |
577 |
|
|
578 |
|
/** |
579 |
< |
* |
579 |
> |
* remove removes next element, or throws NSEE if empty |
580 |
|
*/ |
581 |
|
public void testRemove() { |
582 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
< |
* |
594 |
> |
* remove(x) removes x and returns true if present |
595 |
|
*/ |
596 |
|
public void testRemoveElement() { |
597 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
606 |
|
} |
607 |
|
|
608 |
|
/** |
609 |
< |
* |
609 |
> |
* contains(x) reports true when elements added but not yet removed |
610 |
|
*/ |
611 |
|
public void testContains() { |
612 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
618 |
|
} |
619 |
|
|
620 |
|
/** |
621 |
< |
* |
621 |
> |
* clear removes all elements |
622 |
|
*/ |
623 |
|
public void testClear() { |
624 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
< |
* |
636 |
> |
* containsAll(c) is true when c contains a subset of elements |
637 |
|
*/ |
638 |
|
public void testContainsAll() { |
639 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
647 |
|
} |
648 |
|
|
649 |
|
/** |
650 |
< |
* |
650 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
651 |
|
*/ |
652 |
|
public void testRetainAll() { |
653 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
666 |
|
} |
667 |
|
|
668 |
|
/** |
669 |
< |
* |
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) { |
682 |
|
} |
683 |
|
|
684 |
|
/** |
685 |
< |
* |
685 |
> |
* toArray contains all elements |
686 |
|
*/ |
687 |
|
public void testToArray() { |
688 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
< |
* |
700 |
> |
* toArray(a) contains all elements |
701 |
|
*/ |
702 |
|
public void testToArray2() { |
703 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
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 |
|
/** |
738 |
< |
* |
738 |
> |
* iterator iterates through all elements |
739 |
|
*/ |
740 |
|
public void testIterator() { |
741 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
749 |
|
} |
750 |
|
|
751 |
|
/** |
752 |
< |
* |
752 |
> |
* iterator.remove removes current element |
753 |
|
*/ |
754 |
|
public void testIteratorRemove () { |
755 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
769 |
|
|
770 |
|
|
771 |
|
/** |
772 |
< |
* |
772 |
> |
* toString contains toStrings of elements |
773 |
|
*/ |
774 |
|
public void testToString() { |
775 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
780 |
|
} |
781 |
|
|
782 |
|
/** |
783 |
< |
* |
783 |
> |
* offer transfers elements across Executor tasks |
784 |
|
*/ |
785 |
|
public void testPollInExecutor() { |
739 |
– |
|
786 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
741 |
– |
|
787 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
743 |
– |
|
788 |
|
executor.execute(new Runnable() { |
789 |
|
public void run() { |
790 |
|
threadAssertNull(q.poll()); |
811 |
|
}); |
812 |
|
|
813 |
|
joinPool(executor); |
770 |
– |
|
814 |
|
} |
815 |
|
|
816 |
|
/** |
817 |
< |
* |
817 |
> |
* A deserialized serialized queue has same elements |
818 |
|
*/ |
819 |
|
public void testSerialization() { |
820 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
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 |
|
} |