1 |
dl |
1.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. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import junit.framework.*; |
9 |
|
|
import java.util.*; |
10 |
|
|
import java.util.concurrent.*; |
11 |
|
|
|
12 |
|
|
public class PriorityBlockingQueueTest extends TestCase { |
13 |
|
|
|
14 |
|
|
private static final int N = 10; |
15 |
|
|
private static final long SHORT_DELAY_MS = 100; |
16 |
|
|
private static final long MEDIUM_DELAY_MS = 1000; |
17 |
|
|
private static final long LONG_DELAY_MS = 10000; |
18 |
|
|
private static final int NOCAP = Integer.MAX_VALUE; |
19 |
|
|
|
20 |
|
|
public static void main(String[] args) { |
21 |
|
|
junit.textui.TestRunner.run (suite()); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
public static Test suite() { |
25 |
|
|
return new TestSuite(PriorityBlockingQueueTest.class); |
26 |
|
|
} |
27 |
|
|
|
28 |
|
|
static class MyReverseComparator implements Comparator { |
29 |
|
|
public int compare(Object x, Object y) { |
30 |
|
|
int i = ((Integer)x).intValue(); |
31 |
|
|
int j = ((Integer)y).intValue(); |
32 |
|
|
if (i < j) return 1; |
33 |
|
|
if (i > j) return -1; |
34 |
|
|
return 0; |
35 |
|
|
} |
36 |
|
|
} |
37 |
|
|
|
38 |
|
|
|
39 |
|
|
/** |
40 |
|
|
* Create a queue of given size containing consecutive |
41 |
|
|
* Integers 0 ... n. |
42 |
|
|
*/ |
43 |
|
|
private PriorityBlockingQueue fullQueue(int n) { |
44 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(n); |
45 |
|
|
assertTrue(q.isEmpty()); |
46 |
|
|
for(int i = n-1; i >= 0; i-=2) |
47 |
|
|
assertTrue(q.offer(new Integer(i))); |
48 |
|
|
for(int i = (n & 1); i < n; i+=2) |
49 |
|
|
assertTrue(q.offer(new Integer(i))); |
50 |
|
|
assertFalse(q.isEmpty()); |
51 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
52 |
|
|
assertEquals(n, q.size()); |
53 |
|
|
return q; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
public void testConstructor1(){ |
57 |
|
|
assertEquals(NOCAP, new PriorityBlockingQueue(N).remainingCapacity()); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
public void testConstructor2(){ |
61 |
|
|
try { |
62 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(0); |
63 |
|
|
fail("Cannot make zero-sized"); |
64 |
|
|
} |
65 |
|
|
catch (IllegalArgumentException success) {} |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
public void testConstructor3(){ |
69 |
|
|
|
70 |
|
|
try { |
71 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(null); |
72 |
|
|
fail("Cannot make from null collection"); |
73 |
|
|
} |
74 |
|
|
catch (NullPointerException success) {} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
public void testConstructor4(){ |
78 |
|
|
try { |
79 |
|
|
Integer[] ints = new Integer[N]; |
80 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
81 |
|
|
fail("Cannot make with null elements"); |
82 |
|
|
} |
83 |
|
|
catch (NullPointerException success) {} |
84 |
|
|
} |
85 |
|
|
|
86 |
|
|
public void testConstructor5(){ |
87 |
|
|
try { |
88 |
|
|
Integer[] ints = new Integer[N]; |
89 |
|
|
for (int i = 0; i < N-1; ++i) |
90 |
|
|
ints[i] = new Integer(i); |
91 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
92 |
|
|
fail("Cannot make with null elements"); |
93 |
|
|
} |
94 |
|
|
catch (NullPointerException success) {} |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
public void testConstructor6(){ |
98 |
|
|
try { |
99 |
|
|
Integer[] ints = new Integer[N]; |
100 |
|
|
for (int i = 0; i < N; ++i) |
101 |
|
|
ints[i] = new Integer(i); |
102 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(Arrays.asList(ints)); |
103 |
|
|
for (int i = 0; i < N; ++i) |
104 |
|
|
assertEquals(ints[i], q.poll()); |
105 |
|
|
} |
106 |
|
|
finally {} |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
public void testConstructor7(){ |
110 |
|
|
try { |
111 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(N, new MyReverseComparator()); |
112 |
|
|
Integer[] ints = new Integer[N]; |
113 |
|
|
for (int i = 0; i < N; ++i) |
114 |
|
|
ints[i] = new Integer(i); |
115 |
|
|
q.addAll(Arrays.asList(ints)); |
116 |
|
|
for (int i = N-1; i >= 0; --i) |
117 |
|
|
assertEquals(ints[i], q.poll()); |
118 |
|
|
} |
119 |
|
|
finally {} |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
public void testEmpty() { |
123 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
124 |
|
|
assertTrue(q.isEmpty()); |
125 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
126 |
|
|
q.add(new Integer(1)); |
127 |
|
|
assertFalse(q.isEmpty()); |
128 |
|
|
q.add(new Integer(2)); |
129 |
|
|
q.remove(); |
130 |
|
|
q.remove(); |
131 |
|
|
assertTrue(q.isEmpty()); |
132 |
|
|
} |
133 |
|
|
|
134 |
|
|
public void testRemainingCapacity(){ |
135 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
136 |
|
|
for (int i = 0; i < N; ++i) { |
137 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
138 |
|
|
assertEquals(N-i, q.size()); |
139 |
|
|
q.remove(); |
140 |
|
|
} |
141 |
|
|
for (int i = 0; i < N; ++i) { |
142 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
143 |
|
|
assertEquals(i, q.size()); |
144 |
|
|
q.add(new Integer(i)); |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
public void testOfferNull(){ |
149 |
|
|
try { |
150 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
151 |
|
|
q.offer(null); |
152 |
|
|
fail("should throw NPE"); |
153 |
|
|
} catch (NullPointerException success) { } |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
public void testOffer() { |
157 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
158 |
|
|
assertTrue(q.offer(new Integer(0))); |
159 |
|
|
assertTrue(q.offer(new Integer(1))); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
public void testOfferNonComparable() { |
163 |
|
|
try { |
164 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
165 |
|
|
q.offer(new Object()); |
166 |
|
|
q.offer(new Object()); |
167 |
|
|
q.offer(new Object()); |
168 |
|
|
fail("should throw CCE"); |
169 |
|
|
} |
170 |
|
|
catch(ClassCastException success) {} |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
public void testAdd(){ |
174 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
175 |
|
|
for (int i = 0; i < N; ++i) { |
176 |
|
|
assertEquals(i, q.size()); |
177 |
|
|
assertTrue(q.add(new Integer(i))); |
178 |
|
|
} |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
public void testAddAll1(){ |
182 |
|
|
try { |
183 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(1); |
184 |
|
|
q.addAll(null); |
185 |
|
|
fail("Cannot add null collection"); |
186 |
|
|
} |
187 |
|
|
catch (NullPointerException success) {} |
188 |
|
|
} |
189 |
|
|
public void testAddAll2(){ |
190 |
|
|
try { |
191 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
192 |
|
|
Integer[] ints = new Integer[N]; |
193 |
|
|
q.addAll(Arrays.asList(ints)); |
194 |
|
|
fail("Cannot add null elements"); |
195 |
|
|
} |
196 |
|
|
catch (NullPointerException success) {} |
197 |
|
|
} |
198 |
|
|
public void testAddAll3(){ |
199 |
|
|
try { |
200 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
201 |
|
|
Integer[] ints = new Integer[N]; |
202 |
|
|
for (int i = 0; i < N-1; ++i) |
203 |
|
|
ints[i] = new Integer(i); |
204 |
|
|
q.addAll(Arrays.asList(ints)); |
205 |
|
|
fail("Cannot add null elements"); |
206 |
|
|
} |
207 |
|
|
catch (NullPointerException success) {} |
208 |
|
|
} |
209 |
|
|
|
210 |
|
|
public void testAddAll5(){ |
211 |
|
|
try { |
212 |
|
|
Integer[] empty = new Integer[0]; |
213 |
|
|
Integer[] ints = new Integer[N]; |
214 |
|
|
for (int i = N-1; i >= 0; --i) |
215 |
|
|
ints[i] = new Integer(i); |
216 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
217 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
218 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
219 |
|
|
for (int i = 0; i < N; ++i) |
220 |
|
|
assertEquals(ints[i], q.poll()); |
221 |
|
|
} |
222 |
|
|
finally {} |
223 |
|
|
} |
224 |
|
|
|
225 |
|
|
public void testPutNull() { |
226 |
|
|
try { |
227 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
228 |
|
|
q.put(null); |
229 |
|
|
fail("put should throw NPE"); |
230 |
|
|
} |
231 |
|
|
catch (NullPointerException success){ |
232 |
|
|
} |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
public void testPut() { |
236 |
|
|
try { |
237 |
|
|
PriorityBlockingQueue q = new PriorityBlockingQueue(N); |
238 |
|
|
for (int i = 0; i < N; ++i) { |
239 |
|
|
Integer I = new Integer(i); |
240 |
|
|
q.put(I); |
241 |
|
|
assertTrue(q.contains(I)); |
242 |
|
|
} |
243 |
|
|
assertEquals(N, q.size()); |
244 |
|
|
} |
245 |
|
|
finally { |
246 |
|
|
} |
247 |
|
|
} |
248 |
|
|
|
249 |
|
|
public void testPutWithTake() { |
250 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
251 |
|
|
Thread t = new Thread(new Runnable() { |
252 |
|
|
public void run(){ |
253 |
|
|
int added = 0; |
254 |
|
|
try { |
255 |
|
|
q.put(new Integer(0)); |
256 |
|
|
++added; |
257 |
|
|
q.put(new Integer(0)); |
258 |
|
|
++added; |
259 |
|
|
q.put(new Integer(0)); |
260 |
|
|
++added; |
261 |
|
|
q.put(new Integer(0)); |
262 |
|
|
++added; |
263 |
|
|
assertTrue(added == 4); |
264 |
|
|
} finally { |
265 |
|
|
} |
266 |
|
|
} |
267 |
|
|
}); |
268 |
|
|
try { |
269 |
|
|
t.start(); |
270 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
271 |
|
|
q.take(); |
272 |
|
|
t.interrupt(); |
273 |
|
|
t.join(); |
274 |
|
|
} catch (Exception e){ |
275 |
|
|
fail("Unexpected exception"); |
276 |
|
|
} |
277 |
|
|
} |
278 |
|
|
|
279 |
|
|
public void testTimedOffer() { |
280 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
281 |
|
|
Thread t = new Thread(new Runnable() { |
282 |
|
|
public void run(){ |
283 |
|
|
try { |
284 |
|
|
q.put(new Integer(0)); |
285 |
|
|
q.put(new Integer(0)); |
286 |
|
|
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS/2, TimeUnit.MILLISECONDS)); |
287 |
|
|
assertTrue(q.offer(new Integer(0), LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
288 |
|
|
} finally { } |
289 |
|
|
} |
290 |
|
|
}); |
291 |
|
|
|
292 |
|
|
try { |
293 |
|
|
t.start(); |
294 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
295 |
|
|
t.interrupt(); |
296 |
|
|
t.join(); |
297 |
|
|
} catch (Exception e){ |
298 |
|
|
fail("Unexpected exception"); |
299 |
|
|
} |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
public void testTake(){ |
303 |
|
|
try { |
304 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
305 |
|
|
for (int i = 0; i < N; ++i) { |
306 |
|
|
assertEquals(i, ((Integer)q.take()).intValue()); |
307 |
|
|
} |
308 |
|
|
} catch (InterruptedException e){ |
309 |
|
|
fail("Unexpected exception"); |
310 |
|
|
} |
311 |
|
|
} |
312 |
|
|
|
313 |
|
|
public void testTakeFromEmpty() { |
314 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
315 |
|
|
Thread t = new Thread(new Runnable() { |
316 |
|
|
public void run(){ |
317 |
|
|
try { |
318 |
|
|
q.take(); |
319 |
|
|
fail("Should block"); |
320 |
|
|
} catch (InterruptedException success){ } |
321 |
|
|
} |
322 |
|
|
}); |
323 |
|
|
try { |
324 |
|
|
t.start(); |
325 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
326 |
|
|
t.interrupt(); |
327 |
|
|
t.join(); |
328 |
|
|
} catch (Exception e){ |
329 |
|
|
fail("Unexpected exception"); |
330 |
|
|
} |
331 |
|
|
} |
332 |
|
|
|
333 |
|
|
public void testBlockingTake(){ |
334 |
|
|
Thread t = new Thread(new Runnable() { |
335 |
|
|
public void run() { |
336 |
|
|
try { |
337 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
338 |
|
|
for (int i = 0; i < N; ++i) { |
339 |
|
|
assertEquals(i, ((Integer)q.take()).intValue()); |
340 |
|
|
} |
341 |
|
|
q.take(); |
342 |
|
|
fail("take should block"); |
343 |
|
|
} catch (InterruptedException success){ |
344 |
|
|
} |
345 |
|
|
}}); |
346 |
|
|
t.start(); |
347 |
|
|
try { |
348 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
349 |
|
|
t.interrupt(); |
350 |
|
|
t.join(); |
351 |
|
|
} |
352 |
|
|
catch (InterruptedException ie) { |
353 |
|
|
fail("Unexpected exception"); |
354 |
|
|
} |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
|
358 |
|
|
public void testPoll(){ |
359 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
360 |
|
|
for (int i = 0; i < N; ++i) { |
361 |
|
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
362 |
|
|
} |
363 |
|
|
assertNull(q.poll()); |
364 |
|
|
} |
365 |
|
|
|
366 |
|
|
public void testTimedPoll0() { |
367 |
|
|
try { |
368 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
369 |
|
|
for (int i = 0; i < N; ++i) { |
370 |
|
|
assertEquals(i, ((Integer)q.poll(0, TimeUnit.MILLISECONDS)).intValue()); |
371 |
|
|
} |
372 |
|
|
assertNull(q.poll(0, TimeUnit.MILLISECONDS)); |
373 |
|
|
} catch (InterruptedException e){ |
374 |
|
|
fail("Unexpected exception"); |
375 |
|
|
} |
376 |
|
|
} |
377 |
|
|
|
378 |
|
|
public void testTimedPoll() { |
379 |
|
|
try { |
380 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
381 |
|
|
for (int i = 0; i < N; ++i) { |
382 |
|
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
383 |
|
|
} |
384 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
385 |
|
|
} catch (InterruptedException e){ |
386 |
|
|
fail("Unexpected exception"); |
387 |
|
|
} |
388 |
|
|
} |
389 |
|
|
|
390 |
|
|
public void testInterruptedTimedPoll(){ |
391 |
|
|
Thread t = new Thread(new Runnable() { |
392 |
|
|
public void run() { |
393 |
|
|
try { |
394 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
395 |
|
|
for (int i = 0; i < N; ++i) { |
396 |
|
|
assertEquals(i, ((Integer)q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)).intValue()); |
397 |
|
|
} |
398 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
399 |
|
|
} catch (InterruptedException success){ |
400 |
|
|
} |
401 |
|
|
}}); |
402 |
|
|
t.start(); |
403 |
|
|
try { |
404 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
405 |
|
|
t.interrupt(); |
406 |
|
|
t.join(); |
407 |
|
|
} |
408 |
|
|
catch (InterruptedException ie) { |
409 |
|
|
fail("Unexpected exception"); |
410 |
|
|
} |
411 |
|
|
} |
412 |
|
|
|
413 |
|
|
public void testTimedPollWithOffer(){ |
414 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
415 |
|
|
Thread t = new Thread(new Runnable() { |
416 |
|
|
public void run(){ |
417 |
|
|
try { |
418 |
|
|
assertNull(q.poll(SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
419 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
420 |
|
|
q.poll(LONG_DELAY_MS, TimeUnit.MILLISECONDS); |
421 |
|
|
fail("Should block"); |
422 |
|
|
} catch (InterruptedException success) { } |
423 |
|
|
} |
424 |
|
|
}); |
425 |
|
|
try { |
426 |
|
|
t.start(); |
427 |
|
|
Thread.sleep(SHORT_DELAY_MS * 2); |
428 |
|
|
assertTrue(q.offer(new Integer(0), SHORT_DELAY_MS, TimeUnit.MILLISECONDS)); |
429 |
|
|
t.interrupt(); |
430 |
|
|
t.join(); |
431 |
|
|
} catch (Exception e){ |
432 |
|
|
fail("Unexpected exception"); |
433 |
|
|
} |
434 |
|
|
} |
435 |
|
|
|
436 |
|
|
|
437 |
|
|
public void testPeek(){ |
438 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
439 |
|
|
for (int i = 0; i < N; ++i) { |
440 |
|
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
441 |
|
|
q.poll(); |
442 |
|
|
assertTrue(q.peek() == null || |
443 |
|
|
i != ((Integer)q.peek()).intValue()); |
444 |
|
|
} |
445 |
|
|
assertNull(q.peek()); |
446 |
|
|
} |
447 |
|
|
|
448 |
|
|
public void testElement(){ |
449 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
450 |
|
|
for (int i = 0; i < N; ++i) { |
451 |
|
|
assertEquals(i, ((Integer)q.element()).intValue()); |
452 |
|
|
q.poll(); |
453 |
|
|
} |
454 |
|
|
try { |
455 |
|
|
q.element(); |
456 |
|
|
fail("no such element"); |
457 |
|
|
} |
458 |
|
|
catch (NoSuchElementException success) {} |
459 |
|
|
} |
460 |
|
|
|
461 |
|
|
public void testRemove(){ |
462 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
463 |
|
|
for (int i = 0; i < N; ++i) { |
464 |
|
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
465 |
|
|
} |
466 |
|
|
try { |
467 |
|
|
q.remove(); |
468 |
|
|
fail("remove should throw"); |
469 |
|
|
} catch (NoSuchElementException success){ |
470 |
|
|
} |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
public void testRemoveElement(){ |
474 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
475 |
|
|
for (int i = 1; i < N; i+=2) { |
476 |
|
|
assertTrue(q.remove(new Integer(i))); |
477 |
|
|
} |
478 |
|
|
for (int i = 0; i < N; i+=2) { |
479 |
|
|
assertTrue(q.remove(new Integer(i))); |
480 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
481 |
|
|
} |
482 |
|
|
assert(q.isEmpty()); |
483 |
|
|
} |
484 |
|
|
|
485 |
|
|
public void testContains(){ |
486 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
487 |
|
|
for (int i = 0; i < N; ++i) { |
488 |
|
|
assertTrue(q.contains(new Integer(i))); |
489 |
|
|
q.poll(); |
490 |
|
|
assertFalse(q.contains(new Integer(i))); |
491 |
|
|
} |
492 |
|
|
} |
493 |
|
|
|
494 |
|
|
public void testClear(){ |
495 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
496 |
|
|
q.clear(); |
497 |
|
|
assertTrue(q.isEmpty()); |
498 |
|
|
assertEquals(0, q.size()); |
499 |
|
|
assertEquals(NOCAP, q.remainingCapacity()); |
500 |
|
|
q.add(new Integer(1)); |
501 |
|
|
assertFalse(q.isEmpty()); |
502 |
|
|
q.clear(); |
503 |
|
|
assertTrue(q.isEmpty()); |
504 |
|
|
} |
505 |
|
|
|
506 |
|
|
public void testContainsAll(){ |
507 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
508 |
|
|
PriorityBlockingQueue p = new PriorityBlockingQueue(N); |
509 |
|
|
for (int i = 0; i < N; ++i) { |
510 |
|
|
assertTrue(q.containsAll(p)); |
511 |
|
|
assertFalse(p.containsAll(q)); |
512 |
|
|
p.add(new Integer(i)); |
513 |
|
|
} |
514 |
|
|
assertTrue(p.containsAll(q)); |
515 |
|
|
} |
516 |
|
|
|
517 |
|
|
public void testRetainAll(){ |
518 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
519 |
|
|
PriorityBlockingQueue p = fullQueue(N); |
520 |
|
|
for (int i = 0; i < N; ++i) { |
521 |
|
|
boolean changed = q.retainAll(p); |
522 |
|
|
if (i == 0) |
523 |
|
|
assertFalse(changed); |
524 |
|
|
else |
525 |
|
|
assertTrue(changed); |
526 |
|
|
|
527 |
|
|
assertTrue(q.containsAll(p)); |
528 |
|
|
assertEquals(N-i, q.size()); |
529 |
|
|
p.remove(); |
530 |
|
|
} |
531 |
|
|
} |
532 |
|
|
|
533 |
|
|
public void testRemoveAll(){ |
534 |
|
|
for (int i = 1; i < N; ++i) { |
535 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
536 |
|
|
PriorityBlockingQueue p = fullQueue(i); |
537 |
|
|
assertTrue(q.removeAll(p)); |
538 |
|
|
assertEquals(N-i, q.size()); |
539 |
|
|
for (int j = 0; j < i; ++j) { |
540 |
|
|
Integer I = (Integer)(p.remove()); |
541 |
|
|
assertFalse(q.contains(I)); |
542 |
|
|
} |
543 |
|
|
} |
544 |
|
|
} |
545 |
|
|
|
546 |
|
|
public void testToArray(){ |
547 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
548 |
|
|
Object[] o = q.toArray(); |
549 |
|
|
Arrays.sort(o); |
550 |
|
|
try { |
551 |
|
|
for(int i = 0; i < o.length; i++) |
552 |
|
|
assertEquals(o[i], q.take()); |
553 |
|
|
} catch (InterruptedException e){ |
554 |
|
|
fail("Unexpected exception"); |
555 |
|
|
} |
556 |
|
|
} |
557 |
|
|
|
558 |
|
|
public void testToArray2(){ |
559 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
560 |
|
|
Integer[] ints = new Integer[N]; |
561 |
|
|
ints = (Integer[])q.toArray(ints); |
562 |
|
|
Arrays.sort(ints); |
563 |
|
|
try { |
564 |
|
|
for(int i = 0; i < ints.length; i++) |
565 |
|
|
assertEquals(ints[i], q.take()); |
566 |
|
|
} catch (InterruptedException e){ |
567 |
|
|
fail("Unexpected exception"); |
568 |
|
|
} |
569 |
|
|
} |
570 |
|
|
|
571 |
|
|
public void testIterator(){ |
572 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
573 |
|
|
int i = 0; |
574 |
|
|
Iterator it = q.iterator(); |
575 |
|
|
while(it.hasNext()) { |
576 |
|
|
assertTrue(q.contains(it.next())); |
577 |
|
|
++i; |
578 |
|
|
} |
579 |
|
|
assertEquals(i, N); |
580 |
|
|
} |
581 |
|
|
|
582 |
|
|
public void testIteratorRemove () { |
583 |
|
|
|
584 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(3); |
585 |
|
|
|
586 |
|
|
q.add(new Integer(2)); |
587 |
|
|
q.add(new Integer(1)); |
588 |
|
|
q.add(new Integer(3)); |
589 |
|
|
|
590 |
|
|
Iterator it = q.iterator(); |
591 |
|
|
it.next(); |
592 |
|
|
it.remove(); |
593 |
|
|
|
594 |
|
|
it = q.iterator(); |
595 |
|
|
assertEquals(it.next(), new Integer(2)); |
596 |
|
|
assertEquals(it.next(), new Integer(3)); |
597 |
|
|
assertFalse(it.hasNext()); |
598 |
|
|
} |
599 |
|
|
|
600 |
|
|
|
601 |
|
|
public void testToString(){ |
602 |
|
|
PriorityBlockingQueue q = fullQueue(N); |
603 |
|
|
String s = q.toString(); |
604 |
|
|
for (int i = 0; i < N; ++i) { |
605 |
|
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
606 |
|
|
} |
607 |
|
|
} |
608 |
|
|
|
609 |
|
|
public void testPollInExecutor() { |
610 |
|
|
|
611 |
|
|
final PriorityBlockingQueue q = new PriorityBlockingQueue(2); |
612 |
|
|
|
613 |
|
|
ExecutorService executor = Executors.newFixedThreadPool(2); |
614 |
|
|
|
615 |
|
|
executor.execute(new Runnable() { |
616 |
|
|
public void run() { |
617 |
|
|
assertNull("poll should fail", q.poll()); |
618 |
|
|
try { |
619 |
|
|
assertTrue(null != q.poll(MEDIUM_DELAY_MS * 2, TimeUnit.MILLISECONDS)); |
620 |
|
|
assertTrue(q.isEmpty()); |
621 |
|
|
} |
622 |
|
|
catch (InterruptedException e) { |
623 |
|
|
fail("should not be interrupted"); |
624 |
|
|
} |
625 |
|
|
} |
626 |
|
|
}); |
627 |
|
|
|
628 |
|
|
executor.execute(new Runnable() { |
629 |
|
|
public void run() { |
630 |
|
|
try { |
631 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
632 |
|
|
q.put(new Integer(1)); |
633 |
|
|
} |
634 |
|
|
catch (InterruptedException e) { |
635 |
|
|
fail("should not be interrupted"); |
636 |
|
|
} |
637 |
|
|
} |
638 |
|
|
}); |
639 |
|
|
|
640 |
|
|
executor.shutdown(); |
641 |
|
|
|
642 |
|
|
} |
643 |
|
|
|
644 |
|
|
} |