ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TreeSetTest.java
(Generate patch)

Comparing jsr166/src/test/tck/TreeSetTest.java (file contents):
Revision 1.7 by jsr166, Sat Nov 21 02:07:27 2009 UTC vs.
Revision 1.11 by jsr166, Sun Nov 22 18:57:17 2009 UTC

# Line 19 | Line 19 | public class TreeSetTest extends JSR166T
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 77 | Line 73 | public class TreeSetTest extends JSR166T
73          try {
74              TreeSet q = new TreeSet((Collection)null);
75              shouldThrow();
76 <        }
81 <        catch (NullPointerException success) {}
76 >        } catch (NullPointerException success) {}
77      }
78  
79      /**
# Line 89 | Line 84 | public class TreeSetTest extends JSR166T
84              Integer[] ints = new Integer[SIZE];
85              TreeSet q = new TreeSet(Arrays.asList(ints));
86              shouldThrow();
87 <        }
93 <        catch (NullPointerException success) {}
87 >        } catch (NullPointerException success) {}
88      }
89  
90      /**
# Line 103 | Line 97 | public class TreeSetTest extends JSR166T
97                  ints[i] = new Integer(i);
98              TreeSet q = new TreeSet(Arrays.asList(ints));
99              shouldThrow();
100 <        }
107 <        catch (NullPointerException success) {}
100 >        } catch (NullPointerException success) {}
101      }
102  
103      /**
104       * Set contains all elements of collection used to initialize
105       */
106      public void testConstructor6() {
107 <        try {
108 <            Integer[] ints = new Integer[SIZE];
109 <            for (int i = 0; i < SIZE; ++i)
110 <                ints[i] = new Integer(i);
111 <            TreeSet q = new TreeSet(Arrays.asList(ints));
112 <            for (int i = 0; i < SIZE; ++i)
120 <                assertEquals(ints[i], q.pollFirst());
121 <        }
122 <        finally {}
107 >        Integer[] ints = new Integer[SIZE];
108 >        for (int i = 0; i < SIZE; ++i)
109 >            ints[i] = new Integer(i);
110 >        TreeSet q = new TreeSet(Arrays.asList(ints));
111 >        for (int i = 0; i < SIZE; ++i)
112 >            assertEquals(ints[i], q.pollFirst());
113      }
114  
115      /**
116       * The comparator used in constructor is used
117       */
118      public void testConstructor7() {
119 <        try {
120 <            MyReverseComparator cmp = new MyReverseComparator();
121 <            TreeSet q = new TreeSet(cmp);
122 <            assertEquals(cmp, q.comparator());
123 <            Integer[] ints = new Integer[SIZE];
124 <            for (int i = 0; i < SIZE; ++i)
125 <                ints[i] = new Integer(i);
126 <            q.addAll(Arrays.asList(ints));
127 <            for (int i = SIZE-1; i >= 0; --i)
138 <                assertEquals(ints[i], q.pollFirst());
139 <        }
140 <        finally {}
119 >        MyReverseComparator cmp = new MyReverseComparator();
120 >        TreeSet q = new TreeSet(cmp);
121 >        assertEquals(cmp, q.comparator());
122 >        Integer[] ints = new Integer[SIZE];
123 >        for (int i = 0; i < SIZE; ++i)
124 >            ints[i] = new Integer(i);
125 >        q.addAll(Arrays.asList(ints));
126 >        for (int i = SIZE-1; i >= 0; --i)
127 >            assertEquals(ints[i], q.pollFirst());
128      }
129  
130      /**
# Line 177 | Line 164 | public class TreeSetTest extends JSR166T
164              TreeSet q = populatedSet(SIZE);
165              q.add(null);
166              shouldThrow();
167 <        } catch (NullPointerException success) { }
167 >        } catch (NullPointerException success) {}
168      }
169  
170      /**
# Line 208 | Line 195 | public class TreeSetTest extends JSR166T
195              q.add(new Object());
196              q.add(new Object());
197              shouldThrow();
198 <        }
212 <        catch (ClassCastException success) {}
198 >        } catch (ClassCastException success) {}
199      }
200  
201      /**
# Line 220 | Line 206 | public class TreeSetTest extends JSR166T
206              TreeSet q = new TreeSet();
207              q.addAll(null);
208              shouldThrow();
209 <        }
224 <        catch (NullPointerException success) {}
209 >        } catch (NullPointerException success) {}
210      }
211      /**
212       * addAll of a collection with null elements throws NPE
# Line 232 | Line 217 | public class TreeSetTest extends JSR166T
217              Integer[] ints = new Integer[SIZE];
218              q.addAll(Arrays.asList(ints));
219              shouldThrow();
220 <        }
236 <        catch (NullPointerException success) {}
220 >        } catch (NullPointerException success) {}
221      }
222      /**
223       * addAll of a collection with any null elements throws NPE after
# Line 247 | Line 231 | public class TreeSetTest extends JSR166T
231                  ints[i] = new Integer(i);
232              q.addAll(Arrays.asList(ints));
233              shouldThrow();
234 <        }
251 <        catch (NullPointerException success) {}
234 >        } catch (NullPointerException success) {}
235      }
236  
237      /**
238       * Set contains all elements of successful addAll
239       */
240      public void testAddAll5() {
241 <        try {
242 <            Integer[] empty = new Integer[0];
243 <            Integer[] ints = new Integer[SIZE];
244 <            for (int i = 0; i < SIZE; ++i)
245 <                ints[i] = new Integer(SIZE-1-i);
246 <            TreeSet q = new TreeSet();
247 <            assertFalse(q.addAll(Arrays.asList(empty)));
248 <            assertTrue(q.addAll(Arrays.asList(ints)));
249 <            for (int i = 0; i < SIZE; ++i)
267 <                assertEquals(new Integer(i), q.pollFirst());
268 <        }
269 <        finally {}
241 >        Integer[] empty = new Integer[0];
242 >        Integer[] ints = new Integer[SIZE];
243 >        for (int i = 0; i < SIZE; ++i)
244 >            ints[i] = new Integer(SIZE-1-i);
245 >        TreeSet q = new TreeSet();
246 >        assertFalse(q.addAll(Arrays.asList(empty)));
247 >        assertTrue(q.addAll(Arrays.asList(ints)));
248 >        for (int i = 0; i < SIZE; ++i)
249 >            assertEquals(new Integer(i), q.pollFirst());
250      }
251  
252      /**
# Line 275 | Line 255 | public class TreeSetTest extends JSR166T
255      public void testPollFirst() {
256          TreeSet q = populatedSet(SIZE);
257          for (int i = 0; i < SIZE; ++i) {
258 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
258 >            assertEquals(i, q.pollFirst());
259          }
260          assertNull(q.pollFirst());
261      }
# Line 286 | Line 266 | public class TreeSetTest extends JSR166T
266      public void testPollLast() {
267          TreeSet q = populatedSet(SIZE);
268          for (int i = SIZE-1; i >= 0; --i) {
269 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
269 >            assertEquals(i, q.pollLast());
270          }
271          assertNull(q.pollFirst());
272      }
# Line 545 | Line 525 | public class TreeSetTest extends JSR166T
525      /**
526       * A deserialized serialized set has same elements
527       */
528 <    public void testSerialization() {
528 >    public void testSerialization() throws Exception {
529          TreeSet q = populatedSet(SIZE);
530 <        try {
531 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
532 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
533 <            out.writeObject(q);
534 <            out.close();
535 <
536 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
537 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
538 <            TreeSet r = (TreeSet)in.readObject();
539 <            assertEquals(q.size(), r.size());
540 <            while (!q.isEmpty())
561 <                assertEquals(q.pollFirst(), r.pollFirst());
562 <        } catch (Exception e) {
563 <            e.printStackTrace();
564 <            unexpectedException();
565 <        }
530 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
531 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
532 >        out.writeObject(q);
533 >        out.close();
534 >
535 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
536 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
537 >        TreeSet r = (TreeSet)in.readObject();
538 >        assertEquals(q.size(), r.size());
539 >        while (!q.isEmpty())
540 >            assertEquals(q.pollFirst(), r.pollFirst());
541      }
542  
543      /**
# Line 690 | Line 665 | public class TreeSetTest extends JSR166T
665      /**
666       * Subsets of subsets subdivide correctly
667       */
668 <    public void testRecursiveSubSets() {
668 >    public void testRecursiveSubSets() throws Exception {
669          int setSize = 1000;
670          Class cl = TreeSet.class;
671  
# Line 709 | Line 684 | public class TreeSetTest extends JSR166T
684                     0, setSize - 1, true);
685      }
686  
687 <    static NavigableSet<Integer> newSet(Class cl) {
688 <        NavigableSet<Integer> result = null;
714 <        try {
715 <            result = (NavigableSet<Integer>) cl.newInstance();
716 <        } catch (Exception e) {
717 <            fail();
718 <        }
687 >    static NavigableSet<Integer> newSet(Class cl) throws Exception {
688 >        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
689          assertEquals(result.size(), 0);
690          assertFalse(result.iterator().hasNext());
691          return result;
# Line 778 | Line 748 | public class TreeSetTest extends JSR166T
748              } else {
749                  try {
750                      set.add(element);
751 <                    fail();
752 <                } catch (IllegalArgumentException e) {
783 <                    // expected
784 <                }
751 >                    shouldThrow();
752 >                } catch (IllegalArgumentException success) {}
753              }
754          }
755      }
# Line 980 | Line 948 | public class TreeSetTest extends JSR166T
948              assertEq(rs.last(),  -1);
949              try {
950                  set.first();
951 <                fail();
952 <            } catch (NoSuchElementException e) {
985 <                // expected
986 <            }
951 >                shouldThrow();
952 >            } catch (NoSuchElementException success) {}
953              try {
954                  set.last();
955 <                fail();
956 <            } catch (NoSuchElementException e) {
991 <                // expected
992 <            }
955 >                shouldThrow();
956 >            } catch (NoSuchElementException success) {}
957          }
958      }
959  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines