11 |
|
|
12 |
|
public class TreeMapTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run (suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(TreeMapTest.class); |
17 |
> |
return new TestSuite(TreeMapTest.class); |
18 |
|
} |
19 |
|
|
20 |
|
/** |
21 |
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
|
*/ |
23 |
< |
private static TreeMap map5() { |
24 |
< |
TreeMap map = new TreeMap(); |
23 |
> |
private static TreeMap map5() { |
24 |
> |
TreeMap map = new TreeMap(); |
25 |
|
assertTrue(map.isEmpty()); |
26 |
< |
map.put(one, "A"); |
27 |
< |
map.put(five, "E"); |
28 |
< |
map.put(three, "C"); |
29 |
< |
map.put(two, "B"); |
30 |
< |
map.put(four, "D"); |
26 |
> |
map.put(one, "A"); |
27 |
> |
map.put(five, "E"); |
28 |
> |
map.put(three, "C"); |
29 |
> |
map.put(two, "B"); |
30 |
> |
map.put(four, "D"); |
31 |
|
assertFalse(map.isEmpty()); |
32 |
|
assertEquals(5, map.size()); |
33 |
< |
return map; |
33 |
> |
return map; |
34 |
|
} |
35 |
|
|
36 |
|
/** |
38 |
|
*/ |
39 |
|
public void testClear() { |
40 |
|
TreeMap map = map5(); |
41 |
< |
map.clear(); |
42 |
< |
assertEquals(map.size(), 0); |
41 |
> |
map.clear(); |
42 |
> |
assertEquals(map.size(), 0); |
43 |
|
} |
44 |
|
|
45 |
|
/** |
46 |
< |
* |
46 |
> |
* |
47 |
|
*/ |
48 |
|
public void testConstructFromSorted() { |
49 |
|
TreeMap map = map5(); |
59 |
|
TreeMap map2 = map5(); |
60 |
|
assertEquals(map1, map2); |
61 |
|
assertEquals(map2, map1); |
62 |
< |
map1.clear(); |
62 |
> |
map1.clear(); |
63 |
|
assertFalse(map1.equals(map2)); |
64 |
|
assertFalse(map2.equals(map1)); |
65 |
|
} |
69 |
|
*/ |
70 |
|
public void testContainsKey() { |
71 |
|
TreeMap map = map5(); |
72 |
< |
assertTrue(map.containsKey(one)); |
72 |
> |
assertTrue(map.containsKey(one)); |
73 |
|
assertFalse(map.containsKey(zero)); |
74 |
|
} |
75 |
|
|
78 |
|
*/ |
79 |
|
public void testContainsValue() { |
80 |
|
TreeMap map = map5(); |
81 |
< |
assertTrue(map.containsValue("A")); |
81 |
> |
assertTrue(map.containsValue("A")); |
82 |
|
assertFalse(map.containsValue("Z")); |
83 |
|
} |
84 |
|
|
88 |
|
*/ |
89 |
|
public void testGet() { |
90 |
|
TreeMap map = map5(); |
91 |
< |
assertEquals("A", (String)map.get(one)); |
91 |
> |
assertEquals("A", (String)map.get(one)); |
92 |
|
TreeMap empty = new TreeMap(); |
93 |
|
assertNull(empty.get(one)); |
94 |
|
} |
99 |
|
public void testIsEmpty() { |
100 |
|
TreeMap empty = new TreeMap(); |
101 |
|
TreeMap map = map5(); |
102 |
< |
assertTrue(empty.isEmpty()); |
102 |
> |
assertTrue(empty.isEmpty()); |
103 |
|
assertFalse(map.isEmpty()); |
104 |
|
} |
105 |
|
|
108 |
|
*/ |
109 |
|
public void testFirstKey() { |
110 |
|
TreeMap map = map5(); |
111 |
< |
assertEquals(one, map.firstKey()); |
111 |
> |
assertEquals(one, map.firstKey()); |
112 |
|
} |
113 |
|
|
114 |
|
/** |
116 |
|
*/ |
117 |
|
public void testLastKey() { |
118 |
|
TreeMap map = map5(); |
119 |
< |
assertEquals(five, map.lastKey()); |
119 |
> |
assertEquals(five, map.lastKey()); |
120 |
|
} |
121 |
|
|
122 |
|
|
125 |
|
*/ |
126 |
|
public void testKeySetToArray() { |
127 |
|
TreeMap map = map5(); |
128 |
< |
Set s = map.keySet(); |
128 |
> |
Set s = map.keySet(); |
129 |
|
Object[] ar = s.toArray(); |
130 |
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
131 |
< |
assertEquals(5, ar.length); |
131 |
> |
assertEquals(5, ar.length); |
132 |
|
ar[0] = m10; |
133 |
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
134 |
|
} |
138 |
|
*/ |
139 |
|
public void testDescendingKeySetToArray() { |
140 |
|
TreeMap map = map5(); |
141 |
< |
Set s = map.descendingKeySet(); |
141 |
> |
Set s = map.descendingKeySet(); |
142 |
|
Object[] ar = s.toArray(); |
143 |
< |
assertEquals(5, ar.length); |
143 |
> |
assertEquals(5, ar.length); |
144 |
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
145 |
|
ar[0] = m10; |
146 |
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
151 |
|
*/ |
152 |
|
public void testKeySet() { |
153 |
|
TreeMap map = map5(); |
154 |
< |
Set s = map.keySet(); |
155 |
< |
assertEquals(5, s.size()); |
156 |
< |
assertTrue(s.contains(one)); |
157 |
< |
assertTrue(s.contains(two)); |
158 |
< |
assertTrue(s.contains(three)); |
159 |
< |
assertTrue(s.contains(four)); |
160 |
< |
assertTrue(s.contains(five)); |
154 |
> |
Set s = map.keySet(); |
155 |
> |
assertEquals(5, s.size()); |
156 |
> |
assertTrue(s.contains(one)); |
157 |
> |
assertTrue(s.contains(two)); |
158 |
> |
assertTrue(s.contains(three)); |
159 |
> |
assertTrue(s.contains(four)); |
160 |
> |
assertTrue(s.contains(five)); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
165 |
|
*/ |
166 |
|
public void testKeySetOrder() { |
167 |
|
TreeMap map = map5(); |
168 |
< |
Set s = map.keySet(); |
168 |
> |
Set s = map.keySet(); |
169 |
|
Iterator i = s.iterator(); |
170 |
|
Integer last = (Integer)i.next(); |
171 |
|
assertEquals(last, one); |
172 |
+ |
int count = 1; |
173 |
|
while (i.hasNext()) { |
174 |
|
Integer k = (Integer)i.next(); |
175 |
|
assertTrue(last.compareTo(k) < 0); |
176 |
|
last = k; |
177 |
+ |
++count; |
178 |
+ |
} |
179 |
+ |
assertEquals(count ,5); |
180 |
+ |
} |
181 |
+ |
|
182 |
+ |
/** |
183 |
+ |
* descending iterator of key set is inverse ordered |
184 |
+ |
*/ |
185 |
+ |
public void testKeySetDescendingIteratorOrder() { |
186 |
+ |
TreeMap map = map5(); |
187 |
+ |
NavigableSet s = map.navigableKeySet(); |
188 |
+ |
Iterator i = s.descendingIterator(); |
189 |
+ |
Integer last = (Integer)i.next(); |
190 |
+ |
assertEquals(last, five); |
191 |
+ |
int count = 1; |
192 |
+ |
while (i.hasNext()) { |
193 |
+ |
Integer k = (Integer)i.next(); |
194 |
+ |
assertTrue(last.compareTo(k) > 0); |
195 |
+ |
last = k; |
196 |
+ |
++count; |
197 |
|
} |
198 |
+ |
assertEquals(count ,5); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
203 |
|
*/ |
204 |
|
public void testDescendingKeySetOrder() { |
205 |
|
TreeMap map = map5(); |
206 |
< |
Set s = map.descendingKeySet(); |
206 |
> |
Set s = map.descendingKeySet(); |
207 |
|
Iterator i = s.iterator(); |
208 |
|
Integer last = (Integer)i.next(); |
209 |
|
assertEquals(last, five); |
210 |
+ |
int count = 1; |
211 |
|
while (i.hasNext()) { |
212 |
|
Integer k = (Integer)i.next(); |
213 |
|
assertTrue(last.compareTo(k) > 0); |
214 |
|
last = k; |
215 |
+ |
++count; |
216 |
+ |
} |
217 |
+ |
assertEquals(count, 5); |
218 |
+ |
} |
219 |
+ |
|
220 |
+ |
/** |
221 |
+ |
* descending iterator of descendingKeySet is ordered |
222 |
+ |
*/ |
223 |
+ |
public void testDescendingKeySetDescendingIteratorOrder() { |
224 |
+ |
TreeMap map = map5(); |
225 |
+ |
NavigableSet s = map.descendingKeySet(); |
226 |
+ |
Iterator i = s.descendingIterator(); |
227 |
+ |
Integer last = (Integer)i.next(); |
228 |
+ |
assertEquals(last, one); |
229 |
+ |
int count = 1; |
230 |
+ |
while (i.hasNext()) { |
231 |
+ |
Integer k = (Integer)i.next(); |
232 |
+ |
assertTrue(last.compareTo(k) < 0); |
233 |
+ |
last = k; |
234 |
+ |
++count; |
235 |
|
} |
236 |
+ |
assertEquals(count, 5); |
237 |
|
} |
238 |
|
|
239 |
|
/** |
241 |
|
*/ |
242 |
|
public void testValues() { |
243 |
|
TreeMap map = map5(); |
244 |
< |
Collection s = map.values(); |
245 |
< |
assertEquals(5, s.size()); |
246 |
< |
assertTrue(s.contains("A")); |
247 |
< |
assertTrue(s.contains("B")); |
248 |
< |
assertTrue(s.contains("C")); |
249 |
< |
assertTrue(s.contains("D")); |
250 |
< |
assertTrue(s.contains("E")); |
244 |
> |
Collection s = map.values(); |
245 |
> |
assertEquals(5, s.size()); |
246 |
> |
assertTrue(s.contains("A")); |
247 |
> |
assertTrue(s.contains("B")); |
248 |
> |
assertTrue(s.contains("C")); |
249 |
> |
assertTrue(s.contains("D")); |
250 |
> |
assertTrue(s.contains("E")); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
255 |
|
*/ |
256 |
|
public void testEntrySet() { |
257 |
|
TreeMap map = map5(); |
258 |
< |
Set s = map.entrySet(); |
259 |
< |
assertEquals(5, s.size()); |
258 |
> |
Set s = map.entrySet(); |
259 |
> |
assertEquals(5, s.size()); |
260 |
|
Iterator it = s.iterator(); |
261 |
|
while (it.hasNext()) { |
262 |
|
Map.Entry e = (Map.Entry) it.next(); |
263 |
< |
assertTrue( |
263 |
> |
assertTrue( |
264 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
265 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
266 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
274 |
|
*/ |
275 |
|
public void testDescendingEntrySet() { |
276 |
|
TreeMap map = map5(); |
277 |
< |
Set s = map.descendingMap().entrySet(); |
278 |
< |
assertEquals(5, s.size()); |
277 |
> |
Set s = map.descendingMap().entrySet(); |
278 |
> |
assertEquals(5, s.size()); |
279 |
|
Iterator it = s.iterator(); |
280 |
|
while (it.hasNext()) { |
281 |
|
Map.Entry e = (Map.Entry) it.next(); |
282 |
< |
assertTrue( |
282 |
> |
assertTrue( |
283 |
|
(e.getKey().equals(one) && e.getValue().equals("A")) || |
284 |
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
285 |
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
293 |
|
*/ |
294 |
|
public void testEntrySetToArray() { |
295 |
|
TreeMap map = map5(); |
296 |
< |
Set s = map.entrySet(); |
296 |
> |
Set s = map.entrySet(); |
297 |
|
Object[] ar = s.toArray(); |
298 |
|
assertEquals(5, ar.length); |
299 |
|
for (int i = 0; i < 5; ++i) { |
307 |
|
*/ |
308 |
|
public void testDescendingEntrySetToArray() { |
309 |
|
TreeMap map = map5(); |
310 |
< |
Set s = map.descendingMap().entrySet(); |
310 |
> |
Set s = map.descendingMap().entrySet(); |
311 |
|
Object[] ar = s.toArray(); |
312 |
|
assertEquals(5, ar.length); |
313 |
|
for (int i = 0; i < 5; ++i) { |
322 |
|
public void testPutAll() { |
323 |
|
TreeMap empty = new TreeMap(); |
324 |
|
TreeMap map = map5(); |
325 |
< |
empty.putAll(map); |
326 |
< |
assertEquals(5, empty.size()); |
327 |
< |
assertTrue(empty.containsKey(one)); |
328 |
< |
assertTrue(empty.containsKey(two)); |
329 |
< |
assertTrue(empty.containsKey(three)); |
330 |
< |
assertTrue(empty.containsKey(four)); |
331 |
< |
assertTrue(empty.containsKey(five)); |
325 |
> |
empty.putAll(map); |
326 |
> |
assertEquals(5, empty.size()); |
327 |
> |
assertTrue(empty.containsKey(one)); |
328 |
> |
assertTrue(empty.containsKey(two)); |
329 |
> |
assertTrue(empty.containsKey(three)); |
330 |
> |
assertTrue(empty.containsKey(four)); |
331 |
> |
assertTrue(empty.containsKey(five)); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
336 |
|
*/ |
337 |
|
public void testRemove() { |
338 |
|
TreeMap map = map5(); |
339 |
< |
map.remove(five); |
340 |
< |
assertEquals(4, map.size()); |
341 |
< |
assertFalse(map.containsKey(five)); |
339 |
> |
map.remove(five); |
340 |
> |
assertEquals(4, map.size()); |
341 |
> |
assertFalse(map.containsKey(five)); |
342 |
|
} |
343 |
|
|
344 |
|
/** |
556 |
|
public void testSize() { |
557 |
|
TreeMap map = map5(); |
558 |
|
TreeMap empty = new TreeMap(); |
559 |
< |
assertEquals(0, empty.size()); |
560 |
< |
assertEquals(5, map.size()); |
559 |
> |
assertEquals(0, empty.size()); |
560 |
> |
assertEquals(5, map.size()); |
561 |
|
} |
562 |
|
|
563 |
|
/** |
569 |
|
for (int i = 1; i <= 5; ++i) { |
570 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
571 |
|
} |
572 |
< |
} |
572 |
> |
} |
573 |
|
|
574 |
|
// Exception tests |
575 |
|
|
581 |
|
TreeMap c = map5(); |
582 |
|
c.get(null); |
583 |
|
shouldThrow(); |
584 |
< |
} catch(NullPointerException e){} |
584 |
> |
} catch (NullPointerException e) {} |
585 |
|
} |
586 |
|
|
587 |
|
/** |
592 |
|
TreeMap c = map5(); |
593 |
|
c.containsKey(null); |
594 |
|
shouldThrow(); |
595 |
< |
} catch(NullPointerException e){} |
595 |
> |
} catch (NullPointerException e) {} |
596 |
|
} |
597 |
|
|
598 |
|
/** |
604 |
|
c.put("sadsdf", "asdads"); |
605 |
|
c.remove(null); |
606 |
|
shouldThrow(); |
607 |
< |
} catch(NullPointerException e){} |
607 |
> |
} catch (NullPointerException e) {} |
608 |
|
} |
609 |
|
|
610 |
|
/** |
625 |
|
assertEquals(q.size(), r.size()); |
626 |
|
assertTrue(q.equals(r)); |
627 |
|
assertTrue(r.equals(q)); |
628 |
< |
} catch(Exception e){ |
628 |
> |
} catch (Exception e) { |
629 |
|
e.printStackTrace(); |
630 |
|
unexpectedException(); |
631 |
|
} |
658 |
|
k = (Integer)(r.next()); |
659 |
|
assertEquals(two, k); |
660 |
|
assertFalse(r.hasNext()); |
661 |
< |
|
661 |
> |
|
662 |
|
Iterator j = sm.keySet().iterator(); |
663 |
|
j.next(); |
664 |
|
j.remove(); |
692 |
|
k = (Integer)(r.next()); |
693 |
|
assertEquals(two, k); |
694 |
|
assertFalse(r.hasNext()); |
695 |
< |
|
695 |
> |
|
696 |
|
Iterator j = sm.keySet().iterator(); |
697 |
|
j.next(); |
698 |
|
j.remove(); |
795 |
|
* Submaps of submaps subdivide correctly |
796 |
|
*/ |
797 |
|
public void testRecursiveSubMaps() { |
798 |
< |
int mapSize = 1000; |
799 |
< |
Class cl = TreeMap.class; |
798 |
> |
int mapSize = 1000; |
799 |
> |
Class cl = TreeMap.class; |
800 |
|
NavigableMap<Integer, Integer> map = newMap(cl); |
801 |
|
bs = new BitSet(mapSize); |
802 |
|
|
814 |
|
|
815 |
|
static NavigableMap<Integer, Integer> newMap(Class cl) { |
816 |
|
NavigableMap<Integer, Integer> result = null; |
817 |
< |
try { |
817 |
> |
try { |
818 |
|
result = (NavigableMap<Integer, Integer>) cl.newInstance(); |
819 |
< |
} catch(Exception e) { |
819 |
> |
} catch (Exception e) { |
820 |
|
fail(); |
821 |
< |
} |
821 |
> |
} |
822 |
|
assertEquals(result.size(), 0); |
823 |
|
assertFalse(result.keySet().iterator().hasNext()); |
824 |
|
return result; |
841 |
|
} |
842 |
|
|
843 |
|
// Remove a bunch of entries with iterator |
844 |
< |
for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
844 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
845 |
|
if (rnd.nextBoolean()) { |
846 |
|
bs.clear(it.next()); |
847 |
|
it.remove(); |
866 |
|
} |
867 |
|
|
868 |
|
// Remove a bunch of entries with iterator |
869 |
< |
for(Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
869 |
> |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
870 |
|
if (rnd.nextBoolean()) { |
871 |
|
bs.clear(it.next()); |
872 |
|
it.remove(); |
882 |
|
try { |
883 |
|
map.put(key, 2 * key); |
884 |
|
fail(); |
885 |
< |
} catch(IllegalArgumentException e) { |
885 |
> |
} catch (IllegalArgumentException e) { |
886 |
|
// expected |
887 |
|
} |
888 |
|
} |
1080 |
|
try { |
1081 |
|
map.firstKey(); |
1082 |
|
fail(); |
1083 |
< |
} catch(NoSuchElementException e) { |
1083 |
> |
} catch (NoSuchElementException e) { |
1084 |
|
// expected |
1085 |
|
} |
1086 |
|
try { |
1087 |
|
map.lastKey(); |
1088 |
|
fail(); |
1089 |
< |
} catch(NoSuchElementException e) { |
1089 |
> |
} catch (NoSuchElementException e) { |
1090 |
|
// expected |
1091 |
|
} |
1092 |
|
} |
1102 |
|
static boolean eq(Integer i, int j) { |
1103 |
|
return i == null ? j == -1 : i == j; |
1104 |
|
} |
1105 |
< |
|
1105 |
> |
|
1106 |
|
} |