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 |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
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() { |
28 |
|
private ConcurrentLinkedQueue populatedQueue(int n) { |
29 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
30 |
|
assertTrue(q.isEmpty()); |
31 |
< |
for(int i = 0; i < n; ++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()); |
35 |
|
return q; |
36 |
|
} |
37 |
< |
|
37 |
> |
|
38 |
|
/** |
39 |
|
* new queue is empty |
40 |
|
*/ |
131 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
132 |
|
q.offer(null); |
133 |
|
shouldThrow(); |
134 |
< |
} catch (NullPointerException success) { } |
134 |
> |
} catch (NullPointerException success) { } |
135 |
|
} |
136 |
|
|
137 |
|
/** |
142 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
143 |
|
q.add(null); |
144 |
|
shouldThrow(); |
145 |
< |
} catch (NullPointerException success) { } |
145 |
> |
} catch (NullPointerException success) { } |
146 |
|
} |
147 |
|
|
148 |
|
|
288 |
|
try { |
289 |
|
q.remove(); |
290 |
|
shouldThrow(); |
291 |
< |
} catch (NoSuchElementException success){ |
292 |
< |
} |
291 |
> |
} catch (NoSuchElementException success) { |
292 |
> |
} |
293 |
|
} |
294 |
|
|
295 |
|
/** |
306 |
|
} |
307 |
|
assertTrue(q.isEmpty()); |
308 |
|
} |
309 |
< |
|
309 |
> |
|
310 |
|
/** |
311 |
|
* contains(x) reports true when elements added but not yet removed |
312 |
|
*/ |
389 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
390 |
|
Object[] o = q.toArray(); |
391 |
|
Arrays.sort(o); |
392 |
< |
for(int i = 0; i < o.length; i++) |
392 |
> |
for (int i = 0; i < o.length; i++) |
393 |
|
assertEquals(o[i], q.poll()); |
394 |
|
} |
395 |
|
|
401 |
|
Integer[] ints = new Integer[SIZE]; |
402 |
|
ints = (Integer[])q.toArray(ints); |
403 |
|
Arrays.sort(ints); |
404 |
< |
for(int i = 0; i < ints.length; i++) |
404 |
> |
for (int i = 0; i < ints.length; i++) |
405 |
|
assertEquals(ints[i], q.poll()); |
406 |
|
} |
407 |
|
|
413 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
414 |
|
Object o[] = q.toArray(null); |
415 |
|
shouldThrow(); |
416 |
< |
} catch(NullPointerException success){} |
416 |
> |
} catch (NullPointerException success) {} |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
< |
* toArray with incompatable array type throws CCE |
420 |
> |
* toArray with incompatible array type throws ArrayStoreException |
421 |
|
*/ |
422 |
|
public void testToArray1_BadArg() { |
423 |
|
try { |
424 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
425 |
|
Object o[] = q.toArray(new String[10] ); |
426 |
|
shouldThrow(); |
427 |
< |
} catch(ArrayStoreException success){} |
427 |
> |
} catch (ArrayStoreException success) {} |
428 |
|
} |
429 |
< |
|
429 |
> |
|
430 |
|
/** |
431 |
|
* iterator iterates through all elements |
432 |
|
*/ |
434 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
435 |
|
int i = 0; |
436 |
|
Iterator it = q.iterator(); |
437 |
< |
while(it.hasNext()) { |
437 |
> |
while (it.hasNext()) { |
438 |
|
assertTrue(q.contains(it.next())); |
439 |
|
++i; |
440 |
|
} |
508 |
|
for (int i = 0; i < SIZE; ++i) { |
509 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
510 |
|
} |
511 |
< |
} |
511 |
> |
} |
512 |
|
|
513 |
|
/** |
514 |
|
* A deserialized serialized queue has same elements in same order |
525 |
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
526 |
|
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
527 |
|
assertEquals(q.size(), r.size()); |
528 |
< |
while (!q.isEmpty()) |
528 |
> |
while (!q.isEmpty()) |
529 |
|
assertEquals(q.remove(), r.remove()); |
530 |
< |
} catch(Exception e){ |
530 |
> |
} catch (Exception e) { |
531 |
|
unexpectedException(); |
532 |
|
} |
533 |
|
} |