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"); |
76 |
< |
} |
70 |
< |
catch (NullPointerException success) {} |
75 |
> |
shouldThrow(); |
76 |
> |
} catch (NullPointerException success) {} |
77 |
|
} |
78 |
|
|
79 |
< |
public void testConstructor6(){ |
80 |
< |
try { |
81 |
< |
Integer[] ints = new Integer[N]; |
82 |
< |
for (int i = 0; i < N; ++i) |
83 |
< |
ints[i] = new Integer(i); |
84 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
85 |
< |
for (int i = 0; i < N; ++i) |
86 |
< |
assertEquals(ints[i], q.poll()); |
87 |
< |
} |
88 |
< |
finally {} |
79 |
> |
/** |
80 |
> |
* Queue contains all elements of collection used to initialize |
81 |
> |
*/ |
82 |
> |
public void testConstructor6() { |
83 |
> |
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 |
|
} |
90 |
|
|
91 |
+ |
/** |
92 |
+ |
* isEmpty is true before add, false after |
93 |
+ |
*/ |
94 |
|
public void testEmpty() { |
95 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
96 |
|
assertTrue(q.isEmpty()); |
97 |
< |
q.add(new Integer(1)); |
97 |
> |
q.add(one); |
98 |
|
assertFalse(q.isEmpty()); |
99 |
< |
q.add(new Integer(2)); |
99 |
> |
q.add(two); |
100 |
|
q.remove(); |
101 |
|
q.remove(); |
102 |
|
assertTrue(q.isEmpty()); |
103 |
|
} |
104 |
|
|
105 |
+ |
/** |
106 |
+ |
* size changes when elements added and removed |
107 |
+ |
*/ |
108 |
|
public void testSize() { |
109 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
110 |
< |
for (int i = 0; i < N; ++i) { |
111 |
< |
assertEquals(N-i, q.size()); |
109 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
110 |
> |
for (int i = 0; i < SIZE; ++i) { |
111 |
> |
assertEquals(SIZE-i, q.size()); |
112 |
|
q.remove(); |
113 |
|
} |
114 |
< |
for (int i = 0; i < N; ++i) { |
114 |
> |
for (int i = 0; i < SIZE; ++i) { |
115 |
|
assertEquals(i, q.size()); |
116 |
|
q.add(new Integer(i)); |
117 |
|
} |
118 |
|
} |
119 |
|
|
120 |
< |
public void testOfferNull(){ |
121 |
< |
try { |
120 |
> |
/** |
121 |
> |
* offer(null) throws NPE |
122 |
> |
*/ |
123 |
> |
public void testOfferNull() { |
124 |
> |
try { |
125 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
126 |
|
q.offer(null); |
127 |
< |
fail("should throw NPE"); |
128 |
< |
} catch (NullPointerException success) { } |
127 |
> |
shouldThrow(); |
128 |
> |
} catch (NullPointerException success) {} |
129 |
> |
} |
130 |
> |
|
131 |
> |
/** |
132 |
> |
* add(null) throws NPE |
133 |
> |
*/ |
134 |
> |
public void testAddNull() { |
135 |
> |
try { |
136 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
137 |
> |
q.add(null); |
138 |
> |
shouldThrow(); |
139 |
> |
} catch (NullPointerException success) {} |
140 |
|
} |
141 |
|
|
142 |
+ |
|
143 |
+ |
/** |
144 |
+ |
* Offer returns true |
145 |
+ |
*/ |
146 |
|
public void testOffer() { |
147 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
148 |
< |
assertTrue(q.offer(new Integer(0))); |
149 |
< |
assertTrue(q.offer(new Integer(1))); |
148 |
> |
assertTrue(q.offer(zero)); |
149 |
> |
assertTrue(q.offer(one)); |
150 |
|
} |
151 |
|
|
152 |
< |
public void testAdd(){ |
152 |
> |
/** |
153 |
> |
* add returns true |
154 |
> |
*/ |
155 |
> |
public void testAdd() { |
156 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
157 |
< |
for (int i = 0; i < N; ++i) { |
157 |
> |
for (int i = 0; i < SIZE; ++i) { |
158 |
|
assertEquals(i, q.size()); |
159 |
|
assertTrue(q.add(new Integer(i))); |
160 |
|
} |
161 |
|
} |
162 |
|
|
163 |
< |
public void testAddAll1(){ |
163 |
> |
/** |
164 |
> |
* addAll(null) throws NPE |
165 |
> |
*/ |
166 |
> |
public void testAddAll1() { |
167 |
|
try { |
168 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
169 |
|
q.addAll(null); |
170 |
< |
fail("Cannot add null collection"); |
171 |
< |
} |
136 |
< |
catch (NullPointerException success) {} |
170 |
> |
shouldThrow(); |
171 |
> |
} catch (NullPointerException success) {} |
172 |
|
} |
173 |
< |
public void testAddAll2(){ |
173 |
> |
|
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 |
> |
} catch (IllegalArgumentException success) {} |
183 |
> |
} |
184 |
> |
|
185 |
> |
/** |
186 |
> |
* addAll of a collection with null elements throws NPE |
187 |
> |
*/ |
188 |
> |
public void testAddAll2() { |
189 |
|
try { |
190 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
191 |
< |
Integer[] ints = new Integer[N]; |
191 |
> |
Integer[] ints = new Integer[SIZE]; |
192 |
|
q.addAll(Arrays.asList(ints)); |
193 |
< |
fail("Cannot add null elements"); |
194 |
< |
} |
145 |
< |
catch (NullPointerException success) {} |
193 |
> |
shouldThrow(); |
194 |
> |
} catch (NullPointerException success) {} |
195 |
|
} |
196 |
< |
public void testAddAll3(){ |
196 |
> |
/** |
197 |
> |
* addAll of a collection with any null elements throws NPE after |
198 |
> |
* possibly adding some elements |
199 |
> |
*/ |
200 |
> |
public void testAddAll3() { |
201 |
|
try { |
202 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
203 |
< |
Integer[] ints = new Integer[N]; |
204 |
< |
for (int i = 0; i < N-1; ++i) |
203 |
> |
Integer[] ints = new Integer[SIZE]; |
204 |
> |
for (int i = 0; i < SIZE-1; ++i) |
205 |
|
ints[i] = new Integer(i); |
206 |
|
q.addAll(Arrays.asList(ints)); |
207 |
< |
fail("Cannot add null elements"); |
208 |
< |
} |
156 |
< |
catch (NullPointerException success) {} |
207 |
> |
shouldThrow(); |
208 |
> |
} catch (NullPointerException success) {} |
209 |
|
} |
210 |
|
|
211 |
< |
public void testAddAll5(){ |
212 |
< |
try { |
213 |
< |
Integer[] empty = new Integer[0]; |
214 |
< |
Integer[] ints = new Integer[N]; |
215 |
< |
for (int i = 0; i < N; ++i) |
216 |
< |
ints[i] = new Integer(i); |
217 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
218 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
219 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
220 |
< |
for (int i = 0; i < N; ++i) |
221 |
< |
assertEquals(ints[i], q.poll()); |
222 |
< |
} |
223 |
< |
finally {} |
211 |
> |
/** |
212 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
213 |
> |
*/ |
214 |
> |
public void testAddAll5() { |
215 |
> |
Integer[] empty = new Integer[0]; |
216 |
> |
Integer[] ints = new Integer[SIZE]; |
217 |
> |
for (int i = 0; i < SIZE; ++i) |
218 |
> |
ints[i] = new Integer(i); |
219 |
> |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
220 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
221 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
222 |
> |
for (int i = 0; i < SIZE; ++i) |
223 |
> |
assertEquals(ints[i], q.poll()); |
224 |
|
} |
225 |
|
|
226 |
< |
public void testPoll(){ |
227 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
228 |
< |
for (int i = 0; i < N; ++i) { |
229 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
226 |
> |
/** |
227 |
> |
* poll succeeds unless empty |
228 |
> |
*/ |
229 |
> |
public void testPoll() { |
230 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
231 |
> |
for (int i = 0; i < SIZE; ++i) { |
232 |
> |
assertEquals(i, q.poll()); |
233 |
|
} |
234 |
< |
assertNull(q.poll()); |
234 |
> |
assertNull(q.poll()); |
235 |
|
} |
236 |
|
|
237 |
< |
public void testPeek(){ |
238 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
239 |
< |
for (int i = 0; i < N; ++i) { |
240 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
241 |
< |
q.poll(); |
237 |
> |
/** |
238 |
> |
* peek returns next element, or null if empty |
239 |
> |
*/ |
240 |
> |
public void testPeek() { |
241 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
242 |
> |
for (int i = 0; i < SIZE; ++i) { |
243 |
> |
assertEquals(i, q.peek()); |
244 |
> |
assertEquals(i, q.poll()); |
245 |
|
assertTrue(q.peek() == null || |
246 |
< |
i != ((Integer)q.peek()).intValue()); |
246 |
> |
!q.peek().equals(i)); |
247 |
|
} |
248 |
< |
assertNull(q.peek()); |
248 |
> |
assertNull(q.peek()); |
249 |
|
} |
250 |
|
|
251 |
< |
public void testElement(){ |
252 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
253 |
< |
for (int i = 0; i < N; ++i) { |
254 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
255 |
< |
q.poll(); |
251 |
> |
/** |
252 |
> |
* element returns next element, or throws NSEE if empty |
253 |
> |
*/ |
254 |
> |
public void testElement() { |
255 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
256 |
> |
for (int i = 0; i < SIZE; ++i) { |
257 |
> |
assertEquals(i, q.element()); |
258 |
> |
assertEquals(i, q.poll()); |
259 |
|
} |
260 |
|
try { |
261 |
|
q.element(); |
262 |
< |
fail("no such element"); |
263 |
< |
} |
203 |
< |
catch (NoSuchElementException success) {} |
262 |
> |
shouldThrow(); |
263 |
> |
} catch (NoSuchElementException success) {} |
264 |
|
} |
265 |
|
|
266 |
< |
public void testRemove(){ |
267 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
268 |
< |
for (int i = 0; i < N; ++i) { |
269 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
266 |
> |
/** |
267 |
> |
* remove removes next element, or throws NSEE if empty |
268 |
> |
*/ |
269 |
> |
public void testRemove() { |
270 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
271 |
> |
for (int i = 0; i < SIZE; ++i) { |
272 |
> |
assertEquals(i, q.remove()); |
273 |
|
} |
274 |
|
try { |
275 |
|
q.remove(); |
276 |
< |
fail("remove should throw"); |
277 |
< |
} catch (NoSuchElementException success){ |
215 |
< |
} |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (NoSuchElementException success) {} |
278 |
|
} |
279 |
|
|
280 |
< |
public void testRemoveElement(){ |
281 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
282 |
< |
for (int i = 1; i < N; i+=2) { |
280 |
> |
/** |
281 |
> |
* remove(x) removes x and returns true if present |
282 |
> |
*/ |
283 |
> |
public void testRemoveElement() { |
284 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
285 |
> |
for (int i = 1; i < SIZE; i+=2) { |
286 |
|
assertTrue(q.remove(new Integer(i))); |
287 |
|
} |
288 |
< |
for (int i = 0; i < N; i+=2) { |
288 |
> |
for (int i = 0; i < SIZE; i+=2) { |
289 |
|
assertTrue(q.remove(new Integer(i))); |
290 |
|
assertFalse(q.remove(new Integer(i+1))); |
291 |
|
} |
292 |
|
assertTrue(q.isEmpty()); |
293 |
|
} |
294 |
< |
|
295 |
< |
public void testContains(){ |
296 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
297 |
< |
for (int i = 0; i < N; ++i) { |
294 |
> |
|
295 |
> |
/** |
296 |
> |
* contains(x) reports true when elements added but not yet removed |
297 |
> |
*/ |
298 |
> |
public void testContains() { |
299 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
300 |
> |
for (int i = 0; i < SIZE; ++i) { |
301 |
|
assertTrue(q.contains(new Integer(i))); |
302 |
|
q.poll(); |
303 |
|
assertFalse(q.contains(new Integer(i))); |
304 |
|
} |
305 |
|
} |
306 |
|
|
307 |
< |
public void testClear(){ |
308 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
307 |
> |
/** |
308 |
> |
* clear removes all elements |
309 |
> |
*/ |
310 |
> |
public void testClear() { |
311 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
312 |
|
q.clear(); |
313 |
|
assertTrue(q.isEmpty()); |
314 |
|
assertEquals(0, q.size()); |
315 |
< |
q.add(new Integer(1)); |
315 |
> |
q.add(one); |
316 |
|
assertFalse(q.isEmpty()); |
317 |
|
q.clear(); |
318 |
|
assertTrue(q.isEmpty()); |
319 |
|
} |
320 |
|
|
321 |
< |
public void testContainsAll(){ |
322 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
321 |
> |
/** |
322 |
> |
* containsAll(c) is true when c contains a subset of elements |
323 |
> |
*/ |
324 |
> |
public void testContainsAll() { |
325 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
326 |
|
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue(); |
327 |
< |
for (int i = 0; i < N; ++i) { |
327 |
> |
for (int i = 0; i < SIZE; ++i) { |
328 |
|
assertTrue(q.containsAll(p)); |
329 |
|
assertFalse(p.containsAll(q)); |
330 |
|
p.add(new Integer(i)); |
332 |
|
assertTrue(p.containsAll(q)); |
333 |
|
} |
334 |
|
|
335 |
< |
public void testRetainAll(){ |
336 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
337 |
< |
ConcurrentLinkedQueue p = fullQueue(N); |
338 |
< |
for (int i = 0; i < N; ++i) { |
335 |
> |
/** |
336 |
> |
* retainAll(c) retains only those elements of c and reports true if change |
337 |
> |
*/ |
338 |
> |
public void testRetainAll() { |
339 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
340 |
> |
ConcurrentLinkedQueue p = populatedQueue(SIZE); |
341 |
> |
for (int i = 0; i < SIZE; ++i) { |
342 |
|
boolean changed = q.retainAll(p); |
343 |
|
if (i == 0) |
344 |
|
assertFalse(changed); |
346 |
|
assertTrue(changed); |
347 |
|
|
348 |
|
assertTrue(q.containsAll(p)); |
349 |
< |
assertEquals(N-i, q.size()); |
349 |
> |
assertEquals(SIZE-i, q.size()); |
350 |
|
p.remove(); |
351 |
|
} |
352 |
|
} |
353 |
|
|
354 |
< |
public void testRemoveAll(){ |
355 |
< |
for (int i = 1; i < N; ++i) { |
356 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
357 |
< |
ConcurrentLinkedQueue p = fullQueue(i); |
354 |
> |
/** |
355 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
356 |
> |
*/ |
357 |
> |
public void testRemoveAll() { |
358 |
> |
for (int i = 1; i < SIZE; ++i) { |
359 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
360 |
> |
ConcurrentLinkedQueue p = populatedQueue(i); |
361 |
|
assertTrue(q.removeAll(p)); |
362 |
< |
assertEquals(N-i, q.size()); |
362 |
> |
assertEquals(SIZE-i, q.size()); |
363 |
|
for (int j = 0; j < i; ++j) { |
364 |
|
Integer I = (Integer)(p.remove()); |
365 |
|
assertFalse(q.contains(I)); |
367 |
|
} |
368 |
|
} |
369 |
|
|
370 |
< |
public void testToArray(){ |
371 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
372 |
< |
Object[] o = q.toArray(); |
370 |
> |
/** |
371 |
> |
* toArray contains all elements |
372 |
> |
*/ |
373 |
> |
public void testToArray() { |
374 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
375 |
> |
Object[] o = q.toArray(); |
376 |
|
Arrays.sort(o); |
377 |
< |
for(int i = 0; i < o.length; i++) |
378 |
< |
assertEquals(o[i], q.poll()); |
377 |
> |
for (int i = 0; i < o.length; i++) |
378 |
> |
assertEquals(o[i], q.poll()); |
379 |
|
} |
380 |
|
|
381 |
< |
public void testToArray2(){ |
382 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
383 |
< |
Integer[] ints = new Integer[N]; |
384 |
< |
ints = (Integer[])q.toArray(ints); |
381 |
> |
/** |
382 |
> |
* toArray(a) contains all elements |
383 |
> |
*/ |
384 |
> |
public void testToArray2() { |
385 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
386 |
> |
Integer[] ints = new Integer[SIZE]; |
387 |
> |
ints = (Integer[])q.toArray(ints); |
388 |
|
Arrays.sort(ints); |
389 |
< |
for(int i = 0; i < ints.length; i++) |
389 |
> |
for (int i = 0; i < ints.length; i++) |
390 |
|
assertEquals(ints[i], q.poll()); |
391 |
|
} |
392 |
< |
|
393 |
< |
public void testIterator(){ |
394 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
392 |
> |
|
393 |
> |
/** |
394 |
> |
* toArray(null) throws NPE |
395 |
> |
*/ |
396 |
> |
public void testToArray_BadArg() { |
397 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
398 |
> |
try { |
399 |
> |
Object o[] = q.toArray(null); |
400 |
> |
shouldThrow(); |
401 |
> |
} catch (NullPointerException success) {} |
402 |
> |
} |
403 |
> |
|
404 |
> |
/** |
405 |
> |
* toArray with incompatible array type throws ArrayStoreException |
406 |
> |
*/ |
407 |
> |
public void testToArray1_BadArg() { |
408 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
409 |
> |
try { |
410 |
> |
Object o[] = q.toArray(new String[10]); |
411 |
> |
shouldThrow(); |
412 |
> |
} catch (ArrayStoreException success) {} |
413 |
> |
} |
414 |
> |
|
415 |
> |
/** |
416 |
> |
* iterator iterates through all elements |
417 |
> |
*/ |
418 |
> |
public void testIterator() { |
419 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
420 |
|
int i = 0; |
421 |
< |
Iterator it = q.iterator(); |
422 |
< |
while(it.hasNext()) { |
421 |
> |
Iterator it = q.iterator(); |
422 |
> |
while (it.hasNext()) { |
423 |
|
assertTrue(q.contains(it.next())); |
424 |
|
++i; |
425 |
|
} |
426 |
< |
assertEquals(i, N); |
426 |
> |
assertEquals(i, SIZE); |
427 |
|
} |
428 |
|
|
429 |
+ |
/** |
430 |
+ |
* iterator ordering is FIFO |
431 |
+ |
*/ |
432 |
|
public void testIteratorOrdering() { |
319 |
– |
|
433 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
434 |
< |
|
435 |
< |
q.add(new Integer(1)); |
436 |
< |
q.add(new Integer(2)); |
324 |
< |
q.add(new Integer(3)); |
434 |
> |
q.add(one); |
435 |
> |
q.add(two); |
436 |
> |
q.add(three); |
437 |
|
|
438 |
|
int k = 0; |
439 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
440 |
< |
int i = ((Integer)(it.next())).intValue(); |
329 |
< |
assertEquals("items should come out in order", ++k, i); |
440 |
> |
assertEquals(++k, it.next()); |
441 |
|
} |
442 |
|
|
443 |
< |
assertEquals("should go through 3 elements", 3, k); |
443 |
> |
assertEquals(3, k); |
444 |
|
} |
445 |
|
|
446 |
+ |
/** |
447 |
+ |
* Modifications do not cause iterators to fail |
448 |
+ |
*/ |
449 |
|
public void testWeaklyConsistentIteration () { |
336 |
– |
|
450 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
451 |
+ |
q.add(one); |
452 |
+ |
q.add(two); |
453 |
+ |
q.add(three); |
454 |
|
|
455 |
< |
q.add(new Integer(1)); |
456 |
< |
q.add(new Integer(2)); |
457 |
< |
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"); |
455 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
456 |
> |
q.remove(); |
457 |
> |
it.next(); |
458 |
|
} |
459 |
|
|
460 |
|
assertEquals("queue should be empty again", 0, q.size()); |
461 |
|
} |
462 |
|
|
463 |
+ |
/** |
464 |
+ |
* iterator.remove removes current element |
465 |
+ |
*/ |
466 |
|
public void testIteratorRemove () { |
357 |
– |
|
467 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
468 |
< |
|
469 |
< |
q.add(new Integer(1)); |
470 |
< |
q.add(new Integer(2)); |
362 |
< |
q.add(new Integer(3)); |
363 |
< |
|
468 |
> |
q.add(one); |
469 |
> |
q.add(two); |
470 |
> |
q.add(three); |
471 |
|
Iterator it = q.iterator(); |
472 |
|
it.next(); |
473 |
|
it.remove(); |
367 |
– |
|
474 |
|
it = q.iterator(); |
475 |
< |
assertEquals(it.next(), new Integer(2)); |
476 |
< |
assertEquals(it.next(), new Integer(3)); |
475 |
> |
assertEquals(it.next(), two); |
476 |
> |
assertEquals(it.next(), three); |
477 |
|
assertFalse(it.hasNext()); |
478 |
|
} |
479 |
|
|
480 |
|
|
481 |
< |
public void testToString(){ |
482 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
481 |
> |
/** |
482 |
> |
* toString contains toStrings of elements |
483 |
> |
*/ |
484 |
> |
public void testToString() { |
485 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
486 |
|
String s = q.toString(); |
487 |
< |
for (int i = 0; i < N; ++i) { |
487 |
> |
for (int i = 0; i < SIZE; ++i) { |
488 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
489 |
|
} |
490 |
< |
} |
490 |
> |
} |
491 |
|
|
492 |
< |
public void testSerialization() { |
493 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
494 |
< |
try { |
495 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
496 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
497 |
< |
out.writeObject(q); |
498 |
< |
out.close(); |
499 |
< |
|
500 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
501 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
502 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
503 |
< |
assertEquals(q.size(), r.size()); |
504 |
< |
while (!q.isEmpty()) |
505 |
< |
assertEquals(q.remove(), r.remove()); |
506 |
< |
} catch(Exception e){ |
507 |
< |
e.printStackTrace(); |
399 |
< |
fail("unexpected exception"); |
400 |
< |
} |
492 |
> |
/** |
493 |
> |
* A deserialized serialized queue has same elements in same order |
494 |
> |
*/ |
495 |
> |
public void testSerialization() throws Exception { |
496 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
497 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
498 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
499 |
> |
out.writeObject(q); |
500 |
> |
out.close(); |
501 |
> |
|
502 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
503 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
504 |
> |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
505 |
> |
assertEquals(q.size(), r.size()); |
506 |
> |
while (!q.isEmpty()) |
507 |
> |
assertEquals(q.remove(), r.remove()); |
508 |
|
} |
509 |
|
|
510 |
|
} |