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>(); |
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() { |
56 |
|
TreeSet q = new TreeSet(); |
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 |
|
*/ |
427 |
|
*/ |
428 |
|
public void testIterator() { |
429 |
|
NavigableSet q = populatedSet(SIZE); |
429 |
– |
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())); |
433 |
– |
++i; |
434 |
– |
} |
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(); |
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); |
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 |
|
|
471 |
– |
|
464 |
|
/** |
465 |
|
* toString contains toStrings of elements |
466 |
|
*/ |
476 |
|
* A deserialized serialized set has same elements |
477 |
|
*/ |
478 |
|
public void testSerialization() throws Exception { |
479 |
< |
NavigableSet q = populatedSet(SIZE); |
480 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
481 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
482 |
< |
out.writeObject(q); |
483 |
< |
out.close(); |
484 |
< |
|
485 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
486 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
487 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
488 |
< |
assertEquals(q.size(), r.size()); |
489 |
< |
while (!q.isEmpty()) |
490 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
479 |
> |
NavigableSet x = populatedSet(SIZE); |
480 |
> |
NavigableSet y = serialClone(x); |
481 |
> |
|
482 |
> |
assertNotSame(x, y); |
483 |
> |
assertEquals(x.size(), y.size()); |
484 |
> |
assertEquals(x, y); |
485 |
> |
assertEquals(y, x); |
486 |
> |
while (!x.isEmpty()) { |
487 |
> |
assertFalse(y.isEmpty()); |
488 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
489 |
> |
} |
490 |
> |
assertTrue(y.isEmpty()); |
491 |
|
} |
492 |
|
|
493 |
|
/** |
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 { |
657 |
– |
NavigableSet q = dset0(); |
658 |
– |
q.add(new Object()); |
650 |
|
q.add(new Object()); |
651 |
|
q.add(new Object()); |
652 |
|
shouldThrow(); |
653 |
|
} catch (ClassCastException success) {} |
654 |
|
} |
655 |
|
|
665 |
– |
|
656 |
|
/** |
657 |
|
* addAll(null) throws NPE |
658 |
|
*/ |
659 |
|
public void testDescendingAddAll1() { |
660 |
+ |
NavigableSet q = dset0(); |
661 |
|
try { |
671 |
– |
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 { |
682 |
– |
NavigableSet q = dset0(); |
683 |
– |
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 { |
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); |
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 |
|
} |
809 |
|
|
820 |
– |
|
821 |
– |
|
810 |
|
/** |
811 |
|
* lower returns preceding element |
812 |
|
*/ |
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 |
|
|
964 |
– |
|
952 |
|
/** |
953 |
|
* toString contains toStrings of elements |
954 |
|
*/ |
964 |
|
* A deserialized serialized set has same elements |
965 |
|
*/ |
966 |
|
public void testDescendingSerialization() throws Exception { |
967 |
< |
NavigableSet q = populatedSet(SIZE); |
968 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
969 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
970 |
< |
out.writeObject(q); |
971 |
< |
out.close(); |
972 |
< |
|
973 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
974 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
975 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
976 |
< |
assertEquals(q.size(), r.size()); |
977 |
< |
while (!q.isEmpty()) |
978 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
967 |
> |
NavigableSet x = dset5(); |
968 |
> |
NavigableSet y = serialClone(x); |
969 |
> |
|
970 |
> |
assertNotSame(x, y); |
971 |
> |
assertEquals(x.size(), y.size()); |
972 |
> |
assertEquals(x.toString(), y.toString()); |
973 |
> |
assertEquals(x, y); |
974 |
> |
assertEquals(y, x); |
975 |
> |
while (!x.isEmpty()) { |
976 |
> |
assertFalse(y.isEmpty()); |
977 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
978 |
> |
} |
979 |
> |
assertTrue(y.isEmpty()); |
980 |
|
} |
981 |
|
|
982 |
|
/** |
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 |
|
} |