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) { |
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<Integer> populatedSet(int n) { |
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 |
|
|
96 |
– |
|
100 |
|
/** |
101 |
|
* isEmpty is true before add, false after |
102 |
|
*/ |
167 |
|
} catch (ClassCastException success) {} |
168 |
|
} |
169 |
|
|
167 |
– |
|
170 |
|
/** |
171 |
|
* addAll(null) throws NPE |
172 |
|
*/ |
327 |
|
} |
328 |
|
} |
329 |
|
|
328 |
– |
|
329 |
– |
|
330 |
|
/** |
331 |
|
* lower returns preceding element |
332 |
|
*/ |
446 |
|
assertTrue(q.contains(it.next())); |
447 |
|
++i; |
448 |
|
} |
449 |
< |
assertEquals(i, 0); |
449 |
> |
assertEquals(0, i); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
463 |
|
it.remove(); |
464 |
|
|
465 |
|
it = q.iterator(); |
466 |
< |
assertEquals(it.next(), 2); |
467 |
< |
assertEquals(it.next(), 3); |
466 |
> |
assertEquals(2, it.next()); |
467 |
> |
assertEquals(3, it.next()); |
468 |
|
assertFalse(it.hasNext()); |
469 |
|
} |
470 |
|
|
471 |
– |
|
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 |
> |
assertNotSame(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 |
|
|
665 |
– |
|
664 |
|
/** |
665 |
|
* addAll(null) throws NPE |
666 |
|
*/ |
815 |
|
} |
816 |
|
} |
817 |
|
|
820 |
– |
|
821 |
– |
|
818 |
|
/** |
819 |
|
* lower returns preceding element |
820 |
|
*/ |
935 |
|
assertTrue(q.contains(it.next())); |
936 |
|
++i; |
937 |
|
} |
938 |
< |
assertEquals(i, 0); |
938 |
> |
assertEquals(0, i); |
939 |
|
} |
940 |
|
|
941 |
|
/** |
952 |
|
it.remove(); |
953 |
|
|
954 |
|
it = q.iterator(); |
955 |
< |
assertEquals(it.next(), 2); |
956 |
< |
assertEquals(it.next(), 3); |
955 |
> |
assertEquals(2, it.next()); |
956 |
> |
assertEquals(3, it.next()); |
957 |
|
assertFalse(it.hasNext()); |
958 |
|
} |
959 |
|
|
964 |
– |
|
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 |
> |
assertNotSame(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 |
|
} |