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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines