ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedQueueTest.java
Revision: 1.5
Committed: Sun Oct 5 23:00:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_NOV3_FREEZE, JSR166_DEC9_PRE_ES_SUBMIT, JSR166_DEC9_POST_ES_SUBMIT
Changes since 1.4: +80 -32 lines
Log Message:
Added tests and documentation

File Contents

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