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.9 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.25 by jsr166, Wed Dec 31 16:44:01 2014 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.Comparator;
10 > import java.util.Iterator;
11 > import java.util.NavigableSet;
12 > import java.util.SortedSet;
13 > import java.util.concurrent.ConcurrentSkipListSet;
14  
15   public class ConcurrentSkipListSubSetTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());
17 >        junit.textui.TestRunner.run(suite());
18      }
19      public static Test suite() {
20          return new TestSuite(ConcurrentSkipListSubSetTest.class);
# Line 19 | Line 22 | public class ConcurrentSkipListSubSetTes
22  
23      static class MyReverseComparator implements Comparator {
24          public int compare(Object x, Object y) {
25 <            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;
25 >            return ((Comparable)y).compareTo(x);
26          }
27      }
28  
29      /**
30 <     * Create a set of given size containing consecutive
30 >     * Returns a new set of given size containing consecutive
31       * Integers 0 ... n.
32       */
33 <    private NavigableSet populatedSet(int n) {
34 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
33 >    private NavigableSet<Integer> populatedSet(int n) {
34 >        ConcurrentSkipListSet<Integer> q =
35 >            new ConcurrentSkipListSet<Integer>();
36          assertTrue(q.isEmpty());
37  
38          for (int i = n-1; i >= 0; i-=2)
# Line 48 | Line 48 | public class ConcurrentSkipListSubSetTes
48      }
49  
50      /**
51 <     * Create set of first 5 ints
51 >     * Returns a new set of first 5 ints.
52       */
53      private NavigableSet set5() {
54          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
# Line 66 | Line 66 | public class ConcurrentSkipListSubSetTes
66      }
67  
68      /**
69 <     * Create set of first 5 negative ints
69 >     * Returns a new set of first 5 negative ints.
70       */
71      private NavigableSet dset5() {
72          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
# Line 100 | Line 100 | public class ConcurrentSkipListSubSetTes
100          assertEquals(0, set0().size());
101      }
102  
103
103      /**
104       * isEmpty is true before add, false after
105       */
# Line 171 | Line 170 | public class ConcurrentSkipListSubSetTes
170          } catch (ClassCastException success) {}
171      }
172  
174
173      /**
174       * addAll(null) throws NPE
175       */
# Line 182 | Line 180 | public class ConcurrentSkipListSubSetTes
180              shouldThrow();
181          } catch (NullPointerException success) {}
182      }
183 +
184      /**
185       * addAll of a collection with null elements throws NPE
186       */
# Line 193 | Line 192 | public class ConcurrentSkipListSubSetTes
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
# Line 212 | Line 212 | public class ConcurrentSkipListSubSetTes
212       * Set contains all elements of successful addAll
213       */
214      public void testAddAll5() {
215 <        try {
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);
220 <            NavigableSet q = set0();
221 <            assertFalse(q.addAll(Arrays.asList(empty)));
222 <            assertTrue(q.addAll(Arrays.asList(ints)));
223 <            for (int i = 0; i < SIZE; ++i)
224 <                assertEquals(new Integer(i), q.pollFirst());
225 <        }
226 <        finally {}
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);
219 >        NavigableSet q = set0();
220 >        assertFalse(q.addAll(Arrays.asList(empty)));
221 >        assertTrue(q.addAll(Arrays.asList(ints)));
222 >        for (int i = 0; i < SIZE; ++i)
223 >            assertEquals(new Integer(i), q.pollFirst());
224      }
225  
226      /**
# Line 232 | Line 229 | public class ConcurrentSkipListSubSetTes
229      public void testPoll() {
230          NavigableSet q = populatedSet(SIZE);
231          for (int i = 0; i < SIZE; ++i) {
232 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
232 >            assertEquals(i, q.pollFirst());
233          }
234          assertNull(q.pollFirst());
235      }
# Line 243 | Line 240 | public class ConcurrentSkipListSubSetTes
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)));
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.remove(new Integer(i)));
250 <            assertFalse(q.remove(new Integer(i+1)));
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 327 | Line 330 | public class ConcurrentSkipListSubSetTes
330          }
331      }
332  
330
331
333      /**
334       * lower returns preceding element
335       */
# Line 345 | Line 346 | public class ConcurrentSkipListSubSetTes
346  
347          Object e4 = q.lower(zero);
348          assertNull(e4);
348
349      }
350  
351      /**
# Line 364 | Line 364 | public class ConcurrentSkipListSubSetTes
364  
365          Object e4 = q.higher(six);
366          assertNull(e4);
367
367      }
368  
369      /**
# Line 383 | Line 382 | public class ConcurrentSkipListSubSetTes
382  
383          Object e4 = q.floor(zero);
384          assertNull(e4);
386
385      }
386  
387      /**
# Line 402 | Line 400 | public class ConcurrentSkipListSubSetTes
400  
401          Object e4 = q.ceiling(six);
402          assertNull(e4);
405
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();
414        Arrays.sort(o);
411          for (int i = 0; i < o.length; i++)
412 <            assertEquals(o[i], q.pollFirst());
412 >            assertSame(o[i], q.pollFirst());
413      }
414  
415      /**
416 <     * toArray(a) contains all elements
416 >     * toArray(a) contains all elements in sorted order
417       */
418      public void testToArray2() {
419 <        NavigableSet q = populatedSet(SIZE);
419 >        NavigableSet<Integer> q = populatedSet(SIZE);
420          Integer[] ints = new Integer[SIZE];
421 <        ints = (Integer[])q.toArray(ints);
422 <        Arrays.sort(ints);
421 >        Integer[] array = q.toArray(ints);
422 >        assertSame(ints, array);
423          for (int i = 0; i < ints.length; i++)
424 <            assertEquals(ints[i], q.pollFirst());
424 >            assertSame(ints[i], q.pollFirst());
425      }
426  
427      /**
# Line 453 | Line 449 | public class ConcurrentSkipListSubSetTes
449              assertTrue(q.contains(it.next()));
450              ++i;
451          }
452 <        assertEquals(i, 0);
452 >        assertEquals(0, i);
453      }
454  
455      /**
456       * iterator.remove removes current element
457       */
458 <    public void testIteratorRemove () {
458 >    public void testIteratorRemove() {
459          final NavigableSet q = set0();
460          q.add(new Integer(2));
461          q.add(new Integer(1));
# Line 475 | Line 471 | public class ConcurrentSkipListSubSetTes
471          assertFalse(it.hasNext());
472      }
473  
478
474      /**
475       * toString contains toStrings of elements
476       */
# Line 483 | Line 478 | public class ConcurrentSkipListSubSetTes
478          NavigableSet q = populatedSet(SIZE);
479          String s = q.toString();
480          for (int i = 0; i < SIZE; ++i) {
481 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
481 >            assertTrue(s.contains(String.valueOf(i)));
482          }
483      }
484  
# Line 491 | Line 486 | public class ConcurrentSkipListSubSetTes
486       * A deserialized serialized set has same elements
487       */
488      public void testSerialization() throws Exception {
489 <        NavigableSet q = populatedSet(SIZE);
490 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
491 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
492 <        out.writeObject(q);
493 <        out.close();
494 <
495 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
496 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
497 <        NavigableSet r = (NavigableSet)in.readObject();
498 <        assertEquals(q.size(), r.size());
499 <        while (!q.isEmpty())
500 <            assertEquals(q.pollFirst(), r.pollFirst());
489 >        NavigableSet x = populatedSet(SIZE);
490 >        NavigableSet y = serialClone(x);
491 >
492 >        assertNotSame(y, x);
493 >        assertEquals(x.size(), y.size());
494 >        assertEquals(x, y);
495 >        assertEquals(y, x);
496 >        while (!x.isEmpty()) {
497 >            assertFalse(y.isEmpty());
498 >            assertEquals(x.pollFirst(), y.pollFirst());
499 >        }
500 >        assertTrue(y.isEmpty());
501      }
502  
503      /**
# Line 680 | Line 675 | public class ConcurrentSkipListSubSetTes
675          } catch (ClassCastException success) {}
676      }
677  
683
678      /**
679       * addAll(null) throws NPE
680       */
# Line 691 | Line 685 | public class ConcurrentSkipListSubSetTes
685              shouldThrow();
686          } catch (NullPointerException success) {}
687      }
688 +
689      /**
690       * addAll of a collection with null elements throws NPE
691       */
# Line 702 | Line 697 | public class ConcurrentSkipListSubSetTes
697              shouldThrow();
698          } catch (NullPointerException success) {}
699      }
700 +
701      /**
702       * addAll of a collection with any null elements throws NPE after
703       * possibly adding some elements
# Line 721 | Line 717 | public class ConcurrentSkipListSubSetTes
717       * Set contains all elements of successful addAll
718       */
719      public void testDescendingAddAll5() {
720 <        try {
721 <            Integer[] empty = new Integer[0];
722 <            Integer[] ints = new Integer[SIZE];
723 <            for (int i = 0; i < SIZE; ++i)
724 <                ints[i] = new Integer(SIZE-1- i);
725 <            NavigableSet q = dset0();
726 <            assertFalse(q.addAll(Arrays.asList(empty)));
727 <            assertTrue(q.addAll(Arrays.asList(ints)));
728 <            for (int i = 0; i < SIZE; ++i)
733 <                assertEquals(new Integer(i), q.pollFirst());
734 <        }
735 <        finally {}
720 >        Integer[] empty = new Integer[0];
721 >        Integer[] ints = new Integer[SIZE];
722 >        for (int i = 0; i < SIZE; ++i)
723 >            ints[i] = new Integer(SIZE-1- i);
724 >        NavigableSet q = dset0();
725 >        assertFalse(q.addAll(Arrays.asList(empty)));
726 >        assertTrue(q.addAll(Arrays.asList(ints)));
727 >        for (int i = 0; i < SIZE; ++i)
728 >            assertEquals(new Integer(i), q.pollFirst());
729      }
730  
731      /**
# Line 741 | Line 734 | public class ConcurrentSkipListSubSetTes
734      public void testDescendingPoll() {
735          NavigableSet q = populatedSet(SIZE);
736          for (int i = 0; i < SIZE; ++i) {
737 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
737 >            assertEquals(i, q.pollFirst());
738          }
739          assertNull(q.pollFirst());
740      }
# Line 836 | Line 829 | public class ConcurrentSkipListSubSetTes
829          }
830      }
831  
839
840
832      /**
833       * lower returns preceding element
834       */
# Line 854 | Line 845 | public class ConcurrentSkipListSubSetTes
845  
846          Object e4 = q.lower(zero);
847          assertNull(e4);
857
848      }
849  
850      /**
# Line 873 | Line 863 | public class ConcurrentSkipListSubSetTes
863  
864          Object e4 = q.higher(m6);
865          assertNull(e4);
876
866      }
867  
868      /**
# Line 892 | Line 881 | public class ConcurrentSkipListSubSetTes
881  
882          Object e4 = q.floor(zero);
883          assertNull(e4);
895
884      }
885  
886      /**
# Line 911 | Line 899 | public class ConcurrentSkipListSubSetTes
899  
900          Object e4 = q.ceiling(m6);
901          assertNull(e4);
914
902      }
903  
904      /**
# Line 931 | Line 918 | public class ConcurrentSkipListSubSetTes
918      public void testDescendingToArray2() {
919          NavigableSet q = populatedSet(SIZE);
920          Integer[] ints = new Integer[SIZE];
921 <        ints = (Integer[])q.toArray(ints);
921 >        assertSame(ints, q.toArray(ints));
922          Arrays.sort(ints);
923          for (int i = 0; i < ints.length; i++)
924              assertEquals(ints[i], q.pollFirst());
# Line 962 | Line 949 | public class ConcurrentSkipListSubSetTes
949              assertTrue(q.contains(it.next()));
950              ++i;
951          }
952 <        assertEquals(i, 0);
952 >        assertEquals(0, i);
953      }
954  
955      /**
956       * iterator.remove removes current element
957       */
958 <    public void testDescendingIteratorRemove () {
958 >    public void testDescendingIteratorRemove() {
959          final NavigableSet q = dset0();
960          q.add(new Integer(2));
961          q.add(new Integer(1));
# Line 984 | Line 971 | public class ConcurrentSkipListSubSetTes
971          assertFalse(it.hasNext());
972      }
973  
987
974      /**
975       * toString contains toStrings of elements
976       */
# Line 992 | Line 978 | public class ConcurrentSkipListSubSetTes
978          NavigableSet q = populatedSet(SIZE);
979          String s = q.toString();
980          for (int i = 0; i < SIZE; ++i) {
981 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
981 >            assertTrue(s.contains(String.valueOf(i)));
982          }
983      }
984  
# Line 1000 | Line 986 | public class ConcurrentSkipListSubSetTes
986       * A deserialized serialized set has same elements
987       */
988      public void testDescendingSerialization() throws Exception {
989 <        NavigableSet q = populatedSet(SIZE);
990 <        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
991 <        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
992 <        out.writeObject(q);
993 <        out.close();
994 <
995 <        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
996 <        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
997 <        NavigableSet r = (NavigableSet)in.readObject();
998 <        assertEquals(q.size(), r.size());
999 <        while (!q.isEmpty())
1000 <            assertEquals(q.pollFirst(), r.pollFirst());
989 >        NavigableSet x = dset5();
990 >        NavigableSet y = serialClone(x);
991 >
992 >        assertNotSame(y, x);
993 >        assertEquals(x.size(), y.size());
994 >        assertEquals(x, y);
995 >        assertEquals(y, x);
996 >        while (!x.isEmpty()) {
997 >            assertFalse(y.isEmpty());
998 >            assertEquals(x.pollFirst(), y.pollFirst());
999 >        }
1000 >        assertTrue(y.isEmpty());
1001      }
1002  
1003      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines