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 |
|
*/ |
8 |
|
|
9 |
< |
import junit.framework.*; |
10 |
< |
import java.util.*; |
11 |
< |
import java.util.concurrent.*; |
12 |
< |
import java.io.*; |
9 |
> |
import java.util.Arrays; |
10 |
> |
import java.util.Collection; |
11 |
> |
import java.util.Iterator; |
12 |
> |
import java.util.NoSuchElementException; |
13 |
> |
import java.util.Queue; |
14 |
> |
import java.util.concurrent.ConcurrentLinkedQueue; |
15 |
> |
|
16 |
> |
import junit.framework.Test; |
17 |
> |
import junit.framework.TestSuite; |
18 |
|
|
19 |
|
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
20 |
|
|
27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
< |
* Create a queue of given size containing consecutive |
30 |
> |
* Returns a new queue of given size containing consecutive |
31 |
|
* Integers 0 ... n. |
32 |
|
*/ |
33 |
< |
private ConcurrentLinkedQueue populatedQueue(int n) { |
34 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
33 |
> |
private ConcurrentLinkedQueue<Integer> populatedQueue(int n) { |
34 |
> |
ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>(); |
35 |
|
assertTrue(q.isEmpty()); |
36 |
|
for (int i = 0; i < n; ++i) |
37 |
|
assertTrue(q.offer(new Integer(i))); |
52 |
|
*/ |
53 |
|
public void testConstructor3() { |
54 |
|
try { |
55 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); |
55 |
> |
new ConcurrentLinkedQueue((Collection)null); |
56 |
|
shouldThrow(); |
57 |
|
} catch (NullPointerException success) {} |
58 |
|
} |
63 |
|
public void testConstructor4() { |
64 |
|
try { |
65 |
|
Integer[] ints = new Integer[SIZE]; |
66 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
66 |
> |
new ConcurrentLinkedQueue(Arrays.asList(ints)); |
67 |
|
shouldThrow(); |
68 |
|
} catch (NullPointerException success) {} |
69 |
|
} |
76 |
|
Integer[] ints = new Integer[SIZE]; |
77 |
|
for (int i = 0; i < SIZE-1; ++i) |
78 |
|
ints[i] = new Integer(i); |
79 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
79 |
> |
new ConcurrentLinkedQueue(Arrays.asList(ints)); |
80 |
|
shouldThrow(); |
81 |
|
} catch (NullPointerException success) {} |
82 |
|
} |
144 |
|
} catch (NullPointerException success) {} |
145 |
|
} |
146 |
|
|
142 |
– |
|
147 |
|
/** |
148 |
|
* Offer returns true |
149 |
|
*/ |
287 |
|
*/ |
288 |
|
public void testRemoveElement() { |
289 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
290 |
< |
for (int i = 1; i < SIZE; i+=2) { |
291 |
< |
assertTrue(q.remove(new Integer(i))); |
292 |
< |
} |
293 |
< |
for (int i = 0; i < SIZE; i+=2) { |
294 |
< |
assertTrue(q.remove(new Integer(i))); |
295 |
< |
assertFalse(q.remove(new Integer(i+1))); |
290 |
> |
for (int i = 1; i < SIZE; i += 2) { |
291 |
> |
assertTrue(q.contains(i)); |
292 |
> |
assertTrue(q.remove(i)); |
293 |
> |
assertFalse(q.contains(i)); |
294 |
> |
assertTrue(q.contains(i-1)); |
295 |
> |
} |
296 |
> |
for (int i = 0; i < SIZE; i += 2) { |
297 |
> |
assertTrue(q.contains(i)); |
298 |
> |
assertTrue(q.remove(i)); |
299 |
> |
assertFalse(q.contains(i)); |
300 |
> |
assertFalse(q.remove(i+1)); |
301 |
> |
assertFalse(q.contains(i+1)); |
302 |
|
} |
303 |
|
assertTrue(q.isEmpty()); |
304 |
|
} |
372 |
|
assertTrue(q.removeAll(p)); |
373 |
|
assertEquals(SIZE-i, q.size()); |
374 |
|
for (int j = 0; j < i; ++j) { |
375 |
< |
Integer I = (Integer)(p.remove()); |
376 |
< |
assertFalse(q.contains(I)); |
375 |
> |
Integer x = (Integer)(p.remove()); |
376 |
> |
assertFalse(q.contains(x)); |
377 |
|
} |
378 |
|
} |
379 |
|
} |
380 |
|
|
381 |
|
/** |
382 |
< |
* toArray contains all elements |
382 |
> |
* toArray contains all elements in FIFO order |
383 |
|
*/ |
384 |
|
public void testToArray() { |
385 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
386 |
|
Object[] o = q.toArray(); |
377 |
– |
Arrays.sort(o); |
387 |
|
for (int i = 0; i < o.length; i++) |
388 |
< |
assertEquals(o[i], q.poll()); |
388 |
> |
assertSame(o[i], q.poll()); |
389 |
|
} |
390 |
|
|
391 |
|
/** |
392 |
< |
* toArray(a) contains all elements |
392 |
> |
* toArray(a) contains all elements in FIFO order |
393 |
|
*/ |
394 |
|
public void testToArray2() { |
395 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
395 |
> |
ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE); |
396 |
|
Integer[] ints = new Integer[SIZE]; |
397 |
< |
ints = (Integer[])q.toArray(ints); |
398 |
< |
Arrays.sort(ints); |
397 |
> |
Integer[] array = q.toArray(ints); |
398 |
> |
assertSame(ints, array); |
399 |
|
for (int i = 0; i < ints.length; i++) |
400 |
< |
assertEquals(ints[i], q.poll()); |
400 |
> |
assertSame(ints[i], q.poll()); |
401 |
|
} |
402 |
|
|
403 |
|
/** |
427 |
|
*/ |
428 |
|
public void testIterator() { |
429 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
421 |
– |
int i = 0; |
430 |
|
Iterator it = q.iterator(); |
431 |
< |
while (it.hasNext()) { |
431 |
> |
int i; |
432 |
> |
for (i = 0; it.hasNext(); i++) |
433 |
|
assertTrue(q.contains(it.next())); |
425 |
– |
++i; |
426 |
– |
} |
434 |
|
assertEquals(i, SIZE); |
435 |
+ |
assertIteratorExhausted(it); |
436 |
+ |
} |
437 |
+ |
|
438 |
+ |
/** |
439 |
+ |
* iterator of empty collection has no elements |
440 |
+ |
*/ |
441 |
+ |
public void testEmptyIterator() { |
442 |
+ |
assertIteratorExhausted(new ConcurrentLinkedQueue().iterator()); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
493 |
|
assertFalse(it.hasNext()); |
494 |
|
} |
495 |
|
|
481 |
– |
|
496 |
|
/** |
497 |
|
* toString contains toStrings of elements |
498 |
|
*/ |
500 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
501 |
|
String s = q.toString(); |
502 |
|
for (int i = 0; i < SIZE; ++i) { |
503 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
503 |
> |
assertTrue(s.contains(String.valueOf(i))); |
504 |
|
} |
505 |
|
} |
506 |
|
|
508 |
|
* A deserialized serialized queue has same elements in same order |
509 |
|
*/ |
510 |
|
public void testSerialization() throws Exception { |
511 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
512 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
513 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
514 |
< |
out.writeObject(q); |
515 |
< |
out.close(); |
516 |
< |
|
517 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
518 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
519 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
520 |
< |
assertEquals(q.size(), r.size()); |
521 |
< |
while (!q.isEmpty()) |
522 |
< |
assertEquals(q.remove(), r.remove()); |
511 |
> |
Queue x = populatedQueue(SIZE); |
512 |
> |
Queue y = serialClone(x); |
513 |
> |
|
514 |
> |
assertNotSame(x, y); |
515 |
> |
assertEquals(x.size(), y.size()); |
516 |
> |
assertEquals(x.toString(), y.toString()); |
517 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
518 |
> |
while (!x.isEmpty()) { |
519 |
> |
assertFalse(y.isEmpty()); |
520 |
> |
assertEquals(x.remove(), y.remove()); |
521 |
> |
} |
522 |
> |
assertTrue(y.isEmpty()); |
523 |
|
} |
524 |
|
|
525 |
+ |
/** |
526 |
+ |
* remove(null), contains(null) always return false |
527 |
+ |
*/ |
528 |
+ |
public void testNeverContainsNull() { |
529 |
+ |
Collection<?>[] qs = { |
530 |
+ |
new ConcurrentLinkedQueue<Object>(), |
531 |
+ |
populatedQueue(2), |
532 |
+ |
}; |
533 |
+ |
|
534 |
+ |
for (Collection<?> q : qs) { |
535 |
+ |
assertFalse(q.contains(null)); |
536 |
+ |
assertFalse(q.remove(null)); |
537 |
+ |
} |
538 |
+ |
} |
539 |
|
} |