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 |
|
|
* http://creativecommons.org/licenses/publicdomain |
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.12 |
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 |
dl |
1.3 |
private ConcurrentLinkedQueue populatedQueue(int n) { |
29 |
dl |
1.1 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
30 |
|
|
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 |
dl |
1.5 |
* 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 |
dl |
1.1 |
} |
77 |
|
|
catch (NullPointerException success) {} |
78 |
|
|
} |
79 |
|
|
|
80 |
dl |
1.4 |
/** |
81 |
dl |
1.5 |
* Queue contains all elements of collection used to initialize |
82 |
dl |
1.4 |
*/ |
83 |
|
|
public void testConstructor6() { |
84 |
dl |
1.1 |
try { |
85 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
86 |
|
|
for (int i = 0; i < SIZE; ++i) |
87 |
dl |
1.1 |
ints[i] = new Integer(i); |
88 |
|
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
89 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) |
90 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
91 |
|
|
} |
92 |
|
|
finally {} |
93 |
|
|
} |
94 |
|
|
|
95 |
dl |
1.4 |
/** |
96 |
dl |
1.5 |
* isEmpty is true before add, false after |
97 |
dl |
1.4 |
*/ |
98 |
dl |
1.1 |
public void testEmpty() { |
99 |
|
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
100 |
|
|
assertTrue(q.isEmpty()); |
101 |
dl |
1.3 |
q.add(one); |
102 |
dl |
1.1 |
assertFalse(q.isEmpty()); |
103 |
dl |
1.3 |
q.add(two); |
104 |
dl |
1.1 |
q.remove(); |
105 |
|
|
q.remove(); |
106 |
|
|
assertTrue(q.isEmpty()); |
107 |
|
|
} |
108 |
|
|
|
109 |
dl |
1.4 |
/** |
110 |
dl |
1.5 |
* size changes when elements added and removed |
111 |
dl |
1.4 |
*/ |
112 |
dl |
1.1 |
public void testSize() { |
113 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
114 |
|
|
for (int i = 0; i < SIZE; ++i) { |
115 |
|
|
assertEquals(SIZE-i, q.size()); |
116 |
dl |
1.1 |
q.remove(); |
117 |
|
|
} |
118 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
119 |
dl |
1.1 |
assertEquals(i, q.size()); |
120 |
|
|
q.add(new Integer(i)); |
121 |
|
|
} |
122 |
|
|
} |
123 |
|
|
|
124 |
dl |
1.4 |
/** |
125 |
dl |
1.5 |
* offer(null) throws NPE |
126 |
dl |
1.4 |
*/ |
127 |
|
|
public void testOfferNull() { |
128 |
jsr166 |
1.12 |
try { |
129 |
dl |
1.1 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
130 |
|
|
q.offer(null); |
131 |
dl |
1.4 |
shouldThrow(); |
132 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
133 |
dl |
1.1 |
} |
134 |
|
|
|
135 |
dl |
1.4 |
/** |
136 |
dl |
1.5 |
* add(null) throws NPE |
137 |
|
|
*/ |
138 |
|
|
public void testAddNull() { |
139 |
jsr166 |
1.12 |
try { |
140 |
dl |
1.5 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
141 |
|
|
q.add(null); |
142 |
|
|
shouldThrow(); |
143 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
144 |
dl |
1.5 |
} |
145 |
|
|
|
146 |
|
|
|
147 |
|
|
/** |
148 |
|
|
* Offer returns true |
149 |
dl |
1.4 |
*/ |
150 |
dl |
1.1 |
public void testOffer() { |
151 |
|
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
152 |
dl |
1.3 |
assertTrue(q.offer(zero)); |
153 |
|
|
assertTrue(q.offer(one)); |
154 |
dl |
1.1 |
} |
155 |
|
|
|
156 |
dl |
1.4 |
/** |
157 |
dl |
1.5 |
* add returns true |
158 |
dl |
1.4 |
*/ |
159 |
|
|
public void testAdd() { |
160 |
dl |
1.1 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
161 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
162 |
dl |
1.1 |
assertEquals(i, q.size()); |
163 |
|
|
assertTrue(q.add(new Integer(i))); |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
|
167 |
dl |
1.4 |
/** |
168 |
dl |
1.5 |
* addAll(null) throws NPE |
169 |
dl |
1.4 |
*/ |
170 |
|
|
public void testAddAll1() { |
171 |
dl |
1.1 |
try { |
172 |
|
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
173 |
|
|
q.addAll(null); |
174 |
dl |
1.4 |
shouldThrow(); |
175 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
176 |
dl |
1.1 |
} |
177 |
dl |
1.5 |
|
178 |
|
|
/** |
179 |
|
|
* addAll(this) throws IAE |
180 |
|
|
*/ |
181 |
|
|
public void testAddAllSelf() { |
182 |
|
|
try { |
183 |
|
|
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
184 |
|
|
q.addAll(q); |
185 |
|
|
shouldThrow(); |
186 |
jsr166 |
1.13 |
} catch (IllegalArgumentException success) {} |
187 |
dl |
1.5 |
} |
188 |
|
|
|
189 |
dl |
1.4 |
/** |
190 |
dl |
1.5 |
* addAll of a collection with null elements throws NPE |
191 |
dl |
1.4 |
*/ |
192 |
|
|
public void testAddAll2() { |
193 |
dl |
1.1 |
try { |
194 |
|
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
195 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
196 |
dl |
1.1 |
q.addAll(Arrays.asList(ints)); |
197 |
dl |
1.4 |
shouldThrow(); |
198 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
199 |
dl |
1.1 |
} |
200 |
dl |
1.4 |
/** |
201 |
dl |
1.5 |
* addAll of a collection with any null elements throws NPE after |
202 |
|
|
* possibly adding some elements |
203 |
dl |
1.4 |
*/ |
204 |
|
|
public void testAddAll3() { |
205 |
dl |
1.1 |
try { |
206 |
|
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
207 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
208 |
|
|
for (int i = 0; i < SIZE-1; ++i) |
209 |
dl |
1.1 |
ints[i] = new Integer(i); |
210 |
|
|
q.addAll(Arrays.asList(ints)); |
211 |
dl |
1.4 |
shouldThrow(); |
212 |
jsr166 |
1.13 |
} catch (NullPointerException success) {} |
213 |
dl |
1.1 |
} |
214 |
|
|
|
215 |
dl |
1.4 |
/** |
216 |
dl |
1.5 |
* Queue contains all elements, in traversal order, of successful addAll |
217 |
dl |
1.4 |
*/ |
218 |
|
|
public void testAddAll5() { |
219 |
dl |
1.1 |
try { |
220 |
|
|
Integer[] empty = new Integer[0]; |
221 |
dl |
1.3 |
Integer[] ints = new Integer[SIZE]; |
222 |
|
|
for (int i = 0; i < SIZE; ++i) |
223 |
dl |
1.1 |
ints[i] = new Integer(i); |
224 |
|
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
225 |
|
|
assertFalse(q.addAll(Arrays.asList(empty))); |
226 |
|
|
assertTrue(q.addAll(Arrays.asList(ints))); |
227 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) |
228 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
229 |
|
|
} |
230 |
|
|
finally {} |
231 |
|
|
} |
232 |
|
|
|
233 |
dl |
1.4 |
/** |
234 |
dl |
1.5 |
* poll succeeds unless empty |
235 |
dl |
1.4 |
*/ |
236 |
|
|
public void testPoll() { |
237 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
238 |
|
|
for (int i = 0; i < SIZE; ++i) { |
239 |
dl |
1.1 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
240 |
|
|
} |
241 |
jsr166 |
1.12 |
assertNull(q.poll()); |
242 |
dl |
1.1 |
} |
243 |
|
|
|
244 |
dl |
1.4 |
/** |
245 |
dl |
1.5 |
* peek returns next element, or null if empty |
246 |
dl |
1.4 |
*/ |
247 |
|
|
public void testPeek() { |
248 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
249 |
|
|
for (int i = 0; i < SIZE; ++i) { |
250 |
dl |
1.1 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
251 |
|
|
q.poll(); |
252 |
|
|
assertTrue(q.peek() == null || |
253 |
|
|
i != ((Integer)q.peek()).intValue()); |
254 |
|
|
} |
255 |
jsr166 |
1.12 |
assertNull(q.peek()); |
256 |
dl |
1.1 |
} |
257 |
|
|
|
258 |
dl |
1.4 |
/** |
259 |
dl |
1.5 |
* element returns next element, or throws NSEE if empty |
260 |
dl |
1.4 |
*/ |
261 |
|
|
public void testElement() { |
262 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
263 |
|
|
for (int i = 0; i < SIZE; ++i) { |
264 |
dl |
1.1 |
assertEquals(i, ((Integer)q.element()).intValue()); |
265 |
|
|
q.poll(); |
266 |
|
|
} |
267 |
|
|
try { |
268 |
|
|
q.element(); |
269 |
dl |
1.4 |
shouldThrow(); |
270 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
271 |
dl |
1.1 |
} |
272 |
|
|
|
273 |
dl |
1.4 |
/** |
274 |
dl |
1.5 |
* remove removes next element, or throws NSEE if empty |
275 |
dl |
1.4 |
*/ |
276 |
|
|
public void testRemove() { |
277 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
278 |
|
|
for (int i = 0; i < SIZE; ++i) { |
279 |
dl |
1.1 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
280 |
|
|
} |
281 |
|
|
try { |
282 |
|
|
q.remove(); |
283 |
dl |
1.4 |
shouldThrow(); |
284 |
jsr166 |
1.13 |
} catch (NoSuchElementException success) {} |
285 |
dl |
1.1 |
} |
286 |
|
|
|
287 |
dl |
1.4 |
/** |
288 |
dl |
1.5 |
* remove(x) removes x and returns true if present |
289 |
dl |
1.4 |
*/ |
290 |
|
|
public void testRemoveElement() { |
291 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
292 |
|
|
for (int i = 1; i < SIZE; i+=2) { |
293 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
294 |
|
|
} |
295 |
dl |
1.3 |
for (int i = 0; i < SIZE; i+=2) { |
296 |
dl |
1.1 |
assertTrue(q.remove(new Integer(i))); |
297 |
|
|
assertFalse(q.remove(new Integer(i+1))); |
298 |
|
|
} |
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 |
dl |
1.5 |
* toArray contains all elements |
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 |
dl |
1.1 |
Arrays.sort(o); |
384 |
jsr166 |
1.12 |
for (int i = 0; i < o.length; i++) |
385 |
|
|
assertEquals(o[i], q.poll()); |
386 |
dl |
1.1 |
} |
387 |
|
|
|
388 |
dl |
1.4 |
/** |
389 |
dl |
1.5 |
* toArray(a) contains all elements |
390 |
dl |
1.4 |
*/ |
391 |
|
|
public void testToArray2() { |
392 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
393 |
jsr166 |
1.12 |
Integer[] ints = new Integer[SIZE]; |
394 |
|
|
ints = (Integer[])q.toArray(ints); |
395 |
dl |
1.1 |
Arrays.sort(ints); |
396 |
jsr166 |
1.9 |
for (int i = 0; i < ints.length; i++) |
397 |
dl |
1.1 |
assertEquals(ints[i], q.poll()); |
398 |
|
|
} |
399 |
dl |
1.5 |
|
400 |
|
|
/** |
401 |
|
|
* toArray(null) throws NPE |
402 |
|
|
*/ |
403 |
|
|
public void testToArray_BadArg() { |
404 |
jsr166 |
1.12 |
try { |
405 |
dl |
1.5 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
406 |
jsr166 |
1.12 |
Object o[] = q.toArray(null); |
407 |
|
|
shouldThrow(); |
408 |
|
|
} catch (NullPointerException success) {} |
409 |
dl |
1.5 |
} |
410 |
|
|
|
411 |
|
|
/** |
412 |
jsr166 |
1.11 |
* toArray with incompatible array type throws ArrayStoreException |
413 |
dl |
1.5 |
*/ |
414 |
|
|
public void testToArray1_BadArg() { |
415 |
jsr166 |
1.12 |
try { |
416 |
dl |
1.5 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
417 |
jsr166 |
1.12 |
Object o[] = q.toArray(new String[10] ); |
418 |
|
|
shouldThrow(); |
419 |
|
|
} catch (ArrayStoreException success) {} |
420 |
dl |
1.5 |
} |
421 |
jsr166 |
1.8 |
|
422 |
dl |
1.4 |
/** |
423 |
dl |
1.5 |
* iterator iterates through all elements |
424 |
dl |
1.4 |
*/ |
425 |
|
|
public void testIterator() { |
426 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
427 |
dl |
1.1 |
int i = 0; |
428 |
jsr166 |
1.12 |
Iterator it = q.iterator(); |
429 |
jsr166 |
1.9 |
while (it.hasNext()) { |
430 |
dl |
1.1 |
assertTrue(q.contains(it.next())); |
431 |
|
|
++i; |
432 |
|
|
} |
433 |
dl |
1.3 |
assertEquals(i, SIZE); |
434 |
dl |
1.1 |
} |
435 |
|
|
|
436 |
dl |
1.4 |
/** |
437 |
dl |
1.5 |
* iterator ordering is FIFO |
438 |
dl |
1.4 |
*/ |
439 |
dl |
1.1 |
public void testIteratorOrdering() { |
440 |
|
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
441 |
dl |
1.3 |
q.add(one); |
442 |
|
|
q.add(two); |
443 |
|
|
q.add(three); |
444 |
dl |
1.1 |
|
445 |
|
|
int k = 0; |
446 |
|
|
for (Iterator it = q.iterator(); it.hasNext();) { |
447 |
|
|
int i = ((Integer)(it.next())).intValue(); |
448 |
dl |
1.4 |
assertEquals(++k, i); |
449 |
dl |
1.1 |
} |
450 |
|
|
|
451 |
dl |
1.4 |
assertEquals(3, k); |
452 |
dl |
1.1 |
} |
453 |
|
|
|
454 |
dl |
1.4 |
/** |
455 |
dl |
1.5 |
* Modifications do not cause iterators to fail |
456 |
dl |
1.4 |
*/ |
457 |
dl |
1.1 |
public void testWeaklyConsistentIteration () { |
458 |
|
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
459 |
dl |
1.3 |
q.add(one); |
460 |
|
|
q.add(two); |
461 |
|
|
q.add(three); |
462 |
dl |
1.1 |
|
463 |
jsr166 |
1.13 |
for (Iterator it = q.iterator(); it.hasNext();) { |
464 |
|
|
q.remove(); |
465 |
|
|
it.next(); |
466 |
dl |
1.1 |
} |
467 |
|
|
|
468 |
|
|
assertEquals("queue should be empty again", 0, q.size()); |
469 |
|
|
} |
470 |
|
|
|
471 |
dl |
1.4 |
/** |
472 |
dl |
1.5 |
* iterator.remove removes current element |
473 |
dl |
1.4 |
*/ |
474 |
dl |
1.1 |
public void testIteratorRemove () { |
475 |
|
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
476 |
dl |
1.3 |
q.add(one); |
477 |
|
|
q.add(two); |
478 |
|
|
q.add(three); |
479 |
dl |
1.1 |
Iterator it = q.iterator(); |
480 |
|
|
it.next(); |
481 |
|
|
it.remove(); |
482 |
|
|
it = q.iterator(); |
483 |
dl |
1.3 |
assertEquals(it.next(), two); |
484 |
|
|
assertEquals(it.next(), three); |
485 |
dl |
1.1 |
assertFalse(it.hasNext()); |
486 |
|
|
} |
487 |
|
|
|
488 |
|
|
|
489 |
dl |
1.4 |
/** |
490 |
dl |
1.5 |
* toString contains toStrings of elements |
491 |
dl |
1.4 |
*/ |
492 |
|
|
public void testToString() { |
493 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
494 |
dl |
1.1 |
String s = q.toString(); |
495 |
dl |
1.3 |
for (int i = 0; i < SIZE; ++i) { |
496 |
dl |
1.1 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
497 |
|
|
} |
498 |
jsr166 |
1.8 |
} |
499 |
dl |
1.2 |
|
500 |
dl |
1.4 |
/** |
501 |
dl |
1.5 |
* A deserialized serialized queue has same elements in same order |
502 |
dl |
1.4 |
*/ |
503 |
jsr166 |
1.13 |
public void testSerialization() throws Exception { |
504 |
dl |
1.3 |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
505 |
jsr166 |
1.13 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
506 |
|
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
507 |
|
|
out.writeObject(q); |
508 |
|
|
out.close(); |
509 |
|
|
|
510 |
|
|
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
511 |
|
|
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
512 |
|
|
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
513 |
|
|
assertEquals(q.size(), r.size()); |
514 |
|
|
while (!q.isEmpty()) |
515 |
|
|
assertEquals(q.remove(), r.remove()); |
516 |
dl |
1.2 |
} |
517 |
dl |
1.1 |
|
518 |
|
|
} |