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.4 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.22 by jsr166, Fri May 27 19:21:27 2011 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   */
6  
7   import junit.framework.*;
# Line 11 | Line 11 | import java.io.*;
11  
12   public class ConcurrentSkipListSetTest extends JSR166TestCase {
13      public static void main(String[] args) {
14 <        junit.textui.TestRunner.run (suite());
14 >        junit.textui.TestRunner.run(suite());
15      }
16      public static Test suite() {
17 <        return new TestSuite(ConcurrentSkipListSetTest.class);
17 >        return new TestSuite(ConcurrentSkipListSetTest.class);
18      }
19  
20      static class MyReverseComparator implements Comparator {
21          public int compare(Object x, Object y) {
22 <            int i = ((Integer)x).intValue();
23 <            int j = ((Integer)y).intValue();
24 <            if (i < j) return 1;
25 <            if (i > j) return -1;
26 <            return 0;
22 >            return ((Comparable)y).compareTo(x);
23          }
24      }
25  
# Line 31 | Line 27 | public class ConcurrentSkipListSetTest e
27       * Create a set of given size containing consecutive
28       * Integers 0 ... n.
29       */
30 <    private ConcurrentSkipListSet populatedSet(int n) {
31 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
30 >    private ConcurrentSkipListSet<Integer> populatedSet(int n) {
31 >        ConcurrentSkipListSet<Integer> q =
32 >            new ConcurrentSkipListSet<Integer>();
33          assertTrue(q.isEmpty());
34 <        for(int i = n-1; i >= 0; i-=2)
35 <            assertTrue(q.add(new Integer(i)));
36 <        for(int i = (n & 1); i < n; i+=2)
37 <            assertTrue(q.add(new Integer(i)));
34 >        for (int i = n-1; i >= 0; i-=2)
35 >            assertTrue(q.add(new Integer(i)));
36 >        for (int i = (n & 1); i < n; i+=2)
37 >            assertTrue(q.add(new Integer(i)));
38          assertFalse(q.isEmpty());
39 <        assertEquals(n, q.size());
39 >        assertEquals(n, q.size());
40          return q;
41      }
42  
# Line 54 | Line 51 | public class ConcurrentSkipListSetTest e
51          q.add(three);
52          q.add(four);
53          q.add(five);
54 <        assertEquals(5, q.size());
54 >        assertEquals(5, q.size());
55          return q;
56      }
57  
# Line 72 | Line 69 | public class ConcurrentSkipListSetTest e
69          try {
70              ConcurrentSkipListSet q = new ConcurrentSkipListSet((Collection)null);
71              shouldThrow();
72 <        }
76 <        catch (NullPointerException success) {}
72 >        } catch (NullPointerException success) {}
73      }
74  
75      /**
# Line 84 | Line 80 | public class ConcurrentSkipListSetTest e
80              Integer[] ints = new Integer[SIZE];
81              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
82              shouldThrow();
83 <        }
88 <        catch (NullPointerException success) {}
83 >        } catch (NullPointerException success) {}
84      }
85  
86      /**
# Line 98 | Line 93 | public class ConcurrentSkipListSetTest e
93                  ints[i] = new Integer(i);
94              ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
95              shouldThrow();
96 <        }
102 <        catch (NullPointerException success) {}
96 >        } catch (NullPointerException success) {}
97      }
98  
99      /**
100       * Set contains all elements of collection used to initialize
101       */
102      public void testConstructor6() {
103 <        try {
104 <            Integer[] ints = new Integer[SIZE];
105 <            for (int i = 0; i < SIZE; ++i)
106 <                ints[i] = new Integer(i);
107 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
108 <            for (int i = 0; i < SIZE; ++i)
115 <                assertEquals(ints[i], q.pollFirst());
116 <        }
117 <        finally {}
103 >        Integer[] ints = new Integer[SIZE];
104 >        for (int i = 0; i < SIZE; ++i)
105 >            ints[i] = new Integer(i);
106 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
107 >        for (int i = 0; i < SIZE; ++i)
108 >            assertEquals(ints[i], q.pollFirst());
109      }
110  
111      /**
112       * The comparator used in constructor is used
113       */
114      public void testConstructor7() {
115 <        try {
116 <            MyReverseComparator cmp = new MyReverseComparator();
117 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
118 <            assertEquals(cmp, q.comparator());
119 <            Integer[] ints = new Integer[SIZE];
120 <            for (int i = 0; i < SIZE; ++i)
121 <                ints[i] = new Integer(i);
122 <            q.addAll(Arrays.asList(ints));
123 <            for (int i = SIZE-1; i >= 0; --i)
133 <                assertEquals(ints[i], q.pollFirst());
134 <        }
135 <        finally {}
115 >        MyReverseComparator cmp = new MyReverseComparator();
116 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
117 >        assertEquals(cmp, q.comparator());
118 >        Integer[] ints = new Integer[SIZE];
119 >        for (int i = 0; i < SIZE; ++i)
120 >            ints[i] = new Integer(i);
121 >        q.addAll(Arrays.asList(ints));
122 >        for (int i = SIZE-1; i >= 0; --i)
123 >            assertEquals(ints[i], q.pollFirst());
124      }
125  
126      /**
# Line 168 | Line 156 | public class ConcurrentSkipListSetTest e
156       * add(null) throws NPE
157       */
158      public void testAddNull() {
159 <        try {
159 >        try {
160              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
161              q.add(null);
162              shouldThrow();
163 <        } catch (NullPointerException success) { }
163 >        } catch (NullPointerException success) {}
164      }
165  
166      /**
# Line 203 | Line 191 | public class ConcurrentSkipListSetTest e
191              q.add(new Object());
192              q.add(new Object());
193              shouldThrow();
194 <        }
207 <        catch(ClassCastException success) {}
194 >        } catch (ClassCastException success) {}
195      }
196  
197      /**
# Line 215 | Line 202 | public class ConcurrentSkipListSetTest e
202              ConcurrentSkipListSet q = new ConcurrentSkipListSet();
203              q.addAll(null);
204              shouldThrow();
205 <        }
219 <        catch (NullPointerException success) {}
205 >        } catch (NullPointerException success) {}
206      }
207 +
208      /**
209       * addAll of a collection with null elements throws NPE
210       */
# Line 227 | Line 214 | public class ConcurrentSkipListSetTest e
214              Integer[] ints = new Integer[SIZE];
215              q.addAll(Arrays.asList(ints));
216              shouldThrow();
217 <        }
231 <        catch (NullPointerException success) {}
217 >        } catch (NullPointerException success) {}
218      }
219 +
220      /**
221       * addAll of a collection with any null elements throws NPE after
222       * possibly adding some elements
# Line 242 | Line 229 | public class ConcurrentSkipListSetTest e
229                  ints[i] = new Integer(i);
230              q.addAll(Arrays.asList(ints));
231              shouldThrow();
232 <        }
246 <        catch (NullPointerException success) {}
232 >        } catch (NullPointerException success) {}
233      }
234  
235      /**
236       * Set contains all elements of successful addAll
237       */
238      public void testAddAll5() {
239 <        try {
240 <            Integer[] empty = new Integer[0];
241 <            Integer[] ints = new Integer[SIZE];
242 <            for (int i = 0; i < SIZE; ++i)
243 <                ints[i] = new Integer(SIZE-1-i);
244 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
245 <            assertFalse(q.addAll(Arrays.asList(empty)));
246 <            assertTrue(q.addAll(Arrays.asList(ints)));
247 <            for (int i = 0; i < SIZE; ++i)
262 <                assertEquals(new Integer(i), q.pollFirst());
263 <        }
264 <        finally {}
239 >        Integer[] empty = new Integer[0];
240 >        Integer[] ints = new Integer[SIZE];
241 >        for (int i = 0; i < SIZE; ++i)
242 >            ints[i] = new Integer(SIZE-1-i);
243 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
244 >        assertFalse(q.addAll(Arrays.asList(empty)));
245 >        assertTrue(q.addAll(Arrays.asList(ints)));
246 >        for (int i = 0; i < SIZE; ++i)
247 >            assertEquals(i, q.pollFirst());
248      }
249  
250      /**
# Line 270 | Line 253 | public class ConcurrentSkipListSetTest e
253      public void testPollFirst() {
254          ConcurrentSkipListSet q = populatedSet(SIZE);
255          for (int i = 0; i < SIZE; ++i) {
256 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
256 >            assertEquals(i, q.pollFirst());
257          }
258 <        assertNull(q.pollFirst());
258 >        assertNull(q.pollFirst());
259      }
260  
261      /**
# Line 281 | Line 264 | public class ConcurrentSkipListSetTest e
264      public void testPollLast() {
265          ConcurrentSkipListSet q = populatedSet(SIZE);
266          for (int i = SIZE-1; i >= 0; --i) {
267 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
267 >            assertEquals(i, q.pollLast());
268          }
269 <        assertNull(q.pollFirst());
269 >        assertNull(q.pollFirst());
270      }
271  
289
272      /**
273       * remove(x) removes x and returns true if present
274       */
275      public void testRemoveElement() {
276          ConcurrentSkipListSet q = populatedSet(SIZE);
277          for (int i = 1; i < SIZE; i+=2) {
278 <            assertTrue(q.remove(new Integer(i)));
278 >            assertTrue(q.contains(i));
279 >            assertTrue(q.remove(i));
280 >            assertFalse(q.contains(i));
281 >            assertTrue(q.contains(i-1));
282          }
283          for (int i = 0; i < SIZE; i+=2) {
284 <            assertTrue(q.remove(new Integer(i)));
285 <            assertFalse(q.remove(new Integer(i+1)));
284 >            assertTrue(q.contains(i));
285 >            assertTrue(q.remove(i));
286 >            assertFalse(q.contains(i));
287 >            assertFalse(q.remove(i+1));
288 >            assertFalse(q.contains(i+1));
289          }
290          assertTrue(q.isEmpty());
291      }
# Line 377 | Line 365 | public class ConcurrentSkipListSetTest e
365          }
366      }
367  
380
381
368      /**
369       * lower returns preceding element
370       */
# Line 395 | Line 381 | public class ConcurrentSkipListSetTest e
381  
382          Object e4 = q.lower(zero);
383          assertNull(e4);
398
384      }
385  
386      /**
# Line 414 | Line 399 | public class ConcurrentSkipListSetTest e
399  
400          Object e4 = q.higher(six);
401          assertNull(e4);
417
402      }
403  
404      /**
# Line 433 | Line 417 | public class ConcurrentSkipListSetTest e
417  
418          Object e4 = q.floor(zero);
419          assertNull(e4);
436
420      }
421  
422      /**
# Line 452 | Line 435 | public class ConcurrentSkipListSetTest e
435  
436          Object e4 = q.ceiling(six);
437          assertNull(e4);
455
438      }
439  
440      /**
441 <     * toArray contains all elements
441 >     * toArray contains all elements in sorted order
442       */
443      public void testToArray() {
444          ConcurrentSkipListSet q = populatedSet(SIZE);
445 <        Object[] o = q.toArray();
446 <        Arrays.sort(o);
447 <        for(int i = 0; i < o.length; i++)
466 <            assertEquals(o[i], q.pollFirst());
445 >        Object[] o = q.toArray();
446 >        for (int i = 0; i < o.length; i++)
447 >            assertSame(o[i], q.pollFirst());
448      }
449  
450      /**
451 <     * toArray(a) contains all elements
451 >     * toArray(a) contains all elements in sorted order
452       */
453      public void testToArray2() {
454 <        ConcurrentSkipListSet q = populatedSet(SIZE);
455 <        Integer[] ints = new Integer[SIZE];
456 <        ints = (Integer[])q.toArray(ints);
457 <        Arrays.sort(ints);
458 <        for(int i = 0; i < ints.length; i++)
459 <            assertEquals(ints[i], q.pollFirst());
454 >        ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
455 >        Integer[] ints = new Integer[SIZE];
456 >        Integer[] array = q.toArray(ints);
457 >        assertSame(ints, array);
458 >        for (int i = 0; i < ints.length; i++)
459 >            assertSame(ints[i], q.pollFirst());
460      }
461  
462      /**
# Line 484 | Line 465 | public class ConcurrentSkipListSetTest e
465      public void testIterator() {
466          ConcurrentSkipListSet q = populatedSet(SIZE);
467          int i = 0;
468 <        Iterator it = q.iterator();
469 <        while(it.hasNext()) {
468 >        Iterator it = q.iterator();
469 >        while (it.hasNext()) {
470              assertTrue(q.contains(it.next()));
471              ++i;
472          }
# Line 498 | Line 479 | public class ConcurrentSkipListSetTest e
479      public void testEmptyIterator() {
480          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
481          int i = 0;
482 <        Iterator it = q.iterator();
483 <        while(it.hasNext()) {
482 >        Iterator it = q.iterator();
483 >        while (it.hasNext()) {
484              assertTrue(q.contains(it.next()));
485              ++i;
486          }
# Line 509 | Line 490 | public class ConcurrentSkipListSetTest e
490      /**
491       * iterator.remove removes current element
492       */
493 <    public void testIteratorRemove () {
493 >    public void testIteratorRemove() {
494          final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
495          q.add(new Integer(2));
496          q.add(new Integer(1));
# Line 525 | Line 506 | public class ConcurrentSkipListSetTest e
506          assertFalse(it.hasNext());
507      }
508  
528
509      /**
510       * toString contains toStrings of elements
511       */
# Line 533 | Line 513 | public class ConcurrentSkipListSetTest e
513          ConcurrentSkipListSet q = populatedSet(SIZE);
514          String s = q.toString();
515          for (int i = 0; i < SIZE; ++i) {
516 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
516 >            assertTrue(s.contains(String.valueOf(i)));
517          }
518      }
519  
520      /**
521       * A deserialized serialized set has same elements
522       */
523 <    public void testSerialization() {
523 >    public void testSerialization() throws Exception {
524          ConcurrentSkipListSet q = populatedSet(SIZE);
525 <        try {
526 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
527 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
528 <            out.writeObject(q);
529 <            out.close();
530 <
531 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
532 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
533 <            ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
534 <            assertEquals(q.size(), r.size());
535 <            while (!q.isEmpty())
556 <                assertEquals(q.pollFirst(), r.pollFirst());
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
560 <        }
525 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
526 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
527 >        out.writeObject(q);
528 >        out.close();
529 >
530 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
531 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
532 >        ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
533 >        assertEquals(q.size(), r.size());
534 >        while (!q.isEmpty())
535 >            assertEquals(q.pollFirst(), r.pollFirst());
536      }
537  
538      /**
# Line 685 | Line 660 | public class ConcurrentSkipListSetTest e
660      /**
661       * Subsets of subsets subdivide correctly
662       */
663 <    public void testRecursiveSubSets() {
664 <        int setSize = 1000;
665 <        Class cl = ConcurrentSkipListSet.class;
663 >    public void testRecursiveSubSets() throws Exception {
664 >        int setSize = expensiveTests ? 1000 : 100;
665 >        Class cl = ConcurrentSkipListSet.class;
666  
667          NavigableSet<Integer> set = newSet(cl);
668          bs = new BitSet(setSize);
# Line 704 | Line 679 | public class ConcurrentSkipListSetTest e
679                     0, setSize - 1, true);
680      }
681  
682 <    static NavigableSet<Integer> newSet(Class cl) {
683 <        NavigableSet<Integer> result = null;
709 <        try {
710 <            result = (NavigableSet<Integer>) cl.newInstance();
711 <        } catch(Exception e) {
712 <            fail();
713 <        }
682 >    static NavigableSet<Integer> newSet(Class cl) throws Exception {
683 >        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
684          assertEquals(result.size(), 0);
685          assertFalse(result.iterator().hasNext());
686          return result;
# Line 733 | Line 703 | public class ConcurrentSkipListSetTest e
703          }
704  
705          // Remove a bunch of entries with iterator
706 <        for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
706 >        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
707              if (rnd.nextBoolean()) {
708                  bs.clear(it.next());
709                  it.remove();
# Line 758 | Line 728 | public class ConcurrentSkipListSetTest e
728          }
729  
730          // Remove a bunch of entries with iterator
731 <        for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
731 >        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
732              if (rnd.nextBoolean()) {
733                  bs.clear(it.next());
734                  it.remove();
# Line 773 | Line 743 | public class ConcurrentSkipListSetTest e
743              } else {
744                  try {
745                      set.add(element);
746 <                    fail();
747 <                } catch(IllegalArgumentException e) {
778 <                    // expected
779 <                }
746 >                    shouldThrow();
747 >                } catch (IllegalArgumentException success) {}
748              }
749          }
750      }
# Line 874 | Line 842 | public class ConcurrentSkipListSetTest e
842       */
843      void check(NavigableSet<Integer> set,
844                        final int min, final int max, final boolean ascending) {
845 <       class ReferenceSet {
845 >        class ReferenceSet {
846              int lower(int element) {
847                  return ascending ?
848                      lowerAscending(element) : higherAscending(element);
# Line 909 | Line 877 | public class ConcurrentSkipListSetTest e
877                  // BitSet should support this! Test would run much faster
878                  while (element >= min) {
879                      if (bs.get(element))
880 <                        return(element);
880 >                        return element;
881                      element--;
882                  }
883                  return -1;
# Line 975 | Line 943 | public class ConcurrentSkipListSetTest e
943              assertEq(rs.last(),  -1);
944              try {
945                  set.first();
946 <                fail();
947 <            } catch(NoSuchElementException e) {
980 <                // expected
981 <            }
946 >                shouldThrow();
947 >            } catch (NoSuchElementException success) {}
948              try {
949                  set.last();
950 <                fail();
951 <            } catch(NoSuchElementException e) {
986 <                // expected
987 <            }
950 >                shouldThrow();
951 >            } catch (NoSuchElementException success) {}
952          }
953      }
954  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines