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 { |
73 |
|
ints[i] = new Integer(i); |
74 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
75 |
|
shouldThrow(); |
76 |
< |
} |
77 |
< |
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) |
90 |
< |
assertEquals(ints[i], q.poll()); |
91 |
< |
} |
92 |
< |
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 |
|
/** |
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() { |
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) |
228 |
< |
assertEquals(ints[i], q.poll()); |
229 |
< |
} |
230 |
< |
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()); |
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(); |
284 |
|
public void testRemoveElement() { |
285 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
286 |
|
for (int i = 1; i < SIZE; i+=2) { |
287 |
< |
assertTrue(q.remove(new Integer(i))); |
287 |
> |
assertTrue(q.contains(i)); |
288 |
> |
assertTrue(q.remove(i)); |
289 |
> |
assertFalse(q.contains(i)); |
290 |
> |
assertTrue(q.contains(i-1)); |
291 |
|
} |
292 |
|
for (int i = 0; i < SIZE; i+=2) { |
293 |
< |
assertTrue(q.remove(new Integer(i))); |
294 |
< |
assertFalse(q.remove(new Integer(i+1))); |
293 |
> |
assertTrue(q.contains(i)); |
294 |
> |
assertTrue(q.remove(i)); |
295 |
> |
assertFalse(q.contains(i)); |
296 |
> |
assertFalse(q.remove(i+1)); |
297 |
> |
assertFalse(q.contains(i+1)); |
298 |
|
} |
299 |
|
assertTrue(q.isEmpty()); |
300 |
|
} |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* toArray contains all elements |
378 |
> |
* toArray contains all elements in FIFO order |
379 |
|
*/ |
380 |
|
public void testToArray() { |
381 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
382 |
|
Object[] o = q.toArray(); |
383 |
– |
Arrays.sort(o); |
383 |
|
for (int i = 0; i < o.length; i++) |
384 |
< |
assertEquals(o[i], q.poll()); |
384 |
> |
assertSame(o[i], q.poll()); |
385 |
|
} |
386 |
|
|
387 |
|
/** |
388 |
< |
* toArray(a) contains all elements |
388 |
> |
* toArray(a) contains all elements in FIFO order |
389 |
|
*/ |
390 |
|
public void testToArray2() { |
391 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
391 |
> |
ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE); |
392 |
|
Integer[] ints = new Integer[SIZE]; |
393 |
< |
ints = (Integer[])q.toArray(ints); |
394 |
< |
Arrays.sort(ints); |
393 |
> |
Integer[] array = q.toArray(ints); |
394 |
> |
assertSame(ints, array); |
395 |
|
for (int i = 0; i < ints.length; i++) |
396 |
< |
assertEquals(ints[i], q.poll()); |
396 |
> |
assertSame(ints[i], q.poll()); |
397 |
|
} |
398 |
|
|
399 |
|
/** |
400 |
< |
* toArray(null) throws NPE |
400 |
> |
* toArray(null) throws NullPointerException |
401 |
|
*/ |
402 |
< |
public void testToArray_BadArg() { |
402 |
> |
public void testToArray_NullArg() { |
403 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
404 |
|
try { |
405 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
406 |
< |
Object o[] = q.toArray(null); |
405 |
> |
q.toArray(null); |
406 |
|
shouldThrow(); |
407 |
|
} catch (NullPointerException success) {} |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* toArray with incompatible array type throws ArrayStoreException |
411 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
412 |
|
*/ |
413 |
|
public void testToArray1_BadArg() { |
414 |
+ |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
415 |
|
try { |
416 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
417 |
< |
Object o[] = q.toArray(new String[10] ); |
416 |
> |
q.toArray(new String[10]); |
417 |
|
shouldThrow(); |
418 |
|
} catch (ArrayStoreException success) {} |
419 |
|
} |
420 |
|
|
421 |
|
/** |
422 |
< |
* iterator iterates through all elements |
422 |
> |
* iterator iterates through all elements |
423 |
|
*/ |
424 |
|
public void testIterator() { |
425 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
443 |
|
|
444 |
|
int k = 0; |
445 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
446 |
< |
int i = ((Integer)(it.next())).intValue(); |
448 |
< |
assertEquals(++k, i); |
446 |
> |
assertEquals(++k, it.next()); |
447 |
|
} |
448 |
|
|
449 |
|
assertEquals(3, k); |
452 |
|
/** |
453 |
|
* Modifications do not cause iterators to fail |
454 |
|
*/ |
455 |
< |
public void testWeaklyConsistentIteration () { |
455 |
> |
public void testWeaklyConsistentIteration() { |
456 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
457 |
|
q.add(one); |
458 |
|
q.add(two); |
469 |
|
/** |
470 |
|
* iterator.remove removes current element |
471 |
|
*/ |
472 |
< |
public void testIteratorRemove () { |
472 |
> |
public void testIteratorRemove() { |
473 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
474 |
|
q.add(one); |
475 |
|
q.add(two); |
478 |
|
it.next(); |
479 |
|
it.remove(); |
480 |
|
it = q.iterator(); |
481 |
< |
assertEquals(it.next(), two); |
482 |
< |
assertEquals(it.next(), three); |
481 |
> |
assertSame(it.next(), two); |
482 |
> |
assertSame(it.next(), three); |
483 |
|
assertFalse(it.hasNext()); |
484 |
|
} |
485 |
|
|