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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines