1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
17 |
– |
|
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(PriorityBlockingQueueTest.class); |
20 |
|
} |
32 |
|
} |
33 |
|
} |
34 |
|
|
35 |
– |
|
35 |
|
/** |
36 |
|
* Create a queue of given size containing consecutive |
37 |
|
* Integers 0 ... n. |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* |
53 |
> |
* A new queue has unbounded capacity |
54 |
|
*/ |
55 |
|
public void testConstructor1() { |
56 |
|
assertEquals(NOCAP, new PriorityBlockingQueue(SIZE).remainingCapacity()); |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* |
60 |
> |
* Constructor throws IAE if capacity argument nonpositive |
61 |
|
*/ |
62 |
|
public void testConstructor2() { |
63 |
|
try { |
68 |
|
} |
69 |
|
|
70 |
|
/** |
71 |
< |
* |
71 |
> |
* Initializing from null Collection throws NPE |
72 |
|
*/ |
73 |
|
public void testConstructor3() { |
75 |
– |
|
74 |
|
try { |
75 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
76 |
|
shouldThrow(); |
79 |
|
} |
80 |
|
|
81 |
|
/** |
82 |
< |
* |
82 |
> |
* Initializing from Collection of null elements throws NPE |
83 |
|
*/ |
84 |
|
public void testConstructor4() { |
85 |
|
try { |
91 |
|
} |
92 |
|
|
93 |
|
/** |
94 |
< |
* |
94 |
> |
* Initializing from Collection with some null elements throws NPE |
95 |
|
*/ |
96 |
|
public void testConstructor5() { |
97 |
|
try { |
105 |
|
} |
106 |
|
|
107 |
|
/** |
108 |
< |
* |
108 |
> |
* Queue contains all elements of collection used to initialize |
109 |
|
*/ |
110 |
|
public void testConstructor6() { |
111 |
|
try { |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
< |
* |
123 |
> |
* The comparator used in constructor is used |
124 |
|
*/ |
125 |
|
public void testConstructor7() { |
126 |
|
try { |
138 |
|
} |
139 |
|
|
140 |
|
/** |
141 |
< |
* |
141 |
> |
* isEmpty is true before add, false after |
142 |
|
*/ |
143 |
|
public void testEmpty() { |
144 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
145 |
|
assertTrue(q.isEmpty()); |
146 |
|
assertEquals(NOCAP, q.remainingCapacity()); |
147 |
< |
q.add(new Integer(1)); |
147 |
> |
q.add(one); |
148 |
|
assertFalse(q.isEmpty()); |
149 |
< |
q.add(new Integer(2)); |
149 |
> |
q.add(two); |
150 |
|
q.remove(); |
151 |
|
q.remove(); |
152 |
|
assertTrue(q.isEmpty()); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
< |
* |
156 |
> |
* remainingCapacity does not change when elements added or removed, |
157 |
> |
* but size does |
158 |
|
*/ |
159 |
|
public void testRemainingCapacity() { |
160 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* |
174 |
> |
* offer(null) throws NPE |
175 |
|
*/ |
176 |
|
public void testOfferNull() { |
177 |
|
try { |
182 |
|
} |
183 |
|
|
184 |
|
/** |
185 |
< |
* |
185 |
> |
* add(null) throws NPE |
186 |
> |
*/ |
187 |
> |
public void testAddNull() { |
188 |
> |
try { |
189 |
> |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
190 |
> |
q.add(null); |
191 |
> |
shouldThrow(); |
192 |
> |
} catch (NullPointerException success) { } |
193 |
> |
} |
194 |
> |
|
195 |
> |
/** |
196 |
> |
* Offer of comparable element succeeds |
197 |
|
*/ |
198 |
|
public void testOffer() { |
199 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
200 |
< |
assertTrue(q.offer(new Integer(0))); |
201 |
< |
assertTrue(q.offer(new Integer(1))); |
200 |
> |
assertTrue(q.offer(zero)); |
201 |
> |
assertTrue(q.offer(one)); |
202 |
|
} |
203 |
|
|
204 |
|
/** |
205 |
< |
* |
205 |
> |
* Offer of non-Comparable throws CCE |
206 |
|
*/ |
207 |
|
public void testOfferNonComparable() { |
208 |
|
try { |
216 |
|
} |
217 |
|
|
218 |
|
/** |
219 |
< |
* |
219 |
> |
* add of comparable succeeds |
220 |
|
*/ |
221 |
|
public void testAdd() { |
222 |
|
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
< |
* |
230 |
> |
* addAll(null) throws NPE |
231 |
|
*/ |
232 |
|
public void testAddAll1() { |
233 |
|
try { |
237 |
|
} |
238 |
|
catch (NullPointerException success) {} |
239 |
|
} |
240 |
+ |
|
241 |
+ |
/** |
242 |
+ |
* addAll(this) throws IAE |
243 |
+ |
*/ |
244 |
+ |
public void testAddAllSelf() { |
245 |
+ |
try { |
246 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
247 |
+ |
q.addAll(q); |
248 |
+ |
shouldThrow(); |
249 |
+ |
} |
250 |
+ |
catch (IllegalArgumentException success) {} |
251 |
+ |
} |
252 |
+ |
|
253 |
|
/** |
254 |
< |
* |
254 |
> |
* addAll of a collection with null elements throws NPE |
255 |
|
*/ |
256 |
|
public void testAddAll2() { |
257 |
|
try { |
263 |
|
catch (NullPointerException success) {} |
264 |
|
} |
265 |
|
/** |
266 |
< |
* |
266 |
> |
* addAll of a collection with any null elements throws NPE after |
267 |
> |
* possibly adding some elements |
268 |
|
*/ |
269 |
|
public void testAddAll3() { |
270 |
|
try { |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
< |
* |
282 |
> |
* Queue contains all elements of successful addAll |
283 |
|
*/ |
284 |
|
public void testAddAll5() { |
285 |
|
try { |
297 |
|
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* |
300 |
> |
* put(null) throws NPE |
301 |
|
*/ |
302 |
|
public void testPutNull() { |
303 |
|
try { |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* |
313 |
> |
* all elements successfully put are contained |
314 |
|
*/ |
315 |
|
public void testPut() { |
316 |
|
try { |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* |
330 |
> |
* put doesn't block waiting for take |
331 |
|
*/ |
332 |
|
public void testPutWithTake() { |
333 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* |
363 |
> |
* timed offer does not time out |
364 |
|
*/ |
365 |
|
public void testTimedOffer() { |
366 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
386 |
|
} |
387 |
|
|
388 |
|
/** |
389 |
< |
* |
389 |
> |
* take retrieves elements in priority order |
390 |
|
*/ |
391 |
|
public void testTake() { |
392 |
|
try { |
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* |
403 |
> |
* take blocks interruptibly when empty |
404 |
|
*/ |
405 |
|
public void testTakeFromEmpty() { |
406 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
< |
* |
426 |
> |
* Take removes existing elements until empty, then blocks interruptibly |
427 |
|
*/ |
428 |
|
public void testBlockingTake() { |
429 |
|
Thread t = new Thread(new Runnable() { |
451 |
|
|
452 |
|
|
453 |
|
/** |
454 |
< |
* |
454 |
> |
* poll succeeds unless empty |
455 |
|
*/ |
456 |
|
public void testPoll() { |
457 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
462 |
|
} |
463 |
|
|
464 |
|
/** |
465 |
< |
* |
465 |
> |
* timed pool with zero timeout succeeds when non-empty, else times out |
466 |
|
*/ |
467 |
|
public void testTimedPoll0() { |
468 |
|
try { |
477 |
|
} |
478 |
|
|
479 |
|
/** |
480 |
< |
* |
480 |
> |
* timed pool with nonzero timeout succeeds when non-empty, else times out |
481 |
|
*/ |
482 |
|
public void testTimedPoll() { |
483 |
|
try { |
492 |
|
} |
493 |
|
|
494 |
|
/** |
495 |
< |
* |
495 |
> |
* Interrupted timed poll throws InterruptedException instead of |
496 |
> |
* returning timeout status |
497 |
|
*/ |
498 |
|
public void testInterruptedTimedPoll() { |
499 |
|
Thread t = new Thread(new Runnable() { |
519 |
|
} |
520 |
|
|
521 |
|
/** |
522 |
< |
* |
522 |
> |
* timed poll before a delayed offer fails; after offer succeeds; |
523 |
> |
* on interruption throws |
524 |
|
*/ |
525 |
|
public void testTimedPollWithOffer() { |
526 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
547 |
|
|
548 |
|
|
549 |
|
/** |
550 |
< |
* |
550 |
> |
* peek returns next element, or null if empty |
551 |
|
*/ |
552 |
|
public void testPeek() { |
553 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
< |
* |
564 |
> |
* element returns next element, or throws NSEE if empty |
565 |
|
*/ |
566 |
|
public void testElement() { |
567 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
577 |
|
} |
578 |
|
|
579 |
|
/** |
580 |
< |
* |
580 |
> |
* remove removes next element, or throws NSEE if empty |
581 |
|
*/ |
582 |
|
public void testRemove() { |
583 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
592 |
|
} |
593 |
|
|
594 |
|
/** |
595 |
< |
* |
595 |
> |
* remove(x) removes x and returns true if present |
596 |
|
*/ |
597 |
|
public void testRemoveElement() { |
598 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
< |
* |
610 |
> |
* contains(x) reports true when elements added but not yet removed |
611 |
|
*/ |
612 |
|
public void testContains() { |
613 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
619 |
|
} |
620 |
|
|
621 |
|
/** |
622 |
< |
* |
622 |
> |
* clear removes all elements |
623 |
|
*/ |
624 |
|
public void testClear() { |
625 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
634 |
|
} |
635 |
|
|
636 |
|
/** |
637 |
< |
* |
637 |
> |
* containsAll(c) is true when c contains a subset of elements |
638 |
|
*/ |
639 |
|
public void testContainsAll() { |
640 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
648 |
|
} |
649 |
|
|
650 |
|
/** |
651 |
< |
* |
651 |
> |
* retainAll(c) retains only those elements of c and reports true if changed |
652 |
|
*/ |
653 |
|
public void testRetainAll() { |
654 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
667 |
|
} |
668 |
|
|
669 |
|
/** |
670 |
< |
* |
670 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
671 |
|
*/ |
672 |
|
public void testRemoveAll() { |
673 |
|
for (int i = 1; i < SIZE; ++i) { |
683 |
|
} |
684 |
|
|
685 |
|
/** |
686 |
< |
* |
686 |
> |
* toArray contains all elements |
687 |
|
*/ |
688 |
|
public void testToArray() { |
689 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
< |
* |
701 |
> |
* toArray(a) contains all elements |
702 |
|
*/ |
703 |
|
public void testToArray2() { |
704 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
712 |
|
unexpectedException(); |
713 |
|
} |
714 |
|
} |
715 |
+ |
|
716 |
+ |
/** |
717 |
+ |
* toArray(null) throws NPE |
718 |
+ |
*/ |
719 |
+ |
public void testToArray_BadArg() { |
720 |
+ |
try { |
721 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
722 |
+ |
Object o[] = q.toArray(null); |
723 |
+ |
shouldThrow(); |
724 |
+ |
} catch(NullPointerException success){} |
725 |
+ |
} |
726 |
+ |
|
727 |
+ |
/** |
728 |
+ |
* toArray with incompatible array type throws CCE |
729 |
+ |
*/ |
730 |
+ |
public void testToArray1_BadArg() { |
731 |
+ |
try { |
732 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
733 |
+ |
Object o[] = q.toArray(new String[10] ); |
734 |
+ |
shouldThrow(); |
735 |
+ |
} catch(ArrayStoreException success){} |
736 |
+ |
} |
737 |
|
|
738 |
|
/** |
739 |
< |
* |
739 |
> |
* iterator iterates through all elements |
740 |
|
*/ |
741 |
|
public void testIterator() { |
742 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
750 |
|
} |
751 |
|
|
752 |
|
/** |
753 |
< |
* |
753 |
> |
* iterator.remove removes current element |
754 |
|
*/ |
755 |
|
public void testIteratorRemove () { |
756 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
770 |
|
|
771 |
|
|
772 |
|
/** |
773 |
< |
* |
773 |
> |
* toString contains toStrings of elements |
774 |
|
*/ |
775 |
|
public void testToString() { |
776 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
781 |
|
} |
782 |
|
|
783 |
|
/** |
784 |
< |
* |
784 |
> |
* offer transfers elements across Executor tasks |
785 |
|
*/ |
786 |
|
public void testPollInExecutor() { |
739 |
– |
|
787 |
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
741 |
– |
|
788 |
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
743 |
– |
|
789 |
|
executor.execute(new Runnable() { |
790 |
|
public void run() { |
791 |
|
threadAssertNull(q.poll()); |
812 |
|
}); |
813 |
|
|
814 |
|
joinPool(executor); |
770 |
– |
|
815 |
|
} |
816 |
|
|
817 |
|
/** |
818 |
< |
* |
818 |
> |
* A deserialized serialized queue has same elements |
819 |
|
*/ |
820 |
|
public void testSerialization() { |
821 |
|
PriorityBlockingQueue q = populatedQueue(SIZE); |
836 |
|
} |
837 |
|
} |
838 |
|
|
839 |
+ |
/** |
840 |
+ |
* drainTo(null) throws NPE |
841 |
+ |
*/ |
842 |
+ |
public void testDrainToNull() { |
843 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
844 |
+ |
try { |
845 |
+ |
q.drainTo(null); |
846 |
+ |
shouldThrow(); |
847 |
+ |
} catch(NullPointerException success) { |
848 |
+ |
} |
849 |
+ |
} |
850 |
+ |
|
851 |
+ |
/** |
852 |
+ |
* drainTo(this) throws IAE |
853 |
+ |
*/ |
854 |
+ |
public void testDrainToSelf() { |
855 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
856 |
+ |
try { |
857 |
+ |
q.drainTo(q); |
858 |
+ |
shouldThrow(); |
859 |
+ |
} catch(IllegalArgumentException success) { |
860 |
+ |
} |
861 |
+ |
} |
862 |
+ |
|
863 |
+ |
/** |
864 |
+ |
* drainTo(c) empties queue into another collection c |
865 |
+ |
*/ |
866 |
+ |
public void testDrainTo() { |
867 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
868 |
+ |
ArrayList l = new ArrayList(); |
869 |
+ |
q.drainTo(l); |
870 |
+ |
assertEquals(q.size(), 0); |
871 |
+ |
assertEquals(l.size(), SIZE); |
872 |
+ |
for (int i = 0; i < SIZE; ++i) |
873 |
+ |
assertEquals(l.get(i), new Integer(i)); |
874 |
+ |
} |
875 |
+ |
|
876 |
+ |
/** |
877 |
+ |
* drainTo empties queue |
878 |
+ |
*/ |
879 |
+ |
public void testDrainToWithActivePut() { |
880 |
+ |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
881 |
+ |
Thread t = new Thread(new Runnable() { |
882 |
+ |
public void run() { |
883 |
+ |
q.put(new Integer(SIZE+1)); |
884 |
+ |
} |
885 |
+ |
}); |
886 |
+ |
try { |
887 |
+ |
t.start(); |
888 |
+ |
ArrayList l = new ArrayList(); |
889 |
+ |
q.drainTo(l); |
890 |
+ |
assertTrue(l.size() >= SIZE); |
891 |
+ |
for (int i = 0; i < SIZE; ++i) |
892 |
+ |
assertEquals(l.get(i), new Integer(i)); |
893 |
+ |
t.join(); |
894 |
+ |
assertTrue(q.size() + l.size() >= SIZE); |
895 |
+ |
} catch(Exception e){ |
896 |
+ |
unexpectedException(); |
897 |
+ |
} |
898 |
+ |
} |
899 |
+ |
|
900 |
+ |
/** |
901 |
+ |
* drainTo(null, n) throws NPE |
902 |
+ |
*/ |
903 |
+ |
public void testDrainToNullN() { |
904 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
905 |
+ |
try { |
906 |
+ |
q.drainTo(null, 0); |
907 |
+ |
shouldThrow(); |
908 |
+ |
} catch(NullPointerException success) { |
909 |
+ |
} |
910 |
+ |
} |
911 |
+ |
|
912 |
+ |
/** |
913 |
+ |
* drainTo(this, n) throws IAE |
914 |
+ |
*/ |
915 |
+ |
public void testDrainToSelfN() { |
916 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
917 |
+ |
try { |
918 |
+ |
q.drainTo(q, 0); |
919 |
+ |
shouldThrow(); |
920 |
+ |
} catch(IllegalArgumentException success) { |
921 |
+ |
} |
922 |
+ |
} |
923 |
+ |
|
924 |
+ |
/** |
925 |
+ |
* drainTo(c, n) empties first max {n, size} elements of queue into c |
926 |
+ |
*/ |
927 |
+ |
public void testDrainToN() { |
928 |
+ |
for (int i = 0; i < SIZE + 2; ++i) { |
929 |
+ |
PriorityBlockingQueue q = populatedQueue(SIZE); |
930 |
+ |
ArrayList l = new ArrayList(); |
931 |
+ |
q.drainTo(l, i); |
932 |
+ |
int k = (i < SIZE)? i : SIZE; |
933 |
+ |
assertEquals(q.size(), SIZE-k); |
934 |
+ |
assertEquals(l.size(), k); |
935 |
+ |
for (int j = 0; j < k; ++j) |
936 |
+ |
assertTrue(l.contains(new Integer(j))); |
937 |
+ |
} |
938 |
+ |
} |
939 |
+ |
|
940 |
+ |
|
941 |
|
} |