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.5 by jsr166, Mon Nov 2 20:28:31 2009 UTC vs.
Revision 1.15 by jsr166, Thu Nov 4 01:04:54 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      /**
400 <     * toArray contains all elements
400 >     * toArray contains all elements in sorted order
401       */
402      public void testToArray() {
403          NavigableSet q = populatedSet(SIZE);
404 <        Object[] o = q.toArray();
405 <        Arrays.sort(o);
406 <        for(int i = 0; i < o.length; i++)
420 <            assertEquals(o[i], q.pollFirst());
404 >        Object[] o = q.toArray();
405 >        for (int i = 0; i < o.length; i++)
406 >            assertSame(o[i], q.pollFirst());
407      }
408  
409      /**
410 <     * toArray(a) contains all elements
410 >     * toArray(a) contains all elements in sorted order
411       */
412      public void testToArray2() {
413          NavigableSet q = populatedSet(SIZE);
414 <        Integer[] ints = new Integer[SIZE];
415 <        ints = (Integer[])q.toArray(ints);
416 <        Arrays.sort(ints);
417 <        for(int i = 0; i < ints.length; i++)
432 <            assertEquals(ints[i], q.pollFirst());
414 >        Integer[] ints = new Integer[SIZE];
415 >        assertSame(ints, q.toArray(ints));
416 >        for (int i = 0; i < ints.length; i++)
417 >            assertSame(ints[i], q.pollFirst());
418      }
419  
420      /**
# Line 438 | Line 423 | public class ConcurrentSkipListSubSetTes
423      public void testIterator() {
424          NavigableSet q = populatedSet(SIZE);
425          int i = 0;
426 <        Iterator it = q.iterator();
427 <        while(it.hasNext()) {
426 >        Iterator it = q.iterator();
427 >        while (it.hasNext()) {
428              assertTrue(q.contains(it.next()));
429              ++i;
430          }
# Line 452 | Line 437 | public class ConcurrentSkipListSubSetTes
437      public void testEmptyIterator() {
438          NavigableSet q = set0();
439          int i = 0;
440 <        Iterator it = q.iterator();
441 <        while(it.hasNext()) {
440 >        Iterator it = q.iterator();
441 >        while (it.hasNext()) {
442              assertTrue(q.contains(it.next()));
443              ++i;
444          }
# Line 463 | Line 448 | public class ConcurrentSkipListSubSetTes
448      /**
449       * iterator.remove removes current element
450       */
451 <    public void testIteratorRemove () {
451 >    public void testIteratorRemove() {
452          final NavigableSet q = set0();
453          q.add(new Integer(2));
454          q.add(new Integer(1));
# Line 494 | Line 479 | public class ConcurrentSkipListSubSetTes
479      /**
480       * A deserialized serialized set has same elements
481       */
482 <    public void testSerialization() {
482 >    public void testSerialization() throws Exception {
483          NavigableSet q = populatedSet(SIZE);
484 <        try {
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())
510 <                assertEquals(q.pollFirst(), r.pollFirst());
511 <        } catch(Exception e){
512 <            e.printStackTrace();
513 <            unexpectedException();
514 <        }
484 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
485 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
486 >        out.writeObject(q);
487 >        out.close();
488 >
489 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
490 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
491 >        NavigableSet r = (NavigableSet)in.readObject();
492 >        assertEquals(q.size(), r.size());
493 >        while (!q.isEmpty())
494 >            assertEquals(q.pollFirst(), r.pollFirst());
495      }
496  
497      /**
# Line 652 | Line 632 | public class ConcurrentSkipListSubSetTes
632       * add(null) throws NPE
633       */
634      public void testDescendingAddNull() {
635 <        try {
635 >        try {
636              NavigableSet q = dset0();
637              q.add(null);
638              shouldThrow();
639 <        } catch (NullPointerException success) { }
639 >        } catch (NullPointerException success) {}
640      }
641  
642      /**
# Line 686 | Line 666 | public class ConcurrentSkipListSubSetTes
666              q.add(new Object());
667              q.add(new Object());
668              shouldThrow();
669 <        }
690 <        catch(ClassCastException success) {}
669 >        } catch (ClassCastException success) {}
670      }
671  
672  
# Line 699 | Line 678 | public class ConcurrentSkipListSubSetTes
678              NavigableSet q = dset0();
679              q.addAll(null);
680              shouldThrow();
681 <        }
703 <        catch (NullPointerException success) {}
681 >        } catch (NullPointerException success) {}
682      }
683 +
684      /**
685       * addAll of a collection with null elements throws NPE
686       */
# Line 711 | Line 690 | public class ConcurrentSkipListSubSetTes
690              Integer[] ints = new Integer[SIZE];
691              q.addAll(Arrays.asList(ints));
692              shouldThrow();
693 <        }
715 <        catch (NullPointerException success) {}
693 >        } catch (NullPointerException success) {}
694      }
695 +
696      /**
697       * addAll of a collection with any null elements throws NPE after
698       * possibly adding some elements
# Line 726 | Line 705 | public class ConcurrentSkipListSubSetTes
705                  ints[i] = new Integer(i+SIZE);
706              q.addAll(Arrays.asList(ints));
707              shouldThrow();
708 <        }
730 <        catch (NullPointerException success) {}
708 >        } catch (NullPointerException success) {}
709      }
710  
711      /**
712       * Set contains all elements of successful addAll
713       */
714      public void testDescendingAddAll5() {
715 <        try {
716 <            Integer[] empty = new Integer[0];
717 <            Integer[] ints = new Integer[SIZE];
718 <            for (int i = 0; i < SIZE; ++i)
719 <                ints[i] = new Integer(SIZE-1- i);
720 <            NavigableSet q = dset0();
721 <            assertFalse(q.addAll(Arrays.asList(empty)));
722 <            assertTrue(q.addAll(Arrays.asList(ints)));
723 <            for (int i = 0; i < SIZE; ++i)
746 <                assertEquals(new Integer(i), q.pollFirst());
747 <        }
748 <        finally {}
715 >        Integer[] empty = new Integer[0];
716 >        Integer[] ints = new Integer[SIZE];
717 >        for (int i = 0; i < SIZE; ++i)
718 >            ints[i] = new Integer(SIZE-1- i);
719 >        NavigableSet q = dset0();
720 >        assertFalse(q.addAll(Arrays.asList(empty)));
721 >        assertTrue(q.addAll(Arrays.asList(ints)));
722 >        for (int i = 0; i < SIZE; ++i)
723 >            assertEquals(new Integer(i), q.pollFirst());
724      }
725  
726      /**
# Line 754 | Line 729 | public class ConcurrentSkipListSubSetTes
729      public void testDescendingPoll() {
730          NavigableSet q = populatedSet(SIZE);
731          for (int i = 0; i < SIZE; ++i) {
732 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
732 >            assertEquals(i, q.pollFirst());
733          }
734 <        assertNull(q.pollFirst());
734 >        assertNull(q.pollFirst());
735      }
736  
737      /**
# 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++)
920 >        for (int i = 0; i < ints.length; i++)
921              assertEquals(ints[i], q.pollFirst());
922      }
923  
# 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();
931 <        while(it.hasNext()) {
930 >        Iterator it = q.iterator();
931 >        while (it.hasNext()) {
932              assertTrue(q.contains(it.next()));
933              ++i;
934          }
# Line 970 | Line 941 | public class ConcurrentSkipListSubSetTes
941      public void testDescendingEmptyIterator() {
942          NavigableSet q = dset0();
943          int i = 0;
944 <        Iterator it = q.iterator();
945 <        while(it.hasNext()) {
944 >        Iterator it = q.iterator();
945 >        while (it.hasNext()) {
946              assertTrue(q.contains(it.next()));
947              ++i;
948          }
# 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 1012 | Line 983 | public class ConcurrentSkipListSubSetTes
983      /**
984       * A deserialized serialized set has same elements
985       */
986 <    public void testDescendingSerialization() {
986 >    public void testDescendingSerialization() throws Exception {
987          NavigableSet q = populatedSet(SIZE);
988 <        try {
989 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
990 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
991 <            out.writeObject(q);
992 <            out.close();
993 <
994 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
995 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
996 <            NavigableSet r = (NavigableSet)in.readObject();
997 <            assertEquals(q.size(), r.size());
998 <            while (!q.isEmpty())
1028 <                assertEquals(q.pollFirst(), r.pollFirst());
1029 <        } catch(Exception e){
1030 <            e.printStackTrace();
1031 <            unexpectedException();
1032 <        }
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())
998 >            assertEquals(q.pollFirst(), r.pollFirst());
999      }
1000  
1001      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines