ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ConcurrentSkipListSubSetTest.java (file contents):
Revision 1.6 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.20 by jsr166, Tue May 31 16:16:23 2011 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 ConcurrentSkipListSubSetTest extends JSR166TestCase {
21      public static void main(String[] args) {
22 <        junit.textui.TestRunner.run (suite());
22 >        junit.textui.TestRunner.run(suite());
23      }
24      public static Test suite() {
25 <        return new TestSuite(ConcurrentSkipListSubSetTest.class);
25 >        return new TestSuite(ConcurrentSkipListSubSetTest.class);
26      }
27  
28      static class MyReverseComparator implements Comparator {
29          public int compare(Object x, Object y) {
30 <            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;
30 >            return ((Comparable)y).compareTo(x);
31          }
32      }
33  
# Line 31 | Line 35 | public class ConcurrentSkipListSubSetTes
35       * Create a set of given size containing consecutive
36       * Integers 0 ... n.
37       */
38 <    private NavigableSet populatedSet(int n) {
39 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
38 >    private NavigableSet<Integer> populatedSet(int n) {
39 >        ConcurrentSkipListSet<Integer> q =
40 >            new ConcurrentSkipListSet<Integer>();
41          assertTrue(q.isEmpty());
42  
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)
46 <            assertTrue(q.add(new Integer(i)));
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)
46 >            assertTrue(q.add(new Integer(i)));
47          assertTrue(q.add(new Integer(-n)));
48          assertTrue(q.add(new Integer(n)));
49          NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false);
50          assertFalse(s.isEmpty());
51 <        assertEquals(n, s.size());
51 >        assertEquals(n, s.size());
52          return s;
53      }
54  
# Line 61 | Line 66 | public class ConcurrentSkipListSubSetTes
66          q.add(zero);
67          q.add(seven);
68          NavigableSet s = q.subSet(one, true, seven, false);
69 <        assertEquals(5, s.size());
69 >        assertEquals(5, s.size());
70          return s;
71      }
72  
# Line 77 | Line 82 | public class ConcurrentSkipListSubSetTes
82          q.add(m4);
83          q.add(m5);
84          NavigableSet s = q.descendingSet();
85 <        assertEquals(5, s.size());
85 >        assertEquals(5, s.size());
86          return s;
87      }
88  
89      private static NavigableSet set0() {
90 <        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
90 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
91          assertTrue(set.isEmpty());
92          return set.tailSet(m1, true);
93      }
94  
95      private static NavigableSet dset0() {
96 <        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
96 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
97          assertTrue(set.isEmpty());
98          return set;
99      }
# Line 100 | Line 105 | public class ConcurrentSkipListSubSetTes
105          assertEquals(0, set0().size());
106      }
107  
103
108      /**
109       * isEmpty is true before add, false after
110       */
# Line 134 | Line 138 | public class ConcurrentSkipListSubSetTes
138       * add(null) throws NPE
139       */
140      public void testAddNull() {
141 <        try {
141 >        try {
142              NavigableSet q = set0();
143              q.add(null);
144              shouldThrow();
145 <        } catch (NullPointerException success) { }
145 >        } catch (NullPointerException success) {}
146      }
147  
148      /**
# Line 168 | Line 172 | public class ConcurrentSkipListSubSetTes
172              q.add(new Object());
173              q.add(new Object());
174              shouldThrow();
175 <        }
172 <        catch (ClassCastException success) {}
175 >        } catch (ClassCastException success) {}
176      }
177  
175
178      /**
179       * addAll(null) throws NPE
180       */
# Line 181 | Line 183 | public class ConcurrentSkipListSubSetTes
183              NavigableSet q = set0();
184              q.addAll(null);
185              shouldThrow();
186 <        }
185 <        catch (NullPointerException success) {}
186 >        } catch (NullPointerException success) {}
187      }
188 +
189      /**
190       * addAll of a collection with null elements throws NPE
191       */
# Line 193 | Line 195 | public class ConcurrentSkipListSubSetTes
195              Integer[] ints = new Integer[SIZE];
196              q.addAll(Arrays.asList(ints));
197              shouldThrow();
198 <        }
197 <        catch (NullPointerException success) {}
198 >        } catch (NullPointerException success) {}
199      }
200 +
201      /**
202       * addAll of a collection with any null elements throws NPE after
203       * possibly adding some elements
# Line 208 | Line 210 | public class ConcurrentSkipListSubSetTes
210                  ints[i] = new Integer(i+SIZE);
211              q.addAll(Arrays.asList(ints));
212              shouldThrow();
213 <        }
212 <        catch (NullPointerException success) {}
213 >        } catch (NullPointerException success) {}
214      }
215  
216      /**
217       * Set contains all elements of successful addAll
218       */
219      public void testAddAll5() {
220 <        try {
221 <            Integer[] empty = new Integer[0];
222 <            Integer[] ints = new Integer[SIZE];
223 <            for (int i = 0; i < SIZE; ++i)
224 <                ints[i] = new Integer(SIZE-1- i);
225 <            NavigableSet q = set0();
226 <            assertFalse(q.addAll(Arrays.asList(empty)));
227 <            assertTrue(q.addAll(Arrays.asList(ints)));
228 <            for (int i = 0; i < SIZE; ++i)
228 <                assertEquals(new Integer(i), q.pollFirst());
229 <        }
230 <        finally {}
220 >        Integer[] empty = new Integer[0];
221 >        Integer[] ints = new Integer[SIZE];
222 >        for (int i = 0; i < SIZE; ++i)
223 >            ints[i] = new Integer(SIZE-1- i);
224 >        NavigableSet q = set0();
225 >        assertFalse(q.addAll(Arrays.asList(empty)));
226 >        assertTrue(q.addAll(Arrays.asList(ints)));
227 >        for (int i = 0; i < SIZE; ++i)
228 >            assertEquals(new Integer(i), q.pollFirst());
229      }
230  
231      /**
# Line 236 | Line 234 | public class ConcurrentSkipListSubSetTes
234      public void testPoll() {
235          NavigableSet q = populatedSet(SIZE);
236          for (int i = 0; i < SIZE; ++i) {
237 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
237 >            assertEquals(i, q.pollFirst());
238          }
239 <        assertNull(q.pollFirst());
239 >        assertNull(q.pollFirst());
240      }
241  
242      /**
# Line 247 | Line 245 | public class ConcurrentSkipListSubSetTes
245      public void testRemoveElement() {
246          NavigableSet q = populatedSet(SIZE);
247          for (int i = 1; i < SIZE; i+=2) {
248 <            assertTrue(q.remove(new Integer(i)));
248 >            assertTrue(q.contains(i));
249 >            assertTrue(q.remove(i));
250 >            assertFalse(q.contains(i));
251 >            assertTrue(q.contains(i-1));
252          }
253          for (int i = 0; i < SIZE; i+=2) {
254 <            assertTrue(q.remove(new Integer(i)));
255 <            assertFalse(q.remove(new Integer(i+1)));
254 >            assertTrue(q.contains(i));
255 >            assertTrue(q.remove(i));
256 >            assertFalse(q.contains(i));
257 >            assertFalse(q.remove(i+1));
258 >            assertFalse(q.contains(i+1));
259          }
260          assertTrue(q.isEmpty());
261      }
# Line 331 | Line 335 | public class ConcurrentSkipListSubSetTes
335          }
336      }
337  
334
335
338      /**
339       * lower returns preceding element
340       */
# Line 349 | Line 351 | public class ConcurrentSkipListSubSetTes
351  
352          Object e4 = q.lower(zero);
353          assertNull(e4);
352
354      }
355  
356      /**
# Line 368 | Line 369 | public class ConcurrentSkipListSubSetTes
369  
370          Object e4 = q.higher(six);
371          assertNull(e4);
371
372      }
373  
374      /**
# Line 387 | Line 387 | public class ConcurrentSkipListSubSetTes
387  
388          Object e4 = q.floor(zero);
389          assertNull(e4);
390
390      }
391  
392      /**
# Line 406 | Line 405 | public class ConcurrentSkipListSubSetTes
405  
406          Object e4 = q.ceiling(six);
407          assertNull(e4);
409
408      }
409  
410      /**
411 <     * toArray contains all elements
411 >     * toArray contains all elements in sorted order
412       */
413      public void testToArray() {
414          NavigableSet q = populatedSet(SIZE);
415 <        Object[] o = q.toArray();
416 <        Arrays.sort(o);
417 <        for (int i = 0; i < o.length; i++)
420 <            assertEquals(o[i], q.pollFirst());
415 >        Object[] o = q.toArray();
416 >        for (int i = 0; i < o.length; i++)
417 >            assertSame(o[i], q.pollFirst());
418      }
419  
420      /**
421 <     * toArray(a) contains all elements
421 >     * toArray(a) contains all elements in sorted order
422       */
423      public void testToArray2() {
424 <        NavigableSet q = populatedSet(SIZE);
425 <        Integer[] ints = new Integer[SIZE];
426 <        ints = (Integer[])q.toArray(ints);
427 <        Arrays.sort(ints);
424 >        NavigableSet<Integer> q = populatedSet(SIZE);
425 >        Integer[] ints = new Integer[SIZE];
426 >        Integer[] array = q.toArray(ints);
427 >        assertSame(ints, array);
428          for (int i = 0; i < ints.length; i++)
429 <            assertEquals(ints[i], q.pollFirst());
429 >            assertSame(ints[i], q.pollFirst());
430      }
431  
432      /**
# Line 438 | Line 435 | public class ConcurrentSkipListSubSetTes
435      public void testIterator() {
436          NavigableSet q = populatedSet(SIZE);
437          int i = 0;
438 <        Iterator it = q.iterator();
438 >        Iterator it = q.iterator();
439          while (it.hasNext()) {
440              assertTrue(q.contains(it.next()));
441              ++i;
# Line 452 | Line 449 | public class ConcurrentSkipListSubSetTes
449      public void testEmptyIterator() {
450          NavigableSet q = set0();
451          int i = 0;
452 <        Iterator it = q.iterator();
452 >        Iterator it = q.iterator();
453          while (it.hasNext()) {
454              assertTrue(q.contains(it.next()));
455              ++i;
# Line 463 | Line 460 | public class ConcurrentSkipListSubSetTes
460      /**
461       * iterator.remove removes current element
462       */
463 <    public void testIteratorRemove () {
463 >    public void testIteratorRemove() {
464          final NavigableSet q = set0();
465          q.add(new Integer(2));
466          q.add(new Integer(1));
# Line 479 | Line 476 | public class ConcurrentSkipListSubSetTes
476          assertFalse(it.hasNext());
477      }
478  
482
479      /**
480       * toString contains toStrings of elements
481       */
# Line 487 | Line 483 | public class ConcurrentSkipListSubSetTes
483          NavigableSet q = populatedSet(SIZE);
484          String s = q.toString();
485          for (int i = 0; i < SIZE; ++i) {
486 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
486 >            assertTrue(s.contains(String.valueOf(i)));
487          }
488      }
489  
490      /**
491       * A deserialized serialized set has same elements
492       */
493 <    public void testSerialization() {
494 <        NavigableSet q = populatedSet(SIZE);
495 <        try {
496 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
497 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
498 <            out.writeObject(q);
499 <            out.close();
500 <
501 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
502 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
503 <            NavigableSet r = (NavigableSet)in.readObject();
508 <            assertEquals(q.size(), r.size());
509 <            while (!q.isEmpty())
510 <                assertEquals(q.pollFirst(), r.pollFirst());
511 <        } catch (Exception e){
512 <            e.printStackTrace();
513 <            unexpectedException();
493 >    public void testSerialization() throws Exception {
494 >        NavigableSet x = populatedSet(SIZE);
495 >        NavigableSet y = serialClone(x);
496 >
497 >        assertTrue(x != y);
498 >        assertEquals(x.size(), y.size());
499 >        assertEquals(x, y);
500 >        assertEquals(y, x);
501 >        while (!x.isEmpty()) {
502 >            assertFalse(y.isEmpty());
503 >            assertEquals(x.pollFirst(), y.pollFirst());
504          }
505 +        assertTrue(y.isEmpty());
506      }
507  
508      /**
# Line 652 | Line 643 | public class ConcurrentSkipListSubSetTes
643       * add(null) throws NPE
644       */
645      public void testDescendingAddNull() {
646 <        try {
646 >        try {
647              NavigableSet q = dset0();
648              q.add(null);
649              shouldThrow();
650 <        } catch (NullPointerException success) { }
650 >        } catch (NullPointerException success) {}
651      }
652  
653      /**
# Line 686 | Line 677 | public class ConcurrentSkipListSubSetTes
677              q.add(new Object());
678              q.add(new Object());
679              shouldThrow();
680 <        }
690 <        catch (ClassCastException success) {}
680 >        } catch (ClassCastException success) {}
681      }
682  
693
683      /**
684       * addAll(null) throws NPE
685       */
# Line 699 | Line 688 | public class ConcurrentSkipListSubSetTes
688              NavigableSet q = dset0();
689              q.addAll(null);
690              shouldThrow();
691 <        }
703 <        catch (NullPointerException success) {}
691 >        } catch (NullPointerException success) {}
692      }
693 +
694      /**
695       * addAll of a collection with null elements throws NPE
696       */
# Line 711 | Line 700 | public class ConcurrentSkipListSubSetTes
700              Integer[] ints = new Integer[SIZE];
701              q.addAll(Arrays.asList(ints));
702              shouldThrow();
703 <        }
715 <        catch (NullPointerException success) {}
703 >        } catch (NullPointerException success) {}
704      }
705 +
706      /**
707       * addAll of a collection with any null elements throws NPE after
708       * possibly adding some elements
# Line 726 | Line 715 | public class ConcurrentSkipListSubSetTes
715                  ints[i] = new Integer(i+SIZE);
716              q.addAll(Arrays.asList(ints));
717              shouldThrow();
718 <        }
730 <        catch (NullPointerException success) {}
718 >        } catch (NullPointerException success) {}
719      }
720  
721      /**
722       * Set contains all elements of successful addAll
723       */
724      public void testDescendingAddAll5() {
725 <        try {
726 <            Integer[] empty = new Integer[0];
727 <            Integer[] ints = new Integer[SIZE];
728 <            for (int i = 0; i < SIZE; ++i)
729 <                ints[i] = new Integer(SIZE-1- i);
730 <            NavigableSet q = dset0();
731 <            assertFalse(q.addAll(Arrays.asList(empty)));
732 <            assertTrue(q.addAll(Arrays.asList(ints)));
733 <            for (int i = 0; i < SIZE; ++i)
746 <                assertEquals(new Integer(i), q.pollFirst());
747 <        }
748 <        finally {}
725 >        Integer[] empty = new Integer[0];
726 >        Integer[] ints = new Integer[SIZE];
727 >        for (int i = 0; i < SIZE; ++i)
728 >            ints[i] = new Integer(SIZE-1- i);
729 >        NavigableSet q = dset0();
730 >        assertFalse(q.addAll(Arrays.asList(empty)));
731 >        assertTrue(q.addAll(Arrays.asList(ints)));
732 >        for (int i = 0; i < SIZE; ++i)
733 >            assertEquals(new Integer(i), q.pollFirst());
734      }
735  
736      /**
# Line 754 | Line 739 | public class ConcurrentSkipListSubSetTes
739      public void testDescendingPoll() {
740          NavigableSet q = populatedSet(SIZE);
741          for (int i = 0; i < SIZE; ++i) {
742 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
742 >            assertEquals(i, q.pollFirst());
743          }
744 <        assertNull(q.pollFirst());
744 >        assertNull(q.pollFirst());
745      }
746  
747      /**
# Line 849 | Line 834 | public class ConcurrentSkipListSubSetTes
834          }
835      }
836  
852
853
837      /**
838       * lower returns preceding element
839       */
# Line 867 | Line 850 | public class ConcurrentSkipListSubSetTes
850  
851          Object e4 = q.lower(zero);
852          assertNull(e4);
870
853      }
854  
855      /**
# Line 886 | Line 868 | public class ConcurrentSkipListSubSetTes
868  
869          Object e4 = q.higher(m6);
870          assertNull(e4);
889
871      }
872  
873      /**
# Line 905 | Line 886 | public class ConcurrentSkipListSubSetTes
886  
887          Object e4 = q.floor(zero);
888          assertNull(e4);
908
889      }
890  
891      /**
# Line 924 | Line 904 | public class ConcurrentSkipListSubSetTes
904  
905          Object e4 = q.ceiling(m6);
906          assertNull(e4);
927
907      }
908  
909      /**
# Line 932 | Line 911 | public class ConcurrentSkipListSubSetTes
911       */
912      public void testDescendingToArray() {
913          NavigableSet q = populatedSet(SIZE);
914 <        Object[] o = q.toArray();
914 >        Object[] o = q.toArray();
915          Arrays.sort(o);
916 <        for (int i = 0; i < o.length; i++)
917 <            assertEquals(o[i], q.pollFirst());
916 >        for (int i = 0; i < o.length; i++)
917 >            assertEquals(o[i], q.pollFirst());
918      }
919  
920      /**
# Line 943 | Line 922 | public class ConcurrentSkipListSubSetTes
922       */
923      public void testDescendingToArray2() {
924          NavigableSet q = populatedSet(SIZE);
925 <        Integer[] ints = new Integer[SIZE];
926 <        ints = (Integer[])q.toArray(ints);
925 >        Integer[] ints = new Integer[SIZE];
926 >        assertSame(ints, q.toArray(ints));
927          Arrays.sort(ints);
928          for (int i = 0; i < ints.length; i++)
929              assertEquals(ints[i], q.pollFirst());
# Line 956 | Line 935 | public class ConcurrentSkipListSubSetTes
935      public void testDescendingIterator() {
936          NavigableSet q = populatedSet(SIZE);
937          int i = 0;
938 <        Iterator it = q.iterator();
938 >        Iterator it = q.iterator();
939          while (it.hasNext()) {
940              assertTrue(q.contains(it.next()));
941              ++i;
# Line 970 | Line 949 | public class ConcurrentSkipListSubSetTes
949      public void testDescendingEmptyIterator() {
950          NavigableSet q = dset0();
951          int i = 0;
952 <        Iterator it = q.iterator();
952 >        Iterator it = q.iterator();
953          while (it.hasNext()) {
954              assertTrue(q.contains(it.next()));
955              ++i;
# Line 981 | Line 960 | public class ConcurrentSkipListSubSetTes
960      /**
961       * iterator.remove removes current element
962       */
963 <    public void testDescendingIteratorRemove () {
963 >    public void testDescendingIteratorRemove() {
964          final NavigableSet q = dset0();
965          q.add(new Integer(2));
966          q.add(new Integer(1));
# Line 997 | Line 976 | public class ConcurrentSkipListSubSetTes
976          assertFalse(it.hasNext());
977      }
978  
1000
979      /**
980       * toString contains toStrings of elements
981       */
# Line 1005 | Line 983 | public class ConcurrentSkipListSubSetTes
983          NavigableSet q = populatedSet(SIZE);
984          String s = q.toString();
985          for (int i = 0; i < SIZE; ++i) {
986 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
986 >            assertTrue(s.contains(String.valueOf(i)));
987          }
988      }
989  
990      /**
991       * A deserialized serialized set has same elements
992       */
993 <    public void testDescendingSerialization() {
994 <        NavigableSet q = populatedSet(SIZE);
995 <        try {
996 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
997 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
998 <            out.writeObject(q);
999 <            out.close();
1000 <
1001 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1002 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1003 <            NavigableSet r = (NavigableSet)in.readObject();
1026 <            assertEquals(q.size(), r.size());
1027 <            while (!q.isEmpty())
1028 <                assertEquals(q.pollFirst(), r.pollFirst());
1029 <        } catch (Exception e){
1030 <            e.printStackTrace();
1031 <            unexpectedException();
993 >    public void testDescendingSerialization() throws Exception {
994 >        NavigableSet x = dset5();
995 >        NavigableSet y = serialClone(x);
996 >
997 >        assertTrue(x != y);
998 >        assertEquals(x.size(), y.size());
999 >        assertEquals(x, y);
1000 >        assertEquals(y, x);
1001 >        while (!x.isEmpty()) {
1002 >            assertFalse(y.isEmpty());
1003 >            assertEquals(x.pollFirst(), y.pollFirst());
1004          }
1005 +        assertTrue(y.isEmpty());
1006      }
1007  
1008      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines