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 boolean isConcurrent() { return false; } |
30 |
> |
public boolean permitsNullKeys() { return false; } |
31 |
> |
public boolean permitsNullValues() { return true; } |
32 |
> |
public boolean supportsSetValue() { return true; } |
33 |
> |
} |
34 |
> |
return newTestSuite( |
35 |
> |
TreeMapTest.class, |
36 |
> |
MapTest.testSuite(new Implementation())); |
37 |
|
} |
38 |
|
|
39 |
|
/** |
40 |
< |
* Create a map from Integers 1-5 to Strings "A"-"E". |
40 |
> |
* Returns a new map from Integers 1-5 to Strings "A"-"E". |
41 |
|
*/ |
42 |
|
private static TreeMap map5() { |
43 |
|
TreeMap map = new TreeMap(); |
53 |
|
} |
54 |
|
|
55 |
|
/** |
56 |
< |
* clear removes all pairs |
56 |
> |
* clear removes all pairs |
57 |
|
*/ |
58 |
|
public void testClear() { |
59 |
|
TreeMap map = map5(); |
60 |
|
map.clear(); |
61 |
< |
assertEquals(map.size(), 0); |
61 |
> |
assertEquals(0, map.size()); |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* |
65 |
> |
* copy constructor creates map equal to source map |
66 |
|
*/ |
67 |
|
public void testConstructFromSorted() { |
68 |
|
TreeMap map = map5(); |
71 |
|
} |
72 |
|
|
73 |
|
/** |
74 |
< |
* Maps with same contents are equal |
74 |
> |
* Maps with same contents are equal |
75 |
|
*/ |
76 |
|
public void testEquals() { |
77 |
|
TreeMap map1 = map5(); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
< |
* containsKey returns true for contained key |
87 |
> |
* containsKey returns true for contained key |
88 |
|
*/ |
89 |
|
public void testContainsKey() { |
90 |
|
TreeMap map = map5(); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
96 |
< |
* containsValue returns true for held values |
96 |
> |
* containsValue returns true for held values |
97 |
|
*/ |
98 |
|
public void testContainsValue() { |
99 |
|
TreeMap map = map5(); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* get returns the correct element at the given key, |
106 |
< |
* or null if not present |
105 |
> |
* get returns the correct element at the given key, |
106 |
> |
* or null if not present |
107 |
|
*/ |
108 |
|
public void testGet() { |
109 |
|
TreeMap map = map5(); |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
< |
* isEmpty is true of empty map and false for non-empty |
116 |
> |
* isEmpty is true of empty map and false for non-empty |
117 |
|
*/ |
118 |
|
public void testIsEmpty() { |
119 |
|
TreeMap empty = new TreeMap(); |
123 |
|
} |
124 |
|
|
125 |
|
/** |
126 |
< |
* firstKey returns first key |
126 |
> |
* firstKey returns first key |
127 |
|
*/ |
128 |
|
public void testFirstKey() { |
129 |
|
TreeMap map = map5(); |
131 |
|
} |
132 |
|
|
133 |
|
/** |
134 |
< |
* lastKey returns last key |
134 |
> |
* lastKey returns last key |
135 |
|
*/ |
136 |
|
public void testLastKey() { |
137 |
|
TreeMap map = map5(); |
138 |
|
assertEquals(five, map.lastKey()); |
139 |
|
} |
140 |
|
|
122 |
– |
|
141 |
|
/** |
142 |
< |
* keySet.toArray returns contains all keys |
142 |
> |
* keySet.toArray returns contains all keys |
143 |
|
*/ |
144 |
|
public void testKeySetToArray() { |
145 |
|
TreeMap map = map5(); |
152 |
|
} |
153 |
|
|
154 |
|
/** |
155 |
< |
* descendingkeySet.toArray returns contains all keys |
155 |
> |
* descendingkeySet.toArray returns contains all keys |
156 |
|
*/ |
157 |
|
public void testDescendingKeySetToArray() { |
158 |
|
TreeMap map = map5(); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
< |
* keySet returns a Set containing all the keys |
168 |
> |
* keySet returns a Set containing all the keys |
169 |
|
*/ |
170 |
|
public void testKeySet() { |
171 |
|
TreeMap map = map5(); |
179 |
|
} |
180 |
|
|
181 |
|
/** |
182 |
< |
* keySet is ordered |
182 |
> |
* keySet is ordered |
183 |
|
*/ |
184 |
|
public void testKeySetOrder() { |
185 |
|
TreeMap map = map5(); |
194 |
|
last = k; |
195 |
|
++count; |
196 |
|
} |
197 |
< |
assertEquals(count ,5); |
197 |
> |
assertEquals(5, count); |
198 |
|
} |
199 |
|
|
200 |
|
/** |
213 |
|
last = k; |
214 |
|
++count; |
215 |
|
} |
216 |
< |
assertEquals(count ,5); |
216 |
> |
assertEquals(5, count); |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
< |
* descendingKeySet is ordered |
220 |
> |
* descendingKeySet is ordered |
221 |
|
*/ |
222 |
|
public void testDescendingKeySetOrder() { |
223 |
|
TreeMap map = map5(); |
232 |
|
last = k; |
233 |
|
++count; |
234 |
|
} |
235 |
< |
assertEquals(count, 5); |
235 |
> |
assertEquals(5, count); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
< |
* descending iterator of descendingKeySet is ordered |
239 |
> |
* descending iterator of descendingKeySet is ordered |
240 |
|
*/ |
241 |
|
public void testDescendingKeySetDescendingIteratorOrder() { |
242 |
|
TreeMap map = map5(); |
251 |
|
last = k; |
252 |
|
++count; |
253 |
|
} |
254 |
< |
assertEquals(count, 5); |
254 |
> |
assertEquals(5, count); |
255 |
|
} |
256 |
|
|
257 |
|
/** |
307 |
|
} |
308 |
|
|
309 |
|
/** |
310 |
< |
* entrySet.toArray contains all entries |
310 |
> |
* entrySet.toArray contains all entries |
311 |
|
*/ |
312 |
|
public void testEntrySetToArray() { |
313 |
|
TreeMap map = map5(); |
321 |
|
} |
322 |
|
|
323 |
|
/** |
324 |
< |
* descendingEntrySet.toArray contains all entries |
324 |
> |
* descendingEntrySet.toArray contains all entries |
325 |
|
*/ |
326 |
|
public void testDescendingEntrySetToArray() { |
327 |
|
TreeMap map = map5(); |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* putAll adds all key-value pairs from the given map |
338 |
> |
* putAll adds all key-value pairs from the given map |
339 |
|
*/ |
340 |
|
public void testPutAll() { |
341 |
|
TreeMap empty = new TreeMap(); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
< |
* remove removes the correct key-value pair from the map |
353 |
> |
* remove removes the correct key-value pair from the map |
354 |
|
*/ |
355 |
|
public void testRemove() { |
356 |
|
TreeMap map = map5(); |
431 |
|
assertNull(e4); |
432 |
|
} |
433 |
|
|
416 |
– |
|
434 |
|
/** |
435 |
|
* lowerKey returns preceding element |
436 |
|
*/ |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
< |
* size returns the correct values |
561 |
> |
* size returns the correct values |
562 |
|
*/ |
563 |
|
public void testSize() { |
564 |
|
TreeMap map = map5(); |
574 |
|
TreeMap map = map5(); |
575 |
|
String s = map.toString(); |
576 |
|
for (int i = 1; i <= 5; ++i) { |
577 |
< |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
577 |
> |
assertTrue(s.contains(String.valueOf(i))); |
578 |
|
} |
579 |
|
} |
580 |
|
|
584 |
|
* get(null) of nonempty map throws NPE |
585 |
|
*/ |
586 |
|
public void testGet_NullPointerException() { |
587 |
+ |
TreeMap c = map5(); |
588 |
|
try { |
571 |
– |
TreeMap c = map5(); |
589 |
|
c.get(null); |
590 |
|
shouldThrow(); |
591 |
|
} catch (NullPointerException success) {} |
595 |
|
* containsKey(null) of nonempty map throws NPE |
596 |
|
*/ |
597 |
|
public void testContainsKey_NullPointerException() { |
598 |
+ |
TreeMap c = map5(); |
599 |
|
try { |
582 |
– |
TreeMap c = map5(); |
600 |
|
c.containsKey(null); |
601 |
|
shouldThrow(); |
602 |
|
} catch (NullPointerException success) {} |
606 |
|
* remove(null) throws NPE for nonempty map |
607 |
|
*/ |
608 |
|
public void testRemove1_NullPointerException() { |
609 |
+ |
TreeMap c = new TreeMap(); |
610 |
+ |
c.put("sadsdf", "asdads"); |
611 |
|
try { |
593 |
– |
TreeMap c = new TreeMap(); |
594 |
– |
c.put("sadsdf", "asdads"); |
612 |
|
c.remove(null); |
613 |
|
shouldThrow(); |
614 |
|
} catch (NullPointerException success) {} |
615 |
|
} |
616 |
|
|
617 |
|
/** |
618 |
< |
* A deserialized map equals original |
618 |
> |
* A deserialized/reserialized map equals original |
619 |
|
*/ |
620 |
|
public void testSerialization() throws Exception { |
621 |
< |
TreeMap q = map5(); |
621 |
> |
NavigableMap x = map5(); |
622 |
> |
NavigableMap y = serialClone(x); |
623 |
|
|
624 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
625 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
626 |
< |
out.writeObject(q); |
627 |
< |
out.close(); |
628 |
< |
|
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)); |
624 |
> |
assertNotSame(x, y); |
625 |
> |
assertEquals(x.size(), y.size()); |
626 |
> |
assertEquals(x.toString(), y.toString()); |
627 |
> |
assertEquals(x, y); |
628 |
> |
assertEquals(y, x); |
629 |
|
} |
630 |
|
|
631 |
|
/** |
792 |
|
* Submaps of submaps subdivide correctly |
793 |
|
*/ |
794 |
|
public void testRecursiveSubMaps() throws Exception { |
795 |
< |
int mapSize = 1000; |
795 |
> |
int mapSize = expensiveTests ? 1000 : 100; |
796 |
|
Class cl = TreeMap.class; |
797 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
798 |
|
bs = new BitSet(mapSize); |
811 |
|
|
812 |
|
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
813 |
|
NavigableMap<Integer, Integer> result |
814 |
< |
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
815 |
< |
assertEquals(result.size(), 0); |
814 |
> |
= (NavigableMap<Integer, Integer>) cl.getConstructor().newInstance(); |
815 |
> |
assertEquals(0, result.size()); |
816 |
|
assertFalse(result.keySet().iterator().hasNext()); |
817 |
|
return result; |
818 |
|
} |
844 |
|
// Add entries till we're back to original size |
845 |
|
while (map.size() < size) { |
846 |
|
int key = min + rnd.nextInt(rangeSize); |
847 |
< |
assertTrue(key >= min && key<= max); |
847 |
> |
assertTrue(key >= min && key <= max); |
848 |
|
put(map, key); |
849 |
|
} |
850 |
|
} |
869 |
|
// Add entries till we're back to original size |
870 |
|
while (map.size() < size) { |
871 |
|
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
872 |
< |
if (key >= min && key<= max) { |
872 |
> |
if (key >= min && key <= max) { |
873 |
|
put(map, key); |
874 |
|
} else { |
875 |
|
try { |
973 |
|
*/ |
974 |
|
void check(NavigableMap<Integer, Integer> map, |
975 |
|
final int min, final int max, final boolean ascending) { |
976 |
< |
class ReferenceSet { |
976 |
> |
class ReferenceSet { |
977 |
|
int lower(int key) { |
978 |
|
return ascending ? lowerAscending(key) : higherAscending(key); |
979 |
|
} |
1004 |
|
// BitSet should support this! Test would run much faster |
1005 |
|
while (key >= min) { |
1006 |
|
if (bs.get(key)) |
1007 |
< |
return(key); |
1007 |
> |
return key; |
1008 |
|
key--; |
1009 |
|
} |
1010 |
|
return -1; |
1039 |
|
if (bsContainsI) |
1040 |
|
size++; |
1041 |
|
} |
1042 |
< |
assertEquals(map.size(), size); |
1042 |
> |
assertEquals(size, map.size()); |
1043 |
|
|
1044 |
|
// Test contents using contains keySet iterator |
1045 |
|
int size2 = 0; |