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.13 by jsr166, Wed Aug 25 00:07:03 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       * addAll of a collection with null elements throws NPE
# 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       * addAll of a collection with any null elements throws NPE after
# Line 208 | Line 201 | public class ConcurrentSkipListSubSetTes
201                  ints[i] = new Integer(i+SIZE);
202              q.addAll(Arrays.asList(ints));
203              shouldThrow();
204 <        }
212 <        catch (NullPointerException success) {}
204 >        } catch (NullPointerException success) {}
205      }
206  
207      /**
208       * Set contains all elements of successful addAll
209       */
210      public void testAddAll5() {
211 <        try {
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)
228 <                assertEquals(new Integer(i), q.pollFirst());
229 <        }
230 <        finally {}
211 >        Integer[] empty = new Integer[0];
212 >        Integer[] ints = new Integer[SIZE];
213 >        for (int i = 0; i < SIZE; ++i)
214 >            ints[i] = new Integer(SIZE-1- i);
215 >        NavigableSet q = set0();
216 >        assertFalse(q.addAll(Arrays.asList(empty)));
217 >        assertTrue(q.addAll(Arrays.asList(ints)));
218 >        for (int i = 0; i < SIZE; ++i)
219 >            assertEquals(new Integer(i), q.pollFirst());
220      }
221  
222      /**
# Line 236 | Line 225 | public class ConcurrentSkipListSubSetTes
225      public void testPoll() {
226          NavigableSet q = populatedSet(SIZE);
227          for (int i = 0; i < SIZE; ++i) {
228 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
228 >            assertEquals(i, q.pollFirst());
229          }
230 <        assertNull(q.pollFirst());
230 >        assertNull(q.pollFirst());
231      }
232  
233      /**
# Line 349 | Line 338 | public class ConcurrentSkipListSubSetTes
338  
339          Object e4 = q.lower(zero);
340          assertNull(e4);
352
341      }
342  
343      /**
# Line 368 | Line 356 | public class ConcurrentSkipListSubSetTes
356  
357          Object e4 = q.higher(six);
358          assertNull(e4);
371
359      }
360  
361      /**
# Line 387 | Line 374 | public class ConcurrentSkipListSubSetTes
374  
375          Object e4 = q.floor(zero);
376          assertNull(e4);
390
377      }
378  
379      /**
# Line 406 | Line 392 | public class ConcurrentSkipListSubSetTes
392  
393          Object e4 = q.ceiling(six);
394          assertNull(e4);
409
395      }
396  
397      /**
# Line 414 | Line 399 | public class ConcurrentSkipListSubSetTes
399       */
400      public void testToArray() {
401          NavigableSet q = populatedSet(SIZE);
402 <        Object[] o = q.toArray();
402 >        Object[] o = q.toArray();
403          Arrays.sort(o);
404 <        for(int i = 0; i < o.length; i++)
405 <            assertEquals(o[i], q.pollFirst());
404 >        for (int i = 0; i < o.length; i++)
405 >            assertEquals(o[i], q.pollFirst());
406      }
407  
408      /**
# Line 425 | Line 410 | public class ConcurrentSkipListSubSetTes
410       */
411      public void testToArray2() {
412          NavigableSet q = populatedSet(SIZE);
413 <        Integer[] ints = new Integer[SIZE];
414 <        ints = (Integer[])q.toArray(ints);
413 >        Integer[] ints = new Integer[SIZE];
414 >        ints = (Integer[])q.toArray(ints);
415          Arrays.sort(ints);
416 <        for(int i = 0; i < ints.length; i++)
416 >        for (int i = 0; i < ints.length; i++)
417              assertEquals(ints[i], q.pollFirst());
418      }
419  
# 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       * addAll of a collection with null elements throws NPE
# Line 711 | Line 689 | public class ConcurrentSkipListSubSetTes
689              Integer[] ints = new Integer[SIZE];
690              q.addAll(Arrays.asList(ints));
691              shouldThrow();
692 <        }
715 <        catch (NullPointerException success) {}
692 >        } catch (NullPointerException success) {}
693      }
694      /**
695       * addAll of a collection with any null elements throws NPE after
# Line 726 | Line 703 | public class ConcurrentSkipListSubSetTes
703                  ints[i] = new Integer(i+SIZE);
704              q.addAll(Arrays.asList(ints));
705              shouldThrow();
706 <        }
730 <        catch (NullPointerException success) {}
706 >        } catch (NullPointerException success) {}
707      }
708  
709      /**
710       * Set contains all elements of successful addAll
711       */
712      public void testDescendingAddAll5() {
713 <        try {
714 <            Integer[] empty = new Integer[0];
715 <            Integer[] ints = new Integer[SIZE];
716 <            for (int i = 0; i < SIZE; ++i)
717 <                ints[i] = new Integer(SIZE-1- i);
718 <            NavigableSet q = dset0();
719 <            assertFalse(q.addAll(Arrays.asList(empty)));
720 <            assertTrue(q.addAll(Arrays.asList(ints)));
721 <            for (int i = 0; i < SIZE; ++i)
746 <                assertEquals(new Integer(i), q.pollFirst());
747 <        }
748 <        finally {}
713 >        Integer[] empty = new Integer[0];
714 >        Integer[] ints = new Integer[SIZE];
715 >        for (int i = 0; i < SIZE; ++i)
716 >            ints[i] = new Integer(SIZE-1- i);
717 >        NavigableSet q = dset0();
718 >        assertFalse(q.addAll(Arrays.asList(empty)));
719 >        assertTrue(q.addAll(Arrays.asList(ints)));
720 >        for (int i = 0; i < SIZE; ++i)
721 >            assertEquals(new Integer(i), q.pollFirst());
722      }
723  
724      /**
# Line 754 | Line 727 | public class ConcurrentSkipListSubSetTes
727      public void testDescendingPoll() {
728          NavigableSet q = populatedSet(SIZE);
729          for (int i = 0; i < SIZE; ++i) {
730 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
730 >            assertEquals(i, q.pollFirst());
731          }
732 <        assertNull(q.pollFirst());
732 >        assertNull(q.pollFirst());
733      }
734  
735      /**
# Line 867 | Line 840 | public class ConcurrentSkipListSubSetTes
840  
841          Object e4 = q.lower(zero);
842          assertNull(e4);
870
843      }
844  
845      /**
# Line 886 | Line 858 | public class ConcurrentSkipListSubSetTes
858  
859          Object e4 = q.higher(m6);
860          assertNull(e4);
889
861      }
862  
863      /**
# Line 905 | Line 876 | public class ConcurrentSkipListSubSetTes
876  
877          Object e4 = q.floor(zero);
878          assertNull(e4);
908
879      }
880  
881      /**
# Line 924 | Line 894 | public class ConcurrentSkipListSubSetTes
894  
895          Object e4 = q.ceiling(m6);
896          assertNull(e4);
927
897      }
898  
899      /**
# Line 932 | Line 901 | public class ConcurrentSkipListSubSetTes
901       */
902      public void testDescendingToArray() {
903          NavigableSet q = populatedSet(SIZE);
904 <        Object[] o = q.toArray();
904 >        Object[] o = q.toArray();
905          Arrays.sort(o);
906 <        for(int i = 0; i < o.length; i++)
907 <            assertEquals(o[i], q.pollFirst());
906 >        for (int i = 0; i < o.length; i++)
907 >            assertEquals(o[i], q.pollFirst());
908      }
909  
910      /**
# Line 943 | Line 912 | public class ConcurrentSkipListSubSetTes
912       */
913      public void testDescendingToArray2() {
914          NavigableSet q = populatedSet(SIZE);
915 <        Integer[] ints = new Integer[SIZE];
916 <        ints = (Integer[])q.toArray(ints);
915 >        Integer[] ints = new Integer[SIZE];
916 >        ints = (Integer[])q.toArray(ints);
917          Arrays.sort(ints);
918 <        for(int i = 0; i < ints.length; i++)
918 >        for (int i = 0; i < ints.length; i++)
919              assertEquals(ints[i], q.pollFirst());
920      }
921  
# Line 956 | Line 925 | public class ConcurrentSkipListSubSetTes
925      public void testDescendingIterator() {
926          NavigableSet q = populatedSet(SIZE);
927          int i = 0;
928 <        Iterator it = q.iterator();
929 <        while(it.hasNext()) {
928 >        Iterator it = q.iterator();
929 >        while (it.hasNext()) {
930              assertTrue(q.contains(it.next()));
931              ++i;
932          }
# Line 970 | Line 939 | public class ConcurrentSkipListSubSetTes
939      public void testDescendingEmptyIterator() {
940          NavigableSet q = dset0();
941          int i = 0;
942 <        Iterator it = q.iterator();
943 <        while(it.hasNext()) {
942 >        Iterator it = q.iterator();
943 >        while (it.hasNext()) {
944              assertTrue(q.contains(it.next()));
945              ++i;
946          }
# Line 981 | Line 950 | public class ConcurrentSkipListSubSetTes
950      /**
951       * iterator.remove removes current element
952       */
953 <    public void testDescendingIteratorRemove () {
953 >    public void testDescendingIteratorRemove() {
954          final NavigableSet q = dset0();
955          q.add(new Integer(2));
956          q.add(new Integer(1));
# Line 1012 | Line 981 | public class ConcurrentSkipListSubSetTes
981      /**
982       * A deserialized serialized set has same elements
983       */
984 <    public void testDescendingSerialization() {
984 >    public void testDescendingSerialization() throws Exception {
985          NavigableSet q = populatedSet(SIZE);
986 <        try {
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())
1028 <                assertEquals(q.pollFirst(), r.pollFirst());
1029 <        } catch(Exception e){
1030 <            e.printStackTrace();
1031 <            unexpectedException();
1032 <        }
986 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
987 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
988 >        out.writeObject(q);
989 >        out.close();
990 >
991 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
992 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
993 >        NavigableSet r = (NavigableSet)in.readObject();
994 >        assertEquals(q.size(), r.size());
995 >        while (!q.isEmpty())
996 >            assertEquals(q.pollFirst(), r.pollFirst());
997      }
998  
999      /**

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines