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.*; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.Comparator; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.NavigableSet; |
12 |
> |
import java.util.SortedSet; |
13 |
> |
import java.util.Set; |
14 |
> |
import java.util.TreeSet; |
15 |
|
|
16 |
|
public class TreeSubSetTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(TreeSubSetTest.class); |
23 |
|
|
24 |
|
static class MyReverseComparator implements Comparator { |
25 |
|
public int compare(Object x, Object y) { |
26 |
< |
int i = ((Integer)x).intValue(); |
23 |
< |
int j = ((Integer)y).intValue(); |
24 |
< |
if (i < j) return 1; |
25 |
< |
if (i > j) return -1; |
26 |
< |
return 0; |
26 |
> |
return ((Comparable)y).compareTo(x); |
27 |
|
} |
28 |
|
} |
29 |
|
|
30 |
|
/** |
31 |
< |
* Create a set of given size containing consecutive |
31 |
> |
* Returns a new set of given size containing consecutive |
32 |
|
* Integers 0 ... n. |
33 |
|
*/ |
34 |
< |
private NavigableSet populatedSet(int n) { |
35 |
< |
TreeSet q = new TreeSet(); |
34 |
> |
private NavigableSet<Integer> populatedSet(int n) { |
35 |
> |
TreeSet<Integer> q = new TreeSet<Integer>(); |
36 |
|
assertTrue(q.isEmpty()); |
37 |
|
|
38 |
|
for (int i = n-1; i >= 0; i-=2) |
48 |
|
} |
49 |
|
|
50 |
|
/** |
51 |
< |
* Create set of first 5 ints |
51 |
> |
* Returns a new set of first 5 ints. |
52 |
|
*/ |
53 |
|
private NavigableSet set5() { |
54 |
|
TreeSet q = new TreeSet(); |
97 |
|
assertEquals(0, set0().size()); |
98 |
|
} |
99 |
|
|
100 |
– |
|
100 |
|
/** |
101 |
|
* isEmpty is true before add, false after |
102 |
|
*/ |
103 |
|
public void testEmpty() { |
104 |
|
NavigableSet q = set0(); |
105 |
|
assertTrue(q.isEmpty()); |
106 |
< |
q.add(new Integer(1)); |
106 |
> |
assertTrue(q.add(new Integer(1))); |
107 |
|
assertFalse(q.isEmpty()); |
108 |
< |
q.add(new Integer(2)); |
108 |
> |
assertTrue(q.add(new Integer(2))); |
109 |
|
q.pollFirst(); |
110 |
|
q.pollFirst(); |
111 |
|
assertTrue(q.isEmpty()); |
167 |
|
} catch (ClassCastException success) {} |
168 |
|
} |
169 |
|
|
171 |
– |
|
170 |
|
/** |
171 |
|
* addAll(null) throws NPE |
172 |
|
*/ |
177 |
|
shouldThrow(); |
178 |
|
} catch (NullPointerException success) {} |
179 |
|
} |
180 |
+ |
|
181 |
|
/** |
182 |
|
* addAll of a collection with null elements throws NPE |
183 |
|
*/ |
189 |
|
shouldThrow(); |
190 |
|
} catch (NullPointerException success) {} |
191 |
|
} |
192 |
+ |
|
193 |
|
/** |
194 |
|
* addAll of a collection with any null elements throws NPE after |
195 |
|
* possibly adding some elements |
226 |
|
public void testPoll() { |
227 |
|
NavigableSet q = populatedSet(SIZE); |
228 |
|
for (int i = 0; i < SIZE; ++i) { |
229 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
229 |
> |
assertEquals(i, q.pollFirst()); |
230 |
|
} |
231 |
|
assertNull(q.pollFirst()); |
232 |
|
} |
237 |
|
public void testRemoveElement() { |
238 |
|
NavigableSet q = populatedSet(SIZE); |
239 |
|
for (int i = 1; i < SIZE; i+=2) { |
240 |
< |
assertTrue(q.remove(new Integer(i))); |
240 |
> |
assertTrue(q.contains(i)); |
241 |
> |
assertTrue(q.remove(i)); |
242 |
> |
assertFalse(q.contains(i)); |
243 |
> |
assertTrue(q.contains(i-1)); |
244 |
|
} |
245 |
|
for (int i = 0; i < SIZE; i+=2) { |
246 |
< |
assertTrue(q.remove(new Integer(i))); |
247 |
< |
assertFalse(q.remove(new Integer(i+1))); |
246 |
> |
assertTrue(q.contains(i)); |
247 |
> |
assertTrue(q.remove(i)); |
248 |
> |
assertFalse(q.contains(i)); |
249 |
> |
assertFalse(q.remove(i+1)); |
250 |
> |
assertFalse(q.contains(i+1)); |
251 |
|
} |
252 |
|
assertTrue(q.isEmpty()); |
253 |
|
} |
272 |
|
q.clear(); |
273 |
|
assertTrue(q.isEmpty()); |
274 |
|
assertEquals(0, q.size()); |
275 |
< |
q.add(new Integer(1)); |
275 |
> |
assertTrue(q.add(new Integer(1))); |
276 |
|
assertFalse(q.isEmpty()); |
277 |
|
q.clear(); |
278 |
|
assertTrue(q.isEmpty()); |
327 |
|
} |
328 |
|
} |
329 |
|
|
324 |
– |
|
325 |
– |
|
330 |
|
/** |
331 |
|
* lower returns preceding element |
332 |
|
*/ |
343 |
|
|
344 |
|
Object e4 = q.lower(zero); |
345 |
|
assertNull(e4); |
342 |
– |
|
346 |
|
} |
347 |
|
|
348 |
|
/** |
361 |
|
|
362 |
|
Object e4 = q.higher(six); |
363 |
|
assertNull(e4); |
361 |
– |
|
364 |
|
} |
365 |
|
|
366 |
|
/** |
379 |
|
|
380 |
|
Object e4 = q.floor(zero); |
381 |
|
assertNull(e4); |
380 |
– |
|
382 |
|
} |
383 |
|
|
384 |
|
/** |
397 |
|
|
398 |
|
Object e4 = q.ceiling(six); |
399 |
|
assertNull(e4); |
399 |
– |
|
400 |
|
} |
401 |
|
|
402 |
|
/** |
403 |
< |
* toArray contains all elements |
403 |
> |
* toArray contains all elements in sorted order |
404 |
|
*/ |
405 |
|
public void testToArray() { |
406 |
|
NavigableSet q = populatedSet(SIZE); |
407 |
|
Object[] o = q.toArray(); |
408 |
– |
Arrays.sort(o); |
408 |
|
for (int i = 0; i < o.length; i++) |
409 |
< |
assertEquals(o[i], q.pollFirst()); |
409 |
> |
assertSame(o[i], q.pollFirst()); |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* toArray(a) contains all elements |
413 |
> |
* toArray(a) contains all elements in sorted order |
414 |
|
*/ |
415 |
|
public void testToArray2() { |
416 |
< |
NavigableSet q = populatedSet(SIZE); |
416 |
> |
NavigableSet<Integer> q = populatedSet(SIZE); |
417 |
|
Integer[] ints = new Integer[SIZE]; |
418 |
< |
ints = (Integer[])q.toArray(ints); |
419 |
< |
Arrays.sort(ints); |
418 |
> |
Integer[] array = q.toArray(ints); |
419 |
> |
assertSame(ints, array); |
420 |
|
for (int i = 0; i < ints.length; i++) |
421 |
< |
assertEquals(ints[i], q.pollFirst()); |
421 |
> |
assertSame(ints[i], q.pollFirst()); |
422 |
|
} |
423 |
|
|
424 |
|
/** |
446 |
|
assertTrue(q.contains(it.next())); |
447 |
|
++i; |
448 |
|
} |
449 |
< |
assertEquals(i, 0); |
449 |
> |
assertEquals(0, i); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
|
* iterator.remove removes current element |
454 |
|
*/ |
455 |
< |
public void testIteratorRemove () { |
455 |
> |
public void testIteratorRemove() { |
456 |
|
final NavigableSet q = set0(); |
457 |
|
q.add(new Integer(2)); |
458 |
|
q.add(new Integer(1)); |
463 |
|
it.remove(); |
464 |
|
|
465 |
|
it = q.iterator(); |
466 |
< |
assertEquals(it.next(), new Integer(2)); |
467 |
< |
assertEquals(it.next(), new Integer(3)); |
466 |
> |
assertEquals(2, it.next()); |
467 |
> |
assertEquals(3, it.next()); |
468 |
|
assertFalse(it.hasNext()); |
469 |
|
} |
470 |
|
|
472 |
– |
|
471 |
|
/** |
472 |
|
* toString contains toStrings of elements |
473 |
|
*/ |
475 |
|
NavigableSet q = populatedSet(SIZE); |
476 |
|
String s = q.toString(); |
477 |
|
for (int i = 0; i < SIZE; ++i) { |
478 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
478 |
> |
assertTrue(s.contains(String.valueOf(i))); |
479 |
|
} |
480 |
|
} |
481 |
|
|
483 |
|
* A deserialized serialized set has same elements |
484 |
|
*/ |
485 |
|
public void testSerialization() throws Exception { |
486 |
< |
NavigableSet q = populatedSet(SIZE); |
487 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
488 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
489 |
< |
out.writeObject(q); |
490 |
< |
out.close(); |
491 |
< |
|
492 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
493 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
494 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
495 |
< |
assertEquals(q.size(), r.size()); |
496 |
< |
while (!q.isEmpty()) |
497 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
486 |
> |
NavigableSet x = populatedSet(SIZE); |
487 |
> |
NavigableSet y = serialClone(x); |
488 |
> |
|
489 |
> |
assertTrue(x != y); |
490 |
> |
assertEquals(x.size(), y.size()); |
491 |
> |
assertEquals(x, y); |
492 |
> |
assertEquals(y, x); |
493 |
> |
while (!x.isEmpty()) { |
494 |
> |
assertFalse(y.isEmpty()); |
495 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
496 |
> |
} |
497 |
> |
assertTrue(y.isEmpty()); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
661 |
|
} catch (ClassCastException success) {} |
662 |
|
} |
663 |
|
|
666 |
– |
|
664 |
|
/** |
665 |
|
* addAll(null) throws NPE |
666 |
|
*/ |
671 |
|
shouldThrow(); |
672 |
|
} catch (NullPointerException success) {} |
673 |
|
} |
674 |
+ |
|
675 |
|
/** |
676 |
|
* addAll of a collection with null elements throws NPE |
677 |
|
*/ |
683 |
|
shouldThrow(); |
684 |
|
} catch (NullPointerException success) {} |
685 |
|
} |
686 |
+ |
|
687 |
|
/** |
688 |
|
* addAll of a collection with any null elements throws NPE after |
689 |
|
* possibly adding some elements |
720 |
|
public void testDescendingPoll() { |
721 |
|
NavigableSet q = populatedSet(SIZE); |
722 |
|
for (int i = 0; i < SIZE; ++i) { |
723 |
< |
assertEquals(i, ((Integer)q.pollFirst()).intValue()); |
723 |
> |
assertEquals(i, q.pollFirst()); |
724 |
|
} |
725 |
|
assertNull(q.pollFirst()); |
726 |
|
} |
760 |
|
q.clear(); |
761 |
|
assertTrue(q.isEmpty()); |
762 |
|
assertEquals(0, q.size()); |
763 |
< |
q.add(new Integer(1)); |
763 |
> |
assertTrue(q.add(new Integer(1))); |
764 |
|
assertFalse(q.isEmpty()); |
765 |
|
q.clear(); |
766 |
|
assertTrue(q.isEmpty()); |
815 |
|
} |
816 |
|
} |
817 |
|
|
819 |
– |
|
820 |
– |
|
818 |
|
/** |
819 |
|
* lower returns preceding element |
820 |
|
*/ |
831 |
|
|
832 |
|
Object e4 = q.lower(zero); |
833 |
|
assertNull(e4); |
837 |
– |
|
834 |
|
} |
835 |
|
|
836 |
|
/** |
849 |
|
|
850 |
|
Object e4 = q.higher(m6); |
851 |
|
assertNull(e4); |
856 |
– |
|
852 |
|
} |
853 |
|
|
854 |
|
/** |
867 |
|
|
868 |
|
Object e4 = q.floor(zero); |
869 |
|
assertNull(e4); |
875 |
– |
|
870 |
|
} |
871 |
|
|
872 |
|
/** |
885 |
|
|
886 |
|
Object e4 = q.ceiling(m6); |
887 |
|
assertNull(e4); |
894 |
– |
|
888 |
|
} |
889 |
|
|
890 |
|
/** |
904 |
|
public void testDescendingToArray2() { |
905 |
|
NavigableSet q = populatedSet(SIZE); |
906 |
|
Integer[] ints = new Integer[SIZE]; |
907 |
< |
ints = (Integer[])q.toArray(ints); |
907 |
> |
assertSame(ints, q.toArray(ints)); |
908 |
|
Arrays.sort(ints); |
909 |
|
for (int i = 0; i < ints.length; i++) |
910 |
|
assertEquals(ints[i], q.pollFirst()); |
935 |
|
assertTrue(q.contains(it.next())); |
936 |
|
++i; |
937 |
|
} |
938 |
< |
assertEquals(i, 0); |
938 |
> |
assertEquals(0, i); |
939 |
|
} |
940 |
|
|
941 |
|
/** |
942 |
|
* iterator.remove removes current element |
943 |
|
*/ |
944 |
< |
public void testDescendingIteratorRemove () { |
944 |
> |
public void testDescendingIteratorRemove() { |
945 |
|
final NavigableSet q = dset0(); |
946 |
|
q.add(new Integer(2)); |
947 |
|
q.add(new Integer(1)); |
952 |
|
it.remove(); |
953 |
|
|
954 |
|
it = q.iterator(); |
955 |
< |
assertEquals(it.next(), new Integer(2)); |
956 |
< |
assertEquals(it.next(), new Integer(3)); |
955 |
> |
assertEquals(2, it.next()); |
956 |
> |
assertEquals(3, it.next()); |
957 |
|
assertFalse(it.hasNext()); |
958 |
|
} |
959 |
|
|
967 |
– |
|
960 |
|
/** |
961 |
|
* toString contains toStrings of elements |
962 |
|
*/ |
964 |
|
NavigableSet q = populatedSet(SIZE); |
965 |
|
String s = q.toString(); |
966 |
|
for (int i = 0; i < SIZE; ++i) { |
967 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
967 |
> |
assertTrue(s.contains(String.valueOf(i))); |
968 |
|
} |
969 |
|
} |
970 |
|
|
972 |
|
* A deserialized serialized set has same elements |
973 |
|
*/ |
974 |
|
public void testDescendingSerialization() throws Exception { |
975 |
< |
NavigableSet q = populatedSet(SIZE); |
976 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
977 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
978 |
< |
out.writeObject(q); |
979 |
< |
out.close(); |
980 |
< |
|
981 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
982 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
983 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
984 |
< |
assertEquals(q.size(), r.size()); |
985 |
< |
while (!q.isEmpty()) |
986 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
975 |
> |
NavigableSet x = dset5(); |
976 |
> |
NavigableSet y = serialClone(x); |
977 |
> |
|
978 |
> |
assertTrue(x != y); |
979 |
> |
assertEquals(x.size(), y.size()); |
980 |
> |
assertEquals(x.toString(), y.toString()); |
981 |
> |
assertEquals(x, y); |
982 |
> |
assertEquals(y, x); |
983 |
> |
while (!x.isEmpty()) { |
984 |
> |
assertFalse(y.isEmpty()); |
985 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
986 |
> |
} |
987 |
> |
assertTrue(y.isEmpty()); |
988 |
|
} |
989 |
|
|
990 |
|
/** |
1106 |
|
assertEquals(4, set.size()); |
1107 |
|
} |
1108 |
|
|
1109 |
+ |
/** |
1110 |
+ |
* addAll is idempotent |
1111 |
+ |
*/ |
1112 |
+ |
public void testAddAll_idempotent() throws Exception { |
1113 |
+ |
Set x = populatedSet(SIZE); |
1114 |
+ |
Set y = new TreeSet(x); |
1115 |
+ |
y.addAll(x); |
1116 |
+ |
assertEquals(x, y); |
1117 |
+ |
assertEquals(y, x); |
1118 |
+ |
} |
1119 |
+ |
|
1120 |
|
} |