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.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.35 by jsr166, Sat Jan 17 22:55:06 2015 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.*;
13 <
14 < public class ConcurrentLinkedQueueTest extends TestCase {
15 <    private static final int N = 10;
16 <    private static final long SHORT_DELAY_MS = 100;
17 <    private static final long MEDIUM_DELAY_MS = 1000;
18 <    private static final long LONG_DELAY_MS = 10000;
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 fullQueue(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 <
43 <    public void testConstructor1(){
42 >
43 >    /**
44 >     * new queue is empty
45 >     */
46 >    public void testConstructor1() {
47          assertEquals(0, new ConcurrentLinkedQueue().size());
48      }
49  
50 +    /**
51 +     * Initializing from null Collection throws NPE
52 +     */
53      public void testConstructor3() {
54          try {
55              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
56 <            fail("Cannot make from null collection");
57 <        }
50 <        catch (NullPointerException success) {}
56 >            shouldThrow();
57 >        } catch (NullPointerException success) {}
58      }
59  
60 <    public void testConstructor4(){
60 >    /**
61 >     * Initializing from Collection of null elements throws NPE
62 >     */
63 >    public void testConstructor4() {
64          try {
65 <            Integer[] ints = new Integer[N];
65 >            Integer[] ints = new Integer[SIZE];
66              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
67 <            fail("Cannot make with null elements");
68 <        }
59 <        catch (NullPointerException success) {}
67 >            shouldThrow();
68 >        } catch (NullPointerException success) {}
69      }
70  
71 <    public void testConstructor5(){
71 >    /**
72 >     * Initializing from Collection with some null elements throws NPE
73 >     */
74 >    public void testConstructor5() {
75          try {
76 <            Integer[] ints = new Integer[N];
77 <            for (int i = 0; i < N-1; ++i)
76 >            Integer[] ints = new Integer[SIZE];
77 >            for (int i = 0; i < SIZE-1; ++i)
78                  ints[i] = new Integer(i);
79              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
80 <            fail("Cannot make with null elements");
81 <        }
70 <        catch (NullPointerException success) {}
80 >            shouldThrow();
81 >        } catch (NullPointerException success) {}
82      }
83  
84 <    public void testConstructor6(){
85 <        try {
86 <            Integer[] ints = new Integer[N];
87 <            for (int i = 0; i < N; ++i)
88 <                ints[i] = new Integer(i);
89 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
90 <            for (int i = 0; i < N; ++i)
91 <                assertEquals(ints[i], q.poll());
92 <        }
93 <        finally {}
84 >    /**
85 >     * Queue contains all elements of collection used to initialize
86 >     */
87 >    public void testConstructor6() {
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 +     * isEmpty is true before add, false after
98 +     */
99      public void testEmpty() {
100          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
101          assertTrue(q.isEmpty());
102 <        q.add(new Integer(1));
102 >        q.add(one);
103          assertFalse(q.isEmpty());
104 <        q.add(new Integer(2));
104 >        q.add(two);
105          q.remove();
106          q.remove();
107          assertTrue(q.isEmpty());
108      }
109  
110 +    /**
111 +     * size changes when elements added and removed
112 +     */
113      public void testSize() {
114 <        ConcurrentLinkedQueue q = fullQueue(N);
115 <        for (int i = 0; i < N; ++i) {
116 <            assertEquals(N-i, q.size());
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 < N; ++i) {
119 >        for (int i = 0; i < SIZE; ++i) {
120              assertEquals(i, q.size());
121              q.add(new Integer(i));
122          }
123      }
124  
125 <    public void testOfferNull(){
126 <        try {
125 >    /**
126 >     * offer(null) throws NPE
127 >     */
128 >    public void testOfferNull() {
129 >        try {
130              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
131              q.offer(null);
132 <            fail("should throw NPE");
133 <        } catch (NullPointerException success) { }  
132 >            shouldThrow();
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 +     * Offer returns true
149 +     */
150      public void testOffer() {
151          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
152 <        assertTrue(q.offer(new Integer(0)));
153 <        assertTrue(q.offer(new Integer(1)));
152 >        assertTrue(q.offer(zero));
153 >        assertTrue(q.offer(one));
154      }
155  
156 <    public void testAdd(){
156 >    /**
157 >     * add returns true
158 >     */
159 >    public void testAdd() {
160          ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
161 <        for (int i = 0; i < N; ++i) {
161 >        for (int i = 0; i < SIZE; ++i) {
162              assertEquals(i, q.size());
163              assertTrue(q.add(new Integer(i)));
164          }
165      }
166  
167 <    public void testAddAll1(){
167 >    /**
168 >     * addAll(null) throws NPE
169 >     */
170 >    public void testAddAll1() {
171          try {
172              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
173              q.addAll(null);
174 <            fail("Cannot add null collection");
175 <        }
176 <        catch (NullPointerException success) {}
174 >            shouldThrow();
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 <    public void testAddAll2(){
188 >
189 >    /**
190 >     * addAll of a collection with null elements throws NPE
191 >     */
192 >    public void testAddAll2() {
193          try {
194              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
195 <            Integer[] ints = new Integer[N];
195 >            Integer[] ints = new Integer[SIZE];
196              q.addAll(Arrays.asList(ints));
197 <            fail("Cannot add null elements");
198 <        }
145 <        catch (NullPointerException success) {}
197 >            shouldThrow();
198 >        } catch (NullPointerException success) {}
199      }
200 <    public void testAddAll3(){
200 >
201 >    /**
202 >     * addAll of a collection with any null elements throws NPE after
203 >     * possibly adding some elements
204 >     */
205 >    public void testAddAll3() {
206          try {
207              ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
208 <            Integer[] ints = new Integer[N];
209 <            for (int i = 0; i < N-1; ++i)
208 >            Integer[] ints = new Integer[SIZE];
209 >            for (int i = 0; i < SIZE-1; ++i)
210                  ints[i] = new Integer(i);
211              q.addAll(Arrays.asList(ints));
212 <            fail("Cannot add null elements");
213 <        }
156 <        catch (NullPointerException success) {}
212 >            shouldThrow();
213 >        } catch (NullPointerException success) {}
214      }
215  
216 <    public void testAddAll5(){
217 <        try {
218 <            Integer[] empty = new Integer[0];
219 <            Integer[] ints = new Integer[N];
220 <            for (int i = 0; i < N; ++i)
221 <                ints[i] = new Integer(i);
222 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
223 <            assertFalse(q.addAll(Arrays.asList(empty)));
224 <            assertTrue(q.addAll(Arrays.asList(ints)));
225 <            for (int i = 0; i < N; ++i)
226 <                assertEquals(ints[i], q.poll());
227 <        }
228 <        finally {}
216 >    /**
217 >     * Queue contains all elements, in traversal order, of successful addAll
218 >     */
219 >    public void testAddAll5() {
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 <    public void testPoll(){
232 <        ConcurrentLinkedQueue q = fullQueue(N);
233 <        for (int i = 0; i < N; ++i) {
234 <            assertEquals(i, ((Integer)q.poll()).intValue());
231 >    /**
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, q.poll());
238          }
239 <        assertNull(q.poll());
239 >        assertNull(q.poll());
240      }
241  
242 <    public void testPeek(){
243 <        ConcurrentLinkedQueue q = fullQueue(N);
244 <        for (int i = 0; i < N; ++i) {
245 <            assertEquals(i, ((Integer)q.peek()).intValue());
246 <            q.poll();
242 >    /**
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, 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 <    public void testElement(){
257 <        ConcurrentLinkedQueue q = fullQueue(N);
258 <        for (int i = 0; i < N; ++i) {
259 <            assertEquals(i, ((Integer)q.element()).intValue());
260 <            q.poll();
256 >    /**
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, q.element());
263 >            assertEquals(i, q.poll());
264          }
265          try {
266              q.element();
267 <            fail("no such element");
268 <        }
203 <        catch (NoSuchElementException success) {}
267 >            shouldThrow();
268 >        } catch (NoSuchElementException success) {}
269      }
270  
271 <    public void testRemove(){
272 <        ConcurrentLinkedQueue q = fullQueue(N);
273 <        for (int i = 0; i < N; ++i) {
274 <            assertEquals(i, ((Integer)q.remove()).intValue());
271 >    /**
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, q.remove());
278          }
279          try {
280              q.remove();
281 <            fail("remove should throw");
282 <        } catch (NoSuchElementException success){
215 <        }  
281 >            shouldThrow();
282 >        } catch (NoSuchElementException success) {}
283      }
284  
285 <    public void testRemoveElement(){
286 <        ConcurrentLinkedQueue q = fullQueue(N);
287 <        for (int i = 1; i < N; i+=2) {
288 <            assertTrue(q.remove(new Integer(i)));
289 <        }
290 <        for (int i = 0; i < N; i+=2) {
291 <            assertTrue(q.remove(new Integer(i)));
292 <            assertFalse(q.remove(new Integer(i+1)));
285 >    /**
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.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 <        
306 <    public void testContains(){
307 <        ConcurrentLinkedQueue q = fullQueue(N);
308 <        for (int i = 0; i < N; ++i) {
305 >
306 >    /**
307 >     * contains(x) reports true when elements added but not yet removed
308 >     */
309 >    public void testContains() {
310 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
311 >        for (int i = 0; i < SIZE; ++i) {
312              assertTrue(q.contains(new Integer(i)));
313              q.poll();
314              assertFalse(q.contains(new Integer(i)));
315          }
316      }
317  
318 <    public void testClear(){
319 <        ConcurrentLinkedQueue q = fullQueue(N);
318 >    /**
319 >     * clear removes all elements
320 >     */
321 >    public void testClear() {
322 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
323          q.clear();
324          assertTrue(q.isEmpty());
325          assertEquals(0, q.size());
326 <        q.add(new Integer(1));
326 >        q.add(one);
327          assertFalse(q.isEmpty());
328          q.clear();
329          assertTrue(q.isEmpty());
330      }
331  
332 <    public void testContainsAll(){
333 <        ConcurrentLinkedQueue q = fullQueue(N);
332 >    /**
333 >     * containsAll(c) is true when c contains a subset of elements
334 >     */
335 >    public void testContainsAll() {
336 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
337          ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
338 <        for (int i = 0; i < N; ++i) {
338 >        for (int i = 0; i < SIZE; ++i) {
339              assertTrue(q.containsAll(p));
340              assertFalse(p.containsAll(q));
341              p.add(new Integer(i));
# Line 258 | Line 343 | public class ConcurrentLinkedQueueTest e
343          assertTrue(p.containsAll(q));
344      }
345  
346 <    public void testRetainAll(){
347 <        ConcurrentLinkedQueue q = fullQueue(N);
348 <        ConcurrentLinkedQueue p = fullQueue(N);
349 <        for (int i = 0; i < N; ++i) {
346 >    /**
347 >     * retainAll(c) retains only those elements of c and reports true if change
348 >     */
349 >    public void testRetainAll() {
350 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
351 >        ConcurrentLinkedQueue p = populatedQueue(SIZE);
352 >        for (int i = 0; i < SIZE; ++i) {
353              boolean changed = q.retainAll(p);
354              if (i == 0)
355                  assertFalse(changed);
# Line 269 | Line 357 | public class ConcurrentLinkedQueueTest e
357                  assertTrue(changed);
358  
359              assertTrue(q.containsAll(p));
360 <            assertEquals(N-i, q.size());
360 >            assertEquals(SIZE-i, q.size());
361              p.remove();
362          }
363      }
364  
365 <    public void testRemoveAll(){
366 <        for (int i = 1; i < N; ++i) {
367 <            ConcurrentLinkedQueue q = fullQueue(N);
368 <            ConcurrentLinkedQueue p = fullQueue(i);
365 >    /**
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) {
370 >            ConcurrentLinkedQueue q = populatedQueue(SIZE);
371 >            ConcurrentLinkedQueue p = populatedQueue(i);
372              assertTrue(q.removeAll(p));
373 <            assertEquals(N-i, q.size());
373 >            assertEquals(SIZE-i, q.size());
374              for (int j = 0; j < i; ++j) {
375 <                Integer I = (Integer)(p.remove());
376 <                assertFalse(q.contains(I));
375 >                Integer x = (Integer)(p.remove());
376 >                assertFalse(q.contains(x));
377              }
378          }
379      }
380  
381 <    public void testToArray(){
382 <        ConcurrentLinkedQueue q = fullQueue(N);
383 <        Object[] o = q.toArray();
384 <        Arrays.sort(o);
385 <        for(int i = 0; i < o.length; i++)
386 <            assertEquals(o[i], q.poll());
381 >    /**
382 >     * toArray contains all elements in FIFO order
383 >     */
384 >    public void testToArray() {
385 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
386 >        Object[] o = q.toArray();
387 >        for (int i = 0; i < o.length; i++)
388 >            assertSame(o[i], q.poll());
389      }
390  
391 <    public void testToArray2(){
392 <        ConcurrentLinkedQueue q = fullQueue(N);
393 <        Integer[] ints = new Integer[N];
394 <        ints = (Integer[])q.toArray(ints);
395 <        Arrays.sort(ints);
396 <        for(int i = 0; i < ints.length; i++)
397 <            assertEquals(ints[i], q.poll());
391 >    /**
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 <    public void testIterator(){
404 <        ConcurrentLinkedQueue q = fullQueue(N);
405 <        int i = 0;
406 <        Iterator it = q.iterator();
407 <        while(it.hasNext()) {
402 >
403 >    /**
404 >     * toArray(null) throws NullPointerException
405 >     */
406 >    public void testToArray_NullArg() {
407 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
408 >        try {
409 >            q.toArray(null);
410 >            shouldThrow();
411 >        } catch (NullPointerException success) {}
412 >    }
413 >
414 >    /**
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 >        Iterator it = q.iterator();
431 >        int i;
432 >        for (i = 0; it.hasNext(); i++)
433              assertTrue(q.contains(it.next()));
434 <            ++i;
435 <        }
315 <        assertEquals(i, N);
434 >        assertEquals(i, SIZE);
435 >        assertIteratorExhausted(it);
436      }
437  
438 <    public void testIteratorOrdering() {
438 >    /**
439 >     * iterator of empty collection has no elements
440 >     */
441 >    public void testEmptyIterator() {
442 >        assertIteratorExhausted(new ConcurrentLinkedQueue().iterator());
443 >    }
444  
445 +    /**
446 +     * iterator ordering is FIFO
447 +     */
448 +    public void testIteratorOrdering() {
449          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
450 <
451 <        q.add(new Integer(1));
452 <        q.add(new Integer(2));
324 <        q.add(new Integer(3));
450 >        q.add(one);
451 >        q.add(two);
452 >        q.add(three);
453  
454          int k = 0;
455          for (Iterator it = q.iterator(); it.hasNext();) {
456 <            int i = ((Integer)(it.next())).intValue();
329 <            assertEquals("items should come out in order", ++k, i);
456 >            assertEquals(++k, it.next());
457          }
458  
459 <        assertEquals("should go through 3 elements", 3, k);
459 >        assertEquals(3, k);
460      }
461  
462 <    public void testWeaklyConsistentIteration () {
463 <
462 >    /**
463 >     * Modifications do not cause iterators to fail
464 >     */
465 >    public void testWeaklyConsistentIteration() {
466          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
467 +        q.add(one);
468 +        q.add(two);
469 +        q.add(three);
470  
471 <        q.add(new Integer(1));
472 <        q.add(new Integer(2));
473 <        q.add(new Integer(3));
342 <
343 <        try {
344 <            for (Iterator it = q.iterator(); it.hasNext();) {
345 <                q.remove();
346 <                it.next();
347 <            }
348 <        }
349 <        catch (ConcurrentModificationException e) {
350 <            fail("weakly consistent iterator; should not get CME");
471 >        for (Iterator it = q.iterator(); it.hasNext();) {
472 >            q.remove();
473 >            it.next();
474          }
475  
476          assertEquals("queue should be empty again", 0, q.size());
477      }
478  
479 <    public void testIteratorRemove () {
480 <
479 >    /**
480 >     * iterator.remove removes current element
481 >     */
482 >    public void testIteratorRemove() {
483          final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
484 <
485 <        q.add(new Integer(1));
486 <        q.add(new Integer(2));
362 <        q.add(new Integer(3));
363 <
484 >        q.add(one);
485 >        q.add(two);
486 >        q.add(three);
487          Iterator it = q.iterator();
488          it.next();
489          it.remove();
367
490          it = q.iterator();
491 <        assertEquals(it.next(), new Integer(2));
492 <        assertEquals(it.next(), new Integer(3));
491 >        assertSame(it.next(), two);
492 >        assertSame(it.next(), three);
493          assertFalse(it.hasNext());
494      }
495  
496 <
497 <    public void testToString(){
498 <        ConcurrentLinkedQueue q = fullQueue(N);
496 >    /**
497 >     * toString contains toStrings of elements
498 >     */
499 >    public void testToString() {
500 >        ConcurrentLinkedQueue q = populatedQueue(SIZE);
501          String s = q.toString();
502 <        for (int i = 0; i < N; ++i) {
503 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
502 >        for (int i = 0; i < SIZE; ++i) {
503 >            assertTrue(s.contains(String.valueOf(i)));
504          }
505 <    }        
505 >    }
506  
507 <    public void testSerialization() {
508 <        ConcurrentLinkedQueue q = fullQueue(N);
509 <        try {
510 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
511 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
512 <            out.writeObject(q);
513 <            out.close();
514 <
515 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
516 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
517 <            ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
518 <            assertEquals(q.size(), r.size());
519 <            while (!q.isEmpty())
520 <                assertEquals(q.remove(), r.remove());
397 <        } catch(Exception e){
398 <            e.printStackTrace();
399 <            fail("unexpected exception");
507 >    /**
508 >     * A deserialized serialized queue has same elements in same order
509 >     */
510 >    public void testSerialization() throws Exception {
511 >        Queue x = populatedQueue(SIZE);
512 >        Queue y = serialClone(x);
513 >
514 >        assertNotSame(x, y);
515 >        assertEquals(x.size(), y.size());
516 >        assertEquals(x.toString(), y.toString());
517 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
518 >        while (!x.isEmpty()) {
519 >            assertFalse(y.isEmpty());
520 >            assertEquals(x.remove(), y.remove());
521          }
522 +        assertTrue(y.isEmpty());
523      }
524  
525 +    /**
526 +     * remove(null), contains(null) always return false
527 +     */
528 +    public void testNeverContainsNull() {
529 +        Collection<?>[] qs = {
530 +            new ConcurrentLinkedQueue<Object>(),
531 +            populatedQueue(2),
532 +        };
533 +
534 +        for (Collection<?> q : qs) {
535 +            assertFalse(q.contains(null));
536 +            assertFalse(q.remove(null));
537 +        }
538 +    }
539   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines