4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import junit.framework.*; |
7 |
|
import java.util.Arrays; |
8 |
|
import java.util.Comparator; |
9 |
|
import java.util.Iterator; |
10 |
|
import java.util.NavigableSet; |
11 |
+ |
import java.util.Set; |
12 |
|
import java.util.SortedSet; |
13 |
|
import java.util.TreeSet; |
14 |
|
|
15 |
+ |
import junit.framework.Test; |
16 |
+ |
import junit.framework.TestSuite; |
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); |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* Create a set of given size containing consecutive |
34 |
< |
* Integers 0 ... n. |
33 |
> |
* Returns a new set of given size containing consecutive |
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) |
42 |
> |
for (int i = (n & 1); i < n; i += 2) |
43 |
|
assertTrue(q.add(new Integer(i))); |
44 |
|
assertTrue(q.add(new Integer(-n))); |
45 |
|
assertTrue(q.add(new Integer(n))); |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Create set of first 5 ints |
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 { |
133 |
– |
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 { |
161 |
– |
NavigableSet q = set0(); |
162 |
– |
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 { |
174 |
– |
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 { |
185 |
– |
NavigableSet q = set0(); |
186 |
– |
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 { |
198 |
– |
NavigableSet q = set0(); |
199 |
– |
Integer[] ints = new Integer[SIZE]; |
200 |
– |
for (int i = 0; i < SIZE-1; ++i) |
201 |
– |
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))); |
237 |
|
*/ |
238 |
|
public void testRemoveElement() { |
239 |
|
NavigableSet q = populatedSet(SIZE); |
240 |
< |
for (int i = 1; i < SIZE; i+=2) { |
240 |
> |
for (int i = 1; i < SIZE; i += 2) { |
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) { |
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 |
|
} |
427 |
|
*/ |
428 |
|
public void testIterator() { |
429 |
|
NavigableSet q = populatedSet(SIZE); |
428 |
– |
int i = 0; |
430 |
|
Iterator it = q.iterator(); |
431 |
< |
while (it.hasNext()) { |
431 |
> |
int i; |
432 |
> |
for (i = 0; it.hasNext(); i++) |
433 |
|
assertTrue(q.contains(it.next())); |
432 |
– |
++i; |
433 |
– |
} |
434 |
|
assertEquals(i, SIZE); |
435 |
+ |
assertIteratorExhausted(it); |
436 |
|
} |
437 |
|
|
438 |
|
/** |
439 |
|
* iterator of empty set has no elements |
440 |
|
*/ |
441 |
|
public void testEmptyIterator() { |
442 |
< |
NavigableSet q = set0(); |
442 |
< |
int i = 0; |
443 |
< |
Iterator it = q.iterator(); |
444 |
< |
while (it.hasNext()) { |
445 |
< |
assertTrue(q.contains(it.next())); |
446 |
< |
++i; |
447 |
< |
} |
448 |
< |
assertEquals(i, 0); |
442 |
> |
assertIteratorExhausted(set0().iterator()); |
443 |
|
} |
444 |
|
|
445 |
|
/** |
456 |
|
it.remove(); |
457 |
|
|
458 |
|
it = q.iterator(); |
459 |
< |
assertEquals(it.next(), 2); |
460 |
< |
assertEquals(it.next(), 3); |
459 |
> |
assertEquals(2, it.next()); |
460 |
> |
assertEquals(3, it.next()); |
461 |
|
assertFalse(it.hasNext()); |
462 |
|
} |
463 |
|
|
473 |
|
} |
474 |
|
|
475 |
|
/** |
476 |
< |
* A deserialized serialized set has same elements |
476 |
> |
* A deserialized/reserialized set equals original |
477 |
|
*/ |
478 |
|
public void testSerialization() throws Exception { |
479 |
|
NavigableSet x = populatedSet(SIZE); |
480 |
|
NavigableSet y = serialClone(x); |
481 |
|
|
482 |
< |
assertTrue(x != y); |
482 |
> |
assertNotSame(x, y); |
483 |
|
assertEquals(x.size(), y.size()); |
484 |
|
assertEquals(x, y); |
485 |
|
assertEquals(y, x); |
615 |
|
public void testDescendingSize() { |
616 |
|
NavigableSet q = populatedSet(SIZE); |
617 |
|
for (int i = 0; i < SIZE; ++i) { |
618 |
< |
assertEquals(SIZE-i, q.size()); |
618 |
> |
assertEquals(SIZE - i, q.size()); |
619 |
|
q.pollFirst(); |
620 |
|
} |
621 |
|
for (int i = 0; i < SIZE; ++i) { |
645 |
|
* Add of non-Comparable throws CCE |
646 |
|
*/ |
647 |
|
public void testDescendingAddNonComparable() { |
648 |
+ |
NavigableSet q = dset0(); |
649 |
|
try { |
655 |
– |
NavigableSet q = dset0(); |
656 |
– |
q.add(new Object()); |
650 |
|
q.add(new Object()); |
651 |
|
q.add(new Object()); |
652 |
|
shouldThrow(); |
657 |
|
* addAll(null) throws NPE |
658 |
|
*/ |
659 |
|
public void testDescendingAddAll1() { |
660 |
+ |
NavigableSet q = dset0(); |
661 |
|
try { |
668 |
– |
NavigableSet q = dset0(); |
662 |
|
q.addAll(null); |
663 |
|
shouldThrow(); |
664 |
|
} catch (NullPointerException success) {} |
668 |
|
* addAll of a collection with null elements throws NPE |
669 |
|
*/ |
670 |
|
public void testDescendingAddAll2() { |
671 |
+ |
NavigableSet q = dset0(); |
672 |
+ |
Integer[] ints = new Integer[SIZE]; |
673 |
|
try { |
679 |
– |
NavigableSet q = dset0(); |
680 |
– |
Integer[] ints = new Integer[SIZE]; |
674 |
|
q.addAll(Arrays.asList(ints)); |
675 |
|
shouldThrow(); |
676 |
|
} catch (NullPointerException success) {} |
681 |
|
* possibly adding some elements |
682 |
|
*/ |
683 |
|
public void testDescendingAddAll3() { |
684 |
+ |
NavigableSet q = dset0(); |
685 |
+ |
Integer[] ints = new Integer[SIZE]; |
686 |
+ |
for (int i = 0; i < SIZE - 1; ++i) |
687 |
+ |
ints[i] = new Integer(i + SIZE); |
688 |
|
try { |
692 |
– |
NavigableSet q = dset0(); |
693 |
– |
Integer[] ints = new Integer[SIZE]; |
694 |
– |
for (int i = 0; i < SIZE-1; ++i) |
695 |
– |
ints[i] = new Integer(i+SIZE); |
689 |
|
q.addAll(Arrays.asList(ints)); |
690 |
|
shouldThrow(); |
691 |
|
} catch (NullPointerException success) {} |
698 |
|
Integer[] empty = new Integer[0]; |
699 |
|
Integer[] ints = new Integer[SIZE]; |
700 |
|
for (int i = 0; i < SIZE; ++i) |
701 |
< |
ints[i] = new Integer(SIZE-1- i); |
701 |
> |
ints[i] = new Integer(SIZE - 1 - i); |
702 |
|
NavigableSet q = dset0(); |
703 |
|
assertFalse(q.addAll(Arrays.asList(empty))); |
704 |
|
assertTrue(q.addAll(Arrays.asList(ints))); |
722 |
|
*/ |
723 |
|
public void testDescendingRemoveElement() { |
724 |
|
NavigableSet q = populatedSet(SIZE); |
725 |
< |
for (int i = 1; i < SIZE; i+=2) { |
725 |
> |
for (int i = 1; i < SIZE; i += 2) { |
726 |
|
assertTrue(q.remove(new Integer(i))); |
727 |
|
} |
728 |
< |
for (int i = 0; i < SIZE; i+=2) { |
728 |
> |
for (int i = 0; i < SIZE; i += 2) { |
729 |
|
assertTrue(q.remove(new Integer(i))); |
730 |
< |
assertFalse(q.remove(new Integer(i+1))); |
730 |
> |
assertFalse(q.remove(new Integer(i + 1))); |
731 |
|
} |
732 |
|
assertTrue(q.isEmpty()); |
733 |
|
} |
786 |
|
assertTrue(changed); |
787 |
|
|
788 |
|
assertTrue(q.containsAll(p)); |
789 |
< |
assertEquals(SIZE-i, q.size()); |
789 |
> |
assertEquals(SIZE - i, q.size()); |
790 |
|
p.pollFirst(); |
791 |
|
} |
792 |
|
} |
799 |
|
NavigableSet q = populatedSet(SIZE); |
800 |
|
NavigableSet p = populatedSet(i); |
801 |
|
assertTrue(q.removeAll(p)); |
802 |
< |
assertEquals(SIZE-i, q.size()); |
802 |
> |
assertEquals(SIZE - i, q.size()); |
803 |
|
for (int j = 0; j < i; ++j) { |
804 |
< |
Integer I = (Integer)(p.pollFirst()); |
805 |
< |
assertFalse(q.contains(I)); |
804 |
> |
Integer x = (Integer)(p.pollFirst()); |
805 |
> |
assertFalse(q.contains(x)); |
806 |
|
} |
807 |
|
} |
808 |
|
} |
927 |
|
assertTrue(q.contains(it.next())); |
928 |
|
++i; |
929 |
|
} |
930 |
< |
assertEquals(i, 0); |
930 |
> |
assertEquals(0, i); |
931 |
|
} |
932 |
|
|
933 |
|
/** |
944 |
|
it.remove(); |
945 |
|
|
946 |
|
it = q.iterator(); |
947 |
< |
assertEquals(it.next(), 2); |
948 |
< |
assertEquals(it.next(), 3); |
947 |
> |
assertEquals(2, it.next()); |
948 |
> |
assertEquals(3, it.next()); |
949 |
|
assertFalse(it.hasNext()); |
950 |
|
} |
951 |
|
|
961 |
|
} |
962 |
|
|
963 |
|
/** |
964 |
< |
* A deserialized serialized set has same elements |
964 |
> |
* A deserialized/reserialized set equals original |
965 |
|
*/ |
966 |
|
public void testDescendingSerialization() throws Exception { |
967 |
|
NavigableSet x = dset5(); |
968 |
|
NavigableSet y = serialClone(x); |
969 |
|
|
970 |
< |
assertTrue(x != y); |
970 |
> |
assertNotSame(x, y); |
971 |
|
assertEquals(x.size(), y.size()); |
972 |
|
assertEquals(x.toString(), y.toString()); |
973 |
|
assertEquals(x, y); |
1098 |
|
assertEquals(4, set.size()); |
1099 |
|
} |
1100 |
|
|
1101 |
+ |
/** |
1102 |
+ |
* addAll is idempotent |
1103 |
+ |
*/ |
1104 |
+ |
public void testAddAll_idempotent() throws Exception { |
1105 |
+ |
Set x = populatedSet(SIZE); |
1106 |
+ |
Set y = new TreeSet(x); |
1107 |
+ |
y.addAll(x); |
1108 |
+ |
assertEquals(x, y); |
1109 |
+ |
assertEquals(y, x); |
1110 |
+ |
} |
1111 |
+ |
|
1112 |
|
} |