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.34 by jsr166, Wed Dec 31 21:45:16 2014 UTC vs.
Revision 1.50 by jsr166, Mon May 28 21:36:13 2018 UTC

# Line 21 | Line 21 | 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 35 | Line 35 | public class ConcurrentSkipListSetTest e
35  
36      /**
37       * Returns a new set of given size containing consecutive
38 <     * Integers 0 ... n.
38 >     * Integers 0 ... n - 1.
39       */
40 <    private ConcurrentSkipListSet<Integer> populatedSet(int n) {
41 <        ConcurrentSkipListSet<Integer> q =
42 <            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)
46              assertTrue(q.add(new Integer(i)));
# Line 53 | Line 52 | public class ConcurrentSkipListSetTest e
52      /**
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 77 | 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 87 | Line 86 | public class ConcurrentSkipListSetTest e
86       */
87      public void testConstructor4() {
88          try {
89 <            Integer[] ints = new Integer[SIZE];
91 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
89 >            new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE]));
90              shouldThrow();
91          } catch (NullPointerException success) {}
92      }
# Line 97 | 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];
102 <            for (int i = 0; i < SIZE-1; ++i)
103 <                ints[i] = new Integer(i);
104 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
102 >            new ConcurrentSkipListSet(Arrays.asList(ints));
103              shouldThrow();
104          } catch (NullPointerException success) {}
105      }
# Line 129 | 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 153 | 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 166 | Line 164 | public class ConcurrentSkipListSetTest e
164       * add(null) throws NPE
165       */
166      public void testAddNull() {
167 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
168          try {
170            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
169              q.add(null);
170              shouldThrow();
171          } catch (NullPointerException success) {}
# Line 195 | 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 {
199            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
200            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 {
212            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
217              q.addAll(null);
218              shouldThrow();
219          } catch (NullPointerException success) {}
# Line 219 | 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 {
223            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
224            Integer[] ints = new Integer[SIZE];
229              q.addAll(Arrays.asList(ints));
230              shouldThrow();
231          } catch (NullPointerException success) {}
# Line 232 | 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 {
236            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
237            Integer[] ints = new Integer[SIZE];
238            for (int i = 0; i < SIZE-1; ++i)
239                ints[i] = new Integer(i);
244              q.addAll(Arrays.asList(ints));
245              shouldThrow();
246          } catch (NullPointerException success) {}
# Line 249 | 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 273 | 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 288 | Line 292 | public class ConcurrentSkipListSetTest e
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) {
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 354 | 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 367 | 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 x = (Integer)(p.pollFirst());
377                  assertFalse(q.contains(x));
# Line 452 | Line 456 | public class ConcurrentSkipListSetTest e
456       */
457      public void testToArray() {
458          ConcurrentSkipListSet q = populatedSet(SIZE);
459 <        Object[] o = q.toArray();
460 <        for (int i = 0; i < o.length; i++)
461 <            assertSame(o[i], q.pollFirst());
459 >        Object[] a = q.toArray();
460 >        assertSame(Object[].class, a.getClass());
461 >        for (Object o : a)
462 >            assertSame(o, q.pollFirst());
463 >        assertTrue(q.isEmpty());
464      }
465  
466      /**
# Line 464 | Line 470 | public class ConcurrentSkipListSetTest e
470          ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
471          Integer[] ints = new Integer[SIZE];
472          assertSame(ints, q.toArray(ints));
473 <        for (int i = 0; i < ints.length; i++)
474 <            assertSame(ints[i], q.pollFirst());
473 >        for (Integer o : ints)
474 >            assertSame(o, q.pollFirst());
475 >        assertTrue(q.isEmpty());
476      }
477  
478      /**
# Line 473 | Line 480 | public class ConcurrentSkipListSetTest e
480       */
481      public void testIterator() {
482          ConcurrentSkipListSet q = populatedSet(SIZE);
476        int i = 0;
483          Iterator it = q.iterator();
484 <        while (it.hasNext()) {
484 >        int i;
485 >        for (i = 0; it.hasNext(); i++)
486              assertTrue(q.contains(it.next()));
480            ++i;
481        }
487          assertEquals(i, SIZE);
488 +        assertIteratorExhausted(it);
489      }
490  
491      /**
492       * iterator of empty set has no elements
493       */
494      public void testEmptyIterator() {
495 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
496 <        int i = 0;
497 <        Iterator it = q.iterator();
492 <        while (it.hasNext()) {
493 <            assertTrue(q.contains(it.next()));
494 <            ++i;
495 <        }
496 <        assertEquals(0, i);
495 >        NavigableSet s = new ConcurrentSkipListSet();
496 >        assertIteratorExhausted(s.iterator());
497 >        assertIteratorExhausted(s.descendingSet().iterator());
498      }
499  
500      /**
# Line 527 | Line 528 | public class ConcurrentSkipListSetTest e
528      }
529  
530      /**
531 <     * A deserialized serialized set has same elements
531 >     * A cloned set equals original
532 >     */
533 >    public void testClone() {
534 >        ConcurrentSkipListSet x = populatedSet(SIZE);
535 >        ConcurrentSkipListSet y = x.clone();
536 >
537 >        assertNotSame(x, y);
538 >        assertEquals(x.size(), y.size());
539 >        assertEquals(x, y);
540 >        assertEquals(y, x);
541 >        while (!x.isEmpty()) {
542 >            assertFalse(y.isEmpty());
543 >            assertEquals(x.pollFirst(), y.pollFirst());
544 >        }
545 >        assertTrue(y.isEmpty());
546 >    }
547 >
548 >    /**
549 >     * A deserialized/reserialized set equals original
550       */
551      public void testSerialization() throws Exception {
552          NavigableSet x = populatedSet(SIZE);
# Line 699 | Line 718 | public class ConcurrentSkipListSetTest e
718      }
719  
720      static NavigableSet<Integer> newSet(Class cl) throws Exception {
721 <        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
721 >        NavigableSet<Integer> result =
722 >            (NavigableSet<Integer>) cl.getConstructor().newInstance();
723          assertEquals(0, result.size());
724          assertFalse(result.iterator().hasNext());
725          return result;
# Line 982 | Line 1002 | public class ConcurrentSkipListSetTest e
1002      }
1003  
1004      static boolean eq(Integer i, int j) {
1005 <        return i == null ? j == -1 : i == j;
1005 >        return (i == null) ? j == -1 : i == j;
1006      }
1007  
1008   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines