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.8 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.50 by jsr166, Mon May 28 21:36:13 2018 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);
# Line 19 | Line 29 | public class ConcurrentSkipListSetTest e
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
38 <     * Integers 0 ... n.
37 >     * Returns a new set of given size containing consecutive
38 >     * Integers 0 ... n - 1.
39       */
40 <    private ConcurrentSkipListSet populatedSet(int n) {
41 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
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 44 | 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 70 | 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 80 | Line 86 | public class ConcurrentSkipListSetTest e
86       */
87      public void testConstructor4() {
88          try {
89 <            Integer[] ints = new Integer[SIZE];
84 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
89 >            new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE]));
90              shouldThrow();
91          } catch (NullPointerException success) {}
92      }
# Line 90 | 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];
95 <            for (int i = 0; i < SIZE-1; ++i)
96 <                ints[i] = new Integer(i);
97 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
102 >            new ConcurrentSkipListSet(Arrays.asList(ints));
103              shouldThrow();
104          } catch (NullPointerException success) {}
105      }
# Line 115 | Line 120 | public class ConcurrentSkipListSetTest e
120       * The comparator used in constructor is used
121       */
122      public void testConstructor7() {
123 <        try {
124 <            MyReverseComparator cmp = new MyReverseComparator();
125 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
126 <            assertEquals(cmp, q.comparator());
127 <            Integer[] ints = new Integer[SIZE];
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)
127 <                assertEquals(ints[i], q.pollFirst());
128 <        }
129 <        finally {}
123 >        MyReverseComparator cmp = new MyReverseComparator();
124 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(cmp);
125 >        assertEquals(cmp, q.comparator());
126 >        Integer[] ints = new Integer[SIZE];
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)
131 >            assertEquals(ints[i], q.pollFirst());
132      }
133  
134      /**
# Line 149 | 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 162 | Line 164 | public class ConcurrentSkipListSetTest e
164       * add(null) throws NPE
165       */
166      public void testAddNull() {
167 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
168          try {
166            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
169              q.add(null);
170              shouldThrow();
171          } catch (NullPointerException success) {}
# Line 191 | 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 {
195            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
196            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 {
208            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
217              q.addAll(null);
218              shouldThrow();
219          } catch (NullPointerException success) {}
220      }
221 +
222      /**
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 {
218            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
219            Integer[] ints = new Integer[SIZE];
229              q.addAll(Arrays.asList(ints));
230              shouldThrow();
231          } catch (NullPointerException success) {}
232      }
233 +
234      /**
235       * addAll of a collection with any null elements throws NPE after
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 {
230            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
231            Integer[] ints = new Integer[SIZE];
232            for (int i = 0; i < SIZE-1; ++i)
233                ints[i] = new Integer(i);
244              q.addAll(Arrays.asList(ints));
245              shouldThrow();
246          } catch (NullPointerException success) {}
# Line 240 | Line 250 | public class ConcurrentSkipListSetTest e
250       * Set contains all elements of successful addAll
251       */
252      public void testAddAll5() {
253 <        try {
254 <            Integer[] empty = new Integer[0];
255 <            Integer[] ints = new Integer[SIZE];
256 <            for (int i = 0; i < SIZE; ++i)
257 <                ints[i] = new Integer(SIZE-1-i);
258 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
259 <            assertFalse(q.addAll(Arrays.asList(empty)));
260 <            assertTrue(q.addAll(Arrays.asList(ints)));
261 <            for (int i = 0; i < SIZE; ++i)
252 <                assertEquals(new Integer(i), q.pollFirst());
253 <        }
254 <        finally {}
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);
257 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
258 >        assertFalse(q.addAll(Arrays.asList(empty)));
259 >        assertTrue(q.addAll(Arrays.asList(ints)));
260 >        for (int i = 0; i < SIZE; ++i)
261 >            assertEquals(i, q.pollFirst());
262      }
263  
264      /**
# Line 260 | Line 267 | public class ConcurrentSkipListSetTest e
267      public void testPollFirst() {
268          ConcurrentSkipListSet q = populatedSet(SIZE);
269          for (int i = 0; i < SIZE; ++i) {
270 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
270 >            assertEquals(i, q.pollFirst());
271          }
272          assertNull(q.pollFirst());
273      }
# Line 270 | 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) {
281 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
280 >        for (int i = SIZE - 1; i >= 0; --i) {
281 >            assertEquals(i, q.pollLast());
282          }
283          assertNull(q.pollFirst());
284      }
285  
279
286      /**
287       * remove(x) removes x and returns true if present
288       */
289      public void testRemoveElement() {
290          ConcurrentSkipListSet q = populatedSet(SIZE);
291 <        for (int i = 1; i < SIZE; i+=2) {
292 <            assertTrue(q.remove(new Integer(i)));
293 <        }
294 <        for (int i = 0; i < SIZE; i+=2) {
295 <            assertTrue(q.remove(new Integer(i)));
296 <            assertFalse(q.remove(new Integer(i+1)));
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));
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));
303          }
304          assertTrue(q.isEmpty());
305      }
# Line 346 | 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 359 | 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      }
381  
370
371
382      /**
383       * lower returns preceding element
384       */
# Line 385 | Line 395 | public class ConcurrentSkipListSetTest e
395  
396          Object e4 = q.lower(zero);
397          assertNull(e4);
388
398      }
399  
400      /**
# Line 404 | Line 413 | public class ConcurrentSkipListSetTest e
413  
414          Object e4 = q.higher(six);
415          assertNull(e4);
407
416      }
417  
418      /**
# Line 423 | Line 431 | public class ConcurrentSkipListSetTest e
431  
432          Object e4 = q.floor(zero);
433          assertNull(e4);
426
434      }
435  
436      /**
# Line 442 | Line 449 | public class ConcurrentSkipListSetTest e
449  
450          Object e4 = q.ceiling(six);
451          assertNull(e4);
445
452      }
453  
454      /**
455 <     * toArray contains all elements
455 >     * toArray contains all elements in sorted order
456       */
457      public void testToArray() {
458          ConcurrentSkipListSet q = populatedSet(SIZE);
459 <        Object[] o = q.toArray();
460 <        Arrays.sort(o);
461 <        for (int i = 0; i < o.length; i++)
462 <            assertEquals(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      /**
467 <     * toArray(a) contains all elements
467 >     * toArray(a) contains all elements in sorted order
468       */
469      public void testToArray2() {
470 <        ConcurrentSkipListSet q = populatedSet(SIZE);
470 >        ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
471          Integer[] ints = new Integer[SIZE];
472 <        ints = (Integer[])q.toArray(ints);
473 <        Arrays.sort(ints);
474 <        for (int i = 0; i < ints.length; i++)
475 <            assertEquals(ints[i], q.pollFirst());
472 >        assertSame(ints, q.toArray(ints));
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(i, 0);
495 >        NavigableSet s = new ConcurrentSkipListSet();
496 >        assertIteratorExhausted(s.iterator());
497 >        assertIteratorExhausted(s.descendingSet().iterator());
498      }
499  
500      /**
501       * iterator.remove removes current element
502       */
503 <    public void testIteratorRemove () {
503 >    public void testIteratorRemove() {
504          final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
505          q.add(new Integer(2));
506          q.add(new Integer(1));
# Line 515 | Line 516 | public class ConcurrentSkipListSetTest e
516          assertFalse(it.hasNext());
517      }
518  
518
519      /**
520       * toString contains toStrings of elements
521       */
# Line 523 | Line 523 | public class ConcurrentSkipListSetTest e
523          ConcurrentSkipListSet q = populatedSet(SIZE);
524          String s = q.toString();
525          for (int i = 0; i < SIZE; ++i) {
526 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
526 >            assertTrue(s.contains(String.valueOf(i)));
527 >        }
528 >    }
529 >
530 >    /**
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 serialized set has same elements
549 >     * A deserialized/reserialized set equals original
550       */
551      public void testSerialization() throws Exception {
552 <        ConcurrentSkipListSet q = populatedSet(SIZE);
553 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
554 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
555 <        out.writeObject(q);
556 <        out.close();
557 <
558 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
559 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
560 <        ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
561 <        assertEquals(q.size(), r.size());
562 <        while (!q.isEmpty())
563 <            assertEquals(q.pollFirst(), r.pollFirst());
552 >        NavigableSet x = populatedSet(SIZE);
553 >        NavigableSet y = serialClone(x);
554 >
555 >        assertNotSame(x, y);
556 >        assertEquals(x.size(), y.size());
557 >        assertEquals(x, y);
558 >        assertEquals(y, x);
559 >        while (!x.isEmpty()) {
560 >            assertFalse(y.isEmpty());
561 >            assertEquals(x.pollFirst(), y.pollFirst());
562 >        }
563 >        assertTrue(y.isEmpty());
564      }
565  
566      /**
# Line 665 | Line 683 | public class ConcurrentSkipListSetTest e
683      }
684  
685      Random rnd = new Random(666);
668    BitSet bs;
686  
687      /**
688       * Subsets of subsets subdivide correctly
689       */
690 <    public void testRecursiveSubSets() {
691 <        int setSize = 1000;
690 >    public void testRecursiveSubSets() throws Exception {
691 >        int setSize = expensiveTests ? 1000 : 100;
692          Class cl = ConcurrentSkipListSet.class;
693  
694          NavigableSet<Integer> set = newSet(cl);
695 <        bs = new BitSet(setSize);
695 >        BitSet bs = new BitSet(setSize);
696  
697 <        populate(set, setSize);
698 <        check(set,                 0, setSize - 1, true);
699 <        check(set.descendingSet(), 0, setSize - 1, false);
700 <
701 <        mutateSet(set, 0, setSize - 1);
702 <        check(set,                 0, setSize - 1, true);
703 <        check(set.descendingSet(), 0, setSize - 1, false);
697 >        populate(set, setSize, bs);
698 >        check(set,                 0, setSize - 1, true, bs);
699 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
700 >
701 >        mutateSet(set, 0, setSize - 1, bs);
702 >        check(set,                 0, setSize - 1, true, bs);
703 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
704  
705          bashSubSet(set.subSet(0, true, setSize, false),
706 <                   0, setSize - 1, true);
706 >                   0, setSize - 1, true, bs);
707      }
708  
709 <    static NavigableSet<Integer> newSet(Class cl) {
710 <        NavigableSet<Integer> result = null;
711 <        try {
712 <            result = (NavigableSet<Integer>) cl.newInstance();
713 <        } catch (Exception e) {
714 <            fail();
715 <        }
716 <        assertEquals(result.size(), 0);
709 >    /**
710 >     * addAll is idempotent
711 >     */
712 >    public void testAddAll_idempotent() throws Exception {
713 >        Set x = populatedSet(SIZE);
714 >        Set y = new ConcurrentSkipListSet(x);
715 >        y.addAll(x);
716 >        assertEquals(x, y);
717 >        assertEquals(y, x);
718 >    }
719 >
720 >    static NavigableSet<Integer> newSet(Class cl) throws Exception {
721 >        NavigableSet<Integer> result =
722 >            (NavigableSet<Integer>) cl.getConstructor().newInstance();
723 >        assertEquals(0, result.size());
724          assertFalse(result.iterator().hasNext());
725          return result;
726      }
727  
728 <    void populate(NavigableSet<Integer> set, int limit) {
728 >    void populate(NavigableSet<Integer> set, int limit, BitSet bs) {
729          for (int i = 0, n = 2 * limit / 3; i < n; i++) {
730              int element = rnd.nextInt(limit);
731 <            put(set, element);
731 >            put(set, element, bs);
732          }
733      }
734  
735 <    void mutateSet(NavigableSet<Integer> set, int min, int max) {
735 >    void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) {
736          int size = set.size();
737          int rangeSize = max - min + 1;
738  
739          // Remove a bunch of entries directly
740          for (int i = 0, n = rangeSize / 2; i < n; i++) {
741 <            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
741 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
742          }
743  
744          // Remove a bunch of entries with iterator
# Line 728 | Line 752 | public class ConcurrentSkipListSetTest e
752          // Add entries till we're back to original size
753          while (set.size() < size) {
754              int element = min + rnd.nextInt(rangeSize);
755 <            assertTrue(element >= min && element<= max);
756 <            put(set, element);
755 >            assertTrue(element >= min && element <= max);
756 >            put(set, element, bs);
757          }
758      }
759  
760 <    void mutateSubSet(NavigableSet<Integer> set, int min, int max) {
760 >    void mutateSubSet(NavigableSet<Integer> set, int min, int max,
761 >                      BitSet bs) {
762          int size = set.size();
763          int rangeSize = max - min + 1;
764  
765          // Remove a bunch of entries directly
766          for (int i = 0, n = rangeSize / 2; i < n; i++) {
767 <            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
767 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
768          }
769  
770          // Remove a bunch of entries with iterator
# Line 753 | Line 778 | public class ConcurrentSkipListSetTest e
778          // Add entries till we're back to original size
779          while (set.size() < size) {
780              int element = min - 5 + rnd.nextInt(rangeSize + 10);
781 <            if (element >= min && element<= max) {
782 <                put(set, element);
781 >            if (element >= min && element <= max) {
782 >                put(set, element, bs);
783              } else {
784                  try {
785                      set.add(element);
786 <                    fail();
787 <                } catch (IllegalArgumentException e) {
763 <                    // expected
764 <                }
786 >                    shouldThrow();
787 >                } catch (IllegalArgumentException success) {}
788              }
789          }
790      }
791  
792 <    void put(NavigableSet<Integer> set, int element) {
792 >    void put(NavigableSet<Integer> set, int element, BitSet bs) {
793          if (set.add(element))
794              bs.set(element);
795      }
796  
797 <    void remove(NavigableSet<Integer> set, int element) {
797 >    void remove(NavigableSet<Integer> set, int element, BitSet bs) {
798          if (set.remove(element))
799              bs.clear(element);
800      }
801  
802      void bashSubSet(NavigableSet<Integer> set,
803 <                    int min, int max, boolean ascending) {
804 <        check(set, min, max, ascending);
805 <        check(set.descendingSet(), min, max, !ascending);
806 <
807 <        mutateSubSet(set, min, max);
808 <        check(set, min, max, ascending);
809 <        check(set.descendingSet(), min, max, !ascending);
803 >                    int min, int max, boolean ascending,
804 >                    BitSet bs) {
805 >        check(set, min, max, ascending, bs);
806 >        check(set.descendingSet(), min, max, !ascending, bs);
807 >
808 >        mutateSubSet(set, min, max, bs);
809 >        check(set, min, max, ascending, bs);
810 >        check(set.descendingSet(), min, max, !ascending, bs);
811  
812          // Recurse
813          if (max - min < 2)
# Line 795 | Line 819 | public class ConcurrentSkipListSetTest e
819          NavigableSet<Integer> hm = set.headSet(midPoint, incl);
820          if (ascending) {
821              if (rnd.nextBoolean())
822 <                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
822 >                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs);
823              else
824                  bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
825 <                           false);
825 >                           false, bs);
826          } else {
827              if (rnd.nextBoolean())
828 <                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
828 >                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs);
829              else
830                  bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
831 <                           true);
831 >                           true, bs);
832          }
833  
834          // tailSet - pick direction and endpoint inclusion randomly
# Line 812 | Line 836 | public class ConcurrentSkipListSetTest e
836          NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
837          if (ascending) {
838              if (rnd.nextBoolean())
839 <                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
839 >                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs);
840              else
841                  bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
842 <                           false);
842 >                           false, bs);
843          } else {
844              if (rnd.nextBoolean()) {
845 <                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
845 >                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs);
846              } else {
847                  bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
848 <                           true);
848 >                           true, bs);
849              }
850          }
851  
# Line 838 | Line 862 | public class ConcurrentSkipListSetTest e
862                  endpoints[0], lowIncl, endpoints[1], highIncl);
863              if (rnd.nextBoolean())
864                  bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
865 <                           endpoints[1] - (highIncl ? 0 : 1), true);
865 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
866              else
867                  bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
868 <                           endpoints[1] - (highIncl ? 0 : 1), false);
868 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
869          } else {
870              NavigableSet<Integer> sm = set.subSet(
871                  endpoints[1], highIncl, endpoints[0], lowIncl);
872              if (rnd.nextBoolean())
873                  bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
874 <                           endpoints[1] - (highIncl ? 0 : 1), false);
874 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
875              else
876                  bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
877 <                           endpoints[1] - (highIncl ? 0 : 1), true);
877 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
878          }
879      }
880  
# Line 858 | Line 882 | public class ConcurrentSkipListSetTest e
882       * min and max are both inclusive.  If max < min, interval is empty.
883       */
884      void check(NavigableSet<Integer> set,
885 <                      final int min, final int max, final boolean ascending) {
886 <       class ReferenceSet {
885 >               final int min, final int max, final boolean ascending,
886 >               final BitSet bs) {
887 >        class ReferenceSet {
888              int lower(int element) {
889                  return ascending ?
890                      lowerAscending(element) : higherAscending(element);
# Line 894 | Line 919 | public class ConcurrentSkipListSetTest e
919                  // BitSet should support this! Test would run much faster
920                  while (element >= min) {
921                      if (bs.get(element))
922 <                        return(element);
922 >                        return element;
923                      element--;
924                  }
925                  return -1;
# Line 929 | Line 954 | public class ConcurrentSkipListSetTest e
954              if (bsContainsI)
955                  size++;
956          }
957 <        assertEquals(set.size(), size);
957 >        assertEquals(size, set.size());
958  
959          // Test contents using contains elementSet iterator
960          int size2 = 0;
# Line 960 | Line 985 | public class ConcurrentSkipListSetTest e
985              assertEq(rs.last(),  -1);
986              try {
987                  set.first();
988 <                fail();
989 <            } catch (NoSuchElementException e) {
965 <                // expected
966 <            }
988 >                shouldThrow();
989 >            } catch (NoSuchElementException success) {}
990              try {
991                  set.last();
992 <                fail();
993 <            } catch (NoSuchElementException e) {
971 <                // expected
972 <            }
992 >                shouldThrow();
993 >            } catch (NoSuchElementException success) {}
994          }
995      }
996  
# Line 981 | 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