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 |
|
|
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 |
|
|
142 |
|
|
168 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
169 |
|
q.addAll(null); |
170 |
|
shouldThrow(); |
171 |
< |
} |
178 |
< |
catch (NullPointerException success) {} |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
|
|
174 |
|
/** |
179 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
180 |
|
q.addAll(q); |
181 |
|
shouldThrow(); |
182 |
< |
} |
190 |
< |
catch (IllegalArgumentException success) {} |
182 |
> |
} catch (IllegalArgumentException success) {} |
183 |
|
} |
184 |
|
|
185 |
|
/** |
191 |
|
Integer[] ints = new Integer[SIZE]; |
192 |
|
q.addAll(Arrays.asList(ints)); |
193 |
|
shouldThrow(); |
194 |
< |
} |
203 |
< |
catch (NullPointerException success) {} |
194 |
> |
} catch (NullPointerException success) {} |
195 |
|
} |
196 |
+ |
|
197 |
|
/** |
198 |
< |
* addAll of a collection with any null elements throws NPE after |
198 |
> |
* addAll of a collection with any null elements throws NPE after |
199 |
|
* possibly adding some elements |
200 |
|
*/ |
201 |
|
public void testAddAll3() { |
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 |
|
/** |
230 |
|
public void testPoll() { |
231 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
232 |
|
for (int i = 0; i < SIZE; ++i) { |
233 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
233 |
> |
assertEquals(i, q.poll()); |
234 |
|
} |
235 |
< |
assertNull(q.poll()); |
235 |
> |
assertNull(q.poll()); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
241 |
|
public void testPeek() { |
242 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
243 |
|
for (int i = 0; i < SIZE; ++i) { |
244 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
245 |
< |
q.poll(); |
244 |
> |
assertEquals(i, q.peek()); |
245 |
> |
assertEquals(i, q.poll()); |
246 |
|
assertTrue(q.peek() == null || |
247 |
< |
i != ((Integer)q.peek()).intValue()); |
247 |
> |
!q.peek().equals(i)); |
248 |
|
} |
249 |
< |
assertNull(q.peek()); |
249 |
> |
assertNull(q.peek()); |
250 |
|
} |
251 |
|
|
252 |
|
/** |
255 |
|
public void testElement() { |
256 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
257 |
|
for (int i = 0; i < SIZE; ++i) { |
258 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
259 |
< |
q.poll(); |
258 |
> |
assertEquals(i, q.element()); |
259 |
> |
assertEquals(i, q.poll()); |
260 |
|
} |
261 |
|
try { |
262 |
|
q.element(); |
263 |
|
shouldThrow(); |
264 |
< |
} |
277 |
< |
catch (NoSuchElementException success) {} |
264 |
> |
} catch (NoSuchElementException success) {} |
265 |
|
} |
266 |
|
|
267 |
|
/** |
268 |
< |
* remove removes next element, or throws NSEE if empty |
268 |
> |
* remove removes next element, or throws NSEE if empty |
269 |
|
*/ |
270 |
|
public void testRemove() { |
271 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
272 |
|
for (int i = 0; i < SIZE; ++i) { |
273 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
273 |
> |
assertEquals(i, q.remove()); |
274 |
|
} |
275 |
|
try { |
276 |
|
q.remove(); |
277 |
|
shouldThrow(); |
278 |
< |
} catch (NoSuchElementException success) { |
292 |
< |
} |
278 |
> |
} catch (NoSuchElementException success) {} |
279 |
|
} |
280 |
|
|
281 |
|
/** |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* toArray contains all elements |
372 |
> |
* toArray contains all elements in FIFO order |
373 |
|
*/ |
374 |
|
public void testToArray() { |
375 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
376 |
< |
Object[] o = q.toArray(); |
377 |
< |
Arrays.sort(o); |
378 |
< |
for (int i = 0; i < o.length; i++) |
393 |
< |
assertEquals(o[i], q.poll()); |
376 |
> |
Object[] o = q.toArray(); |
377 |
> |
for (int i = 0; i < o.length; i++) |
378 |
> |
assertSame(o[i], q.poll()); |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* toArray(a) contains all elements |
382 |
> |
* toArray(a) contains all elements in FIFO order |
383 |
|
*/ |
384 |
|
public void testToArray2() { |
385 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
386 |
< |
Integer[] ints = new Integer[SIZE]; |
387 |
< |
ints = (Integer[])q.toArray(ints); |
403 |
< |
Arrays.sort(ints); |
386 |
> |
Integer[] ints = new Integer[SIZE]; |
387 |
> |
assertSame(ints, q.toArray(ints)); |
388 |
|
for (int i = 0; i < ints.length; i++) |
389 |
< |
assertEquals(ints[i], q.poll()); |
389 |
> |
assertSame(ints[i], q.poll()); |
390 |
|
} |
391 |
|
|
392 |
|
/** |
393 |
< |
* toArray(null) throws NPE |
393 |
> |
* toArray(null) throws NullPointerException |
394 |
|
*/ |
395 |
< |
public void testToArray_BadArg() { |
396 |
< |
try { |
397 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
398 |
< |
Object o[] = q.toArray(null); |
399 |
< |
shouldThrow(); |
400 |
< |
} catch (NullPointerException success) {} |
395 |
> |
public void testToArray_NullArg() { |
396 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
397 |
> |
try { |
398 |
> |
q.toArray(null); |
399 |
> |
shouldThrow(); |
400 |
> |
} catch (NullPointerException success) {} |
401 |
|
} |
402 |
|
|
403 |
|
/** |
404 |
< |
* toArray with incompatible array type throws CCE |
404 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
405 |
|
*/ |
406 |
|
public void testToArray1_BadArg() { |
407 |
< |
try { |
408 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
409 |
< |
Object o[] = q.toArray(new String[10] ); |
410 |
< |
shouldThrow(); |
411 |
< |
} catch (ArrayStoreException success) {} |
407 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
408 |
> |
try { |
409 |
> |
q.toArray(new String[10]); |
410 |
> |
shouldThrow(); |
411 |
> |
} catch (ArrayStoreException success) {} |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
< |
* iterator iterates through all elements |
415 |
> |
* iterator iterates through all elements |
416 |
|
*/ |
417 |
|
public void testIterator() { |
418 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
419 |
|
int i = 0; |
420 |
< |
Iterator it = q.iterator(); |
420 |
> |
Iterator it = q.iterator(); |
421 |
|
while (it.hasNext()) { |
422 |
|
assertTrue(q.contains(it.next())); |
423 |
|
++i; |
436 |
|
|
437 |
|
int k = 0; |
438 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
439 |
< |
int i = ((Integer)(it.next())).intValue(); |
456 |
< |
assertEquals(++k, i); |
439 |
> |
assertEquals(++k, it.next()); |
440 |
|
} |
441 |
|
|
442 |
|
assertEquals(3, k); |
445 |
|
/** |
446 |
|
* Modifications do not cause iterators to fail |
447 |
|
*/ |
448 |
< |
public void testWeaklyConsistentIteration () { |
448 |
> |
public void testWeaklyConsistentIteration() { |
449 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
450 |
|
q.add(one); |
451 |
|
q.add(two); |
452 |
|
q.add(three); |
453 |
|
|
454 |
< |
try { |
455 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
456 |
< |
q.remove(); |
474 |
< |
it.next(); |
475 |
< |
} |
476 |
< |
} |
477 |
< |
catch (ConcurrentModificationException e) { |
478 |
< |
shouldThrow(); |
454 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
455 |
> |
q.remove(); |
456 |
> |
it.next(); |
457 |
|
} |
458 |
|
|
459 |
|
assertEquals("queue should be empty again", 0, q.size()); |
462 |
|
/** |
463 |
|
* iterator.remove removes current element |
464 |
|
*/ |
465 |
< |
public void testIteratorRemove () { |
465 |
> |
public void testIteratorRemove() { |
466 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
467 |
|
q.add(one); |
468 |
|
q.add(two); |
471 |
|
it.next(); |
472 |
|
it.remove(); |
473 |
|
it = q.iterator(); |
474 |
< |
assertEquals(it.next(), two); |
475 |
< |
assertEquals(it.next(), three); |
474 |
> |
assertSame(it.next(), two); |
475 |
> |
assertSame(it.next(), three); |
476 |
|
assertFalse(it.hasNext()); |
477 |
|
} |
478 |
|
|
491 |
|
/** |
492 |
|
* A deserialized serialized queue has same elements in same order |
493 |
|
*/ |
494 |
< |
public void testSerialization() { |
494 |
> |
public void testSerialization() throws Exception { |
495 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
496 |
< |
try { |
497 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
498 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
499 |
< |
out.writeObject(q); |
500 |
< |
out.close(); |
501 |
< |
|
502 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
503 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
504 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
505 |
< |
assertEquals(q.size(), r.size()); |
506 |
< |
while (!q.isEmpty()) |
529 |
< |
assertEquals(q.remove(), r.remove()); |
530 |
< |
} catch (Exception e) { |
531 |
< |
unexpectedException(); |
532 |
< |
} |
496 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
497 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
498 |
> |
out.writeObject(q); |
499 |
> |
out.close(); |
500 |
> |
|
501 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
502 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
503 |
> |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
504 |
> |
assertEquals(q.size(), r.size()); |
505 |
> |
while (!q.isEmpty()) |
506 |
> |
assertEquals(q.remove(), r.remove()); |
507 |
|
} |
508 |
|
|
509 |
|
} |