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. |
6 |
*/ |
7 |
|
8 |
import junit.framework.*; |
9 |
import java.util.*; |
10 |
import java.util.concurrent.*; |
11 |
|
12 |
public class ConcurrentLinkedQueueTest extends TestCase { |
13 |
private static final int N = 10; |
14 |
private static final long SHORT_DELAY_MS = 100; |
15 |
private static final long MEDIUM_DELAY_MS = 1000; |
16 |
private static final long LONG_DELAY_MS = 10000; |
17 |
|
18 |
public static void main(String[] args) { |
19 |
junit.textui.TestRunner.run (suite()); |
20 |
} |
21 |
|
22 |
public static Test suite() { |
23 |
return new TestSuite(ConcurrentLinkedQueueTest.class); |
24 |
} |
25 |
|
26 |
/** |
27 |
* Create a queue of given size containing consecutive |
28 |
* Integers 0 ... n. |
29 |
*/ |
30 |
private ConcurrentLinkedQueue fullQueue(int n) { |
31 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
32 |
assertTrue(q.isEmpty()); |
33 |
for(int i = 0; i < n; ++i) |
34 |
assertTrue(q.offer(new Integer(i))); |
35 |
assertFalse(q.isEmpty()); |
36 |
assertEquals(n, q.size()); |
37 |
return q; |
38 |
} |
39 |
|
40 |
public void testConstructor1(){ |
41 |
assertEquals(0, new ConcurrentLinkedQueue().size()); |
42 |
} |
43 |
|
44 |
public void testConstructor3() { |
45 |
try { |
46 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null); |
47 |
fail("Cannot make from null collection"); |
48 |
} |
49 |
catch (NullPointerException success) {} |
50 |
} |
51 |
|
52 |
public void testConstructor4(){ |
53 |
try { |
54 |
Integer[] ints = new Integer[N]; |
55 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
56 |
fail("Cannot make with null elements"); |
57 |
} |
58 |
catch (NullPointerException success) {} |
59 |
} |
60 |
|
61 |
public void testConstructor5(){ |
62 |
try { |
63 |
Integer[] ints = new Integer[N]; |
64 |
for (int i = 0; i < N-1; ++i) |
65 |
ints[i] = new Integer(i); |
66 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
67 |
fail("Cannot make with null elements"); |
68 |
} |
69 |
catch (NullPointerException success) {} |
70 |
} |
71 |
|
72 |
public void testConstructor6(){ |
73 |
try { |
74 |
Integer[] ints = new Integer[N]; |
75 |
for (int i = 0; i < N; ++i) |
76 |
ints[i] = new Integer(i); |
77 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
78 |
for (int i = 0; i < N; ++i) |
79 |
assertEquals(ints[i], q.poll()); |
80 |
} |
81 |
finally {} |
82 |
} |
83 |
|
84 |
public void testEmpty() { |
85 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
86 |
assertTrue(q.isEmpty()); |
87 |
q.add(new Integer(1)); |
88 |
assertFalse(q.isEmpty()); |
89 |
q.add(new Integer(2)); |
90 |
q.remove(); |
91 |
q.remove(); |
92 |
assertTrue(q.isEmpty()); |
93 |
} |
94 |
|
95 |
public void testSize() { |
96 |
ConcurrentLinkedQueue q = fullQueue(N); |
97 |
for (int i = 0; i < N; ++i) { |
98 |
assertEquals(N-i, q.size()); |
99 |
q.remove(); |
100 |
} |
101 |
for (int i = 0; i < N; ++i) { |
102 |
assertEquals(i, q.size()); |
103 |
q.add(new Integer(i)); |
104 |
} |
105 |
} |
106 |
|
107 |
public void testOfferNull(){ |
108 |
try { |
109 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
110 |
q.offer(null); |
111 |
fail("should throw NPE"); |
112 |
} catch (NullPointerException success) { } |
113 |
} |
114 |
|
115 |
public void testOffer() { |
116 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
117 |
assertTrue(q.offer(new Integer(0))); |
118 |
assertTrue(q.offer(new Integer(1))); |
119 |
} |
120 |
|
121 |
public void testAdd(){ |
122 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
123 |
for (int i = 0; i < N; ++i) { |
124 |
assertEquals(i, q.size()); |
125 |
assertTrue(q.add(new Integer(i))); |
126 |
} |
127 |
} |
128 |
|
129 |
public void testAddAll1(){ |
130 |
try { |
131 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
132 |
q.addAll(null); |
133 |
fail("Cannot add null collection"); |
134 |
} |
135 |
catch (NullPointerException success) {} |
136 |
} |
137 |
public void testAddAll2(){ |
138 |
try { |
139 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
140 |
Integer[] ints = new Integer[N]; |
141 |
q.addAll(Arrays.asList(ints)); |
142 |
fail("Cannot add null elements"); |
143 |
} |
144 |
catch (NullPointerException success) {} |
145 |
} |
146 |
public void testAddAll3(){ |
147 |
try { |
148 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
149 |
Integer[] ints = new Integer[N]; |
150 |
for (int i = 0; i < N-1; ++i) |
151 |
ints[i] = new Integer(i); |
152 |
q.addAll(Arrays.asList(ints)); |
153 |
fail("Cannot add null elements"); |
154 |
} |
155 |
catch (NullPointerException success) {} |
156 |
} |
157 |
|
158 |
public void testAddAll5(){ |
159 |
try { |
160 |
Integer[] empty = new Integer[0]; |
161 |
Integer[] ints = new Integer[N]; |
162 |
for (int i = 0; i < N; ++i) |
163 |
ints[i] = new Integer(i); |
164 |
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
165 |
assertFalse(q.addAll(Arrays.asList(empty))); |
166 |
assertTrue(q.addAll(Arrays.asList(ints))); |
167 |
for (int i = 0; i < N; ++i) |
168 |
assertEquals(ints[i], q.poll()); |
169 |
} |
170 |
finally {} |
171 |
} |
172 |
|
173 |
public void testPoll(){ |
174 |
ConcurrentLinkedQueue q = fullQueue(N); |
175 |
for (int i = 0; i < N; ++i) { |
176 |
assertEquals(i, ((Integer)q.poll()).intValue()); |
177 |
} |
178 |
assertNull(q.poll()); |
179 |
} |
180 |
|
181 |
public void testPeek(){ |
182 |
ConcurrentLinkedQueue q = fullQueue(N); |
183 |
for (int i = 0; i < N; ++i) { |
184 |
assertEquals(i, ((Integer)q.peek()).intValue()); |
185 |
q.poll(); |
186 |
assertTrue(q.peek() == null || |
187 |
i != ((Integer)q.peek()).intValue()); |
188 |
} |
189 |
assertNull(q.peek()); |
190 |
} |
191 |
|
192 |
public void testElement(){ |
193 |
ConcurrentLinkedQueue q = fullQueue(N); |
194 |
for (int i = 0; i < N; ++i) { |
195 |
assertEquals(i, ((Integer)q.element()).intValue()); |
196 |
q.poll(); |
197 |
} |
198 |
try { |
199 |
q.element(); |
200 |
fail("no such element"); |
201 |
} |
202 |
catch (NoSuchElementException success) {} |
203 |
} |
204 |
|
205 |
public void testRemove(){ |
206 |
ConcurrentLinkedQueue q = fullQueue(N); |
207 |
for (int i = 0; i < N; ++i) { |
208 |
assertEquals(i, ((Integer)q.remove()).intValue()); |
209 |
} |
210 |
try { |
211 |
q.remove(); |
212 |
fail("remove should throw"); |
213 |
} catch (NoSuchElementException success){ |
214 |
} |
215 |
} |
216 |
|
217 |
public void testRemoveElement(){ |
218 |
ConcurrentLinkedQueue q = fullQueue(N); |
219 |
for (int i = 1; i < N; i+=2) { |
220 |
assertTrue(q.remove(new Integer(i))); |
221 |
} |
222 |
for (int i = 0; i < N; i+=2) { |
223 |
assertTrue(q.remove(new Integer(i))); |
224 |
assertFalse(q.remove(new Integer(i+1))); |
225 |
} |
226 |
assert(q.isEmpty()); |
227 |
} |
228 |
|
229 |
public void testContains(){ |
230 |
ConcurrentLinkedQueue q = fullQueue(N); |
231 |
for (int i = 0; i < N; ++i) { |
232 |
assertTrue(q.contains(new Integer(i))); |
233 |
q.poll(); |
234 |
assertFalse(q.contains(new Integer(i))); |
235 |
} |
236 |
} |
237 |
|
238 |
public void testClear(){ |
239 |
ConcurrentLinkedQueue q = fullQueue(N); |
240 |
q.clear(); |
241 |
assertTrue(q.isEmpty()); |
242 |
assertEquals(0, q.size()); |
243 |
q.add(new Integer(1)); |
244 |
assertFalse(q.isEmpty()); |
245 |
q.clear(); |
246 |
assertTrue(q.isEmpty()); |
247 |
} |
248 |
|
249 |
public void testContainsAll(){ |
250 |
ConcurrentLinkedQueue q = fullQueue(N); |
251 |
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue(); |
252 |
for (int i = 0; i < N; ++i) { |
253 |
assertTrue(q.containsAll(p)); |
254 |
assertFalse(p.containsAll(q)); |
255 |
p.add(new Integer(i)); |
256 |
} |
257 |
assertTrue(p.containsAll(q)); |
258 |
} |
259 |
|
260 |
public void testRetainAll(){ |
261 |
ConcurrentLinkedQueue q = fullQueue(N); |
262 |
ConcurrentLinkedQueue p = fullQueue(N); |
263 |
for (int i = 0; i < N; ++i) { |
264 |
boolean changed = q.retainAll(p); |
265 |
if (i == 0) |
266 |
assertFalse(changed); |
267 |
else |
268 |
assertTrue(changed); |
269 |
|
270 |
assertTrue(q.containsAll(p)); |
271 |
assertEquals(N-i, q.size()); |
272 |
p.remove(); |
273 |
} |
274 |
} |
275 |
|
276 |
public void testRemoveAll(){ |
277 |
for (int i = 1; i < N; ++i) { |
278 |
ConcurrentLinkedQueue q = fullQueue(N); |
279 |
ConcurrentLinkedQueue p = fullQueue(i); |
280 |
assertTrue(q.removeAll(p)); |
281 |
assertEquals(N-i, q.size()); |
282 |
for (int j = 0; j < i; ++j) { |
283 |
Integer I = (Integer)(p.remove()); |
284 |
assertFalse(q.contains(I)); |
285 |
} |
286 |
} |
287 |
} |
288 |
|
289 |
public void testToArray(){ |
290 |
ConcurrentLinkedQueue q = fullQueue(N); |
291 |
Object[] o = q.toArray(); |
292 |
Arrays.sort(o); |
293 |
for(int i = 0; i < o.length; i++) |
294 |
assertEquals(o[i], q.poll()); |
295 |
} |
296 |
|
297 |
public void testToArray2(){ |
298 |
ConcurrentLinkedQueue q = fullQueue(N); |
299 |
Integer[] ints = new Integer[N]; |
300 |
ints = (Integer[])q.toArray(ints); |
301 |
Arrays.sort(ints); |
302 |
for(int i = 0; i < ints.length; i++) |
303 |
assertEquals(ints[i], q.poll()); |
304 |
} |
305 |
|
306 |
public void testIterator(){ |
307 |
ConcurrentLinkedQueue q = fullQueue(N); |
308 |
int i = 0; |
309 |
Iterator it = q.iterator(); |
310 |
while(it.hasNext()) { |
311 |
assertTrue(q.contains(it.next())); |
312 |
++i; |
313 |
} |
314 |
assertEquals(i, N); |
315 |
} |
316 |
|
317 |
public void testIteratorOrdering() { |
318 |
|
319 |
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
320 |
|
321 |
q.add(new Integer(1)); |
322 |
q.add(new Integer(2)); |
323 |
q.add(new Integer(3)); |
324 |
|
325 |
int k = 0; |
326 |
for (Iterator it = q.iterator(); it.hasNext();) { |
327 |
int i = ((Integer)(it.next())).intValue(); |
328 |
assertEquals("items should come out in order", ++k, i); |
329 |
} |
330 |
|
331 |
assertEquals("should go through 3 elements", 3, k); |
332 |
} |
333 |
|
334 |
public void testWeaklyConsistentIteration () { |
335 |
|
336 |
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
337 |
|
338 |
q.add(new Integer(1)); |
339 |
q.add(new Integer(2)); |
340 |
q.add(new Integer(3)); |
341 |
|
342 |
try { |
343 |
for (Iterator it = q.iterator(); it.hasNext();) { |
344 |
q.remove(); |
345 |
it.next(); |
346 |
} |
347 |
} |
348 |
catch (ConcurrentModificationException e) { |
349 |
fail("weakly consistent iterator; should not get CME"); |
350 |
} |
351 |
|
352 |
assertEquals("queue should be empty again", 0, q.size()); |
353 |
} |
354 |
|
355 |
public void testIteratorRemove () { |
356 |
|
357 |
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
358 |
|
359 |
q.add(new Integer(1)); |
360 |
q.add(new Integer(2)); |
361 |
q.add(new Integer(3)); |
362 |
|
363 |
Iterator it = q.iterator(); |
364 |
it.next(); |
365 |
it.remove(); |
366 |
|
367 |
it = q.iterator(); |
368 |
assertEquals(it.next(), new Integer(2)); |
369 |
assertEquals(it.next(), new Integer(3)); |
370 |
assertFalse(it.hasNext()); |
371 |
} |
372 |
|
373 |
|
374 |
public void testToString(){ |
375 |
ConcurrentLinkedQueue q = fullQueue(N); |
376 |
String s = q.toString(); |
377 |
for (int i = 0; i < N; ++i) { |
378 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
379 |
} |
380 |
} |
381 |
|
382 |
} |