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.*; |
8 |
< |
import java.util.*; |
9 |
< |
import java.util.concurrent.*; |
10 |
< |
import java.io.*; |
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); |
99 |
|
assertEquals(0, set0().size()); |
100 |
|
} |
101 |
|
|
96 |
– |
|
102 |
|
/** |
103 |
|
* isEmpty is true before add, false after |
104 |
|
*/ |
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 { |
131 |
– |
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 { |
159 |
– |
NavigableSet q = set0(); |
160 |
– |
q.add(new Object()); |
165 |
|
q.add(new Object()); |
166 |
|
q.add(new Object()); |
167 |
|
shouldThrow(); |
168 |
|
} catch (ClassCastException success) {} |
169 |
|
} |
170 |
|
|
167 |
– |
|
171 |
|
/** |
172 |
|
* addAll(null) throws NPE |
173 |
|
*/ |
174 |
|
public void testAddAll1() { |
175 |
+ |
NavigableSet q = set0(); |
176 |
|
try { |
173 |
– |
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 { |
184 |
– |
NavigableSet q = set0(); |
185 |
– |
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 { |
197 |
– |
NavigableSet q = set0(); |
198 |
– |
Integer[] ints = new Integer[SIZE]; |
199 |
– |
for (int i = 0; i < SIZE-1; ++i) |
200 |
– |
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 |
|
} |
330 |
|
|
328 |
– |
|
329 |
– |
|
331 |
|
/** |
332 |
|
* lower returns preceding element |
333 |
|
*/ |
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); |
429 |
– |
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())); |
433 |
– |
++i; |
434 |
– |
} |
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(); |
443 |
< |
int i = 0; |
444 |
< |
Iterator it = q.iterator(); |
445 |
< |
while (it.hasNext()) { |
446 |
< |
assertTrue(q.contains(it.next())); |
447 |
< |
++i; |
448 |
< |
} |
449 |
< |
assertEquals(i, 0); |
445 |
> |
assertIteratorExhausted(set0().iterator()); |
446 |
|
} |
447 |
|
|
448 |
|
/** |
459 |
|
it.remove(); |
460 |
|
|
461 |
|
it = q.iterator(); |
462 |
< |
assertEquals(it.next(), 2); |
463 |
< |
assertEquals(it.next(), 3); |
462 |
> |
assertEquals(2, it.next()); |
463 |
> |
assertEquals(3, it.next()); |
464 |
|
assertFalse(it.hasNext()); |
465 |
|
} |
466 |
|
|
471 |
– |
|
467 |
|
/** |
468 |
|
* toString contains toStrings of elements |
469 |
|
*/ |
471 |
|
NavigableSet q = populatedSet(SIZE); |
472 |
|
String s = q.toString(); |
473 |
|
for (int i = 0; i < SIZE; ++i) { |
474 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
474 |
> |
assertTrue(s.contains(String.valueOf(i))); |
475 |
|
} |
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 q = populatedSet(SIZE); |
483 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
484 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
485 |
< |
out.writeObject(q); |
486 |
< |
out.close(); |
487 |
< |
|
488 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
489 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
490 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
491 |
< |
assertEquals(q.size(), r.size()); |
492 |
< |
while (!q.isEmpty()) |
493 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
482 |
> |
NavigableSet x = populatedSet(SIZE); |
483 |
> |
NavigableSet y = serialClone(x); |
484 |
> |
|
485 |
> |
assertNotSame(x, y); |
486 |
> |
assertEquals(x.size(), y.size()); |
487 |
> |
assertEquals(x, y); |
488 |
> |
assertEquals(y, x); |
489 |
> |
while (!x.isEmpty()) { |
490 |
> |
assertFalse(y.isEmpty()); |
491 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
492 |
> |
} |
493 |
> |
assertTrue(y.isEmpty()); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
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 { |
657 |
– |
NavigableSet q = dset0(); |
658 |
– |
q.add(new Object()); |
653 |
|
q.add(new Object()); |
654 |
|
q.add(new Object()); |
655 |
|
shouldThrow(); |
656 |
|
} catch (ClassCastException success) {} |
657 |
|
} |
658 |
|
|
665 |
– |
|
659 |
|
/** |
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))); |
725 |
|
*/ |
726 |
|
public void testDescendingRemoveElement() { |
727 |
|
NavigableSet q = populatedSet(SIZE); |
728 |
< |
for (int i = 1; i < SIZE; i+=2) { |
728 |
> |
for (int i = 1; i < SIZE; i += 2) { |
729 |
|
assertTrue(q.remove(new Integer(i))); |
730 |
|
} |
731 |
< |
for (int i = 0; i < SIZE; i+=2) { |
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 |
|
} |
812 |
|
|
820 |
– |
|
821 |
– |
|
813 |
|
/** |
814 |
|
* lower returns preceding element |
815 |
|
*/ |
930 |
|
assertTrue(q.contains(it.next())); |
931 |
|
++i; |
932 |
|
} |
933 |
< |
assertEquals(i, 0); |
933 |
> |
assertEquals(0, i); |
934 |
|
} |
935 |
|
|
936 |
|
/** |
947 |
|
it.remove(); |
948 |
|
|
949 |
|
it = q.iterator(); |
950 |
< |
assertEquals(it.next(), 2); |
951 |
< |
assertEquals(it.next(), 3); |
950 |
> |
assertEquals(2, it.next()); |
951 |
> |
assertEquals(3, it.next()); |
952 |
|
assertFalse(it.hasNext()); |
953 |
|
} |
954 |
|
|
964 |
– |
|
955 |
|
/** |
956 |
|
* toString contains toStrings of elements |
957 |
|
*/ |
959 |
|
NavigableSet q = populatedSet(SIZE); |
960 |
|
String s = q.toString(); |
961 |
|
for (int i = 0; i < SIZE; ++i) { |
962 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
962 |
> |
assertTrue(s.contains(String.valueOf(i))); |
963 |
|
} |
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 q = populatedSet(SIZE); |
971 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
972 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
973 |
< |
out.writeObject(q); |
974 |
< |
out.close(); |
975 |
< |
|
976 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
977 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
978 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
979 |
< |
assertEquals(q.size(), r.size()); |
980 |
< |
while (!q.isEmpty()) |
981 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
970 |
> |
NavigableSet x = dset5(); |
971 |
> |
NavigableSet y = serialClone(x); |
972 |
> |
|
973 |
> |
assertNotSame(x, y); |
974 |
> |
assertEquals(x.size(), y.size()); |
975 |
> |
assertEquals(x.toString(), y.toString()); |
976 |
> |
assertEquals(x, y); |
977 |
> |
assertEquals(y, x); |
978 |
> |
while (!x.isEmpty()) { |
979 |
> |
assertFalse(y.isEmpty()); |
980 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
981 |
> |
} |
982 |
> |
assertTrue(y.isEmpty()); |
983 |
|
} |
984 |
|
|
985 |
|
/** |
1101 |
|
assertEquals(4, set.size()); |
1102 |
|
} |
1103 |
|
|
1104 |
+ |
/** |
1105 |
+ |
* addAll is idempotent |
1106 |
+ |
*/ |
1107 |
+ |
public void testAddAll_idempotent() throws Exception { |
1108 |
+ |
Set x = populatedSet(SIZE); |
1109 |
+ |
Set y = new TreeSet(x); |
1110 |
+ |
y.addAll(x); |
1111 |
+ |
assertEquals(x, y); |
1112 |
+ |
assertEquals(y, x); |
1113 |
+ |
} |
1114 |
+ |
|
1115 |
|
} |