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.12 by jsr166, Tue Dec 1 09:45:15 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 400 | Line 380 | public class TreeSetTest extends JSR166T
380  
381          Object e4 = q.lower(zero);
382          assertNull(e4);
403
383      }
384  
385      /**
# Line 419 | Line 398 | public class TreeSetTest extends JSR166T
398  
399          Object e4 = q.higher(six);
400          assertNull(e4);
422
401      }
402  
403      /**
# Line 438 | Line 416 | public class TreeSetTest extends JSR166T
416  
417          Object e4 = q.floor(zero);
418          assertNull(e4);
441
419      }
420  
421      /**
# Line 457 | Line 434 | public class TreeSetTest extends JSR166T
434  
435          Object e4 = q.ceiling(six);
436          assertNull(e4);
460
437      }
438  
439      /**
# Line 545 | Line 521 | public class TreeSetTest extends JSR166T
521      /**
522       * A deserialized serialized set has same elements
523       */
524 <    public void testSerialization() {
524 >    public void testSerialization() throws Exception {
525          TreeSet q = populatedSet(SIZE);
526 <        try {
527 <            ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
528 <            ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
529 <            out.writeObject(q);
530 <            out.close();
531 <
532 <            ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
533 <            ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
534 <            TreeSet r = (TreeSet)in.readObject();
535 <            assertEquals(q.size(), r.size());
536 <            while (!q.isEmpty())
561 <                assertEquals(q.pollFirst(), r.pollFirst());
562 <        } catch (Exception e) {
563 <            e.printStackTrace();
564 <            unexpectedException();
565 <        }
526 >        ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
527 >        ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
528 >        out.writeObject(q);
529 >        out.close();
530 >
531 >        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
532 >        ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
533 >        TreeSet r = (TreeSet)in.readObject();
534 >        assertEquals(q.size(), r.size());
535 >        while (!q.isEmpty())
536 >            assertEquals(q.pollFirst(), r.pollFirst());
537      }
538  
539      /**
# Line 690 | Line 661 | public class TreeSetTest extends JSR166T
661      /**
662       * Subsets of subsets subdivide correctly
663       */
664 <    public void testRecursiveSubSets() {
664 >    public void testRecursiveSubSets() throws Exception {
665          int setSize = 1000;
666          Class cl = TreeSet.class;
667  
# Line 709 | Line 680 | public class TreeSetTest extends JSR166T
680                     0, setSize - 1, true);
681      }
682  
683 <    static NavigableSet<Integer> newSet(Class cl) {
684 <        NavigableSet<Integer> result = null;
714 <        try {
715 <            result = (NavigableSet<Integer>) cl.newInstance();
716 <        } catch (Exception e) {
717 <            fail();
718 <        }
683 >    static NavigableSet<Integer> newSet(Class cl) throws Exception {
684 >        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
685          assertEquals(result.size(), 0);
686          assertFalse(result.iterator().hasNext());
687          return result;
# Line 778 | Line 744 | public class TreeSetTest extends JSR166T
744              } else {
745                  try {
746                      set.add(element);
747 <                    fail();
748 <                } catch (IllegalArgumentException e) {
783 <                    // expected
784 <                }
747 >                    shouldThrow();
748 >                } catch (IllegalArgumentException success) {}
749              }
750          }
751      }
# Line 980 | Line 944 | public class TreeSetTest extends JSR166T
944              assertEq(rs.last(),  -1);
945              try {
946                  set.first();
947 <                fail();
948 <            } catch (NoSuchElementException e) {
985 <                // expected
986 <            }
947 >                shouldThrow();
948 >            } catch (NoSuchElementException success) {}
949              try {
950                  set.last();
951 <                fail();
952 <            } catch (NoSuchElementException e) {
991 <                // expected
992 <            }
951 >                shouldThrow();
952 >            } catch (NoSuchElementException success) {}
953          }
954      }
955  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines