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.23 by jsr166, Fri Nov 5 00:17:22 2010 UTC vs.
Revision 1.48 by jsr166, Mon May 28 21:19:50 2018 UTC

# Line 1 | Line 1
1   /*
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/licenses/publicdomain
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.*;
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<Integer> populatedQueue(int n) {
41 <        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>();
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)));
45          assertFalse(q.isEmpty());
46          assertEquals(n, q.size());
47 +        assertEquals((Integer) 0, q.peek());
48          return q;
49      }
50  
# Line 47 | Line 60 | public class ConcurrentLinkedQueueTest e
60       */
61      public void testConstructor3() {
62          try {
63 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
63 >            new ConcurrentLinkedQueue((Collection)null);
64              shouldThrow();
65          } catch (NullPointerException success) {}
66      }
# Line 57 | Line 70 | public class ConcurrentLinkedQueueTest e
70       */
71      public void testConstructor4() {
72          try {
73 <            Integer[] ints = new Integer[SIZE];
61 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
73 >            new ConcurrentLinkedQueue(Arrays.asList(new Integer[SIZE]));
74              shouldThrow();
75          } catch (NullPointerException success) {}
76      }
# Line 67 | Line 79 | public class ConcurrentLinkedQueueTest e
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 <            Integer[] ints = new Integer[SIZE];
72 <            for (int i = 0; i < SIZE-1; ++i)
73 <                ints[i] = new Integer(i);
74 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
86 >            new ConcurrentLinkedQueue(Arrays.asList(ints));
87              shouldThrow();
88          } catch (NullPointerException success) {}
89      }
# Line 108 | Line 120 | public class ConcurrentLinkedQueueTest e
120      public void testSize() {
121          ConcurrentLinkedQueue q = populatedQueue(SIZE);
122          for (int i = 0; i < SIZE; ++i) {
123 <            assertEquals(SIZE-i, q.size());
123 >            assertEquals(SIZE - i, q.size());
124              q.remove();
125          }
126          for (int i = 0; i < SIZE; ++i) {
# Line 121 | Line 133 | public class ConcurrentLinkedQueueTest e
133       * offer(null) throws NPE
134       */
135      public void testOfferNull() {
136 +        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
137          try {
125            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
138              q.offer(null);
139              shouldThrow();
140          } catch (NullPointerException success) {}
# Line 132 | Line 144 | public class ConcurrentLinkedQueueTest e
144       * add(null) throws NPE
145       */
146      public void testAddNull() {
147 +        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
148          try {
136            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
149              q.add(null);
150              shouldThrow();
151          } catch (NullPointerException success) {}
152      }
153  
142
154      /**
155       * Offer returns true
156       */
# Line 161 | Line 172 | public class ConcurrentLinkedQueueTest e
172      }
173  
174      /**
175 <     * addAll(null) throws NPE
175 >     * addAll(null) throws NullPointerException
176       */
177      public void testAddAll1() {
178 +        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
179          try {
168            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
180              q.addAll(null);
181              shouldThrow();
182          } catch (NullPointerException success) {}
183      }
184  
185      /**
186 <     * addAll(this) throws IAE
186 >     * addAll(this) throws IllegalArgumentException
187       */
188      public void testAddAllSelf() {
189 +        ConcurrentLinkedQueue q = populatedQueue(SIZE);
190          try {
179            ConcurrentLinkedQueue q = populatedQueue(SIZE);
191              q.addAll(q);
192              shouldThrow();
193          } catch (IllegalArgumentException success) {}
194      }
195  
196      /**
197 <     * addAll of a collection with null elements throws NPE
197 >     * addAll of a collection with null elements throws NullPointerException
198       */
199      public void testAddAll2() {
200 +        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
201          try {
202 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
191 <            Integer[] ints = new Integer[SIZE];
192 <            q.addAll(Arrays.asList(ints));
202 >            q.addAll(Arrays.asList(new Integer[SIZE]));
203              shouldThrow();
204          } catch (NullPointerException success) {}
205      }
# Line 199 | Line 209 | public class ConcurrentLinkedQueueTest e
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 {
203            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
204            Integer[] ints = new Integer[SIZE];
205            for (int i = 0; i < SIZE-1; ++i)
206                ints[i] = new Integer(i);
217              q.addAll(Arrays.asList(ints));
218              shouldThrow();
219          } catch (NullPointerException success) {}
# Line 283 | Line 293 | public class ConcurrentLinkedQueueTest e
293       */
294      public void testRemoveElement() {
295          ConcurrentLinkedQueue q = populatedQueue(SIZE);
296 <        for (int i = 1; i < SIZE; i+=2) {
297 <            assertTrue(q.remove(new Integer(i)));
298 <        }
299 <        for (int i = 0; i < SIZE; i+=2) {
300 <            assertTrue(q.remove(new Integer(i)));
301 <            assertFalse(q.remove(new Integer(i+1)));
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      }
# Line 347 | Line 363 | public class ConcurrentLinkedQueueTest e
363                  assertTrue(changed);
364  
365              assertTrue(q.containsAll(p));
366 <            assertEquals(SIZE-i, q.size());
366 >            assertEquals(SIZE - i, q.size());
367              p.remove();
368          }
369      }
# Line 360 | Line 376 | public class ConcurrentLinkedQueueTest e
376              ConcurrentLinkedQueue q = populatedQueue(SIZE);
377              ConcurrentLinkedQueue p = populatedQueue(i);
378              assertTrue(q.removeAll(p));
379 <            assertEquals(SIZE-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      }
# Line 373 | Line 389 | public class ConcurrentLinkedQueueTest e
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());
392 >        Object[] a = q.toArray();
393 >        assertSame(Object[].class, a.getClass());
394 >        for (Object o : a)
395 >            assertSame(o, q.poll());
396 >        assertTrue(q.isEmpty());
397      }
398  
399      /**
# Line 386 | Line 404 | public class ConcurrentLinkedQueueTest e
404          Integer[] ints = new Integer[SIZE];
405          Integer[] array = q.toArray(ints);
406          assertSame(ints, array);
407 <        for (int i = 0; i < ints.length; i++)
408 <            assertSame(ints[i], q.poll());
407 >        for (Integer o : ints)
408 >            assertSame(o, q.poll());
409 >        assertTrue(q.isEmpty());
410      }
411  
412      /**
# Line 417 | Line 436 | public class ConcurrentLinkedQueueTest e
436       */
437      public void testIterator() {
438          ConcurrentLinkedQueue q = populatedQueue(SIZE);
420        int i = 0;
439          Iterator it = q.iterator();
440 <        while (it.hasNext()) {
440 >        int i;
441 >        for (i = 0; it.hasNext(); i++)
442              assertTrue(q.contains(it.next()));
424            ++i;
425        }
443          assertEquals(i, SIZE);
444 +        assertIteratorExhausted(it);
445 +    }
446 +
447 +    /**
448 +     * iterator of empty collection has no elements
449 +     */
450 +    public void testEmptyIterator() {
451 +        assertIteratorExhausted(new ConcurrentLinkedQueue().iterator());
452      }
453  
454      /**
# Line 477 | Line 502 | public class ConcurrentLinkedQueueTest e
502          assertFalse(it.hasNext());
503      }
504  
480
505      /**
506       * toString contains toStrings of elements
507       */
# Line 485 | Line 509 | public class ConcurrentLinkedQueueTest e
509          ConcurrentLinkedQueue q = populatedQueue(SIZE);
510          String s = q.toString();
511          for (int i = 0; i < SIZE; ++i) {
512 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
512 >            assertTrue(s.contains(String.valueOf(i)));
513          }
514      }
515  
516      /**
517 <     * A deserialized serialized queue has same elements in same order
517 >     * A deserialized/reserialized queue has same elements in same order
518       */
519      public void testSerialization() throws Exception {
520 <        ConcurrentLinkedQueue q = populatedQueue(SIZE);
521 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
522 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
523 <        out.writeObject(q);
524 <        out.close();
525 <
526 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
527 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
528 <        ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
529 <        assertEquals(q.size(), r.size());
530 <        while (!q.isEmpty())
531 <            assertEquals(q.remove(), r.remove());
520 >        Queue x = populatedQueue(SIZE);
521 >        Queue y = serialClone(x);
522 >
523 >        assertNotSame(x, y);
524 >        assertEquals(x.size(), y.size());
525 >        assertEquals(x.toString(), y.toString());
526 >        assertTrue(Arrays.equals(x.toArray(), y.toArray()));
527 >        while (!x.isEmpty()) {
528 >            assertFalse(y.isEmpty());
529 >            assertEquals(x.remove(), y.remove());
530 >        }
531 >        assertTrue(y.isEmpty());
532      }
533  
534 +    /**
535 +     * remove(null), contains(null) always return false
536 +     */
537 +    public void testNeverContainsNull() {
538 +        Collection<?>[] qs = {
539 +            new ConcurrentLinkedQueue<Object>(),
540 +            populatedQueue(2),
541 +        };
542 +
543 +        for (Collection<?> q : qs) {
544 +            assertFalse(q.contains(null));
545 +            assertFalse(q.remove(null));
546 +        }
547 +    }
548   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines