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.4 by dl, Sat Sep 20 18:20:07 2003 UTC vs.
Revision 1.19 by jsr166, Sat Oct 9 19:30:34 2010 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 <
37 >
38      /**
39 <     *
39 >     * new queue is empty
40       */
41      public void testConstructor1() {
42          assertEquals(0, new ConcurrentLinkedQueue().size());
43      }
44  
45      /**
46 <     *
46 >     * Initializing from null Collection throws NPE
47       */
48      public void testConstructor3() {
49          try {
50              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
51              shouldThrow();
52 <        }
52 <        catch (NullPointerException success) {}
52 >        } catch (NullPointerException success) {}
53      }
54  
55      /**
56 <     *
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              shouldThrow();
63 <        }
64 <        catch (NullPointerException success) {}
63 >        } catch (NullPointerException success) {}
64      }
65  
66      /**
67 <     *
67 >     * Initializing from Collection with some null elements throws NPE
68       */
69      public void testConstructor5() {
70          try {
# Line 74 | Line 73 | public class ConcurrentLinkedQueueTest e
73                  ints[i] = new Integer(i);
74              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
75              shouldThrow();
76 <        }
78 <        catch (NullPointerException success) {}
76 >        } catch (NullPointerException success) {}
77      }
78  
79      /**
80 <     *
80 >     * Queue contains all elements of collection used to initialize
81       */
82      public void testConstructor6() {
83 <        try {
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)
91 <                assertEquals(ints[i], q.poll());
92 <        }
93 <        finally {}
83 >        Integer[] ints = new Integer[SIZE];
84 >        for (int i = 0; i < SIZE; ++i)
85 >            ints[i] = new Integer(i);
86 >        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
87 >        for (int i = 0; i < SIZE; ++i)
88 >            assertEquals(ints[i], q.poll());
89      }
90  
91      /**
92 <     *
92 >     * isEmpty is true before add, false after
93       */
94      public void testEmpty() {
95          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 108 | Line 103 | public class ConcurrentLinkedQueueTest e
103      }
104  
105      /**
106 <     *
106 >     * size changes when elements added and removed
107       */
108      public void testSize() {
109          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 123 | Line 118 | public class ConcurrentLinkedQueueTest e
118      }
119  
120      /**
121 <     *
121 >     * offer(null) throws NPE
122       */
123      public void testOfferNull() {
124 <        try {
124 >        try {
125              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
126              q.offer(null);
127              shouldThrow();
128 <        } catch (NullPointerException success) { }  
128 >        } catch (NullPointerException success) {}
129 >    }
130 >
131 >    /**
132 >     * add(null) throws NPE
133 >     */
134 >    public void testAddNull() {
135 >        try {
136 >            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
137 >            q.add(null);
138 >            shouldThrow();
139 >        } catch (NullPointerException success) {}
140      }
141  
142 +
143      /**
144 <     *
144 >     * Offer returns true
145       */
146      public void testOffer() {
147          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 143 | Line 150 | public class ConcurrentLinkedQueueTest e
150      }
151  
152      /**
153 <     *
153 >     * add returns true
154       */
155      public void testAdd() {
156          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 154 | Line 161 | public class ConcurrentLinkedQueueTest e
161      }
162  
163      /**
164 <     *
164 >     * addAll(null) throws NPE
165       */
166      public void testAddAll1() {
167          try {
168              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
169              q.addAll(null);
170              shouldThrow();
171 <        }
165 <        catch (NullPointerException success) {}
171 >        } catch (NullPointerException success) {}
172      }
173 +
174      /**
175 <     *
175 >     * addAll(this) throws IAE
176 >     */
177 >    public void testAddAllSelf() {
178 >        try {
179 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
180 >            q.addAll(q);
181 >            shouldThrow();
182 >        } catch (IllegalArgumentException success) {}
183 >    }
184 >
185 >    /**
186 >     * addAll of a collection with null elements throws NPE
187       */
188      public void testAddAll2() {
189          try {
# Line 173 | Line 191 | public class ConcurrentLinkedQueueTest e
191              Integer[] ints = new Integer[SIZE];
192              q.addAll(Arrays.asList(ints));
193              shouldThrow();
194 <        }
177 <        catch (NullPointerException success) {}
194 >        } catch (NullPointerException success) {}
195      }
196 +
197      /**
198 <     *
198 >     * addAll of a collection with any null elements throws NPE after
199 >     * possibly adding some elements
200       */
201      public void testAddAll3() {
202          try {
# Line 187 | Line 206 | public class ConcurrentLinkedQueueTest e
206                  ints[i] = new Integer(i);
207              q.addAll(Arrays.asList(ints));
208              shouldThrow();
209 <        }
191 <        catch (NullPointerException success) {}
209 >        } catch (NullPointerException success) {}
210      }
211  
212      /**
213 <     *
213 >     * Queue contains all elements, in traversal order, of successful addAll
214       */
215      public void testAddAll5() {
216 <        try {
217 <            Integer[] empty = new Integer[0];
218 <            Integer[] ints = new Integer[SIZE];
219 <            for (int i = 0; i < SIZE; ++i)
220 <                ints[i] = new Integer(i);
221 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
222 <            assertFalse(q.addAll(Arrays.asList(empty)));
223 <            assertTrue(q.addAll(Arrays.asList(ints)));
224 <            for (int i = 0; i < SIZE; ++i)
207 <                assertEquals(ints[i], q.poll());
208 <        }
209 <        finally {}
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      /**
228 <     *
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());
233 >            assertEquals(i, q.poll());
234          }
235 <        assertNull(q.poll());
235 >        assertNull(q.poll());
236      }
237  
238      /**
239 <     *
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());
245 <            q.poll();
244 >            assertEquals(i, q.peek());
245 >            assertEquals(i, q.poll());
246              assertTrue(q.peek() == null ||
247 <                       i != ((Integer)q.peek()).intValue());
247 >                       !q.peek().equals(i));
248          }
249 <        assertNull(q.peek());
249 >        assertNull(q.peek());
250      }
251  
252      /**
253 <     *
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());
259 <            q.poll();
258 >            assertEquals(i, q.element());
259 >            assertEquals(i, q.poll());
260          }
261          try {
262              q.element();
263              shouldThrow();
264 <        }
250 <        catch (NoSuchElementException success) {}
264 >        } catch (NoSuchElementException success) {}
265      }
266  
267      /**
268 <     *
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());
273 >            assertEquals(i, q.remove());
274          }
275          try {
276              q.remove();
277              shouldThrow();
278 <        } catch (NoSuchElementException success){
265 <        }  
278 >        } catch (NoSuchElementException success) {}
279      }
280  
281      /**
282 <     *
282 >     * remove(x) removes x and returns true if present
283       */
284      public void testRemoveElement() {
285          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 279 | Line 292 | public class ConcurrentLinkedQueueTest e
292          }
293          assertTrue(q.isEmpty());
294      }
295 <        
295 >
296      /**
297 <     *
297 >     * contains(x) reports true when elements added but not yet removed
298       */
299      public void testContains() {
300          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 293 | Line 306 | public class ConcurrentLinkedQueueTest e
306      }
307  
308      /**
309 <     *
309 >     * clear removes all elements
310       */
311      public void testClear() {
312          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 307 | Line 320 | public class ConcurrentLinkedQueueTest e
320      }
321  
322      /**
323 <     *
323 >     * containsAll(c) is true when c contains a subset of elements
324       */
325      public void testContainsAll() {
326          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 321 | Line 334 | public class ConcurrentLinkedQueueTest e
334      }
335  
336      /**
337 <     *
337 >     * retainAll(c) retains only those elements of c and reports true if change
338       */
339      public void testRetainAll() {
340          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 340 | Line 353 | public class ConcurrentLinkedQueueTest e
353      }
354  
355      /**
356 <     *
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) {
# Line 356 | Line 369 | public class ConcurrentLinkedQueueTest e
369      }
370  
371      /**
372 <     *
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      /**
383 <     *
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 <    
393 >
394 >    /**
395 >     * toArray(null) throws NPE
396 >     */
397 >    public void testToArray_BadArg() {
398 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
399 >        try {
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 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
410 >        try {
411 >            Object o[] = q.toArray(new String[10]);
412 >            shouldThrow();
413 >        } catch (ArrayStoreException success) {}
414 >    }
415 >
416      /**
417 <     *
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          }
# Line 393 | Line 428 | public class ConcurrentLinkedQueueTest e
428      }
429  
430      /**
431 <     *
431 >     * iterator ordering is FIFO
432       */
433      public void testIteratorOrdering() {
434          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 403 | Line 438 | public class ConcurrentLinkedQueueTest e
438  
439          int k = 0;
440          for (Iterator it = q.iterator(); it.hasNext();) {
441 <            int i = ((Integer)(it.next())).intValue();
407 <            assertEquals(++k, i);
441 >            assertEquals(++k, it.next());
442          }
443  
444          assertEquals(3, k);
445      }
446  
447      /**
448 <     *
448 >     * Modifications do not cause iterators to fail
449       */
450 <    public void testWeaklyConsistentIteration () {
450 >    public void testWeaklyConsistentIteration() {
451          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
452          q.add(one);
453          q.add(two);
454          q.add(three);
455  
456 <        try {
457 <            for (Iterator it = q.iterator(); it.hasNext();) {
458 <                q.remove();
425 <                it.next();
426 <            }
427 <        }
428 <        catch (ConcurrentModificationException e) {
429 <            shouldThrow();
456 >        for (Iterator it = q.iterator(); it.hasNext();) {
457 >            q.remove();
458 >            it.next();
459          }
460  
461          assertEquals("queue should be empty again", 0, q.size());
462      }
463  
464      /**
465 <     *
465 >     * iterator.remove removes current element
466       */
467 <    public void testIteratorRemove () {
467 >    public void testIteratorRemove() {
468          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
469          q.add(one);
470          q.add(two);
# Line 444 | Line 473 | public class ConcurrentLinkedQueueTest e
473          it.next();
474          it.remove();
475          it = q.iterator();
476 <        assertEquals(it.next(), two);
477 <        assertEquals(it.next(), three);
476 >        assertSame(it.next(), two);
477 >        assertSame(it.next(), three);
478          assertFalse(it.hasNext());
479      }
480  
481  
482      /**
483 <     *
483 >     * toString contains toStrings of elements
484       */
485      public void testToString() {
486          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 459 | Line 488 | public class ConcurrentLinkedQueueTest e
488          for (int i = 0; i < SIZE; ++i) {
489              assertTrue(s.indexOf(String.valueOf(i)) >= 0);
490          }
491 <    }        
491 >    }
492  
493      /**
494 <     *
494 >     * A deserialized serialized queue has same elements in same order
495       */
496 <    public void testSerialization() {
496 >    public void testSerialization() throws Exception {
497          ConcurrentLinkedQueue q = populatedQueue(SIZE);
498 <        try {
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())
480 <                assertEquals(q.remove(), r.remove());
481 <        } catch(Exception e){
482 <            unexpectedException();
483 <        }
498 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
499 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
500 >        out.writeObject(q);
501 >        out.close();
502 >
503 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
504 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
505 >        ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
506 >        assertEquals(q.size(), r.size());
507 >        while (!q.isEmpty())
508 >            assertEquals(q.remove(), r.remove());
509      }
510  
511   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines