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.*; |
9 |
|
|
10 |
|
public class TreeMapTest extends JSR166TestCase { |
11 |
|
public static void main(String[] args) { |
12 |
< |
junit.textui.TestRunner.run (suite()); |
12 |
> |
junit.textui.TestRunner.run(suite()); |
13 |
|
} |
14 |
|
public static Test suite() { |
15 |
|
return new TestSuite(TreeMapTest.class); |
16 |
|
} |
17 |
|
|
18 |
|
/** |
19 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
19 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
20 |
|
*/ |
21 |
|
private static TreeMap map5() { |
22 |
|
TreeMap map = new TreeMap(); |
32 |
|
} |
33 |
|
|
34 |
|
/** |
35 |
< |
* clear removes all pairs |
35 |
> |
* clear removes all pairs |
36 |
|
*/ |
37 |
|
public void testClear() { |
38 |
|
TreeMap map = map5(); |
39 |
|
map.clear(); |
40 |
< |
assertEquals(map.size(), 0); |
40 |
> |
assertEquals(0, map.size()); |
41 |
|
} |
42 |
|
|
43 |
|
/** |
50 |
|
} |
51 |
|
|
52 |
|
/** |
53 |
< |
* Maps with same contents are equal |
53 |
> |
* Maps with same contents are equal |
54 |
|
*/ |
55 |
|
public void testEquals() { |
56 |
|
TreeMap map1 = map5(); |
63 |
|
} |
64 |
|
|
65 |
|
/** |
66 |
< |
* containsKey returns true for contained key |
66 |
> |
* containsKey returns true for contained key |
67 |
|
*/ |
68 |
|
public void testContainsKey() { |
69 |
|
TreeMap map = map5(); |
72 |
|
} |
73 |
|
|
74 |
|
/** |
75 |
< |
* containsValue returns true for held values |
75 |
> |
* containsValue returns true for held values |
76 |
|
*/ |
77 |
|
public void testContainsValue() { |
78 |
|
TreeMap map = map5(); |
81 |
|
} |
82 |
|
|
83 |
|
/** |
84 |
< |
* get returns the correct element at the given key, |
85 |
< |
* or null if not present |
84 |
> |
* get returns the correct element at the given key, |
85 |
> |
* or null if not present |
86 |
|
*/ |
87 |
|
public void testGet() { |
88 |
|
TreeMap map = map5(); |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
< |
* isEmpty is true of empty map and false for non-empty |
95 |
> |
* isEmpty is true of empty map and false for non-empty |
96 |
|
*/ |
97 |
|
public void testIsEmpty() { |
98 |
|
TreeMap empty = new TreeMap(); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* firstKey returns first key |
105 |
> |
* firstKey returns first key |
106 |
|
*/ |
107 |
|
public void testFirstKey() { |
108 |
|
TreeMap map = map5(); |
110 |
|
} |
111 |
|
|
112 |
|
/** |
113 |
< |
* lastKey returns last key |
113 |
> |
* lastKey returns last key |
114 |
|
*/ |
115 |
|
public void testLastKey() { |
116 |
|
TreeMap map = map5(); |
117 |
|
assertEquals(five, map.lastKey()); |
118 |
|
} |
119 |
|
|
122 |
– |
|
120 |
|
/** |
121 |
< |
* keySet.toArray returns contains all keys |
121 |
> |
* keySet.toArray returns contains all keys |
122 |
|
*/ |
123 |
|
public void testKeySetToArray() { |
124 |
|
TreeMap map = map5(); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* descendingkeySet.toArray returns contains all keys |
134 |
> |
* descendingkeySet.toArray returns contains all keys |
135 |
|
*/ |
136 |
|
public void testDescendingKeySetToArray() { |
137 |
|
TreeMap map = map5(); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
147 |
< |
* keySet returns a Set containing all the keys |
147 |
> |
* keySet returns a Set containing all the keys |
148 |
|
*/ |
149 |
|
public void testKeySet() { |
150 |
|
TreeMap map = map5(); |
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
< |
* keySet is ordered |
161 |
> |
* keySet is ordered |
162 |
|
*/ |
163 |
|
public void testKeySetOrder() { |
164 |
|
TreeMap map = map5(); |
173 |
|
last = k; |
174 |
|
++count; |
175 |
|
} |
176 |
< |
assertEquals(count ,5); |
176 |
> |
assertEquals(5, count); |
177 |
|
} |
178 |
|
|
179 |
|
/** |
192 |
|
last = k; |
193 |
|
++count; |
194 |
|
} |
195 |
< |
assertEquals(count ,5); |
195 |
> |
assertEquals(5, count); |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
< |
* descendingKeySet is ordered |
199 |
> |
* descendingKeySet is ordered |
200 |
|
*/ |
201 |
|
public void testDescendingKeySetOrder() { |
202 |
|
TreeMap map = map5(); |
211 |
|
last = k; |
212 |
|
++count; |
213 |
|
} |
214 |
< |
assertEquals(count, 5); |
214 |
> |
assertEquals(5, count); |
215 |
|
} |
216 |
|
|
217 |
|
/** |
218 |
< |
* descending iterator of descendingKeySet is ordered |
218 |
> |
* descending iterator of descendingKeySet is ordered |
219 |
|
*/ |
220 |
|
public void testDescendingKeySetDescendingIteratorOrder() { |
221 |
|
TreeMap map = map5(); |
230 |
|
last = k; |
231 |
|
++count; |
232 |
|
} |
233 |
< |
assertEquals(count, 5); |
233 |
> |
assertEquals(5, count); |
234 |
|
} |
235 |
|
|
236 |
|
/** |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* entrySet.toArray contains all entries |
289 |
> |
* entrySet.toArray contains all entries |
290 |
|
*/ |
291 |
|
public void testEntrySetToArray() { |
292 |
|
TreeMap map = map5(); |
300 |
|
} |
301 |
|
|
302 |
|
/** |
303 |
< |
* descendingEntrySet.toArray contains all entries |
303 |
> |
* descendingEntrySet.toArray contains all entries |
304 |
|
*/ |
305 |
|
public void testDescendingEntrySetToArray() { |
306 |
|
TreeMap map = map5(); |
314 |
|
} |
315 |
|
|
316 |
|
/** |
317 |
< |
* putAll adds all key-value pairs from the given map |
317 |
> |
* putAll adds all key-value pairs from the given map |
318 |
|
*/ |
319 |
|
public void testPutAll() { |
320 |
|
TreeMap empty = new TreeMap(); |
329 |
|
} |
330 |
|
|
331 |
|
/** |
332 |
< |
* remove removes the correct key-value pair from the map |
332 |
> |
* remove removes the correct key-value pair from the map |
333 |
|
*/ |
334 |
|
public void testRemove() { |
335 |
|
TreeMap map = map5(); |
410 |
|
assertNull(e4); |
411 |
|
} |
412 |
|
|
416 |
– |
|
413 |
|
/** |
414 |
|
* lowerKey returns preceding element |
415 |
|
*/ |
537 |
|
} |
538 |
|
|
539 |
|
/** |
540 |
< |
* size returns the correct values |
540 |
> |
* size returns the correct values |
541 |
|
*/ |
542 |
|
public void testSize() { |
543 |
|
TreeMap map = map5(); |
553 |
|
TreeMap map = map5(); |
554 |
|
String s = map.toString(); |
555 |
|
for (int i = 1; i <= 5; ++i) { |
556 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
556 |
> |
assertTrue(s.contains(String.valueOf(i))); |
557 |
|
} |
558 |
|
} |
559 |
|
|
597 |
|
* A deserialized map equals original |
598 |
|
*/ |
599 |
|
public void testSerialization() throws Exception { |
600 |
< |
TreeMap q = map5(); |
600 |
> |
NavigableMap x = map5(); |
601 |
> |
NavigableMap y = serialClone(x); |
602 |
|
|
603 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
604 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
605 |
< |
out.writeObject(q); |
606 |
< |
out.close(); |
607 |
< |
|
611 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
612 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
613 |
< |
TreeMap r = (TreeMap)in.readObject(); |
614 |
< |
assertEquals(q.size(), r.size()); |
615 |
< |
assertTrue(q.equals(r)); |
616 |
< |
assertTrue(r.equals(q)); |
603 |
> |
assertTrue(x != y); |
604 |
> |
assertEquals(x.size(), y.size()); |
605 |
> |
assertEquals(x.toString(), y.toString()); |
606 |
> |
assertEquals(x, y); |
607 |
> |
assertEquals(y, x); |
608 |
|
} |
609 |
|
|
610 |
|
/** |
771 |
|
* Submaps of submaps subdivide correctly |
772 |
|
*/ |
773 |
|
public void testRecursiveSubMaps() throws Exception { |
774 |
< |
int mapSize = 1000; |
774 |
> |
int mapSize = expensiveTests ? 1000 : 100; |
775 |
|
Class cl = TreeMap.class; |
776 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
777 |
|
bs = new BitSet(mapSize); |
791 |
|
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
792 |
|
NavigableMap<Integer, Integer> result |
793 |
|
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
794 |
< |
assertEquals(result.size(), 0); |
794 |
> |
assertEquals(0, result.size()); |
795 |
|
assertFalse(result.keySet().iterator().hasNext()); |
796 |
|
return result; |
797 |
|
} |
952 |
|
*/ |
953 |
|
void check(NavigableMap<Integer, Integer> map, |
954 |
|
final int min, final int max, final boolean ascending) { |
955 |
< |
class ReferenceSet { |
955 |
> |
class ReferenceSet { |
956 |
|
int lower(int key) { |
957 |
|
return ascending ? lowerAscending(key) : higherAscending(key); |
958 |
|
} |
983 |
|
// BitSet should support this! Test would run much faster |
984 |
|
while (key >= min) { |
985 |
|
if (bs.get(key)) |
986 |
< |
return(key); |
986 |
> |
return key; |
987 |
|
key--; |
988 |
|
} |
989 |
|
return -1; |
1018 |
|
if (bsContainsI) |
1019 |
|
size++; |
1020 |
|
} |
1021 |
< |
assertEquals(map.size(), size); |
1021 |
> |
assertEquals(size, map.size()); |
1022 |
|
|
1023 |
|
// Test contents using contains keySet iterator |
1024 |
|
int size2 = 0; |