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 |
|
*/ |
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 { |
139 |
|
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
– |
|
142 |
|
/** |
143 |
|
* Offer returns true |
144 |
|
*/ |
192 |
|
shouldThrow(); |
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() { |
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()); |
235 |
|
} |
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()); |
249 |
|
} |
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(); |
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(); |
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(); |
376 |
– |
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 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
399 |
< |
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 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
410 |
< |
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); |
442 |
|
|
443 |
|
int k = 0; |
444 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
445 |
< |
int i = ((Integer)(it.next())).intValue(); |
441 |
< |
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); |
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 |
|
|
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 |
|
|