11 |
|
|
12 |
|
public class TreeSubSetTest 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(TreeSubSetTest.class); |
17 |
> |
return new TestSuite(TreeSubSetTest.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 |
|
|
27 |
|
* Create a set of given size containing consecutive |
28 |
|
* Integers 0 ... n. |
29 |
|
*/ |
30 |
< |
private NavigableSet populatedSet(int n) { |
31 |
< |
TreeSet q = new TreeSet(); |
30 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
31 |
> |
TreeSet<Integer> q = new TreeSet<Integer>(); |
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 |
|
|
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 |
|
|
70 |
|
q.add(m4); |
71 |
|
q.add(m5); |
72 |
|
NavigableSet s = q.descendingSet(); |
73 |
< |
assertEquals(5, s.size()); |
73 |
> |
assertEquals(5, s.size()); |
74 |
|
return s; |
75 |
|
} |
76 |
|
|
77 |
|
private static NavigableSet set0() { |
78 |
< |
TreeSet set = new TreeSet(); |
78 |
> |
TreeSet set = new TreeSet(); |
79 |
|
assertTrue(set.isEmpty()); |
80 |
|
return set.tailSet(m1, false); |
81 |
|
} |
82 |
|
|
83 |
|
private static NavigableSet dset0() { |
84 |
< |
TreeSet set = new TreeSet(); |
84 |
> |
TreeSet set = new TreeSet(); |
85 |
|
assertTrue(set.isEmpty()); |
86 |
|
return set; |
87 |
|
} |
100 |
|
public void testEmpty() { |
101 |
|
NavigableSet q = set0(); |
102 |
|
assertTrue(q.isEmpty()); |
103 |
< |
q.add(new Integer(1)); |
103 |
> |
assertTrue(q.add(new Integer(1))); |
104 |
|
assertFalse(q.isEmpty()); |
105 |
< |
q.add(new Integer(2)); |
105 |
> |
assertTrue(q.add(new Integer(2))); |
106 |
|
q.pollFirst(); |
107 |
|
q.pollFirst(); |
108 |
|
assertTrue(q.isEmpty()); |
127 |
|
* add(null) throws NPE |
128 |
|
*/ |
129 |
|
public void testAddNull() { |
130 |
< |
try { |
130 |
> |
try { |
131 |
|
NavigableSet q = set0(); |
132 |
|
q.add(null); |
133 |
|
shouldThrow(); |
175 |
|
shouldThrow(); |
176 |
|
} catch (NullPointerException success) {} |
177 |
|
} |
178 |
+ |
|
179 |
|
/** |
180 |
|
* addAll of a collection with null elements throws NPE |
181 |
|
*/ |
187 |
|
shouldThrow(); |
188 |
|
} catch (NullPointerException success) {} |
189 |
|
} |
190 |
+ |
|
191 |
|
/** |
192 |
|
* addAll of a collection with any null elements throws NPE after |
193 |
|
* possibly adding some elements |
207 |
|
* Set contains all elements of successful addAll |
208 |
|
*/ |
209 |
|
public void testAddAll5() { |
210 |
< |
try { |
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) |
221 |
< |
assertEquals(new Integer(i), q.pollFirst()); |
222 |
< |
} |
223 |
< |
finally {} |
210 |
> |
Integer[] empty = new Integer[0]; |
211 |
> |
Integer[] ints = new Integer[SIZE]; |
212 |
> |
for (int i = 0; i < SIZE; ++i) |
213 |
> |
ints[i] = new Integer(SIZE-1- i); |
214 |
> |
NavigableSet q = set0(); |
215 |
> |
assertFalse(q.addAll(Arrays.asList(empty))); |
216 |
> |
assertTrue(q.addAll(Arrays.asList(ints))); |
217 |
> |
for (int i = 0; i < SIZE; ++i) |
218 |
> |
assertEquals(new Integer(i), q.pollFirst()); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
224 |
|
public void testPoll() { |
225 |
|
NavigableSet q = populatedSet(SIZE); |
226 |
|
for (int i = 0; i < SIZE; ++i) { |
227 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
227 |
> |
assertEquals(i, q.pollFirst()); |
228 |
|
} |
229 |
< |
assertNull(q.pollFirst()); |
229 |
> |
assertNull(q.pollFirst()); |
230 |
|
} |
231 |
|
|
232 |
|
/** |
264 |
|
q.clear(); |
265 |
|
assertTrue(q.isEmpty()); |
266 |
|
assertEquals(0, q.size()); |
267 |
< |
q.add(new Integer(1)); |
267 |
> |
assertTrue(q.add(new Integer(1))); |
268 |
|
assertFalse(q.isEmpty()); |
269 |
|
q.clear(); |
270 |
|
assertTrue(q.isEmpty()); |
337 |
|
|
338 |
|
Object e4 = q.lower(zero); |
339 |
|
assertNull(e4); |
345 |
– |
|
340 |
|
} |
341 |
|
|
342 |
|
/** |
355 |
|
|
356 |
|
Object e4 = q.higher(six); |
357 |
|
assertNull(e4); |
364 |
– |
|
358 |
|
} |
359 |
|
|
360 |
|
/** |
373 |
|
|
374 |
|
Object e4 = q.floor(zero); |
375 |
|
assertNull(e4); |
383 |
– |
|
376 |
|
} |
377 |
|
|
378 |
|
/** |
391 |
|
|
392 |
|
Object e4 = q.ceiling(six); |
393 |
|
assertNull(e4); |
402 |
– |
|
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
< |
* toArray contains all elements |
397 |
> |
* toArray contains all elements in sorted order |
398 |
|
*/ |
399 |
|
public void testToArray() { |
400 |
|
NavigableSet q = populatedSet(SIZE); |
401 |
< |
Object[] o = q.toArray(); |
402 |
< |
Arrays.sort(o); |
403 |
< |
for (int i = 0; i < o.length; i++) |
413 |
< |
assertEquals(o[i], q.pollFirst()); |
401 |
> |
Object[] o = q.toArray(); |
402 |
> |
for (int i = 0; i < o.length; i++) |
403 |
> |
assertSame(o[i], q.pollFirst()); |
404 |
|
} |
405 |
|
|
406 |
|
/** |
407 |
< |
* toArray(a) contains all elements |
407 |
> |
* toArray(a) contains all elements in sorted order |
408 |
|
*/ |
409 |
|
public void testToArray2() { |
410 |
< |
NavigableSet q = populatedSet(SIZE); |
411 |
< |
Integer[] ints = new Integer[SIZE]; |
412 |
< |
ints = (Integer[])q.toArray(ints); |
413 |
< |
Arrays.sort(ints); |
410 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
411 |
> |
Integer[] ints = new Integer[SIZE]; |
412 |
> |
Integer[] array = q.toArray(ints); |
413 |
> |
assertSame(ints, array); |
414 |
|
for (int i = 0; i < ints.length; i++) |
415 |
< |
assertEquals(ints[i], q.pollFirst()); |
415 |
> |
assertSame(ints[i], q.pollFirst()); |
416 |
|
} |
417 |
|
|
418 |
|
/** |
421 |
|
public void testIterator() { |
422 |
|
NavigableSet q = populatedSet(SIZE); |
423 |
|
int i = 0; |
424 |
< |
Iterator it = q.iterator(); |
424 |
> |
Iterator it = q.iterator(); |
425 |
|
while (it.hasNext()) { |
426 |
|
assertTrue(q.contains(it.next())); |
427 |
|
++i; |
435 |
|
public void testEmptyIterator() { |
436 |
|
NavigableSet q = set0(); |
437 |
|
int i = 0; |
438 |
< |
Iterator it = q.iterator(); |
438 |
> |
Iterator it = q.iterator(); |
439 |
|
while (it.hasNext()) { |
440 |
|
assertTrue(q.contains(it.next())); |
441 |
|
++i; |
446 |
|
/** |
447 |
|
* iterator.remove removes current element |
448 |
|
*/ |
449 |
< |
public void testIteratorRemove () { |
449 |
> |
public void testIteratorRemove() { |
450 |
|
final NavigableSet q = set0(); |
451 |
|
q.add(new Integer(2)); |
452 |
|
q.add(new Integer(1)); |
457 |
|
it.remove(); |
458 |
|
|
459 |
|
it = q.iterator(); |
460 |
< |
assertEquals(it.next(), new Integer(2)); |
461 |
< |
assertEquals(it.next(), new Integer(3)); |
460 |
> |
assertEquals(it.next(), 2); |
461 |
> |
assertEquals(it.next(), 3); |
462 |
|
assertFalse(it.hasNext()); |
463 |
|
} |
464 |
|
|
667 |
|
shouldThrow(); |
668 |
|
} catch (NullPointerException success) {} |
669 |
|
} |
670 |
+ |
|
671 |
|
/** |
672 |
|
* addAll of a collection with null elements throws NPE |
673 |
|
*/ |
679 |
|
shouldThrow(); |
680 |
|
} catch (NullPointerException success) {} |
681 |
|
} |
682 |
+ |
|
683 |
|
/** |
684 |
|
* addAll of a collection with any null elements throws NPE after |
685 |
|
* possibly adding some elements |
716 |
|
public void testDescendingPoll() { |
717 |
|
NavigableSet q = populatedSet(SIZE); |
718 |
|
for (int i = 0; i < SIZE; ++i) { |
719 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
719 |
> |
assertEquals(i, q.pollFirst()); |
720 |
|
} |
721 |
< |
assertNull(q.pollFirst()); |
721 |
> |
assertNull(q.pollFirst()); |
722 |
|
} |
723 |
|
|
724 |
|
/** |
756 |
|
q.clear(); |
757 |
|
assertTrue(q.isEmpty()); |
758 |
|
assertEquals(0, q.size()); |
759 |
< |
q.add(new Integer(1)); |
759 |
> |
assertTrue(q.add(new Integer(1))); |
760 |
|
assertFalse(q.isEmpty()); |
761 |
|
q.clear(); |
762 |
|
assertTrue(q.isEmpty()); |
829 |
|
|
830 |
|
Object e4 = q.lower(zero); |
831 |
|
assertNull(e4); |
840 |
– |
|
832 |
|
} |
833 |
|
|
834 |
|
/** |
847 |
|
|
848 |
|
Object e4 = q.higher(m6); |
849 |
|
assertNull(e4); |
859 |
– |
|
850 |
|
} |
851 |
|
|
852 |
|
/** |
865 |
|
|
866 |
|
Object e4 = q.floor(zero); |
867 |
|
assertNull(e4); |
878 |
– |
|
868 |
|
} |
869 |
|
|
870 |
|
/** |
883 |
|
|
884 |
|
Object e4 = q.ceiling(m6); |
885 |
|
assertNull(e4); |
897 |
– |
|
886 |
|
} |
887 |
|
|
888 |
|
/** |
890 |
|
*/ |
891 |
|
public void testDescendingToArray() { |
892 |
|
NavigableSet q = populatedSet(SIZE); |
893 |
< |
Object[] o = q.toArray(); |
893 |
> |
Object[] o = q.toArray(); |
894 |
|
Arrays.sort(o); |
895 |
< |
for (int i = 0; i < o.length; i++) |
896 |
< |
assertEquals(o[i], q.pollFirst()); |
895 |
> |
for (int i = 0; i < o.length; i++) |
896 |
> |
assertEquals(o[i], q.pollFirst()); |
897 |
|
} |
898 |
|
|
899 |
|
/** |
901 |
|
*/ |
902 |
|
public void testDescendingToArray2() { |
903 |
|
NavigableSet q = populatedSet(SIZE); |
904 |
< |
Integer[] ints = new Integer[SIZE]; |
905 |
< |
ints = (Integer[])q.toArray(ints); |
904 |
> |
Integer[] ints = new Integer[SIZE]; |
905 |
> |
assertSame(ints, q.toArray(ints)); |
906 |
|
Arrays.sort(ints); |
907 |
|
for (int i = 0; i < ints.length; i++) |
908 |
|
assertEquals(ints[i], q.pollFirst()); |
914 |
|
public void testDescendingIterator() { |
915 |
|
NavigableSet q = populatedSet(SIZE); |
916 |
|
int i = 0; |
917 |
< |
Iterator it = q.iterator(); |
917 |
> |
Iterator it = q.iterator(); |
918 |
|
while (it.hasNext()) { |
919 |
|
assertTrue(q.contains(it.next())); |
920 |
|
++i; |
928 |
|
public void testDescendingEmptyIterator() { |
929 |
|
NavigableSet q = dset0(); |
930 |
|
int i = 0; |
931 |
< |
Iterator it = q.iterator(); |
931 |
> |
Iterator it = q.iterator(); |
932 |
|
while (it.hasNext()) { |
933 |
|
assertTrue(q.contains(it.next())); |
934 |
|
++i; |
939 |
|
/** |
940 |
|
* iterator.remove removes current element |
941 |
|
*/ |
942 |
< |
public void testDescendingIteratorRemove () { |
942 |
> |
public void testDescendingIteratorRemove() { |
943 |
|
final NavigableSet q = dset0(); |
944 |
|
q.add(new Integer(2)); |
945 |
|
q.add(new Integer(1)); |
950 |
|
it.remove(); |
951 |
|
|
952 |
|
it = q.iterator(); |
953 |
< |
assertEquals(it.next(), new Integer(2)); |
954 |
< |
assertEquals(it.next(), new Integer(3)); |
953 |
> |
assertEquals(it.next(), 2); |
954 |
> |
assertEquals(it.next(), 3); |
955 |
|
assertFalse(it.hasNext()); |
956 |
|
} |
957 |
|
|