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. |
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 |
|
/** |
28 |
|
private ConcurrentLinkedQueue populatedQueue(int n) { |
29 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
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 |
|
*/ |
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 |
|
/** |
81 |
|
* Queue contains all elements of collection used to initialize |
82 |
|
*/ |
83 |
|
public void testConstructor6() { |
84 |
< |
try { |
85 |
< |
Integer[] ints = new Integer[SIZE]; |
86 |
< |
for (int i = 0; i < SIZE; ++i) |
87 |
< |
ints[i] = new Integer(i); |
88 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
89 |
< |
for (int i = 0; i < SIZE; ++i) |
92 |
< |
assertEquals(ints[i], q.poll()); |
93 |
< |
} |
94 |
< |
finally {} |
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) |
89 |
> |
assertEquals(ints[i], q.poll()); |
90 |
|
} |
91 |
|
|
92 |
|
/** |
122 |
|
* offer(null) throws NPE |
123 |
|
*/ |
124 |
|
public void testOfferNull() { |
125 |
< |
try { |
125 |
> |
try { |
126 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
127 |
|
q.offer(null); |
128 |
|
shouldThrow(); |
129 |
< |
} catch (NullPointerException success) { } |
129 |
> |
} catch (NullPointerException success) {} |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
|
* add(null) throws NPE |
134 |
|
*/ |
135 |
|
public void testAddNull() { |
136 |
< |
try { |
136 |
> |
try { |
137 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
138 |
|
q.add(null); |
139 |
|
shouldThrow(); |
140 |
< |
} catch (NullPointerException success) { } |
140 |
> |
} catch (NullPointerException success) {} |
141 |
|
} |
142 |
|
|
143 |
|
|
169 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
170 |
|
q.addAll(null); |
171 |
|
shouldThrow(); |
172 |
< |
} |
178 |
< |
catch (NullPointerException success) {} |
172 |
> |
} catch (NullPointerException success) {} |
173 |
|
} |
174 |
|
|
175 |
|
/** |
180 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
181 |
|
q.addAll(q); |
182 |
|
shouldThrow(); |
183 |
< |
} |
190 |
< |
catch (IllegalArgumentException success) {} |
183 |
> |
} catch (IllegalArgumentException success) {} |
184 |
|
} |
185 |
|
|
186 |
|
/** |
192 |
|
Integer[] ints = new Integer[SIZE]; |
193 |
|
q.addAll(Arrays.asList(ints)); |
194 |
|
shouldThrow(); |
195 |
< |
} |
203 |
< |
catch (NullPointerException success) {} |
195 |
> |
} catch (NullPointerException success) {} |
196 |
|
} |
197 |
|
/** |
198 |
|
* addAll of a collection with any null elements throws NPE after |
206 |
|
ints[i] = new Integer(i); |
207 |
|
q.addAll(Arrays.asList(ints)); |
208 |
|
shouldThrow(); |
209 |
< |
} |
218 |
< |
catch (NullPointerException success) {} |
209 |
> |
} catch (NullPointerException success) {} |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
|
* Queue contains all elements, in traversal order, of successful addAll |
214 |
|
*/ |
215 |
|
public void testAddAll5() { |
216 |
< |
try { |
217 |
< |
Integer[] empty = new Integer[0]; |
218 |
< |
Integer[] ints = new Integer[SIZE]; |
219 |
< |
for (int i = 0; i < SIZE; ++i) |
220 |
< |
ints[i] = new Integer(i); |
221 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
222 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
223 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
224 |
< |
for (int i = 0; i < SIZE; ++i) |
234 |
< |
assertEquals(ints[i], q.poll()); |
235 |
< |
} |
236 |
< |
finally {} |
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) |
224 |
> |
assertEquals(ints[i], q.poll()); |
225 |
|
} |
226 |
|
|
227 |
|
/** |
232 |
|
for (int i = 0; i < SIZE; ++i) { |
233 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
234 |
|
} |
235 |
< |
assertNull(q.poll()); |
235 |
> |
assertNull(q.poll()); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
246 |
|
assertTrue(q.peek() == null || |
247 |
|
i != ((Integer)q.peek()).intValue()); |
248 |
|
} |
249 |
< |
assertNull(q.peek()); |
249 |
> |
assertNull(q.peek()); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
261 |
|
try { |
262 |
|
q.element(); |
263 |
|
shouldThrow(); |
264 |
< |
} |
277 |
< |
catch (NoSuchElementException success) {} |
264 |
> |
} catch (NoSuchElementException success) {} |
265 |
|
} |
266 |
|
|
267 |
|
/** |
275 |
|
try { |
276 |
|
q.remove(); |
277 |
|
shouldThrow(); |
278 |
< |
} catch (NoSuchElementException success){ |
292 |
< |
} |
278 |
> |
} catch (NoSuchElementException success) {} |
279 |
|
} |
280 |
|
|
281 |
|
/** |
292 |
|
} |
293 |
|
assertTrue(q.isEmpty()); |
294 |
|
} |
295 |
< |
|
295 |
> |
|
296 |
|
/** |
297 |
|
* contains(x) reports true when elements added but not yet removed |
298 |
|
*/ |
373 |
|
*/ |
374 |
|
public void testToArray() { |
375 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
376 |
< |
Object[] o = q.toArray(); |
376 |
> |
Object[] o = q.toArray(); |
377 |
|
Arrays.sort(o); |
378 |
< |
for(int i = 0; i < o.length; i++) |
379 |
< |
assertEquals(o[i], q.poll()); |
378 |
> |
for (int i = 0; i < o.length; i++) |
379 |
> |
assertEquals(o[i], q.poll()); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
384 |
|
*/ |
385 |
|
public void testToArray2() { |
386 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
387 |
< |
Integer[] ints = new Integer[SIZE]; |
388 |
< |
ints = (Integer[])q.toArray(ints); |
387 |
> |
Integer[] ints = new Integer[SIZE]; |
388 |
> |
ints = (Integer[])q.toArray(ints); |
389 |
|
Arrays.sort(ints); |
390 |
< |
for(int i = 0; i < ints.length; i++) |
390 |
> |
for (int i = 0; i < ints.length; i++) |
391 |
|
assertEquals(ints[i], q.poll()); |
392 |
|
} |
393 |
|
|
395 |
|
* toArray(null) throws NPE |
396 |
|
*/ |
397 |
|
public void testToArray_BadArg() { |
398 |
< |
try { |
398 |
> |
try { |
399 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
400 |
< |
Object o[] = q.toArray(null); |
401 |
< |
shouldThrow(); |
402 |
< |
} catch(NullPointerException success){} |
400 |
> |
Object o[] = q.toArray(null); |
401 |
> |
shouldThrow(); |
402 |
> |
} catch (NullPointerException success) {} |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
< |
* toArray with incompatable array type throws CCE |
406 |
> |
* toArray with incompatible array type throws ArrayStoreException |
407 |
|
*/ |
408 |
|
public void testToArray1_BadArg() { |
409 |
< |
try { |
409 |
> |
try { |
410 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
411 |
< |
Object o[] = q.toArray(new String[10] ); |
412 |
< |
shouldThrow(); |
413 |
< |
} catch(ArrayStoreException success){} |
411 |
> |
Object o[] = q.toArray(new String[10] ); |
412 |
> |
shouldThrow(); |
413 |
> |
} catch (ArrayStoreException success) {} |
414 |
|
} |
415 |
< |
|
415 |
> |
|
416 |
|
/** |
417 |
|
* iterator iterates through all elements |
418 |
|
*/ |
419 |
|
public void testIterator() { |
420 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
421 |
|
int i = 0; |
422 |
< |
Iterator it = q.iterator(); |
423 |
< |
while(it.hasNext()) { |
422 |
> |
Iterator it = q.iterator(); |
423 |
> |
while (it.hasNext()) { |
424 |
|
assertTrue(q.contains(it.next())); |
425 |
|
++i; |
426 |
|
} |
454 |
|
q.add(two); |
455 |
|
q.add(three); |
456 |
|
|
457 |
< |
try { |
458 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
459 |
< |
q.remove(); |
474 |
< |
it.next(); |
475 |
< |
} |
476 |
< |
} |
477 |
< |
catch (ConcurrentModificationException e) { |
478 |
< |
shouldThrow(); |
457 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
458 |
> |
q.remove(); |
459 |
> |
it.next(); |
460 |
|
} |
461 |
|
|
462 |
|
assertEquals("queue should be empty again", 0, q.size()); |
489 |
|
for (int i = 0; i < SIZE; ++i) { |
490 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
491 |
|
} |
492 |
< |
} |
492 |
> |
} |
493 |
|
|
494 |
|
/** |
495 |
|
* A deserialized serialized queue has same elements in same order |
496 |
|
*/ |
497 |
< |
public void testSerialization() { |
497 |
> |
public void testSerialization() throws Exception { |
498 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
499 |
< |
try { |
500 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
501 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
502 |
< |
out.writeObject(q); |
503 |
< |
out.close(); |
504 |
< |
|
505 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
506 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
507 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
508 |
< |
assertEquals(q.size(), r.size()); |
509 |
< |
while (!q.isEmpty()) |
529 |
< |
assertEquals(q.remove(), r.remove()); |
530 |
< |
} catch(Exception e){ |
531 |
< |
unexpectedException(); |
532 |
< |
} |
499 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
500 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
501 |
> |
out.writeObject(q); |
502 |
> |
out.close(); |
503 |
> |
|
504 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
505 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
506 |
> |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
507 |
> |
assertEquals(q.size(), r.size()); |
508 |
> |
while (!q.isEmpty()) |
509 |
> |
assertEquals(q.remove(), r.remove()); |
510 |
|
} |
511 |
|
|
512 |
|
} |