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