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.8 by jsr166, Sat Nov 21 10:25:05 2009 UTC vs.
Revision 1.18 by jsr166, Fri Nov 5 00:17:22 2010 UTC

# Line 11 | Line 11 | import java.io.*;
11  
12   public class TreeSetTest 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(TreeSetTest.class);
# 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 36 | Line 32 | public class TreeSetTest extends JSR166T
32       * Create a set of given size containing consecutive
33       * Integers 0 ... n.
34       */
35 <    private TreeSet populatedSet(int n) {
36 <        TreeSet q = new TreeSet();
35 >    private TreeSet<Integer> populatedSet(int n) {
36 >        TreeSet<Integer> q = new TreeSet<Integer>();
37          assertTrue(q.isEmpty());
38          for (int i = n-1; i >= 0; i-=2)
39              assertTrue(q.add(new Integer(i)));
# Line 108 | Line 104 | public class TreeSetTest extends JSR166T
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)
117 <                assertEquals(ints[i], q.pollFirst());
118 <        }
119 <        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)
135 <                assertEquals(ints[i], q.pollFirst());
136 <        }
137 <        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 218 | Line 208 | public class TreeSetTest extends JSR166T
208              shouldThrow();
209          } catch (NullPointerException success) {}
210      }
211 +
212      /**
213       * addAll of a collection with null elements throws NPE
214       */
# Line 229 | Line 220 | public class TreeSetTest extends JSR166T
220              shouldThrow();
221          } catch (NullPointerException success) {}
222      }
223 +
224      /**
225       * addAll of a collection with any null elements throws NPE after
226       * possibly adding some elements
# Line 248 | Line 240 | public class TreeSetTest extends JSR166T
240       * Set contains all elements of successful addAll
241       */
242      public void testAddAll5() {
243 <        try {
244 <            Integer[] empty = new Integer[0];
245 <            Integer[] ints = new Integer[SIZE];
246 <            for (int i = 0; i < SIZE; ++i)
247 <                ints[i] = new Integer(SIZE-1-i);
248 <            TreeSet q = new TreeSet();
249 <            assertFalse(q.addAll(Arrays.asList(empty)));
250 <            assertTrue(q.addAll(Arrays.asList(ints)));
251 <            for (int i = 0; i < SIZE; ++i)
260 <                assertEquals(new Integer(i), q.pollFirst());
261 <        }
262 <        finally {}
243 >        Integer[] empty = new Integer[0];
244 >        Integer[] ints = new Integer[SIZE];
245 >        for (int i = 0; i < SIZE; ++i)
246 >            ints[i] = new Integer(SIZE-1-i);
247 >        TreeSet q = new TreeSet();
248 >        assertFalse(q.addAll(Arrays.asList(empty)));
249 >        assertTrue(q.addAll(Arrays.asList(ints)));
250 >        for (int i = 0; i < SIZE; ++i)
251 >            assertEquals(new Integer(i), q.pollFirst());
252      }
253  
254      /**
# Line 268 | Line 257 | public class TreeSetTest extends JSR166T
257      public void testPollFirst() {
258          TreeSet q = populatedSet(SIZE);
259          for (int i = 0; i < SIZE; ++i) {
260 <            assertEquals(i, ((Integer)q.pollFirst()).intValue());
260 >            assertEquals(i, q.pollFirst());
261          }
262          assertNull(q.pollFirst());
263      }
# Line 279 | Line 268 | public class TreeSetTest extends JSR166T
268      public void testPollLast() {
269          TreeSet q = populatedSet(SIZE);
270          for (int i = SIZE-1; i >= 0; --i) {
271 <            assertEquals(i, ((Integer)q.pollLast()).intValue());
271 >            assertEquals(i, q.pollLast());
272          }
273          assertNull(q.pollFirst());
274      }
# Line 393 | Line 382 | public class TreeSetTest extends JSR166T
382  
383          Object e4 = q.lower(zero);
384          assertNull(e4);
396
385      }
386  
387      /**
# Line 412 | Line 400 | public class TreeSetTest extends JSR166T
400  
401          Object e4 = q.higher(six);
402          assertNull(e4);
415
403      }
404  
405      /**
# Line 431 | Line 418 | public class TreeSetTest extends JSR166T
418  
419          Object e4 = q.floor(zero);
420          assertNull(e4);
434
421      }
422  
423      /**
# Line 450 | Line 436 | public class TreeSetTest extends JSR166T
436  
437          Object e4 = q.ceiling(six);
438          assertNull(e4);
453
439      }
440  
441      /**
442 <     * toArray contains all elements
442 >     * toArray contains all elements in sorted order
443       */
444      public void testToArray() {
445          TreeSet q = populatedSet(SIZE);
446          Object[] o = q.toArray();
462        Arrays.sort(o);
447          for (int i = 0; i < o.length; i++)
448 <            assertEquals(o[i], q.pollFirst());
448 >            assertSame(o[i], q.pollFirst());
449      }
450  
451      /**
452 <     * toArray(a) contains all elements
452 >     * toArray(a) contains all elements in sorted order
453       */
454      public void testToArray2() {
455 <        TreeSet q = populatedSet(SIZE);
455 >        TreeSet<Integer> q = populatedSet(SIZE);
456          Integer[] ints = new Integer[SIZE];
457 <        ints = (Integer[])q.toArray(ints);
458 <        Arrays.sort(ints);
457 >        Integer[] array = q.toArray(ints);
458 >        assertSame(ints, array);
459          for (int i = 0; i < ints.length; i++)
460 <            assertEquals(ints[i], q.pollFirst());
460 >            assertSame(ints[i], q.pollFirst());
461      }
462  
463      /**
# Line 507 | Line 491 | public class TreeSetTest extends JSR166T
491      /**
492       * iterator.remove removes current element
493       */
494 <    public void testIteratorRemove () {
494 >    public void testIteratorRemove() {
495          final TreeSet q = new TreeSet();
496          q.add(new Integer(2));
497          q.add(new Integer(1));
# Line 678 | Line 662 | public class TreeSetTest extends JSR166T
662      /**
663       * Subsets of subsets subdivide correctly
664       */
665 <    public void testRecursiveSubSets() {
666 <        int setSize = 1000;
665 >    public void testRecursiveSubSets() throws Exception {
666 >        int setSize = expensiveTests ? 1000 : 100;
667          Class cl = TreeSet.class;
668  
669          NavigableSet<Integer> set = newSet(cl);
# Line 697 | Line 681 | public class TreeSetTest extends JSR166T
681                     0, setSize - 1, true);
682      }
683  
684 <    static NavigableSet<Integer> newSet(Class cl) {
685 <        NavigableSet<Integer> result = null;
702 <        try {
703 <            result = (NavigableSet<Integer>) cl.newInstance();
704 <        } catch (Exception e) {
705 <            fail();
706 <        }
684 >    static NavigableSet<Integer> newSet(Class cl) throws Exception {
685 >        NavigableSet<Integer> result = (NavigableSet<Integer>) cl.newInstance();
686          assertEquals(result.size(), 0);
687          assertFalse(result.iterator().hasNext());
688          return result;
# Line 766 | Line 745 | public class TreeSetTest extends JSR166T
745              } else {
746                  try {
747                      set.add(element);
748 <                    fail();
749 <                } catch (IllegalArgumentException e) {
771 <                    // expected
772 <                }
748 >                    shouldThrow();
749 >                } catch (IllegalArgumentException success) {}
750              }
751          }
752      }
# Line 902 | Line 879 | public class TreeSetTest extends JSR166T
879                  // BitSet should support this! Test would run much faster
880                  while (element >= min) {
881                      if (bs.get(element))
882 <                        return(element);
882 >                        return element;
883                      element--;
884                  }
885                  return -1;
# Line 968 | Line 945 | public class TreeSetTest extends JSR166T
945              assertEq(rs.last(),  -1);
946              try {
947                  set.first();
948 <                fail();
949 <            } catch (NoSuchElementException e) {
973 <                // expected
974 <            }
948 >                shouldThrow();
949 >            } catch (NoSuchElementException success) {}
950              try {
951                  set.last();
952 <                fail();
953 <            } catch (NoSuchElementException e) {
979 <                // expected
980 <            }
952 >                shouldThrow();
953 >            } catch (NoSuchElementException success) {}
954          }
955      }
956  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines