ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentLinkedDequeTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentLinkedDequeTest.java (file contents):
Revision 1.10 by jsr166, Tue Feb 21 01:54:03 2012 UTC vs.
Revision 1.25 by jsr166, Wed Jan 4 06:09:58 2017 UTC

# Line 6 | Line 6
6   * Pat Fisher, Mike Judd.
7   */
8  
9 import junit.framework.*;
9   import java.util.Arrays;
10   import java.util.Collection;
11 + import java.util.Deque;
12   import java.util.Iterator;
13   import java.util.NoSuchElementException;
14   import java.util.Queue;
15   import java.util.Random;
16   import java.util.concurrent.ConcurrentLinkedDeque;
17  
18 + import junit.framework.Test;
19 + import junit.framework.TestSuite;
20 +
21   public class ConcurrentLinkedDequeTest extends JSR166TestCase {
22  
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run(suite());
24 >        main(suite(), args);
25      }
26  
27      public static Test suite() {
28 <        return new TestSuite(ConcurrentLinkedDequeTest.class);
28 >        class Implementation implements CollectionImplementation {
29 >            public Class<?> klazz() { return ConcurrentLinkedDeque.class; }
30 >            public Collection emptyCollection() { return new ConcurrentLinkedDeque(); }
31 >            public Object makeElement(int i) { return i; }
32 >            public boolean isConcurrent() { return true; }
33 >            public boolean permitsNulls() { return false; }
34 >        }
35 >        return newTestSuite(ConcurrentLinkedDequeTest.class,
36 >                            CollectionTest.testSuite(new Implementation()));
37      }
38  
39      /**
40 <     * Creates a deque of given size containing consecutive
41 <     * Integers 0 ... n.
40 >     * Returns a new deque of given size containing consecutive
41 >     * Integers 0 ... n - 1.
42       */
43      private ConcurrentLinkedDeque<Integer> populatedDeque(int n) {
44 <        ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<Integer>();
44 >        ConcurrentLinkedDeque<Integer> q = new ConcurrentLinkedDeque<>();
45          assertTrue(q.isEmpty());
46          for (int i = 0; i < n; ++i)
47              assertTrue(q.offer(new Integer(i)));
48          assertFalse(q.isEmpty());
49          assertEquals(n, q.size());
50 +        assertEquals((Integer) 0, q.peekFirst());
51 +        assertEquals((Integer) (n - 1), q.peekLast());
52          return q;
53      }
54  
# Line 52 | Line 65 | public class ConcurrentLinkedDequeTest e
65       */
66      public void testConstructor3() {
67          try {
68 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque((Collection)null);
68 >            new ConcurrentLinkedDeque((Collection)null);
69              shouldThrow();
70          } catch (NullPointerException success) {}
71      }
# Line 62 | Line 75 | public class ConcurrentLinkedDequeTest e
75       */
76      public void testConstructor4() {
77          try {
78 <            Integer[] ints = new Integer[SIZE];
66 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
78 >            new ConcurrentLinkedDeque(Arrays.asList(new Integer[SIZE]));
79              shouldThrow();
80          } catch (NullPointerException success) {}
81      }
# Line 72 | Line 84 | public class ConcurrentLinkedDequeTest e
84       * Initializing from Collection with some null elements throws NPE
85       */
86      public void testConstructor5() {
87 +        Integer[] ints = new Integer[SIZE];
88 +        for (int i = 0; i < SIZE - 1; ++i)
89 +            ints[i] = new Integer(i);
90          try {
91 <            Integer[] ints = new Integer[SIZE];
77 <            for (int i = 0; i < SIZE-1; ++i)
78 <                ints[i] = new Integer(i);
79 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque(Arrays.asList(ints));
91 >            new ConcurrentLinkedDeque(Arrays.asList(ints));
92              shouldThrow();
93          } catch (NullPointerException success) {}
94      }
# Line 113 | Line 125 | public class ConcurrentLinkedDequeTest e
125      public void testSize() {
126          ConcurrentLinkedDeque q = populatedDeque(SIZE);
127          for (int i = 0; i < SIZE; ++i) {
128 <            assertEquals(SIZE-i, q.size());
128 >            assertEquals(SIZE - i, q.size());
129              q.remove();
130          }
131          for (int i = 0; i < SIZE; ++i) {
# Line 126 | Line 138 | public class ConcurrentLinkedDequeTest e
138       * push(null) throws NPE
139       */
140      public void testPushNull() {
141 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
142          try {
130            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
143              q.push(null);
144              shouldThrow();
145          } catch (NullPointerException success) {}
# Line 161 | Line 173 | public class ConcurrentLinkedDequeTest e
173       * offer(null) throws NPE
174       */
175      public void testOfferNull() {
176 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
177          try {
165            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
178              q.offer(null);
179              shouldThrow();
180          } catch (NullPointerException success) {}
# Line 172 | Line 184 | public class ConcurrentLinkedDequeTest e
184       * offerFirst(null) throws NPE
185       */
186      public void testOfferFirstNull() {
187 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
188          try {
176            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
189              q.offerFirst(null);
190              shouldThrow();
191          } catch (NullPointerException success) {}
# Line 183 | Line 195 | public class ConcurrentLinkedDequeTest e
195       * offerLast(null) throws NPE
196       */
197      public void testOfferLastNull() {
198 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
199          try {
187            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
200              q.offerLast(null);
201              shouldThrow();
202          } catch (NullPointerException success) {}
# Line 227 | Line 239 | public class ConcurrentLinkedDequeTest e
239       * add(null) throws NPE
240       */
241      public void testAddNull() {
242 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
243          try {
231            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
244              q.add(null);
245              shouldThrow();
246          } catch (NullPointerException success) {}
# Line 238 | Line 250 | public class ConcurrentLinkedDequeTest e
250       * addFirst(null) throws NPE
251       */
252      public void testAddFirstNull() {
253 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
254          try {
242            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
255              q.addFirst(null);
256              shouldThrow();
257          } catch (NullPointerException success) {}
# Line 249 | Line 261 | public class ConcurrentLinkedDequeTest e
261       * addLast(null) throws NPE
262       */
263      public void testAddLastNull() {
264 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
265          try {
253            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
266              q.addLast(null);
267              shouldThrow();
268          } catch (NullPointerException success) {}
# Line 293 | Line 305 | public class ConcurrentLinkedDequeTest e
305       * addAll(null) throws NPE
306       */
307      public void testAddAll1() {
308 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
309          try {
297            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
310              q.addAll(null);
311              shouldThrow();
312          } catch (NullPointerException success) {}
# Line 304 | Line 316 | public class ConcurrentLinkedDequeTest e
316       * addAll(this) throws IAE
317       */
318      public void testAddAllSelf() {
319 +        ConcurrentLinkedDeque q = populatedDeque(SIZE);
320          try {
308            ConcurrentLinkedDeque q = populatedDeque(SIZE);
321              q.addAll(q);
322              shouldThrow();
323          } catch (IllegalArgumentException success) {}
# Line 315 | Line 327 | public class ConcurrentLinkedDequeTest e
327       * addAll of a collection with null elements throws NPE
328       */
329      public void testAddAll2() {
330 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
331          try {
332 <            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
320 <            Integer[] ints = new Integer[SIZE];
321 <            q.addAll(Arrays.asList(ints));
332 >            q.addAll(Arrays.asList(new Integer[SIZE]));
333              shouldThrow();
334          } catch (NullPointerException success) {}
335      }
# Line 328 | Line 339 | public class ConcurrentLinkedDequeTest e
339       * possibly adding some elements
340       */
341      public void testAddAll3() {
342 +        ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
343 +        Integer[] ints = new Integer[SIZE];
344 +        for (int i = 0; i < SIZE - 1; ++i)
345 +            ints[i] = new Integer(i);
346          try {
332            ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
333            Integer[] ints = new Integer[SIZE];
334            for (int i = 0; i < SIZE-1; ++i)
335                ints[i] = new Integer(i);
347              q.addAll(Arrays.asList(ints));
348              shouldThrow();
349          } catch (NullPointerException success) {}
# Line 369 | Line 380 | public class ConcurrentLinkedDequeTest e
380       */
381      public void testPollLast() {
382          ConcurrentLinkedDeque q = populatedDeque(SIZE);
383 <        for (int i = SIZE-1; i >= 0; --i) {
383 >        for (int i = SIZE - 1; i >= 0; --i) {
384              assertEquals(i, q.pollLast());
385          }
386          assertNull(q.pollLast());
# Line 434 | Line 445 | public class ConcurrentLinkedDequeTest e
445       */
446      public void testRemoveElement() {
447          ConcurrentLinkedDeque q = populatedDeque(SIZE);
448 <        for (int i = 1; i < SIZE; i+=2) {
448 >        for (int i = 1; i < SIZE; i += 2) {
449              assertTrue(q.contains(i));
450              assertTrue(q.remove(i));
451              assertFalse(q.contains(i));
452 <            assertTrue(q.contains(i-1));
452 >            assertTrue(q.contains(i - 1));
453          }
454 <        for (int i = 0; i < SIZE; i+=2) {
454 >        for (int i = 0; i < SIZE; i += 2) {
455              assertTrue(q.contains(i));
456              assertTrue(q.remove(i));
457              assertFalse(q.contains(i));
458 <            assertFalse(q.remove(i+1));
459 <            assertFalse(q.contains(i+1));
458 >            assertFalse(q.remove(i + 1));
459 >            assertFalse(q.contains(i + 1));
460          }
461          assertTrue(q.isEmpty());
462      }
# Line 469 | Line 480 | public class ConcurrentLinkedDequeTest e
480       */
481      public void testPeekLast() {
482          ConcurrentLinkedDeque q = populatedDeque(SIZE);
483 <        for (int i = SIZE-1; i >= 0; --i) {
483 >        for (int i = SIZE - 1; i >= 0; --i) {
484              assertEquals(i, q.peekLast());
485              assertEquals(i, q.pollLast());
486              assertTrue(q.peekLast() == null ||
# Line 498 | Line 509 | public class ConcurrentLinkedDequeTest e
509       */
510      public void testLastElement() {
511          ConcurrentLinkedDeque q = populatedDeque(SIZE);
512 <        for (int i = SIZE-1; i >= 0; --i) {
512 >        for (int i = SIZE - 1; i >= 0; --i) {
513              assertEquals(i, q.getLast());
514              assertEquals(i, q.pollLast());
515          }
# Line 544 | Line 555 | public class ConcurrentLinkedDequeTest e
555       */
556      public void testRemoveFirstOccurrence() {
557          ConcurrentLinkedDeque q = populatedDeque(SIZE);
558 <        for (int i = 1; i < SIZE; i+=2) {
558 >        for (int i = 1; i < SIZE; i += 2) {
559              assertTrue(q.removeFirstOccurrence(new Integer(i)));
560          }
561 <        for (int i = 0; i < SIZE; i+=2) {
561 >        for (int i = 0; i < SIZE; i += 2) {
562              assertTrue(q.removeFirstOccurrence(new Integer(i)));
563 <            assertFalse(q.removeFirstOccurrence(new Integer(i+1)));
563 >            assertFalse(q.removeFirstOccurrence(new Integer(i + 1)));
564          }
565          assertTrue(q.isEmpty());
566      }
# Line 559 | Line 570 | public class ConcurrentLinkedDequeTest e
570       */
571      public void testRemoveLastOccurrence() {
572          ConcurrentLinkedDeque q = populatedDeque(SIZE);
573 <        for (int i = 1; i < SIZE; i+=2) {
573 >        for (int i = 1; i < SIZE; i += 2) {
574              assertTrue(q.removeLastOccurrence(new Integer(i)));
575          }
576 <        for (int i = 0; i < SIZE; i+=2) {
576 >        for (int i = 0; i < SIZE; i += 2) {
577              assertTrue(q.removeLastOccurrence(new Integer(i)));
578 <            assertFalse(q.removeLastOccurrence(new Integer(i+1)));
578 >            assertFalse(q.removeLastOccurrence(new Integer(i + 1)));
579          }
580          assertTrue(q.isEmpty());
581      }
# Line 623 | Line 634 | public class ConcurrentLinkedDequeTest e
634                  assertTrue(changed);
635  
636              assertTrue(q.containsAll(p));
637 <            assertEquals(SIZE-i, q.size());
637 >            assertEquals(SIZE - i, q.size());
638              p.remove();
639          }
640      }
# Line 636 | Line 647 | public class ConcurrentLinkedDequeTest e
647              ConcurrentLinkedDeque q = populatedDeque(SIZE);
648              ConcurrentLinkedDeque p = populatedDeque(i);
649              assertTrue(q.removeAll(p));
650 <            assertEquals(SIZE-i, q.size());
650 >            assertEquals(SIZE - i, q.size());
651              for (int j = 0; j < i; ++j) {
652 <                Integer I = (Integer)(p.remove());
653 <                assertFalse(q.contains(I));
652 >                Integer x = (Integer)(p.remove());
653 >                assertFalse(q.contains(x));
654              }
655          }
656      }
# Line 693 | Line 704 | public class ConcurrentLinkedDequeTest e
704       */
705      public void testIterator() {
706          ConcurrentLinkedDeque q = populatedDeque(SIZE);
696        int i = 0;
707          Iterator it = q.iterator();
708 <        while (it.hasNext()) {
708 >        int i;
709 >        for (i = 0; it.hasNext(); i++)
710              assertTrue(q.contains(it.next()));
700            ++i;
701        }
711          assertEquals(i, SIZE);
712 +        assertIteratorExhausted(it);
713 +    }
714 +
715 +    /**
716 +     * iterator of empty collection has no elements
717 +     */
718 +    public void testEmptyIterator() {
719 +        Deque c = new ConcurrentLinkedDeque();
720 +        assertIteratorExhausted(c.iterator());
721 +        assertIteratorExhausted(c.descendingIterator());
722      }
723  
724      /**
# Line 744 | Line 763 | public class ConcurrentLinkedDequeTest e
763          final Random rng = new Random();
764          for (int iters = 0; iters < 100; ++iters) {
765              int max = rng.nextInt(5) + 2;
766 <            int split = rng.nextInt(max-1) + 1;
766 >            int split = rng.nextInt(max - 1) + 1;
767              for (int j = 1; j <= max; ++j)
768                  q.add(new Integer(j));
769              Iterator it = q.iterator();
770              for (int j = 1; j <= split; ++j)
771                  assertEquals(it.next(), new Integer(j));
772              it.remove();
773 <            assertEquals(it.next(), new Integer(split+1));
773 >            assertEquals(it.next(), new Integer(split + 1));
774              for (int j = 1; j <= split; ++j)
775                  q.remove(new Integer(j));
776              it = q.iterator();
777 <            for (int j = split+1; j <= max; ++j) {
777 >            for (int j = split + 1; j <= max; ++j) {
778                  assertEquals(it.next(), new Integer(j));
779                  it.remove();
780              }
# Line 812 | Line 831 | public class ConcurrentLinkedDequeTest e
831          final Random rng = new Random();
832          for (int iters = 0; iters < 100; ++iters) {
833              int max = rng.nextInt(5) + 2;
834 <            int split = rng.nextInt(max-1) + 1;
834 >            int split = rng.nextInt(max - 1) + 1;
835              for (int j = max; j >= 1; --j)
836                  q.add(new Integer(j));
837              Iterator it = q.descendingIterator();
838              for (int j = 1; j <= split; ++j)
839                  assertEquals(it.next(), new Integer(j));
840              it.remove();
841 <            assertEquals(it.next(), new Integer(split+1));
841 >            assertEquals(it.next(), new Integer(split + 1));
842              for (int j = 1; j <= split; ++j)
843                  q.remove(new Integer(j));
844              it = q.descendingIterator();
845 <            for (int j = split+1; j <= max; ++j) {
845 >            for (int j = split + 1; j <= max; ++j) {
846                  assertEquals(it.next(), new Integer(j));
847                  it.remove();
848              }
# Line 850 | Line 869 | public class ConcurrentLinkedDequeTest e
869          Queue x = populatedDeque(SIZE);
870          Queue y = serialClone(x);
871  
872 <        assertTrue(x != y);
872 >        assertNotSame(x, y);
873          assertEquals(x.size(), y.size());
874          assertEquals(x.toString(), y.toString());
875          assertTrue(Arrays.equals(x.toArray(), y.toArray()));
# Line 861 | Line 880 | public class ConcurrentLinkedDequeTest e
880          assertTrue(y.isEmpty());
881      }
882  
883 +    /**
884 +     * contains(null) always return false.
885 +     * remove(null) always throws NullPointerException.
886 +     */
887 +    public void testNeverContainsNull() {
888 +        Deque<?>[] qs = {
889 +            new ConcurrentLinkedDeque<Object>(),
890 +            populatedDeque(2),
891 +        };
892 +
893 +        for (Deque<?> q : qs) {
894 +            assertFalse(q.contains(null));
895 +            try {
896 +                assertFalse(q.remove(null));
897 +                shouldThrow();
898 +            } catch (NullPointerException success) {}
899 +            try {
900 +                assertFalse(q.removeFirstOccurrence(null));
901 +                shouldThrow();
902 +            } catch (NullPointerException success) {}
903 +            try {
904 +                assertFalse(q.removeLastOccurrence(null));
905 +                shouldThrow();
906 +            } catch (NullPointerException success) {}
907 +        }
908 +    }
909   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines