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