14 |
|
import java.util.concurrent.ConcurrentLinkedQueue; |
15 |
|
|
16 |
|
import junit.framework.Test; |
17 |
– |
import junit.framework.TestSuite; |
17 |
|
|
18 |
|
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
19 |
|
|
22 |
|
} |
23 |
|
|
24 |
|
public static Test suite() { |
25 |
< |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
25 |
> |
class Implementation implements CollectionImplementation { |
26 |
> |
public Class<?> klazz() { return ConcurrentLinkedQueue.class; } |
27 |
> |
public Collection emptyCollection() { return new ConcurrentLinkedQueue(); } |
28 |
> |
public Object makeElement(int i) { return i; } |
29 |
> |
public boolean isConcurrent() { return true; } |
30 |
> |
public boolean permitsNulls() { return false; } |
31 |
> |
} |
32 |
> |
return newTestSuite(ConcurrentLinkedQueueTest.class, |
33 |
> |
CollectionTest.testSuite(new Implementation())); |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
|
* Returns a new queue of given size containing consecutive |
38 |
< |
* Integers 0 ... n. |
38 |
> |
* Integers 0 ... n - 1. |
39 |
|
*/ |
40 |
< |
private ConcurrentLinkedQueue<Integer> populatedQueue(int n) { |
41 |
< |
ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>(); |
40 |
> |
private static ConcurrentLinkedQueue<Integer> populatedQueue(int n) { |
41 |
> |
ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>(); |
42 |
|
assertTrue(q.isEmpty()); |
43 |
|
for (int i = 0; i < n; ++i) |
44 |
|
assertTrue(q.offer(new Integer(i))); |
45 |
|
assertFalse(q.isEmpty()); |
46 |
|
assertEquals(n, q.size()); |
47 |
+ |
assertEquals((Integer) 0, q.peek()); |
48 |
|
return q; |
49 |
|
} |
50 |
|
|
80 |
|
*/ |
81 |
|
public void testConstructor5() { |
82 |
|
Integer[] ints = new Integer[SIZE]; |
83 |
< |
for (int i = 0; i < SIZE-1; ++i) |
83 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
84 |
|
ints[i] = new Integer(i); |
85 |
|
try { |
86 |
|
new ConcurrentLinkedQueue(Arrays.asList(ints)); |
120 |
|
public void testSize() { |
121 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
122 |
|
for (int i = 0; i < SIZE; ++i) { |
123 |
< |
assertEquals(SIZE-i, q.size()); |
123 |
> |
assertEquals(SIZE - i, q.size()); |
124 |
|
q.remove(); |
125 |
|
} |
126 |
|
for (int i = 0; i < SIZE; ++i) { |
172 |
|
} |
173 |
|
|
174 |
|
/** |
175 |
< |
* addAll(null) throws NPE |
175 |
> |
* addAll(null) throws NullPointerException |
176 |
|
*/ |
177 |
|
public void testAddAll1() { |
178 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
183 |
|
} |
184 |
|
|
185 |
|
/** |
186 |
< |
* addAll(this) throws IAE |
186 |
> |
* addAll(this) throws IllegalArgumentException |
187 |
|
*/ |
188 |
|
public void testAddAllSelf() { |
189 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
194 |
|
} |
195 |
|
|
196 |
|
/** |
197 |
< |
* addAll of a collection with null elements throws NPE |
197 |
> |
* addAll of a collection with null elements throws NullPointerException |
198 |
|
*/ |
199 |
|
public void testAddAll2() { |
200 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
211 |
|
public void testAddAll3() { |
212 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
213 |
|
Integer[] ints = new Integer[SIZE]; |
214 |
< |
for (int i = 0; i < SIZE-1; ++i) |
214 |
> |
for (int i = 0; i < SIZE - 1; ++i) |
215 |
|
ints[i] = new Integer(i); |
216 |
|
try { |
217 |
|
q.addAll(Arrays.asList(ints)); |
297 |
|
assertTrue(q.contains(i)); |
298 |
|
assertTrue(q.remove(i)); |
299 |
|
assertFalse(q.contains(i)); |
300 |
< |
assertTrue(q.contains(i-1)); |
300 |
> |
assertTrue(q.contains(i - 1)); |
301 |
|
} |
302 |
|
for (int i = 0; i < SIZE; i += 2) { |
303 |
|
assertTrue(q.contains(i)); |
304 |
|
assertTrue(q.remove(i)); |
305 |
|
assertFalse(q.contains(i)); |
306 |
< |
assertFalse(q.remove(i+1)); |
307 |
< |
assertFalse(q.contains(i+1)); |
306 |
> |
assertFalse(q.remove(i + 1)); |
307 |
> |
assertFalse(q.contains(i + 1)); |
308 |
|
} |
309 |
|
assertTrue(q.isEmpty()); |
310 |
|
} |
363 |
|
assertTrue(changed); |
364 |
|
|
365 |
|
assertTrue(q.containsAll(p)); |
366 |
< |
assertEquals(SIZE-i, q.size()); |
366 |
> |
assertEquals(SIZE - i, q.size()); |
367 |
|
p.remove(); |
368 |
|
} |
369 |
|
} |
376 |
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
377 |
|
ConcurrentLinkedQueue p = populatedQueue(i); |
378 |
|
assertTrue(q.removeAll(p)); |
379 |
< |
assertEquals(SIZE-i, q.size()); |
379 |
> |
assertEquals(SIZE - i, q.size()); |
380 |
|
for (int j = 0; j < i; ++j) { |
381 |
|
Integer x = (Integer)(p.remove()); |
382 |
|
assertFalse(q.contains(x)); |
511 |
|
} |
512 |
|
|
513 |
|
/** |
514 |
< |
* A deserialized serialized queue has same elements in same order |
514 |
> |
* A deserialized/reserialized queue has same elements in same order |
515 |
|
*/ |
516 |
|
public void testSerialization() throws Exception { |
517 |
|
Queue x = populatedQueue(SIZE); |