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 |
import java.io.*; |
12 |
|
13 |
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
14 |
|
15 |
public static void main(String[] args) { |
16 |
junit.textui.TestRunner.run (suite()); |
17 |
} |
18 |
|
19 |
public static Test suite() { |
20 |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
21 |
} |
22 |
|
23 |
/** |
24 |
* Create a queue of given size containing consecutive |
25 |
* Integers 0 ... n. |
26 |
*/ |
27 |
private ConcurrentLinkedQueue populatedQueue(int n) { |
28 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
29 |
assertTrue(q.isEmpty()); |
30 |
for(int i = 0; i < n; ++i) |
31 |
assertTrue(q.offer(new Integer(i))); |
32 |
assertFalse(q.isEmpty()); |
33 |
assertEquals(n, q.size()); |
34 |
return q; |
35 |
} |
36 |
|
37 |
/** |
38 |
* new queue is empty |
39 |
*/ |
40 |
public void testConstructor1() { |
41 |
assertEquals(0, new ConcurrentLinkedQueue().size()); |
42 |
} |
43 |
|
44 |
/** |
45 |
* Initializing from null Collection throws NPE |
46 |
*/ |
47 |
public void testConstructor3() { |
48 |
try { |
49 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); |
50 |
shouldThrow(); |
51 |
} |
52 |
catch (NullPointerException success) {} |
53 |
} |
54 |
|
55 |
/** |
56 |
* Initializing from Collection of null elements throws NPE |
57 |
*/ |
58 |
public void testConstructor4() { |
59 |
try { |
60 |
Integer[] ints = new Integer[SIZE]; |
61 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
62 |
shouldThrow(); |
63 |
} |
64 |
catch (NullPointerException success) {} |
65 |
} |
66 |
|
67 |
/** |
68 |
* Initializing from Collection with some null elements throws NPE |
69 |
*/ |
70 |
public void testConstructor5() { |
71 |
try { |
72 |
Integer[] ints = new Integer[SIZE]; |
73 |
for (int i = 0; i < SIZE-1; ++i) |
74 |
ints[i] = new Integer(i); |
75 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
76 |
shouldThrow(); |
77 |
} |
78 |
catch (NullPointerException success) {} |
79 |
} |
80 |
|
81 |
/** |
82 |
* Queue contains all elements of collection used to initialize |
83 |
*/ |
84 |
public void testConstructor6() { |
85 |
try { |
86 |
Integer[] ints = new Integer[SIZE]; |
87 |
for (int i = 0; i < SIZE; ++i) |
88 |
ints[i] = new Integer(i); |
89 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
90 |
for (int i = 0; i < SIZE; ++i) |
91 |
assertEquals(ints[i], q.poll()); |
92 |
} |
93 |
finally {} |
94 |
} |
95 |
|
96 |
/** |
97 |
* isEmpty is true before add, false after |
98 |
*/ |
99 |
public void testEmpty() { |
100 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
101 |
assertTrue(q.isEmpty()); |
102 |
q.add(one); |
103 |
assertFalse(q.isEmpty()); |
104 |
q.add(two); |
105 |
q.remove(); |
106 |
q.remove(); |
107 |
assertTrue(q.isEmpty()); |
108 |
} |
109 |
|
110 |
/** |
111 |
* size changes when elements added and removed |
112 |
*/ |
113 |
public void testSize() { |
114 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
115 |
for (int i = 0; i < SIZE; ++i) { |
116 |
assertEquals(SIZE-i, q.size()); |
117 |
q.remove(); |
118 |
} |
119 |
for (int i = 0; i < SIZE; ++i) { |
120 |
assertEquals(i, q.size()); |
121 |
q.add(new Integer(i)); |
122 |
} |
123 |
} |
124 |
|
125 |
/** |
126 |
* offer(null) throws NPE |
127 |
*/ |
128 |
public void testOfferNull() { |
129 |
try { |
130 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
131 |
q.offer(null); |
132 |
shouldThrow(); |
133 |
} catch (NullPointerException success) { } |
134 |
} |
135 |
|
136 |
/** |
137 |
* add(null) throws NPE |
138 |
*/ |
139 |
public void testAddNull() { |
140 |
try { |
141 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
142 |
q.add(null); |
143 |
shouldThrow(); |
144 |
} catch (NullPointerException success) { } |
145 |
} |
146 |
|
147 |
|
148 |
/** |
149 |
* Offer returns true |
150 |
*/ |
151 |
public void testOffer() { |
152 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
153 |
assertTrue(q.offer(zero)); |
154 |
assertTrue(q.offer(one)); |
155 |
} |
156 |
|
157 |
/** |
158 |
* add returns true |
159 |
*/ |
160 |
public void testAdd() { |
161 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
162 |
for (int i = 0; i < SIZE; ++i) { |
163 |
assertEquals(i, q.size()); |
164 |
assertTrue(q.add(new Integer(i))); |
165 |
} |
166 |
} |
167 |
|
168 |
/** |
169 |
* addAll(null) throws NPE |
170 |
*/ |
171 |
public void testAddAll1() { |
172 |
try { |
173 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
174 |
q.addAll(null); |
175 |
shouldThrow(); |
176 |
} |
177 |
catch (NullPointerException success) {} |
178 |
} |
179 |
|
180 |
/** |
181 |
* addAll(this) throws IAE |
182 |
*/ |
183 |
public void testAddAllSelf() { |
184 |
try { |
185 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
186 |
q.addAll(q); |
187 |
shouldThrow(); |
188 |
} |
189 |
catch (IllegalArgumentException success) {} |
190 |
} |
191 |
|
192 |
/** |
193 |
* addAll of a collection with null elements throws NPE |
194 |
*/ |
195 |
public void testAddAll2() { |
196 |
try { |
197 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
198 |
Integer[] ints = new Integer[SIZE]; |
199 |
q.addAll(Arrays.asList(ints)); |
200 |
shouldThrow(); |
201 |
} |
202 |
catch (NullPointerException success) {} |
203 |
} |
204 |
/** |
205 |
* addAll of a collection with any null elements throws NPE after |
206 |
* possibly adding some elements |
207 |
*/ |
208 |
public void testAddAll3() { |
209 |
try { |
210 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
211 |
Integer[] ints = new Integer[SIZE]; |
212 |
for (int i = 0; i < SIZE-1; ++i) |
213 |
ints[i] = new Integer(i); |
214 |
q.addAll(Arrays.asList(ints)); |
215 |
shouldThrow(); |
216 |
} |
217 |
catch (NullPointerException success) {} |
218 |
} |
219 |
|
220 |
/** |
221 |
* Queue contains all elements, in traversal order, of successful addAll |
222 |
*/ |
223 |
public void testAddAll5() { |
224 |
try { |
225 |
Integer[] empty = new Integer[0]; |
226 |
Integer[] ints = new Integer[SIZE]; |
227 |
for (int i = 0; i < SIZE; ++i) |
228 |
ints[i] = new Integer(i); |
229 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
230 |
assertFalse(q.addAll(Arrays.asList(empty))); |
231 |
assertTrue(q.addAll(Arrays.asList(ints))); |
232 |
for (int i = 0; i < SIZE; ++i) |
233 |
assertEquals(ints[i], q.poll()); |
234 |
} |
235 |
finally {} |
236 |
} |
237 |
|
238 |
/** |
239 |
* poll succeeds unless empty |
240 |
*/ |
241 |
public void testPoll() { |
242 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
243 |
for (int i = 0; i < SIZE; ++i) { |
244 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
245 |
} |
246 |
assertNull(q.poll()); |
247 |
} |
248 |
|
249 |
/** |
250 |
* peek returns next element, or null if empty |
251 |
*/ |
252 |
public void testPeek() { |
253 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
254 |
for (int i = 0; i < SIZE; ++i) { |
255 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
256 |
q.poll(); |
257 |
assertTrue(q.peek() == null || |
258 |
i != ((Integer)q.peek()).intValue()); |
259 |
} |
260 |
assertNull(q.peek()); |
261 |
} |
262 |
|
263 |
/** |
264 |
* element returns next element, or throws NSEE if empty |
265 |
*/ |
266 |
public void testElement() { |
267 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
268 |
for (int i = 0; i < SIZE; ++i) { |
269 |
assertEquals(i, ((Integer)q.element()).intValue()); |
270 |
q.poll(); |
271 |
} |
272 |
try { |
273 |
q.element(); |
274 |
shouldThrow(); |
275 |
} |
276 |
catch (NoSuchElementException success) {} |
277 |
} |
278 |
|
279 |
/** |
280 |
* remove removes next element, or throws NSEE if empty |
281 |
*/ |
282 |
public void testRemove() { |
283 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
284 |
for (int i = 0; i < SIZE; ++i) { |
285 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
286 |
} |
287 |
try { |
288 |
q.remove(); |
289 |
shouldThrow(); |
290 |
} catch (NoSuchElementException success){ |
291 |
} |
292 |
} |
293 |
|
294 |
/** |
295 |
* remove(x) removes x and returns true if present |
296 |
*/ |
297 |
public void testRemoveElement() { |
298 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
299 |
for (int i = 1; i < SIZE; i+=2) { |
300 |
assertTrue(q.remove(new Integer(i))); |
301 |
} |
302 |
for (int i = 0; i < SIZE; i+=2) { |
303 |
assertTrue(q.remove(new Integer(i))); |
304 |
assertFalse(q.remove(new Integer(i+1))); |
305 |
} |
306 |
assertTrue(q.isEmpty()); |
307 |
} |
308 |
|
309 |
/** |
310 |
* contains(x) reports true when elements added but not yet removed |
311 |
*/ |
312 |
public void testContains() { |
313 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
314 |
for (int i = 0; i < SIZE; ++i) { |
315 |
assertTrue(q.contains(new Integer(i))); |
316 |
q.poll(); |
317 |
assertFalse(q.contains(new Integer(i))); |
318 |
} |
319 |
} |
320 |
|
321 |
/** |
322 |
* clear removes all elements |
323 |
*/ |
324 |
public void testClear() { |
325 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
326 |
q.clear(); |
327 |
assertTrue(q.isEmpty()); |
328 |
assertEquals(0, q.size()); |
329 |
q.add(one); |
330 |
assertFalse(q.isEmpty()); |
331 |
q.clear(); |
332 |
assertTrue(q.isEmpty()); |
333 |
} |
334 |
|
335 |
/** |
336 |
* containsAll(c) is true when c contains a subset of elements |
337 |
*/ |
338 |
public void testContainsAll() { |
339 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
340 |
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue(); |
341 |
for (int i = 0; i < SIZE; ++i) { |
342 |
assertTrue(q.containsAll(p)); |
343 |
assertFalse(p.containsAll(q)); |
344 |
p.add(new Integer(i)); |
345 |
} |
346 |
assertTrue(p.containsAll(q)); |
347 |
} |
348 |
|
349 |
/** |
350 |
* retainAll(c) retains only those elements of c and reports true if change |
351 |
*/ |
352 |
public void testRetainAll() { |
353 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
354 |
ConcurrentLinkedQueue p = populatedQueue(SIZE); |
355 |
for (int i = 0; i < SIZE; ++i) { |
356 |
boolean changed = q.retainAll(p); |
357 |
if (i == 0) |
358 |
assertFalse(changed); |
359 |
else |
360 |
assertTrue(changed); |
361 |
|
362 |
assertTrue(q.containsAll(p)); |
363 |
assertEquals(SIZE-i, q.size()); |
364 |
p.remove(); |
365 |
} |
366 |
} |
367 |
|
368 |
/** |
369 |
* removeAll(c) removes only those elements of c and reports true if changed |
370 |
*/ |
371 |
public void testRemoveAll() { |
372 |
for (int i = 1; i < SIZE; ++i) { |
373 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
374 |
ConcurrentLinkedQueue p = populatedQueue(i); |
375 |
assertTrue(q.removeAll(p)); |
376 |
assertEquals(SIZE-i, q.size()); |
377 |
for (int j = 0; j < i; ++j) { |
378 |
Integer I = (Integer)(p.remove()); |
379 |
assertFalse(q.contains(I)); |
380 |
} |
381 |
} |
382 |
} |
383 |
|
384 |
/** |
385 |
* toArray contains all elements |
386 |
*/ |
387 |
public void testToArray() { |
388 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
389 |
Object[] o = q.toArray(); |
390 |
Arrays.sort(o); |
391 |
for(int i = 0; i < o.length; i++) |
392 |
assertEquals(o[i], q.poll()); |
393 |
} |
394 |
|
395 |
/** |
396 |
* toArray(a) contains all elements |
397 |
*/ |
398 |
public void testToArray2() { |
399 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
400 |
Integer[] ints = new Integer[SIZE]; |
401 |
ints = (Integer[])q.toArray(ints); |
402 |
Arrays.sort(ints); |
403 |
for(int i = 0; i < ints.length; i++) |
404 |
assertEquals(ints[i], q.poll()); |
405 |
} |
406 |
|
407 |
/** |
408 |
* toArray(null) throws NPE |
409 |
*/ |
410 |
public void testToArray_BadArg() { |
411 |
try { |
412 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
413 |
Object o[] = q.toArray(null); |
414 |
shouldThrow(); |
415 |
} catch(NullPointerException success){} |
416 |
} |
417 |
|
418 |
/** |
419 |
* toArray with incompatable array type throws CCE |
420 |
*/ |
421 |
public void testToArray1_BadArg() { |
422 |
try { |
423 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
424 |
Object o[] = q.toArray(new String[10] ); |
425 |
shouldThrow(); |
426 |
} catch(ArrayStoreException success){} |
427 |
} |
428 |
|
429 |
/** |
430 |
* iterator iterates through all elements |
431 |
*/ |
432 |
public void testIterator() { |
433 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
434 |
int i = 0; |
435 |
Iterator it = q.iterator(); |
436 |
while(it.hasNext()) { |
437 |
assertTrue(q.contains(it.next())); |
438 |
++i; |
439 |
} |
440 |
assertEquals(i, SIZE); |
441 |
} |
442 |
|
443 |
/** |
444 |
* iterator ordering is FIFO |
445 |
*/ |
446 |
public void testIteratorOrdering() { |
447 |
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
448 |
q.add(one); |
449 |
q.add(two); |
450 |
q.add(three); |
451 |
|
452 |
int k = 0; |
453 |
for (Iterator it = q.iterator(); it.hasNext();) { |
454 |
int i = ((Integer)(it.next())).intValue(); |
455 |
assertEquals(++k, i); |
456 |
} |
457 |
|
458 |
assertEquals(3, k); |
459 |
} |
460 |
|
461 |
/** |
462 |
* Modifications do not cause iterators to fail |
463 |
*/ |
464 |
public void testWeaklyConsistentIteration () { |
465 |
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
466 |
q.add(one); |
467 |
q.add(two); |
468 |
q.add(three); |
469 |
|
470 |
try { |
471 |
for (Iterator it = q.iterator(); it.hasNext();) { |
472 |
q.remove(); |
473 |
it.next(); |
474 |
} |
475 |
} |
476 |
catch (ConcurrentModificationException e) { |
477 |
shouldThrow(); |
478 |
} |
479 |
|
480 |
assertEquals("queue should be empty again", 0, q.size()); |
481 |
} |
482 |
|
483 |
/** |
484 |
* iterator.remove removes current element |
485 |
*/ |
486 |
public void testIteratorRemove () { |
487 |
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
488 |
q.add(one); |
489 |
q.add(two); |
490 |
q.add(three); |
491 |
Iterator it = q.iterator(); |
492 |
it.next(); |
493 |
it.remove(); |
494 |
it = q.iterator(); |
495 |
assertEquals(it.next(), two); |
496 |
assertEquals(it.next(), three); |
497 |
assertFalse(it.hasNext()); |
498 |
} |
499 |
|
500 |
|
501 |
/** |
502 |
* toString contains toStrings of elements |
503 |
*/ |
504 |
public void testToString() { |
505 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
506 |
String s = q.toString(); |
507 |
for (int i = 0; i < SIZE; ++i) { |
508 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
509 |
} |
510 |
} |
511 |
|
512 |
/** |
513 |
* A deserialized serialized queue has same elements in same order |
514 |
*/ |
515 |
public void testSerialization() { |
516 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
517 |
try { |
518 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
519 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
520 |
out.writeObject(q); |
521 |
out.close(); |
522 |
|
523 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
524 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
525 |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
526 |
assertEquals(q.size(), r.size()); |
527 |
while (!q.isEmpty()) |
528 |
assertEquals(q.remove(), r.remove()); |
529 |
} catch(Exception e){ |
530 |
unexpectedException(); |
531 |
} |
532 |
} |
533 |
|
534 |
} |