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.TreeSet; |
14 |
|
|
15 |
|
public class TreeSubSetTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
27 |
|
} |
28 |
|
|
29 |
|
/** |
30 |
< |
* Create a set of given size containing consecutive |
30 |
> |
* Creates a set of given size containing consecutive |
31 |
|
* Integers 0 ... n. |
32 |
|
*/ |
33 |
|
private NavigableSet<Integer> populatedSet(int n) { |
47 |
|
} |
48 |
|
|
49 |
|
/** |
50 |
< |
* Create set of first 5 ints |
50 |
> |
* Creates set of first 5 ints. |
51 |
|
*/ |
52 |
|
private NavigableSet set5() { |
53 |
|
TreeSet q = new TreeSet(); |
96 |
|
assertEquals(0, set0().size()); |
97 |
|
} |
98 |
|
|
96 |
– |
|
99 |
|
/** |
100 |
|
* isEmpty is true before add, false after |
101 |
|
*/ |
166 |
|
} catch (ClassCastException success) {} |
167 |
|
} |
168 |
|
|
167 |
– |
|
169 |
|
/** |
170 |
|
* addAll(null) throws NPE |
171 |
|
*/ |
326 |
|
} |
327 |
|
} |
328 |
|
|
328 |
– |
|
329 |
– |
|
329 |
|
/** |
330 |
|
* lower returns preceding element |
331 |
|
*/ |
445 |
|
assertTrue(q.contains(it.next())); |
446 |
|
++i; |
447 |
|
} |
448 |
< |
assertEquals(i, 0); |
448 |
> |
assertEquals(0, i); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
462 |
|
it.remove(); |
463 |
|
|
464 |
|
it = q.iterator(); |
465 |
< |
assertEquals(it.next(), 2); |
466 |
< |
assertEquals(it.next(), 3); |
465 |
> |
assertEquals(2, it.next()); |
466 |
> |
assertEquals(3, it.next()); |
467 |
|
assertFalse(it.hasNext()); |
468 |
|
} |
469 |
|
|
471 |
– |
|
470 |
|
/** |
471 |
|
* toString contains toStrings of elements |
472 |
|
*/ |
482 |
|
* A deserialized serialized set has same elements |
483 |
|
*/ |
484 |
|
public void testSerialization() throws Exception { |
485 |
< |
NavigableSet q = populatedSet(SIZE); |
486 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
487 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
488 |
< |
out.writeObject(q); |
489 |
< |
out.close(); |
490 |
< |
|
491 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
492 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
493 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
494 |
< |
assertEquals(q.size(), r.size()); |
495 |
< |
while (!q.isEmpty()) |
496 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
485 |
> |
NavigableSet x = populatedSet(SIZE); |
486 |
> |
NavigableSet y = serialClone(x); |
487 |
> |
|
488 |
> |
assertTrue(x != y); |
489 |
> |
assertEquals(x.size(), y.size()); |
490 |
> |
assertEquals(x, y); |
491 |
> |
assertEquals(y, x); |
492 |
> |
while (!x.isEmpty()) { |
493 |
> |
assertFalse(y.isEmpty()); |
494 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
495 |
> |
} |
496 |
> |
assertTrue(y.isEmpty()); |
497 |
|
} |
498 |
|
|
499 |
|
/** |
660 |
|
} catch (ClassCastException success) {} |
661 |
|
} |
662 |
|
|
665 |
– |
|
663 |
|
/** |
664 |
|
* addAll(null) throws NPE |
665 |
|
*/ |
814 |
|
} |
815 |
|
} |
816 |
|
|
820 |
– |
|
821 |
– |
|
817 |
|
/** |
818 |
|
* lower returns preceding element |
819 |
|
*/ |
934 |
|
assertTrue(q.contains(it.next())); |
935 |
|
++i; |
936 |
|
} |
937 |
< |
assertEquals(i, 0); |
937 |
> |
assertEquals(0, i); |
938 |
|
} |
939 |
|
|
940 |
|
/** |
951 |
|
it.remove(); |
952 |
|
|
953 |
|
it = q.iterator(); |
954 |
< |
assertEquals(it.next(), 2); |
955 |
< |
assertEquals(it.next(), 3); |
954 |
> |
assertEquals(2, it.next()); |
955 |
> |
assertEquals(3, it.next()); |
956 |
|
assertFalse(it.hasNext()); |
957 |
|
} |
958 |
|
|
964 |
– |
|
959 |
|
/** |
960 |
|
* toString contains toStrings of elements |
961 |
|
*/ |
971 |
|
* A deserialized serialized set has same elements |
972 |
|
*/ |
973 |
|
public void testDescendingSerialization() throws Exception { |
974 |
< |
NavigableSet q = populatedSet(SIZE); |
975 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
976 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
977 |
< |
out.writeObject(q); |
978 |
< |
out.close(); |
979 |
< |
|
980 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
981 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
982 |
< |
NavigableSet r = (NavigableSet)in.readObject(); |
983 |
< |
assertEquals(q.size(), r.size()); |
984 |
< |
while (!q.isEmpty()) |
985 |
< |
assertEquals(q.pollFirst(), r.pollFirst()); |
974 |
> |
NavigableSet x = dset5(); |
975 |
> |
NavigableSet y = serialClone(x); |
976 |
> |
|
977 |
> |
assertTrue(x != y); |
978 |
> |
assertEquals(x.size(), y.size()); |
979 |
> |
assertEquals(x.toString(), y.toString()); |
980 |
> |
assertEquals(x, y); |
981 |
> |
assertEquals(y, x); |
982 |
> |
while (!x.isEmpty()) { |
983 |
> |
assertFalse(y.isEmpty()); |
984 |
> |
assertEquals(x.pollFirst(), y.pollFirst()); |
985 |
> |
} |
986 |
> |
assertTrue(y.isEmpty()); |
987 |
|
} |
988 |
|
|
989 |
|
/** |