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 |
|
|
21 |
|
public static void main(String[] args) { |
22 |
< |
junit.textui.TestRunner.run(suite()); |
22 |
> |
main(suite(), args); |
23 |
|
} |
24 |
|
|
25 |
|
public static Test suite() { |
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<Integer> populatedQueue(int n) { |
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 |
|
} |
62 |
|
*/ |
63 |
|
public void testConstructor4() { |
64 |
|
try { |
65 |
< |
Integer[] ints = new Integer[SIZE]; |
61 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
65 |
> |
new ConcurrentLinkedQueue(Arrays.asList(new Integer[SIZE])); |
66 |
|
shouldThrow(); |
67 |
|
} catch (NullPointerException success) {} |
68 |
|
} |
71 |
|
* Initializing from Collection with some null elements throws NPE |
72 |
|
*/ |
73 |
|
public void testConstructor5() { |
74 |
+ |
Integer[] ints = new Integer[SIZE]; |
75 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
76 |
+ |
ints[i] = new Integer(i); |
77 |
|
try { |
78 |
< |
Integer[] ints = new Integer[SIZE]; |
72 |
< |
for (int i = 0; i < SIZE-1; ++i) |
73 |
< |
ints[i] = new Integer(i); |
74 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
78 |
> |
new ConcurrentLinkedQueue(Arrays.asList(ints)); |
79 |
|
shouldThrow(); |
80 |
|
} catch (NullPointerException success) {} |
81 |
|
} |
112 |
|
public void testSize() { |
113 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
114 |
|
for (int i = 0; i < SIZE; ++i) { |
115 |
< |
assertEquals(SIZE-i, q.size()); |
115 |
> |
assertEquals(SIZE - i, q.size()); |
116 |
|
q.remove(); |
117 |
|
} |
118 |
|
for (int i = 0; i < SIZE; ++i) { |
125 |
|
* offer(null) throws NPE |
126 |
|
*/ |
127 |
|
public void testOfferNull() { |
128 |
+ |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
129 |
|
try { |
125 |
– |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
130 |
|
q.offer(null); |
131 |
|
shouldThrow(); |
132 |
|
} catch (NullPointerException success) {} |
136 |
|
* add(null) throws NPE |
137 |
|
*/ |
138 |
|
public void testAddNull() { |
139 |
+ |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
140 |
|
try { |
136 |
– |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
141 |
|
q.add(null); |
142 |
|
shouldThrow(); |
143 |
|
} catch (NullPointerException success) {} |
167 |
|
* addAll(null) throws NPE |
168 |
|
*/ |
169 |
|
public void testAddAll1() { |
170 |
+ |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
171 |
|
try { |
167 |
– |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
172 |
|
q.addAll(null); |
173 |
|
shouldThrow(); |
174 |
|
} catch (NullPointerException success) {} |
178 |
|
* addAll(this) throws IAE |
179 |
|
*/ |
180 |
|
public void testAddAllSelf() { |
181 |
+ |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
182 |
|
try { |
178 |
– |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
183 |
|
q.addAll(q); |
184 |
|
shouldThrow(); |
185 |
|
} catch (IllegalArgumentException success) {} |
189 |
|
* addAll of a collection with null elements throws NPE |
190 |
|
*/ |
191 |
|
public void testAddAll2() { |
192 |
+ |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
193 |
|
try { |
194 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
190 |
< |
Integer[] ints = new Integer[SIZE]; |
191 |
< |
q.addAll(Arrays.asList(ints)); |
194 |
> |
q.addAll(Arrays.asList(new Integer[SIZE])); |
195 |
|
shouldThrow(); |
196 |
|
} catch (NullPointerException success) {} |
197 |
|
} |
201 |
|
* possibly adding some elements |
202 |
|
*/ |
203 |
|
public void testAddAll3() { |
204 |
+ |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
205 |
+ |
Integer[] ints = new Integer[SIZE]; |
206 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
207 |
+ |
ints[i] = new Integer(i); |
208 |
|
try { |
202 |
– |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
203 |
– |
Integer[] ints = new Integer[SIZE]; |
204 |
– |
for (int i = 0; i < SIZE-1; ++i) |
205 |
– |
ints[i] = new Integer(i); |
209 |
|
q.addAll(Arrays.asList(ints)); |
210 |
|
shouldThrow(); |
211 |
|
} catch (NullPointerException success) {} |
285 |
|
*/ |
286 |
|
public void testRemoveElement() { |
287 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
288 |
< |
for (int i = 1; i < SIZE; i+=2) { |
288 |
> |
for (int i = 1; i < SIZE; i += 2) { |
289 |
|
assertTrue(q.contains(i)); |
290 |
|
assertTrue(q.remove(i)); |
291 |
|
assertFalse(q.contains(i)); |
292 |
< |
assertTrue(q.contains(i-1)); |
292 |
> |
assertTrue(q.contains(i - 1)); |
293 |
|
} |
294 |
< |
for (int i = 0; i < SIZE; i+=2) { |
294 |
> |
for (int i = 0; i < SIZE; i += 2) { |
295 |
|
assertTrue(q.contains(i)); |
296 |
|
assertTrue(q.remove(i)); |
297 |
|
assertFalse(q.contains(i)); |
298 |
< |
assertFalse(q.remove(i+1)); |
299 |
< |
assertFalse(q.contains(i+1)); |
298 |
> |
assertFalse(q.remove(i + 1)); |
299 |
> |
assertFalse(q.contains(i + 1)); |
300 |
|
} |
301 |
|
assertTrue(q.isEmpty()); |
302 |
|
} |
355 |
|
assertTrue(changed); |
356 |
|
|
357 |
|
assertTrue(q.containsAll(p)); |
358 |
< |
assertEquals(SIZE-i, q.size()); |
358 |
> |
assertEquals(SIZE - i, q.size()); |
359 |
|
p.remove(); |
360 |
|
} |
361 |
|
} |
368 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
369 |
|
ConcurrentLinkedQueue p = populatedQueue(i); |
370 |
|
assertTrue(q.removeAll(p)); |
371 |
< |
assertEquals(SIZE-i, q.size()); |
371 |
> |
assertEquals(SIZE - i, q.size()); |
372 |
|
for (int j = 0; j < i; ++j) { |
373 |
< |
Integer I = (Integer)(p.remove()); |
374 |
< |
assertFalse(q.contains(I)); |
373 |
> |
Integer x = (Integer)(p.remove()); |
374 |
> |
assertFalse(q.contains(x)); |
375 |
|
} |
376 |
|
} |
377 |
|
} |
425 |
|
*/ |
426 |
|
public void testIterator() { |
427 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
425 |
– |
int i = 0; |
428 |
|
Iterator it = q.iterator(); |
429 |
< |
while (it.hasNext()) { |
429 |
> |
int i; |
430 |
> |
for (i = 0; it.hasNext(); i++) |
431 |
|
assertTrue(q.contains(it.next())); |
429 |
– |
++i; |
430 |
– |
} |
432 |
|
assertEquals(i, SIZE); |
433 |
+ |
assertIteratorExhausted(it); |
434 |
+ |
} |
435 |
+ |
|
436 |
+ |
/** |
437 |
+ |
* iterator of empty collection has no elements |
438 |
+ |
*/ |
439 |
+ |
public void testEmptyIterator() { |
440 |
+ |
assertIteratorExhausted(new ConcurrentLinkedQueue().iterator()); |
441 |
|
} |
442 |
|
|
443 |
|
/** |
506 |
|
* A deserialized serialized queue has same elements in same order |
507 |
|
*/ |
508 |
|
public void testSerialization() throws Exception { |
509 |
< |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
510 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
511 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
512 |
< |
out.writeObject(q); |
513 |
< |
out.close(); |
514 |
< |
|
515 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
516 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
517 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
518 |
< |
assertEquals(q.size(), r.size()); |
519 |
< |
while (!q.isEmpty()) |
520 |
< |
assertEquals(q.remove(), r.remove()); |
509 |
> |
Queue x = populatedQueue(SIZE); |
510 |
> |
Queue y = serialClone(x); |
511 |
> |
|
512 |
> |
assertNotSame(x, y); |
513 |
> |
assertEquals(x.size(), y.size()); |
514 |
> |
assertEquals(x.toString(), y.toString()); |
515 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
516 |
> |
while (!x.isEmpty()) { |
517 |
> |
assertFalse(y.isEmpty()); |
518 |
> |
assertEquals(x.remove(), y.remove()); |
519 |
> |
} |
520 |
> |
assertTrue(y.isEmpty()); |
521 |
|
} |
522 |
|
|
523 |
+ |
/** |
524 |
+ |
* remove(null), contains(null) always return false |
525 |
+ |
*/ |
526 |
+ |
public void testNeverContainsNull() { |
527 |
+ |
Collection<?>[] qs = { |
528 |
+ |
new ConcurrentLinkedQueue<Object>(), |
529 |
+ |
populatedQueue(2), |
530 |
+ |
}; |
531 |
+ |
|
532 |
+ |
for (Collection<?> q : qs) { |
533 |
+ |
assertFalse(q.contains(null)); |
534 |
+ |
assertFalse(q.remove(null)); |
535 |
+ |
} |
536 |
+ |
} |
537 |
|
} |