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.BitSet; |
9 |
> |
import java.util.Collection; |
10 |
> |
import java.util.Iterator; |
11 |
> |
import java.util.Map; |
12 |
> |
import java.util.NavigableMap; |
13 |
> |
import java.util.NavigableSet; |
14 |
> |
import java.util.NoSuchElementException; |
15 |
> |
import java.util.Random; |
16 |
> |
import java.util.Set; |
17 |
> |
import java.util.TreeMap; |
18 |
> |
|
19 |
> |
import junit.framework.Test; |
20 |
|
|
21 |
|
public class TreeMapTest extends JSR166TestCase { |
22 |
|
public static void main(String[] args) { |
23 |
< |
junit.textui.TestRunner.run(suite()); |
23 |
> |
main(suite(), args); |
24 |
|
} |
25 |
|
public static Test suite() { |
26 |
< |
return new TestSuite(TreeMapTest.class); |
26 |
> |
class Implementation implements MapImplementation { |
27 |
> |
public Class<?> klazz() { return TreeMap.class; } |
28 |
> |
public Map emptyMap() { return new TreeMap(); } |
29 |
> |
public Object makeKey(int i) { return i; } |
30 |
> |
public Object makeValue(int i) { return i; } |
31 |
> |
public boolean isConcurrent() { return false; } |
32 |
> |
public boolean permitsNullKeys() { return false; } |
33 |
> |
public boolean permitsNullValues() { return true; } |
34 |
> |
public boolean supportsSetValue() { return true; } |
35 |
> |
} |
36 |
> |
return newTestSuite( |
37 |
> |
TreeMapTest.class, |
38 |
> |
MapTest.testSuite(new Implementation())); |
39 |
|
} |
40 |
|
|
41 |
|
/** |
42 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
42 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
43 |
|
*/ |
44 |
|
private static TreeMap map5() { |
45 |
|
TreeMap map = new TreeMap(); |
55 |
|
} |
56 |
|
|
57 |
|
/** |
58 |
< |
* clear removes all pairs |
58 |
> |
* clear removes all pairs |
59 |
|
*/ |
60 |
|
public void testClear() { |
61 |
|
TreeMap map = map5(); |
62 |
|
map.clear(); |
63 |
< |
assertEquals(map.size(), 0); |
63 |
> |
assertEquals(0, map.size()); |
64 |
|
} |
65 |
|
|
66 |
|
/** |
67 |
< |
* |
67 |
> |
* copy constructor creates map equal to source map |
68 |
|
*/ |
69 |
|
public void testConstructFromSorted() { |
70 |
|
TreeMap map = map5(); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
< |
* Maps with same contents are equal |
76 |
> |
* Maps with same contents are equal |
77 |
|
*/ |
78 |
|
public void testEquals() { |
79 |
|
TreeMap map1 = map5(); |
86 |
|
} |
87 |
|
|
88 |
|
/** |
89 |
< |
* containsKey returns true for contained key |
89 |
> |
* containsKey returns true for contained key |
90 |
|
*/ |
91 |
|
public void testContainsKey() { |
92 |
|
TreeMap map = map5(); |
95 |
|
} |
96 |
|
|
97 |
|
/** |
98 |
< |
* containsValue returns true for held values |
98 |
> |
* containsValue returns true for held values |
99 |
|
*/ |
100 |
|
public void testContainsValue() { |
101 |
|
TreeMap map = map5(); |
104 |
|
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* get returns the correct element at the given key, |
108 |
< |
* or null if not present |
107 |
> |
* get returns the correct element at the given key, |
108 |
> |
* or null if not present |
109 |
|
*/ |
110 |
|
public void testGet() { |
111 |
|
TreeMap map = map5(); |
115 |
|
} |
116 |
|
|
117 |
|
/** |
118 |
< |
* isEmpty is true of empty map and false for non-empty |
118 |
> |
* isEmpty is true of empty map and false for non-empty |
119 |
|
*/ |
120 |
|
public void testIsEmpty() { |
121 |
|
TreeMap empty = new TreeMap(); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* firstKey returns first key |
128 |
> |
* firstKey returns first key |
129 |
|
*/ |
130 |
|
public void testFirstKey() { |
131 |
|
TreeMap map = map5(); |
133 |
|
} |
134 |
|
|
135 |
|
/** |
136 |
< |
* lastKey returns last key |
136 |
> |
* lastKey returns last key |
137 |
|
*/ |
138 |
|
public void testLastKey() { |
139 |
|
TreeMap map = map5(); |
140 |
|
assertEquals(five, map.lastKey()); |
141 |
|
} |
142 |
|
|
122 |
– |
|
143 |
|
/** |
144 |
< |
* keySet.toArray returns contains all keys |
144 |
> |
* keySet.toArray returns contains all keys |
145 |
|
*/ |
146 |
|
public void testKeySetToArray() { |
147 |
|
TreeMap map = map5(); |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* descendingkeySet.toArray returns contains all keys |
157 |
> |
* descendingkeySet.toArray returns contains all keys |
158 |
|
*/ |
159 |
|
public void testDescendingKeySetToArray() { |
160 |
|
TreeMap map = map5(); |
167 |
|
} |
168 |
|
|
169 |
|
/** |
170 |
< |
* keySet returns a Set containing all the keys |
170 |
> |
* keySet returns a Set containing all the keys |
171 |
|
*/ |
172 |
|
public void testKeySet() { |
173 |
|
TreeMap map = map5(); |
181 |
|
} |
182 |
|
|
183 |
|
/** |
184 |
< |
* keySet is ordered |
184 |
> |
* keySet is ordered |
185 |
|
*/ |
186 |
|
public void testKeySetOrder() { |
187 |
|
TreeMap map = map5(); |
196 |
|
last = k; |
197 |
|
++count; |
198 |
|
} |
199 |
< |
assertEquals(count ,5); |
199 |
> |
assertEquals(5, count); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
215 |
|
last = k; |
216 |
|
++count; |
217 |
|
} |
218 |
< |
assertEquals(count ,5); |
218 |
> |
assertEquals(5, count); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* descendingKeySet is ordered |
222 |
> |
* descendingKeySet is ordered |
223 |
|
*/ |
224 |
|
public void testDescendingKeySetOrder() { |
225 |
|
TreeMap map = map5(); |
234 |
|
last = k; |
235 |
|
++count; |
236 |
|
} |
237 |
< |
assertEquals(count, 5); |
237 |
> |
assertEquals(5, count); |
238 |
|
} |
239 |
|
|
240 |
|
/** |
241 |
< |
* descending iterator of descendingKeySet is ordered |
241 |
> |
* descending iterator of descendingKeySet is ordered |
242 |
|
*/ |
243 |
|
public void testDescendingKeySetDescendingIteratorOrder() { |
244 |
|
TreeMap map = map5(); |
253 |
|
last = k; |
254 |
|
++count; |
255 |
|
} |
256 |
< |
assertEquals(count, 5); |
256 |
> |
assertEquals(5, count); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
309 |
|
} |
310 |
|
|
311 |
|
/** |
312 |
< |
* entrySet.toArray contains all entries |
312 |
> |
* entrySet.toArray contains all entries |
313 |
|
*/ |
314 |
|
public void testEntrySetToArray() { |
315 |
|
TreeMap map = map5(); |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
< |
* descendingEntrySet.toArray contains all entries |
326 |
> |
* descendingEntrySet.toArray contains all entries |
327 |
|
*/ |
328 |
|
public void testDescendingEntrySetToArray() { |
329 |
|
TreeMap map = map5(); |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
< |
* putAll adds all key-value pairs from the given map |
340 |
> |
* putAll adds all key-value pairs from the given map |
341 |
|
*/ |
342 |
|
public void testPutAll() { |
343 |
|
TreeMap empty = new TreeMap(); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
< |
* remove removes the correct key-value pair from the map |
355 |
> |
* remove removes the correct key-value pair from the map |
356 |
|
*/ |
357 |
|
public void testRemove() { |
358 |
|
TreeMap map = map5(); |
433 |
|
assertNull(e4); |
434 |
|
} |
435 |
|
|
416 |
– |
|
436 |
|
/** |
437 |
|
* lowerKey returns preceding element |
438 |
|
*/ |
560 |
|
} |
561 |
|
|
562 |
|
/** |
563 |
< |
* size returns the correct values |
563 |
> |
* size returns the correct values |
564 |
|
*/ |
565 |
|
public void testSize() { |
566 |
|
TreeMap map = map5(); |
576 |
|
TreeMap map = map5(); |
577 |
|
String s = map.toString(); |
578 |
|
for (int i = 1; i <= 5; ++i) { |
579 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
579 |
> |
assertTrue(s.contains(String.valueOf(i))); |
580 |
|
} |
581 |
|
} |
582 |
|
|
586 |
|
* get(null) of nonempty map throws NPE |
587 |
|
*/ |
588 |
|
public void testGet_NullPointerException() { |
589 |
+ |
TreeMap c = map5(); |
590 |
|
try { |
571 |
– |
TreeMap c = map5(); |
591 |
|
c.get(null); |
592 |
|
shouldThrow(); |
593 |
|
} catch (NullPointerException success) {} |
597 |
|
* containsKey(null) of nonempty map throws NPE |
598 |
|
*/ |
599 |
|
public void testContainsKey_NullPointerException() { |
600 |
+ |
TreeMap c = map5(); |
601 |
|
try { |
582 |
– |
TreeMap c = map5(); |
602 |
|
c.containsKey(null); |
603 |
|
shouldThrow(); |
604 |
|
} catch (NullPointerException success) {} |
608 |
|
* remove(null) throws NPE for nonempty map |
609 |
|
*/ |
610 |
|
public void testRemove1_NullPointerException() { |
611 |
+ |
TreeMap c = new TreeMap(); |
612 |
+ |
c.put("sadsdf", "asdads"); |
613 |
|
try { |
593 |
– |
TreeMap c = new TreeMap(); |
594 |
– |
c.put("sadsdf", "asdads"); |
614 |
|
c.remove(null); |
615 |
|
shouldThrow(); |
616 |
|
} catch (NullPointerException success) {} |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
< |
* A deserialized map equals original |
620 |
> |
* A deserialized/reserialized map equals original |
621 |
|
*/ |
622 |
|
public void testSerialization() throws Exception { |
623 |
< |
TreeMap q = map5(); |
623 |
> |
NavigableMap x = map5(); |
624 |
> |
NavigableMap y = serialClone(x); |
625 |
|
|
626 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
627 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
628 |
< |
out.writeObject(q); |
629 |
< |
out.close(); |
630 |
< |
|
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)); |
626 |
> |
assertNotSame(x, y); |
627 |
> |
assertEquals(x.size(), y.size()); |
628 |
> |
assertEquals(x.toString(), y.toString()); |
629 |
> |
assertEquals(x, y); |
630 |
> |
assertEquals(y, x); |
631 |
|
} |
632 |
|
|
633 |
|
/** |
794 |
|
* Submaps of submaps subdivide correctly |
795 |
|
*/ |
796 |
|
public void testRecursiveSubMaps() throws Exception { |
797 |
< |
int mapSize = 1000; |
797 |
> |
int mapSize = expensiveTests ? 1000 : 100; |
798 |
|
Class cl = TreeMap.class; |
799 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
800 |
|
bs = new BitSet(mapSize); |
813 |
|
|
814 |
|
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
815 |
|
NavigableMap<Integer, Integer> result |
816 |
< |
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
817 |
< |
assertEquals(result.size(), 0); |
816 |
> |
= (NavigableMap<Integer, Integer>) cl.getConstructor().newInstance(); |
817 |
> |
assertEquals(0, result.size()); |
818 |
|
assertFalse(result.keySet().iterator().hasNext()); |
819 |
|
return result; |
820 |
|
} |
846 |
|
// Add entries till we're back to original size |
847 |
|
while (map.size() < size) { |
848 |
|
int key = min + rnd.nextInt(rangeSize); |
849 |
< |
assertTrue(key >= min && key<= max); |
849 |
> |
assertTrue(key >= min && key <= max); |
850 |
|
put(map, key); |
851 |
|
} |
852 |
|
} |
871 |
|
// Add entries till we're back to original size |
872 |
|
while (map.size() < size) { |
873 |
|
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
874 |
< |
if (key >= min && key<= max) { |
874 |
> |
if (key >= min && key <= max) { |
875 |
|
put(map, key); |
876 |
|
} else { |
877 |
|
try { |
975 |
|
*/ |
976 |
|
void check(NavigableMap<Integer, Integer> map, |
977 |
|
final int min, final int max, final boolean ascending) { |
978 |
< |
class ReferenceSet { |
978 |
> |
class ReferenceSet { |
979 |
|
int lower(int key) { |
980 |
|
return ascending ? lowerAscending(key) : higherAscending(key); |
981 |
|
} |
1041 |
|
if (bsContainsI) |
1042 |
|
size++; |
1043 |
|
} |
1044 |
< |
assertEquals(map.size(), size); |
1044 |
> |
assertEquals(size, map.size()); |
1045 |
|
|
1046 |
|
// Test contents using contains keySet iterator |
1047 |
|
int size2 = 0; |