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.38 by jsr166, Sat Apr 25 04:55:30 2015 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.*;
8 < import java.util.*;
9 < import java.util.concurrent.*;
10 < import java.io.*;
7 > import java.util.Arrays;
8 > import java.util.BitSet;
9 > import java.util.Collection;
10 > import java.util.Comparator;
11 > import java.util.Iterator;
12 > import java.util.NavigableSet;
13 > import java.util.NoSuchElementException;
14 > import java.util.Random;
15 > 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);
27 >        return new TestSuite(ConcurrentSkipListSetTest.class);
28      }
29  
30      static class MyReverseComparator implements Comparator {
31          public int compare(Object x, Object y) {
32 <            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;
32 >            return ((Comparable)y).compareTo(x);
33          }
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 populatedSet(int n) {
41 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
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)
45 <            assertTrue(q.add(new Integer(i)));
46 <        for(int i = (n & 1); i < n; i+=2)
47 <            assertTrue(q.add(new Integer(i)));
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)
47 >            assertTrue(q.add(new Integer(i)));
48          assertFalse(q.isEmpty());
49 <        assertEquals(n, q.size());
49 >        assertEquals(n, q.size());
50          return q;
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 54 | Line 61 | public class ConcurrentSkipListSetTest e
61          q.add(three);
62          q.add(four);
63          q.add(five);
64 <        assertEquals(5, q.size());
64 >        assertEquals(5, q.size());
65          return q;
66      }
67  
# Line 70 | 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 <        }
76 <        catch (NullPointerException success) {}
82 >        } catch (NullPointerException success) {}
83      }
84  
85      /**
# Line 82 | Line 88 | public class ConcurrentSkipListSetTest e
88      public void testConstructor4() {
89          try {
90              Integer[] ints = new Integer[SIZE];
91 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
91 >            new ConcurrentSkipListSet(Arrays.asList(ints));
92              shouldThrow();
93 <        }
88 <        catch (NullPointerException success) {}
93 >        } catch (NullPointerException success) {}
94      }
95  
96      /**
# Line 96 | Line 101 | public class ConcurrentSkipListSetTest e
101              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));
104 >            new ConcurrentSkipListSet(Arrays.asList(ints));
105              shouldThrow();
106 <        }
102 <        catch (NullPointerException success) {}
106 >        } catch (NullPointerException success) {}
107      }
108  
109      /**
110       * Set contains all elements of collection used to initialize
111       */
112      public void testConstructor6() {
113 <        try {
114 <            Integer[] ints = new Integer[SIZE];
115 <            for (int i = 0; i < SIZE; ++i)
116 <                ints[i] = new Integer(i);
117 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
118 <            for (int i = 0; i < SIZE; ++i)
115 <                assertEquals(ints[i], q.pollFirst());
116 <        }
117 <        finally {}
113 >        Integer[] ints = new Integer[SIZE];
114 >        for (int i = 0; i < SIZE; ++i)
115 >            ints[i] = new Integer(i);
116 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
117 >        for (int i = 0; i < SIZE; ++i)
118 >            assertEquals(ints[i], q.pollFirst());
119      }
120  
121      /**
122       * The comparator used in constructor is used
123       */
124      public void testConstructor7() {
125 <        try {
126 <            MyReverseComparator cmp = new MyReverseComparator();
127 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
128 <            assertEquals(cmp, q.comparator());
129 <            Integer[] ints = new Integer[SIZE];
130 <            for (int i = 0; i < SIZE; ++i)
131 <                ints[i] = new Integer(i);
132 <            q.addAll(Arrays.asList(ints));
133 <            for (int i = SIZE-1; i >= 0; --i)
133 <                assertEquals(ints[i], q.pollFirst());
134 <        }
135 <        finally {}
125 >        MyReverseComparator cmp = new MyReverseComparator();
126 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
127 >        assertEquals(cmp, q.comparator());
128 >        Integer[] ints = new Integer[SIZE];
129 >        for (int i = 0; i < SIZE; ++i)
130 >            ints[i] = new Integer(i);
131 >        q.addAll(Arrays.asList(ints));
132 >        for (int i = SIZE-1; i >= 0; --i)
133 >            assertEquals(ints[i], q.pollFirst());
134      }
135  
136      /**
# Line 168 | Line 166 | public class ConcurrentSkipListSetTest e
166       * add(null) throws NPE
167       */
168      public void testAddNull() {
169 <        try {
170 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
169 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
170 >        try {
171              q.add(null);
172              shouldThrow();
173 <        } catch (NullPointerException success) { }
173 >        } catch (NullPointerException success) {}
174      }
175  
176      /**
# Line 197 | Line 195 | public class ConcurrentSkipListSetTest e
195       * Add of non-Comparable throws CCE
196       */
197      public void testAddNonComparable() {
198 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
199          try {
201            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
202            q.add(new Object());
200              q.add(new Object());
201              q.add(new Object());
202              shouldThrow();
203 <        }
207 <        catch(ClassCastException success) {}
203 >        } catch (ClassCastException success) {}
204      }
205  
206      /**
207       * addAll(null) throws NPE
208       */
209      public void testAddAll1() {
210 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
211          try {
215            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
212              q.addAll(null);
213              shouldThrow();
214 <        }
219 <        catch (NullPointerException success) {}
214 >        } catch (NullPointerException success) {}
215      }
216 +
217      /**
218       * addAll of a collection with null elements throws NPE
219       */
220      public void testAddAll2() {
221 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
222 +        Integer[] ints = new Integer[SIZE];
223          try {
226            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
227            Integer[] ints = new Integer[SIZE];
224              q.addAll(Arrays.asList(ints));
225              shouldThrow();
226 <        }
231 <        catch (NullPointerException success) {}
226 >        } catch (NullPointerException success) {}
227      }
228 +
229      /**
230       * addAll of a collection with any null elements throws NPE after
231       * possibly adding some elements
232       */
233      public void testAddAll3() {
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          try {
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);
239              q.addAll(Arrays.asList(ints));
240              shouldThrow();
241 <        }
246 <        catch (NullPointerException success) {}
241 >        } catch (NullPointerException success) {}
242      }
243  
244      /**
245       * Set contains all elements of successful addAll
246       */
247      public void testAddAll5() {
248 <        try {
249 <            Integer[] empty = new Integer[0];
250 <            Integer[] ints = new Integer[SIZE];
251 <            for (int i = 0; i < SIZE; ++i)
252 <                ints[i] = new Integer(SIZE-1-i);
253 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
254 <            assertFalse(q.addAll(Arrays.asList(empty)));
255 <            assertTrue(q.addAll(Arrays.asList(ints)));
256 <            for (int i = 0; i < SIZE; ++i)
262 <                assertEquals(new Integer(i), q.pollFirst());
263 <        }
264 <        finally {}
248 >        Integer[] empty = new Integer[0];
249 >        Integer[] ints = new Integer[SIZE];
250 >        for (int i = 0; i < SIZE; ++i)
251 >            ints[i] = new Integer(SIZE-1-i);
252 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
253 >        assertFalse(q.addAll(Arrays.asList(empty)));
254 >        assertTrue(q.addAll(Arrays.asList(ints)));
255 >        for (int i = 0; i < SIZE; ++i)
256 >            assertEquals(i, q.pollFirst());
257      }
258  
259      /**
# Line 270 | Line 262 | public class ConcurrentSkipListSetTest e
262      public void testPollFirst() {
263          ConcurrentSkipListSet q = populatedSet(SIZE);
264          for (int i = 0; i < SIZE; ++i) {
265 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
265 >            assertEquals(i, q.pollFirst());
266          }
267 <        assertNull(q.pollFirst());
267 >        assertNull(q.pollFirst());
268      }
269  
270      /**
# Line 281 | Line 273 | public class ConcurrentSkipListSetTest e
273      public void testPollLast() {
274          ConcurrentSkipListSet q = populatedSet(SIZE);
275          for (int i = SIZE-1; i >= 0; --i) {
276 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
276 >            assertEquals(i, q.pollLast());
277          }
278 <        assertNull(q.pollFirst());
278 >        assertNull(q.pollFirst());
279      }
280  
289
281      /**
282       * remove(x) removes x and returns true if present
283       */
284      public void testRemoveElement() {
285          ConcurrentSkipListSet q = populatedSet(SIZE);
286 <        for (int i = 1; i < SIZE; i+=2) {
287 <            assertTrue(q.remove(new Integer(i)));
288 <        }
289 <        for (int i = 0; i < SIZE; i+=2) {
290 <            assertTrue(q.remove(new Integer(i)));
291 <            assertFalse(q.remove(new Integer(i+1)));
286 >        for (int i = 1; i < SIZE; i += 2) {
287 >            assertTrue(q.contains(i));
288 >            assertTrue(q.remove(i));
289 >            assertFalse(q.contains(i));
290 >            assertTrue(q.contains(i-1));
291 >        }
292 >        for (int i = 0; i < SIZE; i += 2) {
293 >            assertTrue(q.contains(i));
294 >            assertTrue(q.remove(i));
295 >            assertFalse(q.contains(i));
296 >            assertFalse(q.remove(i+1));
297 >            assertFalse(q.contains(i+1));
298          }
299          assertTrue(q.isEmpty());
300      }
# Line 371 | Line 368 | public class ConcurrentSkipListSetTest e
368              assertTrue(q.removeAll(p));
369              assertEquals(SIZE-i, q.size());
370              for (int j = 0; j < i; ++j) {
371 <                Integer I = (Integer)(p.pollFirst());
372 <                assertFalse(q.contains(I));
371 >                Integer x = (Integer)(p.pollFirst());
372 >                assertFalse(q.contains(x));
373              }
374          }
375      }
376  
380
381
377      /**
378       * lower returns preceding element
379       */
# Line 395 | Line 390 | public class ConcurrentSkipListSetTest e
390  
391          Object e4 = q.lower(zero);
392          assertNull(e4);
398
393      }
394  
395      /**
# Line 414 | Line 408 | public class ConcurrentSkipListSetTest e
408  
409          Object e4 = q.higher(six);
410          assertNull(e4);
417
411      }
412  
413      /**
# Line 433 | Line 426 | public class ConcurrentSkipListSetTest e
426  
427          Object e4 = q.floor(zero);
428          assertNull(e4);
436
429      }
430  
431      /**
# Line 452 | Line 444 | public class ConcurrentSkipListSetTest e
444  
445          Object e4 = q.ceiling(six);
446          assertNull(e4);
455
447      }
448  
449      /**
450 <     * toArray contains all elements
450 >     * toArray contains all elements in sorted order
451       */
452      public void testToArray() {
453          ConcurrentSkipListSet q = populatedSet(SIZE);
454 <        Object[] o = q.toArray();
455 <        Arrays.sort(o);
456 <        for(int i = 0; i < o.length; i++)
466 <            assertEquals(o[i], q.pollFirst());
454 >        Object[] o = q.toArray();
455 >        for (int i = 0; i < o.length; i++)
456 >            assertSame(o[i], q.pollFirst());
457      }
458  
459      /**
460 <     * toArray(a) contains all elements
460 >     * toArray(a) contains all elements in sorted order
461       */
462      public void testToArray2() {
463 <        ConcurrentSkipListSet q = populatedSet(SIZE);
464 <        Integer[] ints = new Integer[SIZE];
465 <        ints = (Integer[])q.toArray(ints);
466 <        Arrays.sort(ints);
467 <        for(int i = 0; i < ints.length; i++)
478 <            assertEquals(ints[i], q.pollFirst());
463 >        ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
464 >        Integer[] ints = new Integer[SIZE];
465 >        assertSame(ints, q.toArray(ints));
466 >        for (int i = 0; i < ints.length; i++)
467 >            assertSame(ints[i], q.pollFirst());
468      }
469  
470      /**
# Line 483 | Line 472 | public class ConcurrentSkipListSetTest e
472       */
473      public void testIterator() {
474          ConcurrentSkipListSet q = populatedSet(SIZE);
475 <        int i = 0;
476 <        Iterator it = q.iterator();
477 <        while(it.hasNext()) {
475 >        Iterator it = q.iterator();
476 >        int i;
477 >        for (i = 0; it.hasNext(); i++)
478              assertTrue(q.contains(it.next()));
490            ++i;
491        }
479          assertEquals(i, SIZE);
480 +        assertIteratorExhausted(it);
481      }
482  
483      /**
484       * iterator of empty set has no elements
485       */
486      public void testEmptyIterator() {
487 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
488 <        int i = 0;
489 <        Iterator it = q.iterator();
502 <        while(it.hasNext()) {
503 <            assertTrue(q.contains(it.next()));
504 <            ++i;
505 <        }
506 <        assertEquals(i, 0);
487 >        NavigableSet s = new ConcurrentSkipListSet();
488 >        assertIteratorExhausted(s.iterator());
489 >        assertIteratorExhausted(s.descendingSet().iterator());
490      }
491  
492      /**
493       * iterator.remove removes current element
494       */
495 <    public void testIteratorRemove () {
495 >    public void testIteratorRemove() {
496          final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
497          q.add(new Integer(2));
498          q.add(new Integer(1));
# Line 525 | Line 508 | public class ConcurrentSkipListSetTest e
508          assertFalse(it.hasNext());
509      }
510  
528
511      /**
512       * toString contains toStrings of elements
513       */
# Line 533 | Line 515 | public class ConcurrentSkipListSetTest e
515          ConcurrentSkipListSet q = populatedSet(SIZE);
516          String s = q.toString();
517          for (int i = 0; i < SIZE; ++i) {
518 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
518 >            assertTrue(s.contains(String.valueOf(i)));
519          }
520      }
521  
522      /**
523       * A deserialized serialized set has same elements
524       */
525 <    public void testSerialization() {
526 <        ConcurrentSkipListSet q = populatedSet(SIZE);
527 <        try {
528 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
529 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
530 <            out.writeObject(q);
531 <            out.close();
532 <
533 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
534 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
535 <            ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
554 <            assertEquals(q.size(), r.size());
555 <            while (!q.isEmpty())
556 <                assertEquals(q.pollFirst(), r.pollFirst());
557 <        } catch(Exception e){
558 <            e.printStackTrace();
559 <            unexpectedException();
525 >    public void testSerialization() throws Exception {
526 >        NavigableSet x = populatedSet(SIZE);
527 >        NavigableSet y = serialClone(x);
528 >
529 >        assertNotSame(x, y);
530 >        assertEquals(x.size(), y.size());
531 >        assertEquals(x, y);
532 >        assertEquals(y, x);
533 >        while (!x.isEmpty()) {
534 >            assertFalse(y.isEmpty());
535 >            assertEquals(x.pollFirst(), y.pollFirst());
536          }
537 +        assertTrue(y.isEmpty());
538      }
539  
540      /**
# Line 680 | Line 657 | public class ConcurrentSkipListSetTest e
657      }
658  
659      Random rnd = new Random(666);
683    BitSet bs;
660  
661      /**
662       * Subsets of subsets subdivide correctly
663       */
664 <    public void testRecursiveSubSets() {
665 <        int setSize = 1000;
666 <        Class cl = ConcurrentSkipListSet.class;
664 >    public void testRecursiveSubSets() throws Exception {
665 >        int setSize = expensiveTests ? 1000 : 100;
666 >        Class cl = ConcurrentSkipListSet.class;
667  
668          NavigableSet<Integer> set = newSet(cl);
669 <        bs = new BitSet(setSize);
669 >        BitSet bs = new BitSet(setSize);
670  
671 <        populate(set, setSize);
672 <        check(set,                 0, setSize - 1, true);
673 <        check(set.descendingSet(), 0, setSize - 1, false);
674 <
675 <        mutateSet(set, 0, setSize - 1);
676 <        check(set,                 0, setSize - 1, true);
677 <        check(set.descendingSet(), 0, setSize - 1, false);
671 >        populate(set, setSize, bs);
672 >        check(set,                 0, setSize - 1, true, bs);
673 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
674 >
675 >        mutateSet(set, 0, setSize - 1, bs);
676 >        check(set,                 0, setSize - 1, true, bs);
677 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
678  
679          bashSubSet(set.subSet(0, true, setSize, false),
680 <                   0, setSize - 1, true);
680 >                   0, setSize - 1, true, bs);
681      }
682  
683 <    static NavigableSet<Integer> newSet(Class cl) {
684 <        NavigableSet<Integer> result = null;
685 <        try {
686 <            result = (NavigableSet<Integer>) cl.newInstance();
687 <        } catch(Exception e) {
688 <            fail();
689 <        }
690 <        assertEquals(result.size(), 0);
683 >    /**
684 >     * addAll is idempotent
685 >     */
686 >    public void testAddAll_idempotent() throws Exception {
687 >        Set x = populatedSet(SIZE);
688 >        Set y = new ConcurrentSkipListSet(x);
689 >        y.addAll(x);
690 >        assertEquals(x, y);
691 >        assertEquals(y, x);
692 >    }
693 >
694 >    static NavigableSet<Integer> newSet(Class cl) throws Exception {
695 >        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
696 >        assertEquals(0, result.size());
697          assertFalse(result.iterator().hasNext());
698          return result;
699      }
700  
701 <    void populate(NavigableSet<Integer> set, int limit) {
701 >    void populate(NavigableSet<Integer> set, int limit, BitSet bs) {
702          for (int i = 0, n = 2 * limit / 3; i < n; i++) {
703              int element = rnd.nextInt(limit);
704 <            put(set, element);
704 >            put(set, element, bs);
705          }
706      }
707  
708 <    void mutateSet(NavigableSet<Integer> set, int min, int max) {
708 >    void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) {
709          int size = set.size();
710          int rangeSize = max - min + 1;
711  
712          // Remove a bunch of entries directly
713          for (int i = 0, n = rangeSize / 2; i < n; i++) {
714 <            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
714 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
715          }
716  
717          // Remove a bunch of entries with iterator
718 <        for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
718 >        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
719              if (rnd.nextBoolean()) {
720                  bs.clear(it.next());
721                  it.remove();
# Line 743 | Line 725 | public class ConcurrentSkipListSetTest e
725          // Add entries till we're back to original size
726          while (set.size() < size) {
727              int element = min + rnd.nextInt(rangeSize);
728 <            assertTrue(element >= min && element<= max);
729 <            put(set, element);
728 >            assertTrue(element >= min && element <= max);
729 >            put(set, element, bs);
730          }
731      }
732  
733 <    void mutateSubSet(NavigableSet<Integer> set, int min, int max) {
733 >    void mutateSubSet(NavigableSet<Integer> set, int min, int max,
734 >                      BitSet bs) {
735          int size = set.size();
736          int rangeSize = max - min + 1;
737  
738          // Remove a bunch of entries directly
739          for (int i = 0, n = rangeSize / 2; i < n; i++) {
740 <            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
740 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
741          }
742  
743          // Remove a bunch of entries with iterator
744 <        for(Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
744 >        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
745              if (rnd.nextBoolean()) {
746                  bs.clear(it.next());
747                  it.remove();
# Line 768 | Line 751 | public class ConcurrentSkipListSetTest e
751          // Add entries till we're back to original size
752          while (set.size() < size) {
753              int element = min - 5 + rnd.nextInt(rangeSize + 10);
754 <            if (element >= min && element<= max) {
755 <                put(set, element);
754 >            if (element >= min && element <= max) {
755 >                put(set, element, bs);
756              } else {
757                  try {
758                      set.add(element);
759 <                    fail();
760 <                } catch(IllegalArgumentException e) {
778 <                    // expected
779 <                }
759 >                    shouldThrow();
760 >                } catch (IllegalArgumentException success) {}
761              }
762          }
763      }
764  
765 <    void put(NavigableSet<Integer> set, int element) {
765 >    void put(NavigableSet<Integer> set, int element, BitSet bs) {
766          if (set.add(element))
767              bs.set(element);
768      }
769  
770 <    void remove(NavigableSet<Integer> set, int element) {
770 >    void remove(NavigableSet<Integer> set, int element, BitSet bs) {
771          if (set.remove(element))
772              bs.clear(element);
773      }
774  
775      void bashSubSet(NavigableSet<Integer> set,
776 <                    int min, int max, boolean ascending) {
777 <        check(set, min, max, ascending);
778 <        check(set.descendingSet(), min, max, !ascending);
779 <
780 <        mutateSubSet(set, min, max);
781 <        check(set, min, max, ascending);
782 <        check(set.descendingSet(), min, max, !ascending);
776 >                    int min, int max, boolean ascending,
777 >                    BitSet bs) {
778 >        check(set, min, max, ascending, bs);
779 >        check(set.descendingSet(), min, max, !ascending, bs);
780 >
781 >        mutateSubSet(set, min, max, bs);
782 >        check(set, min, max, ascending, bs);
783 >        check(set.descendingSet(), min, max, !ascending, bs);
784  
785          // Recurse
786          if (max - min < 2)
# Line 810 | Line 792 | public class ConcurrentSkipListSetTest e
792          NavigableSet<Integer> hm = set.headSet(midPoint, incl);
793          if (ascending) {
794              if (rnd.nextBoolean())
795 <                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
795 >                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs);
796              else
797                  bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
798 <                           false);
798 >                           false, bs);
799          } else {
800              if (rnd.nextBoolean())
801 <                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
801 >                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs);
802              else
803                  bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
804 <                           true);
804 >                           true, bs);
805          }
806  
807          // tailSet - pick direction and endpoint inclusion randomly
# Line 827 | Line 809 | public class ConcurrentSkipListSetTest e
809          NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
810          if (ascending) {
811              if (rnd.nextBoolean())
812 <                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
812 >                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs);
813              else
814                  bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
815 <                           false);
815 >                           false, bs);
816          } else {
817              if (rnd.nextBoolean()) {
818 <                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
818 >                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs);
819              } else {
820                  bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
821 <                           true);
821 >                           true, bs);
822              }
823          }
824  
# Line 853 | Line 835 | public class ConcurrentSkipListSetTest e
835                  endpoints[0], lowIncl, endpoints[1], highIncl);
836              if (rnd.nextBoolean())
837                  bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
838 <                           endpoints[1] - (highIncl ? 0 : 1), true);
838 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
839              else
840                  bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
841 <                           endpoints[1] - (highIncl ? 0 : 1), false);
841 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
842          } else {
843              NavigableSet<Integer> sm = set.subSet(
844                  endpoints[1], highIncl, endpoints[0], lowIncl);
845              if (rnd.nextBoolean())
846                  bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
847 <                           endpoints[1] - (highIncl ? 0 : 1), false);
847 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
848              else
849                  bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
850 <                           endpoints[1] - (highIncl ? 0 : 1), true);
850 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
851          }
852      }
853  
# Line 873 | Line 855 | public class ConcurrentSkipListSetTest e
855       * min and max are both inclusive.  If max < min, interval is empty.
856       */
857      void check(NavigableSet<Integer> set,
858 <                      final int min, final int max, final boolean ascending) {
859 <       class ReferenceSet {
858 >               final int min, final int max, final boolean ascending,
859 >               final BitSet bs) {
860 >        class ReferenceSet {
861              int lower(int element) {
862                  return ascending ?
863                      lowerAscending(element) : higherAscending(element);
# Line 909 | Line 892 | public class ConcurrentSkipListSetTest e
892                  // BitSet should support this! Test would run much faster
893                  while (element >= min) {
894                      if (bs.get(element))
895 <                        return(element);
895 >                        return element;
896                      element--;
897                  }
898                  return -1;
# Line 944 | Line 927 | public class ConcurrentSkipListSetTest e
927              if (bsContainsI)
928                  size++;
929          }
930 <        assertEquals(set.size(), size);
930 >        assertEquals(size, set.size());
931  
932          // Test contents using contains elementSet iterator
933          int size2 = 0;
# Line 975 | Line 958 | public class ConcurrentSkipListSetTest e
958              assertEq(rs.last(),  -1);
959              try {
960                  set.first();
961 <                fail();
962 <            } catch(NoSuchElementException e) {
980 <                // expected
981 <            }
961 >                shouldThrow();
962 >            } catch (NoSuchElementException success) {}
963              try {
964                  set.last();
965 <                fail();
966 <            } catch(NoSuchElementException e) {
986 <                // expected
987 <            }
965 >                shouldThrow();
966 >            } catch (NoSuchElementException success) {}
967          }
968      }
969  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines