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.18 by jsr166, Tue Mar 15 19:47:06 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 134 | Line 131 | public class ConcurrentSkipListSubSetTes
131       * add(null) throws NPE
132       */
133      public void testAddNull() {
134 <        try {
134 >        try {
135              NavigableSet q = set0();
136              q.add(null);
137              shouldThrow();
138 <        } catch (NullPointerException success) { }
138 >        } catch (NullPointerException success) {}
139      }
140  
141      /**
# Line 168 | Line 165 | public class ConcurrentSkipListSubSetTes
165              q.add(new Object());
166              q.add(new Object());
167              shouldThrow();
168 <        }
172 <        catch (ClassCastException success) {}
168 >        } catch (ClassCastException success) {}
169      }
170  
171  
# Line 181 | Line 177 | public class ConcurrentSkipListSubSetTes
177              NavigableSet q = set0();
178              q.addAll(null);
179              shouldThrow();
180 <        }
185 <        catch (NullPointerException success) {}
180 >        } catch (NullPointerException success) {}
181      }
182 +
183      /**
184       * addAll of a collection with null elements throws NPE
185       */
# Line 193 | Line 189 | public class ConcurrentSkipListSubSetTes
189              Integer[] ints = new Integer[SIZE];
190              q.addAll(Arrays.asList(ints));
191              shouldThrow();
192 <        }
197 <        catch (NullPointerException success) {}
192 >        } catch (NullPointerException success) {}
193      }
194 +
195      /**
196       * addAll of a collection with any null elements throws NPE after
197       * possibly adding some elements
# Line 208 | Line 204 | public class ConcurrentSkipListSubSetTes
204                  ints[i] = new Integer(i+SIZE);
205              q.addAll(Arrays.asList(ints));
206              shouldThrow();
207 <        }
212 <        catch (NullPointerException success) {}
207 >        } catch (NullPointerException success) {}
208      }
209  
210      /**
211       * Set contains all elements of successful addAll
212       */
213      public void testAddAll5() {
214 <        try {
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)
228 <                assertEquals(new Integer(i), q.pollFirst());
229 <        }
230 <        finally {}
214 >        Integer[] empty = new Integer[0];
215 >        Integer[] ints = new Integer[SIZE];
216 >        for (int i = 0; i < SIZE; ++i)
217 >            ints[i] = new Integer(SIZE-1- i);
218 >        NavigableSet q = set0();
219 >        assertFalse(q.addAll(Arrays.asList(empty)));
220 >        assertTrue(q.addAll(Arrays.asList(ints)));
221 >        for (int i = 0; i < SIZE; ++i)
222 >            assertEquals(new Integer(i), q.pollFirst());
223      }
224  
225      /**
# Line 236 | Line 228 | public class ConcurrentSkipListSubSetTes
228      public void testPoll() {
229          NavigableSet q = populatedSet(SIZE);
230          for (int i = 0; i < SIZE; ++i) {
231 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
231 >            assertEquals(i, q.pollFirst());
232          }
233 <        assertNull(q.pollFirst());
233 >        assertNull(q.pollFirst());
234      }
235  
236      /**
# Line 247 | Line 239 | public class ConcurrentSkipListSubSetTes
239      public void testRemoveElement() {
240          NavigableSet q = populatedSet(SIZE);
241          for (int i = 1; i < SIZE; i+=2) {
242 <            assertTrue(q.remove(new Integer(i)));
242 >            assertTrue(q.contains(i));
243 >            assertTrue(q.remove(i));
244 >            assertFalse(q.contains(i));
245 >            assertTrue(q.contains(i-1));
246          }
247          for (int i = 0; i < SIZE; i+=2) {
248 <            assertTrue(q.remove(new Integer(i)));
249 <            assertFalse(q.remove(new Integer(i+1)));
248 >            assertTrue(q.contains(i));
249 >            assertTrue(q.remove(i));
250 >            assertFalse(q.contains(i));
251 >            assertFalse(q.remove(i+1));
252 >            assertFalse(q.contains(i+1));
253          }
254          assertTrue(q.isEmpty());
255      }
# Line 349 | Line 347 | public class ConcurrentSkipListSubSetTes
347  
348          Object e4 = q.lower(zero);
349          assertNull(e4);
352
350      }
351  
352      /**
# Line 368 | Line 365 | public class ConcurrentSkipListSubSetTes
365  
366          Object e4 = q.higher(six);
367          assertNull(e4);
371
368      }
369  
370      /**
# Line 387 | Line 383 | public class ConcurrentSkipListSubSetTes
383  
384          Object e4 = q.floor(zero);
385          assertNull(e4);
390
386      }
387  
388      /**
# Line 406 | Line 401 | public class ConcurrentSkipListSubSetTes
401  
402          Object e4 = q.ceiling(six);
403          assertNull(e4);
409
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();
412 <        Arrays.sort(o);
413 <        for (int i = 0; i < o.length; i++)
420 <            assertEquals(o[i], q.pollFirst());
411 >        Object[] o = q.toArray();
412 >        for (int i = 0; i < o.length; i++)
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);
421 <        Integer[] ints = new Integer[SIZE];
422 <        ints = (Integer[])q.toArray(ints);
423 <        Arrays.sort(ints);
420 >        NavigableSet<Integer> q = populatedSet(SIZE);
421 >        Integer[] ints = new Integer[SIZE];
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 438 | Line 431 | public class ConcurrentSkipListSubSetTes
431      public void testIterator() {
432          NavigableSet q = populatedSet(SIZE);
433          int i = 0;
434 <        Iterator it = q.iterator();
434 >        Iterator it = q.iterator();
435          while (it.hasNext()) {
436              assertTrue(q.contains(it.next()));
437              ++i;
# Line 452 | Line 445 | public class ConcurrentSkipListSubSetTes
445      public void testEmptyIterator() {
446          NavigableSet q = set0();
447          int i = 0;
448 <        Iterator it = q.iterator();
448 >        Iterator it = q.iterator();
449          while (it.hasNext()) {
450              assertTrue(q.contains(it.next()));
451              ++i;
# Line 463 | Line 456 | public class ConcurrentSkipListSubSetTes
456      /**
457       * iterator.remove removes current element
458       */
459 <    public void testIteratorRemove () {
459 >    public void testIteratorRemove() {
460          final NavigableSet q = set0();
461          q.add(new Integer(2));
462          q.add(new Integer(1));
# Line 494 | Line 487 | public class ConcurrentSkipListSubSetTes
487      /**
488       * A deserialized serialized set has same elements
489       */
490 <    public void testSerialization() {
490 >    public void testSerialization() throws Exception {
491          NavigableSet q = populatedSet(SIZE);
492 <        try {
493 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
494 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
495 <            out.writeObject(q);
496 <            out.close();
497 <
498 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
499 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
500 <            NavigableSet r = (NavigableSet)in.readObject();
501 <            assertEquals(q.size(), r.size());
502 <            while (!q.isEmpty())
510 <                assertEquals(q.pollFirst(), r.pollFirst());
511 <        } catch (Exception e){
512 <            e.printStackTrace();
513 <            unexpectedException();
514 <        }
492 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
493 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
494 >        out.writeObject(q);
495 >        out.close();
496 >
497 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
498 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
499 >        NavigableSet r = (NavigableSet)in.readObject();
500 >        assertEquals(q.size(), r.size());
501 >        while (!q.isEmpty())
502 >            assertEquals(q.pollFirst(), r.pollFirst());
503      }
504  
505      /**
# Line 652 | Line 640 | public class ConcurrentSkipListSubSetTes
640       * add(null) throws NPE
641       */
642      public void testDescendingAddNull() {
643 <        try {
643 >        try {
644              NavigableSet q = dset0();
645              q.add(null);
646              shouldThrow();
647 <        } catch (NullPointerException success) { }
647 >        } catch (NullPointerException success) {}
648      }
649  
650      /**
# Line 686 | Line 674 | public class ConcurrentSkipListSubSetTes
674              q.add(new Object());
675              q.add(new Object());
676              shouldThrow();
677 <        }
690 <        catch (ClassCastException success) {}
677 >        } catch (ClassCastException success) {}
678      }
679  
680  
# Line 699 | Line 686 | public class ConcurrentSkipListSubSetTes
686              NavigableSet q = dset0();
687              q.addAll(null);
688              shouldThrow();
689 <        }
703 <        catch (NullPointerException success) {}
689 >        } catch (NullPointerException success) {}
690      }
691 +
692      /**
693       * addAll of a collection with null elements throws NPE
694       */
# Line 711 | Line 698 | public class ConcurrentSkipListSubSetTes
698              Integer[] ints = new Integer[SIZE];
699              q.addAll(Arrays.asList(ints));
700              shouldThrow();
701 <        }
715 <        catch (NullPointerException success) {}
701 >        } catch (NullPointerException success) {}
702      }
703 +
704      /**
705       * addAll of a collection with any null elements throws NPE after
706       * possibly adding some elements
# Line 726 | Line 713 | public class ConcurrentSkipListSubSetTes
713                  ints[i] = new Integer(i+SIZE);
714              q.addAll(Arrays.asList(ints));
715              shouldThrow();
716 <        }
730 <        catch (NullPointerException success) {}
716 >        } catch (NullPointerException success) {}
717      }
718  
719      /**
720       * Set contains all elements of successful addAll
721       */
722      public void testDescendingAddAll5() {
723 <        try {
724 <            Integer[] empty = new Integer[0];
725 <            Integer[] ints = new Integer[SIZE];
726 <            for (int i = 0; i < SIZE; ++i)
727 <                ints[i] = new Integer(SIZE-1- i);
728 <            NavigableSet q = dset0();
729 <            assertFalse(q.addAll(Arrays.asList(empty)));
730 <            assertTrue(q.addAll(Arrays.asList(ints)));
731 <            for (int i = 0; i < SIZE; ++i)
746 <                assertEquals(new Integer(i), q.pollFirst());
747 <        }
748 <        finally {}
723 >        Integer[] empty = new Integer[0];
724 >        Integer[] ints = new Integer[SIZE];
725 >        for (int i = 0; i < SIZE; ++i)
726 >            ints[i] = new Integer(SIZE-1- i);
727 >        NavigableSet q = dset0();
728 >        assertFalse(q.addAll(Arrays.asList(empty)));
729 >        assertTrue(q.addAll(Arrays.asList(ints)));
730 >        for (int i = 0; i < SIZE; ++i)
731 >            assertEquals(new Integer(i), q.pollFirst());
732      }
733  
734      /**
# Line 754 | Line 737 | public class ConcurrentSkipListSubSetTes
737      public void testDescendingPoll() {
738          NavigableSet q = populatedSet(SIZE);
739          for (int i = 0; i < SIZE; ++i) {
740 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
740 >            assertEquals(i, q.pollFirst());
741          }
742 <        assertNull(q.pollFirst());
742 >        assertNull(q.pollFirst());
743      }
744  
745      /**
# 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 1012 | Line 991 | public class ConcurrentSkipListSubSetTes
991      /**
992       * A deserialized serialized set has same elements
993       */
994 <    public void testDescendingSerialization() {
994 >    public void testDescendingSerialization() throws Exception {
995          NavigableSet q = populatedSet(SIZE);
996 <        try {
997 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
998 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
999 <            out.writeObject(q);
1000 <            out.close();
1001 <
1002 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1003 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1004 <            NavigableSet r = (NavigableSet)in.readObject();
1005 <            assertEquals(q.size(), r.size());
1006 <            while (!q.isEmpty())
1028 <                assertEquals(q.pollFirst(), r.pollFirst());
1029 <        } catch (Exception e){
1030 <            e.printStackTrace();
1031 <            unexpectedException();
1032 <        }
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();
1004 >        assertEquals(q.size(), r.size());
1005 >        while (!q.isEmpty())
1006 >            assertEquals(q.pollFirst(), r.pollFirst());
1007      }
1008  
1009      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines