10 |
|
import java.util.concurrent.*; |
11 |
|
import java.io.*; |
12 |
|
|
13 |
< |
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; |
13 |
> |
public class ConcurrentLinkedQueueTest extends JSR166TestCase { |
14 |
|
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
24 |
|
* Create a queue of given size containing consecutive |
25 |
|
* Integers 0 ... n. |
26 |
|
*/ |
27 |
< |
private ConcurrentLinkedQueue fullQueue(int n) { |
27 |
> |
private ConcurrentLinkedQueue populatedQueue(int n) { |
28 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
29 |
|
assertTrue(q.isEmpty()); |
30 |
|
for(int i = 0; i < n; ++i) |
48 |
|
|
49 |
|
public void testConstructor4(){ |
50 |
|
try { |
51 |
< |
Integer[] ints = new Integer[N]; |
51 |
> |
Integer[] ints = new Integer[SIZE]; |
52 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
53 |
|
fail("Cannot make with null elements"); |
54 |
|
} |
57 |
|
|
58 |
|
public void testConstructor5(){ |
59 |
|
try { |
60 |
< |
Integer[] ints = new Integer[N]; |
61 |
< |
for (int i = 0; i < N-1; ++i) |
60 |
> |
Integer[] ints = new Integer[SIZE]; |
61 |
> |
for (int i = 0; i < SIZE-1; ++i) |
62 |
|
ints[i] = new Integer(i); |
63 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
64 |
|
fail("Cannot make with null elements"); |
68 |
|
|
69 |
|
public void testConstructor6(){ |
70 |
|
try { |
71 |
< |
Integer[] ints = new Integer[N]; |
72 |
< |
for (int i = 0; i < N; ++i) |
71 |
> |
Integer[] ints = new Integer[SIZE]; |
72 |
> |
for (int i = 0; i < SIZE; ++i) |
73 |
|
ints[i] = new Integer(i); |
74 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints)); |
75 |
< |
for (int i = 0; i < N; ++i) |
75 |
> |
for (int i = 0; i < SIZE; ++i) |
76 |
|
assertEquals(ints[i], q.poll()); |
77 |
|
} |
78 |
|
finally {} |
81 |
|
public void testEmpty() { |
82 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
83 |
|
assertTrue(q.isEmpty()); |
84 |
< |
q.add(new Integer(1)); |
84 |
> |
q.add(one); |
85 |
|
assertFalse(q.isEmpty()); |
86 |
< |
q.add(new Integer(2)); |
86 |
> |
q.add(two); |
87 |
|
q.remove(); |
88 |
|
q.remove(); |
89 |
|
assertTrue(q.isEmpty()); |
90 |
|
} |
91 |
|
|
92 |
|
public void testSize() { |
93 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
94 |
< |
for (int i = 0; i < N; ++i) { |
95 |
< |
assertEquals(N-i, q.size()); |
93 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
94 |
> |
for (int i = 0; i < SIZE; ++i) { |
95 |
> |
assertEquals(SIZE-i, q.size()); |
96 |
|
q.remove(); |
97 |
|
} |
98 |
< |
for (int i = 0; i < N; ++i) { |
98 |
> |
for (int i = 0; i < SIZE; ++i) { |
99 |
|
assertEquals(i, q.size()); |
100 |
|
q.add(new Integer(i)); |
101 |
|
} |
111 |
|
|
112 |
|
public void testOffer() { |
113 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
114 |
< |
assertTrue(q.offer(new Integer(0))); |
115 |
< |
assertTrue(q.offer(new Integer(1))); |
114 |
> |
assertTrue(q.offer(zero)); |
115 |
> |
assertTrue(q.offer(one)); |
116 |
|
} |
117 |
|
|
118 |
|
public void testAdd(){ |
119 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
120 |
< |
for (int i = 0; i < N; ++i) { |
120 |
> |
for (int i = 0; i < SIZE; ++i) { |
121 |
|
assertEquals(i, q.size()); |
122 |
|
assertTrue(q.add(new Integer(i))); |
123 |
|
} |
134 |
|
public void testAddAll2(){ |
135 |
|
try { |
136 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
137 |
< |
Integer[] ints = new Integer[N]; |
137 |
> |
Integer[] ints = new Integer[SIZE]; |
138 |
|
q.addAll(Arrays.asList(ints)); |
139 |
|
fail("Cannot add null elements"); |
140 |
|
} |
143 |
|
public void testAddAll3(){ |
144 |
|
try { |
145 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
146 |
< |
Integer[] ints = new Integer[N]; |
147 |
< |
for (int i = 0; i < N-1; ++i) |
146 |
> |
Integer[] ints = new Integer[SIZE]; |
147 |
> |
for (int i = 0; i < SIZE-1; ++i) |
148 |
|
ints[i] = new Integer(i); |
149 |
|
q.addAll(Arrays.asList(ints)); |
150 |
|
fail("Cannot add null elements"); |
155 |
|
public void testAddAll5(){ |
156 |
|
try { |
157 |
|
Integer[] empty = new Integer[0]; |
158 |
< |
Integer[] ints = new Integer[N]; |
159 |
< |
for (int i = 0; i < N; ++i) |
158 |
> |
Integer[] ints = new Integer[SIZE]; |
159 |
> |
for (int i = 0; i < SIZE; ++i) |
160 |
|
ints[i] = new Integer(i); |
161 |
|
ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
162 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
163 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
164 |
< |
for (int i = 0; i < N; ++i) |
164 |
> |
for (int i = 0; i < SIZE; ++i) |
165 |
|
assertEquals(ints[i], q.poll()); |
166 |
|
} |
167 |
|
finally {} |
168 |
|
} |
169 |
|
|
170 |
|
public void testPoll(){ |
171 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
172 |
< |
for (int i = 0; i < N; ++i) { |
171 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
172 |
> |
for (int i = 0; i < SIZE; ++i) { |
173 |
|
assertEquals(i, ((Integer)q.poll()).intValue()); |
174 |
|
} |
175 |
|
assertNull(q.poll()); |
176 |
|
} |
177 |
|
|
178 |
|
public void testPeek(){ |
179 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
180 |
< |
for (int i = 0; i < N; ++i) { |
179 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
180 |
> |
for (int i = 0; i < SIZE; ++i) { |
181 |
|
assertEquals(i, ((Integer)q.peek()).intValue()); |
182 |
|
q.poll(); |
183 |
|
assertTrue(q.peek() == null || |
187 |
|
} |
188 |
|
|
189 |
|
public void testElement(){ |
190 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
191 |
< |
for (int i = 0; i < N; ++i) { |
190 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
191 |
> |
for (int i = 0; i < SIZE; ++i) { |
192 |
|
assertEquals(i, ((Integer)q.element()).intValue()); |
193 |
|
q.poll(); |
194 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
public void testRemove(){ |
203 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
204 |
< |
for (int i = 0; i < N; ++i) { |
203 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
204 |
> |
for (int i = 0; i < SIZE; ++i) { |
205 |
|
assertEquals(i, ((Integer)q.remove()).intValue()); |
206 |
|
} |
207 |
|
try { |
212 |
|
} |
213 |
|
|
214 |
|
public void testRemoveElement(){ |
215 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
216 |
< |
for (int i = 1; i < N; i+=2) { |
215 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
216 |
> |
for (int i = 1; i < SIZE; i+=2) { |
217 |
|
assertTrue(q.remove(new Integer(i))); |
218 |
|
} |
219 |
< |
for (int i = 0; i < N; i+=2) { |
219 |
> |
for (int i = 0; i < SIZE; i+=2) { |
220 |
|
assertTrue(q.remove(new Integer(i))); |
221 |
|
assertFalse(q.remove(new Integer(i+1))); |
222 |
|
} |
224 |
|
} |
225 |
|
|
226 |
|
public void testContains(){ |
227 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
228 |
< |
for (int i = 0; i < N; ++i) { |
227 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
228 |
> |
for (int i = 0; i < SIZE; ++i) { |
229 |
|
assertTrue(q.contains(new Integer(i))); |
230 |
|
q.poll(); |
231 |
|
assertFalse(q.contains(new Integer(i))); |
233 |
|
} |
234 |
|
|
235 |
|
public void testClear(){ |
236 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
236 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
237 |
|
q.clear(); |
238 |
|
assertTrue(q.isEmpty()); |
239 |
|
assertEquals(0, q.size()); |
240 |
< |
q.add(new Integer(1)); |
240 |
> |
q.add(one); |
241 |
|
assertFalse(q.isEmpty()); |
242 |
|
q.clear(); |
243 |
|
assertTrue(q.isEmpty()); |
244 |
|
} |
245 |
|
|
246 |
|
public void testContainsAll(){ |
247 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
247 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
248 |
|
ConcurrentLinkedQueue p = new ConcurrentLinkedQueue(); |
249 |
< |
for (int i = 0; i < N; ++i) { |
249 |
> |
for (int i = 0; i < SIZE; ++i) { |
250 |
|
assertTrue(q.containsAll(p)); |
251 |
|
assertFalse(p.containsAll(q)); |
252 |
|
p.add(new Integer(i)); |
255 |
|
} |
256 |
|
|
257 |
|
public void testRetainAll(){ |
258 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
259 |
< |
ConcurrentLinkedQueue p = fullQueue(N); |
260 |
< |
for (int i = 0; i < N; ++i) { |
258 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
259 |
> |
ConcurrentLinkedQueue p = populatedQueue(SIZE); |
260 |
> |
for (int i = 0; i < SIZE; ++i) { |
261 |
|
boolean changed = q.retainAll(p); |
262 |
|
if (i == 0) |
263 |
|
assertFalse(changed); |
265 |
|
assertTrue(changed); |
266 |
|
|
267 |
|
assertTrue(q.containsAll(p)); |
268 |
< |
assertEquals(N-i, q.size()); |
268 |
> |
assertEquals(SIZE-i, q.size()); |
269 |
|
p.remove(); |
270 |
|
} |
271 |
|
} |
272 |
|
|
273 |
|
public void testRemoveAll(){ |
274 |
< |
for (int i = 1; i < N; ++i) { |
275 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
276 |
< |
ConcurrentLinkedQueue p = fullQueue(i); |
274 |
> |
for (int i = 1; i < SIZE; ++i) { |
275 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
276 |
> |
ConcurrentLinkedQueue p = populatedQueue(i); |
277 |
|
assertTrue(q.removeAll(p)); |
278 |
< |
assertEquals(N-i, q.size()); |
278 |
> |
assertEquals(SIZE-i, q.size()); |
279 |
|
for (int j = 0; j < i; ++j) { |
280 |
|
Integer I = (Integer)(p.remove()); |
281 |
|
assertFalse(q.contains(I)); |
284 |
|
} |
285 |
|
|
286 |
|
public void testToArray(){ |
287 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
287 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
288 |
|
Object[] o = q.toArray(); |
289 |
|
Arrays.sort(o); |
290 |
|
for(int i = 0; i < o.length; i++) |
292 |
|
} |
293 |
|
|
294 |
|
public void testToArray2(){ |
295 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
296 |
< |
Integer[] ints = new Integer[N]; |
295 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
296 |
> |
Integer[] ints = new Integer[SIZE]; |
297 |
|
ints = (Integer[])q.toArray(ints); |
298 |
|
Arrays.sort(ints); |
299 |
|
for(int i = 0; i < ints.length; i++) |
301 |
|
} |
302 |
|
|
303 |
|
public void testIterator(){ |
304 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
304 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
305 |
|
int i = 0; |
306 |
|
Iterator it = q.iterator(); |
307 |
|
while(it.hasNext()) { |
308 |
|
assertTrue(q.contains(it.next())); |
309 |
|
++i; |
310 |
|
} |
311 |
< |
assertEquals(i, N); |
311 |
> |
assertEquals(i, SIZE); |
312 |
|
} |
313 |
|
|
314 |
|
public void testIteratorOrdering() { |
319 |
– |
|
315 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
316 |
< |
|
317 |
< |
q.add(new Integer(1)); |
318 |
< |
q.add(new Integer(2)); |
324 |
< |
q.add(new Integer(3)); |
316 |
> |
q.add(one); |
317 |
> |
q.add(two); |
318 |
> |
q.add(three); |
319 |
|
|
320 |
|
int k = 0; |
321 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
327 |
|
} |
328 |
|
|
329 |
|
public void testWeaklyConsistentIteration () { |
336 |
– |
|
330 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
331 |
< |
|
332 |
< |
q.add(new Integer(1)); |
333 |
< |
q.add(new Integer(2)); |
341 |
< |
q.add(new Integer(3)); |
331 |
> |
q.add(one); |
332 |
> |
q.add(two); |
333 |
> |
q.add(three); |
334 |
|
|
335 |
|
try { |
336 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
346 |
|
} |
347 |
|
|
348 |
|
public void testIteratorRemove () { |
357 |
– |
|
349 |
|
final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(); |
350 |
< |
|
351 |
< |
q.add(new Integer(1)); |
352 |
< |
q.add(new Integer(2)); |
362 |
< |
q.add(new Integer(3)); |
363 |
< |
|
350 |
> |
q.add(one); |
351 |
> |
q.add(two); |
352 |
> |
q.add(three); |
353 |
|
Iterator it = q.iterator(); |
354 |
|
it.next(); |
355 |
|
it.remove(); |
367 |
– |
|
356 |
|
it = q.iterator(); |
357 |
< |
assertEquals(it.next(), new Integer(2)); |
358 |
< |
assertEquals(it.next(), new Integer(3)); |
357 |
> |
assertEquals(it.next(), two); |
358 |
> |
assertEquals(it.next(), three); |
359 |
|
assertFalse(it.hasNext()); |
360 |
|
} |
361 |
|
|
362 |
|
|
363 |
|
public void testToString(){ |
364 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
364 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
365 |
|
String s = q.toString(); |
366 |
< |
for (int i = 0; i < N; ++i) { |
366 |
> |
for (int i = 0; i < SIZE; ++i) { |
367 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
368 |
|
} |
369 |
|
} |
370 |
|
|
371 |
|
public void testSerialization() { |
372 |
< |
ConcurrentLinkedQueue q = fullQueue(N); |
372 |
> |
ConcurrentLinkedQueue q = populatedQueue(SIZE); |
373 |
|
try { |
374 |
|
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
375 |
|
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |