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 |
> |
junit.textui.TestRunner.run(suite()); |
21 |
|
} |
22 |
|
public static Test suite() { |
23 |
|
return new TestSuite(TreeSubSetTest.class); |
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) |
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 |
|
*/ |
179 |
|
shouldThrow(); |
180 |
|
} catch (NullPointerException success) {} |
181 |
|
} |
182 |
+ |
|
183 |
|
/** |
184 |
|
* addAll of a collection with null elements throws NPE |
185 |
|
*/ |
191 |
|
shouldThrow(); |
192 |
|
} catch (NullPointerException success) {} |
193 |
|
} |
194 |
+ |
|
195 |
|
/** |
196 |
|
* addAll of a collection with any null elements throws NPE after |
197 |
|
* possibly adding some elements |
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))); |
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.remove(new Integer(i))); |
249 |
< |
assertFalse(q.remove(new Integer(i+1))); |
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 |
|
} |
329 |
|
} |
330 |
|
} |
331 |
|
|
320 |
– |
|
321 |
– |
|
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(); |
400 |
– |
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 |
|
/** |
448 |
|
assertTrue(q.contains(it.next())); |
449 |
|
++i; |
450 |
|
} |
451 |
< |
assertEquals(i, 0); |
451 |
> |
assertEquals(0, i); |
452 |
|
} |
453 |
|
|
454 |
|
/** |
455 |
|
* iterator.remove removes current element |
456 |
|
*/ |
457 |
< |
public void testIteratorRemove () { |
457 |
> |
public void testIteratorRemove() { |
458 |
|
final NavigableSet q = set0(); |
459 |
|
q.add(new Integer(2)); |
460 |
|
q.add(new Integer(1)); |
465 |
|
it.remove(); |
466 |
|
|
467 |
|
it = q.iterator(); |
468 |
< |
assertEquals(it.next(), 2); |
469 |
< |
assertEquals(it.next(), 3); |
468 |
> |
assertEquals(2, it.next()); |
469 |
> |
assertEquals(3, it.next()); |
470 |
|
assertFalse(it.hasNext()); |
471 |
|
} |
472 |
|
|
464 |
– |
|
473 |
|
/** |
474 |
|
* toString contains toStrings of elements |
475 |
|
*/ |
477 |
|
NavigableSet q = populatedSet(SIZE); |
478 |
|
String s = q.toString(); |
479 |
|
for (int i = 0; i < SIZE; ++i) { |
480 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
480 |
> |
assertTrue(s.contains(String.valueOf(i))); |
481 |
|
} |
482 |
|
} |
483 |
|
|
485 |
|
* A deserialized serialized set has same elements |
486 |
|
*/ |
487 |
|
public void testSerialization() throws Exception { |
488 |
< |
NavigableSet q = populatedSet(SIZE); |
489 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
490 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
491 |
< |
out.writeObject(q); |
492 |
< |
out.close(); |
493 |
< |
|
494 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
495 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
496 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
497 |
< |
assertEquals(q.size(), r.size()); |
498 |
< |
while (!q.isEmpty()) |
499 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
488 |
> |
NavigableSet x = populatedSet(SIZE); |
489 |
> |
NavigableSet y = serialClone(x); |
490 |
> |
|
491 |
> |
assertNotSame(x, y); |
492 |
> |
assertEquals(x.size(), y.size()); |
493 |
> |
assertEquals(x, y); |
494 |
> |
assertEquals(y, x); |
495 |
> |
while (!x.isEmpty()) { |
496 |
> |
assertFalse(y.isEmpty()); |
497 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
498 |
> |
} |
499 |
> |
assertTrue(y.isEmpty()); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
663 |
|
} catch (ClassCastException success) {} |
664 |
|
} |
665 |
|
|
658 |
– |
|
666 |
|
/** |
667 |
|
* addAll(null) throws NPE |
668 |
|
*/ |
673 |
|
shouldThrow(); |
674 |
|
} catch (NullPointerException success) {} |
675 |
|
} |
676 |
+ |
|
677 |
|
/** |
678 |
|
* addAll of a collection with null elements throws NPE |
679 |
|
*/ |
685 |
|
shouldThrow(); |
686 |
|
} catch (NullPointerException success) {} |
687 |
|
} |
688 |
+ |
|
689 |
|
/** |
690 |
|
* addAll of a collection with any null elements throws NPE after |
691 |
|
* possibly adding some elements |
817 |
|
} |
818 |
|
} |
819 |
|
|
811 |
– |
|
812 |
– |
|
820 |
|
/** |
821 |
|
* lower returns preceding element |
822 |
|
*/ |
906 |
|
public void testDescendingToArray2() { |
907 |
|
NavigableSet q = populatedSet(SIZE); |
908 |
|
Integer[] ints = new Integer[SIZE]; |
909 |
< |
ints = (Integer[])q.toArray(ints); |
909 |
> |
assertSame(ints, q.toArray(ints)); |
910 |
|
Arrays.sort(ints); |
911 |
|
for (int i = 0; i < ints.length; i++) |
912 |
|
assertEquals(ints[i], q.pollFirst()); |
937 |
|
assertTrue(q.contains(it.next())); |
938 |
|
++i; |
939 |
|
} |
940 |
< |
assertEquals(i, 0); |
940 |
> |
assertEquals(0, i); |
941 |
|
} |
942 |
|
|
943 |
|
/** |
944 |
|
* iterator.remove removes current element |
945 |
|
*/ |
946 |
< |
public void testDescendingIteratorRemove () { |
946 |
> |
public void testDescendingIteratorRemove() { |
947 |
|
final NavigableSet q = dset0(); |
948 |
|
q.add(new Integer(2)); |
949 |
|
q.add(new Integer(1)); |
954 |
|
it.remove(); |
955 |
|
|
956 |
|
it = q.iterator(); |
957 |
< |
assertEquals(it.next(), 2); |
958 |
< |
assertEquals(it.next(), 3); |
957 |
> |
assertEquals(2, it.next()); |
958 |
> |
assertEquals(3, it.next()); |
959 |
|
assertFalse(it.hasNext()); |
960 |
|
} |
961 |
|
|
955 |
– |
|
962 |
|
/** |
963 |
|
* toString contains toStrings of elements |
964 |
|
*/ |
966 |
|
NavigableSet q = populatedSet(SIZE); |
967 |
|
String s = q.toString(); |
968 |
|
for (int i = 0; i < SIZE; ++i) { |
969 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
969 |
> |
assertTrue(s.contains(String.valueOf(i))); |
970 |
|
} |
971 |
|
} |
972 |
|
|
974 |
|
* A deserialized serialized set has same elements |
975 |
|
*/ |
976 |
|
public void testDescendingSerialization() throws Exception { |
977 |
< |
NavigableSet q = populatedSet(SIZE); |
978 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
979 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
980 |
< |
out.writeObject(q); |
981 |
< |
out.close(); |
982 |
< |
|
983 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
984 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
985 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
986 |
< |
assertEquals(q.size(), r.size()); |
987 |
< |
while (!q.isEmpty()) |
988 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
977 |
> |
NavigableSet x = dset5(); |
978 |
> |
NavigableSet y = serialClone(x); |
979 |
> |
|
980 |
> |
assertNotSame(x, y); |
981 |
> |
assertEquals(x.size(), y.size()); |
982 |
> |
assertEquals(x.toString(), y.toString()); |
983 |
> |
assertEquals(x, y); |
984 |
> |
assertEquals(y, x); |
985 |
> |
while (!x.isEmpty()) { |
986 |
> |
assertFalse(y.isEmpty()); |
987 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
988 |
> |
} |
989 |
> |
assertTrue(y.isEmpty()); |
990 |
|
} |
991 |
|
|
992 |
|
/** |
1108 |
|
assertEquals(4, set.size()); |
1109 |
|
} |
1110 |
|
|
1111 |
+ |
/** |
1112 |
+ |
* addAll is idempotent |
1113 |
+ |
*/ |
1114 |
+ |
public void testAddAll_idempotent() throws Exception { |
1115 |
+ |
Set x = populatedSet(SIZE); |
1116 |
+ |
Set y = new TreeSet(x); |
1117 |
+ |
y.addAll(x); |
1118 |
+ |
assertEquals(x, y); |
1119 |
+ |
assertEquals(y, x); |
1120 |
+ |
} |
1121 |
+ |
|
1122 |
|
} |