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) { |
30 |
|
} |
31 |
|
|
32 |
|
/** |
33 |
< |
* Create a set of given size containing consecutive |
33 |
> |
* Returns a new set of given size containing consecutive |
34 |
|
* Integers 0 ... n. |
35 |
|
*/ |
36 |
< |
private NavigableSet populatedSet(int n) { |
37 |
< |
TreeSet q = new TreeSet(); |
36 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
37 |
> |
TreeSet<Integer> q = new TreeSet<Integer>(); |
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 |
|
*/ |
169 |
|
} catch (ClassCastException success) {} |
170 |
|
} |
171 |
|
|
167 |
– |
|
172 |
|
/** |
173 |
|
* addAll(null) throws NPE |
174 |
|
*/ |
238 |
|
*/ |
239 |
|
public void testRemoveElement() { |
240 |
|
NavigableSet q = populatedSet(SIZE); |
241 |
< |
for (int i = 1; i < SIZE; i+=2) { |
242 |
< |
assertTrue(q.remove(new Integer(i))); |
243 |
< |
} |
244 |
< |
for (int i = 0; i < SIZE; i+=2) { |
245 |
< |
assertTrue(q.remove(new Integer(i))); |
246 |
< |
assertFalse(q.remove(new Integer(i+1))); |
241 |
> |
for (int i = 1; i < SIZE; i += 2) { |
242 |
> |
assertTrue(q.contains(i)); |
243 |
> |
assertTrue(q.remove(i)); |
244 |
> |
assertFalse(q.contains(i)); |
245 |
> |
assertTrue(q.contains(i-1)); |
246 |
> |
} |
247 |
> |
for (int i = 0; i < SIZE; i += 2) { |
248 |
> |
assertTrue(q.contains(i)); |
249 |
> |
assertTrue(q.remove(i)); |
250 |
> |
assertFalse(q.contains(i)); |
251 |
> |
assertFalse(q.remove(i+1)); |
252 |
> |
assertFalse(q.contains(i+1)); |
253 |
|
} |
254 |
|
assertTrue(q.isEmpty()); |
255 |
|
} |
323 |
|
assertTrue(q.removeAll(p)); |
324 |
|
assertEquals(SIZE-i, q.size()); |
325 |
|
for (int j = 0; j < i; ++j) { |
326 |
< |
Integer I = (Integer)(p.pollFirst()); |
327 |
< |
assertFalse(q.contains(I)); |
326 |
> |
Integer x = (Integer)(p.pollFirst()); |
327 |
> |
assertFalse(q.contains(x)); |
328 |
|
} |
329 |
|
} |
330 |
|
} |
331 |
|
|
322 |
– |
|
323 |
– |
|
332 |
|
/** |
333 |
|
* lower returns preceding element |
334 |
|
*/ |
402 |
|
} |
403 |
|
|
404 |
|
/** |
405 |
< |
* toArray contains all elements |
405 |
> |
* toArray contains all elements in sorted order |
406 |
|
*/ |
407 |
|
public void testToArray() { |
408 |
|
NavigableSet q = populatedSet(SIZE); |
409 |
|
Object[] o = q.toArray(); |
402 |
– |
Arrays.sort(o); |
410 |
|
for (int i = 0; i < o.length; i++) |
411 |
< |
assertEquals(o[i], q.pollFirst()); |
411 |
> |
assertSame(o[i], q.pollFirst()); |
412 |
|
} |
413 |
|
|
414 |
|
/** |
415 |
< |
* toArray(a) contains all elements |
415 |
> |
* toArray(a) contains all elements in sorted order |
416 |
|
*/ |
417 |
|
public void testToArray2() { |
418 |
< |
NavigableSet q = populatedSet(SIZE); |
418 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
419 |
|
Integer[] ints = new Integer[SIZE]; |
420 |
< |
ints = (Integer[])q.toArray(ints); |
421 |
< |
Arrays.sort(ints); |
420 |
> |
Integer[] array = q.toArray(ints); |
421 |
> |
assertSame(ints, array); |
422 |
|
for (int i = 0; i < ints.length; i++) |
423 |
< |
assertEquals(ints[i], q.pollFirst()); |
423 |
> |
assertSame(ints[i], q.pollFirst()); |
424 |
|
} |
425 |
|
|
426 |
|
/** |
428 |
|
*/ |
429 |
|
public void testIterator() { |
430 |
|
NavigableSet q = populatedSet(SIZE); |
424 |
– |
int i = 0; |
431 |
|
Iterator it = q.iterator(); |
432 |
< |
while (it.hasNext()) { |
432 |
> |
int i; |
433 |
> |
for (i = 0; it.hasNext(); i++) |
434 |
|
assertTrue(q.contains(it.next())); |
428 |
– |
++i; |
429 |
– |
} |
435 |
|
assertEquals(i, SIZE); |
436 |
+ |
assertIteratorExhausted(it); |
437 |
|
} |
438 |
|
|
439 |
|
/** |
440 |
|
* iterator of empty set has no elements |
441 |
|
*/ |
442 |
|
public void testEmptyIterator() { |
443 |
< |
NavigableSet q = set0(); |
438 |
< |
int i = 0; |
439 |
< |
Iterator it = q.iterator(); |
440 |
< |
while (it.hasNext()) { |
441 |
< |
assertTrue(q.contains(it.next())); |
442 |
< |
++i; |
443 |
< |
} |
444 |
< |
assertEquals(i, 0); |
443 |
> |
assertIteratorExhausted(set0().iterator()); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
457 |
|
it.remove(); |
458 |
|
|
459 |
|
it = q.iterator(); |
460 |
< |
assertEquals(it.next(), 2); |
461 |
< |
assertEquals(it.next(), 3); |
460 |
> |
assertEquals(2, it.next()); |
461 |
> |
assertEquals(3, it.next()); |
462 |
|
assertFalse(it.hasNext()); |
463 |
|
} |
464 |
|
|
466 |
– |
|
465 |
|
/** |
466 |
|
* toString contains toStrings of elements |
467 |
|
*/ |
469 |
|
NavigableSet q = populatedSet(SIZE); |
470 |
|
String s = q.toString(); |
471 |
|
for (int i = 0; i < SIZE; ++i) { |
472 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
472 |
> |
assertTrue(s.contains(String.valueOf(i))); |
473 |
|
} |
474 |
|
} |
475 |
|
|
477 |
|
* A deserialized serialized set has same elements |
478 |
|
*/ |
479 |
|
public void testSerialization() throws Exception { |
480 |
< |
NavigableSet q = populatedSet(SIZE); |
481 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
482 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
483 |
< |
out.writeObject(q); |
484 |
< |
out.close(); |
485 |
< |
|
486 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
487 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
488 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
489 |
< |
assertEquals(q.size(), r.size()); |
490 |
< |
while (!q.isEmpty()) |
491 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
480 |
> |
NavigableSet x = populatedSet(SIZE); |
481 |
> |
NavigableSet y = serialClone(x); |
482 |
> |
|
483 |
> |
assertNotSame(x, y); |
484 |
> |
assertEquals(x.size(), y.size()); |
485 |
> |
assertEquals(x, y); |
486 |
> |
assertEquals(y, x); |
487 |
> |
while (!x.isEmpty()) { |
488 |
> |
assertFalse(y.isEmpty()); |
489 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
490 |
> |
} |
491 |
> |
assertTrue(y.isEmpty()); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
655 |
|
} catch (ClassCastException success) {} |
656 |
|
} |
657 |
|
|
660 |
– |
|
658 |
|
/** |
659 |
|
* addAll(null) throws NPE |
660 |
|
*/ |
677 |
|
shouldThrow(); |
678 |
|
} catch (NullPointerException success) {} |
679 |
|
} |
680 |
< |
|
680 |
> |
|
681 |
|
/** |
682 |
|
* addAll of a collection with any null elements throws NPE after |
683 |
|
* possibly adding some elements |
724 |
|
*/ |
725 |
|
public void testDescendingRemoveElement() { |
726 |
|
NavigableSet q = populatedSet(SIZE); |
727 |
< |
for (int i = 1; i < SIZE; i+=2) { |
727 |
> |
for (int i = 1; i < SIZE; i += 2) { |
728 |
|
assertTrue(q.remove(new Integer(i))); |
729 |
|
} |
730 |
< |
for (int i = 0; i < SIZE; i+=2) { |
730 |
> |
for (int i = 0; i < SIZE; i += 2) { |
731 |
|
assertTrue(q.remove(new Integer(i))); |
732 |
|
assertFalse(q.remove(new Integer(i+1))); |
733 |
|
} |
803 |
|
assertTrue(q.removeAll(p)); |
804 |
|
assertEquals(SIZE-i, q.size()); |
805 |
|
for (int j = 0; j < i; ++j) { |
806 |
< |
Integer I = (Integer)(p.pollFirst()); |
807 |
< |
assertFalse(q.contains(I)); |
806 |
> |
Integer x = (Integer)(p.pollFirst()); |
807 |
> |
assertFalse(q.contains(x)); |
808 |
|
} |
809 |
|
} |
810 |
|
} |
811 |
|
|
815 |
– |
|
816 |
– |
|
812 |
|
/** |
813 |
|
* lower returns preceding element |
814 |
|
*/ |
898 |
|
public void testDescendingToArray2() { |
899 |
|
NavigableSet q = populatedSet(SIZE); |
900 |
|
Integer[] ints = new Integer[SIZE]; |
901 |
< |
ints = (Integer[])q.toArray(ints); |
901 |
> |
assertSame(ints, q.toArray(ints)); |
902 |
|
Arrays.sort(ints); |
903 |
|
for (int i = 0; i < ints.length; i++) |
904 |
|
assertEquals(ints[i], q.pollFirst()); |
929 |
|
assertTrue(q.contains(it.next())); |
930 |
|
++i; |
931 |
|
} |
932 |
< |
assertEquals(i, 0); |
932 |
> |
assertEquals(0, i); |
933 |
|
} |
934 |
|
|
935 |
|
/** |
946 |
|
it.remove(); |
947 |
|
|
948 |
|
it = q.iterator(); |
949 |
< |
assertEquals(it.next(), 2); |
950 |
< |
assertEquals(it.next(), 3); |
949 |
> |
assertEquals(2, it.next()); |
950 |
> |
assertEquals(3, it.next()); |
951 |
|
assertFalse(it.hasNext()); |
952 |
|
} |
953 |
|
|
959 |
– |
|
954 |
|
/** |
955 |
|
* toString contains toStrings of elements |
956 |
|
*/ |
958 |
|
NavigableSet q = populatedSet(SIZE); |
959 |
|
String s = q.toString(); |
960 |
|
for (int i = 0; i < SIZE; ++i) { |
961 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
961 |
> |
assertTrue(s.contains(String.valueOf(i))); |
962 |
|
} |
963 |
|
} |
964 |
|
|
966 |
|
* A deserialized serialized set has same elements |
967 |
|
*/ |
968 |
|
public void testDescendingSerialization() throws Exception { |
969 |
< |
NavigableSet q = populatedSet(SIZE); |
970 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
971 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
972 |
< |
out.writeObject(q); |
973 |
< |
out.close(); |
974 |
< |
|
975 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
976 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
977 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
978 |
< |
assertEquals(q.size(), r.size()); |
979 |
< |
while (!q.isEmpty()) |
980 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
969 |
> |
NavigableSet x = dset5(); |
970 |
> |
NavigableSet y = serialClone(x); |
971 |
> |
|
972 |
> |
assertNotSame(x, y); |
973 |
> |
assertEquals(x.size(), y.size()); |
974 |
> |
assertEquals(x.toString(), y.toString()); |
975 |
> |
assertEquals(x, y); |
976 |
> |
assertEquals(y, x); |
977 |
> |
while (!x.isEmpty()) { |
978 |
> |
assertFalse(y.isEmpty()); |
979 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
980 |
> |
} |
981 |
> |
assertTrue(y.isEmpty()); |
982 |
|
} |
983 |
|
|
984 |
|
/** |
1100 |
|
assertEquals(4, set.size()); |
1101 |
|
} |
1102 |
|
|
1103 |
+ |
/** |
1104 |
+ |
* addAll is idempotent |
1105 |
+ |
*/ |
1106 |
+ |
public void testAddAll_idempotent() throws Exception { |
1107 |
+ |
Set x = populatedSet(SIZE); |
1108 |
+ |
Set y = new TreeSet(x); |
1109 |
+ |
y.addAll(x); |
1110 |
+ |
assertEquals(x, y); |
1111 |
+ |
assertEquals(y, x); |
1112 |
+ |
} |
1113 |
+ |
|
1114 |
|
} |