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.43 by jsr166, Wed Jan 4 06:09:58 2017 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 > 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<Integer> populatedQueue(int n) {
42 <        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<Integer>();
42 >        ConcurrentLinkedQueue<Integer> q = new ConcurrentLinkedQueue<>();
43          assertTrue(q.isEmpty());
44          for (int i = 0; i < n; ++i)
45              assertTrue(q.offer(new Integer(i)));
46          assertFalse(q.isEmpty());
47          assertEquals(n, q.size());
48 +        assertEquals((Integer) 0, q.peek());
49          return q;
50      }
51  
# Line 47 | Line 61 | public class ConcurrentLinkedQueueTest e
61       */
62      public void testConstructor3() {
63          try {
64 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue((Collection)null);
64 >            new ConcurrentLinkedQueue((Collection)null);
65              shouldThrow();
66          } catch (NullPointerException success) {}
67      }
# Line 57 | Line 71 | public class ConcurrentLinkedQueueTest e
71       */
72      public void testConstructor4() {
73          try {
74 <            Integer[] ints = new Integer[SIZE];
61 <            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue(Arrays.asList(ints));
74 >            new ConcurrentLinkedQueue(Arrays.asList(new Integer[SIZE]));
75              shouldThrow();
76          } catch (NullPointerException success) {}
77      }
# Line 67 | Line 80 | public class ConcurrentLinkedQueueTest e
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 <            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));
87 >            new ConcurrentLinkedQueue(Arrays.asList(ints));
88              shouldThrow();
89          } catch (NullPointerException success) {}
90      }
# Line 108 | Line 121 | public class ConcurrentLinkedQueueTest e
121      public void testSize() {
122          ConcurrentLinkedQueue q = populatedQueue(SIZE);
123          for (int i = 0; i < SIZE; ++i) {
124 <            assertEquals(SIZE-i, q.size());
124 >            assertEquals(SIZE - i, q.size());
125              q.remove();
126          }
127          for (int i = 0; i < SIZE; ++i) {
# Line 121 | Line 134 | public class ConcurrentLinkedQueueTest e
134       * offer(null) throws NPE
135       */
136      public void testOfferNull() {
137 +        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
138          try {
125            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
139              q.offer(null);
140              shouldThrow();
141          } catch (NullPointerException success) {}
# Line 132 | Line 145 | public class ConcurrentLinkedQueueTest e
145       * add(null) throws NPE
146       */
147      public void testAddNull() {
148 +        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
149          try {
136            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
150              q.add(null);
151              shouldThrow();
152          } catch (NullPointerException success) {}
153      }
154  
142
155      /**
156       * Offer returns true
157       */
# Line 164 | Line 176 | public class ConcurrentLinkedQueueTest e
176       * addAll(null) throws NPE
177       */
178      public void testAddAll1() {
179 +        ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
180          try {
168            ConcurrentLinkedQueue q = new ConcurrentLinkedQueue();
181              q.addAll(null);
182              shouldThrow();
183          } catch (NullPointerException success) {}
# Line 175 | Line 187 | public class ConcurrentLinkedQueueTest e
187       * addAll(this) throws IAE
188       */
189      public void testAddAllSelf() {
190 +        ConcurrentLinkedQueue q = populatedQueue(SIZE);
191          try {
179            ConcurrentLinkedQueue q = populatedQueue(SIZE);
192              q.addAll(q);
193              shouldThrow();
194          } catch (IllegalArgumentException success) {}
# Line 186 | Line 198 | public class ConcurrentLinkedQueueTest e
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();
191 <            Integer[] ints = new Integer[SIZE];
192 <            q.addAll(Arrays.asList(ints));
203 >            q.addAll(Arrays.asList(new Integer[SIZE]));
204              shouldThrow();
205          } catch (NullPointerException success) {}
206      }
# Line 199 | Line 210 | public class ConcurrentLinkedQueueTest e
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 {
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);
218              q.addAll(Arrays.asList(ints));
219              shouldThrow();
220          } catch (NullPointerException success) {}
# Line 283 | Line 294 | public class ConcurrentLinkedQueueTest e
294       */
295      public void testRemoveElement() {
296          ConcurrentLinkedQueue q = populatedQueue(SIZE);
297 <        for (int i = 1; i < SIZE; i+=2) {
298 <            assertTrue(q.remove(new Integer(i)));
299 <        }
300 <        for (int i = 0; i < SIZE; i+=2) {
301 <            assertTrue(q.remove(new Integer(i)));
302 <            assertFalse(q.remove(new Integer(i+1)));
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      }
# Line 347 | Line 364 | public class ConcurrentLinkedQueueTest e
364                  assertTrue(changed);
365  
366              assertTrue(q.containsAll(p));
367 <            assertEquals(SIZE-i, q.size());
367 >            assertEquals(SIZE - i, q.size());
368              p.remove();
369          }
370      }
# Line 360 | Line 377 | public class ConcurrentLinkedQueueTest e
377              ConcurrentLinkedQueue q = populatedQueue(SIZE);
378              ConcurrentLinkedQueue p = populatedQueue(i);
379              assertTrue(q.removeAll(p));
380 <            assertEquals(SIZE-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      }
# Line 417 | Line 434 | public class ConcurrentLinkedQueueTest e
434       */
435      public void testIterator() {
436          ConcurrentLinkedQueue q = populatedQueue(SIZE);
420        int i = 0;
437          Iterator it = q.iterator();
438 <        while (it.hasNext()) {
438 >        int i;
439 >        for (i = 0; it.hasNext(); i++)
440              assertTrue(q.contains(it.next()));
424            ++i;
425        }
441          assertEquals(i, SIZE);
442 +        assertIteratorExhausted(it);
443 +    }
444 +
445 +    /**
446 +     * iterator of empty collection has no elements
447 +     */
448 +    public void testEmptyIterator() {
449 +        assertIteratorExhausted(new ConcurrentLinkedQueue().iterator());
450      }
451  
452      /**
# Line 477 | Line 500 | public class ConcurrentLinkedQueueTest e
500          assertFalse(it.hasNext());
501      }
502  
480
503      /**
504       * toString contains toStrings of elements
505       */
# Line 485 | Line 507 | public class ConcurrentLinkedQueueTest e
507          ConcurrentLinkedQueue q = populatedQueue(SIZE);
508          String s = q.toString();
509          for (int i = 0; i < SIZE; ++i) {
510 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
510 >            assertTrue(s.contains(String.valueOf(i)));
511          }
512      }
513  
# Line 493 | Line 515 | public class ConcurrentLinkedQueueTest e
515       * A deserialized serialized queue has same elements in same order
516       */
517      public void testSerialization() throws Exception {
518 <        ConcurrentLinkedQueue q = populatedQueue(SIZE);
519 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
520 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
521 <        out.writeObject(q);
522 <        out.close();
523 <
524 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
525 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
526 <        ConcurrentLinkedQueue r = (ConcurrentLinkedQueue)in.readObject();
527 <        assertEquals(q.size(), r.size());
528 <        while (!q.isEmpty())
529 <            assertEquals(q.remove(), r.remove());
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