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.17 by jsr166, Thu Nov 4 01:04:54 2010 UTC vs.
Revision 1.28 by jsr166, Tue Feb 21 02:04:17 2012 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.*;
8 > import java.util.Arrays;
9 > import java.util.BitSet;
10 > import java.util.Collection;
11 > import java.util.Comparator;
12 > import java.util.Iterator;
13 > import java.util.NavigableSet;
14 > import java.util.NoSuchElementException;
15 > import java.util.Random;
16 > import java.util.Set;
17 > import java.util.SortedSet;
18 > import java.util.concurrent.ConcurrentSkipListSet;
19  
20   public class ConcurrentSkipListSetTest extends JSR166TestCase {
21      public static void main(String[] args) {
# Line 24 | Line 32 | public class ConcurrentSkipListSetTest e
32      }
33  
34      /**
35 <     * Create a set of given size containing consecutive
35 >     * Returns a new set of given size containing consecutive
36       * Integers 0 ... n.
37       */
38 <    private ConcurrentSkipListSet populatedSet(int n) {
39 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
38 >    private ConcurrentSkipListSet<Integer> populatedSet(int n) {
39 >        ConcurrentSkipListSet<Integer> q =
40 >            new ConcurrentSkipListSet<Integer>();
41          assertTrue(q.isEmpty());
42          for (int i = n-1; i >= 0; i-=2)
43              assertTrue(q.add(new Integer(i)));
# Line 40 | Line 49 | public class ConcurrentSkipListSetTest e
49      }
50  
51      /**
52 <     * Create set of first 5 ints
52 >     * Returns a new set of first 5 ints.
53       */
54      private ConcurrentSkipListSet set5() {
55          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
# Line 268 | Line 277 | public class ConcurrentSkipListSetTest e
277          assertNull(q.pollFirst());
278      }
279  
271
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)));
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.remove(new Integer(i)));
293 <            assertFalse(q.remove(new Integer(i+1)));
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      }
# Line 359 | Line 373 | public class ConcurrentSkipListSetTest e
373          }
374      }
375  
362
363
376      /**
377       * lower returns preceding element
378       */
# Line 447 | Line 459 | public class ConcurrentSkipListSetTest e
459       * toArray(a) contains all elements in sorted order
460       */
461      public void testToArray2() {
462 <        ConcurrentSkipListSet q = populatedSet(SIZE);
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++)
# Line 479 | Line 491 | public class ConcurrentSkipListSetTest e
491              assertTrue(q.contains(it.next()));
492              ++i;
493          }
494 <        assertEquals(i, 0);
494 >        assertEquals(0, i);
495      }
496  
497      /**
# Line 501 | Line 513 | public class ConcurrentSkipListSetTest e
513          assertFalse(it.hasNext());
514      }
515  
504
516      /**
517       * toString contains toStrings of elements
518       */
# Line 509 | Line 520 | public class ConcurrentSkipListSetTest e
520          ConcurrentSkipListSet q = populatedSet(SIZE);
521          String s = q.toString();
522          for (int i = 0; i < SIZE; ++i) {
523 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
523 >            assertTrue(s.contains(String.valueOf(i)));
524          }
525      }
526  
# Line 517 | Line 528 | public class ConcurrentSkipListSetTest e
528       * A deserialized serialized set has same elements
529       */
530      public void testSerialization() throws Exception {
531 <        ConcurrentSkipListSet q = populatedSet(SIZE);
532 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
533 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
534 <        out.writeObject(q);
535 <        out.close();
536 <
537 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
538 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
539 <        ConcurrentSkipListSet r = (ConcurrentSkipListSet)in.readObject();
540 <        assertEquals(q.size(), r.size());
541 <        while (!q.isEmpty())
542 <            assertEquals(q.pollFirst(), r.pollFirst());
531 >        NavigableSet x = populatedSet(SIZE);
532 >        NavigableSet y = serialClone(x);
533 >
534 >        assertTrue(x != y);
535 >        assertEquals(x.size(), y.size());
536 >        assertEquals(x, y);
537 >        assertEquals(y, x);
538 >        while (!x.isEmpty()) {
539 >            assertFalse(y.isEmpty());
540 >            assertEquals(x.pollFirst(), y.pollFirst());
541 >        }
542 >        assertTrue(y.isEmpty());
543      }
544  
545      /**
# Line 651 | Line 662 | public class ConcurrentSkipListSetTest e
662      }
663  
664      Random rnd = new Random(666);
654    BitSet bs;
665  
666      /**
667       * Subsets of subsets subdivide correctly
# Line 661 | Line 671 | public class ConcurrentSkipListSetTest e
671          Class cl = ConcurrentSkipListSet.class;
672  
673          NavigableSet<Integer> set = newSet(cl);
674 <        bs = new BitSet(setSize);
674 >        BitSet bs = new BitSet(setSize);
675  
676 <        populate(set, setSize);
677 <        check(set,                 0, setSize - 1, true);
678 <        check(set.descendingSet(), 0, setSize - 1, false);
679 <
680 <        mutateSet(set, 0, setSize - 1);
681 <        check(set,                 0, setSize - 1, true);
682 <        check(set.descendingSet(), 0, setSize - 1, false);
676 >        populate(set, setSize, bs);
677 >        check(set,                 0, setSize - 1, true, bs);
678 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
679 >
680 >        mutateSet(set, 0, setSize - 1, bs);
681 >        check(set,                 0, setSize - 1, true, bs);
682 >        check(set.descendingSet(), 0, setSize - 1, false, bs);
683  
684          bashSubSet(set.subSet(0, true, setSize, false),
685 <                   0, setSize - 1, true);
685 >                   0, setSize - 1, true, bs);
686      }
687  
688      static NavigableSet<Integer> newSet(Class cl) throws Exception {
689          NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
690 <        assertEquals(result.size(), 0);
690 >        assertEquals(0, result.size());
691          assertFalse(result.iterator().hasNext());
692          return result;
693      }
694  
695 <    void populate(NavigableSet<Integer> set, int limit) {
695 >    void populate(NavigableSet<Integer> set, int limit, BitSet bs) {
696          for (int i = 0, n = 2 * limit / 3; i < n; i++) {
697              int element = rnd.nextInt(limit);
698 <            put(set, element);
698 >            put(set, element, bs);
699          }
700      }
701  
702 <    void mutateSet(NavigableSet<Integer> set, int min, int max) {
702 >    void mutateSet(NavigableSet<Integer> set, int min, int max, BitSet bs) {
703          int size = set.size();
704          int rangeSize = max - min + 1;
705  
706          // Remove a bunch of entries directly
707          for (int i = 0, n = rangeSize / 2; i < n; i++) {
708 <            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
708 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
709          }
710  
711          // Remove a bunch of entries with iterator
# Line 710 | Line 720 | public class ConcurrentSkipListSetTest e
720          while (set.size() < size) {
721              int element = min + rnd.nextInt(rangeSize);
722              assertTrue(element >= min && element<= max);
723 <            put(set, element);
723 >            put(set, element, bs);
724          }
725      }
726  
727 <    void mutateSubSet(NavigableSet<Integer> set, int min, int max) {
727 >    void mutateSubSet(NavigableSet<Integer> set, int min, int max,
728 >                      BitSet bs) {
729          int size = set.size();
730          int rangeSize = max - min + 1;
731  
732          // Remove a bunch of entries directly
733          for (int i = 0, n = rangeSize / 2; i < n; i++) {
734 <            remove(set, min - 5 + rnd.nextInt(rangeSize + 10));
734 >            remove(set, min - 5 + rnd.nextInt(rangeSize + 10), bs);
735          }
736  
737          // Remove a bunch of entries with iterator
# Line 735 | Line 746 | public class ConcurrentSkipListSetTest e
746          while (set.size() < size) {
747              int element = min - 5 + rnd.nextInt(rangeSize + 10);
748              if (element >= min && element<= max) {
749 <                put(set, element);
749 >                put(set, element, bs);
750              } else {
751                  try {
752                      set.add(element);
# Line 745 | Line 756 | public class ConcurrentSkipListSetTest e
756          }
757      }
758  
759 <    void put(NavigableSet<Integer> set, int element) {
759 >    void put(NavigableSet<Integer> set, int element, BitSet bs) {
760          if (set.add(element))
761              bs.set(element);
762      }
763  
764 <    void remove(NavigableSet<Integer> set, int element) {
764 >    void remove(NavigableSet<Integer> set, int element, BitSet bs) {
765          if (set.remove(element))
766              bs.clear(element);
767      }
768  
769      void bashSubSet(NavigableSet<Integer> set,
770 <                    int min, int max, boolean ascending) {
771 <        check(set, min, max, ascending);
772 <        check(set.descendingSet(), min, max, !ascending);
773 <
774 <        mutateSubSet(set, min, max);
775 <        check(set, min, max, ascending);
776 <        check(set.descendingSet(), min, max, !ascending);
770 >                    int min, int max, boolean ascending,
771 >                    BitSet bs) {
772 >        check(set, min, max, ascending, bs);
773 >        check(set.descendingSet(), min, max, !ascending, bs);
774 >
775 >        mutateSubSet(set, min, max, bs);
776 >        check(set, min, max, ascending, bs);
777 >        check(set.descendingSet(), min, max, !ascending, bs);
778  
779          // Recurse
780          if (max - min < 2)
# Line 774 | Line 786 | public class ConcurrentSkipListSetTest e
786          NavigableSet<Integer> hm = set.headSet(midPoint, incl);
787          if (ascending) {
788              if (rnd.nextBoolean())
789 <                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true);
789 >                bashSubSet(hm, min, midPoint - (incl ? 0 : 1), true, bs);
790              else
791                  bashSubSet(hm.descendingSet(), min, midPoint - (incl ? 0 : 1),
792 <                           false);
792 >                           false, bs);
793          } else {
794              if (rnd.nextBoolean())
795 <                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false);
795 >                bashSubSet(hm, midPoint + (incl ? 0 : 1), max, false, bs);
796              else
797                  bashSubSet(hm.descendingSet(), midPoint + (incl ? 0 : 1), max,
798 <                           true);
798 >                           true, bs);
799          }
800  
801          // tailSet - pick direction and endpoint inclusion randomly
# Line 791 | Line 803 | public class ConcurrentSkipListSetTest e
803          NavigableSet<Integer> tm = set.tailSet(midPoint,incl);
804          if (ascending) {
805              if (rnd.nextBoolean())
806 <                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true);
806 >                bashSubSet(tm, midPoint + (incl ? 0 : 1), max, true, bs);
807              else
808                  bashSubSet(tm.descendingSet(), midPoint + (incl ? 0 : 1), max,
809 <                           false);
809 >                           false, bs);
810          } else {
811              if (rnd.nextBoolean()) {
812 <                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false);
812 >                bashSubSet(tm, min, midPoint - (incl ? 0 : 1), false, bs);
813              } else {
814                  bashSubSet(tm.descendingSet(), min, midPoint - (incl ? 0 : 1),
815 <                           true);
815 >                           true, bs);
816              }
817          }
818  
# Line 817 | Line 829 | public class ConcurrentSkipListSetTest e
829                  endpoints[0], lowIncl, endpoints[1], highIncl);
830              if (rnd.nextBoolean())
831                  bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
832 <                           endpoints[1] - (highIncl ? 0 : 1), true);
832 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
833              else
834                  bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
835 <                           endpoints[1] - (highIncl ? 0 : 1), false);
835 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
836          } else {
837              NavigableSet<Integer> sm = set.subSet(
838                  endpoints[1], highIncl, endpoints[0], lowIncl);
839              if (rnd.nextBoolean())
840                  bashSubSet(sm, endpoints[0] + (lowIncl ? 0 : 1),
841 <                           endpoints[1] - (highIncl ? 0 : 1), false);
841 >                           endpoints[1] - (highIncl ? 0 : 1), false, bs);
842              else
843                  bashSubSet(sm.descendingSet(), endpoints[0] + (lowIncl ? 0 : 1),
844 <                           endpoints[1] - (highIncl ? 0 : 1), true);
844 >                           endpoints[1] - (highIncl ? 0 : 1), true, bs);
845          }
846      }
847  
# Line 837 | Line 849 | public class ConcurrentSkipListSetTest e
849       * min and max are both inclusive.  If max < min, interval is empty.
850       */
851      void check(NavigableSet<Integer> set,
852 <                      final int min, final int max, final boolean ascending) {
853 <       class ReferenceSet {
852 >               final int min, final int max, final boolean ascending,
853 >               final BitSet bs) {
854 >        class ReferenceSet {
855              int lower(int element) {
856                  return ascending ?
857                      lowerAscending(element) : higherAscending(element);
# Line 908 | Line 921 | public class ConcurrentSkipListSetTest e
921              if (bsContainsI)
922                  size++;
923          }
924 <        assertEquals(set.size(), size);
924 >        assertEquals(size, set.size());
925  
926          // Test contents using contains elementSet iterator
927          int size2 = 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines