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.51 by jsr166, Wed Jan 27 01:57:24 2021 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 >
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 JSR166TestCase.itemFor(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 >     * Items 0 ... n - 1.
39       */
40 <    private ConcurrentLinkedQueue fullQueue(int n) {
41 <        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
40 >    private static ConcurrentLinkedQueue<Item> populatedQueue(int n) {
41 >        ConcurrentLinkedQueue<Item> 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 >            mustOffer(q, i);
45          assertFalse(q.isEmpty());
46 <        assertEquals(n, q.size());
46 >        mustEqual(n, q.size());
47 >        mustEqual(0, q.peek());
48          return q;
49      }
50 <
51 <    public void testConstructor1(){
52 <        assertEquals(0, new ConcurrentLinkedQueue().size());
50 >
51 >    /**
52 >     * new queue is empty
53 >     */
54 >    public void testConstructor1() {
55 >        mustEqual(0, new ConcurrentLinkedQueue<Item>().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 <        }
49 <        catch (NullPointerException success) {}
63 >            new ConcurrentLinkedQueue<Item>((Collection<Item>)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");
57 <        }
58 <        catch (NullPointerException success) {}
73 >            new ConcurrentLinkedQueue<Item>(Arrays.asList(new Item[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 >        Item[] items = new Item[2];
83 >        items[0] = zero;
84 >        try {
85 >            new ConcurrentLinkedQueue<Item>(Arrays.asList(items));
86 >            shouldThrow();
87 >        } catch (NullPointerException success) {}
88      }
89  
90 <    public void testConstructor6(){
91 <        try {
92 <            Integer[] ints = new Integer[N];
93 <            for (int i = 0; i < N; ++i)
94 <                ints[i] = new Integer(i);
95 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
96 <            for (int i = 0; i < N; ++i)
97 <                assertEquals(ints[i], q.poll());
80 <        }
81 <        finally {}
90 >    /**
91 >     * Queue contains all elements of collection used to initialize
92 >     */
93 >    public void testConstructor6() {
94 >        Item[] items = defaultItems;
95 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>(Arrays.asList(items));
96 >        for (int i = 0; i < SIZE; ++i)
97 >            mustEqual(items[i], q.poll());
98      }
99  
100 +    /**
101 +     * isEmpty is true before add, false after
102 +     */
103      public void testEmpty() {
104 <        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
104 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
105          assertTrue(q.isEmpty());
106 <        q.add(new Integer(1));
106 >        q.add(one);
107          assertFalse(q.isEmpty());
108 <        q.add(new Integer(2));
108 >        q.add(two);
109          q.remove();
110          q.remove();
111          assertTrue(q.isEmpty());
112      }
113  
114 +    /**
115 +     * size changes when elements added and removed
116 +     */
117      public void testSize() {
118 <        ConcurrentLinkedQueue q = fullQueue(N);
119 <        for (int i = 0; i < N; ++i) {
120 <            assertEquals(N-i, q.size());
118 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
119 >        for (int i = 0; i < SIZE; ++i) {
120 >            mustEqual(SIZE - i, q.size());
121              q.remove();
122          }
123 <        for (int i = 0; i < N; ++i) {
124 <            assertEquals(i, q.size());
125 <            q.add(new Integer(i));
123 >        for (int i = 0; i < SIZE; ++i) {
124 >            mustEqual(i, q.size());
125 >            mustAdd(q, i);
126          }
127      }
128  
129 <    public void testOfferNull(){
130 <        try {
131 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
129 >    /**
130 >     * offer(null) throws NPE
131 >     */
132 >    public void testOfferNull() {
133 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
134 >        try {
135              q.offer(null);
136 <            fail("should throw NPE");
137 <        } catch (NullPointerException success) { }  
136 >            shouldThrow();
137 >        } catch (NullPointerException success) {}
138      }
139  
140 +    /**
141 +     * add(null) throws NPE
142 +     */
143 +    public void testAddNull() {
144 +        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
145 +        try {
146 +            q.add(null);
147 +            shouldThrow();
148 +        } catch (NullPointerException success) {}
149 +    }
150 +
151 +    /**
152 +     * Offer returns true
153 +     */
154      public void testOffer() {
155 <        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
156 <        assertTrue(q.offer(new Integer(0)));
157 <        assertTrue(q.offer(new Integer(1)));
155 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
156 >        assertTrue(q.offer(zero));
157 >        assertTrue(q.offer(one));
158      }
159  
160 <    public void testAdd(){
161 <        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
162 <        for (int i = 0; i < N; ++i) {
163 <            assertEquals(i, q.size());
164 <            assertTrue(q.add(new Integer(i)));
160 >    /**
161 >     * add returns true
162 >     */
163 >    public void testAdd() {
164 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
165 >        for (int i = 0; i < SIZE; ++i) {
166 >            mustEqual(i, q.size());
167 >            mustAdd(q, i);
168          }
169      }
170  
171 <    public void testAddAll1(){
171 >    /**
172 >     * addAll(null) throws NullPointerException
173 >     */
174 >    public void testAddAll1() {
175 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
176          try {
131            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
177              q.addAll(null);
178 <            fail("Cannot add null collection");
179 <        }
135 <        catch (NullPointerException success) {}
178 >            shouldThrow();
179 >        } catch (NullPointerException success) {}
180      }
181 <    public void testAddAll2(){
181 >
182 >    /**
183 >     * addAll(this) throws IllegalArgumentException
184 >     */
185 >    public void testAddAllSelf() {
186 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
187          try {
188 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
189 <            Integer[] ints = new Integer[N];
190 <            q.addAll(Arrays.asList(ints));
142 <            fail("Cannot add null elements");
143 <        }
144 <        catch (NullPointerException success) {}
188 >            q.addAll(q);
189 >            shouldThrow();
190 >        } catch (IllegalArgumentException success) {}
191      }
192 <    public void testAddAll3(){
192 >
193 >    /**
194 >     * addAll of a collection with null elements throws NullPointerException
195 >     */
196 >    public void testAddAll2() {
197 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
198          try {
199 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
200 <            Integer[] ints = new Integer[N];
201 <            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) {}
199 >            q.addAll(Arrays.asList(new Item[SIZE]));
200 >            shouldThrow();
201 >        } catch (NullPointerException success) {}
202      }
203  
204 <    public void testAddAll5(){
205 <        try {
206 <            Integer[] empty = new Integer[0];
207 <            Integer[] ints = new Integer[N];
208 <            for (int i = 0; i < N; ++i)
209 <                ints[i] = new Integer(i);
210 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
211 <            assertFalse(q.addAll(Arrays.asList(empty)));
212 <            assertTrue(q.addAll(Arrays.asList(ints)));
213 <            for (int i = 0; i < N; ++i)
214 <                assertEquals(ints[i], q.poll());
215 <        }
216 <        finally {}
204 >    /**
205 >     * addAll of a collection with any null elements throws NPE after
206 >     * possibly adding some elements
207 >     */
208 >    public void testAddAll3() {
209 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
210 >        Item[] items = new Item[2];
211 >        items[0] = zero;
212 >        try {
213 >            q.addAll(Arrays.asList(items));
214 >            shouldThrow();
215 >        } catch (NullPointerException success) {}
216 >    }
217 >
218 >    /**
219 >     * Queue contains all elements, in traversal order, of successful addAll
220 >     */
221 >    public void testAddAll5() {
222 >        Item[] empty = new Item[0];
223 >        Item[] items = defaultItems;
224 >        ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
225 >        assertFalse(q.addAll(Arrays.asList(empty)));
226 >        assertTrue(q.addAll(Arrays.asList(items)));
227 >        for (int i = 0; i < SIZE; ++i)
228 >            mustEqual(items[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<Item> q = populatedQueue(SIZE);
236 >        for (int i = 0; i < SIZE; ++i) {
237 >            mustEqual(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<Item> q = populatedQueue(SIZE);
247 >        for (int i = 0; i < SIZE; ++i) {
248 >            mustEqual(i, q.peek());
249 >            mustEqual(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<Item> q = populatedQueue(SIZE);
261 >        for (int i = 0; i < SIZE; ++i) {
262 >            mustEqual(i, q.element());
263 >            mustEqual(i, q.poll());
264          }
265          try {
266              q.element();
267 <            fail("no such element");
268 <        }
202 <        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<Item> q = populatedQueue(SIZE);
276 >        for (int i = 0; i < SIZE; ++i) {
277 >            mustEqual(i, q.remove());
278          }
279          try {
280              q.remove();
281 <            fail("remove should throw");
282 <        } catch (NoSuchElementException success){
283 <        }  
284 <    }
285 <
286 <    public void testRemoveElement(){
287 <        ConcurrentLinkedQueue q = fullQueue(N);
288 <        for (int i = 1; i < N; i+=2) {
289 <            assertTrue(q.remove(new Integer(i)));
290 <        }
291 <        for (int i = 0; i < N; i+=2) {
292 <            assertTrue(q.remove(new Integer(i)));
293 <            assertFalse(q.remove(new Integer(i+1)));
294 <        }
295 <        assert(q.isEmpty());
296 <    }
297 <        
298 <    public void testContains(){
299 <        ConcurrentLinkedQueue q = fullQueue(N);
300 <        for (int i = 0; i < N; ++i) {
301 <            assertTrue(q.contains(new Integer(i)));
281 >            shouldThrow();
282 >        } catch (NoSuchElementException success) {}
283 >    }
284 >
285 >    /**
286 >     * remove(x) removes x and returns true if present
287 >     */
288 >    public void testRemoveElement() {
289 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
290 >        for (int i = 1; i < SIZE; i += 2) {
291 >            mustContain(q, i);
292 >            mustRemove(q, i);
293 >            mustNotContain(q, i);
294 >            mustContain(q, i - 1);
295 >        }
296 >        for (int i = 0; i < SIZE; i += 2) {
297 >            mustContain(q, i);
298 >            mustRemove(q, i);
299 >            mustNotContain(q, i);
300 >            mustNotRemove(q, i + 1);
301 >            mustNotContain(q, i + 1);
302 >        }
303 >        assertTrue(q.isEmpty());
304 >    }
305 >
306 >    /**
307 >     * contains(x) reports true when elements added but not yet removed
308 >     */
309 >    public void testContains() {
310 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
311 >        for (int i = 0; i < SIZE; ++i) {
312 >            mustContain(q, i);
313              q.poll();
314 <            assertFalse(q.contains(new Integer(i)));
314 >            mustNotContain(q, 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<Item> q = populatedQueue(SIZE);
323          q.clear();
324          assertTrue(q.isEmpty());
325 <        assertEquals(0, q.size());
326 <        q.add(new Integer(1));
325 >        mustEqual(0, q.size());
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);
334 <        ConcurrentLinkedQueue p = new ConcurrentLinkedQueue();
335 <        for (int i = 0; i < N; ++i) {
332 >    /**
333 >     * containsAll(c) is true when c contains a subset of elements
334 >     */
335 >    public void testContainsAll() {
336 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
337 >        ConcurrentLinkedQueue<Item> p = new ConcurrentLinkedQueue<>();
338 >        for (int i = 0; i < SIZE; ++i) {
339              assertTrue(q.containsAll(p));
340              assertFalse(p.containsAll(q));
341 <            p.add(new Integer(i));
341 >            mustAdd(p, i);
342          }
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<Item> q = populatedQueue(SIZE);
351 >        ConcurrentLinkedQueue<Item> 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 268 | Line 357 | public class ConcurrentLinkedQueueTest e
357                  assertTrue(changed);
358  
359              assertTrue(q.containsAll(p));
360 <            assertEquals(N-i, q.size());
360 >            mustEqual(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<Item> q = populatedQueue(SIZE);
371 >            ConcurrentLinkedQueue<Item> p = populatedQueue(i);
372              assertTrue(q.removeAll(p));
373 <            assertEquals(N-i, q.size());
373 >            mustEqual(SIZE - i, q.size());
374              for (int j = 0; j < i; ++j) {
375 <                Integer I = (Integer)(p.remove());
376 <                assertFalse(q.contains(I));
375 >                Item x = 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());
387 <    }
388 <
389 <    public void testToArray2(){
390 <        ConcurrentLinkedQueue q = fullQueue(N);
299 <        Integer[] ints = new Integer[N];
300 <        ints = (Integer[])q.toArray(ints);
301 <        Arrays.sort(ints);
302 <        for(int i = 0; i < ints.length; i++)
303 <            assertEquals(ints[i], q.poll());
304 <    }
305 <    
306 <    public void testIterator(){
307 <        ConcurrentLinkedQueue q = fullQueue(N);
308 <        int i = 0;
309 <        Iterator it = q.iterator();
310 <        while(it.hasNext()) {
311 <            assertTrue(q.contains(it.next()));
312 <            ++i;
313 <        }
314 <        assertEquals(i, N);
381 >    /**
382 >     * toArray contains all elements in FIFO order
383 >     */
384 >    public void testToArray() {
385 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
386 >        Object[] a = q.toArray();
387 >        assertSame(Object[].class, a.getClass());
388 >        for (Object o : a)
389 >            assertSame(o, q.poll());
390 >        assertTrue(q.isEmpty());
391      }
392  
393 <    public void testIteratorOrdering() {
394 <
395 <        final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
396 <
397 <        q.add(new Integer(1));
398 <        q.add(new Integer(2));
399 <        q.add(new Integer(3));
393 >    /**
394 >     * toArray(a) contains all elements in FIFO order
395 >     */
396 >    public void testToArray2() {
397 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
398 >        Item[] items = new Item[SIZE];
399 >        Item[] array = q.toArray(items);
400 >        assertSame(items, array);
401 >        for (Item o : items)
402 >            assertSame(o, q.poll());
403 >        assertTrue(q.isEmpty());
404 >    }
405  
406 <        int k = 0;
407 <        for (Iterator it = q.iterator(); it.hasNext();) {
408 <            int i = ((Integer)(it.next())).intValue();
409 <            assertEquals("items should come out in order", ++k, i);
410 <        }
406 >    /**
407 >     * toArray(null) throws NullPointerException
408 >     */
409 >    public void testToArray_NullArg() {
410 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
411 >        try {
412 >            q.toArray((Object[])null);
413 >            shouldThrow();
414 >        } catch (NullPointerException success) {}
415 >    }
416  
417 <        assertEquals("should go through 3 elements", 3, k);
417 >    /**
418 >     * toArray(incompatible array type) throws ArrayStoreException
419 >     */
420 >    public void testToArray1_BadArg() {
421 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
422 >        try {
423 >            q.toArray(new String[10]);
424 >            shouldThrow();
425 >        } catch (ArrayStoreException success) {}
426      }
427  
428 <    public void testWeaklyConsistentIteration () {
428 >    /**
429 >     * iterator iterates through all elements
430 >     */
431 >    public void testIterator() {
432 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
433 >        Iterator<? extends Item> it = q.iterator();
434 >        int i;
435 >        for (i = 0; it.hasNext(); i++)
436 >            mustContain(q, it.next());
437 >        mustEqual(i, SIZE);
438 >        assertIteratorExhausted(it);
439 >    }
440  
441 <        final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
441 >    /**
442 >     * iterator of empty collection has no elements
443 >     */
444 >    public void testEmptyIterator() {
445 >        assertIteratorExhausted(new ConcurrentLinkedQueue<>().iterator());
446 >    }
447  
448 <        q.add(new Integer(1));
449 <        q.add(new Integer(2));
450 <        q.add(new Integer(3));
448 >    /**
449 >     * iterator ordering is FIFO
450 >     */
451 >    public void testIteratorOrdering() {
452 >        final ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
453 >        q.add(one);
454 >        q.add(two);
455 >        q.add(three);
456  
457 <        try {
458 <            for (Iterator it = q.iterator(); it.hasNext();) {
459 <                q.remove();
345 <                it.next();
346 <            }
347 <        }
348 <        catch (ConcurrentModificationException e) {
349 <            fail("weakly consistent iterator; should not get CME");
457 >        int k = 0;
458 >        for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
459 >            mustEqual(++k, it.next());
460          }
461  
462 <        assertEquals("queue should be empty again", 0, q.size());
462 >        mustEqual(3, k);
463      }
464  
465 <    public void testIteratorRemove () {
465 >    /**
466 >     * Modifications do not cause iterators to fail
467 >     */
468 >    public void testWeaklyConsistentIteration() {
469 >        final ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
470 >        q.add(one);
471 >        q.add(two);
472 >        q.add(three);
473  
474 <        final ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
474 >        for (Iterator<? extends Item> it = q.iterator(); it.hasNext();) {
475 >            q.remove();
476 >            it.next();
477 >        }
478  
479 <        q.add(new Integer(1));
480 <        q.add(new Integer(2));
361 <        q.add(new Integer(3));
479 >        mustEqual(0, q.size());
480 >    }
481  
482 <        Iterator it = q.iterator();
482 >    /**
483 >     * iterator.remove removes current element
484 >     */
485 >    public void testIteratorRemove() {
486 >        final ConcurrentLinkedQueue<Item> q = new ConcurrentLinkedQueue<>();
487 >        q.add(one);
488 >        q.add(two);
489 >        q.add(three);
490 >        Iterator<? extends Item> it = q.iterator();
491          it.next();
492          it.remove();
366
493          it = q.iterator();
494 <        assertEquals(it.next(), new Integer(2));
495 <        assertEquals(it.next(), new Integer(3));
494 >        assertSame(it.next(), two);
495 >        assertSame(it.next(), three);
496          assertFalse(it.hasNext());
497      }
498  
499 <
500 <    public void testToString(){
501 <        ConcurrentLinkedQueue q = fullQueue(N);
499 >    /**
500 >     * toString contains toStrings of elements
501 >     */
502 >    public void testToString() {
503 >        ConcurrentLinkedQueue<Item> q = populatedQueue(SIZE);
504          String s = q.toString();
505 <        for (int i = 0; i < N; ++i) {
506 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
505 >        for (int i = 0; i < SIZE; ++i) {
506 >            assertTrue(s.contains(String.valueOf(i)));
507          }
508 <    }        
508 >    }
509  
510 +    /**
511 +     * A deserialized/reserialized queue has same elements in same order
512 +     */
513 +    public void testSerialization() throws Exception {
514 +        Queue<Item> x = populatedQueue(SIZE);
515 +        Queue<Item> y = serialClone(x);
516 +
517 +        assertNotSame(x, y);
518 +        mustEqual(x.size(), y.size());
519 +        mustEqual(x.toString(), y.toString());
520 +        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
521 +        while (!x.isEmpty()) {
522 +            assertFalse(y.isEmpty());
523 +            mustEqual(x.remove(), y.remove());
524 +        }
525 +        assertTrue(y.isEmpty());
526 +    }
527 +
528 +    /**
529 +     * remove(null), contains(null) always return false
530 +     */
531 +    public void testNeverContainsNull() {
532 +        Collection<?>[] qs = {
533 +            new ConcurrentLinkedQueue<>(),
534 +            populatedQueue(2),
535 +        };
536 +
537 +        for (Collection<?> q : qs) {
538 +            assertFalse(q.contains(null));
539 +            assertFalse(q.remove(null));
540 +        }
541 +    }
542   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines