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