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

Comparing jsr166/src/test/tck/ConcurrentSkipListSetTest.java (file contents):
Revision 1.23 by jsr166, Tue May 31 16:16:23 2011 UTC vs.
Revision 1.40 by jsr166, Sat May 23 00:53:08 2015 UTC

# Line 4 | Line 4
4   * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 import junit.framework.*;
7   import java.util.Arrays;
8   import java.util.BitSet;
9   import java.util.Collection;
# Line 17 | Line 16 | import java.util.Set;
16   import java.util.SortedSet;
17   import java.util.concurrent.ConcurrentSkipListSet;
18  
19 + import junit.framework.Test;
20 + import junit.framework.TestSuite;
21 +
22   public class ConcurrentSkipListSetTest extends JSR166TestCase {
23      public static void main(String[] args) {
24 <        junit.textui.TestRunner.run(suite());
24 >        main(suite(), args);
25      }
26      public static Test suite() {
27          return new TestSuite(ConcurrentSkipListSetTest.class);
# Line 32 | Line 34 | public class ConcurrentSkipListSetTest e
34      }
35  
36      /**
37 <     * Create a set of given size containing consecutive
37 >     * Returns a new set of given size containing consecutive
38       * Integers 0 ... n.
39       */
40      private ConcurrentSkipListSet<Integer> populatedSet(int n) {
41          ConcurrentSkipListSet<Integer> q =
42              new ConcurrentSkipListSet<Integer>();
43          assertTrue(q.isEmpty());
44 <        for (int i = n-1; i >= 0; i-=2)
44 >        for (int i = n-1; i >= 0; i -= 2)
45              assertTrue(q.add(new Integer(i)));
46 <        for (int i = (n & 1); i < n; i+=2)
46 >        for (int i = (n & 1); i < n; i += 2)
47              assertTrue(q.add(new Integer(i)));
48          assertFalse(q.isEmpty());
49          assertEquals(n, q.size());
# Line 49 | Line 51 | public class ConcurrentSkipListSetTest e
51      }
52  
53      /**
54 <     * Create set of first 5 ints
54 >     * Returns a new set of first 5 ints.
55       */
56      private ConcurrentSkipListSet set5() {
57          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
# Line 75 | Line 77 | public class ConcurrentSkipListSetTest e
77       */
78      public void testConstructor3() {
79          try {
80 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null);
80 >            new ConcurrentSkipListSet((Collection)null);
81              shouldThrow();
82          } catch (NullPointerException success) {}
83      }
# Line 85 | Line 87 | public class ConcurrentSkipListSetTest e
87       */
88      public void testConstructor4() {
89          try {
90 <            Integer[] ints = new Integer[SIZE];
89 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
90 >            new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE]));
91              shouldThrow();
92          } catch (NullPointerException success) {}
93      }
# Line 95 | Line 96 | public class ConcurrentSkipListSetTest e
96       * Initializing from Collection with some null elements throws NPE
97       */
98      public void testConstructor5() {
99 +        Integer[] ints = new Integer[SIZE];
100 +        for (int i = 0; i < SIZE - 1; ++i)
101 +            ints[i] = new Integer(i);
102          try {
103 <            Integer[] ints = new Integer[SIZE];
100 <            for (int i = 0; i < SIZE-1; ++i)
101 <                ints[i] = new Integer(i);
102 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
103 >            new ConcurrentSkipListSet(Arrays.asList(ints));
104              shouldThrow();
105          } catch (NullPointerException success) {}
106      }
# Line 127 | Line 128 | public class ConcurrentSkipListSetTest e
128          for (int i = 0; i < SIZE; ++i)
129              ints[i] = new Integer(i);
130          q.addAll(Arrays.asList(ints));
131 <        for (int i = SIZE-1; i >= 0; --i)
131 >        for (int i = SIZE - 1; i >= 0; --i)
132              assertEquals(ints[i], q.pollFirst());
133      }
134  
# Line 151 | Line 152 | public class ConcurrentSkipListSetTest e
152      public void testSize() {
153          ConcurrentSkipListSet q = populatedSet(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
155 <            assertEquals(SIZE-i, q.size());
155 >            assertEquals(SIZE - i, q.size());
156              q.pollFirst();
157          }
158          for (int i = 0; i < SIZE; ++i) {
# Line 164 | Line 165 | public class ConcurrentSkipListSetTest e
165       * add(null) throws NPE
166       */
167      public void testAddNull() {
168 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
169          try {
168            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
170              q.add(null);
171              shouldThrow();
172          } catch (NullPointerException success) {}
# Line 193 | Line 194 | public class ConcurrentSkipListSetTest e
194       * Add of non-Comparable throws CCE
195       */
196      public void testAddNonComparable() {
197 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
198          try {
197            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
198            q.add(new Object());
199              q.add(new Object());
200              q.add(new Object());
201              shouldThrow();
# Line 206 | Line 206 | public class ConcurrentSkipListSetTest e
206       * addAll(null) throws NPE
207       */
208      public void testAddAll1() {
209 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
210          try {
210            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
211              q.addAll(null);
212              shouldThrow();
213          } catch (NullPointerException success) {}
# Line 217 | Line 217 | public class ConcurrentSkipListSetTest e
217       * addAll of a collection with null elements throws NPE
218       */
219      public void testAddAll2() {
220 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
221 +        Integer[] ints = new Integer[SIZE];
222          try {
221            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
222            Integer[] ints = new Integer[SIZE];
223              q.addAll(Arrays.asList(ints));
224              shouldThrow();
225          } catch (NullPointerException success) {}
# Line 230 | Line 230 | public class ConcurrentSkipListSetTest e
230       * possibly adding some elements
231       */
232      public void testAddAll3() {
233 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
234 +        Integer[] ints = new Integer[SIZE];
235 +        for (int i = 0; i < SIZE - 1; ++i)
236 +            ints[i] = new Integer(i);
237          try {
234            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
235            Integer[] ints = new Integer[SIZE];
236            for (int i = 0; i < SIZE-1; ++i)
237                ints[i] = new Integer(i);
238              q.addAll(Arrays.asList(ints));
239              shouldThrow();
240          } catch (NullPointerException success) {}
# Line 247 | Line 247 | public class ConcurrentSkipListSetTest e
247          Integer[] empty = new Integer[0];
248          Integer[] ints = new Integer[SIZE];
249          for (int i = 0; i < SIZE; ++i)
250 <            ints[i] = new Integer(SIZE-1-i);
250 >            ints[i] = new Integer(SIZE - 1 - i);
251          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
252          assertFalse(q.addAll(Arrays.asList(empty)));
253          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 271 | Line 271 | public class ConcurrentSkipListSetTest e
271       */
272      public void testPollLast() {
273          ConcurrentSkipListSet q = populatedSet(SIZE);
274 <        for (int i = SIZE-1; i >= 0; --i) {
274 >        for (int i = SIZE - 1; i >= 0; --i) {
275              assertEquals(i, q.pollLast());
276          }
277          assertNull(q.pollFirst());
# Line 282 | Line 282 | public class ConcurrentSkipListSetTest e
282       */
283      public void testRemoveElement() {
284          ConcurrentSkipListSet q = populatedSet(SIZE);
285 <        for (int i = 1; i < SIZE; i+=2) {
285 >        for (int i = 1; i < SIZE; i += 2) {
286              assertTrue(q.contains(i));
287              assertTrue(q.remove(i));
288              assertFalse(q.contains(i));
289              assertTrue(q.contains(i-1));
290          }
291 <        for (int i = 0; i < SIZE; i+=2) {
291 >        for (int i = 0; i < SIZE; i += 2) {
292              assertTrue(q.contains(i));
293              assertTrue(q.remove(i));
294              assertFalse(q.contains(i));
# Line 352 | Line 352 | public class ConcurrentSkipListSetTest e
352                  assertTrue(changed);
353  
354              assertTrue(q.containsAll(p));
355 <            assertEquals(SIZE-i, q.size());
355 >            assertEquals(SIZE - i, q.size());
356              p.pollFirst();
357          }
358      }
# Line 365 | Line 365 | public class ConcurrentSkipListSetTest e
365              ConcurrentSkipListSet q = populatedSet(SIZE);
366              ConcurrentSkipListSet p = populatedSet(i);
367              assertTrue(q.removeAll(p));
368 <            assertEquals(SIZE-i, q.size());
368 >            assertEquals(SIZE - i, q.size());
369              for (int j = 0; j < i; ++j) {
370 <                Integer I = (Integer)(p.pollFirst());
371 <                assertFalse(q.contains(I));
370 >                Integer x = (Integer)(p.pollFirst());
371 >                assertFalse(q.contains(x));
372              }
373          }
374      }
# Line 461 | Line 461 | public class ConcurrentSkipListSetTest e
461      public void testToArray2() {
462          ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
463          Integer[] ints = new Integer[SIZE];
464 <        Integer[] array = q.toArray(ints);
465 <        assertSame(ints, array);
464 >        assertSame(ints, q.toArray(ints));
465          for (int i = 0; i < ints.length; i++)
466              assertSame(ints[i], q.pollFirst());
467      }
# Line 472 | Line 471 | public class ConcurrentSkipListSetTest e
471       */
472      public void testIterator() {
473          ConcurrentSkipListSet q = populatedSet(SIZE);
475        int i = 0;
474          Iterator it = q.iterator();
475 <        while (it.hasNext()) {
475 >        int i;
476 >        for (i = 0; it.hasNext(); i++)
477              assertTrue(q.contains(it.next()));
479            ++i;
480        }
478          assertEquals(i, SIZE);
479 +        assertIteratorExhausted(it);
480      }
481  
482      /**
483       * iterator of empty set has no elements
484       */
485      public void testEmptyIterator() {
486 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
487 <        int i = 0;
488 <        Iterator it = q.iterator();
491 <        while (it.hasNext()) {
492 <            assertTrue(q.contains(it.next()));
493 <            ++i;
494 <        }
495 <        assertEquals(i, 0);
486 >        NavigableSet s = new ConcurrentSkipListSet();
487 >        assertIteratorExhausted(s.iterator());
488 >        assertIteratorExhausted(s.descendingSet().iterator());
489      }
490  
491      /**
# Line 532 | Line 525 | public class ConcurrentSkipListSetTest e
525          NavigableSet x = populatedSet(SIZE);
526          NavigableSet y = serialClone(x);
527  
528 <        assertTrue(x != y);
528 >        assertNotSame(x, y);
529          assertEquals(x.size(), y.size());
530          assertEquals(x, y);
531          assertEquals(y, x);
# Line 686 | Line 679 | public class ConcurrentSkipListSetTest e
679                     0, setSize - 1, true, bs);
680      }
681  
682 +    /**
683 +     * addAll is idempotent
684 +     */
685 +    public void testAddAll_idempotent() throws Exception {
686 +        Set x = populatedSet(SIZE);
687 +        Set y = new ConcurrentSkipListSet(x);
688 +        y.addAll(x);
689 +        assertEquals(x, y);
690 +        assertEquals(y, x);
691 +    }
692 +
693      static NavigableSet<Integer> newSet(Class cl) throws Exception {
694          NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
695 <        assertEquals(result.size(), 0);
695 >        assertEquals(0, result.size());
696          assertFalse(result.iterator().hasNext());
697          return result;
698      }
# Line 720 | Line 724 | public class ConcurrentSkipListSetTest e
724          // Add entries till we're back to original size
725          while (set.size() < size) {
726              int element = min + rnd.nextInt(rangeSize);
727 <            assertTrue(element >= min && element<= max);
727 >            assertTrue(element >= min && element <= max);
728              put(set, element, bs);
729          }
730      }
# Line 746 | Line 750 | public class ConcurrentSkipListSetTest e
750          // Add entries till we're back to original size
751          while (set.size() < size) {
752              int element = min - 5 + rnd.nextInt(rangeSize + 10);
753 <            if (element >= min && element<= max) {
753 >            if (element >= min && element <= max) {
754                  put(set, element, bs);
755              } else {
756                  try {
# Line 922 | Line 926 | public class ConcurrentSkipListSetTest e
926              if (bsContainsI)
927                  size++;
928          }
929 <        assertEquals(set.size(), size);
929 >        assertEquals(size, set.size());
930  
931          // Test contents using contains elementSet iterator
932          int size2 = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines