1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
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); |
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) |
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()); |
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 |
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()); |
230 |
|
} |
235 |
|
public void testRemoveElement() { |
236 |
|
NavigableSet q = populatedSet(SIZE); |
237 |
|
for (int i = 1; i < SIZE; i+=2) { |
238 |
< |
assertTrue(q.remove(new Integer(i))); |
238 |
> |
assertTrue(q.contains(i)); |
239 |
> |
assertTrue(q.remove(i)); |
240 |
> |
assertFalse(q.contains(i)); |
241 |
> |
assertTrue(q.contains(i-1)); |
242 |
|
} |
243 |
|
for (int i = 0; i < SIZE; i+=2) { |
244 |
< |
assertTrue(q.remove(new Integer(i))); |
245 |
< |
assertFalse(q.remove(new Integer(i+1))); |
244 |
> |
assertTrue(q.contains(i)); |
245 |
> |
assertTrue(q.remove(i)); |
246 |
> |
assertFalse(q.contains(i)); |
247 |
> |
assertFalse(q.remove(i+1)); |
248 |
> |
assertFalse(q.contains(i+1)); |
249 |
|
} |
250 |
|
assertTrue(q.isEmpty()); |
251 |
|
} |
270 |
|
q.clear(); |
271 |
|
assertTrue(q.isEmpty()); |
272 |
|
assertEquals(0, q.size()); |
273 |
< |
q.add(new Integer(1)); |
273 |
> |
assertTrue(q.add(new Integer(1))); |
274 |
|
assertFalse(q.isEmpty()); |
275 |
|
q.clear(); |
276 |
|
assertTrue(q.isEmpty()); |
343 |
|
|
344 |
|
Object e4 = q.lower(zero); |
345 |
|
assertNull(e4); |
342 |
– |
|
346 |
|
} |
347 |
|
|
348 |
|
/** |
361 |
|
|
362 |
|
Object e4 = q.higher(six); |
363 |
|
assertNull(e4); |
361 |
– |
|
364 |
|
} |
365 |
|
|
366 |
|
/** |
379 |
|
|
380 |
|
Object e4 = q.floor(zero); |
381 |
|
assertNull(e4); |
380 |
– |
|
382 |
|
} |
383 |
|
|
384 |
|
/** |
397 |
|
|
398 |
|
Object e4 = q.ceiling(six); |
399 |
|
assertNull(e4); |
399 |
– |
|
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* toArray contains all elements |
403 |
> |
* toArray contains all elements in sorted order |
404 |
|
*/ |
405 |
|
public void testToArray() { |
406 |
|
NavigableSet q = populatedSet(SIZE); |
407 |
|
Object[] o = q.toArray(); |
408 |
– |
Arrays.sort(o); |
408 |
|
for (int i = 0; i < o.length; i++) |
409 |
< |
assertEquals(o[i], q.pollFirst()); |
409 |
> |
assertSame(o[i], q.pollFirst()); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* toArray(a) contains all elements |
413 |
> |
* toArray(a) contains all elements in sorted order |
414 |
|
*/ |
415 |
|
public void testToArray2() { |
416 |
< |
NavigableSet q = populatedSet(SIZE); |
416 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
417 |
|
Integer[] ints = new Integer[SIZE]; |
418 |
< |
ints = (Integer[])q.toArray(ints); |
419 |
< |
Arrays.sort(ints); |
418 |
> |
Integer[] array = q.toArray(ints); |
419 |
> |
assertSame(ints, array); |
420 |
|
for (int i = 0; i < ints.length; i++) |
421 |
< |
assertEquals(ints[i], q.pollFirst()); |
421 |
> |
assertSame(ints[i], q.pollFirst()); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
452 |
|
/** |
453 |
|
* iterator.remove removes current element |
454 |
|
*/ |
455 |
< |
public void testIteratorRemove () { |
455 |
> |
public void testIteratorRemove() { |
456 |
|
final NavigableSet q = set0(); |
457 |
|
q.add(new Integer(2)); |
458 |
|
q.add(new Integer(1)); |
463 |
|
it.remove(); |
464 |
|
|
465 |
|
it = q.iterator(); |
466 |
< |
assertEquals(it.next(), new Integer(2)); |
467 |
< |
assertEquals(it.next(), new Integer(3)); |
466 |
> |
assertEquals(it.next(), 2); |
467 |
> |
assertEquals(it.next(), 3); |
468 |
|
assertFalse(it.hasNext()); |
469 |
|
} |
470 |
|
|
673 |
|
shouldThrow(); |
674 |
|
} catch (NullPointerException success) {} |
675 |
|
} |
676 |
+ |
|
677 |
|
/** |
678 |
|
* addAll of a collection with null elements throws NPE |
679 |
|
*/ |
685 |
|
shouldThrow(); |
686 |
|
} catch (NullPointerException success) {} |
687 |
|
} |
688 |
+ |
|
689 |
|
/** |
690 |
|
* addAll of a collection with any null elements throws NPE after |
691 |
|
* possibly adding some elements |
722 |
|
public void testDescendingPoll() { |
723 |
|
NavigableSet q = populatedSet(SIZE); |
724 |
|
for (int i = 0; i < SIZE; ++i) { |
725 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
725 |
> |
assertEquals(i, q.pollFirst()); |
726 |
|
} |
727 |
|
assertNull(q.pollFirst()); |
728 |
|
} |
762 |
|
q.clear(); |
763 |
|
assertTrue(q.isEmpty()); |
764 |
|
assertEquals(0, q.size()); |
765 |
< |
q.add(new Integer(1)); |
765 |
> |
assertTrue(q.add(new Integer(1))); |
766 |
|
assertFalse(q.isEmpty()); |
767 |
|
q.clear(); |
768 |
|
assertTrue(q.isEmpty()); |
835 |
|
|
836 |
|
Object e4 = q.lower(zero); |
837 |
|
assertNull(e4); |
837 |
– |
|
838 |
|
} |
839 |
|
|
840 |
|
/** |
853 |
|
|
854 |
|
Object e4 = q.higher(m6); |
855 |
|
assertNull(e4); |
856 |
– |
|
856 |
|
} |
857 |
|
|
858 |
|
/** |
871 |
|
|
872 |
|
Object e4 = q.floor(zero); |
873 |
|
assertNull(e4); |
875 |
– |
|
874 |
|
} |
875 |
|
|
876 |
|
/** |
889 |
|
|
890 |
|
Object e4 = q.ceiling(m6); |
891 |
|
assertNull(e4); |
894 |
– |
|
892 |
|
} |
893 |
|
|
894 |
|
/** |
908 |
|
public void testDescendingToArray2() { |
909 |
|
NavigableSet q = populatedSet(SIZE); |
910 |
|
Integer[] ints = new Integer[SIZE]; |
911 |
< |
ints = (Integer[])q.toArray(ints); |
911 |
> |
assertSame(ints, q.toArray(ints)); |
912 |
|
Arrays.sort(ints); |
913 |
|
for (int i = 0; i < ints.length; i++) |
914 |
|
assertEquals(ints[i], q.pollFirst()); |
945 |
|
/** |
946 |
|
* iterator.remove removes current element |
947 |
|
*/ |
948 |
< |
public void testDescendingIteratorRemove () { |
948 |
> |
public void testDescendingIteratorRemove() { |
949 |
|
final NavigableSet q = dset0(); |
950 |
|
q.add(new Integer(2)); |
951 |
|
q.add(new Integer(1)); |
956 |
|
it.remove(); |
957 |
|
|
958 |
|
it = q.iterator(); |
959 |
< |
assertEquals(it.next(), new Integer(2)); |
960 |
< |
assertEquals(it.next(), new Integer(3)); |
959 |
> |
assertEquals(it.next(), 2); |
960 |
> |
assertEquals(it.next(), 3); |
961 |
|
assertFalse(it.hasNext()); |
962 |
|
} |
963 |
|
|