ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedQueueTest.java
Revision: 1.4
Committed: Sat Sep 20 18:20:07 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +133 -38 lines
Log Message:
Documentation scaffolding

File Contents

# Content
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 import java.io.*;
12
13 public class ConcurrentLinkedQueueTest extends JSR166TestCase {
14
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18
19 public static Test suite() {
20 return new TestSuite(ConcurrentLinkedQueueTest.class);
21 }
22
23 /**
24 * Create a queue of given size containing consecutive
25 * Integers 0 ... n.
26 */
27 private ConcurrentLinkedQueue populatedQueue(int n) {
28 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
29 assertTrue(q.isEmpty());
30 for(int i = 0; i < n; ++i)
31 assertTrue(q.offer(new Integer(i)));
32 assertFalse(q.isEmpty());
33 assertEquals(n, q.size());
34 return q;
35 }
36
37 /**
38 *
39 */
40 public void testConstructor1() {
41 assertEquals(0, new ConcurrentLinkedQueue().size());
42 }
43
44 /**
45 *
46 */
47 public void testConstructor3() {
48 try {
49 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
50 shouldThrow();
51 }
52 catch (NullPointerException success) {}
53 }
54
55 /**
56 *
57 */
58 public void testConstructor4() {
59 try {
60 Integer[] ints = new Integer[SIZE];
61 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
62 shouldThrow();
63 }
64 catch (NullPointerException success) {}
65 }
66
67 /**
68 *
69 */
70 public void testConstructor5() {
71 try {
72 Integer[] ints = new Integer[SIZE];
73 for (int i = 0; i < SIZE-1; ++i)
74 ints[i] = new Integer(i);
75 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
76 shouldThrow();
77 }
78 catch (NullPointerException success) {}
79 }
80
81 /**
82 *
83 */
84 public void testConstructor6() {
85 try {
86 Integer[] ints = new Integer[SIZE];
87 for (int i = 0; i < SIZE; ++i)
88 ints[i] = new Integer(i);
89 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
90 for (int i = 0; i < SIZE; ++i)
91 assertEquals(ints[i], q.poll());
92 }
93 finally {}
94 }
95
96 /**
97 *
98 */
99 public void testEmpty() {
100 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
101 assertTrue(q.isEmpty());
102 q.add(one);
103 assertFalse(q.isEmpty());
104 q.add(two);
105 q.remove();
106 q.remove();
107 assertTrue(q.isEmpty());
108 }
109
110 /**
111 *
112 */
113 public void testSize() {
114 ConcurrentLinkedQueue q = populatedQueue(SIZE);
115 for (int i = 0; i < SIZE; ++i) {
116 assertEquals(SIZE-i, q.size());
117 q.remove();
118 }
119 for (int i = 0; i < SIZE; ++i) {
120 assertEquals(i, q.size());
121 q.add(new Integer(i));
122 }
123 }
124
125 /**
126 *
127 */
128 public void testOfferNull() {
129 try {
130 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
131 q.offer(null);
132 shouldThrow();
133 } catch (NullPointerException success) { }
134 }
135
136 /**
137 *
138 */
139 public void testOffer() {
140 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
141 assertTrue(q.offer(zero));
142 assertTrue(q.offer(one));
143 }
144
145 /**
146 *
147 */
148 public void testAdd() {
149 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
150 for (int i = 0; i < SIZE; ++i) {
151 assertEquals(i, q.size());
152 assertTrue(q.add(new Integer(i)));
153 }
154 }
155
156 /**
157 *
158 */
159 public void testAddAll1() {
160 try {
161 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
162 q.addAll(null);
163 shouldThrow();
164 }
165 catch (NullPointerException success) {}
166 }
167 /**
168 *
169 */
170 public void testAddAll2() {
171 try {
172 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
173 Integer[] ints = new Integer[SIZE];
174 q.addAll(Arrays.asList(ints));
175 shouldThrow();
176 }
177 catch (NullPointerException success) {}
178 }
179 /**
180 *
181 */
182 public void testAddAll3() {
183 try {
184 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
185 Integer[] ints = new Integer[SIZE];
186 for (int i = 0; i < SIZE-1; ++i)
187 ints[i] = new Integer(i);
188 q.addAll(Arrays.asList(ints));
189 shouldThrow();
190 }
191 catch (NullPointerException success) {}
192 }
193
194 /**
195 *
196 */
197 public void testAddAll5() {
198 try {
199 Integer[] empty = new Integer[0];
200 Integer[] ints = new Integer[SIZE];
201 for (int i = 0; i < SIZE; ++i)
202 ints[i] = new Integer(i);
203 ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
204 assertFalse(q.addAll(Arrays.asList(empty)));
205 assertTrue(q.addAll(Arrays.asList(ints)));
206 for (int i = 0; i < SIZE; ++i)
207 assertEquals(ints[i], q.poll());
208 }
209 finally {}
210 }
211
212 /**
213 *
214 */
215 public void testPoll() {
216 ConcurrentLinkedQueue q = populatedQueue(SIZE);
217 for (int i = 0; i < SIZE; ++i) {
218 assertEquals(i, ((Integer)q.poll()).intValue());
219 }
220 assertNull(q.poll());
221 }
222
223 /**
224 *
225 */
226 public void testPeek() {
227 ConcurrentLinkedQueue q = populatedQueue(SIZE);
228 for (int i = 0; i < SIZE; ++i) {
229 assertEquals(i, ((Integer)q.peek()).intValue());
230 q.poll();
231 assertTrue(q.peek() == null ||
232 i != ((Integer)q.peek()).intValue());
233 }
234 assertNull(q.peek());
235 }
236
237 /**
238 *
239 */
240 public void testElement() {
241 ConcurrentLinkedQueue q = populatedQueue(SIZE);
242 for (int i = 0; i < SIZE; ++i) {
243 assertEquals(i, ((Integer)q.element()).intValue());
244 q.poll();
245 }
246 try {
247 q.element();
248 shouldThrow();
249 }
250 catch (NoSuchElementException success) {}
251 }
252
253 /**
254 *
255 */
256 public void testRemove() {
257 ConcurrentLinkedQueue q = populatedQueue(SIZE);
258 for (int i = 0; i < SIZE; ++i) {
259 assertEquals(i, ((Integer)q.remove()).intValue());
260 }
261 try {
262 q.remove();
263 shouldThrow();
264 } catch (NoSuchElementException success){
265 }
266 }
267
268 /**
269 *
270 */
271 public void testRemoveElement() {
272 ConcurrentLinkedQueue q = populatedQueue(SIZE);
273 for (int i = 1; i < SIZE; i+=2) {
274 assertTrue(q.remove(new Integer(i)));
275 }
276 for (int i = 0; i < SIZE; i+=2) {
277 assertTrue(q.remove(new Integer(i)));
278 assertFalse(q.remove(new Integer(i+1)));
279 }
280 assertTrue(q.isEmpty());
281 }
282
283 /**
284 *
285 */
286 public void testContains() {
287 ConcurrentLinkedQueue q = populatedQueue(SIZE);
288 for (int i = 0; i < SIZE; ++i) {
289 assertTrue(q.contains(new Integer(i)));
290 q.poll();
291 assertFalse(q.contains(new Integer(i)));
292 }
293 }
294
295 /**
296 *
297 */
298 public void testClear() {
299 ConcurrentLinkedQueue q = populatedQueue(SIZE);
300 q.clear();
301 assertTrue(q.isEmpty());
302 assertEquals(0, q.size());
303 q.add(one);
304 assertFalse(q.isEmpty());
305 q.clear();
306 assertTrue(q.isEmpty());
307 }
308
309 /**
310 *
311 */
312 public void testContainsAll() {
313 ConcurrentLinkedQueue q = populatedQueue(SIZE);
314 ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
315 for (int i = 0; i < SIZE; ++i) {
316 assertTrue(q.containsAll(p));
317 assertFalse(p.containsAll(q));
318 p.add(new Integer(i));
319 }
320 assertTrue(p.containsAll(q));
321 }
322
323 /**
324 *
325 */
326 public void testRetainAll() {
327 ConcurrentLinkedQueue q = populatedQueue(SIZE);
328 ConcurrentLinkedQueue p = populatedQueue(SIZE);
329 for (int i = 0; i < SIZE; ++i) {
330 boolean changed = q.retainAll(p);
331 if (i == 0)
332 assertFalse(changed);
333 else
334 assertTrue(changed);
335
336 assertTrue(q.containsAll(p));
337 assertEquals(SIZE-i, q.size());
338 p.remove();
339 }
340 }
341
342 /**
343 *
344 */
345 public void testRemoveAll() {
346 for (int i = 1; i < SIZE; ++i) {
347 ConcurrentLinkedQueue q = populatedQueue(SIZE);
348 ConcurrentLinkedQueue p = populatedQueue(i);
349 assertTrue(q.removeAll(p));
350 assertEquals(SIZE-i, q.size());
351 for (int j = 0; j < i; ++j) {
352 Integer I = (Integer)(p.remove());
353 assertFalse(q.contains(I));
354 }
355 }
356 }
357
358 /**
359 *
360 */
361 public void testToArray() {
362 ConcurrentLinkedQueue q = populatedQueue(SIZE);
363 Object[] o = q.toArray();
364 Arrays.sort(o);
365 for(int i = 0; i < o.length; i++)
366 assertEquals(o[i], q.poll());
367 }
368
369 /**
370 *
371 */
372 public void testToArray2() {
373 ConcurrentLinkedQueue q = populatedQueue(SIZE);
374 Integer[] ints = new Integer[SIZE];
375 ints = (Integer[])q.toArray(ints);
376 Arrays.sort(ints);
377 for(int i = 0; i < ints.length; i++)
378 assertEquals(ints[i], q.poll());
379 }
380
381 /**
382 *
383 */
384 public void testIterator() {
385 ConcurrentLinkedQueue q = populatedQueue(SIZE);
386 int i = 0;
387 Iterator it = q.iterator();
388 while(it.hasNext()) {
389 assertTrue(q.contains(it.next()));
390 ++i;
391 }
392 assertEquals(i, SIZE);
393 }
394
395 /**
396 *
397 */
398 public void testIteratorOrdering() {
399 final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
400 q.add(one);
401 q.add(two);
402 q.add(three);
403
404 int k = 0;
405 for (Iterator it = q.iterator(); it.hasNext();) {
406 int i = ((Integer)(it.next())).intValue();
407 assertEquals(++k, i);
408 }
409
410 assertEquals(3, k);
411 }
412
413 /**
414 *
415 */
416 public void testWeaklyConsistentIteration () {
417 final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
418 q.add(one);
419 q.add(two);
420 q.add(three);
421
422 try {
423 for (Iterator it = q.iterator(); it.hasNext();) {
424 q.remove();
425 it.next();
426 }
427 }
428 catch (ConcurrentModificationException e) {
429 shouldThrow();
430 }
431
432 assertEquals("queue should be empty again", 0, q.size());
433 }
434
435 /**
436 *
437 */
438 public void testIteratorRemove () {
439 final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
440 q.add(one);
441 q.add(two);
442 q.add(three);
443 Iterator it = q.iterator();
444 it.next();
445 it.remove();
446 it = q.iterator();
447 assertEquals(it.next(), two);
448 assertEquals(it.next(), three);
449 assertFalse(it.hasNext());
450 }
451
452
453 /**
454 *
455 */
456 public void testToString() {
457 ConcurrentLinkedQueue q = populatedQueue(SIZE);
458 String s = q.toString();
459 for (int i = 0; i < SIZE; ++i) {
460 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
461 }
462 }
463
464 /**
465 *
466 */
467 public void testSerialization() {
468 ConcurrentLinkedQueue q = populatedQueue(SIZE);
469 try {
470 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
471 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
472 out.writeObject(q);
473 out.close();
474
475 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
476 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
477 ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
478 assertEquals(q.size(), r.size());
479 while (!q.isEmpty())
480 assertEquals(q.remove(), r.remove());
481 } catch(Exception e){
482 unexpectedException();
483 }
484 }
485
486 }