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.49 by jsr166, Sun Jan 7 22:59:18 2018 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
38 <     * Integers 0 ... n.
37 >     * Returns a new set of given size containing consecutive
38 >     * Integers 0 ... n - 1.
39       */
40 <    private ConcurrentSkipListSet<Integer> populatedSet(int n) {
41 <        ConcurrentSkipListSet<Integer> q =
40 <            new ConcurrentSkipListSet<Integer>();
40 >    private static ConcurrentSkipListSet<Integer> populatedSet(int n) {
41 >        ConcurrentSkipListSet<Integer> q = new ConcurrentSkipListSet<>();
42          assertTrue(q.isEmpty());
43 <        for (int i = n-1; i >= 0; i-=2)
43 >        for (int i = n - 1; i >= 0; i -= 2)
44              assertTrue(q.add(new Integer(i)));
45 <        for (int i = (n & 1); i < n; i+=2)
45 >        for (int i = (n & 1); i < n; i += 2)
46              assertTrue(q.add(new Integer(i)));
47          assertFalse(q.isEmpty());
48          assertEquals(n, q.size());
# Line 49 | Line 50 | public class ConcurrentSkipListSetTest e
50      }
51  
52      /**
53 <     * Create set of first 5 ints
53 >     * Returns a new set of first 5 ints.
54       */
55 <    private ConcurrentSkipListSet set5() {
55 >    private static ConcurrentSkipListSet set5() {
56          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
57          assertTrue(q.isEmpty());
58          q.add(one);
# Line 75 | Line 76 | public class ConcurrentSkipListSetTest e
76       */
77      public void testConstructor3() {
78          try {
79 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null);
79 >            new ConcurrentSkipListSet((Collection)null);
80              shouldThrow();
81          } catch (NullPointerException success) {}
82      }
# Line 85 | Line 86 | public class ConcurrentSkipListSetTest e
86       */
87      public void testConstructor4() {
88          try {
89 <            Integer[] ints = new Integer[SIZE];
89 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
89 >            new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE]));
90              shouldThrow();
91          } catch (NullPointerException success) {}
92      }
# Line 95 | Line 95 | public class ConcurrentSkipListSetTest e
95       * Initializing from Collection with some null elements throws NPE
96       */
97      public void testConstructor5() {
98 +        Integer[] ints = new Integer[SIZE];
99 +        for (int i = 0; i < SIZE - 1; ++i)
100 +            ints[i] = new Integer(i);
101          try {
102 <            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));
102 >            new ConcurrentSkipListSet(Arrays.asList(ints));
103              shouldThrow();
104          } catch (NullPointerException success) {}
105      }
# Line 127 | Line 127 | public class ConcurrentSkipListSetTest e
127          for (int i = 0; i < SIZE; ++i)
128              ints[i] = new Integer(i);
129          q.addAll(Arrays.asList(ints));
130 <        for (int i = SIZE-1; i >= 0; --i)
130 >        for (int i = SIZE - 1; i >= 0; --i)
131              assertEquals(ints[i], q.pollFirst());
132      }
133  
# Line 151 | Line 151 | public class ConcurrentSkipListSetTest e
151      public void testSize() {
152          ConcurrentSkipListSet q = populatedSet(SIZE);
153          for (int i = 0; i < SIZE; ++i) {
154 <            assertEquals(SIZE-i, q.size());
154 >            assertEquals(SIZE - i, q.size());
155              q.pollFirst();
156          }
157          for (int i = 0; i < SIZE; ++i) {
# Line 164 | Line 164 | public class ConcurrentSkipListSetTest e
164       * add(null) throws NPE
165       */
166      public void testAddNull() {
167 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
168          try {
168            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
169              q.add(null);
170              shouldThrow();
171          } catch (NullPointerException success) {}
# Line 193 | Line 193 | public class ConcurrentSkipListSetTest e
193       * Add of non-Comparable throws CCE
194       */
195      public void testAddNonComparable() {
196 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
197          try {
197            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
198            q.add(new Object());
198              q.add(new Object());
199              q.add(new Object());
200              shouldThrow();
201 <        } catch (ClassCastException success) {}
201 >        } catch (ClassCastException success) {
202 >            assertTrue(q.size() < 2);
203 >            for (int i = 0, size = q.size(); i < size; i++)
204 >                assertSame(Object.class, q.pollFirst().getClass());
205 >            assertNull(q.pollFirst());
206 >            assertTrue(q.isEmpty());
207 >            assertEquals(0, q.size());
208 >        }
209      }
210  
211      /**
212       * addAll(null) throws NPE
213       */
214      public void testAddAll1() {
215 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
216          try {
210            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
217              q.addAll(null);
218              shouldThrow();
219          } catch (NullPointerException success) {}
# Line 217 | Line 223 | public class ConcurrentSkipListSetTest e
223       * addAll of a collection with null elements throws NPE
224       */
225      public void testAddAll2() {
226 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
227 +        Integer[] ints = new Integer[SIZE];
228          try {
221            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
222            Integer[] ints = new Integer[SIZE];
229              q.addAll(Arrays.asList(ints));
230              shouldThrow();
231          } catch (NullPointerException success) {}
# Line 230 | Line 236 | public class ConcurrentSkipListSetTest e
236       * possibly adding some elements
237       */
238      public void testAddAll3() {
239 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
240 +        Integer[] ints = new Integer[SIZE];
241 +        for (int i = 0; i < SIZE - 1; ++i)
242 +            ints[i] = new Integer(i);
243          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);
244              q.addAll(Arrays.asList(ints));
245              shouldThrow();
246          } catch (NullPointerException success) {}
# Line 247 | Line 253 | public class ConcurrentSkipListSetTest e
253          Integer[] empty = new Integer[0];
254          Integer[] ints = new Integer[SIZE];
255          for (int i = 0; i < SIZE; ++i)
256 <            ints[i] = new Integer(SIZE-1-i);
256 >            ints[i] = new Integer(SIZE - 1 - i);
257          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
258          assertFalse(q.addAll(Arrays.asList(empty)));
259          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 271 | Line 277 | public class ConcurrentSkipListSetTest e
277       */
278      public void testPollLast() {
279          ConcurrentSkipListSet q = populatedSet(SIZE);
280 <        for (int i = SIZE-1; i >= 0; --i) {
280 >        for (int i = SIZE - 1; i >= 0; --i) {
281              assertEquals(i, q.pollLast());
282          }
283          assertNull(q.pollFirst());
# Line 282 | Line 288 | public class ConcurrentSkipListSetTest e
288       */
289      public void testRemoveElement() {
290          ConcurrentSkipListSet q = populatedSet(SIZE);
291 <        for (int i = 1; i < SIZE; i+=2) {
291 >        for (int i = 1; i < SIZE; i += 2) {
292              assertTrue(q.contains(i));
293              assertTrue(q.remove(i));
294              assertFalse(q.contains(i));
295 <            assertTrue(q.contains(i-1));
295 >            assertTrue(q.contains(i - 1));
296          }
297 <        for (int i = 0; i < SIZE; i+=2) {
297 >        for (int i = 0; i < SIZE; i += 2) {
298              assertTrue(q.contains(i));
299              assertTrue(q.remove(i));
300              assertFalse(q.contains(i));
301 <            assertFalse(q.remove(i+1));
302 <            assertFalse(q.contains(i+1));
301 >            assertFalse(q.remove(i + 1));
302 >            assertFalse(q.contains(i + 1));
303          }
304          assertTrue(q.isEmpty());
305      }
# Line 352 | Line 358 | public class ConcurrentSkipListSetTest e
358                  assertTrue(changed);
359  
360              assertTrue(q.containsAll(p));
361 <            assertEquals(SIZE-i, q.size());
361 >            assertEquals(SIZE - i, q.size());
362              p.pollFirst();
363          }
364      }
# Line 365 | Line 371 | public class ConcurrentSkipListSetTest e
371              ConcurrentSkipListSet q = populatedSet(SIZE);
372              ConcurrentSkipListSet p = populatedSet(i);
373              assertTrue(q.removeAll(p));
374 <            assertEquals(SIZE-i, q.size());
374 >            assertEquals(SIZE - i, q.size());
375              for (int j = 0; j < i; ++j) {
376 <                Integer I = (Integer)(p.pollFirst());
377 <                assertFalse(q.contains(I));
376 >                Integer x = (Integer)(p.pollFirst());
377 >                assertFalse(q.contains(x));
378              }
379          }
380      }
# Line 461 | Line 467 | public class ConcurrentSkipListSetTest e
467      public void testToArray2() {
468          ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
469          Integer[] ints = new Integer[SIZE];
470 <        Integer[] array = q.toArray(ints);
465 <        assertSame(ints, array);
470 >        assertSame(ints, q.toArray(ints));
471          for (int i = 0; i < ints.length; i++)
472              assertSame(ints[i], q.pollFirst());
473      }
# Line 472 | Line 477 | public class ConcurrentSkipListSetTest e
477       */
478      public void testIterator() {
479          ConcurrentSkipListSet q = populatedSet(SIZE);
475        int i = 0;
480          Iterator it = q.iterator();
481 <        while (it.hasNext()) {
481 >        int i;
482 >        for (i = 0; it.hasNext(); i++)
483              assertTrue(q.contains(it.next()));
479            ++i;
480        }
484          assertEquals(i, SIZE);
485 +        assertIteratorExhausted(it);
486      }
487  
488      /**
489       * iterator of empty set has no elements
490       */
491      public void testEmptyIterator() {
492 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
493 <        int i = 0;
494 <        Iterator it = q.iterator();
491 <        while (it.hasNext()) {
492 <            assertTrue(q.contains(it.next()));
493 <            ++i;
494 <        }
495 <        assertEquals(i, 0);
492 >        NavigableSet s = new ConcurrentSkipListSet();
493 >        assertIteratorExhausted(s.iterator());
494 >        assertIteratorExhausted(s.descendingSet().iterator());
495      }
496  
497      /**
# Line 526 | Line 525 | public class ConcurrentSkipListSetTest e
525      }
526  
527      /**
528 <     * A deserialized serialized set has same elements
528 >     * A cloned set equals original
529 >     */
530 >    public void testClone() {
531 >        ConcurrentSkipListSet x = populatedSet(SIZE);
532 >        ConcurrentSkipListSet y = x.clone();
533 >
534 >        assertNotSame(x, y);
535 >        assertEquals(x.size(), y.size());
536 >        assertEquals(x, y);
537 >        assertEquals(y, x);
538 >        while (!x.isEmpty()) {
539 >            assertFalse(y.isEmpty());
540 >            assertEquals(x.pollFirst(), y.pollFirst());
541 >        }
542 >        assertTrue(y.isEmpty());
543 >    }
544 >
545 >    /**
546 >     * A deserialized/reserialized set equals original
547       */
548      public void testSerialization() throws Exception {
549          NavigableSet x = populatedSet(SIZE);
550          NavigableSet y = serialClone(x);
551  
552 <        assertTrue(x != y);
552 >        assertNotSame(x, y);
553          assertEquals(x.size(), y.size());
554          assertEquals(x, y);
555          assertEquals(y, x);
# Line 686 | Line 703 | public class ConcurrentSkipListSetTest e
703                     0, setSize - 1, true, bs);
704      }
705  
706 +    /**
707 +     * addAll is idempotent
708 +     */
709 +    public void testAddAll_idempotent() throws Exception {
710 +        Set x = populatedSet(SIZE);
711 +        Set y = new ConcurrentSkipListSet(x);
712 +        y.addAll(x);
713 +        assertEquals(x, y);
714 +        assertEquals(y, x);
715 +    }
716 +
717      static NavigableSet<Integer> newSet(Class cl) throws Exception {
718 <        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
719 <        assertEquals(result.size(), 0);
718 >        NavigableSet<Integer> result =
719 >            (NavigableSet<Integer>) cl.getConstructor().newInstance();
720 >        assertEquals(0, result.size());
721          assertFalse(result.iterator().hasNext());
722          return result;
723      }
# Line 720 | Line 749 | public class ConcurrentSkipListSetTest e
749          // Add entries till we're back to original size
750          while (set.size() < size) {
751              int element = min + rnd.nextInt(rangeSize);
752 <            assertTrue(element >= min && element<= max);
752 >            assertTrue(element >= min && element <= max);
753              put(set, element, bs);
754          }
755      }
# Line 746 | Line 775 | public class ConcurrentSkipListSetTest e
775          // Add entries till we're back to original size
776          while (set.size() < size) {
777              int element = min - 5 + rnd.nextInt(rangeSize + 10);
778 <            if (element >= min && element<= max) {
778 >            if (element >= min && element <= max) {
779                  put(set, element, bs);
780              } else {
781                  try {
# Line 922 | Line 951 | public class ConcurrentSkipListSetTest e
951              if (bsContainsI)
952                  size++;
953          }
954 <        assertEquals(set.size(), size);
954 >        assertEquals(size, set.size());
955  
956          // Test contents using contains elementSet iterator
957          int size2 = 0;
# Line 970 | Line 999 | public class ConcurrentSkipListSetTest e
999      }
1000  
1001      static boolean eq(Integer i, int j) {
1002 <        return i == null ? j == -1 : i == j;
1002 >        return (i == null) ? j == -1 : i == j;
1003      }
1004  
1005   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines