17 |
|
|
18 |
|
public class TreeSubSetTest extends JSR166TestCase { |
19 |
|
public static void main(String[] args) { |
20 |
< |
junit.textui.TestRunner.run(suite()); |
20 |
> |
main(suite(), args); |
21 |
|
} |
22 |
|
public static Test suite() { |
23 |
|
return new TestSuite(TreeSubSetTest.class); |
31 |
|
|
32 |
|
/** |
33 |
|
* Returns a new set of given size containing consecutive |
34 |
< |
* Integers 0 ... n. |
34 |
> |
* Integers 0 ... n - 1. |
35 |
|
*/ |
36 |
< |
private NavigableSet<Integer> populatedSet(int n) { |
37 |
< |
TreeSet<Integer> q = new TreeSet<Integer>(); |
36 |
> |
private static NavigableSet<Integer> populatedSet(int n) { |
37 |
> |
TreeSet<Integer> q = new TreeSet<>(); |
38 |
|
assertTrue(q.isEmpty()); |
39 |
|
|
40 |
< |
for (int i = n-1; i >= 0; i -= 2) |
40 |
> |
for (int i = n - 1; i >= 0; i -= 2) |
41 |
|
assertTrue(q.add(new Integer(i))); |
42 |
|
for (int i = (n & 1); i < n; i += 2) |
43 |
|
assertTrue(q.add(new Integer(i))); |
52 |
|
/** |
53 |
|
* Returns a new set of first 5 ints. |
54 |
|
*/ |
55 |
< |
private NavigableSet set5() { |
55 |
> |
private static NavigableSet set5() { |
56 |
|
TreeSet q = new TreeSet(); |
57 |
|
assertTrue(q.isEmpty()); |
58 |
|
q.add(one); |
67 |
|
return s; |
68 |
|
} |
69 |
|
|
70 |
< |
private NavigableSet dset5() { |
70 |
> |
private static NavigableSet dset5() { |
71 |
|
TreeSet q = new TreeSet(); |
72 |
|
assertTrue(q.isEmpty()); |
73 |
|
q.add(m1); |
119 |
|
public void testSize() { |
120 |
|
NavigableSet q = populatedSet(SIZE); |
121 |
|
for (int i = 0; i < SIZE; ++i) { |
122 |
< |
assertEquals(SIZE-i, q.size()); |
122 |
> |
assertEquals(SIZE - i, q.size()); |
123 |
|
q.pollFirst(); |
124 |
|
} |
125 |
|
for (int i = 0; i < SIZE; ++i) { |
132 |
|
* add(null) throws NPE |
133 |
|
*/ |
134 |
|
public void testAddNull() { |
135 |
+ |
NavigableSet q = set0(); |
136 |
|
try { |
136 |
– |
NavigableSet q = set0(); |
137 |
|
q.add(null); |
138 |
|
shouldThrow(); |
139 |
|
} catch (NullPointerException success) {} |
160 |
|
* Add of non-Comparable throws CCE |
161 |
|
*/ |
162 |
|
public void testAddNonComparable() { |
163 |
+ |
NavigableSet q = set0(); |
164 |
|
try { |
164 |
– |
NavigableSet q = set0(); |
165 |
– |
q.add(new Object()); |
165 |
|
q.add(new Object()); |
166 |
|
q.add(new Object()); |
167 |
|
shouldThrow(); |
172 |
|
* addAll(null) throws NPE |
173 |
|
*/ |
174 |
|
public void testAddAll1() { |
175 |
+ |
NavigableSet q = set0(); |
176 |
|
try { |
177 |
– |
NavigableSet q = set0(); |
177 |
|
q.addAll(null); |
178 |
|
shouldThrow(); |
179 |
|
} catch (NullPointerException success) {} |
183 |
|
* addAll of a collection with null elements throws NPE |
184 |
|
*/ |
185 |
|
public void testAddAll2() { |
186 |
+ |
NavigableSet q = set0(); |
187 |
+ |
Integer[] ints = new Integer[SIZE]; |
188 |
|
try { |
188 |
– |
NavigableSet q = set0(); |
189 |
– |
Integer[] ints = new Integer[SIZE]; |
189 |
|
q.addAll(Arrays.asList(ints)); |
190 |
|
shouldThrow(); |
191 |
|
} catch (NullPointerException success) {} |
196 |
|
* possibly adding some elements |
197 |
|
*/ |
198 |
|
public void testAddAll3() { |
199 |
+ |
NavigableSet q = set0(); |
200 |
+ |
Integer[] ints = new Integer[SIZE]; |
201 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
202 |
+ |
ints[i] = new Integer(i + SIZE); |
203 |
|
try { |
201 |
– |
NavigableSet q = set0(); |
202 |
– |
Integer[] ints = new Integer[SIZE]; |
203 |
– |
for (int i = 0; i < SIZE-1; ++i) |
204 |
– |
ints[i] = new Integer(i+SIZE); |
204 |
|
q.addAll(Arrays.asList(ints)); |
205 |
|
shouldThrow(); |
206 |
|
} catch (NullPointerException success) {} |
213 |
|
Integer[] empty = new Integer[0]; |
214 |
|
Integer[] ints = new Integer[SIZE]; |
215 |
|
for (int i = 0; i < SIZE; ++i) |
216 |
< |
ints[i] = new Integer(SIZE-1- i); |
216 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
217 |
|
NavigableSet q = set0(); |
218 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
219 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
241 |
|
assertTrue(q.contains(i)); |
242 |
|
assertTrue(q.remove(i)); |
243 |
|
assertFalse(q.contains(i)); |
244 |
< |
assertTrue(q.contains(i-1)); |
244 |
> |
assertTrue(q.contains(i - 1)); |
245 |
|
} |
246 |
|
for (int i = 0; i < SIZE; i += 2) { |
247 |
|
assertTrue(q.contains(i)); |
248 |
|
assertTrue(q.remove(i)); |
249 |
|
assertFalse(q.contains(i)); |
250 |
< |
assertFalse(q.remove(i+1)); |
251 |
< |
assertFalse(q.contains(i+1)); |
250 |
> |
assertFalse(q.remove(i + 1)); |
251 |
> |
assertFalse(q.contains(i + 1)); |
252 |
|
} |
253 |
|
assertTrue(q.isEmpty()); |
254 |
|
} |
307 |
|
assertTrue(changed); |
308 |
|
|
309 |
|
assertTrue(q.containsAll(p)); |
310 |
< |
assertEquals(SIZE-i, q.size()); |
310 |
> |
assertEquals(SIZE - i, q.size()); |
311 |
|
p.pollFirst(); |
312 |
|
} |
313 |
|
} |
320 |
|
NavigableSet q = populatedSet(SIZE); |
321 |
|
NavigableSet p = populatedSet(i); |
322 |
|
assertTrue(q.removeAll(p)); |
323 |
< |
assertEquals(SIZE-i, q.size()); |
323 |
> |
assertEquals(SIZE - i, q.size()); |
324 |
|
for (int j = 0; j < i; ++j) { |
325 |
< |
Integer I = (Integer)(p.pollFirst()); |
326 |
< |
assertFalse(q.contains(I)); |
325 |
> |
Integer x = (Integer)(p.pollFirst()); |
326 |
> |
assertFalse(q.contains(x)); |
327 |
|
} |
328 |
|
} |
329 |
|
} |
405 |
|
*/ |
406 |
|
public void testToArray() { |
407 |
|
NavigableSet q = populatedSet(SIZE); |
408 |
< |
Object[] o = q.toArray(); |
409 |
< |
for (int i = 0; i < o.length; i++) |
410 |
< |
assertSame(o[i], q.pollFirst()); |
408 |
> |
Object[] a = q.toArray(); |
409 |
> |
assertSame(Object[].class, a.getClass()); |
410 |
> |
for (Object o : a) |
411 |
> |
assertSame(o, q.pollFirst()); |
412 |
> |
assertTrue(q.isEmpty()); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
420 |
|
Integer[] ints = new Integer[SIZE]; |
421 |
|
Integer[] array = q.toArray(ints); |
422 |
|
assertSame(ints, array); |
423 |
< |
for (int i = 0; i < ints.length; i++) |
424 |
< |
assertSame(ints[i], q.pollFirst()); |
423 |
> |
for (Integer o : ints) |
424 |
> |
assertSame(o, q.pollFirst()); |
425 |
> |
assertTrue(q.isEmpty()); |
426 |
|
} |
427 |
|
|
428 |
|
/** |
430 |
|
*/ |
431 |
|
public void testIterator() { |
432 |
|
NavigableSet q = populatedSet(SIZE); |
431 |
– |
int i = 0; |
433 |
|
Iterator it = q.iterator(); |
434 |
< |
while (it.hasNext()) { |
434 |
> |
int i; |
435 |
> |
for (i = 0; it.hasNext(); i++) |
436 |
|
assertTrue(q.contains(it.next())); |
435 |
– |
++i; |
436 |
– |
} |
437 |
|
assertEquals(i, SIZE); |
438 |
+ |
assertIteratorExhausted(it); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
442 |
|
* iterator of empty set has no elements |
443 |
|
*/ |
444 |
|
public void testEmptyIterator() { |
445 |
< |
NavigableSet q = set0(); |
445 |
< |
int i = 0; |
446 |
< |
Iterator it = q.iterator(); |
447 |
< |
while (it.hasNext()) { |
448 |
< |
assertTrue(q.contains(it.next())); |
449 |
< |
++i; |
450 |
< |
} |
451 |
< |
assertEquals(0, i); |
445 |
> |
assertIteratorExhausted(set0().iterator()); |
446 |
|
} |
447 |
|
|
448 |
|
/** |
476 |
|
} |
477 |
|
|
478 |
|
/** |
479 |
< |
* A deserialized serialized set has same elements |
479 |
> |
* A deserialized/reserialized set equals original |
480 |
|
*/ |
481 |
|
public void testSerialization() throws Exception { |
482 |
|
NavigableSet x = populatedSet(SIZE); |
618 |
|
public void testDescendingSize() { |
619 |
|
NavigableSet q = populatedSet(SIZE); |
620 |
|
for (int i = 0; i < SIZE; ++i) { |
621 |
< |
assertEquals(SIZE-i, q.size()); |
621 |
> |
assertEquals(SIZE - i, q.size()); |
622 |
|
q.pollFirst(); |
623 |
|
} |
624 |
|
for (int i = 0; i < SIZE; ++i) { |
648 |
|
* Add of non-Comparable throws CCE |
649 |
|
*/ |
650 |
|
public void testDescendingAddNonComparable() { |
651 |
+ |
NavigableSet q = dset0(); |
652 |
|
try { |
658 |
– |
NavigableSet q = dset0(); |
659 |
– |
q.add(new Object()); |
653 |
|
q.add(new Object()); |
654 |
|
q.add(new Object()); |
655 |
|
shouldThrow(); |
660 |
|
* addAll(null) throws NPE |
661 |
|
*/ |
662 |
|
public void testDescendingAddAll1() { |
663 |
+ |
NavigableSet q = dset0(); |
664 |
|
try { |
671 |
– |
NavigableSet q = dset0(); |
665 |
|
q.addAll(null); |
666 |
|
shouldThrow(); |
667 |
|
} catch (NullPointerException success) {} |
671 |
|
* addAll of a collection with null elements throws NPE |
672 |
|
*/ |
673 |
|
public void testDescendingAddAll2() { |
674 |
+ |
NavigableSet q = dset0(); |
675 |
+ |
Integer[] ints = new Integer[SIZE]; |
676 |
|
try { |
682 |
– |
NavigableSet q = dset0(); |
683 |
– |
Integer[] ints = new Integer[SIZE]; |
677 |
|
q.addAll(Arrays.asList(ints)); |
678 |
|
shouldThrow(); |
679 |
|
} catch (NullPointerException success) {} |
684 |
|
* possibly adding some elements |
685 |
|
*/ |
686 |
|
public void testDescendingAddAll3() { |
687 |
+ |
NavigableSet q = dset0(); |
688 |
+ |
Integer[] ints = new Integer[SIZE]; |
689 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
690 |
+ |
ints[i] = new Integer(i + SIZE); |
691 |
|
try { |
695 |
– |
NavigableSet q = dset0(); |
696 |
– |
Integer[] ints = new Integer[SIZE]; |
697 |
– |
for (int i = 0; i < SIZE-1; ++i) |
698 |
– |
ints[i] = new Integer(i+SIZE); |
692 |
|
q.addAll(Arrays.asList(ints)); |
693 |
|
shouldThrow(); |
694 |
|
} catch (NullPointerException success) {} |
701 |
|
Integer[] empty = new Integer[0]; |
702 |
|
Integer[] ints = new Integer[SIZE]; |
703 |
|
for (int i = 0; i < SIZE; ++i) |
704 |
< |
ints[i] = new Integer(SIZE-1- i); |
704 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
705 |
|
NavigableSet q = dset0(); |
706 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
707 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
730 |
|
} |
731 |
|
for (int i = 0; i < SIZE; i += 2) { |
732 |
|
assertTrue(q.remove(new Integer(i))); |
733 |
< |
assertFalse(q.remove(new Integer(i+1))); |
733 |
> |
assertFalse(q.remove(new Integer(i + 1))); |
734 |
|
} |
735 |
|
assertTrue(q.isEmpty()); |
736 |
|
} |
789 |
|
assertTrue(changed); |
790 |
|
|
791 |
|
assertTrue(q.containsAll(p)); |
792 |
< |
assertEquals(SIZE-i, q.size()); |
792 |
> |
assertEquals(SIZE - i, q.size()); |
793 |
|
p.pollFirst(); |
794 |
|
} |
795 |
|
} |
802 |
|
NavigableSet q = populatedSet(SIZE); |
803 |
|
NavigableSet p = populatedSet(i); |
804 |
|
assertTrue(q.removeAll(p)); |
805 |
< |
assertEquals(SIZE-i, q.size()); |
805 |
> |
assertEquals(SIZE - i, q.size()); |
806 |
|
for (int j = 0; j < i; ++j) { |
807 |
< |
Integer I = (Integer)(p.pollFirst()); |
808 |
< |
assertFalse(q.contains(I)); |
807 |
> |
Integer x = (Integer)(p.pollFirst()); |
808 |
> |
assertFalse(q.contains(x)); |
809 |
|
} |
810 |
|
} |
811 |
|
} |
964 |
|
} |
965 |
|
|
966 |
|
/** |
967 |
< |
* A deserialized serialized set has same elements |
967 |
> |
* A deserialized/reserialized set equals original |
968 |
|
*/ |
969 |
|
public void testDescendingSerialization() throws Exception { |
970 |
|
NavigableSet x = dset5(); |