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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines