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.ArrayList; |
8 |
> |
import java.util.Arrays; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.Map; |
12 |
> |
import java.util.NavigableMap; |
13 |
> |
import java.util.Set; |
14 |
> |
import java.util.SortedMap; |
15 |
> |
import java.util.TreeMap; |
16 |
> |
|
17 |
> |
import junit.framework.Test; |
18 |
> |
import junit.framework.TestSuite; |
19 |
|
|
20 |
|
public class TreeSubMapTest extends JSR166TestCase { |
21 |
|
public static void main(String[] args) { |
26 |
|
} |
27 |
|
|
28 |
|
/** |
29 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
29 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
30 |
|
*/ |
31 |
|
private static NavigableMap map5() { |
32 |
|
TreeMap map = new TreeMap(); |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Create a map from Integers -5 to -1 to Strings "A"-"E". |
53 |
> |
* Returns a new map from Integers -5 to -1 to Strings "A"-"E". |
54 |
|
*/ |
55 |
|
private static NavigableMap dmap5() { |
56 |
|
TreeMap map = new TreeMap(); |
77 |
|
public void testClear() { |
78 |
|
NavigableMap map = map5(); |
79 |
|
map.clear(); |
80 |
< |
assertEquals(map.size(), 0); |
80 |
> |
assertEquals(0, map.size()); |
81 |
|
} |
82 |
|
|
75 |
– |
|
83 |
|
/** |
84 |
|
* Maps with same contents are equal |
85 |
|
*/ |
148 |
|
assertEquals(five, map.lastKey()); |
149 |
|
} |
150 |
|
|
144 |
– |
|
151 |
|
/** |
152 |
|
* keySet returns a Set containing all the keys |
153 |
|
*/ |
382 |
|
NavigableMap map = map5(); |
383 |
|
String s = map.toString(); |
384 |
|
for (int i = 1; i <= 5; ++i) { |
385 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
385 |
> |
assertTrue(s.contains(String.valueOf(i))); |
386 |
|
} |
387 |
|
} |
388 |
|
|
436 |
|
* A deserialized map equals original |
437 |
|
*/ |
438 |
|
public void testSerialization() throws Exception { |
439 |
< |
NavigableMap q = map5(); |
439 |
> |
NavigableMap x = map5(); |
440 |
> |
NavigableMap y = serialClone(x); |
441 |
|
|
442 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
443 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
444 |
< |
out.writeObject(q); |
445 |
< |
out.close(); |
446 |
< |
|
440 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
441 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
442 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
443 |
< |
assertFalse(r.isEmpty()); |
444 |
< |
assertEquals(q.size(), r.size()); |
445 |
< |
assertTrue(q.equals(r)); |
446 |
< |
assertTrue(r.equals(q)); |
442 |
> |
assertNotSame(x, y); |
443 |
> |
assertEquals(x.size(), y.size()); |
444 |
> |
assertEquals(x.toString(), y.toString()); |
445 |
> |
assertEquals(x, y); |
446 |
> |
assertEquals(y, x); |
447 |
|
} |
448 |
|
|
449 |
– |
|
450 |
– |
|
449 |
|
/** |
450 |
|
* subMap returns map with keys in requested range |
451 |
|
*/ |
587 |
|
public void testDescendingClear() { |
588 |
|
NavigableMap map = dmap5(); |
589 |
|
map.clear(); |
590 |
< |
assertEquals(map.size(), 0); |
590 |
> |
assertEquals(0, map.size()); |
591 |
|
} |
592 |
|
|
595 |
– |
|
593 |
|
/** |
594 |
|
* Maps with same contents are equal |
595 |
|
*/ |
658 |
|
assertEquals(m5, map.lastKey()); |
659 |
|
} |
660 |
|
|
664 |
– |
|
661 |
|
/** |
662 |
|
* keySet returns a Set containing all the keys |
663 |
|
*/ |
744 |
|
assertTrue(s.contains("E")); |
745 |
|
} |
746 |
|
|
751 |
– |
|
747 |
|
/** |
748 |
|
* entrySet contains all pairs |
749 |
|
*/ |
778 |
|
assertTrue(empty.containsKey(m5)); |
779 |
|
} |
780 |
|
|
786 |
– |
|
781 |
|
/** |
782 |
|
* remove removes the correct key-value pair from the map |
783 |
|
*/ |
931 |
|
NavigableMap map = dmap5(); |
932 |
|
String s = map.toString(); |
933 |
|
for (int i = 1; i <= 5; ++i) { |
934 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
934 |
> |
assertTrue(s.contains(String.valueOf(i))); |
935 |
|
} |
936 |
|
} |
937 |
|
|
963 |
|
* A deserialized map equals original |
964 |
|
*/ |
965 |
|
public void testDescendingSerialization() throws Exception { |
966 |
< |
NavigableMap q = dmap5(); |
966 |
> |
NavigableMap x = dmap5(); |
967 |
> |
NavigableMap y = serialClone(x); |
968 |
|
|
969 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
970 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
971 |
< |
out.writeObject(q); |
972 |
< |
out.close(); |
973 |
< |
|
979 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
980 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
981 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
982 |
< |
assertEquals(q.size(), r.size()); |
983 |
< |
assertTrue(q.equals(r)); |
984 |
< |
assertTrue(r.equals(q)); |
969 |
> |
assertNotSame(x, y); |
970 |
> |
assertEquals(x.size(), y.size()); |
971 |
> |
assertEquals(x.toString(), y.toString()); |
972 |
> |
assertEquals(x, y); |
973 |
> |
assertEquals(y, x); |
974 |
|
} |
975 |
|
|
987 |
– |
|
976 |
|
/** |
977 |
|
* subMap returns map with keys in requested range |
978 |
|
*/ |