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.11 by jsr166, Sun Nov 22 18:57:17 2009 UTC vs.
Revision 1.39 by jsr166, Mon May 28 21:19:50 2018 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 = new ConcurrentSkipListSet<>();
37          assertTrue(q.isEmpty());
38  
39 <        for (int i = n-1; i >= 0; i-=2)
39 >        for (int i = n - 1; i >= 0; i -= 2)
40              assertTrue(q.add(new Integer(i)));
41 <        for (int i = (n & 1); i < n; i+=2)
41 >        for (int i = (n & 1); i < n; i += 2)
42              assertTrue(q.add(new Integer(i)));
43          assertTrue(q.add(new Integer(-n)));
44          assertTrue(q.add(new Integer(n)));
# Line 44 | Line 49 | public class ConcurrentSkipListSubSetTes
49      }
50  
51      /**
52 <     * Create set of first 5 ints
52 >     * Returns a new set of first 5 ints.
53       */
54 <    private NavigableSet set5() {
54 >    private static NavigableSet set5() {
55          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
56          assertTrue(q.isEmpty());
57          q.add(one);
# Line 62 | Line 67 | public class ConcurrentSkipListSubSetTes
67      }
68  
69      /**
70 <     * Create set of first 5 negative ints
70 >     * Returns a new set of first 5 negative ints.
71       */
72 <    private NavigableSet dset5() {
72 >    private static NavigableSet dset5() {
73          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
74          assertTrue(q.isEmpty());
75          q.add(m1);
# Line 96 | Line 101 | public class ConcurrentSkipListSubSetTes
101          assertEquals(0, set0().size());
102      }
103  
99
104      /**
105       * isEmpty is true before add, false after
106       */
# Line 117 | Line 121 | public class ConcurrentSkipListSubSetTes
121      public void testSize() {
122          NavigableSet q = populatedSet(SIZE);
123          for (int i = 0; i < SIZE; ++i) {
124 <            assertEquals(SIZE-i, q.size());
124 >            assertEquals(SIZE - i, q.size());
125              q.pollFirst();
126          }
127          for (int i = 0; i < SIZE; ++i) {
# Line 130 | Line 134 | public class ConcurrentSkipListSubSetTes
134       * add(null) throws NPE
135       */
136      public void testAddNull() {
137 +        NavigableSet q = set0();
138          try {
134            NavigableSet q = set0();
139              q.add(null);
140              shouldThrow();
141          } catch (NullPointerException success) {}
# Line 158 | Line 162 | public class ConcurrentSkipListSubSetTes
162       * Add of non-Comparable throws CCE
163       */
164      public void testAddNonComparable() {
165 +        NavigableSet q = set0();
166          try {
162            NavigableSet q = set0();
163            q.add(new Object());
167              q.add(new Object());
168              q.add(new Object());
169              shouldThrow();
170          } catch (ClassCastException success) {}
171      }
172  
170
173      /**
174       * addAll(null) throws NPE
175       */
176      public void testAddAll1() {
177 +        NavigableSet q = set0();
178          try {
176            NavigableSet q = set0();
179              q.addAll(null);
180              shouldThrow();
181          } catch (NullPointerException success) {}
182      }
183 +
184      /**
185       * addAll of a collection with null elements throws NPE
186       */
187      public void testAddAll2() {
188 +        NavigableSet q = set0();
189 +        Integer[] ints = new Integer[SIZE];
190          try {
186            NavigableSet q = set0();
187            Integer[] ints = new Integer[SIZE];
191              q.addAll(Arrays.asList(ints));
192              shouldThrow();
193          } catch (NullPointerException success) {}
194      }
195 +
196      /**
197       * addAll of a collection with any null elements throws NPE after
198       * possibly adding some elements
199       */
200      public void testAddAll3() {
201 +        NavigableSet q = set0();
202 +        Integer[] ints = new Integer[SIZE];
203 +        for (int i = 0; i < SIZE - 1; ++i)
204 +            ints[i] = new Integer(i + SIZE);
205          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);
206              q.addAll(Arrays.asList(ints));
207              shouldThrow();
208          } catch (NullPointerException success) {}
# Line 211 | Line 215 | public class ConcurrentSkipListSubSetTes
215          Integer[] empty = new Integer[0];
216          Integer[] ints = new Integer[SIZE];
217          for (int i = 0; i < SIZE; ++i)
218 <            ints[i] = new Integer(SIZE-1- i);
218 >            ints[i] = new Integer(SIZE - 1 - i);
219          NavigableSet q = set0();
220          assertFalse(q.addAll(Arrays.asList(empty)));
221          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 235 | Line 239 | public class ConcurrentSkipListSubSetTes
239       */
240      public void testRemoveElement() {
241          NavigableSet q = populatedSet(SIZE);
242 <        for (int i = 1; i < SIZE; i+=2) {
243 <            assertTrue(q.remove(new Integer(i)));
244 <        }
245 <        for (int i = 0; i < SIZE; i+=2) {
246 <            assertTrue(q.remove(new Integer(i)));
247 <            assertFalse(q.remove(new Integer(i+1)));
242 >        for (int i = 1; i < SIZE; i += 2) {
243 >            assertTrue(q.contains(i));
244 >            assertTrue(q.remove(i));
245 >            assertFalse(q.contains(i));
246 >            assertTrue(q.contains(i - 1));
247 >        }
248 >        for (int i = 0; i < SIZE; i += 2) {
249 >            assertTrue(q.contains(i));
250 >            assertTrue(q.remove(i));
251 >            assertFalse(q.contains(i));
252 >            assertFalse(q.remove(i + 1));
253 >            assertFalse(q.contains(i + 1));
254          }
255          assertTrue(q.isEmpty());
256      }
# Line 299 | Line 309 | public class ConcurrentSkipListSubSetTes
309                  assertTrue(changed);
310  
311              assertTrue(q.containsAll(p));
312 <            assertEquals(SIZE-i, q.size());
312 >            assertEquals(SIZE - i, q.size());
313              p.pollFirst();
314          }
315      }
# Line 312 | Line 322 | public class ConcurrentSkipListSubSetTes
322              NavigableSet q = populatedSet(SIZE);
323              NavigableSet p = populatedSet(i);
324              assertTrue(q.removeAll(p));
325 <            assertEquals(SIZE-i, q.size());
325 >            assertEquals(SIZE - i, q.size());
326              for (int j = 0; j < i; ++j) {
327 <                Integer I = (Integer)(p.pollFirst());
328 <                assertFalse(q.contains(I));
327 >                Integer x = (Integer)(p.pollFirst());
328 >                assertFalse(q.contains(x));
329              }
330          }
331      }
332  
323
324
333      /**
334       * lower returns preceding element
335       */
# Line 338 | Line 346 | public class ConcurrentSkipListSubSetTes
346  
347          Object e4 = q.lower(zero);
348          assertNull(e4);
341
349      }
350  
351      /**
# Line 357 | Line 364 | public class ConcurrentSkipListSubSetTes
364  
365          Object e4 = q.higher(six);
366          assertNull(e4);
360
367      }
368  
369      /**
# Line 376 | Line 382 | public class ConcurrentSkipListSubSetTes
382  
383          Object e4 = q.floor(zero);
384          assertNull(e4);
379
385      }
386  
387      /**
# Line 395 | Line 400 | public class ConcurrentSkipListSubSetTes
400  
401          Object e4 = q.ceiling(six);
402          assertNull(e4);
398
403      }
404  
405      /**
406 <     * toArray contains all elements
406 >     * toArray contains all elements in sorted order
407       */
408      public void testToArray() {
409          NavigableSet q = populatedSet(SIZE);
410 <        Object[] o = q.toArray();
411 <        Arrays.sort(o);
412 <        for (int i = 0; i < o.length; i++)
413 <            assertEquals(o[i], q.pollFirst());
410 >        Object[] a = q.toArray();
411 >        assertSame(Object[].class, a.getClass());
412 >        for (Object o : a)
413 >            assertSame(o, q.pollFirst());
414 >        assertTrue(q.isEmpty());
415      }
416  
417      /**
418 <     * toArray(a) contains all elements
418 >     * toArray(a) contains all elements in sorted order
419       */
420      public void testToArray2() {
421 <        NavigableSet q = populatedSet(SIZE);
421 >        NavigableSet<Integer> q = populatedSet(SIZE);
422          Integer[] ints = new Integer[SIZE];
423 <        ints = (Integer[])q.toArray(ints);
424 <        Arrays.sort(ints);
425 <        for (int i = 0; i < ints.length; i++)
426 <            assertEquals(ints[i], q.pollFirst());
423 >        Integer[] array = q.toArray(ints);
424 >        assertSame(ints, array);
425 >        for (Integer o : ints)
426 >            assertSame(o, q.pollFirst());
427 >        assertTrue(q.isEmpty());
428      }
429  
430      /**
# Line 426 | Line 432 | public class ConcurrentSkipListSubSetTes
432       */
433      public void testIterator() {
434          NavigableSet q = populatedSet(SIZE);
429        int i = 0;
435          Iterator it = q.iterator();
436 <        while (it.hasNext()) {
436 >        int i;
437 >        for (i = 0; it.hasNext(); i++)
438              assertTrue(q.contains(it.next()));
433            ++i;
434        }
439          assertEquals(i, SIZE);
440 +        assertIteratorExhausted(it);
441      }
442  
443      /**
444       * iterator of empty set has no elements
445       */
446      public void testEmptyIterator() {
447 <        NavigableSet q = set0();
443 <        int i = 0;
444 <        Iterator it = q.iterator();
445 <        while (it.hasNext()) {
446 <            assertTrue(q.contains(it.next()));
447 <            ++i;
448 <        }
449 <        assertEquals(i, 0);
447 >        assertIteratorExhausted(set0().iterator());
448      }
449  
450      /**
451       * iterator.remove removes current element
452       */
453 <    public void testIteratorRemove () {
453 >    public void testIteratorRemove() {
454          final NavigableSet q = set0();
455          q.add(new Integer(2));
456          q.add(new Integer(1));
# Line 468 | Line 466 | public class ConcurrentSkipListSubSetTes
466          assertFalse(it.hasNext());
467      }
468  
471
469      /**
470       * toString contains toStrings of elements
471       */
# Line 476 | Line 473 | public class ConcurrentSkipListSubSetTes
473          NavigableSet q = populatedSet(SIZE);
474          String s = q.toString();
475          for (int i = 0; i < SIZE; ++i) {
476 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
476 >            assertTrue(s.contains(String.valueOf(i)));
477          }
478      }
479  
480      /**
481 <     * A deserialized serialized set has same elements
481 >     * A deserialized/reserialized set equals original
482       */
483      public void testSerialization() throws Exception {
484 <        NavigableSet q = populatedSet(SIZE);
485 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
486 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
487 <        out.writeObject(q);
488 <        out.close();
489 <
490 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
491 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
492 <        NavigableSet r = (NavigableSet)in.readObject();
493 <        assertEquals(q.size(), r.size());
494 <        while (!q.isEmpty())
495 <            assertEquals(q.pollFirst(), r.pollFirst());
484 >        NavigableSet x = populatedSet(SIZE);
485 >        NavigableSet y = serialClone(x);
486 >
487 >        assertNotSame(y, x);
488 >        assertEquals(x.size(), y.size());
489 >        assertEquals(x, y);
490 >        assertEquals(y, x);
491 >        while (!x.isEmpty()) {
492 >            assertFalse(y.isEmpty());
493 >            assertEquals(x.pollFirst(), y.pollFirst());
494 >        }
495 >        assertTrue(y.isEmpty());
496      }
497  
498      /**
# Line 623 | Line 620 | public class ConcurrentSkipListSubSetTes
620      public void testDescendingSize() {
621          NavigableSet q = populatedSet(SIZE);
622          for (int i = 0; i < SIZE; ++i) {
623 <            assertEquals(SIZE-i, q.size());
623 >            assertEquals(SIZE - i, q.size());
624              q.pollFirst();
625          }
626          for (int i = 0; i < SIZE; ++i) {
# Line 636 | Line 633 | public class ConcurrentSkipListSubSetTes
633       * add(null) throws NPE
634       */
635      public void testDescendingAddNull() {
636 +        NavigableSet q = dset0();
637          try {
640            NavigableSet q = dset0();
638              q.add(null);
639              shouldThrow();
640          } catch (NullPointerException success) {}
# Line 664 | Line 661 | public class ConcurrentSkipListSubSetTes
661       * Add of non-Comparable throws CCE
662       */
663      public void testDescendingAddNonComparable() {
664 +        NavigableSet q = dset0();
665          try {
668            NavigableSet q = dset0();
669            q.add(new Object());
666              q.add(new Object());
667              q.add(new Object());
668              shouldThrow();
669          } catch (ClassCastException success) {}
670      }
671  
676
672      /**
673       * addAll(null) throws NPE
674       */
675      public void testDescendingAddAll1() {
676 +        NavigableSet q = dset0();
677          try {
682            NavigableSet q = dset0();
678              q.addAll(null);
679              shouldThrow();
680          } catch (NullPointerException success) {}
681      }
682 +
683      /**
684       * addAll of a collection with null elements throws NPE
685       */
686      public void testDescendingAddAll2() {
687 +        NavigableSet q = dset0();
688 +        Integer[] ints = new Integer[SIZE];
689          try {
692            NavigableSet q = dset0();
693            Integer[] ints = new Integer[SIZE];
690              q.addAll(Arrays.asList(ints));
691              shouldThrow();
692          } catch (NullPointerException success) {}
693      }
694 +
695      /**
696       * addAll of a collection with any null elements throws NPE after
697       * possibly adding some elements
698       */
699      public void testDescendingAddAll3() {
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);
704          try {
704            NavigableSet q = dset0();
705            Integer[] ints = new Integer[SIZE];
706            for (int i = 0; i < SIZE-1; ++i)
707                ints[i] = new Integer(i+SIZE);
705              q.addAll(Arrays.asList(ints));
706              shouldThrow();
707          } catch (NullPointerException success) {}
# Line 717 | Line 714 | public class ConcurrentSkipListSubSetTes
714          Integer[] empty = new Integer[0];
715          Integer[] ints = new Integer[SIZE];
716          for (int i = 0; i < SIZE; ++i)
717 <            ints[i] = new Integer(SIZE-1- i);
717 >            ints[i] = new Integer(SIZE - 1 - i);
718          NavigableSet q = dset0();
719          assertFalse(q.addAll(Arrays.asList(empty)));
720          assertTrue(q.addAll(Arrays.asList(ints)));
# Line 741 | Line 738 | public class ConcurrentSkipListSubSetTes
738       */
739      public void testDescendingRemoveElement() {
740          NavigableSet q = populatedSet(SIZE);
741 <        for (int i = 1; i < SIZE; i+=2) {
741 >        for (int i = 1; i < SIZE; i += 2) {
742              assertTrue(q.remove(new Integer(i)));
743          }
744 <        for (int i = 0; i < SIZE; i+=2) {
744 >        for (int i = 0; i < SIZE; i += 2 ) {
745              assertTrue(q.remove(new Integer(i)));
746 <            assertFalse(q.remove(new Integer(i+1)));
746 >            assertFalse(q.remove(new Integer(i + 1)));
747          }
748          assertTrue(q.isEmpty());
749      }
# Line 805 | Line 802 | public class ConcurrentSkipListSubSetTes
802                  assertTrue(changed);
803  
804              assertTrue(q.containsAll(p));
805 <            assertEquals(SIZE-i, q.size());
805 >            assertEquals(SIZE - i, q.size());
806              p.pollFirst();
807          }
808      }
# Line 818 | Line 815 | public class ConcurrentSkipListSubSetTes
815              NavigableSet q = populatedSet(SIZE);
816              NavigableSet p = populatedSet(i);
817              assertTrue(q.removeAll(p));
818 <            assertEquals(SIZE-i, q.size());
818 >            assertEquals(SIZE - i, q.size());
819              for (int j = 0; j < i; ++j) {
820 <                Integer I = (Integer)(p.pollFirst());
821 <                assertFalse(q.contains(I));
820 >                Integer x = (Integer)(p.pollFirst());
821 >                assertFalse(q.contains(x));
822              }
823          }
824      }
825  
829
830
826      /**
827       * lower returns preceding element
828       */
# Line 844 | Line 839 | public class ConcurrentSkipListSubSetTes
839  
840          Object e4 = q.lower(zero);
841          assertNull(e4);
847
842      }
843  
844      /**
# Line 863 | Line 857 | public class ConcurrentSkipListSubSetTes
857  
858          Object e4 = q.higher(m6);
859          assertNull(e4);
866
860      }
861  
862      /**
# Line 882 | Line 875 | public class ConcurrentSkipListSubSetTes
875  
876          Object e4 = q.floor(zero);
877          assertNull(e4);
885
878      }
879  
880      /**
# Line 901 | Line 893 | public class ConcurrentSkipListSubSetTes
893  
894          Object e4 = q.ceiling(m6);
895          assertNull(e4);
904
896      }
897  
898      /**
# Line 921 | Line 912 | public class ConcurrentSkipListSubSetTes
912      public void testDescendingToArray2() {
913          NavigableSet q = populatedSet(SIZE);
914          Integer[] ints = new Integer[SIZE];
915 <        ints = (Integer[])q.toArray(ints);
915 >        assertSame(ints, q.toArray(ints));
916          Arrays.sort(ints);
917          for (int i = 0; i < ints.length; i++)
918              assertEquals(ints[i], q.pollFirst());
# Line 952 | Line 943 | public class ConcurrentSkipListSubSetTes
943              assertTrue(q.contains(it.next()));
944              ++i;
945          }
946 <        assertEquals(i, 0);
946 >        assertEquals(0, i);
947      }
948  
949      /**
950       * iterator.remove removes current element
951       */
952 <    public void testDescendingIteratorRemove () {
952 >    public void testDescendingIteratorRemove() {
953          final NavigableSet q = dset0();
954          q.add(new Integer(2));
955          q.add(new Integer(1));
# Line 974 | Line 965 | public class ConcurrentSkipListSubSetTes
965          assertFalse(it.hasNext());
966      }
967  
977
968      /**
969       * toString contains toStrings of elements
970       */
# Line 982 | Line 972 | public class ConcurrentSkipListSubSetTes
972          NavigableSet q = populatedSet(SIZE);
973          String s = q.toString();
974          for (int i = 0; i < SIZE; ++i) {
975 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
975 >            assertTrue(s.contains(String.valueOf(i)));
976          }
977      }
978  
979      /**
980 <     * A deserialized serialized set has same elements
980 >     * A deserialized/reserialized set equals original
981       */
982      public void testDescendingSerialization() throws Exception {
983 <        NavigableSet q = populatedSet(SIZE);
984 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
985 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
986 <        out.writeObject(q);
987 <        out.close();
988 <
989 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
990 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
991 <        NavigableSet r = (NavigableSet)in.readObject();
992 <        assertEquals(q.size(), r.size());
993 <        while (!q.isEmpty())
994 <            assertEquals(q.pollFirst(), r.pollFirst());
983 >        NavigableSet x = dset5();
984 >        NavigableSet y = serialClone(x);
985 >
986 >        assertNotSame(y, x);
987 >        assertEquals(x.size(), y.size());
988 >        assertEquals(x, y);
989 >        assertEquals(y, x);
990 >        while (!x.isEmpty()) {
991 >            assertFalse(y.isEmpty());
992 >            assertEquals(x.pollFirst(), y.pollFirst());
993 >        }
994 >        assertTrue(y.isEmpty());
995      }
996  
997      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines