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.1 by dl, Tue Dec 28 16:15:59 2004 UTC vs.
Revision 1.43 by jsr166, Thu Jun 2 01:15:46 2016 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 {
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 <
67 >
68      /**
69       * A new set has unbounded capacity
70       */
# 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 81 | Line 87 | public class ConcurrentSkipListSetTest e
87       */
88      public void testConstructor4() {
89          try {
90 <            Integer[] ints = new Integer[SIZE];
85 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
90 >            new ConcurrentSkipListSet(Arrays.asList(new Integer[SIZE]));
91              shouldThrow();
92 <        }
88 <        catch (NullPointerException success) {}
92 >        } catch (NullPointerException success) {}
93      }
94  
95      /**
96       * Initializing from Collection with some null elements throws NPE
97       */
98      public void testConstructor5() {
99 +        Integer[] ints = new Integer[SIZE];
100 +        for (int i = 0; i < SIZE - 1; ++i)
101 +            ints[i] = new Integer(i);
102          try {
103 <            Integer[] ints = new Integer[SIZE];
97 <            for (int i = 0; i < SIZE-1; ++i)
98 <                ints[i] = new Integer(i);
99 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
103 >            new ConcurrentSkipListSet(Arrays.asList(ints));
104              shouldThrow();
105 <        }
102 <        catch (NullPointerException success) {}
105 >        } catch (NullPointerException success) {}
106      }
107  
108      /**
109       * Set contains all elements of collection used to initialize
110       */
111      public void testConstructor6() {
112 <        try {
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)
115 <                assertEquals(ints[i], q.pollFirst());
116 <        }
117 <        finally {}
112 >        Integer[] ints = new Integer[SIZE];
113 >        for (int i = 0; i < SIZE; ++i)
114 >            ints[i] = new Integer(i);
115 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet(Arrays.asList(ints));
116 >        for (int i = 0; i < SIZE; ++i)
117 >            assertEquals(ints[i], q.pollFirst());
118      }
119  
120      /**
121       * The comparator used in constructor is used
122       */
123      public void testConstructor7() {
124 <        try {
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 <        finally {}
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)
132 >            assertEquals(ints[i], q.pollFirst());
133      }
134  
135      /**
# Line 155 | Line 152 | public class ConcurrentSkipListSetTest e
152      public void testSize() {
153          ConcurrentSkipListSet q = populatedSet(SIZE);
154          for (int i = 0; i < SIZE; ++i) {
155 <            assertEquals(SIZE-i, q.size());
155 >            assertEquals(SIZE - i, q.size());
156              q.pollFirst();
157          }
158          for (int i = 0; i < SIZE; ++i) {
# Line 168 | Line 165 | public class ConcurrentSkipListSetTest e
165       * add(null) throws NPE
166       */
167      public void testAddNull() {
168 <        try {
169 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
168 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
169 >        try {
170              q.add(null);
171              shouldThrow();
172 <        } catch (NullPointerException success) { }  
172 >        } catch (NullPointerException success) {}
173      }
174  
175      /**
# Line 197 | Line 194 | public class ConcurrentSkipListSetTest e
194       * Add of non-Comparable throws CCE
195       */
196      public void testAddNonComparable() {
197 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
198          try {
201            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
202            q.add(new Object());
199              q.add(new Object());
200              q.add(new Object());
201              shouldThrow();
202 +        } catch (ClassCastException success) {
203 +            assertTrue(q.size() < 2);
204 +            for (int i = 0, size = q.size(); i < size; i++)
205 +                assertTrue(q.pollFirst().getClass() == Object.class);
206 +            assertNull(q.pollFirst());
207 +            assertTrue(q.isEmpty());
208 +            assertEquals(0, q.size());
209          }
207        catch(ClassCastException success) {}
210      }
211  
212      /**
213       * addAll(null) throws NPE
214       */
215      public void testAddAll1() {
216 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
217          try {
215            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
218              q.addAll(null);
219              shouldThrow();
220 <        }
219 <        catch (NullPointerException success) {}
220 >        } catch (NullPointerException success) {}
221      }
222 +
223      /**
224       * addAll of a collection with null elements throws NPE
225       */
226      public void testAddAll2() {
227 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
228 +        Integer[] ints = new Integer[SIZE];
229          try {
226            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
227            Integer[] ints = new Integer[SIZE];
230              q.addAll(Arrays.asList(ints));
231              shouldThrow();
232 <        }
231 <        catch (NullPointerException success) {}
232 >        } catch (NullPointerException success) {}
233      }
234 +
235      /**
236       * addAll of a collection with any null elements throws NPE after
237       * possibly adding some elements
238       */
239      public void testAddAll3() {
240 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
241 +        Integer[] ints = new Integer[SIZE];
242 +        for (int i = 0; i < SIZE - 1; ++i)
243 +            ints[i] = new Integer(i);
244          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);
245              q.addAll(Arrays.asList(ints));
246              shouldThrow();
247 <        }
246 <        catch (NullPointerException success) {}
247 >        } catch (NullPointerException success) {}
248      }
249  
250      /**
251       * Set contains all elements of successful addAll
252       */
253      public void testAddAll5() {
254 <        try {
255 <            Integer[] empty = new Integer[0];
256 <            Integer[] ints = new Integer[SIZE];
257 <            for (int i = 0; i < SIZE; ++i)
258 <                ints[i] = new Integer(SIZE-1-i);
259 <            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
260 <            assertFalse(q.addAll(Arrays.asList(empty)));
261 <            assertTrue(q.addAll(Arrays.asList(ints)));
262 <            for (int i = 0; i < SIZE; ++i)
262 <                assertEquals(new Integer(i), q.pollFirst());
263 <        }
264 <        finally {}
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)
262 >            assertEquals(i, q.pollFirst());
263      }
264  
265      /**
# Line 270 | Line 268 | public class ConcurrentSkipListSetTest e
268      public void testPollFirst() {
269          ConcurrentSkipListSet q = populatedSet(SIZE);
270          for (int i = 0; i < SIZE; ++i) {
271 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
271 >            assertEquals(i, q.pollFirst());
272          }
273 <        assertNull(q.pollFirst());
273 >        assertNull(q.pollFirst());
274      }
275  
276      /**
# Line 280 | Line 278 | public class ConcurrentSkipListSetTest e
278       */
279      public void testPollLast() {
280          ConcurrentSkipListSet q = populatedSet(SIZE);
281 <        for (int i = SIZE-1; i >= 0; --i) {
282 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
281 >        for (int i = SIZE - 1; i >= 0; --i) {
282 >            assertEquals(i, q.pollLast());
283          }
284 <        assertNull(q.pollFirst());
284 >        assertNull(q.pollFirst());
285      }
286  
289
287      /**
288       * remove(x) removes x and returns true if present
289       */
290      public void testRemoveElement() {
291          ConcurrentSkipListSet q = populatedSet(SIZE);
292 <        for (int i = 1; i < SIZE; i+=2) {
293 <            assertTrue(q.remove(new Integer(i)));
294 <        }
295 <        for (int i = 0; i < SIZE; i+=2) {
296 <            assertTrue(q.remove(new Integer(i)));
297 <            assertFalse(q.remove(new Integer(i+1)));
292 >        for (int i = 1; i < SIZE; i += 2) {
293 >            assertTrue(q.contains(i));
294 >            assertTrue(q.remove(i));
295 >            assertFalse(q.contains(i));
296 >            assertTrue(q.contains(i - 1));
297 >        }
298 >        for (int i = 0; i < SIZE; i += 2) {
299 >            assertTrue(q.contains(i));
300 >            assertTrue(q.remove(i));
301 >            assertFalse(q.contains(i));
302 >            assertFalse(q.remove(i + 1));
303 >            assertFalse(q.contains(i + 1));
304          }
305          assertTrue(q.isEmpty());
306      }
307 <        
307 >
308      /**
309       * contains(x) reports true when elements added but not yet removed
310       */
# Line 356 | Line 359 | public class ConcurrentSkipListSetTest e
359                  assertTrue(changed);
360  
361              assertTrue(q.containsAll(p));
362 <            assertEquals(SIZE-i, q.size());
362 >            assertEquals(SIZE - i, q.size());
363              p.pollFirst();
364          }
365      }
# Line 369 | Line 372 | public class ConcurrentSkipListSetTest e
372              ConcurrentSkipListSet q = populatedSet(SIZE);
373              ConcurrentSkipListSet p = populatedSet(i);
374              assertTrue(q.removeAll(p));
375 <            assertEquals(SIZE-i, q.size());
375 >            assertEquals(SIZE - i, q.size());
376              for (int j = 0; j < i; ++j) {
377 <                Integer I = (Integer)(p.pollFirst());
378 <                assertFalse(q.contains(I));
377 >                Integer x = (Integer)(p.pollFirst());
378 >                assertFalse(q.contains(x));
379              }
380          }
381      }
382  
380    
381
383      /**
384       * lower returns preceding element
385       */
# Line 395 | Line 396 | public class ConcurrentSkipListSetTest e
396  
397          Object e4 = q.lower(zero);
398          assertNull(e4);
398
399      }
400  
401      /**
# Line 414 | Line 414 | public class ConcurrentSkipListSetTest e
414  
415          Object e4 = q.higher(six);
416          assertNull(e4);
417
417      }
418  
419      /**
# Line 433 | Line 432 | public class ConcurrentSkipListSetTest e
432  
433          Object e4 = q.floor(zero);
434          assertNull(e4);
436
435      }
436  
437      /**
# Line 452 | Line 450 | public class ConcurrentSkipListSetTest e
450  
451          Object e4 = q.ceiling(six);
452          assertNull(e4);
455
453      }
454  
455      /**
456 <     * toArray contains all elements
456 >     * toArray contains all elements in sorted order
457       */
458      public void testToArray() {
459          ConcurrentSkipListSet q = populatedSet(SIZE);
460 <        Object[] o = q.toArray();
461 <        Arrays.sort(o);
462 <        for(int i = 0; i < o.length; i++)
466 <            assertEquals(o[i], q.pollFirst());
460 >        Object[] o = q.toArray();
461 >        for (int i = 0; i < o.length; i++)
462 >            assertSame(o[i], q.pollFirst());
463      }
464  
465      /**
466 <     * toArray(a) contains all elements
466 >     * toArray(a) contains all elements in sorted order
467       */
468      public void testToArray2() {
469 <        ConcurrentSkipListSet q = populatedSet(SIZE);
470 <        Integer[] ints = new Integer[SIZE];
471 <        ints = (Integer[])q.toArray(ints);
472 <        Arrays.sort(ints);
473 <        for(int i = 0; i < ints.length; i++)
478 <            assertEquals(ints[i], q.pollFirst());
469 >        ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
470 >        Integer[] ints = new Integer[SIZE];
471 >        assertSame(ints, q.toArray(ints));
472 >        for (int i = 0; i < ints.length; i++)
473 >            assertSame(ints[i], q.pollFirst());
474      }
475 <    
475 >
476      /**
477       * iterator iterates through all elements
478       */
479      public void testIterator() {
480          ConcurrentSkipListSet q = populatedSet(SIZE);
481 <        int i = 0;
482 <        Iterator it = q.iterator();
483 <        while(it.hasNext()) {
481 >        Iterator it = q.iterator();
482 >        int i;
483 >        for (i = 0; it.hasNext(); i++)
484              assertTrue(q.contains(it.next()));
490            ++i;
491        }
485          assertEquals(i, SIZE);
486 +        assertIteratorExhausted(it);
487      }
488  
489      /**
490       * iterator of empty set has no elements
491       */
492      public void testEmptyIterator() {
493 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
494 <        int i = 0;
495 <        Iterator it = q.iterator();
502 <        while(it.hasNext()) {
503 <            assertTrue(q.contains(it.next()));
504 <            ++i;
505 <        }
506 <        assertEquals(i, 0);
493 >        NavigableSet s = new ConcurrentSkipListSet();
494 >        assertIteratorExhausted(s.iterator());
495 >        assertIteratorExhausted(s.descendingSet().iterator());
496      }
497  
498      /**
499       * iterator.remove removes current element
500       */
501 <    public void testIteratorRemove () {
501 >    public void testIteratorRemove() {
502          final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
503          q.add(new Integer(2));
504          q.add(new Integer(1));
# Line 525 | Line 514 | public class ConcurrentSkipListSetTest e
514          assertFalse(it.hasNext());
515      }
516  
528
517      /**
518       * toString contains toStrings of elements
519       */
# Line 533 | Line 521 | public class ConcurrentSkipListSetTest e
521          ConcurrentSkipListSet q = populatedSet(SIZE);
522          String s = q.toString();
523          for (int i = 0; i < SIZE; ++i) {
524 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
524 >            assertTrue(s.contains(String.valueOf(i)));
525          }
526 <    }        
526 >    }
527  
528      /**
529 <     * A deserialized serialized set has same elements
529 >     * A deserialized serialized set has same elements
530       */
531 <    public void testSerialization() {
532 <        ConcurrentSkipListSet q = populatedSet(SIZE);
533 <        try {
534 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
535 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
536 <            out.writeObject(q);
537 <            out.close();
538 <
539 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
540 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
541 <            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();
531 >    public void testSerialization() throws Exception {
532 >        NavigableSet x = populatedSet(SIZE);
533 >        NavigableSet y = serialClone(x);
534 >
535 >        assertNotSame(x, y);
536 >        assertEquals(x.size(), y.size());
537 >        assertEquals(x, y);
538 >        assertEquals(y, x);
539 >        while (!x.isEmpty()) {
540 >            assertFalse(y.isEmpty());
541 >            assertEquals(x.pollFirst(), y.pollFirst());
542          }
543 +        assertTrue(y.isEmpty());
544      }
545  
546      /**
# Line 679 | Line 662 | public class ConcurrentSkipListSetTest e
662          assertEquals(4, set.size());
663      }
664  
665 +    Random rnd = new Random(666);
666 +
667 +    /**
668 +     * Subsets of subsets subdivide correctly
669 +     */
670 +    public void testRecursiveSubSets() throws Exception {
671 +        int setSize = expensiveTests ? 1000 : 100;
672 +        Class cl = ConcurrentSkipListSet.class;
673 +
674 +        NavigableSet<Integer> set = newSet(cl);
675 +        BitSet bs = new BitSet(setSize);
676 +
677 +        populate(set, setSize, bs);
678 +        check(set,                 0, setSize - 1, true, bs);
679 +        check(set.descendingSet(), 0, setSize - 1, false, bs);
680 +
681 +        mutateSet(set, 0, setSize - 1, bs);
682 +        check(set,                 0, setSize - 1, true, bs);
683 +        check(set.descendingSet(), 0, setSize - 1, false, bs);
684 +
685 +        bashSubSet(set.subSet(0, true, setSize, false),
686 +                   0, setSize - 1, true, bs);
687 +    }
688 +
689 +    /**
690 +     * addAll is idempotent
691 +     */
692 +    public void testAddAll_idempotent() throws Exception {
693 +        Set x = populatedSet(SIZE);
694 +        Set y = new ConcurrentSkipListSet(x);
695 +        y.addAll(x);
696 +        assertEquals(x, y);
697 +        assertEquals(y, x);
698 +    }
699 +
700 +    static NavigableSet<Integer> newSet(Class cl) throws Exception {
701 +        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
702 +        assertEquals(0, result.size());
703 +        assertFalse(result.iterator().hasNext());
704 +        return result;
705 +    }
706 +
707 +    void populate(NavigableSet<Integer> set, int limit, BitSet bs) {
708 +        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
709 +            int element = rnd.nextInt(limit);
710 +            put(set, element, bs);
711 +        }
712 +    }
713 +
714 +    void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) {
715 +        int size = set.size();
716 +        int rangeSize = max - min + 1;
717 +
718 +        // Remove a bunch of entries directly
719 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
720 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
721 +        }
722 +
723 +        // Remove a bunch of entries with iterator
724 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
725 +            if (rnd.nextBoolean()) {
726 +                bs.clear(it.next());
727 +                it.remove();
728 +            }
729 +        }
730 +
731 +        // Add entries till we're back to original size
732 +        while (set.size() < size) {
733 +            int element = min + rnd.nextInt(rangeSize);
734 +            assertTrue(element >= min && element <= max);
735 +            put(set, element, bs);
736 +        }
737 +    }
738 +
739 +    void mutateSubSet(NavigableSet<Integer> set, int min, int max,
740 +                      BitSet bs) {
741 +        int size = set.size();
742 +        int rangeSize = max - min + 1;
743 +
744 +        // Remove a bunch of entries directly
745 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
746 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
747 +        }
748 +
749 +        // Remove a bunch of entries with iterator
750 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
751 +            if (rnd.nextBoolean()) {
752 +                bs.clear(it.next());
753 +                it.remove();
754 +            }
755 +        }
756 +
757 +        // Add entries till we're back to original size
758 +        while (set.size() < size) {
759 +            int element = min - 5 + rnd.nextInt(rangeSize + 10);
760 +            if (element >= min && element <= max) {
761 +                put(set, element, bs);
762 +            } else {
763 +                try {
764 +                    set.add(element);
765 +                    shouldThrow();
766 +                } catch (IllegalArgumentException success) {}
767 +            }
768 +        }
769 +    }
770 +
771 +    void put(NavigableSet<Integer> set, int element, BitSet bs) {
772 +        if (set.add(element))
773 +            bs.set(element);
774 +    }
775 +
776 +    void remove(NavigableSet<Integer> set, int element, BitSet bs) {
777 +        if (set.remove(element))
778 +            bs.clear(element);
779 +    }
780 +
781 +    void bashSubSet(NavigableSet<Integer> set,
782 +                    int min, int max, boolean ascending,
783 +                    BitSet bs) {
784 +        check(set, min, max, ascending, bs);
785 +        check(set.descendingSet(), min, max, !ascending, bs);
786 +
787 +        mutateSubSet(set, min, max, bs);
788 +        check(set, min, max, ascending, bs);
789 +        check(set.descendingSet(), min, max, !ascending, bs);
790 +
791 +        // Recurse
792 +        if (max - min < 2)
793 +            return;
794 +        int midPoint = (min + max) / 2;
795 +
796 +        // headSet - pick direction and endpoint inclusion randomly
797 +        boolean incl = rnd.nextBoolean();
798 +        NavigableSet<Integer> hm = set.headSet(midPoint, incl);
799 +        if (ascending) {
800 +            if (rnd.nextBoolean())
801 +                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs);
802 +            else
803 +                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
804 +                           false, bs);
805 +        } else {
806 +            if (rnd.nextBoolean())
807 +                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs);
808 +            else
809 +                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
810 +                           true, bs);
811 +        }
812 +
813 +        // tailSet - pick direction and endpoint inclusion randomly
814 +        incl = rnd.nextBoolean();
815 +        NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
816 +        if (ascending) {
817 +            if (rnd.nextBoolean())
818 +                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs);
819 +            else
820 +                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
821 +                           false, bs);
822 +        } else {
823 +            if (rnd.nextBoolean()) {
824 +                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs);
825 +            } else {
826 +                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
827 +                           true, bs);
828 +            }
829 +        }
830 +
831 +        // subSet - pick direction and endpoint inclusion randomly
832 +        int rangeSize = max - min + 1;
833 +        int[] endpoints = new int[2];
834 +        endpoints[0] = min + rnd.nextInt(rangeSize);
835 +        endpoints[1] = min + rnd.nextInt(rangeSize);
836 +        Arrays.sort(endpoints);
837 +        boolean lowIncl = rnd.nextBoolean();
838 +        boolean highIncl = rnd.nextBoolean();
839 +        if (ascending) {
840 +            NavigableSet<Integer> sm = set.subSet(
841 +                endpoints[0], lowIncl, endpoints[1], highIncl);
842 +            if (rnd.nextBoolean())
843 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
844 +                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
845 +            else
846 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
847 +                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
848 +        } else {
849 +            NavigableSet<Integer> sm = set.subSet(
850 +                endpoints[1], highIncl, endpoints[0], lowIncl);
851 +            if (rnd.nextBoolean())
852 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
853 +                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
854 +            else
855 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
856 +                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
857 +        }
858 +    }
859 +
860 +    /**
861 +     * min and max are both inclusive.  If max < min, interval is empty.
862 +     */
863 +    void check(NavigableSet<Integer> set,
864 +               final int min, final int max, final boolean ascending,
865 +               final BitSet bs) {
866 +        class ReferenceSet {
867 +            int lower(int element) {
868 +                return ascending ?
869 +                    lowerAscending(element) : higherAscending(element);
870 +            }
871 +            int floor(int element) {
872 +                return ascending ?
873 +                    floorAscending(element) : ceilingAscending(element);
874 +            }
875 +            int ceiling(int element) {
876 +                return ascending ?
877 +                    ceilingAscending(element) : floorAscending(element);
878 +            }
879 +            int higher(int element) {
880 +                return ascending ?
881 +                    higherAscending(element) : lowerAscending(element);
882 +            }
883 +            int first() {
884 +                return ascending ? firstAscending() : lastAscending();
885 +            }
886 +            int last() {
887 +                return ascending ? lastAscending() : firstAscending();
888 +            }
889 +            int lowerAscending(int element) {
890 +                return floorAscending(element - 1);
891 +            }
892 +            int floorAscending(int element) {
893 +                if (element < min)
894 +                    return -1;
895 +                else if (element > max)
896 +                    element = max;
897 +
898 +                // BitSet should support this! Test would run much faster
899 +                while (element >= min) {
900 +                    if (bs.get(element))
901 +                        return element;
902 +                    element--;
903 +                }
904 +                return -1;
905 +            }
906 +            int ceilingAscending(int element) {
907 +                if (element < min)
908 +                    element = min;
909 +                else if (element > max)
910 +                    return -1;
911 +                int result = bs.nextSetBit(element);
912 +                return result > max ? -1 : result;
913 +            }
914 +            int higherAscending(int element) {
915 +                return ceilingAscending(element + 1);
916 +            }
917 +            private int firstAscending() {
918 +                int result = ceilingAscending(min);
919 +                return result > max ? -1 : result;
920 +            }
921 +            private int lastAscending() {
922 +                int result = floorAscending(max);
923 +                return result < min ? -1 : result;
924 +            }
925 +        }
926 +        ReferenceSet rs = new ReferenceSet();
927 +
928 +        // Test contents using containsElement
929 +        int size = 0;
930 +        for (int i = min; i <= max; i++) {
931 +            boolean bsContainsI = bs.get(i);
932 +            assertEquals(bsContainsI, set.contains(i));
933 +            if (bsContainsI)
934 +                size++;
935 +        }
936 +        assertEquals(size, set.size());
937 +
938 +        // Test contents using contains elementSet iterator
939 +        int size2 = 0;
940 +        int previousElement = -1;
941 +        for (int element : set) {
942 +            assertTrue(bs.get(element));
943 +            size2++;
944 +            assertTrue(previousElement < 0 || (ascending ?
945 +                element - previousElement > 0 : element - previousElement < 0));
946 +            previousElement = element;
947 +        }
948 +        assertEquals(size2, size);
949 +
950 +        // Test navigation ops
951 +        for (int element = min - 1; element <= max + 1; element++) {
952 +            assertEq(set.lower(element), rs.lower(element));
953 +            assertEq(set.floor(element), rs.floor(element));
954 +            assertEq(set.higher(element), rs.higher(element));
955 +            assertEq(set.ceiling(element), rs.ceiling(element));
956 +        }
957 +
958 +        // Test extrema
959 +        if (set.size() != 0) {
960 +            assertEq(set.first(), rs.first());
961 +            assertEq(set.last(), rs.last());
962 +        } else {
963 +            assertEq(rs.first(), -1);
964 +            assertEq(rs.last(),  -1);
965 +            try {
966 +                set.first();
967 +                shouldThrow();
968 +            } catch (NoSuchElementException success) {}
969 +            try {
970 +                set.last();
971 +                shouldThrow();
972 +            } catch (NoSuchElementException success) {}
973 +        }
974 +    }
975 +
976 +    static void assertEq(Integer i, int j) {
977 +        if (i == null)
978 +            assertEquals(j, -1);
979 +        else
980 +            assertEquals((int) i, j);
981 +    }
982 +
983 +    static boolean eq(Integer i, int j) {
984 +        return (i == null) ? j == -1 : i == j;
985 +    }
986 +
987   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines