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); |
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 |
|
|
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 |
|
/** |
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) |
260 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
261 |
< |
} |
262 |
< |
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 |
|
/** |
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 |
|
} |
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 |
|
} |
380 |
|
|
381 |
|
Object e4 = q.lower(zero); |
382 |
|
assertNull(e4); |
396 |
– |
|
383 |
|
} |
384 |
|
|
385 |
|
/** |
398 |
|
|
399 |
|
Object e4 = q.higher(six); |
400 |
|
assertNull(e4); |
415 |
– |
|
401 |
|
} |
402 |
|
|
403 |
|
/** |
416 |
|
|
417 |
|
Object e4 = q.floor(zero); |
418 |
|
assertNull(e4); |
434 |
– |
|
419 |
|
} |
420 |
|
|
421 |
|
/** |
434 |
|
|
435 |
|
Object e4 = q.ceiling(six); |
436 |
|
assertNull(e4); |
453 |
– |
|
437 |
|
} |
438 |
|
|
439 |
|
/** |
490 |
|
/** |
491 |
|
* iterator.remove removes current element |
492 |
|
*/ |
493 |
< |
public void testIteratorRemove () { |
493 |
> |
public void testIteratorRemove() { |
494 |
|
final TreeSet q = new TreeSet(); |
495 |
|
q.add(new Integer(2)); |
496 |
|
q.add(new Integer(1)); |
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 |
|
|
680 |
|
0, setSize - 1, true); |
681 |
|
} |
682 |
|
|
683 |
< |
static NavigableSet<Integer> newSet(Class cl) { |
684 |
< |
NavigableSet<Integer> result = null; |
702 |
< |
try { |
703 |
< |
result = (NavigableSet<Integer>) cl.newInstance(); |
704 |
< |
} catch (Exception e) { |
705 |
< |
fail(); |
706 |
< |
} |
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; |
744 |
|
} else { |
745 |
|
try { |
746 |
|
set.add(element); |
747 |
< |
fail(); |
748 |
< |
} catch (IllegalArgumentException e) { |
771 |
< |
// expected |
772 |
< |
} |
747 |
> |
shouldThrow(); |
748 |
> |
} catch (IllegalArgumentException success) {} |
749 |
|
} |
750 |
|
} |
751 |
|
} |
944 |
|
assertEq(rs.last(), -1); |
945 |
|
try { |
946 |
|
set.first(); |
947 |
< |
fail(); |
948 |
< |
} catch (NoSuchElementException e) { |
973 |
< |
// expected |
974 |
< |
} |
947 |
> |
shouldThrow(); |
948 |
> |
} catch (NoSuchElementException success) {} |
949 |
|
try { |
950 |
|
set.last(); |
951 |
< |
fail(); |
952 |
< |
} catch (NoSuchElementException e) { |
979 |
< |
// expected |
980 |
< |
} |
951 |
> |
shouldThrow(); |
952 |
> |
} catch (NoSuchElementException success) {} |
953 |
|
} |
954 |
|
} |
955 |
|
|