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