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. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
14 |
|
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
15 |
|
|
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run (suite()); |
18 |
|
} |
19 |
|
|
20 |
|
public static Test suite() { |
28 |
|
private ConcurrentLinkedQueue populatedQueue(int n) { |
29 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
for(int i = 0; i < n; ++i) |
31 |
> |
for (int i = 0; i < n; ++i) |
32 |
|
assertTrue(q.offer(new Integer(i))); |
33 |
|
assertFalse(q.isEmpty()); |
34 |
|
assertEquals(n, q.size()); |
35 |
|
return q; |
36 |
|
} |
37 |
< |
|
37 |
> |
|
38 |
|
/** |
39 |
< |
* |
39 |
> |
* new queue is empty |
40 |
|
*/ |
41 |
|
public void testConstructor1() { |
42 |
|
assertEquals(0, new ConcurrentLinkedQueue().size()); |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* |
46 |
> |
* Initializing from null Collection throws NPE |
47 |
|
*/ |
48 |
|
public void testConstructor3() { |
49 |
|
try { |
54 |
|
} |
55 |
|
|
56 |
|
/** |
57 |
< |
* |
57 |
> |
* Initializing from Collection of null elements throws NPE |
58 |
|
*/ |
59 |
|
public void testConstructor4() { |
60 |
|
try { |
66 |
|
} |
67 |
|
|
68 |
|
/** |
69 |
< |
* |
69 |
> |
* Initializing from Collection with some null elements throws NPE |
70 |
|
*/ |
71 |
|
public void testConstructor5() { |
72 |
|
try { |
80 |
|
} |
81 |
|
|
82 |
|
/** |
83 |
< |
* |
83 |
> |
* Queue contains all elements of collection used to initialize |
84 |
|
*/ |
85 |
|
public void testConstructor6() { |
86 |
|
try { |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* |
98 |
> |
* isEmpty is true before add, false after |
99 |
|
*/ |
100 |
|
public void testEmpty() { |
101 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* |
112 |
> |
* size changes when elements added and removed |
113 |
|
*/ |
114 |
|
public void testSize() { |
115 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* |
127 |
> |
* offer(null) throws NPE |
128 |
|
*/ |
129 |
|
public void testOfferNull() { |
130 |
|
try { |
131 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
132 |
|
q.offer(null); |
133 |
|
shouldThrow(); |
134 |
< |
} catch (NullPointerException success) { } |
134 |
> |
} catch (NullPointerException success) { } |
135 |
|
} |
136 |
|
|
137 |
|
/** |
138 |
< |
* |
138 |
> |
* add(null) throws NPE |
139 |
> |
*/ |
140 |
> |
public void testAddNull() { |
141 |
> |
try { |
142 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
143 |
> |
q.add(null); |
144 |
> |
shouldThrow(); |
145 |
> |
} catch (NullPointerException success) { } |
146 |
> |
} |
147 |
> |
|
148 |
> |
|
149 |
> |
/** |
150 |
> |
* Offer returns true |
151 |
|
*/ |
152 |
|
public void testOffer() { |
153 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
159 |
< |
* |
159 |
> |
* add returns true |
160 |
|
*/ |
161 |
|
public void testAdd() { |
162 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
170 |
< |
* |
170 |
> |
* addAll(null) throws NPE |
171 |
|
*/ |
172 |
|
public void testAddAll1() { |
173 |
|
try { |
177 |
|
} |
178 |
|
catch (NullPointerException success) {} |
179 |
|
} |
180 |
+ |
|
181 |
|
/** |
182 |
< |
* |
182 |
> |
* addAll(this) throws IAE |
183 |
> |
*/ |
184 |
> |
public void testAddAllSelf() { |
185 |
> |
try { |
186 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
187 |
> |
q.addAll(q); |
188 |
> |
shouldThrow(); |
189 |
> |
} |
190 |
> |
catch (IllegalArgumentException success) {} |
191 |
> |
} |
192 |
> |
|
193 |
> |
/** |
194 |
> |
* addAll of a collection with null elements throws NPE |
195 |
|
*/ |
196 |
|
public void testAddAll2() { |
197 |
|
try { |
203 |
|
catch (NullPointerException success) {} |
204 |
|
} |
205 |
|
/** |
206 |
< |
* |
206 |
> |
* addAll of a collection with any null elements throws NPE after |
207 |
> |
* possibly adding some elements |
208 |
|
*/ |
209 |
|
public void testAddAll3() { |
210 |
|
try { |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* |
222 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
223 |
|
*/ |
224 |
|
public void testAddAll5() { |
225 |
|
try { |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
< |
* |
240 |
> |
* poll succeeds unless empty |
241 |
|
*/ |
242 |
|
public void testPoll() { |
243 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
248 |
|
} |
249 |
|
|
250 |
|
/** |
251 |
< |
* |
251 |
> |
* peek returns next element, or null if empty |
252 |
|
*/ |
253 |
|
public void testPeek() { |
254 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
262 |
|
} |
263 |
|
|
264 |
|
/** |
265 |
< |
* |
265 |
> |
* element returns next element, or throws NSEE if empty |
266 |
|
*/ |
267 |
|
public void testElement() { |
268 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
278 |
|
} |
279 |
|
|
280 |
|
/** |
281 |
< |
* |
281 |
> |
* remove removes next element, or throws NSEE if empty |
282 |
|
*/ |
283 |
|
public void testRemove() { |
284 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
289 |
|
q.remove(); |
290 |
|
shouldThrow(); |
291 |
|
} catch (NoSuchElementException success){ |
292 |
< |
} |
292 |
> |
} |
293 |
|
} |
294 |
|
|
295 |
|
/** |
296 |
< |
* |
296 |
> |
* remove(x) removes x and returns true if present |
297 |
|
*/ |
298 |
|
public void testRemoveElement() { |
299 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
306 |
|
} |
307 |
|
assertTrue(q.isEmpty()); |
308 |
|
} |
309 |
< |
|
309 |
> |
|
310 |
|
/** |
311 |
< |
* |
311 |
> |
* contains(x) reports true when elements added but not yet removed |
312 |
|
*/ |
313 |
|
public void testContains() { |
314 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
320 |
|
} |
321 |
|
|
322 |
|
/** |
323 |
< |
* |
323 |
> |
* clear removes all elements |
324 |
|
*/ |
325 |
|
public void testClear() { |
326 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
334 |
|
} |
335 |
|
|
336 |
|
/** |
337 |
< |
* |
337 |
> |
* containsAll(c) is true when c contains a subset of elements |
338 |
|
*/ |
339 |
|
public void testContainsAll() { |
340 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* |
351 |
> |
* retainAll(c) retains only those elements of c and reports true if change |
352 |
|
*/ |
353 |
|
public void testRetainAll() { |
354 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
< |
* |
370 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
371 |
|
*/ |
372 |
|
public void testRemoveAll() { |
373 |
|
for (int i = 1; i < SIZE; ++i) { |
383 |
|
} |
384 |
|
|
385 |
|
/** |
386 |
< |
* |
386 |
> |
* toArray contains all elements |
387 |
|
*/ |
388 |
|
public void testToArray() { |
389 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
390 |
|
Object[] o = q.toArray(); |
391 |
|
Arrays.sort(o); |
392 |
< |
for(int i = 0; i < o.length; i++) |
392 |
> |
for (int i = 0; i < o.length; i++) |
393 |
|
assertEquals(o[i], q.poll()); |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
< |
* |
397 |
> |
* toArray(a) contains all elements |
398 |
|
*/ |
399 |
|
public void testToArray2() { |
400 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
401 |
|
Integer[] ints = new Integer[SIZE]; |
402 |
|
ints = (Integer[])q.toArray(ints); |
403 |
|
Arrays.sort(ints); |
404 |
< |
for(int i = 0; i < ints.length; i++) |
404 |
> |
for (int i = 0; i < ints.length; i++) |
405 |
|
assertEquals(ints[i], q.poll()); |
406 |
|
} |
407 |
< |
|
407 |
> |
|
408 |
> |
/** |
409 |
> |
* toArray(null) throws NPE |
410 |
> |
*/ |
411 |
> |
public void testToArray_BadArg() { |
412 |
> |
try { |
413 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
414 |
> |
Object o[] = q.toArray(null); |
415 |
> |
shouldThrow(); |
416 |
> |
} catch (NullPointerException success){} |
417 |
> |
} |
418 |
> |
|
419 |
> |
/** |
420 |
> |
* toArray with incompatible array type throws CCE |
421 |
> |
*/ |
422 |
> |
public void testToArray1_BadArg() { |
423 |
> |
try { |
424 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
425 |
> |
Object o[] = q.toArray(new String[10] ); |
426 |
> |
shouldThrow(); |
427 |
> |
} catch (ArrayStoreException success){} |
428 |
> |
} |
429 |
> |
|
430 |
|
/** |
431 |
< |
* |
431 |
> |
* iterator iterates through all elements |
432 |
|
*/ |
433 |
|
public void testIterator() { |
434 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
435 |
|
int i = 0; |
436 |
|
Iterator it = q.iterator(); |
437 |
< |
while(it.hasNext()) { |
437 |
> |
while (it.hasNext()) { |
438 |
|
assertTrue(q.contains(it.next())); |
439 |
|
++i; |
440 |
|
} |
442 |
|
} |
443 |
|
|
444 |
|
/** |
445 |
< |
* |
445 |
> |
* iterator ordering is FIFO |
446 |
|
*/ |
447 |
|
public void testIteratorOrdering() { |
448 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
< |
* |
463 |
> |
* Modifications do not cause iterators to fail |
464 |
|
*/ |
465 |
|
public void testWeaklyConsistentIteration () { |
466 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
< |
* |
485 |
> |
* iterator.remove removes current element |
486 |
|
*/ |
487 |
|
public void testIteratorRemove () { |
488 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
500 |
|
|
501 |
|
|
502 |
|
/** |
503 |
< |
* |
503 |
> |
* toString contains toStrings of elements |
504 |
|
*/ |
505 |
|
public void testToString() { |
506 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
508 |
|
for (int i = 0; i < SIZE; ++i) { |
509 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
510 |
|
} |
511 |
< |
} |
511 |
> |
} |
512 |
|
|
513 |
|
/** |
514 |
< |
* |
514 |
> |
* A deserialized serialized queue has same elements in same order |
515 |
|
*/ |
516 |
|
public void testSerialization() { |
517 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
525 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
526 |
|
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
527 |
|
assertEquals(q.size(), r.size()); |
528 |
< |
while (!q.isEmpty()) |
528 |
> |
while (!q.isEmpty()) |
529 |
|
assertEquals(q.remove(), r.remove()); |
530 |
< |
} catch(Exception e){ |
530 |
> |
} catch (Exception e){ |
531 |
|
unexpectedException(); |
532 |
|
} |
533 |
|
} |