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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
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 { |
139 |
|
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
– |
|
142 |
|
/** |
143 |
|
* Offer returns true |
144 |
|
*/ |
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); |
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 |
|
} |
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(); |
377 |
– |
Arrays.sort(o); |
382 |
|
for (int i = 0; i < o.length; i++) |
383 |
< |
assertEquals(o[i], q.poll()); |
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); |
390 |
> |
ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE); |
391 |
|
Integer[] ints = new Integer[SIZE]; |
392 |
< |
ints = (Integer[])q.toArray(ints); |
393 |
< |
Arrays.sort(ints); |
392 |
> |
Integer[] array = q.toArray(ints); |
393 |
> |
assertSame(ints, array); |
394 |
|
for (int i = 0; i < ints.length; i++) |
395 |
< |
assertEquals(ints[i], q.poll()); |
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() { |
401 |
> |
public void testToArray_NullArg() { |
402 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
403 |
|
try { |
404 |
< |
Object o[] = q.toArray(null); |
404 |
> |
q.toArray(null); |
405 |
|
shouldThrow(); |
406 |
|
} catch (NullPointerException success) {} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* toArray with incompatible array type throws ArrayStoreException |
410 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
411 |
|
*/ |
412 |
|
public void testToArray1_BadArg() { |
413 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
414 |
|
try { |
415 |
< |
Object o[] = q.toArray(new String[10]); |
415 |
> |
q.toArray(new String[10]); |
416 |
|
shouldThrow(); |
417 |
|
} catch (ArrayStoreException success) {} |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* iterator iterates through all elements |
421 |
> |
* iterator iterates through all elements |
422 |
|
*/ |
423 |
|
public void testIterator() { |
424 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
482 |
|
assertFalse(it.hasNext()); |
483 |
|
} |
484 |
|
|
481 |
– |
|
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 |
|
} |
495 |
|
|