1 |
dl |
1.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 |
jsr166 |
1.18 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
dl |
1.1 |
*/ |
6 |
|
|
|
7 |
|
|
import junit.framework.*; |
8 |
|
|
import java.util.*; |
9 |
|
|
|
10 |
|
|
public class TreeMapTest extends JSR166TestCase { |
11 |
|
|
public static void main(String[] args) { |
12 |
jsr166 |
1.14 |
junit.textui.TestRunner.run(suite()); |
13 |
dl |
1.1 |
} |
14 |
|
|
public static Test suite() { |
15 |
jsr166 |
1.9 |
return new TestSuite(TreeMapTest.class); |
16 |
dl |
1.1 |
} |
17 |
|
|
|
18 |
|
|
/** |
19 |
|
|
* Create a map from Integers 1-5 to Strings "A"-"E". |
20 |
|
|
*/ |
21 |
jsr166 |
1.6 |
private static TreeMap map5() { |
22 |
jsr166 |
1.9 |
TreeMap map = new TreeMap(); |
23 |
dl |
1.1 |
assertTrue(map.isEmpty()); |
24 |
jsr166 |
1.9 |
map.put(one, "A"); |
25 |
|
|
map.put(five, "E"); |
26 |
|
|
map.put(three, "C"); |
27 |
|
|
map.put(two, "B"); |
28 |
|
|
map.put(four, "D"); |
29 |
dl |
1.1 |
assertFalse(map.isEmpty()); |
30 |
|
|
assertEquals(5, map.size()); |
31 |
jsr166 |
1.9 |
return map; |
32 |
dl |
1.1 |
} |
33 |
|
|
|
34 |
|
|
/** |
35 |
jsr166 |
1.16 |
* clear removes all pairs |
36 |
dl |
1.1 |
*/ |
37 |
|
|
public void testClear() { |
38 |
|
|
TreeMap map = map5(); |
39 |
jsr166 |
1.9 |
map.clear(); |
40 |
jsr166 |
1.22 |
assertEquals(0, map.size()); |
41 |
dl |
1.1 |
} |
42 |
|
|
|
43 |
|
|
/** |
44 |
jsr166 |
1.6 |
* |
45 |
dl |
1.1 |
*/ |
46 |
|
|
public void testConstructFromSorted() { |
47 |
|
|
TreeMap map = map5(); |
48 |
|
|
TreeMap map2 = new TreeMap(map); |
49 |
|
|
assertEquals(map, map2); |
50 |
|
|
} |
51 |
|
|
|
52 |
|
|
/** |
53 |
jsr166 |
1.16 |
* Maps with same contents are equal |
54 |
dl |
1.1 |
*/ |
55 |
|
|
public void testEquals() { |
56 |
|
|
TreeMap map1 = map5(); |
57 |
|
|
TreeMap map2 = map5(); |
58 |
|
|
assertEquals(map1, map2); |
59 |
|
|
assertEquals(map2, map1); |
60 |
jsr166 |
1.9 |
map1.clear(); |
61 |
dl |
1.1 |
assertFalse(map1.equals(map2)); |
62 |
|
|
assertFalse(map2.equals(map1)); |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
/** |
66 |
jsr166 |
1.16 |
* containsKey returns true for contained key |
67 |
dl |
1.1 |
*/ |
68 |
|
|
public void testContainsKey() { |
69 |
|
|
TreeMap map = map5(); |
70 |
jsr166 |
1.9 |
assertTrue(map.containsKey(one)); |
71 |
dl |
1.1 |
assertFalse(map.containsKey(zero)); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
/** |
75 |
jsr166 |
1.16 |
* containsValue returns true for held values |
76 |
dl |
1.1 |
*/ |
77 |
|
|
public void testContainsValue() { |
78 |
|
|
TreeMap map = map5(); |
79 |
jsr166 |
1.9 |
assertTrue(map.containsValue("A")); |
80 |
dl |
1.1 |
assertFalse(map.containsValue("Z")); |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
/** |
84 |
jsr166 |
1.16 |
* get returns the correct element at the given key, |
85 |
|
|
* or null if not present |
86 |
dl |
1.1 |
*/ |
87 |
|
|
public void testGet() { |
88 |
|
|
TreeMap map = map5(); |
89 |
jsr166 |
1.9 |
assertEquals("A", (String)map.get(one)); |
90 |
dl |
1.1 |
TreeMap empty = new TreeMap(); |
91 |
|
|
assertNull(empty.get(one)); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
/** |
95 |
jsr166 |
1.16 |
* isEmpty is true of empty map and false for non-empty |
96 |
dl |
1.1 |
*/ |
97 |
|
|
public void testIsEmpty() { |
98 |
|
|
TreeMap empty = new TreeMap(); |
99 |
|
|
TreeMap map = map5(); |
100 |
jsr166 |
1.9 |
assertTrue(empty.isEmpty()); |
101 |
dl |
1.1 |
assertFalse(map.isEmpty()); |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
/** |
105 |
jsr166 |
1.16 |
* firstKey returns first key |
106 |
dl |
1.1 |
*/ |
107 |
|
|
public void testFirstKey() { |
108 |
|
|
TreeMap map = map5(); |
109 |
jsr166 |
1.9 |
assertEquals(one, map.firstKey()); |
110 |
dl |
1.1 |
} |
111 |
|
|
|
112 |
|
|
/** |
113 |
jsr166 |
1.16 |
* lastKey returns last key |
114 |
dl |
1.1 |
*/ |
115 |
|
|
public void testLastKey() { |
116 |
|
|
TreeMap map = map5(); |
117 |
jsr166 |
1.9 |
assertEquals(five, map.lastKey()); |
118 |
dl |
1.1 |
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
jsr166 |
1.16 |
* keySet.toArray returns contains all keys |
122 |
dl |
1.1 |
*/ |
123 |
|
|
public void testKeySetToArray() { |
124 |
|
|
TreeMap map = map5(); |
125 |
jsr166 |
1.9 |
Set s = map.keySet(); |
126 |
dl |
1.1 |
Object[] ar = s.toArray(); |
127 |
|
|
assertTrue(s.containsAll(Arrays.asList(ar))); |
128 |
jsr166 |
1.9 |
assertEquals(5, ar.length); |
129 |
dl |
1.1 |
ar[0] = m10; |
130 |
|
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
/** |
134 |
jsr166 |
1.16 |
* descendingkeySet.toArray returns contains all keys |
135 |
dl |
1.1 |
*/ |
136 |
|
|
public void testDescendingKeySetToArray() { |
137 |
|
|
TreeMap map = map5(); |
138 |
jsr166 |
1.9 |
Set s = map.descendingKeySet(); |
139 |
dl |
1.1 |
Object[] ar = s.toArray(); |
140 |
jsr166 |
1.9 |
assertEquals(5, ar.length); |
141 |
dl |
1.1 |
assertTrue(s.containsAll(Arrays.asList(ar))); |
142 |
|
|
ar[0] = m10; |
143 |
|
|
assertFalse(s.containsAll(Arrays.asList(ar))); |
144 |
|
|
} |
145 |
|
|
|
146 |
|
|
/** |
147 |
jsr166 |
1.16 |
* keySet returns a Set containing all the keys |
148 |
dl |
1.1 |
*/ |
149 |
|
|
public void testKeySet() { |
150 |
|
|
TreeMap map = map5(); |
151 |
jsr166 |
1.9 |
Set s = map.keySet(); |
152 |
|
|
assertEquals(5, s.size()); |
153 |
|
|
assertTrue(s.contains(one)); |
154 |
|
|
assertTrue(s.contains(two)); |
155 |
|
|
assertTrue(s.contains(three)); |
156 |
|
|
assertTrue(s.contains(four)); |
157 |
|
|
assertTrue(s.contains(five)); |
158 |
dl |
1.1 |
} |
159 |
|
|
|
160 |
|
|
/** |
161 |
jsr166 |
1.16 |
* keySet is ordered |
162 |
dl |
1.1 |
*/ |
163 |
|
|
public void testKeySetOrder() { |
164 |
|
|
TreeMap map = map5(); |
165 |
jsr166 |
1.9 |
Set s = map.keySet(); |
166 |
dl |
1.1 |
Iterator i = s.iterator(); |
167 |
|
|
Integer last = (Integer)i.next(); |
168 |
|
|
assertEquals(last, one); |
169 |
dl |
1.5 |
int count = 1; |
170 |
dl |
1.1 |
while (i.hasNext()) { |
171 |
|
|
Integer k = (Integer)i.next(); |
172 |
|
|
assertTrue(last.compareTo(k) < 0); |
173 |
|
|
last = k; |
174 |
dl |
1.5 |
++count; |
175 |
dl |
1.1 |
} |
176 |
jsr166 |
1.22 |
assertEquals(5, count); |
177 |
dl |
1.5 |
} |
178 |
|
|
|
179 |
|
|
/** |
180 |
|
|
* descending iterator of key set is inverse ordered |
181 |
|
|
*/ |
182 |
|
|
public void testKeySetDescendingIteratorOrder() { |
183 |
|
|
TreeMap map = map5(); |
184 |
jsr166 |
1.9 |
NavigableSet s = map.navigableKeySet(); |
185 |
dl |
1.5 |
Iterator i = s.descendingIterator(); |
186 |
|
|
Integer last = (Integer)i.next(); |
187 |
|
|
assertEquals(last, five); |
188 |
|
|
int count = 1; |
189 |
|
|
while (i.hasNext()) { |
190 |
|
|
Integer k = (Integer)i.next(); |
191 |
|
|
assertTrue(last.compareTo(k) > 0); |
192 |
|
|
last = k; |
193 |
|
|
++count; |
194 |
|
|
} |
195 |
jsr166 |
1.22 |
assertEquals(5, count); |
196 |
dl |
1.1 |
} |
197 |
|
|
|
198 |
|
|
/** |
199 |
jsr166 |
1.16 |
* descendingKeySet is ordered |
200 |
dl |
1.1 |
*/ |
201 |
|
|
public void testDescendingKeySetOrder() { |
202 |
|
|
TreeMap map = map5(); |
203 |
jsr166 |
1.9 |
Set s = map.descendingKeySet(); |
204 |
dl |
1.1 |
Iterator i = s.iterator(); |
205 |
|
|
Integer last = (Integer)i.next(); |
206 |
|
|
assertEquals(last, five); |
207 |
dl |
1.5 |
int count = 1; |
208 |
dl |
1.1 |
while (i.hasNext()) { |
209 |
|
|
Integer k = (Integer)i.next(); |
210 |
|
|
assertTrue(last.compareTo(k) > 0); |
211 |
|
|
last = k; |
212 |
dl |
1.5 |
++count; |
213 |
|
|
} |
214 |
jsr166 |
1.22 |
assertEquals(5, count); |
215 |
dl |
1.5 |
} |
216 |
|
|
|
217 |
|
|
/** |
218 |
jsr166 |
1.16 |
* descending iterator of descendingKeySet is ordered |
219 |
dl |
1.5 |
*/ |
220 |
|
|
public void testDescendingKeySetDescendingIteratorOrder() { |
221 |
|
|
TreeMap map = map5(); |
222 |
jsr166 |
1.9 |
NavigableSet s = map.descendingKeySet(); |
223 |
dl |
1.5 |
Iterator i = s.descendingIterator(); |
224 |
|
|
Integer last = (Integer)i.next(); |
225 |
|
|
assertEquals(last, one); |
226 |
|
|
int count = 1; |
227 |
|
|
while (i.hasNext()) { |
228 |
|
|
Integer k = (Integer)i.next(); |
229 |
|
|
assertTrue(last.compareTo(k) < 0); |
230 |
|
|
last = k; |
231 |
|
|
++count; |
232 |
dl |
1.1 |
} |
233 |
jsr166 |
1.22 |
assertEquals(5, count); |
234 |
dl |
1.1 |
} |
235 |
|
|
|
236 |
|
|
/** |
237 |
|
|
* values collection contains all values |
238 |
|
|
*/ |
239 |
|
|
public void testValues() { |
240 |
|
|
TreeMap map = map5(); |
241 |
jsr166 |
1.9 |
Collection s = map.values(); |
242 |
|
|
assertEquals(5, s.size()); |
243 |
|
|
assertTrue(s.contains("A")); |
244 |
|
|
assertTrue(s.contains("B")); |
245 |
|
|
assertTrue(s.contains("C")); |
246 |
|
|
assertTrue(s.contains("D")); |
247 |
|
|
assertTrue(s.contains("E")); |
248 |
dl |
1.1 |
} |
249 |
|
|
|
250 |
|
|
/** |
251 |
|
|
* entrySet contains all pairs |
252 |
|
|
*/ |
253 |
|
|
public void testEntrySet() { |
254 |
|
|
TreeMap map = map5(); |
255 |
jsr166 |
1.9 |
Set s = map.entrySet(); |
256 |
|
|
assertEquals(5, s.size()); |
257 |
dl |
1.1 |
Iterator it = s.iterator(); |
258 |
|
|
while (it.hasNext()) { |
259 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
260 |
jsr166 |
1.6 |
assertTrue( |
261 |
dl |
1.1 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
262 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
263 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
264 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
265 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
266 |
|
|
} |
267 |
|
|
} |
268 |
|
|
|
269 |
|
|
/** |
270 |
|
|
* descendingEntrySet contains all pairs |
271 |
|
|
*/ |
272 |
|
|
public void testDescendingEntrySet() { |
273 |
|
|
TreeMap map = map5(); |
274 |
jsr166 |
1.9 |
Set s = map.descendingMap().entrySet(); |
275 |
|
|
assertEquals(5, s.size()); |
276 |
dl |
1.1 |
Iterator it = s.iterator(); |
277 |
|
|
while (it.hasNext()) { |
278 |
|
|
Map.Entry e = (Map.Entry) it.next(); |
279 |
jsr166 |
1.6 |
assertTrue( |
280 |
dl |
1.1 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
281 |
|
|
(e.getKey().equals(two) && e.getValue().equals("B")) || |
282 |
|
|
(e.getKey().equals(three) && e.getValue().equals("C")) || |
283 |
|
|
(e.getKey().equals(four) && e.getValue().equals("D")) || |
284 |
|
|
(e.getKey().equals(five) && e.getValue().equals("E"))); |
285 |
|
|
} |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
/** |
289 |
jsr166 |
1.16 |
* entrySet.toArray contains all entries |
290 |
dl |
1.1 |
*/ |
291 |
|
|
public void testEntrySetToArray() { |
292 |
|
|
TreeMap map = map5(); |
293 |
jsr166 |
1.9 |
Set s = map.entrySet(); |
294 |
dl |
1.1 |
Object[] ar = s.toArray(); |
295 |
|
|
assertEquals(5, ar.length); |
296 |
|
|
for (int i = 0; i < 5; ++i) { |
297 |
|
|
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
298 |
|
|
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
299 |
|
|
} |
300 |
|
|
} |
301 |
|
|
|
302 |
|
|
/** |
303 |
jsr166 |
1.16 |
* descendingEntrySet.toArray contains all entries |
304 |
dl |
1.1 |
*/ |
305 |
|
|
public void testDescendingEntrySetToArray() { |
306 |
|
|
TreeMap map = map5(); |
307 |
jsr166 |
1.9 |
Set s = map.descendingMap().entrySet(); |
308 |
dl |
1.1 |
Object[] ar = s.toArray(); |
309 |
|
|
assertEquals(5, ar.length); |
310 |
|
|
for (int i = 0; i < 5; ++i) { |
311 |
|
|
assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey())); |
312 |
|
|
assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue())); |
313 |
|
|
} |
314 |
|
|
} |
315 |
|
|
|
316 |
|
|
/** |
317 |
jsr166 |
1.16 |
* putAll adds all key-value pairs from the given map |
318 |
dl |
1.1 |
*/ |
319 |
|
|
public void testPutAll() { |
320 |
|
|
TreeMap empty = new TreeMap(); |
321 |
|
|
TreeMap map = map5(); |
322 |
jsr166 |
1.9 |
empty.putAll(map); |
323 |
|
|
assertEquals(5, empty.size()); |
324 |
|
|
assertTrue(empty.containsKey(one)); |
325 |
|
|
assertTrue(empty.containsKey(two)); |
326 |
|
|
assertTrue(empty.containsKey(three)); |
327 |
|
|
assertTrue(empty.containsKey(four)); |
328 |
|
|
assertTrue(empty.containsKey(five)); |
329 |
dl |
1.1 |
} |
330 |
|
|
|
331 |
|
|
/** |
332 |
jsr166 |
1.16 |
* remove removes the correct key-value pair from the map |
333 |
dl |
1.1 |
*/ |
334 |
|
|
public void testRemove() { |
335 |
|
|
TreeMap map = map5(); |
336 |
jsr166 |
1.9 |
map.remove(five); |
337 |
|
|
assertEquals(4, map.size()); |
338 |
|
|
assertFalse(map.containsKey(five)); |
339 |
dl |
1.1 |
} |
340 |
|
|
|
341 |
|
|
/** |
342 |
|
|
* lowerEntry returns preceding entry. |
343 |
|
|
*/ |
344 |
|
|
public void testLowerEntry() { |
345 |
|
|
TreeMap map = map5(); |
346 |
|
|
Map.Entry e1 = map.lowerEntry(three); |
347 |
|
|
assertEquals(two, e1.getKey()); |
348 |
|
|
|
349 |
|
|
Map.Entry e2 = map.lowerEntry(six); |
350 |
|
|
assertEquals(five, e2.getKey()); |
351 |
|
|
|
352 |
|
|
Map.Entry e3 = map.lowerEntry(one); |
353 |
|
|
assertNull(e3); |
354 |
|
|
|
355 |
|
|
Map.Entry e4 = map.lowerEntry(zero); |
356 |
|
|
assertNull(e4); |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
/** |
360 |
|
|
* higherEntry returns next entry. |
361 |
|
|
*/ |
362 |
|
|
public void testHigherEntry() { |
363 |
|
|
TreeMap map = map5(); |
364 |
|
|
Map.Entry e1 = map.higherEntry(three); |
365 |
|
|
assertEquals(four, e1.getKey()); |
366 |
|
|
|
367 |
|
|
Map.Entry e2 = map.higherEntry(zero); |
368 |
|
|
assertEquals(one, e2.getKey()); |
369 |
|
|
|
370 |
|
|
Map.Entry e3 = map.higherEntry(five); |
371 |
|
|
assertNull(e3); |
372 |
|
|
|
373 |
|
|
Map.Entry e4 = map.higherEntry(six); |
374 |
|
|
assertNull(e4); |
375 |
|
|
} |
376 |
|
|
|
377 |
|
|
/** |
378 |
|
|
* floorEntry returns preceding entry. |
379 |
|
|
*/ |
380 |
|
|
public void testFloorEntry() { |
381 |
|
|
TreeMap map = map5(); |
382 |
|
|
Map.Entry e1 = map.floorEntry(three); |
383 |
|
|
assertEquals(three, e1.getKey()); |
384 |
|
|
|
385 |
|
|
Map.Entry e2 = map.floorEntry(six); |
386 |
|
|
assertEquals(five, e2.getKey()); |
387 |
|
|
|
388 |
|
|
Map.Entry e3 = map.floorEntry(one); |
389 |
|
|
assertEquals(one, e3.getKey()); |
390 |
|
|
|
391 |
|
|
Map.Entry e4 = map.floorEntry(zero); |
392 |
|
|
assertNull(e4); |
393 |
|
|
} |
394 |
|
|
|
395 |
|
|
/** |
396 |
|
|
* ceilingEntry returns next entry. |
397 |
|
|
*/ |
398 |
|
|
public void testCeilingEntry() { |
399 |
|
|
TreeMap map = map5(); |
400 |
|
|
Map.Entry e1 = map.ceilingEntry(three); |
401 |
|
|
assertEquals(three, e1.getKey()); |
402 |
|
|
|
403 |
|
|
Map.Entry e2 = map.ceilingEntry(zero); |
404 |
|
|
assertEquals(one, e2.getKey()); |
405 |
|
|
|
406 |
|
|
Map.Entry e3 = map.ceilingEntry(five); |
407 |
|
|
assertEquals(five, e3.getKey()); |
408 |
|
|
|
409 |
|
|
Map.Entry e4 = map.ceilingEntry(six); |
410 |
|
|
assertNull(e4); |
411 |
|
|
} |
412 |
|
|
|
413 |
|
|
/** |
414 |
|
|
* lowerKey returns preceding element |
415 |
|
|
*/ |
416 |
|
|
public void testLowerKey() { |
417 |
|
|
TreeMap q = map5(); |
418 |
|
|
Object e1 = q.lowerKey(three); |
419 |
|
|
assertEquals(two, e1); |
420 |
|
|
|
421 |
|
|
Object e2 = q.lowerKey(six); |
422 |
|
|
assertEquals(five, e2); |
423 |
|
|
|
424 |
|
|
Object e3 = q.lowerKey(one); |
425 |
|
|
assertNull(e3); |
426 |
|
|
|
427 |
|
|
Object e4 = q.lowerKey(zero); |
428 |
|
|
assertNull(e4); |
429 |
|
|
} |
430 |
|
|
|
431 |
|
|
/** |
432 |
|
|
* higherKey returns next element |
433 |
|
|
*/ |
434 |
|
|
public void testHigherKey() { |
435 |
|
|
TreeMap q = map5(); |
436 |
|
|
Object e1 = q.higherKey(three); |
437 |
|
|
assertEquals(four, e1); |
438 |
|
|
|
439 |
|
|
Object e2 = q.higherKey(zero); |
440 |
|
|
assertEquals(one, e2); |
441 |
|
|
|
442 |
|
|
Object e3 = q.higherKey(five); |
443 |
|
|
assertNull(e3); |
444 |
|
|
|
445 |
|
|
Object e4 = q.higherKey(six); |
446 |
|
|
assertNull(e4); |
447 |
|
|
} |
448 |
|
|
|
449 |
|
|
/** |
450 |
|
|
* floorKey returns preceding element |
451 |
|
|
*/ |
452 |
|
|
public void testFloorKey() { |
453 |
|
|
TreeMap q = map5(); |
454 |
|
|
Object e1 = q.floorKey(three); |
455 |
|
|
assertEquals(three, e1); |
456 |
|
|
|
457 |
|
|
Object e2 = q.floorKey(six); |
458 |
|
|
assertEquals(five, e2); |
459 |
|
|
|
460 |
|
|
Object e3 = q.floorKey(one); |
461 |
|
|
assertEquals(one, e3); |
462 |
|
|
|
463 |
|
|
Object e4 = q.floorKey(zero); |
464 |
|
|
assertNull(e4); |
465 |
|
|
} |
466 |
|
|
|
467 |
|
|
/** |
468 |
|
|
* ceilingKey returns next element |
469 |
|
|
*/ |
470 |
|
|
public void testCeilingKey() { |
471 |
|
|
TreeMap q = map5(); |
472 |
|
|
Object e1 = q.ceilingKey(three); |
473 |
|
|
assertEquals(three, e1); |
474 |
|
|
|
475 |
|
|
Object e2 = q.ceilingKey(zero); |
476 |
|
|
assertEquals(one, e2); |
477 |
|
|
|
478 |
|
|
Object e3 = q.ceilingKey(five); |
479 |
|
|
assertEquals(five, e3); |
480 |
|
|
|
481 |
|
|
Object e4 = q.ceilingKey(six); |
482 |
|
|
assertNull(e4); |
483 |
|
|
} |
484 |
|
|
|
485 |
|
|
/** |
486 |
|
|
* pollFirstEntry returns entries in order |
487 |
|
|
*/ |
488 |
|
|
public void testPollFirstEntry() { |
489 |
|
|
TreeMap map = map5(); |
490 |
|
|
Map.Entry e = map.pollFirstEntry(); |
491 |
|
|
assertEquals(one, e.getKey()); |
492 |
|
|
assertEquals("A", e.getValue()); |
493 |
|
|
e = map.pollFirstEntry(); |
494 |
|
|
assertEquals(two, e.getKey()); |
495 |
|
|
map.put(one, "A"); |
496 |
|
|
e = map.pollFirstEntry(); |
497 |
|
|
assertEquals(one, e.getKey()); |
498 |
|
|
assertEquals("A", e.getValue()); |
499 |
|
|
e = map.pollFirstEntry(); |
500 |
|
|
assertEquals(three, e.getKey()); |
501 |
|
|
map.remove(four); |
502 |
|
|
e = map.pollFirstEntry(); |
503 |
|
|
assertEquals(five, e.getKey()); |
504 |
|
|
try { |
505 |
|
|
e.setValue("A"); |
506 |
|
|
shouldThrow(); |
507 |
jsr166 |
1.11 |
} catch (UnsupportedOperationException success) {} |
508 |
dl |
1.1 |
e = map.pollFirstEntry(); |
509 |
|
|
assertNull(e); |
510 |
|
|
} |
511 |
|
|
|
512 |
|
|
/** |
513 |
|
|
* pollLastEntry returns entries in order |
514 |
|
|
*/ |
515 |
|
|
public void testPollLastEntry() { |
516 |
|
|
TreeMap map = map5(); |
517 |
|
|
Map.Entry e = map.pollLastEntry(); |
518 |
|
|
assertEquals(five, e.getKey()); |
519 |
|
|
assertEquals("E", e.getValue()); |
520 |
|
|
e = map.pollLastEntry(); |
521 |
|
|
assertEquals(four, e.getKey()); |
522 |
|
|
map.put(five, "E"); |
523 |
|
|
e = map.pollLastEntry(); |
524 |
|
|
assertEquals(five, e.getKey()); |
525 |
|
|
assertEquals("E", e.getValue()); |
526 |
|
|
e = map.pollLastEntry(); |
527 |
|
|
assertEquals(three, e.getKey()); |
528 |
|
|
map.remove(two); |
529 |
|
|
e = map.pollLastEntry(); |
530 |
|
|
assertEquals(one, e.getKey()); |
531 |
|
|
try { |
532 |
|
|
e.setValue("E"); |
533 |
|
|
shouldThrow(); |
534 |
jsr166 |
1.11 |
} catch (UnsupportedOperationException success) {} |
535 |
dl |
1.1 |
e = map.pollLastEntry(); |
536 |
|
|
assertNull(e); |
537 |
|
|
} |
538 |
|
|
|
539 |
|
|
/** |
540 |
jsr166 |
1.16 |
* size returns the correct values |
541 |
dl |
1.1 |
*/ |
542 |
|
|
public void testSize() { |
543 |
|
|
TreeMap map = map5(); |
544 |
|
|
TreeMap empty = new TreeMap(); |
545 |
jsr166 |
1.9 |
assertEquals(0, empty.size()); |
546 |
|
|
assertEquals(5, map.size()); |
547 |
dl |
1.1 |
} |
548 |
|
|
|
549 |
|
|
/** |
550 |
|
|
* toString contains toString of elements |
551 |
|
|
*/ |
552 |
|
|
public void testToString() { |
553 |
|
|
TreeMap map = map5(); |
554 |
|
|
String s = map.toString(); |
555 |
|
|
for (int i = 1; i <= 5; ++i) { |
556 |
jsr166 |
1.20 |
assertTrue(s.contains(String.valueOf(i))); |
557 |
dl |
1.1 |
} |
558 |
jsr166 |
1.6 |
} |
559 |
dl |
1.1 |
|
560 |
|
|
// Exception tests |
561 |
|
|
|
562 |
|
|
/** |
563 |
|
|
* get(null) of nonempty map throws NPE |
564 |
|
|
*/ |
565 |
|
|
public void testGet_NullPointerException() { |
566 |
|
|
try { |
567 |
|
|
TreeMap c = map5(); |
568 |
|
|
c.get(null); |
569 |
|
|
shouldThrow(); |
570 |
jsr166 |
1.11 |
} catch (NullPointerException success) {} |
571 |
dl |
1.1 |
} |
572 |
|
|
|
573 |
|
|
/** |
574 |
|
|
* containsKey(null) of nonempty map throws NPE |
575 |
|
|
*/ |
576 |
|
|
public void testContainsKey_NullPointerException() { |
577 |
|
|
try { |
578 |
|
|
TreeMap c = map5(); |
579 |
|
|
c.containsKey(null); |
580 |
|
|
shouldThrow(); |
581 |
jsr166 |
1.11 |
} catch (NullPointerException success) {} |
582 |
dl |
1.1 |
} |
583 |
|
|
|
584 |
|
|
/** |
585 |
|
|
* remove(null) throws NPE for nonempty map |
586 |
|
|
*/ |
587 |
|
|
public void testRemove1_NullPointerException() { |
588 |
|
|
try { |
589 |
|
|
TreeMap c = new TreeMap(); |
590 |
|
|
c.put("sadsdf", "asdads"); |
591 |
|
|
c.remove(null); |
592 |
|
|
shouldThrow(); |
593 |
jsr166 |
1.11 |
} catch (NullPointerException success) {} |
594 |
dl |
1.1 |
} |
595 |
|
|
|
596 |
|
|
/** |
597 |
|
|
* A deserialized map equals original |
598 |
|
|
*/ |
599 |
jsr166 |
1.10 |
public void testSerialization() throws Exception { |
600 |
jsr166 |
1.21 |
NavigableMap x = map5(); |
601 |
|
|
NavigableMap y = serialClone(x); |
602 |
dl |
1.1 |
|
603 |
jsr166 |
1.21 |
assertTrue(x != y); |
604 |
|
|
assertEquals(x.size(), y.size()); |
605 |
|
|
assertEquals(x.toString(), y.toString()); |
606 |
|
|
assertEquals(x, y); |
607 |
|
|
assertEquals(y, x); |
608 |
dl |
1.1 |
} |
609 |
|
|
|
610 |
|
|
/** |
611 |
|
|
* subMap returns map with keys in requested range |
612 |
|
|
*/ |
613 |
|
|
public void testSubMapContents() { |
614 |
|
|
TreeMap map = map5(); |
615 |
dl |
1.4 |
NavigableMap sm = map.subMap(two, true, four, false); |
616 |
dl |
1.1 |
assertEquals(two, sm.firstKey()); |
617 |
|
|
assertEquals(three, sm.lastKey()); |
618 |
|
|
assertEquals(2, sm.size()); |
619 |
|
|
assertFalse(sm.containsKey(one)); |
620 |
|
|
assertTrue(sm.containsKey(two)); |
621 |
|
|
assertTrue(sm.containsKey(three)); |
622 |
|
|
assertFalse(sm.containsKey(four)); |
623 |
|
|
assertFalse(sm.containsKey(five)); |
624 |
|
|
Iterator i = sm.keySet().iterator(); |
625 |
|
|
Object k; |
626 |
|
|
k = (Integer)(i.next()); |
627 |
|
|
assertEquals(two, k); |
628 |
|
|
k = (Integer)(i.next()); |
629 |
|
|
assertEquals(three, k); |
630 |
|
|
assertFalse(i.hasNext()); |
631 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
632 |
|
|
k = (Integer)(r.next()); |
633 |
|
|
assertEquals(three, k); |
634 |
|
|
k = (Integer)(r.next()); |
635 |
|
|
assertEquals(two, k); |
636 |
|
|
assertFalse(r.hasNext()); |
637 |
jsr166 |
1.6 |
|
638 |
dl |
1.1 |
Iterator j = sm.keySet().iterator(); |
639 |
|
|
j.next(); |
640 |
|
|
j.remove(); |
641 |
|
|
assertFalse(map.containsKey(two)); |
642 |
|
|
assertEquals(4, map.size()); |
643 |
|
|
assertEquals(1, sm.size()); |
644 |
|
|
assertEquals(three, sm.firstKey()); |
645 |
|
|
assertEquals(three, sm.lastKey()); |
646 |
jsr166 |
1.12 |
assertEquals("C", sm.remove(three)); |
647 |
dl |
1.1 |
assertTrue(sm.isEmpty()); |
648 |
|
|
assertEquals(3, map.size()); |
649 |
|
|
} |
650 |
|
|
|
651 |
|
|
public void testSubMapContents2() { |
652 |
|
|
TreeMap map = map5(); |
653 |
dl |
1.4 |
NavigableMap sm = map.subMap(two, true, three, false); |
654 |
dl |
1.1 |
assertEquals(1, sm.size()); |
655 |
|
|
assertEquals(two, sm.firstKey()); |
656 |
|
|
assertEquals(two, sm.lastKey()); |
657 |
|
|
assertFalse(sm.containsKey(one)); |
658 |
|
|
assertTrue(sm.containsKey(two)); |
659 |
|
|
assertFalse(sm.containsKey(three)); |
660 |
|
|
assertFalse(sm.containsKey(four)); |
661 |
|
|
assertFalse(sm.containsKey(five)); |
662 |
|
|
Iterator i = sm.keySet().iterator(); |
663 |
|
|
Object k; |
664 |
|
|
k = (Integer)(i.next()); |
665 |
|
|
assertEquals(two, k); |
666 |
|
|
assertFalse(i.hasNext()); |
667 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
668 |
|
|
k = (Integer)(r.next()); |
669 |
|
|
assertEquals(two, k); |
670 |
|
|
assertFalse(r.hasNext()); |
671 |
jsr166 |
1.6 |
|
672 |
dl |
1.1 |
Iterator j = sm.keySet().iterator(); |
673 |
|
|
j.next(); |
674 |
|
|
j.remove(); |
675 |
|
|
assertFalse(map.containsKey(two)); |
676 |
|
|
assertEquals(4, map.size()); |
677 |
|
|
assertEquals(0, sm.size()); |
678 |
|
|
assertTrue(sm.isEmpty()); |
679 |
jsr166 |
1.12 |
assertSame(sm.remove(three), null); |
680 |
dl |
1.1 |
assertEquals(4, map.size()); |
681 |
|
|
} |
682 |
|
|
|
683 |
|
|
/** |
684 |
|
|
* headMap returns map with keys in requested range |
685 |
|
|
*/ |
686 |
|
|
public void testHeadMapContents() { |
687 |
|
|
TreeMap map = map5(); |
688 |
dl |
1.4 |
NavigableMap sm = map.headMap(four, false); |
689 |
dl |
1.1 |
assertTrue(sm.containsKey(one)); |
690 |
|
|
assertTrue(sm.containsKey(two)); |
691 |
|
|
assertTrue(sm.containsKey(three)); |
692 |
|
|
assertFalse(sm.containsKey(four)); |
693 |
|
|
assertFalse(sm.containsKey(five)); |
694 |
|
|
Iterator i = sm.keySet().iterator(); |
695 |
|
|
Object k; |
696 |
|
|
k = (Integer)(i.next()); |
697 |
|
|
assertEquals(one, k); |
698 |
|
|
k = (Integer)(i.next()); |
699 |
|
|
assertEquals(two, k); |
700 |
|
|
k = (Integer)(i.next()); |
701 |
|
|
assertEquals(three, k); |
702 |
|
|
assertFalse(i.hasNext()); |
703 |
|
|
sm.clear(); |
704 |
|
|
assertTrue(sm.isEmpty()); |
705 |
|
|
assertEquals(2, map.size()); |
706 |
|
|
assertEquals(four, map.firstKey()); |
707 |
|
|
} |
708 |
|
|
|
709 |
|
|
/** |
710 |
|
|
* headMap returns map with keys in requested range |
711 |
|
|
*/ |
712 |
|
|
public void testTailMapContents() { |
713 |
|
|
TreeMap map = map5(); |
714 |
dl |
1.4 |
NavigableMap sm = map.tailMap(two, true); |
715 |
dl |
1.1 |
assertFalse(sm.containsKey(one)); |
716 |
|
|
assertTrue(sm.containsKey(two)); |
717 |
|
|
assertTrue(sm.containsKey(three)); |
718 |
|
|
assertTrue(sm.containsKey(four)); |
719 |
|
|
assertTrue(sm.containsKey(five)); |
720 |
|
|
Iterator i = sm.keySet().iterator(); |
721 |
|
|
Object k; |
722 |
|
|
k = (Integer)(i.next()); |
723 |
|
|
assertEquals(two, k); |
724 |
|
|
k = (Integer)(i.next()); |
725 |
|
|
assertEquals(three, k); |
726 |
|
|
k = (Integer)(i.next()); |
727 |
|
|
assertEquals(four, k); |
728 |
|
|
k = (Integer)(i.next()); |
729 |
|
|
assertEquals(five, k); |
730 |
|
|
assertFalse(i.hasNext()); |
731 |
|
|
Iterator r = sm.descendingKeySet().iterator(); |
732 |
|
|
k = (Integer)(r.next()); |
733 |
|
|
assertEquals(five, k); |
734 |
|
|
k = (Integer)(r.next()); |
735 |
|
|
assertEquals(four, k); |
736 |
|
|
k = (Integer)(r.next()); |
737 |
|
|
assertEquals(three, k); |
738 |
|
|
k = (Integer)(r.next()); |
739 |
|
|
assertEquals(two, k); |
740 |
|
|
assertFalse(r.hasNext()); |
741 |
|
|
|
742 |
|
|
Iterator ei = sm.entrySet().iterator(); |
743 |
|
|
Map.Entry e; |
744 |
|
|
e = (Map.Entry)(ei.next()); |
745 |
|
|
assertEquals(two, e.getKey()); |
746 |
|
|
assertEquals("B", e.getValue()); |
747 |
|
|
e = (Map.Entry)(ei.next()); |
748 |
|
|
assertEquals(three, e.getKey()); |
749 |
|
|
assertEquals("C", e.getValue()); |
750 |
|
|
e = (Map.Entry)(ei.next()); |
751 |
|
|
assertEquals(four, e.getKey()); |
752 |
|
|
assertEquals("D", e.getValue()); |
753 |
|
|
e = (Map.Entry)(ei.next()); |
754 |
|
|
assertEquals(five, e.getKey()); |
755 |
|
|
assertEquals("E", e.getValue()); |
756 |
|
|
assertFalse(i.hasNext()); |
757 |
|
|
|
758 |
dl |
1.4 |
NavigableMap ssm = sm.tailMap(four, true); |
759 |
dl |
1.1 |
assertEquals(four, ssm.firstKey()); |
760 |
|
|
assertEquals(five, ssm.lastKey()); |
761 |
jsr166 |
1.12 |
assertEquals("D", ssm.remove(four)); |
762 |
dl |
1.1 |
assertEquals(1, ssm.size()); |
763 |
|
|
assertEquals(3, sm.size()); |
764 |
|
|
assertEquals(4, map.size()); |
765 |
|
|
} |
766 |
dl |
1.3 |
|
767 |
|
|
Random rnd = new Random(666); |
768 |
|
|
BitSet bs; |
769 |
|
|
|
770 |
|
|
/** |
771 |
|
|
* Submaps of submaps subdivide correctly |
772 |
|
|
*/ |
773 |
jsr166 |
1.11 |
public void testRecursiveSubMaps() throws Exception { |
774 |
jsr166 |
1.17 |
int mapSize = expensiveTests ? 1000 : 100; |
775 |
jsr166 |
1.9 |
Class cl = TreeMap.class; |
776 |
dl |
1.3 |
NavigableMap<Integer, Integer> map = newMap(cl); |
777 |
|
|
bs = new BitSet(mapSize); |
778 |
|
|
|
779 |
|
|
populate(map, mapSize); |
780 |
|
|
check(map, 0, mapSize - 1, true); |
781 |
|
|
check(map.descendingMap(), 0, mapSize - 1, false); |
782 |
|
|
|
783 |
|
|
mutateMap(map, 0, mapSize - 1); |
784 |
|
|
check(map, 0, mapSize - 1, true); |
785 |
|
|
check(map.descendingMap(), 0, mapSize - 1, false); |
786 |
|
|
|
787 |
dl |
1.4 |
bashSubMap(map.subMap(0, true, mapSize, false), |
788 |
dl |
1.3 |
0, mapSize - 1, true); |
789 |
|
|
} |
790 |
|
|
|
791 |
jsr166 |
1.11 |
static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception { |
792 |
|
|
NavigableMap<Integer, Integer> result |
793 |
|
|
= (NavigableMap<Integer, Integer>) cl.newInstance(); |
794 |
jsr166 |
1.22 |
assertEquals(0, result.size()); |
795 |
dl |
1.3 |
assertFalse(result.keySet().iterator().hasNext()); |
796 |
|
|
return result; |
797 |
|
|
} |
798 |
|
|
|
799 |
|
|
void populate(NavigableMap<Integer, Integer> map, int limit) { |
800 |
|
|
for (int i = 0, n = 2 * limit / 3; i < n; i++) { |
801 |
|
|
int key = rnd.nextInt(limit); |
802 |
|
|
put(map, key); |
803 |
|
|
} |
804 |
|
|
} |
805 |
|
|
|
806 |
|
|
void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) { |
807 |
|
|
int size = map.size(); |
808 |
|
|
int rangeSize = max - min + 1; |
809 |
|
|
|
810 |
|
|
// Remove a bunch of entries directly |
811 |
|
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
812 |
|
|
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
813 |
|
|
} |
814 |
|
|
|
815 |
|
|
// Remove a bunch of entries with iterator |
816 |
jsr166 |
1.7 |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
817 |
dl |
1.3 |
if (rnd.nextBoolean()) { |
818 |
|
|
bs.clear(it.next()); |
819 |
|
|
it.remove(); |
820 |
|
|
} |
821 |
|
|
} |
822 |
|
|
|
823 |
|
|
// Add entries till we're back to original size |
824 |
|
|
while (map.size() < size) { |
825 |
|
|
int key = min + rnd.nextInt(rangeSize); |
826 |
|
|
assertTrue(key >= min && key<= max); |
827 |
|
|
put(map, key); |
828 |
|
|
} |
829 |
|
|
} |
830 |
|
|
|
831 |
|
|
void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) { |
832 |
|
|
int size = map.size(); |
833 |
|
|
int rangeSize = max - min + 1; |
834 |
|
|
|
835 |
|
|
// Remove a bunch of entries directly |
836 |
|
|
for (int i = 0, n = rangeSize / 2; i < n; i++) { |
837 |
|
|
remove(map, min - 5 + rnd.nextInt(rangeSize + 10)); |
838 |
|
|
} |
839 |
|
|
|
840 |
|
|
// Remove a bunch of entries with iterator |
841 |
jsr166 |
1.7 |
for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) { |
842 |
dl |
1.3 |
if (rnd.nextBoolean()) { |
843 |
|
|
bs.clear(it.next()); |
844 |
|
|
it.remove(); |
845 |
|
|
} |
846 |
|
|
} |
847 |
|
|
|
848 |
|
|
// Add entries till we're back to original size |
849 |
|
|
while (map.size() < size) { |
850 |
|
|
int key = min - 5 + rnd.nextInt(rangeSize + 10); |
851 |
|
|
if (key >= min && key<= max) { |
852 |
|
|
put(map, key); |
853 |
|
|
} else { |
854 |
|
|
try { |
855 |
|
|
map.put(key, 2 * key); |
856 |
jsr166 |
1.11 |
shouldThrow(); |
857 |
|
|
} catch (IllegalArgumentException success) {} |
858 |
dl |
1.3 |
} |
859 |
|
|
} |
860 |
|
|
} |
861 |
|
|
|
862 |
|
|
void put(NavigableMap<Integer, Integer> map, int key) { |
863 |
|
|
if (map.put(key, 2 * key) == null) |
864 |
|
|
bs.set(key); |
865 |
|
|
} |
866 |
|
|
|
867 |
|
|
void remove(NavigableMap<Integer, Integer> map, int key) { |
868 |
|
|
if (map.remove(key) != null) |
869 |
|
|
bs.clear(key); |
870 |
|
|
} |
871 |
|
|
|
872 |
|
|
void bashSubMap(NavigableMap<Integer, Integer> map, |
873 |
|
|
int min, int max, boolean ascending) { |
874 |
|
|
check(map, min, max, ascending); |
875 |
|
|
check(map.descendingMap(), min, max, !ascending); |
876 |
|
|
|
877 |
|
|
mutateSubMap(map, min, max); |
878 |
|
|
check(map, min, max, ascending); |
879 |
|
|
check(map.descendingMap(), min, max, !ascending); |
880 |
|
|
|
881 |
|
|
// Recurse |
882 |
|
|
if (max - min < 2) |
883 |
|
|
return; |
884 |
|
|
int midPoint = (min + max) / 2; |
885 |
|
|
|
886 |
|
|
// headMap - pick direction and endpoint inclusion randomly |
887 |
|
|
boolean incl = rnd.nextBoolean(); |
888 |
dl |
1.4 |
NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl); |
889 |
dl |
1.3 |
if (ascending) { |
890 |
|
|
if (rnd.nextBoolean()) |
891 |
|
|
bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true); |
892 |
|
|
else |
893 |
|
|
bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
894 |
|
|
false); |
895 |
|
|
} else { |
896 |
|
|
if (rnd.nextBoolean()) |
897 |
|
|
bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false); |
898 |
|
|
else |
899 |
|
|
bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
900 |
|
|
true); |
901 |
|
|
} |
902 |
|
|
|
903 |
|
|
// tailMap - pick direction and endpoint inclusion randomly |
904 |
|
|
incl = rnd.nextBoolean(); |
905 |
dl |
1.4 |
NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl); |
906 |
dl |
1.3 |
if (ascending) { |
907 |
|
|
if (rnd.nextBoolean()) |
908 |
|
|
bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true); |
909 |
|
|
else |
910 |
|
|
bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max, |
911 |
|
|
false); |
912 |
|
|
} else { |
913 |
|
|
if (rnd.nextBoolean()) { |
914 |
|
|
bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false); |
915 |
|
|
} else { |
916 |
|
|
bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1), |
917 |
|
|
true); |
918 |
|
|
} |
919 |
|
|
} |
920 |
|
|
|
921 |
|
|
// subMap - pick direction and endpoint inclusion randomly |
922 |
|
|
int rangeSize = max - min + 1; |
923 |
|
|
int[] endpoints = new int[2]; |
924 |
|
|
endpoints[0] = min + rnd.nextInt(rangeSize); |
925 |
|
|
endpoints[1] = min + rnd.nextInt(rangeSize); |
926 |
|
|
Arrays.sort(endpoints); |
927 |
|
|
boolean lowIncl = rnd.nextBoolean(); |
928 |
|
|
boolean highIncl = rnd.nextBoolean(); |
929 |
|
|
if (ascending) { |
930 |
dl |
1.4 |
NavigableMap<Integer,Integer> sm = map.subMap( |
931 |
dl |
1.3 |
endpoints[0], lowIncl, endpoints[1], highIncl); |
932 |
|
|
if (rnd.nextBoolean()) |
933 |
|
|
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
934 |
|
|
endpoints[1] - (highIncl ? 0 : 1), true); |
935 |
|
|
else |
936 |
|
|
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
937 |
|
|
endpoints[1] - (highIncl ? 0 : 1), false); |
938 |
|
|
} else { |
939 |
dl |
1.4 |
NavigableMap<Integer,Integer> sm = map.subMap( |
940 |
dl |
1.3 |
endpoints[1], highIncl, endpoints[0], lowIncl); |
941 |
|
|
if (rnd.nextBoolean()) |
942 |
|
|
bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1), |
943 |
|
|
endpoints[1] - (highIncl ? 0 : 1), false); |
944 |
|
|
else |
945 |
|
|
bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1), |
946 |
|
|
endpoints[1] - (highIncl ? 0 : 1), true); |
947 |
|
|
} |
948 |
|
|
} |
949 |
|
|
|
950 |
|
|
/** |
951 |
|
|
* min and max are both inclusive. If max < min, interval is empty. |
952 |
|
|
*/ |
953 |
|
|
void check(NavigableMap<Integer, Integer> map, |
954 |
|
|
final int min, final int max, final boolean ascending) { |
955 |
jsr166 |
1.19 |
class ReferenceSet { |
956 |
dl |
1.3 |
int lower(int key) { |
957 |
|
|
return ascending ? lowerAscending(key) : higherAscending(key); |
958 |
|
|
} |
959 |
|
|
int floor(int key) { |
960 |
|
|
return ascending ? floorAscending(key) : ceilingAscending(key); |
961 |
|
|
} |
962 |
|
|
int ceiling(int key) { |
963 |
|
|
return ascending ? ceilingAscending(key) : floorAscending(key); |
964 |
|
|
} |
965 |
|
|
int higher(int key) { |
966 |
|
|
return ascending ? higherAscending(key) : lowerAscending(key); |
967 |
|
|
} |
968 |
|
|
int first() { |
969 |
|
|
return ascending ? firstAscending() : lastAscending(); |
970 |
|
|
} |
971 |
|
|
int last() { |
972 |
|
|
return ascending ? lastAscending() : firstAscending(); |
973 |
|
|
} |
974 |
|
|
int lowerAscending(int key) { |
975 |
|
|
return floorAscending(key - 1); |
976 |
|
|
} |
977 |
|
|
int floorAscending(int key) { |
978 |
|
|
if (key < min) |
979 |
|
|
return -1; |
980 |
|
|
else if (key > max) |
981 |
|
|
key = max; |
982 |
|
|
|
983 |
|
|
// BitSet should support this! Test would run much faster |
984 |
|
|
while (key >= min) { |
985 |
|
|
if (bs.get(key)) |
986 |
jsr166 |
1.15 |
return key; |
987 |
dl |
1.3 |
key--; |
988 |
|
|
} |
989 |
|
|
return -1; |
990 |
|
|
} |
991 |
|
|
int ceilingAscending(int key) { |
992 |
|
|
if (key < min) |
993 |
|
|
key = min; |
994 |
|
|
else if (key > max) |
995 |
|
|
return -1; |
996 |
|
|
int result = bs.nextSetBit(key); |
997 |
|
|
return result > max ? -1 : result; |
998 |
|
|
} |
999 |
|
|
int higherAscending(int key) { |
1000 |
|
|
return ceilingAscending(key + 1); |
1001 |
|
|
} |
1002 |
|
|
private int firstAscending() { |
1003 |
|
|
int result = ceilingAscending(min); |
1004 |
|
|
return result > max ? -1 : result; |
1005 |
|
|
} |
1006 |
|
|
private int lastAscending() { |
1007 |
|
|
int result = floorAscending(max); |
1008 |
|
|
return result < min ? -1 : result; |
1009 |
|
|
} |
1010 |
|
|
} |
1011 |
|
|
ReferenceSet rs = new ReferenceSet(); |
1012 |
|
|
|
1013 |
|
|
// Test contents using containsKey |
1014 |
|
|
int size = 0; |
1015 |
|
|
for (int i = min; i <= max; i++) { |
1016 |
|
|
boolean bsContainsI = bs.get(i); |
1017 |
|
|
assertEquals(bsContainsI, map.containsKey(i)); |
1018 |
|
|
if (bsContainsI) |
1019 |
|
|
size++; |
1020 |
|
|
} |
1021 |
|
|
assertEquals(map.size(), size); |
1022 |
|
|
|
1023 |
|
|
// Test contents using contains keySet iterator |
1024 |
|
|
int size2 = 0; |
1025 |
|
|
int previousKey = -1; |
1026 |
|
|
for (int key : map.keySet()) { |
1027 |
|
|
assertTrue(bs.get(key)); |
1028 |
|
|
size2++; |
1029 |
|
|
assertTrue(previousKey < 0 || |
1030 |
|
|
(ascending ? key - previousKey > 0 : key - previousKey < 0)); |
1031 |
|
|
previousKey = key; |
1032 |
|
|
} |
1033 |
|
|
assertEquals(size2, size); |
1034 |
|
|
|
1035 |
|
|
// Test navigation ops |
1036 |
|
|
for (int key = min - 1; key <= max + 1; key++) { |
1037 |
|
|
assertEq(map.lowerKey(key), rs.lower(key)); |
1038 |
|
|
assertEq(map.floorKey(key), rs.floor(key)); |
1039 |
|
|
assertEq(map.higherKey(key), rs.higher(key)); |
1040 |
|
|
assertEq(map.ceilingKey(key), rs.ceiling(key)); |
1041 |
|
|
} |
1042 |
|
|
|
1043 |
|
|
// Test extrema |
1044 |
|
|
if (map.size() != 0) { |
1045 |
|
|
assertEq(map.firstKey(), rs.first()); |
1046 |
|
|
assertEq(map.lastKey(), rs.last()); |
1047 |
|
|
} else { |
1048 |
|
|
assertEq(rs.first(), -1); |
1049 |
|
|
assertEq(rs.last(), -1); |
1050 |
|
|
try { |
1051 |
|
|
map.firstKey(); |
1052 |
jsr166 |
1.11 |
shouldThrow(); |
1053 |
|
|
} catch (NoSuchElementException success) {} |
1054 |
dl |
1.3 |
try { |
1055 |
|
|
map.lastKey(); |
1056 |
jsr166 |
1.11 |
shouldThrow(); |
1057 |
|
|
} catch (NoSuchElementException success) {} |
1058 |
dl |
1.3 |
} |
1059 |
|
|
} |
1060 |
|
|
|
1061 |
|
|
static void assertEq(Integer i, int j) { |
1062 |
|
|
if (i == null) |
1063 |
|
|
assertEquals(j, -1); |
1064 |
|
|
else |
1065 |
|
|
assertEquals((int) i, j); |
1066 |
|
|
} |
1067 |
|
|
|
1068 |
|
|
static boolean eq(Integer i, int j) { |
1069 |
|
|
return i == null ? j == -1 : i == j; |
1070 |
|
|
} |
1071 |
jsr166 |
1.6 |
|
1072 |
dl |
1.1 |
} |