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 |
< |
} |
172 |
< |
catch (NullPointerException success) {} |
170 |
> |
shouldThrow(); |
171 |
> |
} catch (NullPointerException success) {} |
172 |
> |
} |
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 |
< |
public void testAddAll2(){ |
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 |
> |
/** |
198 |
> |
* addAll of a collection with any null elements throws NPE after |
199 |
> |
* possibly adding some elements |
200 |
> |
*/ |
201 |
> |
public void testAddAll3() { |
202 |
|
try { |
203 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
204 |
< |
Integer[] ints = new Integer[N]; |
205 |
< |
for (int i = 0; i < N-1; ++i) |
204 |
> |
Integer[] ints = new Integer[SIZE]; |
205 |
> |
for (int i = 0; i < SIZE-1; ++i) |
206 |
|
ints[i] = new Integer(i); |
207 |
|
q.addAll(Arrays.asList(ints)); |
208 |
< |
fail("Cannot add null elements"); |
209 |
< |
} |
156 |
< |
catch (NullPointerException success) {} |
208 |
> |
shouldThrow(); |
209 |
> |
} catch (NullPointerException success) {} |
210 |
|
} |
211 |
|
|
212 |
< |
public void testAddAll5(){ |
213 |
< |
try { |
214 |
< |
Integer[] empty = new Integer[0]; |
215 |
< |
Integer[] ints = new Integer[N]; |
216 |
< |
for (int i = 0; i < N; ++i) |
217 |
< |
ints[i] = new Integer(i); |
218 |
< |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
219 |
< |
assertFalse(q.addAll(Arrays.asList(empty))); |
220 |
< |
assertTrue(q.addAll(Arrays.asList(ints))); |
221 |
< |
for (int i = 0; i < N; ++i) |
222 |
< |
assertEquals(ints[i], q.poll()); |
223 |
< |
} |
224 |
< |
finally {} |
212 |
> |
/** |
213 |
> |
* Queue contains all elements, in traversal order, of successful addAll |
214 |
> |
*/ |
215 |
> |
public void testAddAll5() { |
216 |
> |
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 |
|
} |
226 |
|
|
227 |
< |
public void testPoll(){ |
228 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
229 |
< |
for (int i = 0; i < N; ++i) { |
230 |
< |
assertEquals(i, ((Integer)q.poll()).intValue()); |
227 |
> |
/** |
228 |
> |
* poll succeeds unless empty |
229 |
> |
*/ |
230 |
> |
public void testPoll() { |
231 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
232 |
> |
for (int i = 0; i < SIZE; ++i) { |
233 |
> |
assertEquals(i, q.poll()); |
234 |
|
} |
235 |
< |
assertNull(q.poll()); |
235 |
> |
assertNull(q.poll()); |
236 |
|
} |
237 |
|
|
238 |
< |
public void testPeek(){ |
239 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
240 |
< |
for (int i = 0; i < N; ++i) { |
241 |
< |
assertEquals(i, ((Integer)q.peek()).intValue()); |
242 |
< |
q.poll(); |
238 |
> |
/** |
239 |
> |
* peek returns next element, or null if empty |
240 |
> |
*/ |
241 |
> |
public void testPeek() { |
242 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
243 |
> |
for (int i = 0; i < SIZE; ++i) { |
244 |
> |
assertEquals(i, q.peek()); |
245 |
> |
assertEquals(i, q.poll()); |
246 |
|
assertTrue(q.peek() == null || |
247 |
< |
i != ((Integer)q.peek()).intValue()); |
247 |
> |
!q.peek().equals(i)); |
248 |
|
} |
249 |
< |
assertNull(q.peek()); |
249 |
> |
assertNull(q.peek()); |
250 |
|
} |
251 |
|
|
252 |
< |
public void testElement(){ |
253 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
254 |
< |
for (int i = 0; i < N; ++i) { |
255 |
< |
assertEquals(i, ((Integer)q.element()).intValue()); |
256 |
< |
q.poll(); |
252 |
> |
/** |
253 |
> |
* element returns next element, or throws NSEE if empty |
254 |
> |
*/ |
255 |
> |
public void testElement() { |
256 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
257 |
> |
for (int i = 0; i < SIZE; ++i) { |
258 |
> |
assertEquals(i, q.element()); |
259 |
> |
assertEquals(i, q.poll()); |
260 |
|
} |
261 |
|
try { |
262 |
|
q.element(); |
263 |
< |
fail("no such element"); |
264 |
< |
} |
203 |
< |
catch (NoSuchElementException success) {} |
263 |
> |
shouldThrow(); |
264 |
> |
} catch (NoSuchElementException success) {} |
265 |
|
} |
266 |
|
|
267 |
< |
public void testRemove(){ |
268 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
269 |
< |
for (int i = 0; i < N; ++i) { |
270 |
< |
assertEquals(i, ((Integer)q.remove()).intValue()); |
267 |
> |
/** |
268 |
> |
* remove removes next element, or throws NSEE if empty |
269 |
> |
*/ |
270 |
> |
public void testRemove() { |
271 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
272 |
> |
for (int i = 0; i < SIZE; ++i) { |
273 |
> |
assertEquals(i, q.remove()); |
274 |
|
} |
275 |
|
try { |
276 |
|
q.remove(); |
277 |
< |
fail("remove should throw"); |
278 |
< |
} catch (NoSuchElementException success){ |
215 |
< |
} |
277 |
> |
shouldThrow(); |
278 |
> |
} catch (NoSuchElementException success) {} |
279 |
|
} |
280 |
|
|
281 |
< |
public void testRemoveElement(){ |
282 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
283 |
< |
for (int i = 1; i < N; i+=2) { |
281 |
> |
/** |
282 |
> |
* remove(x) removes x and returns true if present |
283 |
> |
*/ |
284 |
> |
public void testRemoveElement() { |
285 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
286 |
> |
for (int i = 1; i < SIZE; i+=2) { |
287 |
|
assertTrue(q.remove(new Integer(i))); |
288 |
|
} |
289 |
< |
for (int i = 0; i < N; i+=2) { |
289 |
> |
for (int i = 0; i < SIZE; i+=2) { |
290 |
|
assertTrue(q.remove(new Integer(i))); |
291 |
|
assertFalse(q.remove(new Integer(i+1))); |
292 |
|
} |
293 |
|
assertTrue(q.isEmpty()); |
294 |
|
} |
295 |
< |
|
296 |
< |
public void testContains(){ |
297 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
298 |
< |
for (int i = 0; i < N; ++i) { |
295 |
> |
|
296 |
> |
/** |
297 |
> |
* contains(x) reports true when elements added but not yet removed |
298 |
> |
*/ |
299 |
> |
public void testContains() { |
300 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
301 |
> |
for (int i = 0; i < SIZE; ++i) { |
302 |
|
assertTrue(q.contains(new Integer(i))); |
303 |
|
q.poll(); |
304 |
|
assertFalse(q.contains(new Integer(i))); |
305 |
|
} |
306 |
|
} |
307 |
|
|
308 |
< |
public void testClear(){ |
309 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
308 |
> |
/** |
309 |
> |
* clear removes all elements |
310 |
> |
*/ |
311 |
> |
public void testClear() { |
312 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
313 |
|
q.clear(); |
314 |
|
assertTrue(q.isEmpty()); |
315 |
|
assertEquals(0, q.size()); |
316 |
< |
q.add(new Integer(1)); |
316 |
> |
q.add(one); |
317 |
|
assertFalse(q.isEmpty()); |
318 |
|
q.clear(); |
319 |
|
assertTrue(q.isEmpty()); |
320 |
|
} |
321 |
|
|
322 |
< |
public void testContainsAll(){ |
323 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
322 |
> |
/** |
323 |
> |
* containsAll(c) is true when c contains a subset of elements |
324 |
> |
*/ |
325 |
> |
public void testContainsAll() { |
326 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
327 |
|
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue(); |
328 |
< |
for (int i = 0; i < N; ++i) { |
328 |
> |
for (int i = 0; i < SIZE; ++i) { |
329 |
|
assertTrue(q.containsAll(p)); |
330 |
|
assertFalse(p.containsAll(q)); |
331 |
|
p.add(new Integer(i)); |
333 |
|
assertTrue(p.containsAll(q)); |
334 |
|
} |
335 |
|
|
336 |
< |
public void testRetainAll(){ |
337 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
338 |
< |
ConcurrentLinkedQueue p = fullQueue(N); |
339 |
< |
for (int i = 0; i < N; ++i) { |
336 |
> |
/** |
337 |
> |
* retainAll(c) retains only those elements of c and reports true if change |
338 |
> |
*/ |
339 |
> |
public void testRetainAll() { |
340 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
341 |
> |
ConcurrentLinkedQueue p = populatedQueue(SIZE); |
342 |
> |
for (int i = 0; i < SIZE; ++i) { |
343 |
|
boolean changed = q.retainAll(p); |
344 |
|
if (i == 0) |
345 |
|
assertFalse(changed); |
347 |
|
assertTrue(changed); |
348 |
|
|
349 |
|
assertTrue(q.containsAll(p)); |
350 |
< |
assertEquals(N-i, q.size()); |
350 |
> |
assertEquals(SIZE-i, q.size()); |
351 |
|
p.remove(); |
352 |
|
} |
353 |
|
} |
354 |
|
|
355 |
< |
public void testRemoveAll(){ |
356 |
< |
for (int i = 1; i < N; ++i) { |
357 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
358 |
< |
ConcurrentLinkedQueue p = fullQueue(i); |
355 |
> |
/** |
356 |
> |
* removeAll(c) removes only those elements of c and reports true if changed |
357 |
> |
*/ |
358 |
> |
public void testRemoveAll() { |
359 |
> |
for (int i = 1; i < SIZE; ++i) { |
360 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
361 |
> |
ConcurrentLinkedQueue p = populatedQueue(i); |
362 |
|
assertTrue(q.removeAll(p)); |
363 |
< |
assertEquals(N-i, q.size()); |
363 |
> |
assertEquals(SIZE-i, q.size()); |
364 |
|
for (int j = 0; j < i; ++j) { |
365 |
|
Integer I = (Integer)(p.remove()); |
366 |
|
assertFalse(q.contains(I)); |
368 |
|
} |
369 |
|
} |
370 |
|
|
371 |
< |
public void testToArray(){ |
372 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
373 |
< |
Object[] o = q.toArray(); |
374 |
< |
Arrays.sort(o); |
375 |
< |
for(int i = 0; i < o.length; i++) |
376 |
< |
assertEquals(o[i], q.poll()); |
371 |
> |
/** |
372 |
> |
* toArray contains all elements in FIFO order |
373 |
> |
*/ |
374 |
> |
public void testToArray() { |
375 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
376 |
> |
Object[] o = q.toArray(); |
377 |
> |
for (int i = 0; i < o.length; i++) |
378 |
> |
assertSame(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); |
385 |
< |
Arrays.sort(ints); |
386 |
< |
for(int i = 0; i < ints.length; i++) |
387 |
< |
assertEquals(ints[i], q.poll()); |
381 |
> |
/** |
382 |
> |
* toArray(a) contains all elements in FIFO order |
383 |
> |
*/ |
384 |
> |
public void testToArray2() { |
385 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
386 |
> |
Integer[] ints = new Integer[SIZE]; |
387 |
> |
assertSame(ints, q.toArray(ints)); |
388 |
> |
for (int i = 0; i < ints.length; i++) |
389 |
> |
assertSame(ints[i], q.poll()); |
390 |
|
} |
391 |
< |
|
392 |
< |
public void testIterator(){ |
393 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
391 |
> |
|
392 |
> |
/** |
393 |
> |
* toArray(null) throws NullPointerException |
394 |
> |
*/ |
395 |
> |
public void testToArray_NullArg() { |
396 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
397 |
> |
try { |
398 |
> |
q.toArray(null); |
399 |
> |
shouldThrow(); |
400 |
> |
} catch (NullPointerException success) {} |
401 |
> |
} |
402 |
> |
|
403 |
> |
/** |
404 |
> |
* toArray(incompatible array type) throws ArrayStoreException |
405 |
> |
*/ |
406 |
> |
public void testToArray1_BadArg() { |
407 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
408 |
> |
try { |
409 |
> |
q.toArray(new String[10]); |
410 |
> |
shouldThrow(); |
411 |
> |
} catch (ArrayStoreException success) {} |
412 |
> |
} |
413 |
> |
|
414 |
> |
/** |
415 |
> |
* iterator iterates through all elements |
416 |
> |
*/ |
417 |
> |
public void testIterator() { |
418 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
419 |
|
int i = 0; |
420 |
< |
Iterator it = q.iterator(); |
421 |
< |
while(it.hasNext()) { |
420 |
> |
Iterator it = q.iterator(); |
421 |
> |
while (it.hasNext()) { |
422 |
|
assertTrue(q.contains(it.next())); |
423 |
|
++i; |
424 |
|
} |
425 |
< |
assertEquals(i, N); |
425 |
> |
assertEquals(i, SIZE); |
426 |
|
} |
427 |
|
|
428 |
+ |
/** |
429 |
+ |
* iterator ordering is FIFO |
430 |
+ |
*/ |
431 |
|
public void testIteratorOrdering() { |
319 |
– |
|
432 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
433 |
< |
|
434 |
< |
q.add(new Integer(1)); |
435 |
< |
q.add(new Integer(2)); |
324 |
< |
q.add(new Integer(3)); |
433 |
> |
q.add(one); |
434 |
> |
q.add(two); |
435 |
> |
q.add(three); |
436 |
|
|
437 |
|
int k = 0; |
438 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
439 |
< |
int i = ((Integer)(it.next())).intValue(); |
329 |
< |
assertEquals("items should come out in order", ++k, i); |
439 |
> |
assertEquals(++k, it.next()); |
440 |
|
} |
441 |
|
|
442 |
< |
assertEquals("should go through 3 elements", 3, k); |
442 |
> |
assertEquals(3, k); |
443 |
|
} |
444 |
|
|
445 |
< |
public void testWeaklyConsistentIteration () { |
446 |
< |
|
445 |
> |
/** |
446 |
> |
* Modifications do not cause iterators to fail |
447 |
> |
*/ |
448 |
> |
public void testWeaklyConsistentIteration() { |
449 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
450 |
+ |
q.add(one); |
451 |
+ |
q.add(two); |
452 |
+ |
q.add(three); |
453 |
|
|
454 |
< |
q.add(new Integer(1)); |
455 |
< |
q.add(new Integer(2)); |
456 |
< |
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"); |
454 |
> |
for (Iterator it = q.iterator(); it.hasNext();) { |
455 |
> |
q.remove(); |
456 |
> |
it.next(); |
457 |
|
} |
458 |
|
|
459 |
|
assertEquals("queue should be empty again", 0, q.size()); |
460 |
|
} |
461 |
|
|
462 |
< |
public void testIteratorRemove () { |
463 |
< |
|
462 |
> |
/** |
463 |
> |
* iterator.remove removes current element |
464 |
> |
*/ |
465 |
> |
public void testIteratorRemove() { |
466 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
467 |
< |
|
468 |
< |
q.add(new Integer(1)); |
469 |
< |
q.add(new Integer(2)); |
362 |
< |
q.add(new Integer(3)); |
363 |
< |
|
467 |
> |
q.add(one); |
468 |
> |
q.add(two); |
469 |
> |
q.add(three); |
470 |
|
Iterator it = q.iterator(); |
471 |
|
it.next(); |
472 |
|
it.remove(); |
367 |
– |
|
473 |
|
it = q.iterator(); |
474 |
< |
assertEquals(it.next(), new Integer(2)); |
475 |
< |
assertEquals(it.next(), new Integer(3)); |
474 |
> |
assertSame(it.next(), two); |
475 |
> |
assertSame(it.next(), three); |
476 |
|
assertFalse(it.hasNext()); |
477 |
|
} |
478 |
|
|
479 |
|
|
480 |
< |
public void testToString(){ |
481 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
480 |
> |
/** |
481 |
> |
* toString contains toStrings of elements |
482 |
> |
*/ |
483 |
> |
public void testToString() { |
484 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
485 |
|
String s = q.toString(); |
486 |
< |
for (int i = 0; i < N; ++i) { |
486 |
> |
for (int i = 0; i < SIZE; ++i) { |
487 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
488 |
|
} |
489 |
< |
} |
489 |
> |
} |
490 |
|
|
491 |
< |
public void testSerialization() { |
492 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
493 |
< |
try { |
494 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
495 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
496 |
< |
out.writeObject(q); |
497 |
< |
out.close(); |
498 |
< |
|
499 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
500 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
501 |
< |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
502 |
< |
assertEquals(q.size(), r.size()); |
503 |
< |
while (!q.isEmpty()) |
504 |
< |
assertEquals(q.remove(), r.remove()); |
505 |
< |
} catch(Exception e){ |
506 |
< |
e.printStackTrace(); |
399 |
< |
fail("unexpected exception"); |
400 |
< |
} |
491 |
> |
/** |
492 |
> |
* A deserialized serialized queue has same elements in same order |
493 |
> |
*/ |
494 |
> |
public void testSerialization() throws Exception { |
495 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
496 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
497 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
498 |
> |
out.writeObject(q); |
499 |
> |
out.close(); |
500 |
> |
|
501 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
502 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
503 |
> |
ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject(); |
504 |
> |
assertEquals(q.size(), r.size()); |
505 |
> |
while (!q.isEmpty()) |
506 |
> |
assertEquals(q.remove(), r.remove()); |
507 |
|
} |
508 |
|
|
509 |
|
} |