1 |
|
/* |
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. |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
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() { |
21 |
< |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
21 |
> |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
22 |
|
} |
23 |
|
|
24 |
|
/** |
25 |
|
* Create a queue of given size containing consecutive |
26 |
|
* Integers 0 ... n. |
27 |
|
*/ |
28 |
< |
private ConcurrentLinkedQueue populatedQueue(int n) { |
29 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
28 |
> |
private ConcurrentLinkedQueue<Integer> populatedQueue(int n) { |
29 |
> |
ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>(); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
for(int i = 0; i < n; ++i) |
32 |
< |
assertTrue(q.offer(new Integer(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()); |
34 |
> |
assertEquals(n, q.size()); |
35 |
|
return q; |
36 |
|
} |
37 |
< |
|
37 |
> |
|
38 |
|
/** |
39 |
|
* new queue is empty |
40 |
|
*/ |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* Initializing from null Collection throws NPE |
46 |
> |
* Initializing from null Collection throws NPE |
47 |
|
*/ |
48 |
|
public void testConstructor3() { |
49 |
|
try { |
50 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); |
51 |
|
shouldThrow(); |
52 |
< |
} |
53 |
< |
catch (NullPointerException success) {} |
52 |
> |
} catch (NullPointerException success) {} |
53 |
|
} |
54 |
|
|
55 |
|
/** |
60 |
|
Integer[] ints = new Integer[SIZE]; |
61 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
62 |
|
shouldThrow(); |
63 |
< |
} |
65 |
< |
catch (NullPointerException success) {} |
63 |
> |
} catch (NullPointerException success) {} |
64 |
|
} |
65 |
|
|
66 |
|
/** |
73 |
|
ints[i] = new Integer(i); |
74 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
75 |
|
shouldThrow(); |
76 |
< |
} |
79 |
< |
catch (NullPointerException success) {} |
76 |
> |
} catch (NullPointerException success) {} |
77 |
|
} |
78 |
|
|
79 |
|
/** |
80 |
|
* Queue contains all elements of collection used to initialize |
81 |
|
*/ |
82 |
|
public void testConstructor6() { |
83 |
< |
try { |
84 |
< |
Integer[] ints = new Integer[SIZE]; |
85 |
< |
for (int i = 0; i < SIZE; ++i) |
86 |
< |
ints[i] = new Integer(i); |
87 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
88 |
< |
for (int i = 0; i < SIZE; ++i) |
92 |
< |
assertEquals(ints[i], q.poll()); |
93 |
< |
} |
94 |
< |
finally {} |
83 |
> |
Integer[] ints = new Integer[SIZE]; |
84 |
> |
for (int i = 0; i < SIZE; ++i) |
85 |
> |
ints[i] = new Integer(i); |
86 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
87 |
> |
for (int i = 0; i < SIZE; ++i) |
88 |
> |
assertEquals(ints[i], q.poll()); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
121 |
|
* offer(null) throws NPE |
122 |
|
*/ |
123 |
|
public void testOfferNull() { |
124 |
< |
try { |
124 |
> |
try { |
125 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
126 |
|
q.offer(null); |
127 |
|
shouldThrow(); |
128 |
< |
} catch (NullPointerException success) { } |
128 |
> |
} catch (NullPointerException success) {} |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
|
* add(null) throws NPE |
133 |
|
*/ |
134 |
|
public void testAddNull() { |
135 |
< |
try { |
135 |
> |
try { |
136 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
137 |
|
q.add(null); |
138 |
|
shouldThrow(); |
139 |
< |
} catch (NullPointerException success) { } |
139 |
> |
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
148 |
– |
|
142 |
|
/** |
143 |
|
* Offer returns true |
144 |
|
*/ |
167 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
168 |
|
q.addAll(null); |
169 |
|
shouldThrow(); |
170 |
< |
} |
178 |
< |
catch (NullPointerException success) {} |
170 |
> |
} catch (NullPointerException success) {} |
171 |
|
} |
172 |
|
|
173 |
|
/** |
178 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
179 |
|
q.addAll(q); |
180 |
|
shouldThrow(); |
181 |
< |
} |
190 |
< |
catch (IllegalArgumentException success) {} |
181 |
> |
} catch (IllegalArgumentException success) {} |
182 |
|
} |
183 |
|
|
184 |
|
/** |
190 |
|
Integer[] ints = new Integer[SIZE]; |
191 |
|
q.addAll(Arrays.asList(ints)); |
192 |
|
shouldThrow(); |
193 |
< |
} |
203 |
< |
catch (NullPointerException success) {} |
193 |
> |
} catch (NullPointerException success) {} |
194 |
|
} |
195 |
+ |
|
196 |
|
/** |
197 |
< |
* addAll of a collection with any null elements throws NPE after |
197 |
> |
* addAll of a collection with any null elements throws NPE after |
198 |
|
* possibly adding some elements |
199 |
|
*/ |
200 |
|
public void testAddAll3() { |
205 |
|
ints[i] = new Integer(i); |
206 |
|
q.addAll(Arrays.asList(ints)); |
207 |
|
shouldThrow(); |
208 |
< |
} |
218 |
< |
catch (NullPointerException success) {} |
208 |
> |
} catch (NullPointerException success) {} |
209 |
|
} |
210 |
|
|
211 |
|
/** |
212 |
|
* Queue contains all elements, in traversal order, of successful addAll |
213 |
|
*/ |
214 |
|
public void testAddAll5() { |
215 |
< |
try { |
216 |
< |
Integer[] empty = new Integer[0]; |
217 |
< |
Integer[] ints = new Integer[SIZE]; |
218 |
< |
for (int i = 0; i < SIZE; ++i) |
219 |
< |
ints[i] = new Integer(i); |
220 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
221 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
222 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
223 |
< |
for (int i = 0; i < SIZE; ++i) |
234 |
< |
assertEquals(ints[i], q.poll()); |
235 |
< |
} |
236 |
< |
finally {} |
215 |
> |
Integer[] empty = new Integer[0]; |
216 |
> |
Integer[] ints = new Integer[SIZE]; |
217 |
> |
for (int i = 0; i < SIZE; ++i) |
218 |
> |
ints[i] = new Integer(i); |
219 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
220 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
221 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
222 |
> |
for (int i = 0; i < SIZE; ++i) |
223 |
> |
assertEquals(ints[i], q.poll()); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
229 |
|
public void testPoll() { |
230 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
231 |
|
for (int i = 0; i < SIZE; ++i) { |
232 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
232 |
> |
assertEquals(i, q.poll()); |
233 |
|
} |
234 |
< |
assertNull(q.poll()); |
234 |
> |
assertNull(q.poll()); |
235 |
|
} |
236 |
|
|
237 |
|
/** |
240 |
|
public void testPeek() { |
241 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
242 |
|
for (int i = 0; i < SIZE; ++i) { |
243 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
244 |
< |
q.poll(); |
243 |
> |
assertEquals(i, q.peek()); |
244 |
> |
assertEquals(i, q.poll()); |
245 |
|
assertTrue(q.peek() == null || |
246 |
< |
i != ((Integer)q.peek()).intValue()); |
246 |
> |
!q.peek().equals(i)); |
247 |
|
} |
248 |
< |
assertNull(q.peek()); |
248 |
> |
assertNull(q.peek()); |
249 |
|
} |
250 |
|
|
251 |
|
/** |
254 |
|
public void testElement() { |
255 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
256 |
|
for (int i = 0; i < SIZE; ++i) { |
257 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
258 |
< |
q.poll(); |
257 |
> |
assertEquals(i, q.element()); |
258 |
> |
assertEquals(i, q.poll()); |
259 |
|
} |
260 |
|
try { |
261 |
|
q.element(); |
262 |
|
shouldThrow(); |
263 |
< |
} |
277 |
< |
catch (NoSuchElementException success) {} |
263 |
> |
} catch (NoSuchElementException success) {} |
264 |
|
} |
265 |
|
|
266 |
|
/** |
267 |
< |
* remove removes next element, or throws NSEE if empty |
267 |
> |
* remove removes next element, or throws NSEE if empty |
268 |
|
*/ |
269 |
|
public void testRemove() { |
270 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
271 |
|
for (int i = 0; i < SIZE; ++i) { |
272 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
272 |
> |
assertEquals(i, q.remove()); |
273 |
|
} |
274 |
|
try { |
275 |
|
q.remove(); |
276 |
|
shouldThrow(); |
277 |
< |
} catch (NoSuchElementException success){ |
292 |
< |
} |
277 |
> |
} catch (NoSuchElementException success) {} |
278 |
|
} |
279 |
|
|
280 |
|
/** |
283 |
|
public void testRemoveElement() { |
284 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
285 |
|
for (int i = 1; i < SIZE; i+=2) { |
286 |
< |
assertTrue(q.remove(new Integer(i))); |
286 |
> |
assertTrue(q.contains(i)); |
287 |
> |
assertTrue(q.remove(i)); |
288 |
> |
assertFalse(q.contains(i)); |
289 |
> |
assertTrue(q.contains(i-1)); |
290 |
|
} |
291 |
|
for (int i = 0; i < SIZE; i+=2) { |
292 |
< |
assertTrue(q.remove(new Integer(i))); |
293 |
< |
assertFalse(q.remove(new Integer(i+1))); |
292 |
> |
assertTrue(q.contains(i)); |
293 |
> |
assertTrue(q.remove(i)); |
294 |
> |
assertFalse(q.contains(i)); |
295 |
> |
assertFalse(q.remove(i+1)); |
296 |
> |
assertFalse(q.contains(i+1)); |
297 |
|
} |
298 |
|
assertTrue(q.isEmpty()); |
299 |
|
} |
300 |
< |
|
300 |
> |
|
301 |
|
/** |
302 |
|
* contains(x) reports true when elements added but not yet removed |
303 |
|
*/ |
374 |
|
} |
375 |
|
|
376 |
|
/** |
377 |
< |
* toArray contains all elements |
377 |
> |
* toArray contains all elements in FIFO order |
378 |
|
*/ |
379 |
|
public void testToArray() { |
380 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
381 |
< |
Object[] o = q.toArray(); |
382 |
< |
Arrays.sort(o); |
383 |
< |
for(int i = 0; i < o.length; i++) |
393 |
< |
assertEquals(o[i], q.poll()); |
381 |
> |
Object[] o = q.toArray(); |
382 |
> |
for (int i = 0; i < o.length; i++) |
383 |
> |
assertSame(o[i], q.poll()); |
384 |
|
} |
385 |
|
|
386 |
|
/** |
387 |
< |
* toArray(a) contains all elements |
387 |
> |
* toArray(a) contains all elements in FIFO order |
388 |
|
*/ |
389 |
|
public void testToArray2() { |
390 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
391 |
< |
Integer[] ints = new Integer[SIZE]; |
392 |
< |
ints = (Integer[])q.toArray(ints); |
393 |
< |
Arrays.sort(ints); |
394 |
< |
for(int i = 0; i < ints.length; i++) |
395 |
< |
assertEquals(ints[i], q.poll()); |
390 |
> |
ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE); |
391 |
> |
Integer[] ints = new Integer[SIZE]; |
392 |
> |
Integer[] array = q.toArray(ints); |
393 |
> |
assertSame(ints, array); |
394 |
> |
for (int i = 0; i < ints.length; i++) |
395 |
> |
assertSame(ints[i], q.poll()); |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
< |
* toArray(null) throws NPE |
399 |
> |
* toArray(null) throws NullPointerException |
400 |
|
*/ |
401 |
< |
public void testToArray_BadArg() { |
402 |
< |
try { |
403 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
404 |
< |
Object o[] = q.toArray(null); |
405 |
< |
shouldThrow(); |
406 |
< |
} catch(NullPointerException success){} |
401 |
> |
public void testToArray_NullArg() { |
402 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
403 |
> |
try { |
404 |
> |
q.toArray(null); |
405 |
> |
shouldThrow(); |
406 |
> |
} catch (NullPointerException success) {} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* toArray with incompatible array type throws CCE |
410 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
411 |
|
*/ |
412 |
|
public void testToArray1_BadArg() { |
413 |
< |
try { |
414 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
415 |
< |
Object o[] = q.toArray(new String[10] ); |
416 |
< |
shouldThrow(); |
417 |
< |
} catch(ArrayStoreException success){} |
413 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
414 |
> |
try { |
415 |
> |
q.toArray(new String[10]); |
416 |
> |
shouldThrow(); |
417 |
> |
} catch (ArrayStoreException success) {} |
418 |
|
} |
419 |
< |
|
419 |
> |
|
420 |
|
/** |
421 |
< |
* iterator iterates through all elements |
421 |
> |
* iterator iterates through all elements |
422 |
|
*/ |
423 |
|
public void testIterator() { |
424 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
425 |
|
int i = 0; |
426 |
< |
Iterator it = q.iterator(); |
427 |
< |
while(it.hasNext()) { |
426 |
> |
Iterator it = q.iterator(); |
427 |
> |
while (it.hasNext()) { |
428 |
|
assertTrue(q.contains(it.next())); |
429 |
|
++i; |
430 |
|
} |
442 |
|
|
443 |
|
int k = 0; |
444 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
445 |
< |
int i = ((Integer)(it.next())).intValue(); |
456 |
< |
assertEquals(++k, i); |
445 |
> |
assertEquals(++k, it.next()); |
446 |
|
} |
447 |
|
|
448 |
|
assertEquals(3, k); |
451 |
|
/** |
452 |
|
* Modifications do not cause iterators to fail |
453 |
|
*/ |
454 |
< |
public void testWeaklyConsistentIteration () { |
454 |
> |
public void testWeaklyConsistentIteration() { |
455 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
456 |
|
q.add(one); |
457 |
|
q.add(two); |
458 |
|
q.add(three); |
459 |
|
|
460 |
< |
try { |
461 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
462 |
< |
q.remove(); |
474 |
< |
it.next(); |
475 |
< |
} |
476 |
< |
} |
477 |
< |
catch (ConcurrentModificationException e) { |
478 |
< |
shouldThrow(); |
460 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
461 |
> |
q.remove(); |
462 |
> |
it.next(); |
463 |
|
} |
464 |
|
|
465 |
|
assertEquals("queue should be empty again", 0, q.size()); |
468 |
|
/** |
469 |
|
* iterator.remove removes current element |
470 |
|
*/ |
471 |
< |
public void testIteratorRemove () { |
471 |
> |
public void testIteratorRemove() { |
472 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
473 |
|
q.add(one); |
474 |
|
q.add(two); |
477 |
|
it.next(); |
478 |
|
it.remove(); |
479 |
|
it = q.iterator(); |
480 |
< |
assertEquals(it.next(), two); |
481 |
< |
assertEquals(it.next(), three); |
480 |
> |
assertSame(it.next(), two); |
481 |
> |
assertSame(it.next(), three); |
482 |
|
assertFalse(it.hasNext()); |
483 |
|
} |
484 |
|
|
501 |
– |
|
485 |
|
/** |
486 |
|
* toString contains toStrings of elements |
487 |
|
*/ |
489 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
490 |
|
String s = q.toString(); |
491 |
|
for (int i = 0; i < SIZE; ++i) { |
492 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
492 |
> |
assertTrue(s.contains(String.valueOf(i))); |
493 |
|
} |
494 |
< |
} |
494 |
> |
} |
495 |
|
|
496 |
|
/** |
497 |
|
* A deserialized serialized queue has same elements in same order |
498 |
|
*/ |
499 |
< |
public void testSerialization() { |
499 |
> |
public void testSerialization() throws Exception { |
500 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
501 |
< |
try { |
502 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
503 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
504 |
< |
out.writeObject(q); |
505 |
< |
out.close(); |
506 |
< |
|
507 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
508 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
509 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
510 |
< |
assertEquals(q.size(), r.size()); |
511 |
< |
while (!q.isEmpty()) |
529 |
< |
assertEquals(q.remove(), r.remove()); |
530 |
< |
} catch(Exception e){ |
531 |
< |
unexpectedException(); |
532 |
< |
} |
501 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
502 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
503 |
> |
out.writeObject(q); |
504 |
> |
out.close(); |
505 |
> |
|
506 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
507 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
508 |
> |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
509 |
> |
assertEquals(q.size(), r.size()); |
510 |
> |
while (!q.isEmpty()) |
511 |
> |
assertEquals(q.remove(), r.remove()); |
512 |
|
} |
513 |
|
|
514 |
|
} |