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.40 by jsr166, Sat May 23 00:53:08 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 {
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 <        }
207 <        catch(ClassCastException success) {}
202 >        } catch (ClassCastException success) {}
203      }
204  
205      /**
206       * addAll(null) throws NPE
207       */
208      public void testAddAll1() {
209 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
210          try {
215            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
211              q.addAll(null);
212              shouldThrow();
213 <        }
219 <        catch (NullPointerException success) {}
213 >        } catch (NullPointerException success) {}
214      }
215 +
216      /**
217       * addAll of a collection with null elements throws NPE
218       */
219      public void testAddAll2() {
220 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
221 +        Integer[] ints = new Integer[SIZE];
222          try {
226            ConcurrentSkipListSet q = new ConcurrentSkipListSet();
227            Integer[] ints = new Integer[SIZE];
223              q.addAll(Arrays.asList(ints));
224              shouldThrow();
225 <        }
231 <        catch (NullPointerException success) {}
225 >        } catch (NullPointerException success) {}
226      }
227 +
228      /**
229       * addAll of a collection with any null elements throws NPE after
230       * possibly adding some elements
231       */
232      public void testAddAll3() {
233 +        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
234 +        Integer[] ints = new Integer[SIZE];
235 +        for (int i = 0; i < SIZE - 1; ++i)
236 +            ints[i] = new Integer(i);
237          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);
238              q.addAll(Arrays.asList(ints));
239              shouldThrow();
240 <        }
246 <        catch (NullPointerException success) {}
240 >        } catch (NullPointerException success) {}
241      }
242  
243      /**
244       * Set contains all elements of successful addAll
245       */
246      public void testAddAll5() {
247 <        try {
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)
262 <                assertEquals(new Integer(i), q.pollFirst());
263 <        }
264 <        finally {}
247 >        Integer[] empty = new Integer[0];
248 >        Integer[] ints = new Integer[SIZE];
249 >        for (int i = 0; i < SIZE; ++i)
250 >            ints[i] = new Integer(SIZE - 1 - i);
251 >        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
252 >        assertFalse(q.addAll(Arrays.asList(empty)));
253 >        assertTrue(q.addAll(Arrays.asList(ints)));
254 >        for (int i = 0; i < SIZE; ++i)
255 >            assertEquals(i, q.pollFirst());
256      }
257  
258      /**
# Line 270 | Line 261 | public class ConcurrentSkipListSetTest e
261      public void testPollFirst() {
262          ConcurrentSkipListSet q = populatedSet(SIZE);
263          for (int i = 0; i < SIZE; ++i) {
264 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
264 >            assertEquals(i, q.pollFirst());
265          }
266 <        assertNull(q.pollFirst());
266 >        assertNull(q.pollFirst());
267      }
268  
269      /**
# Line 280 | Line 271 | public class ConcurrentSkipListSetTest e
271       */
272      public void testPollLast() {
273          ConcurrentSkipListSet q = populatedSet(SIZE);
274 <        for (int i = SIZE-1; i >= 0; --i) {
275 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
274 >        for (int i = SIZE - 1; i >= 0; --i) {
275 >            assertEquals(i, q.pollLast());
276          }
277 <        assertNull(q.pollFirst());
277 >        assertNull(q.pollFirst());
278      }
279  
289
280      /**
281       * remove(x) removes x and returns true if present
282       */
283      public void testRemoveElement() {
284          ConcurrentSkipListSet q = populatedSet(SIZE);
285 <        for (int i = 1; i < SIZE; i+=2) {
286 <            assertTrue(q.remove(new Integer(i)));
287 <        }
288 <        for (int i = 0; i < SIZE; i+=2) {
289 <            assertTrue(q.remove(new Integer(i)));
290 <            assertFalse(q.remove(new Integer(i+1)));
285 >        for (int i = 1; i < SIZE; i += 2) {
286 >            assertTrue(q.contains(i));
287 >            assertTrue(q.remove(i));
288 >            assertFalse(q.contains(i));
289 >            assertTrue(q.contains(i-1));
290 >        }
291 >        for (int i = 0; i < SIZE; i += 2) {
292 >            assertTrue(q.contains(i));
293 >            assertTrue(q.remove(i));
294 >            assertFalse(q.contains(i));
295 >            assertFalse(q.remove(i+1));
296 >            assertFalse(q.contains(i+1));
297          }
298          assertTrue(q.isEmpty());
299      }
300 <        
300 >
301      /**
302       * contains(x) reports true when elements added but not yet removed
303       */
# Line 356 | Line 352 | public class ConcurrentSkipListSetTest e
352                  assertTrue(changed);
353  
354              assertTrue(q.containsAll(p));
355 <            assertEquals(SIZE-i, q.size());
355 >            assertEquals(SIZE - i, q.size());
356              p.pollFirst();
357          }
358      }
# Line 369 | Line 365 | public class ConcurrentSkipListSetTest e
365              ConcurrentSkipListSet q = populatedSet(SIZE);
366              ConcurrentSkipListSet p = populatedSet(i);
367              assertTrue(q.removeAll(p));
368 <            assertEquals(SIZE-i, q.size());
368 >            assertEquals(SIZE - i, q.size());
369              for (int j = 0; j < i; ++j) {
370 <                Integer I = (Integer)(p.pollFirst());
371 <                assertFalse(q.contains(I));
370 >                Integer x = (Integer)(p.pollFirst());
371 >                assertFalse(q.contains(x));
372              }
373          }
374      }
375  
380    
381
376      /**
377       * lower returns preceding element
378       */
# Line 395 | Line 389 | public class ConcurrentSkipListSetTest e
389  
390          Object e4 = q.lower(zero);
391          assertNull(e4);
398
392      }
393  
394      /**
# Line 414 | Line 407 | public class ConcurrentSkipListSetTest e
407  
408          Object e4 = q.higher(six);
409          assertNull(e4);
417
410      }
411  
412      /**
# Line 433 | Line 425 | public class ConcurrentSkipListSetTest e
425  
426          Object e4 = q.floor(zero);
427          assertNull(e4);
436
428      }
429  
430      /**
# Line 452 | Line 443 | public class ConcurrentSkipListSetTest e
443  
444          Object e4 = q.ceiling(six);
445          assertNull(e4);
455
446      }
447  
448      /**
449 <     * toArray contains all elements
449 >     * toArray contains all elements in sorted order
450       */
451      public void testToArray() {
452          ConcurrentSkipListSet q = populatedSet(SIZE);
453 <        Object[] o = q.toArray();
454 <        Arrays.sort(o);
455 <        for(int i = 0; i < o.length; i++)
466 <            assertEquals(o[i], q.pollFirst());
453 >        Object[] o = q.toArray();
454 >        for (int i = 0; i < o.length; i++)
455 >            assertSame(o[i], q.pollFirst());
456      }
457  
458      /**
459 <     * toArray(a) contains all elements
459 >     * toArray(a) contains all elements in sorted order
460       */
461      public void testToArray2() {
462 <        ConcurrentSkipListSet q = populatedSet(SIZE);
463 <        Integer[] ints = new Integer[SIZE];
464 <        ints = (Integer[])q.toArray(ints);
465 <        Arrays.sort(ints);
466 <        for(int i = 0; i < ints.length; i++)
478 <            assertEquals(ints[i], q.pollFirst());
462 >        ConcurrentSkipListSet<Integer> q = populatedSet(SIZE);
463 >        Integer[] ints = new Integer[SIZE];
464 >        assertSame(ints, q.toArray(ints));
465 >        for (int i = 0; i < ints.length; i++)
466 >            assertSame(ints[i], q.pollFirst());
467      }
468 <    
468 >
469      /**
470       * iterator iterates through all elements
471       */
472      public void testIterator() {
473          ConcurrentSkipListSet q = populatedSet(SIZE);
474 <        int i = 0;
475 <        Iterator it = q.iterator();
476 <        while(it.hasNext()) {
474 >        Iterator it = q.iterator();
475 >        int i;
476 >        for (i = 0; it.hasNext(); i++)
477              assertTrue(q.contains(it.next()));
490            ++i;
491        }
478          assertEquals(i, SIZE);
479 +        assertIteratorExhausted(it);
480      }
481  
482      /**
483       * iterator of empty set has no elements
484       */
485      public void testEmptyIterator() {
486 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
487 <        int i = 0;
488 <        Iterator it = q.iterator();
502 <        while(it.hasNext()) {
503 <            assertTrue(q.contains(it.next()));
504 <            ++i;
505 <        }
506 <        assertEquals(i, 0);
486 >        NavigableSet s = new ConcurrentSkipListSet();
487 >        assertIteratorExhausted(s.iterator());
488 >        assertIteratorExhausted(s.descendingSet().iterator());
489      }
490  
491      /**
492       * iterator.remove removes current element
493       */
494 <    public void testIteratorRemove () {
494 >    public void testIteratorRemove() {
495          final ConcurrentSkipListSet q = new ConcurrentSkipListSet();
496          q.add(new Integer(2));
497          q.add(new Integer(1));
# Line 525 | Line 507 | public class ConcurrentSkipListSetTest e
507          assertFalse(it.hasNext());
508      }
509  
528
510      /**
511       * toString contains toStrings of elements
512       */
# Line 533 | Line 514 | public class ConcurrentSkipListSetTest e
514          ConcurrentSkipListSet q = populatedSet(SIZE);
515          String s = q.toString();
516          for (int i = 0; i < SIZE; ++i) {
517 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
517 >            assertTrue(s.contains(String.valueOf(i)));
518          }
519 <    }        
519 >    }
520  
521      /**
522 <     * A deserialized serialized set has same elements
522 >     * A deserialized serialized set has same elements
523       */
524 <    public void testSerialization() {
525 <        ConcurrentSkipListSet q = populatedSet(SIZE);
526 <        try {
527 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
528 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
529 <            out.writeObject(q);
530 <            out.close();
531 <
532 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
533 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
534 <            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();
524 >    public void testSerialization() throws Exception {
525 >        NavigableSet x = populatedSet(SIZE);
526 >        NavigableSet y = serialClone(x);
527 >
528 >        assertNotSame(x, y);
529 >        assertEquals(x.size(), y.size());
530 >        assertEquals(x, y);
531 >        assertEquals(y, x);
532 >        while (!x.isEmpty()) {
533 >            assertFalse(y.isEmpty());
534 >            assertEquals(x.pollFirst(), y.pollFirst());
535          }
536 +        assertTrue(y.isEmpty());
537      }
538  
539      /**
# Line 679 | Line 655 | public class ConcurrentSkipListSetTest e
655          assertEquals(4, set.size());
656      }
657  
658 +    Random rnd = new Random(666);
659 +
660 +    /**
661 +     * Subsets of subsets subdivide correctly
662 +     */
663 +    public void testRecursiveSubSets() throws Exception {
664 +        int setSize = expensiveTests ? 1000 : 100;
665 +        Class cl = ConcurrentSkipListSet.class;
666 +
667 +        NavigableSet<Integer> set = newSet(cl);
668 +        BitSet bs = new BitSet(setSize);
669 +
670 +        populate(set, setSize, bs);
671 +        check(set,                 0, setSize - 1, true, bs);
672 +        check(set.descendingSet(), 0, setSize - 1, false, bs);
673 +
674 +        mutateSet(set, 0, setSize - 1, bs);
675 +        check(set,                 0, setSize - 1, true, bs);
676 +        check(set.descendingSet(), 0, setSize - 1, false, bs);
677 +
678 +        bashSubSet(set.subSet(0, true, setSize, false),
679 +                   0, setSize - 1, true, bs);
680 +    }
681 +
682 +    /**
683 +     * addAll is idempotent
684 +     */
685 +    public void testAddAll_idempotent() throws Exception {
686 +        Set x = populatedSet(SIZE);
687 +        Set y = new ConcurrentSkipListSet(x);
688 +        y.addAll(x);
689 +        assertEquals(x, y);
690 +        assertEquals(y, x);
691 +    }
692 +
693 +    static NavigableSet<Integer> newSet(Class cl) throws Exception {
694 +        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
695 +        assertEquals(0, result.size());
696 +        assertFalse(result.iterator().hasNext());
697 +        return result;
698 +    }
699 +
700 +    void populate(NavigableSet<Integer> set, int limit, BitSet bs) {
701 +        for (int i = 0, n = 2 * limit / 3; i < n; i++) {
702 +            int element = rnd.nextInt(limit);
703 +            put(set, element, bs);
704 +        }
705 +    }
706 +
707 +    void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) {
708 +        int size = set.size();
709 +        int rangeSize = max - min + 1;
710 +
711 +        // Remove a bunch of entries directly
712 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
713 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
714 +        }
715 +
716 +        // Remove a bunch of entries with iterator
717 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
718 +            if (rnd.nextBoolean()) {
719 +                bs.clear(it.next());
720 +                it.remove();
721 +            }
722 +        }
723 +
724 +        // Add entries till we're back to original size
725 +        while (set.size() < size) {
726 +            int element = min + rnd.nextInt(rangeSize);
727 +            assertTrue(element >= min && element <= max);
728 +            put(set, element, bs);
729 +        }
730 +    }
731 +
732 +    void mutateSubSet(NavigableSet<Integer> set, int min, int max,
733 +                      BitSet bs) {
734 +        int size = set.size();
735 +        int rangeSize = max - min + 1;
736 +
737 +        // Remove a bunch of entries directly
738 +        for (int i = 0, n = rangeSize / 2; i < n; i++) {
739 +            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
740 +        }
741 +
742 +        // Remove a bunch of entries with iterator
743 +        for (Iterator<Integer> it = set.iterator(); it.hasNext(); ) {
744 +            if (rnd.nextBoolean()) {
745 +                bs.clear(it.next());
746 +                it.remove();
747 +            }
748 +        }
749 +
750 +        // Add entries till we're back to original size
751 +        while (set.size() < size) {
752 +            int element = min - 5 + rnd.nextInt(rangeSize + 10);
753 +            if (element >= min && element <= max) {
754 +                put(set, element, bs);
755 +            } else {
756 +                try {
757 +                    set.add(element);
758 +                    shouldThrow();
759 +                } catch (IllegalArgumentException success) {}
760 +            }
761 +        }
762 +    }
763 +
764 +    void put(NavigableSet<Integer> set, int element, BitSet bs) {
765 +        if (set.add(element))
766 +            bs.set(element);
767 +    }
768 +
769 +    void remove(NavigableSet<Integer> set, int element, BitSet bs) {
770 +        if (set.remove(element))
771 +            bs.clear(element);
772 +    }
773 +
774 +    void bashSubSet(NavigableSet<Integer> set,
775 +                    int min, int max, boolean ascending,
776 +                    BitSet bs) {
777 +        check(set, min, max, ascending, bs);
778 +        check(set.descendingSet(), min, max, !ascending, bs);
779 +
780 +        mutateSubSet(set, min, max, bs);
781 +        check(set, min, max, ascending, bs);
782 +        check(set.descendingSet(), min, max, !ascending, bs);
783 +
784 +        // Recurse
785 +        if (max - min < 2)
786 +            return;
787 +        int midPoint = (min + max) / 2;
788 +
789 +        // headSet - pick direction and endpoint inclusion randomly
790 +        boolean incl = rnd.nextBoolean();
791 +        NavigableSet<Integer> hm = set.headSet(midPoint, incl);
792 +        if (ascending) {
793 +            if (rnd.nextBoolean())
794 +                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs);
795 +            else
796 +                bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
797 +                           false, bs);
798 +        } else {
799 +            if (rnd.nextBoolean())
800 +                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs);
801 +            else
802 +                bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
803 +                           true, bs);
804 +        }
805 +
806 +        // tailSet - pick direction and endpoint inclusion randomly
807 +        incl = rnd.nextBoolean();
808 +        NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
809 +        if (ascending) {
810 +            if (rnd.nextBoolean())
811 +                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs);
812 +            else
813 +                bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
814 +                           false, bs);
815 +        } else {
816 +            if (rnd.nextBoolean()) {
817 +                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs);
818 +            } else {
819 +                bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
820 +                           true, bs);
821 +            }
822 +        }
823 +
824 +        // subSet - pick direction and endpoint inclusion randomly
825 +        int rangeSize = max - min + 1;
826 +        int[] endpoints = new int[2];
827 +        endpoints[0] = min + rnd.nextInt(rangeSize);
828 +        endpoints[1] = min + rnd.nextInt(rangeSize);
829 +        Arrays.sort(endpoints);
830 +        boolean lowIncl = rnd.nextBoolean();
831 +        boolean highIncl = rnd.nextBoolean();
832 +        if (ascending) {
833 +            NavigableSet<Integer> sm = set.subSet(
834 +                endpoints[0], lowIncl, endpoints[1], highIncl);
835 +            if (rnd.nextBoolean())
836 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
837 +                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
838 +            else
839 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
840 +                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
841 +        } else {
842 +            NavigableSet<Integer> sm = set.subSet(
843 +                endpoints[1], highIncl, endpoints[0], lowIncl);
844 +            if (rnd.nextBoolean())
845 +                bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
846 +                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
847 +            else
848 +                bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
849 +                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
850 +        }
851 +    }
852 +
853 +    /**
854 +     * min and max are both inclusive.  If max < min, interval is empty.
855 +     */
856 +    void check(NavigableSet<Integer> set,
857 +               final int min, final int max, final boolean ascending,
858 +               final BitSet bs) {
859 +        class ReferenceSet {
860 +            int lower(int element) {
861 +                return ascending ?
862 +                    lowerAscending(element) : higherAscending(element);
863 +            }
864 +            int floor(int element) {
865 +                return ascending ?
866 +                    floorAscending(element) : ceilingAscending(element);
867 +            }
868 +            int ceiling(int element) {
869 +                return ascending ?
870 +                    ceilingAscending(element) : floorAscending(element);
871 +            }
872 +            int higher(int element) {
873 +                return ascending ?
874 +                    higherAscending(element) : lowerAscending(element);
875 +            }
876 +            int first() {
877 +                return ascending ? firstAscending() : lastAscending();
878 +            }
879 +            int last() {
880 +                return ascending ? lastAscending() : firstAscending();
881 +            }
882 +            int lowerAscending(int element) {
883 +                return floorAscending(element - 1);
884 +            }
885 +            int floorAscending(int element) {
886 +                if (element < min)
887 +                    return -1;
888 +                else if (element > max)
889 +                    element = max;
890 +
891 +                // BitSet should support this! Test would run much faster
892 +                while (element >= min) {
893 +                    if (bs.get(element))
894 +                        return element;
895 +                    element--;
896 +                }
897 +                return -1;
898 +            }
899 +            int ceilingAscending(int element) {
900 +                if (element < min)
901 +                    element = min;
902 +                else if (element > max)
903 +                    return -1;
904 +                int result = bs.nextSetBit(element);
905 +                return result > max ? -1 : result;
906 +            }
907 +            int higherAscending(int element) {
908 +                return ceilingAscending(element + 1);
909 +            }
910 +            private int firstAscending() {
911 +                int result = ceilingAscending(min);
912 +                return result > max ? -1 : result;
913 +            }
914 +            private int lastAscending() {
915 +                int result = floorAscending(max);
916 +                return result < min ? -1 : result;
917 +            }
918 +        }
919 +        ReferenceSet rs = new ReferenceSet();
920 +
921 +        // Test contents using containsElement
922 +        int size = 0;
923 +        for (int i = min; i <= max; i++) {
924 +            boolean bsContainsI = bs.get(i);
925 +            assertEquals(bsContainsI, set.contains(i));
926 +            if (bsContainsI)
927 +                size++;
928 +        }
929 +        assertEquals(size, set.size());
930 +
931 +        // Test contents using contains elementSet iterator
932 +        int size2 = 0;
933 +        int previousElement = -1;
934 +        for (int element : set) {
935 +            assertTrue(bs.get(element));
936 +            size2++;
937 +            assertTrue(previousElement < 0 || (ascending ?
938 +                element - previousElement > 0 : element - previousElement < 0));
939 +            previousElement = element;
940 +        }
941 +        assertEquals(size2, size);
942 +
943 +        // Test navigation ops
944 +        for (int element = min - 1; element <= max + 1; element++) {
945 +            assertEq(set.lower(element), rs.lower(element));
946 +            assertEq(set.floor(element), rs.floor(element));
947 +            assertEq(set.higher(element), rs.higher(element));
948 +            assertEq(set.ceiling(element), rs.ceiling(element));
949 +        }
950 +
951 +        // Test extrema
952 +        if (set.size() != 0) {
953 +            assertEq(set.first(), rs.first());
954 +            assertEq(set.last(), rs.last());
955 +        } else {
956 +            assertEq(rs.first(), -1);
957 +            assertEq(rs.last(),  -1);
958 +            try {
959 +                set.first();
960 +                shouldThrow();
961 +            } catch (NoSuchElementException success) {}
962 +            try {
963 +                set.last();
964 +                shouldThrow();
965 +            } catch (NoSuchElementException success) {}
966 +        }
967 +    }
968 +
969 +    static void assertEq(Integer i, int j) {
970 +        if (i == null)
971 +            assertEquals(j, -1);
972 +        else
973 +            assertEquals((int) i, j);
974 +    }
975 +
976 +    static boolean eq(Integer i, int j) {
977 +        return i == null ? j == -1 : i == j;
978 +    }
979 +
980   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines