1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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/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.*; |
13 |
< |
|
14 |
< |
public class ConcurrentLinkedQueueTest extends TestCase { |
15 |
< |
private static final int N = 10; |
16 |
< |
private static final long SHORT_DELAY_MS = 100; |
17 |
< |
private static final long MEDIUM_DELAY_MS = 1000; |
17 |
< |
private static final long LONG_DELAY_MS = 10000; |
10 |
> |
import java.util.Arrays; |
11 |
> |
import java.util.Collection; |
12 |
> |
import java.util.Iterator; |
13 |
> |
import java.util.NoSuchElementException; |
14 |
> |
import java.util.Queue; |
15 |
> |
import java.util.concurrent.ConcurrentLinkedQueue; |
16 |
> |
|
17 |
> |
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
18 |
|
|
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run (suite()); |
20 |
> |
junit.textui.TestRunner.run(suite()); |
21 |
|
} |
22 |
|
|
23 |
|
public static Test suite() { |
24 |
< |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
24 |
> |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
25 |
|
} |
26 |
|
|
27 |
|
/** |
28 |
< |
* Create a queue of given size containing consecutive |
28 |
> |
* Returns a new queue of given size containing consecutive |
29 |
|
* Integers 0 ... n. |
30 |
|
*/ |
31 |
< |
private ConcurrentLinkedQueue fullQueue(int n) { |
32 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
31 |
> |
private ConcurrentLinkedQueue<Integer> populatedQueue(int n) { |
32 |
> |
ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>(); |
33 |
|
assertTrue(q.isEmpty()); |
34 |
< |
for(int i = 0; i < n; ++i) |
35 |
< |
assertTrue(q.offer(new Integer(i))); |
34 |
> |
for (int i = 0; i < n; ++i) |
35 |
> |
assertTrue(q.offer(new Integer(i))); |
36 |
|
assertFalse(q.isEmpty()); |
37 |
< |
assertEquals(n, q.size()); |
37 |
> |
assertEquals(n, q.size()); |
38 |
|
return q; |
39 |
|
} |
40 |
< |
|
41 |
< |
public void testConstructor1(){ |
40 |
> |
|
41 |
> |
/** |
42 |
> |
* new queue is empty |
43 |
> |
*/ |
44 |
> |
public void testConstructor1() { |
45 |
|
assertEquals(0, new ConcurrentLinkedQueue().size()); |
46 |
|
} |
47 |
|
|
48 |
+ |
/** |
49 |
+ |
* Initializing from null Collection throws NPE |
50 |
+ |
*/ |
51 |
|
public void testConstructor3() { |
52 |
|
try { |
53 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); |
54 |
< |
fail("Cannot make from null collection"); |
55 |
< |
} |
50 |
< |
catch (NullPointerException success) {} |
54 |
> |
shouldThrow(); |
55 |
> |
} catch (NullPointerException success) {} |
56 |
|
} |
57 |
|
|
58 |
< |
public void testConstructor4(){ |
58 |
> |
/** |
59 |
> |
* Initializing from Collection of null elements throws NPE |
60 |
> |
*/ |
61 |
> |
public void testConstructor4() { |
62 |
|
try { |
63 |
< |
Integer[] ints = new Integer[N]; |
63 |
> |
Integer[] ints = new Integer[SIZE]; |
64 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
65 |
< |
fail("Cannot make with null elements"); |
66 |
< |
} |
59 |
< |
catch (NullPointerException success) {} |
65 |
> |
shouldThrow(); |
66 |
> |
} catch (NullPointerException success) {} |
67 |
|
} |
68 |
|
|
69 |
< |
public void testConstructor5(){ |
69 |
> |
/** |
70 |
> |
* Initializing from Collection with some null elements throws NPE |
71 |
> |
*/ |
72 |
> |
public void testConstructor5() { |
73 |
|
try { |
74 |
< |
Integer[] ints = new Integer[N]; |
75 |
< |
for (int i = 0; i < N-1; ++i) |
74 |
> |
Integer[] ints = new Integer[SIZE]; |
75 |
> |
for (int i = 0; i < SIZE-1; ++i) |
76 |
|
ints[i] = new Integer(i); |
77 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
78 |
< |
fail("Cannot make with null elements"); |
79 |
< |
} |
70 |
< |
catch (NullPointerException success) {} |
78 |
> |
shouldThrow(); |
79 |
> |
} catch (NullPointerException success) {} |
80 |
|
} |
81 |
|
|
82 |
< |
public void testConstructor6(){ |
83 |
< |
try { |
84 |
< |
Integer[] ints = new Integer[N]; |
85 |
< |
for (int i = 0; i < N; ++i) |
86 |
< |
ints[i] = new Integer(i); |
87 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
88 |
< |
for (int i = 0; i < N; ++i) |
89 |
< |
assertEquals(ints[i], q.poll()); |
90 |
< |
} |
91 |
< |
finally {} |
82 |
> |
/** |
83 |
> |
* Queue contains all elements of collection used to initialize |
84 |
> |
*/ |
85 |
> |
public void testConstructor6() { |
86 |
> |
Integer[] ints = new Integer[SIZE]; |
87 |
> |
for (int i = 0; i < SIZE; ++i) |
88 |
> |
ints[i] = new Integer(i); |
89 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
90 |
> |
for (int i = 0; i < SIZE; ++i) |
91 |
> |
assertEquals(ints[i], q.poll()); |
92 |
|
} |
93 |
|
|
94 |
+ |
/** |
95 |
+ |
* isEmpty is true before add, false after |
96 |
+ |
*/ |
97 |
|
public void testEmpty() { |
98 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
99 |
|
assertTrue(q.isEmpty()); |
100 |
< |
q.add(new Integer(1)); |
100 |
> |
q.add(one); |
101 |
|
assertFalse(q.isEmpty()); |
102 |
< |
q.add(new Integer(2)); |
102 |
> |
q.add(two); |
103 |
|
q.remove(); |
104 |
|
q.remove(); |
105 |
|
assertTrue(q.isEmpty()); |
106 |
|
} |
107 |
|
|
108 |
+ |
/** |
109 |
+ |
* size changes when elements added and removed |
110 |
+ |
*/ |
111 |
|
public void testSize() { |
112 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
113 |
< |
for (int i = 0; i < N; ++i) { |
114 |
< |
assertEquals(N-i, q.size()); |
112 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
113 |
> |
for (int i = 0; i < SIZE; ++i) { |
114 |
> |
assertEquals(SIZE-i, q.size()); |
115 |
|
q.remove(); |
116 |
|
} |
117 |
< |
for (int i = 0; i < N; ++i) { |
117 |
> |
for (int i = 0; i < SIZE; ++i) { |
118 |
|
assertEquals(i, q.size()); |
119 |
|
q.add(new Integer(i)); |
120 |
|
} |
121 |
|
} |
122 |
|
|
123 |
< |
public void testOfferNull(){ |
124 |
< |
try { |
123 |
> |
/** |
124 |
> |
* offer(null) throws NPE |
125 |
> |
*/ |
126 |
> |
public void testOfferNull() { |
127 |
> |
try { |
128 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
129 |
|
q.offer(null); |
130 |
< |
fail("should throw NPE"); |
131 |
< |
} catch (NullPointerException success) { } |
130 |
> |
shouldThrow(); |
131 |
> |
} catch (NullPointerException success) {} |
132 |
> |
} |
133 |
> |
|
134 |
> |
/** |
135 |
> |
* add(null) throws NPE |
136 |
> |
*/ |
137 |
> |
public void testAddNull() { |
138 |
> |
try { |
139 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
140 |
> |
q.add(null); |
141 |
> |
shouldThrow(); |
142 |
> |
} catch (NullPointerException success) {} |
143 |
|
} |
144 |
|
|
145 |
+ |
/** |
146 |
+ |
* Offer returns true |
147 |
+ |
*/ |
148 |
|
public void testOffer() { |
149 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
150 |
< |
assertTrue(q.offer(new Integer(0))); |
151 |
< |
assertTrue(q.offer(new Integer(1))); |
150 |
> |
assertTrue(q.offer(zero)); |
151 |
> |
assertTrue(q.offer(one)); |
152 |
|
} |
153 |
|
|
154 |
< |
public void testAdd(){ |
154 |
> |
/** |
155 |
> |
* add returns true |
156 |
> |
*/ |
157 |
> |
public void testAdd() { |
158 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
159 |
< |
for (int i = 0; i < N; ++i) { |
159 |
> |
for (int i = 0; i < SIZE; ++i) { |
160 |
|
assertEquals(i, q.size()); |
161 |
|
assertTrue(q.add(new Integer(i))); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
< |
public void testAddAll1(){ |
165 |
> |
/** |
166 |
> |
* addAll(null) throws NPE |
167 |
> |
*/ |
168 |
> |
public void testAddAll1() { |
169 |
|
try { |
170 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
171 |
|
q.addAll(null); |
172 |
< |
fail("Cannot add null collection"); |
173 |
< |
} |
136 |
< |
catch (NullPointerException success) {} |
172 |
> |
shouldThrow(); |
173 |
> |
} catch (NullPointerException success) {} |
174 |
|
} |
175 |
< |
public void testAddAll2(){ |
175 |
> |
|
176 |
> |
/** |
177 |
> |
* addAll(this) throws IAE |
178 |
> |
*/ |
179 |
> |
public void testAddAllSelf() { |
180 |
> |
try { |
181 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
182 |
> |
q.addAll(q); |
183 |
> |
shouldThrow(); |
184 |
> |
} catch (IllegalArgumentException success) {} |
185 |
> |
} |
186 |
> |
|
187 |
> |
/** |
188 |
> |
* addAll of a collection with null elements throws NPE |
189 |
> |
*/ |
190 |
> |
public void testAddAll2() { |
191 |
|
try { |
192 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
193 |
< |
Integer[] ints = new Integer[N]; |
193 |
> |
Integer[] ints = new Integer[SIZE]; |
194 |
|
q.addAll(Arrays.asList(ints)); |
195 |
< |
fail("Cannot add null elements"); |
196 |
< |
} |
145 |
< |
catch (NullPointerException success) {} |
195 |
> |
shouldThrow(); |
196 |
> |
} catch (NullPointerException success) {} |
197 |
|
} |
198 |
< |
public void testAddAll3(){ |
198 |
> |
|
199 |
> |
/** |
200 |
> |
* addAll of a collection with any null elements throws NPE after |
201 |
> |
* possibly adding some elements |
202 |
> |
*/ |
203 |
> |
public void testAddAll3() { |
204 |
|
try { |
205 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
206 |
< |
Integer[] ints = new Integer[N]; |
207 |
< |
for (int i = 0; i < N-1; ++i) |
206 |
> |
Integer[] ints = new Integer[SIZE]; |
207 |
> |
for (int i = 0; i < SIZE-1; ++i) |
208 |
|
ints[i] = new Integer(i); |
209 |
|
q.addAll(Arrays.asList(ints)); |
210 |
< |
fail("Cannot add null elements"); |
211 |
< |
} |
156 |
< |
catch (NullPointerException success) {} |
210 |
> |
shouldThrow(); |
211 |
> |
} catch (NullPointerException success) {} |
212 |
|
} |
213 |
|
|
214 |
< |
public void testAddAll5(){ |
215 |
< |
try { |
216 |
< |
Integer[] empty = new Integer[0]; |
217 |
< |
Integer[] ints = new Integer[N]; |
218 |
< |
for (int i = 0; i < N; ++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 < N; ++i) |
224 |
< |
assertEquals(ints[i], q.poll()); |
225 |
< |
} |
226 |
< |
finally {} |
214 |
> |
/** |
215 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
216 |
> |
*/ |
217 |
> |
public void testAddAll5() { |
218 |
> |
Integer[] empty = new Integer[0]; |
219 |
> |
Integer[] ints = new Integer[SIZE]; |
220 |
> |
for (int i = 0; i < SIZE; ++i) |
221 |
> |
ints[i] = new Integer(i); |
222 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
223 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
224 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
225 |
> |
for (int i = 0; i < SIZE; ++i) |
226 |
> |
assertEquals(ints[i], q.poll()); |
227 |
|
} |
228 |
|
|
229 |
< |
public void testPoll(){ |
230 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
231 |
< |
for (int i = 0; i < N; ++i) { |
232 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
229 |
> |
/** |
230 |
> |
* poll succeeds unless empty |
231 |
> |
*/ |
232 |
> |
public void testPoll() { |
233 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
234 |
> |
for (int i = 0; i < SIZE; ++i) { |
235 |
> |
assertEquals(i, q.poll()); |
236 |
|
} |
237 |
< |
assertNull(q.poll()); |
237 |
> |
assertNull(q.poll()); |
238 |
|
} |
239 |
|
|
240 |
< |
public void testPeek(){ |
241 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
242 |
< |
for (int i = 0; i < N; ++i) { |
243 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
244 |
< |
q.poll(); |
240 |
> |
/** |
241 |
> |
* peek returns next element, or null if empty |
242 |
> |
*/ |
243 |
> |
public void testPeek() { |
244 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
245 |
> |
for (int i = 0; i < SIZE; ++i) { |
246 |
> |
assertEquals(i, q.peek()); |
247 |
> |
assertEquals(i, q.poll()); |
248 |
|
assertTrue(q.peek() == null || |
249 |
< |
i != ((Integer)q.peek()).intValue()); |
249 |
> |
!q.peek().equals(i)); |
250 |
|
} |
251 |
< |
assertNull(q.peek()); |
251 |
> |
assertNull(q.peek()); |
252 |
|
} |
253 |
|
|
254 |
< |
public void testElement(){ |
255 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
256 |
< |
for (int i = 0; i < N; ++i) { |
257 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
258 |
< |
q.poll(); |
254 |
> |
/** |
255 |
> |
* element returns next element, or throws NSEE if empty |
256 |
> |
*/ |
257 |
> |
public void testElement() { |
258 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
259 |
> |
for (int i = 0; i < SIZE; ++i) { |
260 |
> |
assertEquals(i, q.element()); |
261 |
> |
assertEquals(i, q.poll()); |
262 |
|
} |
263 |
|
try { |
264 |
|
q.element(); |
265 |
< |
fail("no such element"); |
266 |
< |
} |
203 |
< |
catch (NoSuchElementException success) {} |
265 |
> |
shouldThrow(); |
266 |
> |
} catch (NoSuchElementException success) {} |
267 |
|
} |
268 |
|
|
269 |
< |
public void testRemove(){ |
270 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
271 |
< |
for (int i = 0; i < N; ++i) { |
272 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
269 |
> |
/** |
270 |
> |
* remove removes next element, or throws NSEE if empty |
271 |
> |
*/ |
272 |
> |
public void testRemove() { |
273 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
274 |
> |
for (int i = 0; i < SIZE; ++i) { |
275 |
> |
assertEquals(i, q.remove()); |
276 |
|
} |
277 |
|
try { |
278 |
|
q.remove(); |
279 |
< |
fail("remove should throw"); |
280 |
< |
} catch (NoSuchElementException success){ |
215 |
< |
} |
279 |
> |
shouldThrow(); |
280 |
> |
} catch (NoSuchElementException success) {} |
281 |
|
} |
282 |
|
|
283 |
< |
public void testRemoveElement(){ |
284 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
285 |
< |
for (int i = 1; i < N; i+=2) { |
286 |
< |
assertTrue(q.remove(new Integer(i))); |
287 |
< |
} |
288 |
< |
for (int i = 0; i < N; i+=2) { |
289 |
< |
assertTrue(q.remove(new Integer(i))); |
290 |
< |
assertFalse(q.remove(new Integer(i+1))); |
283 |
> |
/** |
284 |
> |
* remove(x) removes x and returns true if present |
285 |
> |
*/ |
286 |
> |
public void testRemoveElement() { |
287 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
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)); |
293 |
> |
} |
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)); |
300 |
|
} |
301 |
|
assertTrue(q.isEmpty()); |
302 |
|
} |
303 |
< |
|
304 |
< |
public void testContains(){ |
305 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
306 |
< |
for (int i = 0; i < N; ++i) { |
303 |
> |
|
304 |
> |
/** |
305 |
> |
* contains(x) reports true when elements added but not yet removed |
306 |
> |
*/ |
307 |
> |
public void testContains() { |
308 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
309 |
> |
for (int i = 0; i < SIZE; ++i) { |
310 |
|
assertTrue(q.contains(new Integer(i))); |
311 |
|
q.poll(); |
312 |
|
assertFalse(q.contains(new Integer(i))); |
313 |
|
} |
314 |
|
} |
315 |
|
|
316 |
< |
public void testClear(){ |
317 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
316 |
> |
/** |
317 |
> |
* clear removes all elements |
318 |
> |
*/ |
319 |
> |
public void testClear() { |
320 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
321 |
|
q.clear(); |
322 |
|
assertTrue(q.isEmpty()); |
323 |
|
assertEquals(0, q.size()); |
324 |
< |
q.add(new Integer(1)); |
324 |
> |
q.add(one); |
325 |
|
assertFalse(q.isEmpty()); |
326 |
|
q.clear(); |
327 |
|
assertTrue(q.isEmpty()); |
328 |
|
} |
329 |
|
|
330 |
< |
public void testContainsAll(){ |
331 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
330 |
> |
/** |
331 |
> |
* containsAll(c) is true when c contains a subset of elements |
332 |
> |
*/ |
333 |
> |
public void testContainsAll() { |
334 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
335 |
|
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue(); |
336 |
< |
for (int i = 0; i < N; ++i) { |
336 |
> |
for (int i = 0; i < SIZE; ++i) { |
337 |
|
assertTrue(q.containsAll(p)); |
338 |
|
assertFalse(p.containsAll(q)); |
339 |
|
p.add(new Integer(i)); |
341 |
|
assertTrue(p.containsAll(q)); |
342 |
|
} |
343 |
|
|
344 |
< |
public void testRetainAll(){ |
345 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
346 |
< |
ConcurrentLinkedQueue p = fullQueue(N); |
347 |
< |
for (int i = 0; i < N; ++i) { |
344 |
> |
/** |
345 |
> |
* retainAll(c) retains only those elements of c and reports true if change |
346 |
> |
*/ |
347 |
> |
public void testRetainAll() { |
348 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
349 |
> |
ConcurrentLinkedQueue p = populatedQueue(SIZE); |
350 |
> |
for (int i = 0; i < SIZE; ++i) { |
351 |
|
boolean changed = q.retainAll(p); |
352 |
|
if (i == 0) |
353 |
|
assertFalse(changed); |
355 |
|
assertTrue(changed); |
356 |
|
|
357 |
|
assertTrue(q.containsAll(p)); |
358 |
< |
assertEquals(N-i, q.size()); |
358 |
> |
assertEquals(SIZE-i, q.size()); |
359 |
|
p.remove(); |
360 |
|
} |
361 |
|
} |
362 |
|
|
363 |
< |
public void testRemoveAll(){ |
364 |
< |
for (int i = 1; i < N; ++i) { |
365 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
366 |
< |
ConcurrentLinkedQueue p = fullQueue(i); |
363 |
> |
/** |
364 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
365 |
> |
*/ |
366 |
> |
public void testRemoveAll() { |
367 |
> |
for (int i = 1; i < SIZE; ++i) { |
368 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
369 |
> |
ConcurrentLinkedQueue p = populatedQueue(i); |
370 |
|
assertTrue(q.removeAll(p)); |
371 |
< |
assertEquals(N-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)); |
376 |
|
} |
377 |
|
} |
378 |
|
|
379 |
< |
public void testToArray(){ |
380 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
381 |
< |
Object[] o = q.toArray(); |
382 |
< |
Arrays.sort(o); |
383 |
< |
for(int i = 0; i < o.length; i++) |
384 |
< |
assertEquals(o[i], q.poll()); |
379 |
> |
/** |
380 |
> |
* toArray contains all elements in FIFO order |
381 |
> |
*/ |
382 |
> |
public void testToArray() { |
383 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
384 |
> |
Object[] o = q.toArray(); |
385 |
> |
for (int i = 0; i < o.length; i++) |
386 |
> |
assertSame(o[i], q.poll()); |
387 |
|
} |
388 |
|
|
389 |
< |
public void testToArray2(){ |
390 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
391 |
< |
Integer[] ints = new Integer[N]; |
392 |
< |
ints = (Integer[])q.toArray(ints); |
393 |
< |
Arrays.sort(ints); |
394 |
< |
for(int i = 0; i < ints.length; i++) |
395 |
< |
assertEquals(ints[i], q.poll()); |
389 |
> |
/** |
390 |
> |
* toArray(a) contains all elements in FIFO order |
391 |
> |
*/ |
392 |
> |
public void testToArray2() { |
393 |
> |
ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE); |
394 |
> |
Integer[] ints = new Integer[SIZE]; |
395 |
> |
Integer[] array = q.toArray(ints); |
396 |
> |
assertSame(ints, array); |
397 |
> |
for (int i = 0; i < ints.length; i++) |
398 |
> |
assertSame(ints[i], q.poll()); |
399 |
|
} |
400 |
< |
|
401 |
< |
public void testIterator(){ |
402 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
400 |
> |
|
401 |
> |
/** |
402 |
> |
* toArray(null) throws NullPointerException |
403 |
> |
*/ |
404 |
> |
public void testToArray_NullArg() { |
405 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
406 |
> |
try { |
407 |
> |
q.toArray(null); |
408 |
> |
shouldThrow(); |
409 |
> |
} catch (NullPointerException success) {} |
410 |
> |
} |
411 |
> |
|
412 |
> |
/** |
413 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
414 |
> |
*/ |
415 |
> |
public void testToArray1_BadArg() { |
416 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
417 |
> |
try { |
418 |
> |
q.toArray(new String[10]); |
419 |
> |
shouldThrow(); |
420 |
> |
} catch (ArrayStoreException success) {} |
421 |
> |
} |
422 |
> |
|
423 |
> |
/** |
424 |
> |
* iterator iterates through all elements |
425 |
> |
*/ |
426 |
> |
public void testIterator() { |
427 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
428 |
|
int i = 0; |
429 |
< |
Iterator it = q.iterator(); |
430 |
< |
while(it.hasNext()) { |
429 |
> |
Iterator it = q.iterator(); |
430 |
> |
while (it.hasNext()) { |
431 |
|
assertTrue(q.contains(it.next())); |
432 |
|
++i; |
433 |
|
} |
434 |
< |
assertEquals(i, N); |
434 |
> |
assertEquals(i, SIZE); |
435 |
|
} |
436 |
|
|
437 |
+ |
/** |
438 |
+ |
* iterator ordering is FIFO |
439 |
+ |
*/ |
440 |
|
public void testIteratorOrdering() { |
319 |
– |
|
441 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
442 |
< |
|
443 |
< |
q.add(new Integer(1)); |
444 |
< |
q.add(new Integer(2)); |
324 |
< |
q.add(new Integer(3)); |
442 |
> |
q.add(one); |
443 |
> |
q.add(two); |
444 |
> |
q.add(three); |
445 |
|
|
446 |
|
int k = 0; |
447 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
448 |
< |
int i = ((Integer)(it.next())).intValue(); |
329 |
< |
assertEquals("items should come out in order", ++k, i); |
448 |
> |
assertEquals(++k, it.next()); |
449 |
|
} |
450 |
|
|
451 |
< |
assertEquals("should go through 3 elements", 3, k); |
451 |
> |
assertEquals(3, k); |
452 |
|
} |
453 |
|
|
454 |
< |
public void testWeaklyConsistentIteration () { |
455 |
< |
|
454 |
> |
/** |
455 |
> |
* Modifications do not cause iterators to fail |
456 |
> |
*/ |
457 |
> |
public void testWeaklyConsistentIteration() { |
458 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
459 |
+ |
q.add(one); |
460 |
+ |
q.add(two); |
461 |
+ |
q.add(three); |
462 |
|
|
463 |
< |
q.add(new Integer(1)); |
464 |
< |
q.add(new Integer(2)); |
465 |
< |
q.add(new Integer(3)); |
342 |
< |
|
343 |
< |
try { |
344 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
345 |
< |
q.remove(); |
346 |
< |
it.next(); |
347 |
< |
} |
348 |
< |
} |
349 |
< |
catch (ConcurrentModificationException e) { |
350 |
< |
fail("weakly consistent iterator; should not get CME"); |
463 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
464 |
> |
q.remove(); |
465 |
> |
it.next(); |
466 |
|
} |
467 |
|
|
468 |
|
assertEquals("queue should be empty again", 0, q.size()); |
469 |
|
} |
470 |
|
|
471 |
< |
public void testIteratorRemove () { |
472 |
< |
|
471 |
> |
/** |
472 |
> |
* iterator.remove removes current element |
473 |
> |
*/ |
474 |
> |
public void testIteratorRemove() { |
475 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
476 |
< |
|
477 |
< |
q.add(new Integer(1)); |
478 |
< |
q.add(new Integer(2)); |
362 |
< |
q.add(new Integer(3)); |
363 |
< |
|
476 |
> |
q.add(one); |
477 |
> |
q.add(two); |
478 |
> |
q.add(three); |
479 |
|
Iterator it = q.iterator(); |
480 |
|
it.next(); |
481 |
|
it.remove(); |
367 |
– |
|
482 |
|
it = q.iterator(); |
483 |
< |
assertEquals(it.next(), new Integer(2)); |
484 |
< |
assertEquals(it.next(), new Integer(3)); |
483 |
> |
assertSame(it.next(), two); |
484 |
> |
assertSame(it.next(), three); |
485 |
|
assertFalse(it.hasNext()); |
486 |
|
} |
487 |
|
|
488 |
< |
|
489 |
< |
public void testToString(){ |
490 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
488 |
> |
/** |
489 |
> |
* toString contains toStrings of elements |
490 |
> |
*/ |
491 |
> |
public void testToString() { |
492 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
493 |
|
String s = q.toString(); |
494 |
< |
for (int i = 0; i < N; ++i) { |
495 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
494 |
> |
for (int i = 0; i < SIZE; ++i) { |
495 |
> |
assertTrue(s.contains(String.valueOf(i))); |
496 |
|
} |
497 |
< |
} |
497 |
> |
} |
498 |
|
|
499 |
< |
public void testSerialization() { |
500 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
501 |
< |
try { |
502 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
503 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
504 |
< |
out.writeObject(q); |
505 |
< |
out.close(); |
506 |
< |
|
507 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
508 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
509 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
510 |
< |
assertEquals(q.size(), r.size()); |
511 |
< |
while (!q.isEmpty()) |
512 |
< |
assertEquals(q.remove(), r.remove()); |
397 |
< |
} catch(Exception e){ |
398 |
< |
e.printStackTrace(); |
399 |
< |
fail("unexpected exception"); |
499 |
> |
/** |
500 |
> |
* A deserialized serialized queue has same elements in same order |
501 |
> |
*/ |
502 |
> |
public void testSerialization() throws Exception { |
503 |
> |
Queue x = populatedQueue(SIZE); |
504 |
> |
Queue y = serialClone(x); |
505 |
> |
|
506 |
> |
assertNotSame(x, y); |
507 |
> |
assertEquals(x.size(), y.size()); |
508 |
> |
assertEquals(x.toString(), y.toString()); |
509 |
> |
assertTrue(Arrays.equals(x.toArray(), y.toArray())); |
510 |
> |
while (!x.isEmpty()) { |
511 |
> |
assertFalse(y.isEmpty()); |
512 |
> |
assertEquals(x.remove(), y.remove()); |
513 |
|
} |
514 |
+ |
assertTrue(y.isEmpty()); |
515 |
|
} |
516 |
|
|
517 |
|
} |