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.33 by jsr166, Wed Dec 31 20:09:08 2014 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/publicdomain/zero/1.0/
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9 < import junit.framework.*;
10 < import java.util.*;
11 < import java.util.concurrent.*;
12 < import java.io.*;
9 > import java.util.Arrays;
10 > import java.util.Collection;
11 > import java.util.Iterator;
12 > import java.util.NoSuchElementException;
13 > import java.util.Queue;
14 > import java.util.concurrent.ConcurrentLinkedQueue;
15 >
16 > import junit.framework.Test;
17 > import junit.framework.TestSuite;
18  
19   public class ConcurrentLinkedQueueTest extends JSR166TestCase {
20  
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());  
22 >        junit.textui.TestRunner.run(suite());
23      }
24  
25      public static Test suite() {
26 <        return new TestSuite(ConcurrentLinkedQueueTest.class);
26 >        return new TestSuite(ConcurrentLinkedQueueTest.class);
27      }
28  
29      /**
30 <     * Create a queue of given size containing consecutive
30 >     * Returns a new queue of given size containing consecutive
31       * Integers 0 ... n.
32       */
33 <    private ConcurrentLinkedQueue populatedQueue(int n) {
34 <        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
33 >    private ConcurrentLinkedQueue<Integer> populatedQueue(int n) {
34 >        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>();
35          assertTrue(q.isEmpty());
36 <        for(int i = 0; i < n; ++i)
37 <            assertTrue(q.offer(new Integer(i)));
36 >        for (int i = 0; i < n; ++i)
37 >            assertTrue(q.offer(new Integer(i)));
38          assertFalse(q.isEmpty());
39 <        assertEquals(n, q.size());
39 >        assertEquals(n, q.size());
40          return q;
41      }
42 <
42 >
43      /**
44 <     *
44 >     * new queue is empty
45       */
46      public void testConstructor1() {
47          assertEquals(0, new ConcurrentLinkedQueue().size());
48      }
49  
50      /**
51 <     *
51 >     * Initializing from null Collection throws NPE
52       */
53      public void testConstructor3() {
54          try {
55              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
56              shouldThrow();
57 <        }
52 <        catch (NullPointerException success) {}
57 >        } catch (NullPointerException success) {}
58      }
59  
60      /**
61 <     *
61 >     * Initializing from Collection of null elements throws NPE
62       */
63      public void testConstructor4() {
64          try {
65              Integer[] ints = new Integer[SIZE];
66              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
67              shouldThrow();
68 <        }
64 <        catch (NullPointerException success) {}
68 >        } catch (NullPointerException success) {}
69      }
70  
71      /**
72 <     *
72 >     * Initializing from Collection with some null elements throws NPE
73       */
74      public void testConstructor5() {
75          try {
# Line 74 | Line 78 | public class ConcurrentLinkedQueueTest e
78                  ints[i] = new Integer(i);
79              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
80              shouldThrow();
81 <        }
78 <        catch (NullPointerException success) {}
81 >        } catch (NullPointerException success) {}
82      }
83  
84      /**
85 <     *
85 >     * Queue contains all elements of collection used to initialize
86       */
87      public void testConstructor6() {
88 <        try {
89 <            Integer[] ints = new Integer[SIZE];
90 <            for (int i = 0; i < SIZE; ++i)
91 <                ints[i] = new Integer(i);
92 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
93 <            for (int i = 0; i < SIZE; ++i)
91 <                assertEquals(ints[i], q.poll());
92 <        }
93 <        finally {}
88 >        Integer[] ints = new Integer[SIZE];
89 >        for (int i = 0; i < SIZE; ++i)
90 >            ints[i] = new Integer(i);
91 >        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
92 >        for (int i = 0; i < SIZE; ++i)
93 >            assertEquals(ints[i], q.poll());
94      }
95  
96      /**
97 <     *
97 >     * isEmpty is true before add, false after
98       */
99      public void testEmpty() {
100          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 108 | Line 108 | public class ConcurrentLinkedQueueTest e
108      }
109  
110      /**
111 <     *
111 >     * size changes when elements added and removed
112       */
113      public void testSize() {
114          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 123 | Line 123 | public class ConcurrentLinkedQueueTest e
123      }
124  
125      /**
126 <     *
126 >     * offer(null) throws NPE
127       */
128      public void testOfferNull() {
129 <        try {
129 >        try {
130              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
131              q.offer(null);
132              shouldThrow();
133 <        } catch (NullPointerException success) { }  
133 >        } catch (NullPointerException success) {}
134 >    }
135 >
136 >    /**
137 >     * 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 <     *
148 >     * Offer returns true
149       */
150      public void testOffer() {
151          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 143 | Line 154 | public class ConcurrentLinkedQueueTest e
154      }
155  
156      /**
157 <     *
157 >     * add returns true
158       */
159      public void testAdd() {
160          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 154 | Line 165 | public class ConcurrentLinkedQueueTest e
165      }
166  
167      /**
168 <     *
168 >     * addAll(null) throws NPE
169       */
170      public void testAddAll1() {
171          try {
172              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
173              q.addAll(null);
174              shouldThrow();
175 <        }
176 <        catch (NullPointerException success) {}
175 >        } catch (NullPointerException success) {}
176 >    }
177 >
178 >    /**
179 >     * addAll(this) throws IAE
180 >     */
181 >    public void testAddAllSelf() {
182 >        try {
183 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
184 >            q.addAll(q);
185 >            shouldThrow();
186 >        } catch (IllegalArgumentException success) {}
187      }
188 +
189      /**
190 <     *
190 >     * addAll of a collection with null elements throws NPE
191       */
192      public void testAddAll2() {
193          try {
# Line 173 | Line 195 | public class ConcurrentLinkedQueueTest e
195              Integer[] ints = new Integer[SIZE];
196              q.addAll(Arrays.asList(ints));
197              shouldThrow();
198 <        }
177 <        catch (NullPointerException success) {}
198 >        } catch (NullPointerException success) {}
199      }
200 +
201      /**
202 <     *
202 >     * addAll of a collection with any null elements throws NPE after
203 >     * possibly adding some elements
204       */
205      public void testAddAll3() {
206          try {
# Line 187 | Line 210 | public class ConcurrentLinkedQueueTest e
210                  ints[i] = new Integer(i);
211              q.addAll(Arrays.asList(ints));
212              shouldThrow();
213 <        }
191 <        catch (NullPointerException success) {}
213 >        } catch (NullPointerException success) {}
214      }
215  
216      /**
217 <     *
217 >     * Queue contains all elements, in traversal order, of successful addAll
218       */
219      public void testAddAll5() {
220 <        try {
221 <            Integer[] empty = new Integer[0];
222 <            Integer[] ints = new Integer[SIZE];
223 <            for (int i = 0; i < SIZE; ++i)
224 <                ints[i] = new Integer(i);
225 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
226 <            assertFalse(q.addAll(Arrays.asList(empty)));
227 <            assertTrue(q.addAll(Arrays.asList(ints)));
228 <            for (int i = 0; i < SIZE; ++i)
207 <                assertEquals(ints[i], q.poll());
208 <        }
209 <        finally {}
220 >        Integer[] empty = new Integer[0];
221 >        Integer[] ints = new Integer[SIZE];
222 >        for (int i = 0; i < SIZE; ++i)
223 >            ints[i] = new Integer(i);
224 >        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
225 >        assertFalse(q.addAll(Arrays.asList(empty)));
226 >        assertTrue(q.addAll(Arrays.asList(ints)));
227 >        for (int i = 0; i < SIZE; ++i)
228 >            assertEquals(ints[i], q.poll());
229      }
230  
231      /**
232 <     *
232 >     * poll succeeds unless empty
233       */
234      public void testPoll() {
235          ConcurrentLinkedQueue q = populatedQueue(SIZE);
236          for (int i = 0; i < SIZE; ++i) {
237 <            assertEquals(i, ((Integer)q.poll()).intValue());
237 >            assertEquals(i, q.poll());
238          }
239 <        assertNull(q.poll());
239 >        assertNull(q.poll());
240      }
241  
242      /**
243 <     *
243 >     * peek returns next element, or null if empty
244       */
245      public void testPeek() {
246          ConcurrentLinkedQueue q = populatedQueue(SIZE);
247          for (int i = 0; i < SIZE; ++i) {
248 <            assertEquals(i, ((Integer)q.peek()).intValue());
249 <            q.poll();
248 >            assertEquals(i, q.peek());
249 >            assertEquals(i, q.poll());
250              assertTrue(q.peek() == null ||
251 <                       i != ((Integer)q.peek()).intValue());
251 >                       !q.peek().equals(i));
252          }
253 <        assertNull(q.peek());
253 >        assertNull(q.peek());
254      }
255  
256      /**
257 <     *
257 >     * element returns next element, or throws NSEE if empty
258       */
259      public void testElement() {
260          ConcurrentLinkedQueue q = populatedQueue(SIZE);
261          for (int i = 0; i < SIZE; ++i) {
262 <            assertEquals(i, ((Integer)q.element()).intValue());
263 <            q.poll();
262 >            assertEquals(i, q.element());
263 >            assertEquals(i, q.poll());
264          }
265          try {
266              q.element();
267              shouldThrow();
268 <        }
250 <        catch (NoSuchElementException success) {}
268 >        } catch (NoSuchElementException success) {}
269      }
270  
271      /**
272 <     *
272 >     * remove removes next element, or throws NSEE if empty
273       */
274      public void testRemove() {
275          ConcurrentLinkedQueue q = populatedQueue(SIZE);
276          for (int i = 0; i < SIZE; ++i) {
277 <            assertEquals(i, ((Integer)q.remove()).intValue());
277 >            assertEquals(i, q.remove());
278          }
279          try {
280              q.remove();
281              shouldThrow();
282 <        } catch (NoSuchElementException success){
265 <        }  
282 >        } catch (NoSuchElementException success) {}
283      }
284  
285      /**
286 <     *
286 >     * remove(x) removes x and returns true if present
287       */
288      public void testRemoveElement() {
289          ConcurrentLinkedQueue q = populatedQueue(SIZE);
290 <        for (int i = 1; i < SIZE; i+=2) {
291 <            assertTrue(q.remove(new Integer(i)));
292 <        }
293 <        for (int i = 0; i < SIZE; i+=2) {
294 <            assertTrue(q.remove(new Integer(i)));
295 <            assertFalse(q.remove(new Integer(i+1)));
290 >        for (int i = 1; i < SIZE; i += 2) {
291 >            assertTrue(q.contains(i));
292 >            assertTrue(q.remove(i));
293 >            assertFalse(q.contains(i));
294 >            assertTrue(q.contains(i-1));
295 >        }
296 >        for (int i = 0; i < SIZE; i += 2) {
297 >            assertTrue(q.contains(i));
298 >            assertTrue(q.remove(i));
299 >            assertFalse(q.contains(i));
300 >            assertFalse(q.remove(i+1));
301 >            assertFalse(q.contains(i+1));
302          }
303          assertTrue(q.isEmpty());
304      }
305 <        
305 >
306      /**
307 <     *
307 >     * contains(x) reports true when elements added but not yet removed
308       */
309      public void testContains() {
310          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 293 | Line 316 | public class ConcurrentLinkedQueueTest e
316      }
317  
318      /**
319 <     *
319 >     * clear removes all elements
320       */
321      public void testClear() {
322          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 307 | Line 330 | public class ConcurrentLinkedQueueTest e
330      }
331  
332      /**
333 <     *
333 >     * containsAll(c) is true when c contains a subset of elements
334       */
335      public void testContainsAll() {
336          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 321 | Line 344 | public class ConcurrentLinkedQueueTest e
344      }
345  
346      /**
347 <     *
347 >     * retainAll(c) retains only those elements of c and reports true if change
348       */
349      public void testRetainAll() {
350          ConcurrentLinkedQueue q = populatedQueue(SIZE);
# Line 340 | Line 363 | public class ConcurrentLinkedQueueTest e
363      }
364  
365      /**
366 <     *
366 >     * removeAll(c) removes only those elements of c and reports true if changed
367       */
368      public void testRemoveAll() {
369          for (int i = 1; i < SIZE; ++i) {
# Line 356 | Line 379 | public class ConcurrentLinkedQueueTest e
379      }
380  
381      /**
382 <     *
382 >     * toArray contains all elements in FIFO order
383       */
384      public void testToArray() {
385          ConcurrentLinkedQueue q = populatedQueue(SIZE);
386 <        Object[] o = q.toArray();
387 <        Arrays.sort(o);
388 <        for(int i = 0; i < o.length; i++)
366 <            assertEquals(o[i], q.poll());
386 >        Object[] o = q.toArray();
387 >        for (int i = 0; i < o.length; i++)
388 >            assertSame(o[i], q.poll());
389      }
390  
391      /**
392 <     *
392 >     * toArray(a) contains all elements in FIFO order
393       */
394      public void testToArray2() {
395 +        ConcurrentLinkedQueue<Integer> q = populatedQueue(SIZE);
396 +        Integer[] ints = new Integer[SIZE];
397 +        Integer[] array = q.toArray(ints);
398 +        assertSame(ints, array);
399 +        for (int i = 0; i < ints.length; i++)
400 +            assertSame(ints[i], q.poll());
401 +    }
402 +
403 +    /**
404 +     * toArray(null) throws NullPointerException
405 +     */
406 +    public void testToArray_NullArg() {
407          ConcurrentLinkedQueue q = populatedQueue(SIZE);
408 <        Integer[] ints = new Integer[SIZE];
409 <        ints = (Integer[])q.toArray(ints);
410 <        Arrays.sort(ints);
411 <        for(int i = 0; i < ints.length; i++)
378 <            assertEquals(ints[i], q.poll());
408 >        try {
409 >            q.toArray(null);
410 >            shouldThrow();
411 >        } catch (NullPointerException success) {}
412      }
413 <    
413 >
414      /**
415 <     *
415 >     * toArray(incompatible array type) throws ArrayStoreException
416 >     */
417 >    public void testToArray1_BadArg() {
418 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
419 >        try {
420 >            q.toArray(new String[10]);
421 >            shouldThrow();
422 >        } catch (ArrayStoreException success) {}
423 >    }
424 >
425 >    /**
426 >     * iterator iterates through all elements
427       */
428      public void testIterator() {
429          ConcurrentLinkedQueue q = populatedQueue(SIZE);
430          int i = 0;
431 <        Iterator it = q.iterator();
432 <        while(it.hasNext()) {
431 >        Iterator it = q.iterator();
432 >        while (it.hasNext()) {
433              assertTrue(q.contains(it.next()));
434              ++i;
435          }
# Line 393 | Line 437 | public class ConcurrentLinkedQueueTest e
437      }
438  
439      /**
440 <     *
440 >     * iterator ordering is FIFO
441       */
442      public void testIteratorOrdering() {
443          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
# Line 403 | Line 447 | public class ConcurrentLinkedQueueTest e
447  
448          int k = 0;
449          for (Iterator it = q.iterator(); it.hasNext();) {
450 <            int i = ((Integer)(it.next())).intValue();
407 <            assertEquals(++k, i);
450 >            assertEquals(++k, it.next());
451          }
452  
453          assertEquals(3, k);
454      }
455  
456      /**
457 <     *
457 >     * Modifications do not cause iterators to fail
458       */
459 <    public void testWeaklyConsistentIteration () {
459 >    public void testWeaklyConsistentIteration() {
460          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
461          q.add(one);
462          q.add(two);
463          q.add(three);
464  
465 <        try {
466 <            for (Iterator it = q.iterator(); it.hasNext();) {
467 <                q.remove();
425 <                it.next();
426 <            }
427 <        }
428 <        catch (ConcurrentModificationException e) {
429 <            shouldThrow();
465 >        for (Iterator it = q.iterator(); it.hasNext();) {
466 >            q.remove();
467 >            it.next();
468          }
469  
470          assertEquals("queue should be empty again", 0, q.size());
471      }
472  
473      /**
474 <     *
474 >     * iterator.remove removes current element
475       */
476 <    public void testIteratorRemove () {
476 >    public void testIteratorRemove() {
477          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
478          q.add(one);
479          q.add(two);
# Line 444 | Line 482 | public class ConcurrentLinkedQueueTest e
482          it.next();
483          it.remove();
484          it = q.iterator();
485 <        assertEquals(it.next(), two);
486 <        assertEquals(it.next(), three);
485 >        assertSame(it.next(), two);
486 >        assertSame(it.next(), three);
487          assertFalse(it.hasNext());
488      }
489  
452
490      /**
491 <     *
491 >     * toString contains toStrings of elements
492       */
493      public void testToString() {
494          ConcurrentLinkedQueue q = populatedQueue(SIZE);
495          String s = q.toString();
496          for (int i = 0; i < SIZE; ++i) {
497 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
497 >            assertTrue(s.contains(String.valueOf(i)));
498          }
499 <    }        
499 >    }
500  
501      /**
502 <     *
502 >     * A deserialized serialized queue has same elements in same order
503       */
504 <    public void testSerialization() {
505 <        ConcurrentLinkedQueue q = populatedQueue(SIZE);
506 <        try {
470 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
471 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
472 <            out.writeObject(q);
473 <            out.close();
504 >    public void testSerialization() throws Exception {
505 >        Queue x = populatedQueue(SIZE);
506 >        Queue y = serialClone(x);
507  
508 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
509 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
510 <            ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
511 <            assertEquals(q.size(), r.size());
512 <            while (!q.isEmpty())
513 <                assertEquals(q.remove(), r.remove());
514 <        } catch(Exception e){
482 <            unexpectedException();
508 >        assertNotSame(x, y);
509 >        assertEquals(x.size(), y.size());
510 >        assertEquals(x.toString(), y.toString());
511 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
512 >        while (!x.isEmpty()) {
513 >            assertFalse(y.isEmpty());
514 >            assertEquals(x.remove(), y.remove());
515          }
516 +        assertTrue(y.isEmpty());
517      }
518  
519 +    /**
520 +     * remove(null), contains(null) always return false
521 +     */
522 +    public void testNeverContainsNull() {
523 +        Collection<?>[] qs = {
524 +            new ConcurrentLinkedQueue<Object>(),
525 +            populatedQueue(2),
526 +        };
527 +
528 +        for (Collection<?> q : qs) {
529 +            assertFalse(q.contains(null));
530 +            assertFalse(q.remove(null));
531 +        }
532 +    }
533   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines