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.14 by jsr166, Wed Aug 25 01:44:48 2010 UTC

# 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 35 | Line 31 | public class ConcurrentSkipListSubSetTes
31          ConcurrentSkipListSet q = new ConcurrentSkipListSet();
32          assertTrue(q.isEmpty());
33  
34 <        for (int i = n-1; i >= 0; i-=2)
35 <            assertTrue(q.add(new Integer(i)));
36 <        for (int i = (n & 1); i < n; i+=2)
37 <            assertTrue(q.add(new Integer(i)));
34 >        for (int i = n-1; i >= 0; i-=2)
35 >            assertTrue(q.add(new Integer(i)));
36 >        for (int i = (n & 1); i < n; i+=2)
37 >            assertTrue(q.add(new Integer(i)));
38          assertTrue(q.add(new Integer(-n)));
39          assertTrue(q.add(new Integer(n)));
40          NavigableSet s = q.subSet(new Integer(0), true, new Integer(n), false);
41          assertFalse(s.isEmpty());
42 <        assertEquals(n, s.size());
42 >        assertEquals(n, s.size());
43          return s;
44      }
45  
# Line 61 | Line 57 | public class ConcurrentSkipListSubSetTes
57          q.add(zero);
58          q.add(seven);
59          NavigableSet s = q.subSet(one, true, seven, false);
60 <        assertEquals(5, s.size());
60 >        assertEquals(5, s.size());
61          return s;
62      }
63  
# Line 77 | Line 73 | public class ConcurrentSkipListSubSetTes
73          q.add(m4);
74          q.add(m5);
75          NavigableSet s = q.descendingSet();
76 <        assertEquals(5, s.size());
76 >        assertEquals(5, s.size());
77          return s;
78      }
79  
80      private static NavigableSet set0() {
81 <        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
81 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
82          assertTrue(set.isEmpty());
83          return set.tailSet(m1, true);
84      }
85  
86      private static NavigableSet dset0() {
87 <        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
87 >        ConcurrentSkipListSet set = new ConcurrentSkipListSet();
88          assertTrue(set.isEmpty());
89          return set;
90      }
# 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  
170  
# Line 181 | Line 176 | public class ConcurrentSkipListSubSetTes
176              NavigableSet q = set0();
177              q.addAll(null);
178              shouldThrow();
179 <        }
185 <        catch (NullPointerException success) {}
179 >        } catch (NullPointerException success) {}
180      }
181 +
182      /**
183       * addAll of a collection with null elements throws NPE
184       */
# Line 193 | Line 188 | public class ConcurrentSkipListSubSetTes
188              Integer[] ints = new Integer[SIZE];
189              q.addAll(Arrays.asList(ints));
190              shouldThrow();
191 <        }
197 <        catch (NullPointerException success) {}
191 >        } catch (NullPointerException success) {}
192      }
193 +
194      /**
195       * addAll of a collection with any null elements throws NPE after
196       * possibly adding some elements
# Line 208 | Line 203 | public class ConcurrentSkipListSubSetTes
203                  ints[i] = new Integer(i+SIZE);
204              q.addAll(Arrays.asList(ints));
205              shouldThrow();
206 <        }
212 <        catch (NullPointerException success) {}
206 >        } catch (NullPointerException success) {}
207      }
208  
209      /**
210       * Set contains all elements of successful addAll
211       */
212      public void testAddAll5() {
213 <        try {
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)
228 <                assertEquals(new Integer(i), q.pollFirst());
229 <        }
230 <        finally {}
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)
221 >            assertEquals(new Integer(i), q.pollFirst());
222      }
223  
224      /**
# Line 236 | Line 227 | public class ConcurrentSkipListSubSetTes
227      public void testPoll() {
228          NavigableSet q = populatedSet(SIZE);
229          for (int i = 0; i < SIZE; ++i) {
230 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
230 >            assertEquals(i, q.pollFirst());
231          }
232 <        assertNull(q.pollFirst());
232 >        assertNull(q.pollFirst());
233      }
234  
235      /**
# Line 349 | Line 340 | public class ConcurrentSkipListSubSetTes
340  
341          Object e4 = q.lower(zero);
342          assertNull(e4);
352
343      }
344  
345      /**
# Line 368 | Line 358 | public class ConcurrentSkipListSubSetTes
358  
359          Object e4 = q.higher(six);
360          assertNull(e4);
371
361      }
362  
363      /**
# Line 387 | Line 376 | public class ConcurrentSkipListSubSetTes
376  
377          Object e4 = q.floor(zero);
378          assertNull(e4);
390
379      }
380  
381      /**
# Line 406 | Line 394 | public class ConcurrentSkipListSubSetTes
394  
395          Object e4 = q.ceiling(six);
396          assertNull(e4);
409
397      }
398  
399      /**
# Line 414 | Line 401 | public class ConcurrentSkipListSubSetTes
401       */
402      public void testToArray() {
403          NavigableSet q = populatedSet(SIZE);
404 <        Object[] o = q.toArray();
404 >        Object[] o = q.toArray();
405          Arrays.sort(o);
406 <        for (int i = 0; i < o.length; i++)
407 <            assertEquals(o[i], q.pollFirst());
406 >        for (int i = 0; i < o.length; i++)
407 >            assertEquals(o[i], q.pollFirst());
408      }
409  
410      /**
# Line 425 | Line 412 | public class ConcurrentSkipListSubSetTes
412       */
413      public void testToArray2() {
414          NavigableSet q = populatedSet(SIZE);
415 <        Integer[] ints = new Integer[SIZE];
416 <        ints = (Integer[])q.toArray(ints);
415 >        Integer[] ints = new Integer[SIZE];
416 >        ints = (Integer[])q.toArray(ints);
417          Arrays.sort(ints);
418          for (int i = 0; i < ints.length; i++)
419              assertEquals(ints[i], q.pollFirst());
# Line 438 | Line 425 | public class ConcurrentSkipListSubSetTes
425      public void testIterator() {
426          NavigableSet q = populatedSet(SIZE);
427          int i = 0;
428 <        Iterator it = q.iterator();
428 >        Iterator it = q.iterator();
429          while (it.hasNext()) {
430              assertTrue(q.contains(it.next()));
431              ++i;
# Line 452 | Line 439 | public class ConcurrentSkipListSubSetTes
439      public void testEmptyIterator() {
440          NavigableSet q = set0();
441          int i = 0;
442 <        Iterator it = q.iterator();
442 >        Iterator it = q.iterator();
443          while (it.hasNext()) {
444              assertTrue(q.contains(it.next()));
445              ++i;
# Line 463 | Line 450 | public class ConcurrentSkipListSubSetTes
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 494 | Line 481 | public class ConcurrentSkipListSubSetTes
481      /**
482       * A deserialized serialized set has same elements
483       */
484 <    public void testSerialization() {
484 >    public void testSerialization() throws Exception {
485          NavigableSet q = populatedSet(SIZE);
486 <        try {
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())
510 <                assertEquals(q.pollFirst(), r.pollFirst());
511 <        } catch (Exception e){
512 <            e.printStackTrace();
513 <            unexpectedException();
514 <        }
486 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
487 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
488 >        out.writeObject(q);
489 >        out.close();
490 >
491 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
492 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
493 >        NavigableSet r = (NavigableSet)in.readObject();
494 >        assertEquals(q.size(), r.size());
495 >        while (!q.isEmpty())
496 >            assertEquals(q.pollFirst(), r.pollFirst());
497      }
498  
499      /**
# Line 652 | Line 634 | public class ConcurrentSkipListSubSetTes
634       * add(null) throws NPE
635       */
636      public void testDescendingAddNull() {
637 <        try {
637 >        try {
638              NavigableSet q = dset0();
639              q.add(null);
640              shouldThrow();
641 <        } catch (NullPointerException success) { }
641 >        } catch (NullPointerException success) {}
642      }
643  
644      /**
# Line 686 | Line 668 | public class ConcurrentSkipListSubSetTes
668              q.add(new Object());
669              q.add(new Object());
670              shouldThrow();
671 <        }
690 <        catch (ClassCastException success) {}
671 >        } catch (ClassCastException success) {}
672      }
673  
674  
# 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 867 | Line 844 | public class ConcurrentSkipListSubSetTes
844  
845          Object e4 = q.lower(zero);
846          assertNull(e4);
870
847      }
848  
849      /**
# Line 886 | Line 862 | public class ConcurrentSkipListSubSetTes
862  
863          Object e4 = q.higher(m6);
864          assertNull(e4);
889
865      }
866  
867      /**
# Line 905 | Line 880 | public class ConcurrentSkipListSubSetTes
880  
881          Object e4 = q.floor(zero);
882          assertNull(e4);
908
883      }
884  
885      /**
# Line 924 | Line 898 | public class ConcurrentSkipListSubSetTes
898  
899          Object e4 = q.ceiling(m6);
900          assertNull(e4);
927
901      }
902  
903      /**
# Line 932 | Line 905 | public class ConcurrentSkipListSubSetTes
905       */
906      public void testDescendingToArray() {
907          NavigableSet q = populatedSet(SIZE);
908 <        Object[] o = q.toArray();
908 >        Object[] o = q.toArray();
909          Arrays.sort(o);
910 <        for (int i = 0; i < o.length; i++)
911 <            assertEquals(o[i], q.pollFirst());
910 >        for (int i = 0; i < o.length; i++)
911 >            assertEquals(o[i], q.pollFirst());
912      }
913  
914      /**
# Line 943 | Line 916 | public class ConcurrentSkipListSubSetTes
916       */
917      public void testDescendingToArray2() {
918          NavigableSet q = populatedSet(SIZE);
919 <        Integer[] ints = new Integer[SIZE];
920 <        ints = (Integer[])q.toArray(ints);
919 >        Integer[] ints = new Integer[SIZE];
920 >        ints = (Integer[])q.toArray(ints);
921          Arrays.sort(ints);
922          for (int i = 0; i < ints.length; i++)
923              assertEquals(ints[i], q.pollFirst());
# Line 956 | Line 929 | public class ConcurrentSkipListSubSetTes
929      public void testDescendingIterator() {
930          NavigableSet q = populatedSet(SIZE);
931          int i = 0;
932 <        Iterator it = q.iterator();
932 >        Iterator it = q.iterator();
933          while (it.hasNext()) {
934              assertTrue(q.contains(it.next()));
935              ++i;
# Line 970 | Line 943 | public class ConcurrentSkipListSubSetTes
943      public void testDescendingEmptyIterator() {
944          NavigableSet q = dset0();
945          int i = 0;
946 <        Iterator it = q.iterator();
946 >        Iterator it = q.iterator();
947          while (it.hasNext()) {
948              assertTrue(q.contains(it.next()));
949              ++i;
# Line 981 | Line 954 | public class ConcurrentSkipListSubSetTes
954      /**
955       * iterator.remove removes current element
956       */
957 <    public void testDescendingIteratorRemove () {
957 >    public void testDescendingIteratorRemove() {
958          final NavigableSet q = dset0();
959          q.add(new Integer(2));
960          q.add(new Integer(1));
# Line 1012 | Line 985 | public class ConcurrentSkipListSubSetTes
985      /**
986       * A deserialized serialized set has same elements
987       */
988 <    public void testDescendingSerialization() {
988 >    public void testDescendingSerialization() throws Exception {
989          NavigableSet q = populatedSet(SIZE);
990 <        try {
991 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
992 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
993 <            out.writeObject(q);
994 <            out.close();
995 <
996 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
997 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
998 <            NavigableSet r = (NavigableSet)in.readObject();
999 <            assertEquals(q.size(), r.size());
1000 <            while (!q.isEmpty())
1028 <                assertEquals(q.pollFirst(), r.pollFirst());
1029 <        } catch (Exception e){
1030 <            e.printStackTrace();
1031 <            unexpectedException();
1032 <        }
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());
1001      }
1002  
1003      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines