ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedQueueTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentLinkedQueueTest.java (file contents):
Revision 1.3 by dl, Sun Sep 14 20:42:40 2003 UTC vs.
Revision 1.14 by jsr166, Sat Nov 21 10:29:50 2009 UTC

# Line 1 | Line 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.
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.*;
# Line 13 | Line 14 | import java.io.*;
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      /**
# Line 27 | Line 28 | public class ConcurrentLinkedQueueTest e
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 <        }
46 <        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[SIZE];
61              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
62 <            fail("Cannot make with null elements");
63 <        }
55 <        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[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");
75 >            shouldThrow();
76          }
77          catch (NullPointerException success) {}
78      }
79  
80 <    public void testConstructor6(){
81 <        try {
82 <            Integer[] ints = new Integer[SIZE];
83 <            for (int i = 0; i < SIZE; ++i)
84 <                ints[i] = new Integer(i);
85 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
86 <            for (int i = 0; i < SIZE; ++i)
87 <                assertEquals(ints[i], q.poll());
88 <        }
89 <        finally {}
80 >    /**
81 >     * Queue contains all elements of collection used to initialize
82 >     */
83 >    public void testConstructor6() {
84 >        Integer[] ints = new Integer[SIZE];
85 >        for (int i = 0; i < SIZE; ++i)
86 >            ints[i] = new Integer(i);
87 >        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
88 >        for (int i = 0; i < SIZE; ++i)
89 >            assertEquals(ints[i], q.poll());
90      }
91  
92 +    /**
93 +     * isEmpty is true before add, false after
94 +     */
95      public void testEmpty() {
96          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
97          assertTrue(q.isEmpty());
# Line 89 | Line 103 | public class ConcurrentLinkedQueueTest e
103          assertTrue(q.isEmpty());
104      }
105  
106 +    /**
107 +     * size changes when elements added and removed
108 +     */
109      public void testSize() {
110          ConcurrentLinkedQueue q = populatedQueue(SIZE);
111          for (int i = 0; i < SIZE; ++i) {
# Line 101 | Line 118 | public class ConcurrentLinkedQueueTest e
118          }
119      }
120  
121 <    public void testOfferNull(){
122 <        try {
121 >    /**
122 >     * offer(null) throws NPE
123 >     */
124 >    public void testOfferNull() {
125 >        try {
126              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
127              q.offer(null);
128 <            fail("should throw NPE");
129 <        } catch (NullPointerException success) { }  
128 >            shouldThrow();
129 >        } catch (NullPointerException success) {}
130 >    }
131 >
132 >    /**
133 >     * add(null) throws NPE
134 >     */
135 >    public void testAddNull() {
136 >        try {
137 >            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
138 >            q.add(null);
139 >            shouldThrow();
140 >        } catch (NullPointerException success) {}
141      }
142  
143 +
144 +    /**
145 +     * Offer returns true
146 +     */
147      public void testOffer() {
148          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
149          assertTrue(q.offer(zero));
150          assertTrue(q.offer(one));
151      }
152  
153 <    public void testAdd(){
153 >    /**
154 >     * add returns true
155 >     */
156 >    public void testAdd() {
157          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
158          for (int i = 0; i < SIZE; ++i) {
159              assertEquals(i, q.size());
# Line 123 | Line 161 | public class ConcurrentLinkedQueueTest e
161          }
162      }
163  
164 <    public void testAddAll1(){
164 >    /**
165 >     * addAll(null) throws NPE
166 >     */
167 >    public void testAddAll1() {
168          try {
169              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
170              q.addAll(null);
171 <            fail("Cannot add null collection");
172 <        }
173 <        catch (NullPointerException success) {}
171 >            shouldThrow();
172 >        } catch (NullPointerException success) {}
173 >    }
174 >
175 >    /**
176 >     * addAll(this) throws IAE
177 >     */
178 >    public void testAddAllSelf() {
179 >        try {
180 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
181 >            q.addAll(q);
182 >            shouldThrow();
183 >        } catch (IllegalArgumentException success) {}
184      }
185 <    public void testAddAll2(){
185 >
186 >    /**
187 >     * addAll of a collection with null elements throws NPE
188 >     */
189 >    public void testAddAll2() {
190          try {
191              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
192              Integer[] ints = new Integer[SIZE];
193              q.addAll(Arrays.asList(ints));
194 <            fail("Cannot add null elements");
195 <        }
141 <        catch (NullPointerException success) {}
194 >            shouldThrow();
195 >        } catch (NullPointerException success) {}
196      }
197 <    public void testAddAll3(){
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[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 <        }
152 <        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[SIZE];
216 <            for (int i = 0; i < SIZE; ++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 < SIZE; ++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(){
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, ((Integer)q.poll()).intValue());
234          }
235 <        assertNull(q.poll());
235 >        assertNull(q.poll());
236      }
237  
238 <    public void testPeek(){
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, ((Integer)q.peek()).intValue());
# Line 183 | Line 246 | public class ConcurrentLinkedQueueTest e
246              assertTrue(q.peek() == null ||
247                         i != ((Integer)q.peek()).intValue());
248          }
249 <        assertNull(q.peek());
249 >        assertNull(q.peek());
250      }
251  
252 <    public void testElement(){
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, ((Integer)q.element()).intValue());
# Line 194 | Line 260 | public class ConcurrentLinkedQueueTest e
260          }
261          try {
262              q.element();
263 <            fail("no such element");
264 <        }
199 <        catch (NoSuchElementException success) {}
263 >            shouldThrow();
264 >        } catch (NoSuchElementException success) {}
265      }
266  
267 <    public void testRemove(){
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, ((Integer)q.remove()).intValue());
274          }
275          try {
276              q.remove();
277 <            fail("remove should throw");
278 <        } catch (NoSuchElementException success){
211 <        }  
277 >            shouldThrow();
278 >        } catch (NoSuchElementException success) {}
279      }
280  
281 <    public void testRemoveElement(){
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)));
# Line 222 | Line 292 | public class ConcurrentLinkedQueueTest e
292          }
293          assertTrue(q.isEmpty());
294      }
295 <        
296 <    public void testContains(){
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)));
# Line 232 | Line 305 | public class ConcurrentLinkedQueueTest e
305          }
306      }
307  
308 <    public void testClear(){
308 >    /**
309 >     * clear removes all elements
310 >     */
311 >    public void testClear() {
312          ConcurrentLinkedQueue q = populatedQueue(SIZE);
313          q.clear();
314          assertTrue(q.isEmpty());
# Line 243 | Line 319 | public class ConcurrentLinkedQueueTest e
319          assertTrue(q.isEmpty());
320      }
321  
322 <    public void testContainsAll(){
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 < SIZE; ++i) {
# Line 254 | Line 333 | public class ConcurrentLinkedQueueTest e
333          assertTrue(p.containsAll(q));
334      }
335  
336 <    public void testRetainAll(){
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) {
# Line 270 | Line 352 | public class ConcurrentLinkedQueueTest e
352          }
353      }
354  
355 <    public void testRemoveAll(){
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);
# Line 283 | Line 368 | public class ConcurrentLinkedQueueTest e
368          }
369      }
370  
371 <    public void testToArray(){
371 >    /**
372 >     * toArray contains all elements
373 >     */
374 >    public void testToArray() {
375          ConcurrentLinkedQueue q = populatedQueue(SIZE);
376 <        Object[] o = q.toArray();
376 >        Object[] o = q.toArray();
377          Arrays.sort(o);
378 <        for(int i = 0; i < o.length; i++)
379 <            assertEquals(o[i], q.poll());
378 >        for (int i = 0; i < o.length; i++)
379 >            assertEquals(o[i], q.poll());
380      }
381  
382 <    public void testToArray2(){
382 >    /**
383 >     *  toArray(a) contains all elements
384 >     */
385 >    public void testToArray2() {
386          ConcurrentLinkedQueue q = populatedQueue(SIZE);
387 <        Integer[] ints = new Integer[SIZE];
388 <        ints = (Integer[])q.toArray(ints);
387 >        Integer[] ints = new Integer[SIZE];
388 >        ints = (Integer[])q.toArray(ints);
389          Arrays.sort(ints);
390 <        for(int i = 0; i < ints.length; i++)
390 >        for (int i = 0; i < ints.length; i++)
391              assertEquals(ints[i], q.poll());
392      }
393 <    
394 <    public void testIterator(){
393 >
394 >    /**
395 >     * toArray(null) throws NPE
396 >     */
397 >    public void testToArray_BadArg() {
398 >        try {
399 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
400 >            Object o[] = q.toArray(null);
401 >            shouldThrow();
402 >        } catch (NullPointerException success) {}
403 >    }
404 >
405 >    /**
406 >     * toArray with incompatible array type throws ArrayStoreException
407 >     */
408 >    public void testToArray1_BadArg() {
409 >        try {
410 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
411 >            Object o[] = q.toArray(new String[10] );
412 >            shouldThrow();
413 >        } catch (ArrayStoreException success) {}
414 >    }
415 >
416 >    /**
417 >     *  iterator iterates through all elements
418 >     */
419 >    public void testIterator() {
420          ConcurrentLinkedQueue q = populatedQueue(SIZE);
421          int i = 0;
422 <        Iterator it = q.iterator();
423 <        while(it.hasNext()) {
422 >        Iterator it = q.iterator();
423 >        while (it.hasNext()) {
424              assertTrue(q.contains(it.next()));
425              ++i;
426          }
427          assertEquals(i, SIZE);
428      }
429  
430 +    /**
431 +     * iterator ordering is FIFO
432 +     */
433      public void testIteratorOrdering() {
434          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
435          q.add(one);
# Line 320 | Line 439 | public class ConcurrentLinkedQueueTest e
439          int k = 0;
440          for (Iterator it = q.iterator(); it.hasNext();) {
441              int i = ((Integer)(it.next())).intValue();
442 <            assertEquals("items should come out in order", ++k, i);
442 >            assertEquals(++k, i);
443          }
444  
445 <        assertEquals("should go through 3 elements", 3, k);
445 >        assertEquals(3, k);
446      }
447  
448 +    /**
449 +     * Modifications do not cause iterators to fail
450 +     */
451      public void testWeaklyConsistentIteration () {
452          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
453          q.add(one);
454          q.add(two);
455          q.add(three);
456  
457 <        try {
458 <            for (Iterator it = q.iterator(); it.hasNext();) {
459 <                q.remove();
338 <                it.next();
339 <            }
340 <        }
341 <        catch (ConcurrentModificationException e) {
342 <            fail("weakly consistent iterator; should not get CME");
457 >        for (Iterator it = q.iterator(); it.hasNext();) {
458 >            q.remove();
459 >            it.next();
460          }
461  
462          assertEquals("queue should be empty again", 0, q.size());
463      }
464  
465 +    /**
466 +     * iterator.remove removes current element
467 +     */
468      public void testIteratorRemove () {
469          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
470          q.add(one);
# Line 360 | Line 480 | public class ConcurrentLinkedQueueTest e
480      }
481  
482  
483 <    public void testToString(){
483 >    /**
484 >     * toString contains toStrings of elements
485 >     */
486 >    public void testToString() {
487          ConcurrentLinkedQueue q = populatedQueue(SIZE);
488          String s = q.toString();
489          for (int i = 0; i < SIZE; ++i) {
490              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
491          }
492 <    }        
492 >    }
493  
494 <    public void testSerialization() {
494 >    /**
495 >     * A deserialized serialized queue has same elements in same order
496 >     */
497 >    public void testSerialization() throws Exception {
498          ConcurrentLinkedQueue q = populatedQueue(SIZE);
499 <        try {
500 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
501 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
502 <            out.writeObject(q);
503 <            out.close();
504 <
505 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
506 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
507 <            ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
508 <            assertEquals(q.size(), r.size());
509 <            while (!q.isEmpty())
384 <                assertEquals(q.remove(), r.remove());
385 <        } catch(Exception e){
386 <            e.printStackTrace();
387 <            fail("unexpected exception");
388 <        }
499 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
500 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
501 >        out.writeObject(q);
502 >        out.close();
503 >
504 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
505 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
506 >        ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
507 >        assertEquals(q.size(), r.size());
508 >        while (!q.isEmpty())
509 >            assertEquals(q.remove(), r.remove());
510      }
511  
512   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines