author | dl |
Mon, 12 Sep 2016 13:14:49 -0700 | |
changeset 40817 | 4f5fb115676d |
parent 40280 | 5542e4584972 |
child 42322 | c3474fef4fe4 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
|
30 |
* Expert Group and released to the public domain, as explained at |
|
31 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
32 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
|
33 |
* Pat Fisher, Mike Judd. |
|
34 |
*/ |
|
35 |
||
36 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
37 |
||
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Arrays; |
|
40 |
import java.util.Collection; |
|
41 |
import java.util.Comparator; |
|
42 |
import java.util.Iterator; |
|
43 |
import java.util.NoSuchElementException; |
|
44 |
import java.util.Queue; |
|
45 |
import java.util.concurrent.BlockingQueue; |
|
46 |
import java.util.concurrent.CountDownLatch; |
|
47 |
import java.util.concurrent.Executors; |
|
48 |
import java.util.concurrent.ExecutorService; |
|
49 |
import java.util.concurrent.PriorityBlockingQueue; |
|
50 |
||
51 |
import junit.framework.Test; |
|
52 |
||
53 |
public class PriorityBlockingQueueTest extends JSR166TestCase { |
|
54 |
||
55 |
public static class Generic extends BlockingQueueTest { |
|
56 |
protected BlockingQueue emptyCollection() { |
|
57 |
return new PriorityBlockingQueue(); |
|
58 |
} |
|
59 |
} |
|
60 |
||
61 |
public static class InitialCapacity extends BlockingQueueTest { |
|
62 |
protected BlockingQueue emptyCollection() { |
|
63 |
return new PriorityBlockingQueue(SIZE); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
public static void main(String[] args) { |
|
68 |
main(suite(), args); |
|
69 |
} |
|
70 |
||
71 |
public static Test suite() { |
|
72 |
return newTestSuite(PriorityBlockingQueueTest.class, |
|
73 |
new Generic().testSuite(), |
|
74 |
new InitialCapacity().testSuite()); |
|
75 |
} |
|
76 |
||
77 |
/** Sample Comparator */ |
|
78 |
static class MyReverseComparator implements Comparator { |
|
79 |
public int compare(Object x, Object y) { |
|
80 |
return ((Comparable)y).compareTo(x); |
|
81 |
} |
|
82 |
} |
|
83 |
||
84 |
/** |
|
85 |
* Returns a new queue of given size containing consecutive |
|
86 |
* Integers 0 ... n. |
|
87 |
*/ |
|
88 |
private PriorityBlockingQueue<Integer> populatedQueue(int n) { |
|
89 |
PriorityBlockingQueue<Integer> q = |
|
90 |
new PriorityBlockingQueue<Integer>(n); |
|
91 |
assertTrue(q.isEmpty()); |
|
92 |
for (int i = n - 1; i >= 0; i -= 2) |
|
93 |
assertTrue(q.offer(new Integer(i))); |
|
94 |
for (int i = (n & 1); i < n; i += 2) |
|
95 |
assertTrue(q.offer(new Integer(i))); |
|
96 |
assertFalse(q.isEmpty()); |
|
97 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
98 |
assertEquals(n, q.size()); |
|
99 |
return q; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* A new queue has unbounded capacity |
|
104 |
*/ |
|
105 |
public void testConstructor1() { |
|
106 |
assertEquals(Integer.MAX_VALUE, |
|
107 |
new PriorityBlockingQueue(SIZE).remainingCapacity()); |
|
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Constructor throws IAE if capacity argument nonpositive |
|
112 |
*/ |
|
113 |
public void testConstructor2() { |
|
114 |
try { |
|
115 |
new PriorityBlockingQueue(0); |
|
116 |
shouldThrow(); |
|
117 |
} catch (IllegalArgumentException success) {} |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Initializing from null Collection throws NPE |
|
122 |
*/ |
|
123 |
public void testConstructor3() { |
|
124 |
try { |
|
125 |
new PriorityBlockingQueue(null); |
|
126 |
shouldThrow(); |
|
127 |
} catch (NullPointerException success) {} |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Initializing from Collection of null elements throws NPE |
|
132 |
*/ |
|
133 |
public void testConstructor4() { |
|
134 |
Collection<Integer> elements = Arrays.asList(new Integer[SIZE]); |
|
135 |
try { |
|
136 |
new PriorityBlockingQueue(elements); |
|
137 |
shouldThrow(); |
|
138 |
} catch (NullPointerException success) {} |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Initializing from Collection with some null elements throws NPE |
|
143 |
*/ |
|
144 |
public void testConstructor5() { |
|
145 |
Integer[] ints = new Integer[SIZE]; |
|
146 |
for (int i = 0; i < SIZE - 1; ++i) |
|
147 |
ints[i] = i; |
|
148 |
Collection<Integer> elements = Arrays.asList(ints); |
|
149 |
try { |
|
150 |
new PriorityBlockingQueue(elements); |
|
151 |
shouldThrow(); |
|
152 |
} catch (NullPointerException success) {} |
|
153 |
} |
|
154 |
||
155 |
/** |
|
156 |
* Queue contains all elements of collection used to initialize |
|
157 |
*/ |
|
158 |
public void testConstructor6() { |
|
159 |
Integer[] ints = new Integer[SIZE]; |
|
160 |
for (int i = 0; i < SIZE; ++i) |
|
161 |
ints[i] = i; |
|
162 |
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
|
163 |
for (int i = 0; i < SIZE; ++i) |
|
164 |
assertEquals(ints[i], q.poll()); |
|
165 |
} |
|
166 |
||
167 |
/** |
|
168 |
* The comparator used in constructor is used |
|
169 |
*/ |
|
170 |
public void testConstructor7() { |
|
171 |
MyReverseComparator cmp = new MyReverseComparator(); |
|
172 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE, cmp); |
|
173 |
assertEquals(cmp, q.comparator()); |
|
174 |
Integer[] ints = new Integer[SIZE]; |
|
175 |
for (int i = 0; i < SIZE; ++i) |
|
176 |
ints[i] = new Integer(i); |
|
177 |
q.addAll(Arrays.asList(ints)); |
|
178 |
for (int i = SIZE - 1; i >= 0; --i) |
|
179 |
assertEquals(ints[i], q.poll()); |
|
180 |
} |
|
181 |
||
182 |
/** |
|
183 |
* isEmpty is true before add, false after |
|
184 |
*/ |
|
185 |
public void testEmpty() { |
|
186 |
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
|
187 |
assertTrue(q.isEmpty()); |
|
188 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
189 |
q.add(one); |
|
190 |
assertFalse(q.isEmpty()); |
|
191 |
q.add(two); |
|
192 |
q.remove(); |
|
193 |
q.remove(); |
|
194 |
assertTrue(q.isEmpty()); |
|
195 |
} |
|
196 |
||
197 |
/** |
|
198 |
* remainingCapacity() always returns Integer.MAX_VALUE |
|
199 |
*/ |
|
200 |
public void testRemainingCapacity() { |
|
201 |
BlockingQueue q = populatedQueue(SIZE); |
|
202 |
for (int i = 0; i < SIZE; ++i) { |
|
203 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
204 |
assertEquals(SIZE - i, q.size()); |
|
205 |
assertEquals(i, q.remove()); |
|
206 |
} |
|
207 |
for (int i = 0; i < SIZE; ++i) { |
|
208 |
assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); |
|
209 |
assertEquals(i, q.size()); |
|
210 |
assertTrue(q.add(i)); |
|
211 |
} |
|
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Offer of comparable element succeeds |
|
216 |
*/ |
|
217 |
public void testOffer() { |
|
218 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
|
219 |
assertTrue(q.offer(zero)); |
|
220 |
assertTrue(q.offer(one)); |
|
221 |
} |
|
222 |
||
223 |
/** |
|
224 |
* Offer of non-Comparable throws CCE |
|
225 |
*/ |
|
226 |
public void testOfferNonComparable() { |
|
227 |
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
|
228 |
try { |
|
229 |
q.offer(new Object()); |
|
230 |
shouldThrow(); |
|
39039
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
231 |
} catch (ClassCastException success) { |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
232 |
assertTrue(q.isEmpty()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
233 |
assertEquals(0, q.size()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
234 |
assertNull(q.poll()); |
9dea20ff2dee
8066070: PriorityQueue corrupted when adding non-Comparable
dl
parents:
35394
diff
changeset
|
235 |
} |
35394 | 236 |
} |
237 |
||
238 |
/** |
|
239 |
* add of comparable succeeds |
|
240 |
*/ |
|
241 |
public void testAdd() { |
|
242 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
243 |
for (int i = 0; i < SIZE; ++i) { |
|
244 |
assertEquals(i, q.size()); |
|
245 |
assertTrue(q.add(new Integer(i))); |
|
246 |
} |
|
247 |
} |
|
248 |
||
249 |
/** |
|
250 |
* addAll(this) throws IAE |
|
251 |
*/ |
|
252 |
public void testAddAllSelf() { |
|
253 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
254 |
try { |
|
255 |
q.addAll(q); |
|
256 |
shouldThrow(); |
|
257 |
} catch (IllegalArgumentException success) {} |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* addAll of a collection with any null elements throws NPE after |
|
262 |
* possibly adding some elements |
|
263 |
*/ |
|
264 |
public void testAddAll3() { |
|
265 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
266 |
Integer[] ints = new Integer[SIZE]; |
|
267 |
for (int i = 0; i < SIZE - 1; ++i) |
|
268 |
ints[i] = new Integer(i); |
|
269 |
try { |
|
270 |
q.addAll(Arrays.asList(ints)); |
|
271 |
shouldThrow(); |
|
272 |
} catch (NullPointerException success) {} |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* Queue contains all elements of successful addAll |
|
277 |
*/ |
|
278 |
public void testAddAll5() { |
|
279 |
Integer[] empty = new Integer[0]; |
|
280 |
Integer[] ints = new Integer[SIZE]; |
|
281 |
for (int i = SIZE - 1; i >= 0; --i) |
|
282 |
ints[i] = new Integer(i); |
|
283 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
284 |
assertFalse(q.addAll(Arrays.asList(empty))); |
|
285 |
assertTrue(q.addAll(Arrays.asList(ints))); |
|
286 |
for (int i = 0; i < SIZE; ++i) |
|
287 |
assertEquals(ints[i], q.poll()); |
|
288 |
} |
|
289 |
||
290 |
/** |
|
291 |
* all elements successfully put are contained |
|
292 |
*/ |
|
293 |
public void testPut() { |
|
294 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE); |
|
295 |
for (int i = 0; i < SIZE; ++i) { |
|
296 |
Integer x = new Integer(i); |
|
297 |
q.put(x); |
|
298 |
assertTrue(q.contains(x)); |
|
299 |
} |
|
300 |
assertEquals(SIZE, q.size()); |
|
301 |
} |
|
302 |
||
303 |
/** |
|
304 |
* put doesn't block waiting for take |
|
305 |
*/ |
|
306 |
public void testPutWithTake() throws InterruptedException { |
|
307 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
|
308 |
final int size = 4; |
|
309 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
310 |
public void realRun() { |
|
311 |
for (int i = 0; i < size; i++) |
|
312 |
q.put(new Integer(0)); |
|
313 |
}}); |
|
314 |
||
315 |
awaitTermination(t); |
|
316 |
assertEquals(size, q.size()); |
|
317 |
q.take(); |
|
318 |
} |
|
319 |
||
320 |
/** |
|
321 |
* timed offer does not time out |
|
322 |
*/ |
|
323 |
public void testTimedOffer() throws InterruptedException { |
|
324 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
|
325 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
326 |
public void realRun() { |
|
327 |
q.put(new Integer(0)); |
|
328 |
q.put(new Integer(0)); |
|
329 |
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, MILLISECONDS)); |
|
330 |
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, MILLISECONDS)); |
|
331 |
}}); |
|
332 |
||
333 |
awaitTermination(t); |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* take retrieves elements in priority order |
|
338 |
*/ |
|
339 |
public void testTake() throws InterruptedException { |
|
340 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
341 |
for (int i = 0; i < SIZE; ++i) { |
|
342 |
assertEquals(i, q.take()); |
|
343 |
} |
|
344 |
} |
|
345 |
||
346 |
/** |
|
347 |
* Take removes existing elements until empty, then blocks interruptibly |
|
348 |
*/ |
|
349 |
public void testBlockingTake() throws InterruptedException { |
|
350 |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
|
351 |
final CountDownLatch pleaseInterrupt = new CountDownLatch(1); |
|
352 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
353 |
public void realRun() throws InterruptedException { |
|
354 |
for (int i = 0; i < SIZE; ++i) { |
|
355 |
assertEquals(i, q.take()); |
|
356 |
} |
|
357 |
||
358 |
Thread.currentThread().interrupt(); |
|
359 |
try { |
|
360 |
q.take(); |
|
361 |
shouldThrow(); |
|
362 |
} catch (InterruptedException success) {} |
|
363 |
assertFalse(Thread.interrupted()); |
|
364 |
||
365 |
pleaseInterrupt.countDown(); |
|
366 |
try { |
|
367 |
q.take(); |
|
368 |
shouldThrow(); |
|
369 |
} catch (InterruptedException success) {} |
|
370 |
assertFalse(Thread.interrupted()); |
|
371 |
}}); |
|
372 |
||
373 |
await(pleaseInterrupt); |
|
374 |
assertThreadStaysAlive(t); |
|
375 |
t.interrupt(); |
|
376 |
awaitTermination(t); |
|
377 |
} |
|
378 |
||
379 |
/** |
|
380 |
* poll succeeds unless empty |
|
381 |
*/ |
|
382 |
public void testPoll() { |
|
383 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
384 |
for (int i = 0; i < SIZE; ++i) { |
|
385 |
assertEquals(i, q.poll()); |
|
386 |
} |
|
387 |
assertNull(q.poll()); |
|
388 |
} |
|
389 |
||
390 |
/** |
|
391 |
* timed poll with zero timeout succeeds when non-empty, else times out |
|
392 |
*/ |
|
393 |
public void testTimedPoll0() throws InterruptedException { |
|
394 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
395 |
for (int i = 0; i < SIZE; ++i) { |
|
396 |
assertEquals(i, q.poll(0, MILLISECONDS)); |
|
397 |
} |
|
398 |
assertNull(q.poll(0, MILLISECONDS)); |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* timed poll with nonzero timeout succeeds when non-empty, else times out |
|
403 |
*/ |
|
404 |
public void testTimedPoll() throws InterruptedException { |
|
405 |
PriorityBlockingQueue<Integer> q = populatedQueue(SIZE); |
|
406 |
for (int i = 0; i < SIZE; ++i) { |
|
407 |
long startTime = System.nanoTime(); |
|
408 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
|
409 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
410 |
} |
|
411 |
long startTime = System.nanoTime(); |
|
412 |
assertNull(q.poll(timeoutMillis(), MILLISECONDS)); |
|
413 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
|
414 |
checkEmpty(q); |
|
415 |
} |
|
416 |
||
417 |
/** |
|
418 |
* Interrupted timed poll throws InterruptedException instead of |
|
419 |
* returning timeout status |
|
420 |
*/ |
|
421 |
public void testInterruptedTimedPoll() throws InterruptedException { |
|
422 |
final BlockingQueue<Integer> q = populatedQueue(SIZE); |
|
423 |
final CountDownLatch aboutToWait = new CountDownLatch(1); |
|
424 |
Thread t = newStartedThread(new CheckedRunnable() { |
|
425 |
public void realRun() throws InterruptedException { |
|
426 |
long startTime = System.nanoTime(); |
|
427 |
for (int i = 0; i < SIZE; ++i) { |
|
428 |
assertEquals(i, (int) q.poll(LONG_DELAY_MS, MILLISECONDS)); |
|
429 |
} |
|
430 |
aboutToWait.countDown(); |
|
431 |
try { |
|
432 |
q.poll(LONG_DELAY_MS, MILLISECONDS); |
|
433 |
shouldThrow(); |
|
434 |
} catch (InterruptedException success) { |
|
435 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
|
436 |
} |
|
437 |
}}); |
|
438 |
||
439 |
aboutToWait.await(); |
|
40280
5542e4584972
8162627: Miscellaneous changes imported from jsr166 CVS 2016-08
dl
parents:
39039
diff
changeset
|
440 |
waitForThreadToEnterWaitState(t); |
35394 | 441 |
t.interrupt(); |
442 |
awaitTermination(t); |
|
443 |
} |
|
444 |
||
445 |
/** |
|
446 |
* peek returns next element, or null if empty |
|
447 |
*/ |
|
448 |
public void testPeek() { |
|
449 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
450 |
for (int i = 0; i < SIZE; ++i) { |
|
451 |
assertEquals(i, q.peek()); |
|
452 |
assertEquals(i, q.poll()); |
|
453 |
assertTrue(q.peek() == null || |
|
454 |
!q.peek().equals(i)); |
|
455 |
} |
|
456 |
assertNull(q.peek()); |
|
457 |
} |
|
458 |
||
459 |
/** |
|
460 |
* element returns next element, or throws NSEE if empty |
|
461 |
*/ |
|
462 |
public void testElement() { |
|
463 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
464 |
for (int i = 0; i < SIZE; ++i) { |
|
465 |
assertEquals(i, q.element()); |
|
466 |
assertEquals(i, q.poll()); |
|
467 |
} |
|
468 |
try { |
|
469 |
q.element(); |
|
470 |
shouldThrow(); |
|
471 |
} catch (NoSuchElementException success) {} |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* remove removes next element, or throws NSEE if empty |
|
476 |
*/ |
|
477 |
public void testRemove() { |
|
478 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
479 |
for (int i = 0; i < SIZE; ++i) { |
|
480 |
assertEquals(i, q.remove()); |
|
481 |
} |
|
482 |
try { |
|
483 |
q.remove(); |
|
484 |
shouldThrow(); |
|
485 |
} catch (NoSuchElementException success) {} |
|
486 |
} |
|
487 |
||
488 |
/** |
|
489 |
* contains(x) reports true when elements added but not yet removed |
|
490 |
*/ |
|
491 |
public void testContains() { |
|
492 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
493 |
for (int i = 0; i < SIZE; ++i) { |
|
494 |
assertTrue(q.contains(new Integer(i))); |
|
495 |
q.poll(); |
|
496 |
assertFalse(q.contains(new Integer(i))); |
|
497 |
} |
|
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* clear removes all elements |
|
502 |
*/ |
|
503 |
public void testClear() { |
|
504 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
505 |
q.clear(); |
|
506 |
assertTrue(q.isEmpty()); |
|
507 |
assertEquals(0, q.size()); |
|
508 |
q.add(one); |
|
509 |
assertFalse(q.isEmpty()); |
|
510 |
assertTrue(q.contains(one)); |
|
511 |
q.clear(); |
|
512 |
assertTrue(q.isEmpty()); |
|
513 |
} |
|
514 |
||
515 |
/** |
|
516 |
* containsAll(c) is true when c contains a subset of elements |
|
517 |
*/ |
|
518 |
public void testContainsAll() { |
|
519 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
520 |
PriorityBlockingQueue p = new PriorityBlockingQueue(SIZE); |
|
521 |
for (int i = 0; i < SIZE; ++i) { |
|
522 |
assertTrue(q.containsAll(p)); |
|
523 |
assertFalse(p.containsAll(q)); |
|
524 |
p.add(new Integer(i)); |
|
525 |
} |
|
526 |
assertTrue(p.containsAll(q)); |
|
527 |
} |
|
528 |
||
529 |
/** |
|
530 |
* retainAll(c) retains only those elements of c and reports true if changed |
|
531 |
*/ |
|
532 |
public void testRetainAll() { |
|
533 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
534 |
PriorityBlockingQueue p = populatedQueue(SIZE); |
|
535 |
for (int i = 0; i < SIZE; ++i) { |
|
536 |
boolean changed = q.retainAll(p); |
|
537 |
if (i == 0) |
|
538 |
assertFalse(changed); |
|
539 |
else |
|
540 |
assertTrue(changed); |
|
541 |
||
542 |
assertTrue(q.containsAll(p)); |
|
543 |
assertEquals(SIZE - i, q.size()); |
|
544 |
p.remove(); |
|
545 |
} |
|
546 |
} |
|
547 |
||
548 |
/** |
|
549 |
* removeAll(c) removes only those elements of c and reports true if changed |
|
550 |
*/ |
|
551 |
public void testRemoveAll() { |
|
552 |
for (int i = 1; i < SIZE; ++i) { |
|
553 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
554 |
PriorityBlockingQueue p = populatedQueue(i); |
|
555 |
assertTrue(q.removeAll(p)); |
|
556 |
assertEquals(SIZE - i, q.size()); |
|
557 |
for (int j = 0; j < i; ++j) { |
|
558 |
Integer x = (Integer)(p.remove()); |
|
559 |
assertFalse(q.contains(x)); |
|
560 |
} |
|
561 |
} |
|
562 |
} |
|
563 |
||
564 |
/** |
|
565 |
* toArray contains all elements |
|
566 |
*/ |
|
567 |
public void testToArray() throws InterruptedException { |
|
568 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
569 |
Object[] o = q.toArray(); |
|
570 |
Arrays.sort(o); |
|
571 |
for (int i = 0; i < o.length; i++) |
|
572 |
assertSame(o[i], q.take()); |
|
573 |
} |
|
574 |
||
575 |
/** |
|
576 |
* toArray(a) contains all elements |
|
577 |
*/ |
|
578 |
public void testToArray2() throws InterruptedException { |
|
579 |
PriorityBlockingQueue<Integer> q = populatedQueue(SIZE); |
|
580 |
Integer[] ints = new Integer[SIZE]; |
|
581 |
Integer[] array = q.toArray(ints); |
|
582 |
assertSame(ints, array); |
|
583 |
Arrays.sort(ints); |
|
584 |
for (int i = 0; i < ints.length; i++) |
|
585 |
assertSame(ints[i], q.take()); |
|
586 |
} |
|
587 |
||
588 |
/** |
|
589 |
* toArray(incompatible array type) throws ArrayStoreException |
|
590 |
*/ |
|
591 |
public void testToArray1_BadArg() { |
|
592 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
593 |
try { |
|
594 |
q.toArray(new String[10]); |
|
595 |
shouldThrow(); |
|
596 |
} catch (ArrayStoreException success) {} |
|
597 |
} |
|
598 |
||
599 |
/** |
|
600 |
* iterator iterates through all elements |
|
601 |
*/ |
|
602 |
public void testIterator() { |
|
603 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
604 |
Iterator it = q.iterator(); |
|
605 |
int i; |
|
606 |
for (i = 0; it.hasNext(); i++) |
|
607 |
assertTrue(q.contains(it.next())); |
|
608 |
assertEquals(i, SIZE); |
|
609 |
assertIteratorExhausted(it); |
|
610 |
} |
|
611 |
||
612 |
/** |
|
613 |
* iterator of empty collection has no elements |
|
614 |
*/ |
|
615 |
public void testEmptyIterator() { |
|
616 |
assertIteratorExhausted(new PriorityBlockingQueue().iterator()); |
|
617 |
} |
|
618 |
||
619 |
/** |
|
620 |
* iterator.remove removes current element |
|
621 |
*/ |
|
622 |
public void testIteratorRemove() { |
|
623 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
|
624 |
q.add(new Integer(2)); |
|
625 |
q.add(new Integer(1)); |
|
626 |
q.add(new Integer(3)); |
|
627 |
||
628 |
Iterator it = q.iterator(); |
|
629 |
it.next(); |
|
630 |
it.remove(); |
|
631 |
||
632 |
it = q.iterator(); |
|
633 |
assertEquals(it.next(), new Integer(2)); |
|
634 |
assertEquals(it.next(), new Integer(3)); |
|
635 |
assertFalse(it.hasNext()); |
|
636 |
} |
|
637 |
||
638 |
/** |
|
639 |
* toString contains toStrings of elements |
|
640 |
*/ |
|
641 |
public void testToString() { |
|
642 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
643 |
String s = q.toString(); |
|
644 |
for (int i = 0; i < SIZE; ++i) { |
|
645 |
assertTrue(s.contains(String.valueOf(i))); |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
/** |
|
650 |
* timed poll transfers elements across Executor tasks |
|
651 |
*/ |
|
652 |
public void testPollInExecutor() { |
|
653 |
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
|
654 |
final CheckedBarrier threadsStarted = new CheckedBarrier(2); |
|
655 |
final ExecutorService executor = Executors.newFixedThreadPool(2); |
|
656 |
try (PoolCleaner cleaner = cleaner(executor)) { |
|
657 |
executor.execute(new CheckedRunnable() { |
|
658 |
public void realRun() throws InterruptedException { |
|
659 |
assertNull(q.poll()); |
|
660 |
threadsStarted.await(); |
|
661 |
assertSame(one, q.poll(LONG_DELAY_MS, MILLISECONDS)); |
|
662 |
checkEmpty(q); |
|
663 |
}}); |
|
664 |
||
665 |
executor.execute(new CheckedRunnable() { |
|
666 |
public void realRun() throws InterruptedException { |
|
667 |
threadsStarted.await(); |
|
668 |
q.put(one); |
|
669 |
}}); |
|
670 |
} |
|
671 |
} |
|
672 |
||
673 |
/** |
|
674 |
* A deserialized serialized queue has same elements |
|
675 |
*/ |
|
676 |
public void testSerialization() throws Exception { |
|
677 |
Queue x = populatedQueue(SIZE); |
|
678 |
Queue y = serialClone(x); |
|
679 |
||
680 |
assertNotSame(x, y); |
|
681 |
assertEquals(x.size(), y.size()); |
|
682 |
while (!x.isEmpty()) { |
|
683 |
assertFalse(y.isEmpty()); |
|
684 |
assertEquals(x.remove(), y.remove()); |
|
685 |
} |
|
686 |
assertTrue(y.isEmpty()); |
|
687 |
} |
|
688 |
||
689 |
/** |
|
690 |
* drainTo(c) empties queue into another collection c |
|
691 |
*/ |
|
692 |
public void testDrainTo() { |
|
693 |
PriorityBlockingQueue q = populatedQueue(SIZE); |
|
694 |
ArrayList l = new ArrayList(); |
|
695 |
q.drainTo(l); |
|
696 |
assertEquals(0, q.size()); |
|
697 |
assertEquals(SIZE, l.size()); |
|
698 |
for (int i = 0; i < SIZE; ++i) |
|
699 |
assertEquals(l.get(i), new Integer(i)); |
|
700 |
q.add(zero); |
|
701 |
q.add(one); |
|
702 |
assertFalse(q.isEmpty()); |
|
703 |
assertTrue(q.contains(zero)); |
|
704 |
assertTrue(q.contains(one)); |
|
705 |
l.clear(); |
|
706 |
q.drainTo(l); |
|
707 |
assertEquals(0, q.size()); |
|
708 |
assertEquals(2, l.size()); |
|
709 |
for (int i = 0; i < 2; ++i) |
|
710 |
assertEquals(l.get(i), new Integer(i)); |
|
711 |
} |
|
712 |
||
713 |
/** |
|
714 |
* drainTo empties queue |
|
715 |
*/ |
|
716 |
public void testDrainToWithActivePut() throws InterruptedException { |
|
717 |
final PriorityBlockingQueue q = populatedQueue(SIZE); |
|
718 |
Thread t = new Thread(new CheckedRunnable() { |
|
719 |
public void realRun() { |
|
720 |
q.put(new Integer(SIZE + 1)); |
|
721 |
}}); |
|
722 |
||
723 |
t.start(); |
|
724 |
ArrayList l = new ArrayList(); |
|
725 |
q.drainTo(l); |
|
726 |
assertTrue(l.size() >= SIZE); |
|
727 |
for (int i = 0; i < SIZE; ++i) |
|
728 |
assertEquals(l.get(i), new Integer(i)); |
|
729 |
t.join(); |
|
730 |
assertTrue(q.size() + l.size() >= SIZE); |
|
731 |
} |
|
732 |
||
733 |
/** |
|
734 |
* drainTo(c, n) empties first min(n, size) elements of queue into c |
|
735 |
*/ |
|
736 |
public void testDrainToN() { |
|
737 |
PriorityBlockingQueue q = new PriorityBlockingQueue(SIZE * 2); |
|
738 |
for (int i = 0; i < SIZE + 2; ++i) { |
|
739 |
for (int j = 0; j < SIZE; j++) |
|
740 |
assertTrue(q.offer(new Integer(j))); |
|
741 |
ArrayList l = new ArrayList(); |
|
742 |
q.drainTo(l, i); |
|
743 |
int k = (i < SIZE) ? i : SIZE; |
|
744 |
assertEquals(k, l.size()); |
|
745 |
assertEquals(SIZE - k, q.size()); |
|
746 |
for (int j = 0; j < k; ++j) |
|
747 |
assertEquals(l.get(j), new Integer(j)); |
|
748 |
do {} while (q.poll() != null); |
|
749 |
} |
|
750 |
} |
|
751 |
||
752 |
/** |
|
753 |
* remove(null), contains(null) always return false |
|
754 |
*/ |
|
755 |
public void testNeverContainsNull() { |
|
756 |
Collection<?>[] qs = { |
|
757 |
new PriorityBlockingQueue<Object>(), |
|
758 |
populatedQueue(2), |
|
759 |
}; |
|
760 |
||
761 |
for (Collection<?> q : qs) { |
|
762 |
assertFalse(q.contains(null)); |
|
763 |
assertFalse(q.remove(null)); |
|
764 |
} |
|
765 |
} |
|
766 |
||
767 |
} |