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.7 by jsr166, Mon Nov 16 05:30:07 2009 UTC vs.
Revision 1.19 by jsr166, Fri May 27 19:21:27 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.*;
# Line 11 | Line 11 | import java.io.*;
11  
12   public class ConcurrentSkipListSubSetTest extends JSR166TestCase {
13      public static void main(String[] args) {
14 <        junit.textui.TestRunner.run (suite());
14 >        junit.textui.TestRunner.run(suite());
15      }
16      public static Test suite() {
17 <        return new TestSuite(ConcurrentSkipListSubSetTest.class);
17 >        return new TestSuite(ConcurrentSkipListSubSetTest.class);
18      }
19  
20      static class MyReverseComparator implements Comparator {
21          public int compare(Object x, Object y) {
22 <            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;
22 >            return ((Comparable)y).compareTo(x);
23          }
24      }
25  
# Line 31 | Line 27 | public class ConcurrentSkipListSubSetTes
27       * Create a set of given size containing consecutive
28       * Integers 0 ... n.
29       */
30 <    private NavigableSet populatedSet(int n) {
31 <        ConcurrentSkipListSet q = new ConcurrentSkipListSet();
30 >    private NavigableSet<Integer> populatedSet(int n) {
31 >        ConcurrentSkipListSet<Integer> q =
32 >            new ConcurrentSkipListSet<Integer>();
33          assertTrue(q.isEmpty());
34  
35 <        for (int i = n-1; i >= 0; i-=2)
36 <            assertTrue(q.add(new Integer(i)));
37 <        for (int i = (n & 1); i < n; i+=2)
38 <            assertTrue(q.add(new Integer(i)));
35 >        for (int i = n-1; i >= 0; i-=2)
36 >            assertTrue(q.add(new Integer(i)));
37 >        for (int i = (n & 1); i < n; i+=2)
38 >            assertTrue(q.add(new Integer(i)));
39          assertTrue(q.add(new Integer(-n)));
40          assertTrue(q.add(new Integer(n)));
41          NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false);
42          assertFalse(s.isEmpty());
43 <        assertEquals(n, s.size());
43 >        assertEquals(n, s.size());
44          return s;
45      }
46  
# Line 61 | Line 58 | public class ConcurrentSkipListSubSetTes
58          q.add(zero);
59          q.add(seven);
60          NavigableSet s = q.subSet(one, true, seven, false);
61 <        assertEquals(5, s.size());
61 >        assertEquals(5, s.size());
62          return s;
63      }
64  
# Line 77 | Line 74 | public class ConcurrentSkipListSubSetTes
74          q.add(m4);
75          q.add(m5);
76          NavigableSet s = q.descendingSet();
77 <        assertEquals(5, s.size());
77 >        assertEquals(5, s.size());
78          return s;
79      }
80  
81      private static NavigableSet set0() {
82 <        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
82 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
83          assertTrue(set.isEmpty());
84          return set.tailSet(m1, true);
85      }
86  
87      private static NavigableSet dset0() {
88 <        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
88 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
89          assertTrue(set.isEmpty());
90          return set;
91      }
# Line 100 | Line 97 | public class ConcurrentSkipListSubSetTes
97          assertEquals(0, set0().size());
98      }
99  
103
100      /**
101       * isEmpty is true before add, false after
102       */
# Line 134 | Line 130 | public class ConcurrentSkipListSubSetTes
130       * add(null) throws NPE
131       */
132      public void testAddNull() {
133 <        try {
133 >        try {
134              NavigableSet q = set0();
135              q.add(null);
136              shouldThrow();
137 <        } catch (NullPointerException success) { }
137 >        } catch (NullPointerException success) {}
138      }
139  
140      /**
# Line 168 | Line 164 | public class ConcurrentSkipListSubSetTes
164              q.add(new Object());
165              q.add(new Object());
166              shouldThrow();
167 <        }
172 <        catch (ClassCastException success) {}
167 >        } catch (ClassCastException success) {}
168      }
169  
175
170      /**
171       * addAll(null) throws NPE
172       */
# Line 181 | Line 175 | public class ConcurrentSkipListSubSetTes
175              NavigableSet q = set0();
176              q.addAll(null);
177              shouldThrow();
178 <        }
185 <        catch (NullPointerException success) {}
178 >        } catch (NullPointerException success) {}
179      }
180 +
181      /**
182       * addAll of a collection with null elements throws NPE
183       */
# Line 193 | Line 187 | public class ConcurrentSkipListSubSetTes
187              Integer[] ints = new Integer[SIZE];
188              q.addAll(Arrays.asList(ints));
189              shouldThrow();
190 <        }
197 <        catch (NullPointerException success) {}
190 >        } catch (NullPointerException success) {}
191      }
192 +
193      /**
194       * addAll of a collection with any null elements throws NPE after
195       * possibly adding some elements
# Line 208 | Line 202 | public class ConcurrentSkipListSubSetTes
202                  ints[i] = new Integer(i+SIZE);
203              q.addAll(Arrays.asList(ints));
204              shouldThrow();
205 <        }
212 <        catch (NullPointerException success) {}
205 >        } catch (NullPointerException success) {}
206      }
207  
208      /**
209       * Set contains all elements of successful addAll
210       */
211      public void testAddAll5() {
212 <        try {
213 <            Integer[] empty = new Integer[0];
214 <            Integer[] ints = new Integer[SIZE];
215 <            for (int i = 0; i < SIZE; ++i)
216 <                ints[i] = new Integer(SIZE-1- i);
217 <            NavigableSet q = set0();
218 <            assertFalse(q.addAll(Arrays.asList(empty)));
219 <            assertTrue(q.addAll(Arrays.asList(ints)));
220 <            for (int i = 0; i < SIZE; ++i)
228 <                assertEquals(new Integer(i), q.pollFirst());
229 <        }
230 <        finally {}
212 >        Integer[] empty = new Integer[0];
213 >        Integer[] ints = new Integer[SIZE];
214 >        for (int i = 0; i < SIZE; ++i)
215 >            ints[i] = new Integer(SIZE-1- i);
216 >        NavigableSet q = set0();
217 >        assertFalse(q.addAll(Arrays.asList(empty)));
218 >        assertTrue(q.addAll(Arrays.asList(ints)));
219 >        for (int i = 0; i < SIZE; ++i)
220 >            assertEquals(new Integer(i), q.pollFirst());
221      }
222  
223      /**
# Line 236 | Line 226 | public class ConcurrentSkipListSubSetTes
226      public void testPoll() {
227          NavigableSet q = populatedSet(SIZE);
228          for (int i = 0; i < SIZE; ++i) {
229 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
229 >            assertEquals(i, q.pollFirst());
230          }
231 <        assertNull(q.pollFirst());
231 >        assertNull(q.pollFirst());
232      }
233  
234      /**
# Line 247 | Line 237 | public class ConcurrentSkipListSubSetTes
237      public void testRemoveElement() {
238          NavigableSet q = populatedSet(SIZE);
239          for (int i = 1; i < SIZE; i+=2) {
240 <            assertTrue(q.remove(new Integer(i)));
240 >            assertTrue(q.contains(i));
241 >            assertTrue(q.remove(i));
242 >            assertFalse(q.contains(i));
243 >            assertTrue(q.contains(i-1));
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)));
246 >            assertTrue(q.contains(i));
247 >            assertTrue(q.remove(i));
248 >            assertFalse(q.contains(i));
249 >            assertFalse(q.remove(i+1));
250 >            assertFalse(q.contains(i+1));
251          }
252          assertTrue(q.isEmpty());
253      }
# Line 331 | Line 327 | public class ConcurrentSkipListSubSetTes
327          }
328      }
329  
334
335
330      /**
331       * lower returns preceding element
332       */
# Line 349 | Line 343 | public class ConcurrentSkipListSubSetTes
343  
344          Object e4 = q.lower(zero);
345          assertNull(e4);
352
346      }
347  
348      /**
# Line 368 | Line 361 | public class ConcurrentSkipListSubSetTes
361  
362          Object e4 = q.higher(six);
363          assertNull(e4);
371
364      }
365  
366      /**
# Line 387 | Line 379 | public class ConcurrentSkipListSubSetTes
379  
380          Object e4 = q.floor(zero);
381          assertNull(e4);
390
382      }
383  
384      /**
# Line 406 | Line 397 | public class ConcurrentSkipListSubSetTes
397  
398          Object e4 = q.ceiling(six);
399          assertNull(e4);
409
400      }
401  
402      /**
403 <     * toArray contains all elements
403 >     * toArray contains all elements in sorted order
404       */
405      public void testToArray() {
406          NavigableSet q = populatedSet(SIZE);
407 <        Object[] o = q.toArray();
408 <        Arrays.sort(o);
409 <        for (int i = 0; i < o.length; i++)
420 <            assertEquals(o[i], q.pollFirst());
407 >        Object[] o = q.toArray();
408 >        for (int i = 0; i < o.length; i++)
409 >            assertSame(o[i], q.pollFirst());
410      }
411  
412      /**
413 <     * toArray(a) contains all elements
413 >     * toArray(a) contains all elements in sorted order
414       */
415      public void testToArray2() {
416 <        NavigableSet q = populatedSet(SIZE);
417 <        Integer[] ints = new Integer[SIZE];
418 <        ints = (Integer[])q.toArray(ints);
419 <        Arrays.sort(ints);
416 >        NavigableSet<Integer> q = populatedSet(SIZE);
417 >        Integer[] ints = new Integer[SIZE];
418 >        Integer[] array = q.toArray(ints);
419 >        assertSame(ints, array);
420          for (int i = 0; i < ints.length; i++)
421 <            assertEquals(ints[i], q.pollFirst());
421 >            assertSame(ints[i], q.pollFirst());
422      }
423  
424      /**
# Line 438 | Line 427 | public class ConcurrentSkipListSubSetTes
427      public void testIterator() {
428          NavigableSet q = populatedSet(SIZE);
429          int i = 0;
430 <        Iterator it = q.iterator();
430 >        Iterator it = q.iterator();
431          while (it.hasNext()) {
432              assertTrue(q.contains(it.next()));
433              ++i;
# Line 452 | Line 441 | public class ConcurrentSkipListSubSetTes
441      public void testEmptyIterator() {
442          NavigableSet q = set0();
443          int i = 0;
444 <        Iterator it = q.iterator();
444 >        Iterator it = q.iterator();
445          while (it.hasNext()) {
446              assertTrue(q.contains(it.next()));
447              ++i;
# Line 463 | Line 452 | public class ConcurrentSkipListSubSetTes
452      /**
453       * iterator.remove removes current element
454       */
455 <    public void testIteratorRemove () {
455 >    public void testIteratorRemove() {
456          final NavigableSet q = set0();
457          q.add(new Integer(2));
458          q.add(new Integer(1));
# Line 479 | Line 468 | public class ConcurrentSkipListSubSetTes
468          assertFalse(it.hasNext());
469      }
470  
482
471      /**
472       * toString contains toStrings of elements
473       */
# Line 487 | Line 475 | public class ConcurrentSkipListSubSetTes
475          NavigableSet q = populatedSet(SIZE);
476          String s = q.toString();
477          for (int i = 0; i < SIZE; ++i) {
478 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
478 >            assertTrue(s.contains(String.valueOf(i)));
479          }
480      }
481  
482      /**
483       * A deserialized serialized set has same elements
484       */
485 <    public void testSerialization() {
485 >    public void testSerialization() throws Exception {
486          NavigableSet q = populatedSet(SIZE);
487 <        try {
488 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
489 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
490 <            out.writeObject(q);
491 <            out.close();
492 <
493 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
494 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
495 <            NavigableSet r = (NavigableSet)in.readObject();
496 <            assertEquals(q.size(), r.size());
497 <            while (!q.isEmpty())
510 <                assertEquals(q.pollFirst(), r.pollFirst());
511 <        } catch (Exception e) {
512 <            e.printStackTrace();
513 <            unexpectedException();
514 <        }
487 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
488 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
489 >        out.writeObject(q);
490 >        out.close();
491 >
492 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
493 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
494 >        NavigableSet r = (NavigableSet)in.readObject();
495 >        assertEquals(q.size(), r.size());
496 >        while (!q.isEmpty())
497 >            assertEquals(q.pollFirst(), r.pollFirst());
498      }
499  
500      /**
# Line 652 | Line 635 | public class ConcurrentSkipListSubSetTes
635       * add(null) throws NPE
636       */
637      public void testDescendingAddNull() {
638 <        try {
638 >        try {
639              NavigableSet q = dset0();
640              q.add(null);
641              shouldThrow();
642 <        } catch (NullPointerException success) { }
642 >        } catch (NullPointerException success) {}
643      }
644  
645      /**
# Line 686 | Line 669 | public class ConcurrentSkipListSubSetTes
669              q.add(new Object());
670              q.add(new Object());
671              shouldThrow();
672 <        }
690 <        catch (ClassCastException success) {}
672 >        } catch (ClassCastException success) {}
673      }
674  
693
675      /**
676       * addAll(null) throws NPE
677       */
# Line 699 | Line 680 | public class ConcurrentSkipListSubSetTes
680              NavigableSet q = dset0();
681              q.addAll(null);
682              shouldThrow();
683 <        }
703 <        catch (NullPointerException success) {}
683 >        } catch (NullPointerException success) {}
684      }
685 +
686      /**
687       * addAll of a collection with null elements throws NPE
688       */
# Line 711 | Line 692 | public class ConcurrentSkipListSubSetTes
692              Integer[] ints = new Integer[SIZE];
693              q.addAll(Arrays.asList(ints));
694              shouldThrow();
695 <        }
715 <        catch (NullPointerException success) {}
695 >        } catch (NullPointerException success) {}
696      }
697 +
698      /**
699       * addAll of a collection with any null elements throws NPE after
700       * possibly adding some elements
# Line 726 | Line 707 | public class ConcurrentSkipListSubSetTes
707                  ints[i] = new Integer(i+SIZE);
708              q.addAll(Arrays.asList(ints));
709              shouldThrow();
710 <        }
730 <        catch (NullPointerException success) {}
710 >        } catch (NullPointerException success) {}
711      }
712  
713      /**
714       * Set contains all elements of successful addAll
715       */
716      public void testDescendingAddAll5() {
717 <        try {
718 <            Integer[] empty = new Integer[0];
719 <            Integer[] ints = new Integer[SIZE];
720 <            for (int i = 0; i < SIZE; ++i)
721 <                ints[i] = new Integer(SIZE-1- i);
722 <            NavigableSet q = dset0();
723 <            assertFalse(q.addAll(Arrays.asList(empty)));
724 <            assertTrue(q.addAll(Arrays.asList(ints)));
725 <            for (int i = 0; i < SIZE; ++i)
746 <                assertEquals(new Integer(i), q.pollFirst());
747 <        }
748 <        finally {}
717 >        Integer[] empty = new Integer[0];
718 >        Integer[] ints = new Integer[SIZE];
719 >        for (int i = 0; i < SIZE; ++i)
720 >            ints[i] = new Integer(SIZE-1- i);
721 >        NavigableSet q = dset0();
722 >        assertFalse(q.addAll(Arrays.asList(empty)));
723 >        assertTrue(q.addAll(Arrays.asList(ints)));
724 >        for (int i = 0; i < SIZE; ++i)
725 >            assertEquals(new Integer(i), q.pollFirst());
726      }
727  
728      /**
# Line 754 | Line 731 | public class ConcurrentSkipListSubSetTes
731      public void testDescendingPoll() {
732          NavigableSet q = populatedSet(SIZE);
733          for (int i = 0; i < SIZE; ++i) {
734 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
734 >            assertEquals(i, q.pollFirst());
735          }
736 <        assertNull(q.pollFirst());
736 >        assertNull(q.pollFirst());
737      }
738  
739      /**
# Line 849 | Line 826 | public class ConcurrentSkipListSubSetTes
826          }
827      }
828  
852
853
829      /**
830       * lower returns preceding element
831       */
# Line 867 | Line 842 | public class ConcurrentSkipListSubSetTes
842  
843          Object e4 = q.lower(zero);
844          assertNull(e4);
870
845      }
846  
847      /**
# Line 886 | Line 860 | public class ConcurrentSkipListSubSetTes
860  
861          Object e4 = q.higher(m6);
862          assertNull(e4);
889
863      }
864  
865      /**
# Line 905 | Line 878 | public class ConcurrentSkipListSubSetTes
878  
879          Object e4 = q.floor(zero);
880          assertNull(e4);
908
881      }
882  
883      /**
# Line 924 | Line 896 | public class ConcurrentSkipListSubSetTes
896  
897          Object e4 = q.ceiling(m6);
898          assertNull(e4);
927
899      }
900  
901      /**
# Line 932 | Line 903 | public class ConcurrentSkipListSubSetTes
903       */
904      public void testDescendingToArray() {
905          NavigableSet q = populatedSet(SIZE);
906 <        Object[] o = q.toArray();
906 >        Object[] o = q.toArray();
907          Arrays.sort(o);
908 <        for (int i = 0; i < o.length; i++)
909 <            assertEquals(o[i], q.pollFirst());
908 >        for (int i = 0; i < o.length; i++)
909 >            assertEquals(o[i], q.pollFirst());
910      }
911  
912      /**
# Line 943 | Line 914 | public class ConcurrentSkipListSubSetTes
914       */
915      public void testDescendingToArray2() {
916          NavigableSet q = populatedSet(SIZE);
917 <        Integer[] ints = new Integer[SIZE];
918 <        ints = (Integer[])q.toArray(ints);
917 >        Integer[] ints = new Integer[SIZE];
918 >        assertSame(ints, q.toArray(ints));
919          Arrays.sort(ints);
920          for (int i = 0; i < ints.length; i++)
921              assertEquals(ints[i], q.pollFirst());
# Line 956 | Line 927 | public class ConcurrentSkipListSubSetTes
927      public void testDescendingIterator() {
928          NavigableSet q = populatedSet(SIZE);
929          int i = 0;
930 <        Iterator it = q.iterator();
930 >        Iterator it = q.iterator();
931          while (it.hasNext()) {
932              assertTrue(q.contains(it.next()));
933              ++i;
# Line 970 | Line 941 | public class ConcurrentSkipListSubSetTes
941      public void testDescendingEmptyIterator() {
942          NavigableSet q = dset0();
943          int i = 0;
944 <        Iterator it = q.iterator();
944 >        Iterator it = q.iterator();
945          while (it.hasNext()) {
946              assertTrue(q.contains(it.next()));
947              ++i;
# Line 981 | Line 952 | public class ConcurrentSkipListSubSetTes
952      /**
953       * iterator.remove removes current element
954       */
955 <    public void testDescendingIteratorRemove () {
955 >    public void testDescendingIteratorRemove() {
956          final NavigableSet q = dset0();
957          q.add(new Integer(2));
958          q.add(new Integer(1));
# Line 997 | Line 968 | public class ConcurrentSkipListSubSetTes
968          assertFalse(it.hasNext());
969      }
970  
1000
971      /**
972       * toString contains toStrings of elements
973       */
# Line 1005 | Line 975 | public class ConcurrentSkipListSubSetTes
975          NavigableSet q = populatedSet(SIZE);
976          String s = q.toString();
977          for (int i = 0; i < SIZE; ++i) {
978 <            assertTrue(s.indexOf(String.valueOf(i)) >= 0);
978 >            assertTrue(s.contains(String.valueOf(i)));
979          }
980      }
981  
982      /**
983       * A deserialized serialized set has same elements
984       */
985 <    public void testDescendingSerialization() {
985 >    public void testDescendingSerialization() throws Exception {
986          NavigableSet q = populatedSet(SIZE);
987 <        try {
988 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
989 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
990 <            out.writeObject(q);
991 <            out.close();
992 <
993 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
994 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
995 <            NavigableSet r = (NavigableSet)in.readObject();
996 <            assertEquals(q.size(), r.size());
997 <            while (!q.isEmpty())
1028 <                assertEquals(q.pollFirst(), r.pollFirst());
1029 <        } catch (Exception e) {
1030 <            e.printStackTrace();
1031 <            unexpectedException();
1032 <        }
987 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
988 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
989 >        out.writeObject(q);
990 >        out.close();
991 >
992 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
993 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
994 >        NavigableSet r = (NavigableSet)in.readObject();
995 >        assertEquals(q.size(), r.size());
996 >        while (!q.isEmpty())
997 >            assertEquals(q.pollFirst(), r.pollFirst());
998      }
999  
1000      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines