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.12 by jsr166, Tue Dec 1 09:48:13 2009 UTC vs.
Revision 1.36 by jsr166, Sat Mar 11 18:20:46 2017 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.Comparator;
9 > import java.util.Iterator;
10 > import java.util.NavigableSet;
11 > import java.util.SortedSet;
12 > import java.util.concurrent.ConcurrentSkipListSet;
13 >
14 > import junit.framework.Test;
15 > import junit.framework.TestSuite;
16  
17   public class ConcurrentSkipListSubSetTest extends JSR166TestCase {
18      public static void main(String[] args) {
19 <        junit.textui.TestRunner.run (suite());
19 >        main(suite(), args);
20      }
21      public static Test suite() {
22          return new TestSuite(ConcurrentSkipListSubSetTest.class);
# Line 24 | Line 29 | public class ConcurrentSkipListSubSetTes
29      }
30  
31      /**
32 <     * Create a set of given size containing consecutive
33 <     * Integers 0 ... n.
32 >     * Returns a new set of given size containing consecutive
33 >     * Integers 0 ... n - 1.
34       */
35 <    private NavigableSet populatedSet(int n) {
36 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
35 >    private static NavigableSet<Integer> populatedSet(int n) {
36 >        ConcurrentSkipListSet<Integer> q =
37 >            new ConcurrentSkipListSet<Integer>();
38          assertTrue(q.isEmpty());
39  
40 <        for (int i = n-1; i >= 0; i-=2)
40 >        for (int i = n - 1; i >= 0; i -= 2)
41              assertTrue(q.add(new Integer(i)));
42 <        for (int i = (n & 1); i < n; i+=2)
42 >        for (int i = (n & 1); i < n; i += 2)
43              assertTrue(q.add(new Integer(i)));
44          assertTrue(q.add(new Integer(-n)));
45          assertTrue(q.add(new Integer(n)));
# Line 44 | Line 50 | public class ConcurrentSkipListSubSetTes
50      }
51  
52      /**
53 <     * Create set of first 5 ints
53 >     * Returns a new set of first 5 ints.
54       */
55 <    private NavigableSet set5() {
55 >    private static NavigableSet set5() {
56          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
57          assertTrue(q.isEmpty());
58          q.add(one);
# Line 62 | Line 68 | public class ConcurrentSkipListSubSetTes
68      }
69  
70      /**
71 <     * Create set of first 5 negative ints
71 >     * Returns a new set of first 5 negative ints.
72       */
73 <    private NavigableSet dset5() {
73 >    private static NavigableSet dset5() {
74          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
75          assertTrue(q.isEmpty());
76          q.add(m1);
# Line 96 | Line 102 | public class ConcurrentSkipListSubSetTes
102          assertEquals(0, set0().size());
103      }
104  
99
105      /**
106       * isEmpty is true before add, false after
107       */
# Line 117 | Line 122 | public class ConcurrentSkipListSubSetTes
122      public void testSize() {
123          NavigableSet q = populatedSet(SIZE);
124          for (int i = 0; i < SIZE; ++i) {
125 <            assertEquals(SIZE-i, q.size());
125 >            assertEquals(SIZE - i, q.size());
126              q.pollFirst();
127          }
128          for (int i = 0; i < SIZE; ++i) {
# Line 130 | Line 135 | public class ConcurrentSkipListSubSetTes
135       * add(null) throws NPE
136       */
137      public void testAddNull() {
138 +        NavigableSet q = set0();
139          try {
134            NavigableSet q = set0();
140              q.add(null);
141              shouldThrow();
142          } catch (NullPointerException success) {}
# Line 158 | Line 163 | public class ConcurrentSkipListSubSetTes
163       * Add of non-Comparable throws CCE
164       */
165      public void testAddNonComparable() {
166 +        NavigableSet q = set0();
167          try {
162            NavigableSet q = set0();
163            q.add(new Object());
168              q.add(new Object());
169              q.add(new Object());
170              shouldThrow();
171          } catch (ClassCastException success) {}
172      }
173  
170
174      /**
175       * addAll(null) throws NPE
176       */
177      public void testAddAll1() {
178 +        NavigableSet q = set0();
179          try {
176            NavigableSet q = set0();
180              q.addAll(null);
181              shouldThrow();
182          } catch (NullPointerException success) {}
183      }
184 +
185      /**
186       * addAll of a collection with null elements throws NPE
187       */
188      public void testAddAll2() {
189 +        NavigableSet q = set0();
190 +        Integer[] ints = new Integer[SIZE];
191          try {
186            NavigableSet q = set0();
187            Integer[] ints = new Integer[SIZE];
192              q.addAll(Arrays.asList(ints));
193              shouldThrow();
194          } catch (NullPointerException success) {}
195      }
196 +
197      /**
198       * addAll of a collection with any null elements throws NPE after
199       * possibly adding some elements
200       */
201      public void testAddAll3() {
202 +        NavigableSet q = set0();
203 +        Integer[] ints = new Integer[SIZE];
204 +        for (int i = 0; i < SIZE - 1; ++i)
205 +            ints[i] = new Integer(i + SIZE);
206          try {
198            NavigableSet q = set0();
199            Integer[] ints = new Integer[SIZE];
200            for (int i = 0; i < SIZE-1; ++i)
201                ints[i] = new Integer(i+SIZE);
207              q.addAll(Arrays.asList(ints));
208              shouldThrow();
209          } catch (NullPointerException success) {}
# Line 211 | Line 216 | public class ConcurrentSkipListSubSetTes
216          Integer[] empty = new Integer[0];
217          Integer[] ints = new Integer[SIZE];
218          for (int i = 0; i < SIZE; ++i)
219 <            ints[i] = new Integer(SIZE-1- i);
219 >            ints[i] = new Integer(SIZE - 1 - i);
220          NavigableSet q = set0();
221          assertFalse(q.addAll(Arrays.asList(empty)));
222          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 235 | Line 240 | public class ConcurrentSkipListSubSetTes
240       */
241      public void testRemoveElement() {
242          NavigableSet q = populatedSet(SIZE);
243 <        for (int i = 1; i < SIZE; i+=2) {
244 <            assertTrue(q.remove(new Integer(i)));
245 <        }
246 <        for (int i = 0; i < SIZE; i+=2) {
247 <            assertTrue(q.remove(new Integer(i)));
248 <            assertFalse(q.remove(new Integer(i+1)));
243 >        for (int i = 1; i < SIZE; i += 2) {
244 >            assertTrue(q.contains(i));
245 >            assertTrue(q.remove(i));
246 >            assertFalse(q.contains(i));
247 >            assertTrue(q.contains(i - 1));
248 >        }
249 >        for (int i = 0; i < SIZE; i += 2) {
250 >            assertTrue(q.contains(i));
251 >            assertTrue(q.remove(i));
252 >            assertFalse(q.contains(i));
253 >            assertFalse(q.remove(i + 1));
254 >            assertFalse(q.contains(i + 1));
255          }
256          assertTrue(q.isEmpty());
257      }
# Line 299 | Line 310 | public class ConcurrentSkipListSubSetTes
310                  assertTrue(changed);
311  
312              assertTrue(q.containsAll(p));
313 <            assertEquals(SIZE-i, q.size());
313 >            assertEquals(SIZE - i, q.size());
314              p.pollFirst();
315          }
316      }
# Line 312 | Line 323 | public class ConcurrentSkipListSubSetTes
323              NavigableSet q = populatedSet(SIZE);
324              NavigableSet p = populatedSet(i);
325              assertTrue(q.removeAll(p));
326 <            assertEquals(SIZE-i, q.size());
326 >            assertEquals(SIZE - i, q.size());
327              for (int j = 0; j < i; ++j) {
328 <                Integer I = (Integer)(p.pollFirst());
329 <                assertFalse(q.contains(I));
328 >                Integer x = (Integer)(p.pollFirst());
329 >                assertFalse(q.contains(x));
330              }
331          }
332      }
333  
323
324
334      /**
335       * lower returns preceding element
336       */
# Line 395 | Line 404 | public class ConcurrentSkipListSubSetTes
404      }
405  
406      /**
407 <     * toArray contains all elements
407 >     * toArray contains all elements in sorted order
408       */
409      public void testToArray() {
410          NavigableSet q = populatedSet(SIZE);
411          Object[] o = q.toArray();
403        Arrays.sort(o);
412          for (int i = 0; i < o.length; i++)
413 <            assertEquals(o[i], q.pollFirst());
413 >            assertSame(o[i], q.pollFirst());
414      }
415  
416      /**
417 <     * toArray(a) contains all elements
417 >     * toArray(a) contains all elements in sorted order
418       */
419      public void testToArray2() {
420 <        NavigableSet q = populatedSet(SIZE);
420 >        NavigableSet<Integer> q = populatedSet(SIZE);
421          Integer[] ints = new Integer[SIZE];
422 <        ints = (Integer[])q.toArray(ints);
423 <        Arrays.sort(ints);
422 >        Integer[] array = q.toArray(ints);
423 >        assertSame(ints, array);
424          for (int i = 0; i < ints.length; i++)
425 <            assertEquals(ints[i], q.pollFirst());
425 >            assertSame(ints[i], q.pollFirst());
426      }
427  
428      /**
# Line 422 | Line 430 | public class ConcurrentSkipListSubSetTes
430       */
431      public void testIterator() {
432          NavigableSet q = populatedSet(SIZE);
425        int i = 0;
433          Iterator it = q.iterator();
434 <        while (it.hasNext()) {
434 >        int i;
435 >        for (i = 0; it.hasNext(); i++)
436              assertTrue(q.contains(it.next()));
429            ++i;
430        }
437          assertEquals(i, SIZE);
438 +        assertIteratorExhausted(it);
439      }
440  
441      /**
442       * iterator of empty set has no elements
443       */
444      public void testEmptyIterator() {
445 <        NavigableSet q = set0();
439 <        int i = 0;
440 <        Iterator it = q.iterator();
441 <        while (it.hasNext()) {
442 <            assertTrue(q.contains(it.next()));
443 <            ++i;
444 <        }
445 <        assertEquals(i, 0);
445 >        assertIteratorExhausted(set0().iterator());
446      }
447  
448      /**
449       * iterator.remove removes current element
450       */
451 <    public void testIteratorRemove () {
451 >    public void testIteratorRemove() {
452          final NavigableSet q = set0();
453          q.add(new Integer(2));
454          q.add(new Integer(1));
# Line 464 | Line 464 | public class ConcurrentSkipListSubSetTes
464          assertFalse(it.hasNext());
465      }
466  
467
467      /**
468       * toString contains toStrings of elements
469       */
# Line 472 | Line 471 | public class ConcurrentSkipListSubSetTes
471          NavigableSet q = populatedSet(SIZE);
472          String s = q.toString();
473          for (int i = 0; i < SIZE; ++i) {
474 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
474 >            assertTrue(s.contains(String.valueOf(i)));
475          }
476      }
477  
# Line 480 | Line 479 | public class ConcurrentSkipListSubSetTes
479       * A deserialized serialized set has same elements
480       */
481      public void testSerialization() throws Exception {
482 <        NavigableSet q = populatedSet(SIZE);
483 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
484 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
485 <        out.writeObject(q);
486 <        out.close();
487 <
488 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
489 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
490 <        NavigableSet r = (NavigableSet)in.readObject();
491 <        assertEquals(q.size(), r.size());
492 <        while (!q.isEmpty())
493 <            assertEquals(q.pollFirst(), r.pollFirst());
482 >        NavigableSet x = populatedSet(SIZE);
483 >        NavigableSet y = serialClone(x);
484 >
485 >        assertNotSame(y, x);
486 >        assertEquals(x.size(), y.size());
487 >        assertEquals(x, y);
488 >        assertEquals(y, x);
489 >        while (!x.isEmpty()) {
490 >            assertFalse(y.isEmpty());
491 >            assertEquals(x.pollFirst(), y.pollFirst());
492 >        }
493 >        assertTrue(y.isEmpty());
494      }
495  
496      /**
# Line 619 | Line 618 | public class ConcurrentSkipListSubSetTes
618      public void testDescendingSize() {
619          NavigableSet q = populatedSet(SIZE);
620          for (int i = 0; i < SIZE; ++i) {
621 <            assertEquals(SIZE-i, q.size());
621 >            assertEquals(SIZE - i, q.size());
622              q.pollFirst();
623          }
624          for (int i = 0; i < SIZE; ++i) {
# Line 632 | Line 631 | public class ConcurrentSkipListSubSetTes
631       * add(null) throws NPE
632       */
633      public void testDescendingAddNull() {
634 +        NavigableSet q = dset0();
635          try {
636            NavigableSet q = dset0();
636              q.add(null);
637              shouldThrow();
638          } catch (NullPointerException success) {}
# Line 660 | Line 659 | public class ConcurrentSkipListSubSetTes
659       * Add of non-Comparable throws CCE
660       */
661      public void testDescendingAddNonComparable() {
662 +        NavigableSet q = dset0();
663          try {
664            NavigableSet q = dset0();
665            q.add(new Object());
664              q.add(new Object());
665              q.add(new Object());
666              shouldThrow();
667          } catch (ClassCastException success) {}
668      }
669  
672
670      /**
671       * addAll(null) throws NPE
672       */
673      public void testDescendingAddAll1() {
674 +        NavigableSet q = dset0();
675          try {
678            NavigableSet q = dset0();
676              q.addAll(null);
677              shouldThrow();
678          } catch (NullPointerException success) {}
679      }
680 +
681      /**
682       * addAll of a collection with null elements throws NPE
683       */
684      public void testDescendingAddAll2() {
685 +        NavigableSet q = dset0();
686 +        Integer[] ints = new Integer[SIZE];
687          try {
688            NavigableSet q = dset0();
689            Integer[] ints = new Integer[SIZE];
688              q.addAll(Arrays.asList(ints));
689              shouldThrow();
690          } catch (NullPointerException success) {}
691      }
692 +
693      /**
694       * addAll of a collection with any null elements throws NPE after
695       * possibly adding some elements
696       */
697      public void testDescendingAddAll3() {
698 +        NavigableSet q = dset0();
699 +        Integer[] ints = new Integer[SIZE];
700 +        for (int i = 0; i < SIZE - 1; ++i)
701 +            ints[i] = new Integer(i + SIZE);
702          try {
700            NavigableSet q = dset0();
701            Integer[] ints = new Integer[SIZE];
702            for (int i = 0; i < SIZE-1; ++i)
703                ints[i] = new Integer(i+SIZE);
703              q.addAll(Arrays.asList(ints));
704              shouldThrow();
705          } catch (NullPointerException success) {}
# Line 713 | Line 712 | public class ConcurrentSkipListSubSetTes
712          Integer[] empty = new Integer[0];
713          Integer[] ints = new Integer[SIZE];
714          for (int i = 0; i < SIZE; ++i)
715 <            ints[i] = new Integer(SIZE-1- i);
715 >            ints[i] = new Integer(SIZE - 1 - i);
716          NavigableSet q = dset0();
717          assertFalse(q.addAll(Arrays.asList(empty)));
718          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 737 | Line 736 | public class ConcurrentSkipListSubSetTes
736       */
737      public void testDescendingRemoveElement() {
738          NavigableSet q = populatedSet(SIZE);
739 <        for (int i = 1; i < SIZE; i+=2) {
739 >        for (int i = 1; i < SIZE; i += 2) {
740              assertTrue(q.remove(new Integer(i)));
741          }
742 <        for (int i = 0; i < SIZE; i+=2) {
742 >        for (int i = 0; i < SIZE; i += 2 ) {
743              assertTrue(q.remove(new Integer(i)));
744 <            assertFalse(q.remove(new Integer(i+1)));
744 >            assertFalse(q.remove(new Integer(i + 1)));
745          }
746          assertTrue(q.isEmpty());
747      }
# Line 801 | Line 800 | public class ConcurrentSkipListSubSetTes
800                  assertTrue(changed);
801  
802              assertTrue(q.containsAll(p));
803 <            assertEquals(SIZE-i, q.size());
803 >            assertEquals(SIZE - i, q.size());
804              p.pollFirst();
805          }
806      }
# Line 814 | Line 813 | public class ConcurrentSkipListSubSetTes
813              NavigableSet q = populatedSet(SIZE);
814              NavigableSet p = populatedSet(i);
815              assertTrue(q.removeAll(p));
816 <            assertEquals(SIZE-i, q.size());
816 >            assertEquals(SIZE - i, q.size());
817              for (int j = 0; j < i; ++j) {
818 <                Integer I = (Integer)(p.pollFirst());
819 <                assertFalse(q.contains(I));
818 >                Integer x = (Integer)(p.pollFirst());
819 >                assertFalse(q.contains(x));
820              }
821          }
822      }
823  
825
826
824      /**
825       * lower returns preceding element
826       */
# Line 913 | Line 910 | public class ConcurrentSkipListSubSetTes
910      public void testDescendingToArray2() {
911          NavigableSet q = populatedSet(SIZE);
912          Integer[] ints = new Integer[SIZE];
913 <        ints = (Integer[])q.toArray(ints);
913 >        assertSame(ints, q.toArray(ints));
914          Arrays.sort(ints);
915          for (int i = 0; i < ints.length; i++)
916              assertEquals(ints[i], q.pollFirst());
# Line 944 | Line 941 | public class ConcurrentSkipListSubSetTes
941              assertTrue(q.contains(it.next()));
942              ++i;
943          }
944 <        assertEquals(i, 0);
944 >        assertEquals(0, i);
945      }
946  
947      /**
948       * iterator.remove removes current element
949       */
950 <    public void testDescendingIteratorRemove () {
950 >    public void testDescendingIteratorRemove() {
951          final NavigableSet q = dset0();
952          q.add(new Integer(2));
953          q.add(new Integer(1));
# Line 966 | Line 963 | public class ConcurrentSkipListSubSetTes
963          assertFalse(it.hasNext());
964      }
965  
969
966      /**
967       * toString contains toStrings of elements
968       */
# Line 974 | Line 970 | public class ConcurrentSkipListSubSetTes
970          NavigableSet q = populatedSet(SIZE);
971          String s = q.toString();
972          for (int i = 0; i < SIZE; ++i) {
973 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
973 >            assertTrue(s.contains(String.valueOf(i)));
974          }
975      }
976  
# Line 982 | Line 978 | public class ConcurrentSkipListSubSetTes
978       * A deserialized serialized set has same elements
979       */
980      public void testDescendingSerialization() throws Exception {
981 <        NavigableSet q = populatedSet(SIZE);
982 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
983 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
984 <        out.writeObject(q);
985 <        out.close();
986 <
987 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
988 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
989 <        NavigableSet r = (NavigableSet)in.readObject();
990 <        assertEquals(q.size(), r.size());
991 <        while (!q.isEmpty())
992 <            assertEquals(q.pollFirst(), r.pollFirst());
981 >        NavigableSet x = dset5();
982 >        NavigableSet y = serialClone(x);
983 >
984 >        assertNotSame(y, x);
985 >        assertEquals(x.size(), y.size());
986 >        assertEquals(x, y);
987 >        assertEquals(y, x);
988 >        while (!x.isEmpty()) {
989 >            assertFalse(y.isEmpty());
990 >            assertEquals(x.pollFirst(), y.pollFirst());
991 >        }
992 >        assertTrue(y.isEmpty());
993      }
994  
995      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines