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() { |
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))); |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* Initializing from null Collection throws NPE |
46 |
> |
* Initializing from null Collection throws NPE |
47 |
|
*/ |
48 |
|
public void testConstructor3() { |
49 |
|
try { |
193 |
|
shouldThrow(); |
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() { |
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()); |
236 |
|
} |
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()); |
250 |
|
} |
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(); |
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(); |
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(); |
376 |
– |
Arrays.sort(o); |
377 |
|
for (int i = 0; i < o.length; i++) |
378 |
< |
assertEquals(o[i], q.poll()); |
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); |
385 |
> |
ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE); |
386 |
|
Integer[] ints = new Integer[SIZE]; |
387 |
< |
ints = (Integer[])q.toArray(ints); |
388 |
< |
Arrays.sort(ints); |
387 |
> |
Integer[] array = q.toArray(ints); |
388 |
> |
assertSame(ints, array); |
389 |
|
for (int i = 0; i < ints.length; i++) |
390 |
< |
assertEquals(ints[i], q.poll()); |
390 |
> |
assertSame(ints[i], q.poll()); |
391 |
|
} |
392 |
|
|
393 |
|
/** |
394 |
< |
* toArray(null) throws NPE |
394 |
> |
* toArray(null) throws NullPointerException |
395 |
|
*/ |
396 |
< |
public void testToArray_BadArg() { |
396 |
> |
public void testToArray_NullArg() { |
397 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
398 |
|
try { |
399 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
399 |
< |
Object o[] = q.toArray(null); |
399 |
> |
q.toArray(null); |
400 |
|
shouldThrow(); |
401 |
|
} catch (NullPointerException success) {} |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
< |
* toArray with incompatible array type throws ArrayStoreException |
405 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
406 |
|
*/ |
407 |
|
public void testToArray1_BadArg() { |
408 |
+ |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
409 |
|
try { |
410 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
410 |
< |
Object o[] = q.toArray(new String[10] ); |
410 |
> |
q.toArray(new String[10]); |
411 |
|
shouldThrow(); |
412 |
|
} catch (ArrayStoreException success) {} |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
< |
* iterator iterates through all elements |
416 |
> |
* iterator iterates through all elements |
417 |
|
*/ |
418 |
|
public void testIterator() { |
419 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
437 |
|
|
438 |
|
int k = 0; |
439 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
440 |
< |
int i = ((Integer)(it.next())).intValue(); |
441 |
< |
assertEquals(++k, i); |
440 |
> |
assertEquals(++k, it.next()); |
441 |
|
} |
442 |
|
|
443 |
|
assertEquals(3, k); |
446 |
|
/** |
447 |
|
* Modifications do not cause iterators to fail |
448 |
|
*/ |
449 |
< |
public void testWeaklyConsistentIteration () { |
449 |
> |
public void testWeaklyConsistentIteration() { |
450 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
451 |
|
q.add(one); |
452 |
|
q.add(two); |
463 |
|
/** |
464 |
|
* iterator.remove removes current element |
465 |
|
*/ |
466 |
< |
public void testIteratorRemove () { |
466 |
> |
public void testIteratorRemove() { |
467 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
468 |
|
q.add(one); |
469 |
|
q.add(two); |
472 |
|
it.next(); |
473 |
|
it.remove(); |
474 |
|
it = q.iterator(); |
475 |
< |
assertEquals(it.next(), two); |
476 |
< |
assertEquals(it.next(), three); |
475 |
> |
assertSame(it.next(), two); |
476 |
> |
assertSame(it.next(), three); |
477 |
|
assertFalse(it.hasNext()); |
478 |
|
} |
479 |
|
|