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 |
5 |
*/ |
6 |
|
7 |
import junit.framework.*; |
8 |
import java.util.*; |
9 |
import java.util.concurrent.*; |
10 |
import java.io.*; |
11 |
|
12 |
public class TreeSubMapTest extends JSR166TestCase { |
13 |
public static void main(String[] args) { |
14 |
junit.textui.TestRunner.run (suite()); |
15 |
} |
16 |
public static Test suite() { |
17 |
return new TestSuite(TreeSubMapTest.class); |
18 |
} |
19 |
|
20 |
/** |
21 |
* Create a map from Integers 1-5 to Strings "A"-"E". |
22 |
*/ |
23 |
private static NavigableMap map5() { |
24 |
TreeMap map = new TreeMap(); |
25 |
assertTrue(map.isEmpty()); |
26 |
map.put(zero, "Z"); |
27 |
map.put(one, "A"); |
28 |
map.put(five, "E"); |
29 |
map.put(three, "C"); |
30 |
map.put(two, "B"); |
31 |
map.put(four, "D"); |
32 |
map.put(seven, "F"); |
33 |
assertFalse(map.isEmpty()); |
34 |
assertEquals(7, map.size()); |
35 |
return map.navigableSubMap(one, seven); |
36 |
} |
37 |
|
38 |
private static NavigableMap map0() { |
39 |
TreeMap map = new TreeMap(); |
40 |
assertTrue(map.isEmpty()); |
41 |
return map.navigableTailMap(one); |
42 |
} |
43 |
|
44 |
/** |
45 |
* clear removes all pairs |
46 |
*/ |
47 |
public void testClear() { |
48 |
NavigableMap map = map5(); |
49 |
map.clear(); |
50 |
assertEquals(map.size(), 0); |
51 |
} |
52 |
|
53 |
|
54 |
/** |
55 |
* Maps with same contents are equal |
56 |
*/ |
57 |
public void testEquals() { |
58 |
NavigableMap map1 = map5(); |
59 |
NavigableMap map2 = map5(); |
60 |
assertEquals(map1, map2); |
61 |
assertEquals(map2, map1); |
62 |
map1.clear(); |
63 |
assertFalse(map1.equals(map2)); |
64 |
assertFalse(map2.equals(map1)); |
65 |
} |
66 |
|
67 |
/** |
68 |
* containsKey returns true for contained key |
69 |
*/ |
70 |
public void testContainsKey() { |
71 |
NavigableMap map = map5(); |
72 |
assertTrue(map.containsKey(one)); |
73 |
assertFalse(map.containsKey(zero)); |
74 |
} |
75 |
|
76 |
/** |
77 |
* containsValue returns true for held values |
78 |
*/ |
79 |
public void testContainsValue() { |
80 |
NavigableMap map = map5(); |
81 |
assertTrue(map.containsValue("A")); |
82 |
assertFalse(map.containsValue("Z")); |
83 |
} |
84 |
|
85 |
/** |
86 |
* get returns the correct element at the given key, |
87 |
* or null if not present |
88 |
*/ |
89 |
public void testGet() { |
90 |
NavigableMap map = map5(); |
91 |
assertEquals("A", (String)map.get(one)); |
92 |
NavigableMap empty = map0(); |
93 |
assertNull(empty.get(one)); |
94 |
} |
95 |
|
96 |
/** |
97 |
* isEmpty is true of empty map and false for non-empty |
98 |
*/ |
99 |
public void testIsEmpty() { |
100 |
NavigableMap empty = map0(); |
101 |
NavigableMap map = map5(); |
102 |
assertTrue(empty.isEmpty()); |
103 |
assertFalse(map.isEmpty()); |
104 |
} |
105 |
|
106 |
/** |
107 |
* firstKey returns first key |
108 |
*/ |
109 |
public void testFirstKey() { |
110 |
NavigableMap map = map5(); |
111 |
assertEquals(one, map.firstKey()); |
112 |
} |
113 |
|
114 |
/** |
115 |
* lastKey returns last key |
116 |
*/ |
117 |
public void testLastKey() { |
118 |
NavigableMap map = map5(); |
119 |
assertEquals(five, map.lastKey()); |
120 |
} |
121 |
|
122 |
|
123 |
/** |
124 |
* keySet returns a Set containing all the keys |
125 |
*/ |
126 |
public void testKeySet() { |
127 |
NavigableMap map = map5(); |
128 |
Set s = map.keySet(); |
129 |
assertEquals(5, s.size()); |
130 |
assertTrue(s.contains(one)); |
131 |
assertTrue(s.contains(two)); |
132 |
assertTrue(s.contains(three)); |
133 |
assertTrue(s.contains(four)); |
134 |
assertTrue(s.contains(five)); |
135 |
} |
136 |
|
137 |
/** |
138 |
* keySet is ordered |
139 |
*/ |
140 |
public void testKeySetOrder() { |
141 |
NavigableMap map = map5(); |
142 |
Set s = map.keySet(); |
143 |
Iterator i = s.iterator(); |
144 |
Integer last = (Integer)i.next(); |
145 |
assertEquals(last, one); |
146 |
while (i.hasNext()) { |
147 |
Integer k = (Integer)i.next(); |
148 |
assertTrue(last.compareTo(k) < 0); |
149 |
last = k; |
150 |
} |
151 |
} |
152 |
|
153 |
/** |
154 |
* values collection contains all values |
155 |
*/ |
156 |
public void testValues() { |
157 |
NavigableMap map = map5(); |
158 |
Collection s = map.values(); |
159 |
assertEquals(5, s.size()); |
160 |
assertTrue(s.contains("A")); |
161 |
assertTrue(s.contains("B")); |
162 |
assertTrue(s.contains("C")); |
163 |
assertTrue(s.contains("D")); |
164 |
assertTrue(s.contains("E")); |
165 |
} |
166 |
|
167 |
/** |
168 |
* entrySet contains all pairs |
169 |
*/ |
170 |
public void testEntrySet() { |
171 |
NavigableMap map = map5(); |
172 |
Set s = map.entrySet(); |
173 |
assertEquals(5, s.size()); |
174 |
Iterator it = s.iterator(); |
175 |
while (it.hasNext()) { |
176 |
Map.Entry e = (Map.Entry) it.next(); |
177 |
assertTrue( |
178 |
(e.getKey().equals(one) && e.getValue().equals("A")) || |
179 |
(e.getKey().equals(two) && e.getValue().equals("B")) || |
180 |
(e.getKey().equals(three) && e.getValue().equals("C")) || |
181 |
(e.getKey().equals(four) && e.getValue().equals("D")) || |
182 |
(e.getKey().equals(five) && e.getValue().equals("E"))); |
183 |
} |
184 |
} |
185 |
|
186 |
/** |
187 |
* putAll adds all key-value pairs from the given map |
188 |
*/ |
189 |
public void testPutAll() { |
190 |
NavigableMap empty = map0(); |
191 |
NavigableMap map = map5(); |
192 |
empty.putAll(map); |
193 |
assertEquals(5, empty.size()); |
194 |
assertTrue(empty.containsKey(one)); |
195 |
assertTrue(empty.containsKey(two)); |
196 |
assertTrue(empty.containsKey(three)); |
197 |
assertTrue(empty.containsKey(four)); |
198 |
assertTrue(empty.containsKey(five)); |
199 |
} |
200 |
|
201 |
/** |
202 |
* remove removes the correct key-value pair from the map |
203 |
*/ |
204 |
public void testRemove() { |
205 |
NavigableMap map = map5(); |
206 |
map.remove(five); |
207 |
assertEquals(4, map.size()); |
208 |
assertFalse(map.containsKey(five)); |
209 |
} |
210 |
|
211 |
/** |
212 |
* lowerEntry returns preceding entry. |
213 |
*/ |
214 |
public void testLowerEntry() { |
215 |
NavigableMap map = map5(); |
216 |
Map.Entry e1 = map.lowerEntry(three); |
217 |
assertEquals(two, e1.getKey()); |
218 |
|
219 |
Map.Entry e2 = map.lowerEntry(six); |
220 |
assertEquals(five, e2.getKey()); |
221 |
|
222 |
Map.Entry e3 = map.lowerEntry(one); |
223 |
assertNull(e3); |
224 |
|
225 |
Map.Entry e4 = map.lowerEntry(zero); |
226 |
assertNull(e4); |
227 |
|
228 |
} |
229 |
|
230 |
/** |
231 |
* higherEntry returns next entry. |
232 |
*/ |
233 |
public void testHigherEntry() { |
234 |
NavigableMap map = map5(); |
235 |
Map.Entry e1 = map.higherEntry(three); |
236 |
assertEquals(four, e1.getKey()); |
237 |
|
238 |
Map.Entry e2 = map.higherEntry(zero); |
239 |
assertEquals(one, e2.getKey()); |
240 |
|
241 |
Map.Entry e3 = map.higherEntry(five); |
242 |
assertNull(e3); |
243 |
|
244 |
Map.Entry e4 = map.higherEntry(six); |
245 |
assertNull(e4); |
246 |
|
247 |
} |
248 |
|
249 |
/** |
250 |
* floorEntry returns preceding entry. |
251 |
*/ |
252 |
public void testFloorEntry() { |
253 |
NavigableMap map = map5(); |
254 |
Map.Entry e1 = map.floorEntry(three); |
255 |
assertEquals(three, e1.getKey()); |
256 |
|
257 |
Map.Entry e2 = map.floorEntry(six); |
258 |
assertEquals(five, e2.getKey()); |
259 |
|
260 |
Map.Entry e3 = map.floorEntry(one); |
261 |
assertEquals(one, e3.getKey()); |
262 |
|
263 |
Map.Entry e4 = map.floorEntry(zero); |
264 |
assertNull(e4); |
265 |
|
266 |
} |
267 |
|
268 |
/** |
269 |
* ceilingEntry returns next entry. |
270 |
*/ |
271 |
public void testCeilingEntry() { |
272 |
NavigableMap map = map5(); |
273 |
Map.Entry e1 = map.ceilingEntry(three); |
274 |
assertEquals(three, e1.getKey()); |
275 |
|
276 |
Map.Entry e2 = map.ceilingEntry(zero); |
277 |
assertEquals(one, e2.getKey()); |
278 |
|
279 |
Map.Entry e3 = map.ceilingEntry(five); |
280 |
assertEquals(five, e3.getKey()); |
281 |
|
282 |
Map.Entry e4 = map.ceilingEntry(six); |
283 |
assertNull(e4); |
284 |
|
285 |
} |
286 |
|
287 |
/** |
288 |
* pollFirstEntry returns entries in order |
289 |
*/ |
290 |
public void testPollFirstEntry() { |
291 |
NavigableMap map = map5(); |
292 |
Map.Entry e = map.pollFirstEntry(); |
293 |
assertEquals(one, e.getKey()); |
294 |
assertEquals("A", e.getValue()); |
295 |
e = map.pollFirstEntry(); |
296 |
assertEquals(two, e.getKey()); |
297 |
map.put(one, "A"); |
298 |
e = map.pollFirstEntry(); |
299 |
assertEquals(one, e.getKey()); |
300 |
assertEquals("A", e.getValue()); |
301 |
e = map.pollFirstEntry(); |
302 |
assertEquals(three, e.getKey()); |
303 |
map.remove(four); |
304 |
e = map.pollFirstEntry(); |
305 |
assertEquals(five, e.getKey()); |
306 |
try { |
307 |
e.setValue("A"); |
308 |
shouldThrow(); |
309 |
} catch (Exception ok) { |
310 |
} |
311 |
assertTrue(map.isEmpty()); |
312 |
Map.Entry f = map.firstEntry(); |
313 |
assertNull(f); |
314 |
e = map.pollFirstEntry(); |
315 |
assertNull(e); |
316 |
} |
317 |
|
318 |
/** |
319 |
* pollLastEntry returns entries in order |
320 |
*/ |
321 |
public void testPollLastEntry() { |
322 |
NavigableMap map = map5(); |
323 |
Map.Entry e = map.pollLastEntry(); |
324 |
assertEquals(five, e.getKey()); |
325 |
assertEquals("E", e.getValue()); |
326 |
e = map.pollLastEntry(); |
327 |
assertEquals(four, e.getKey()); |
328 |
map.put(five, "E"); |
329 |
e = map.pollLastEntry(); |
330 |
assertEquals(five, e.getKey()); |
331 |
assertEquals("E", e.getValue()); |
332 |
e = map.pollLastEntry(); |
333 |
assertEquals(three, e.getKey()); |
334 |
map.remove(two); |
335 |
e = map.pollLastEntry(); |
336 |
assertEquals(one, e.getKey()); |
337 |
try { |
338 |
e.setValue("E"); |
339 |
shouldThrow(); |
340 |
} catch (Exception ok) { |
341 |
} |
342 |
e = map.pollLastEntry(); |
343 |
assertNull(e); |
344 |
} |
345 |
|
346 |
/** |
347 |
* size returns the correct values |
348 |
*/ |
349 |
public void testSize() { |
350 |
NavigableMap map = map5(); |
351 |
NavigableMap empty = map0(); |
352 |
assertEquals(0, empty.size()); |
353 |
assertEquals(5, map.size()); |
354 |
} |
355 |
|
356 |
/** |
357 |
* toString contains toString of elements |
358 |
*/ |
359 |
public void testToString() { |
360 |
NavigableMap map = map5(); |
361 |
String s = map.toString(); |
362 |
for (int i = 1; i <= 5; ++i) { |
363 |
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
364 |
} |
365 |
} |
366 |
|
367 |
// Exception tests |
368 |
|
369 |
/** |
370 |
* get(null) of nonempty map throws NPE |
371 |
*/ |
372 |
public void testGet_NullPointerException() { |
373 |
try { |
374 |
NavigableMap c = map5(); |
375 |
c.get(null); |
376 |
shouldThrow(); |
377 |
} catch(NullPointerException e){} |
378 |
} |
379 |
|
380 |
/** |
381 |
* containsKey(null) of nonempty map throws NPE |
382 |
*/ |
383 |
public void testContainsKey_NullPointerException() { |
384 |
try { |
385 |
NavigableMap c = map5(); |
386 |
c.containsKey(null); |
387 |
shouldThrow(); |
388 |
} catch(NullPointerException e){} |
389 |
} |
390 |
|
391 |
/** |
392 |
* put(null,x) throws NPE |
393 |
*/ |
394 |
public void testPut1_NullPointerException() { |
395 |
try { |
396 |
NavigableMap c = map5(); |
397 |
c.put(null, "whatever"); |
398 |
shouldThrow(); |
399 |
} catch(NullPointerException e){} |
400 |
} |
401 |
|
402 |
/** |
403 |
* remove(null) throws NPE |
404 |
*/ |
405 |
public void testRemove1_NullPointerException() { |
406 |
try { |
407 |
NavigableMap c = map5(); |
408 |
c.remove(null); |
409 |
shouldThrow(); |
410 |
} catch(NullPointerException e){} |
411 |
} |
412 |
|
413 |
/** |
414 |
* A deserialized map equals original |
415 |
*/ |
416 |
public void testSerialization() { |
417 |
NavigableMap q = map5(); |
418 |
|
419 |
try { |
420 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
421 |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
422 |
out.writeObject(q); |
423 |
out.close(); |
424 |
|
425 |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
426 |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
427 |
NavigableMap r = (NavigableMap)in.readObject(); |
428 |
assertFalse(r.isEmpty()); |
429 |
assertEquals(q.size(), r.size()); |
430 |
assertTrue(q.equals(r)); |
431 |
assertTrue(r.equals(q)); |
432 |
} catch(Exception e){ |
433 |
e.printStackTrace(); |
434 |
unexpectedException(); |
435 |
} |
436 |
} |
437 |
|
438 |
|
439 |
|
440 |
/** |
441 |
* subMap returns map with keys in requested range |
442 |
*/ |
443 |
public void testSubMapContents() { |
444 |
NavigableMap map = map5(); |
445 |
SortedMap sm = map.subMap(two, four); |
446 |
assertEquals(two, sm.firstKey()); |
447 |
assertEquals(three, sm.lastKey()); |
448 |
assertEquals(2, sm.size()); |
449 |
assertFalse(sm.containsKey(one)); |
450 |
assertTrue(sm.containsKey(two)); |
451 |
assertTrue(sm.containsKey(three)); |
452 |
assertFalse(sm.containsKey(four)); |
453 |
assertFalse(sm.containsKey(five)); |
454 |
Iterator i = sm.keySet().iterator(); |
455 |
Object k; |
456 |
k = (Integer)(i.next()); |
457 |
assertEquals(two, k); |
458 |
k = (Integer)(i.next()); |
459 |
assertEquals(three, k); |
460 |
assertFalse(i.hasNext()); |
461 |
Iterator j = sm.keySet().iterator(); |
462 |
j.next(); |
463 |
j.remove(); |
464 |
assertFalse(map.containsKey(two)); |
465 |
assertEquals(4, map.size()); |
466 |
assertEquals(1, sm.size()); |
467 |
assertEquals(three, sm.firstKey()); |
468 |
assertEquals(three, sm.lastKey()); |
469 |
assertTrue(sm.remove(three) != null); |
470 |
assertTrue(sm.isEmpty()); |
471 |
assertEquals(3, map.size()); |
472 |
} |
473 |
|
474 |
public void testSubMapContents2() { |
475 |
NavigableMap map = map5(); |
476 |
SortedMap sm = map.subMap(two, three); |
477 |
assertEquals(1, sm.size()); |
478 |
assertEquals(two, sm.firstKey()); |
479 |
assertEquals(two, sm.lastKey()); |
480 |
assertFalse(sm.containsKey(one)); |
481 |
assertTrue(sm.containsKey(two)); |
482 |
assertFalse(sm.containsKey(three)); |
483 |
assertFalse(sm.containsKey(four)); |
484 |
assertFalse(sm.containsKey(five)); |
485 |
Iterator i = sm.keySet().iterator(); |
486 |
Object k; |
487 |
k = (Integer)(i.next()); |
488 |
assertEquals(two, k); |
489 |
assertFalse(i.hasNext()); |
490 |
Iterator j = sm.keySet().iterator(); |
491 |
j.next(); |
492 |
j.remove(); |
493 |
assertFalse(map.containsKey(two)); |
494 |
assertEquals(4, map.size()); |
495 |
assertEquals(0, sm.size()); |
496 |
assertTrue(sm.isEmpty()); |
497 |
assertTrue(sm.remove(three) == null); |
498 |
assertEquals(4, map.size()); |
499 |
} |
500 |
|
501 |
/** |
502 |
* headMap returns map with keys in requested range |
503 |
*/ |
504 |
public void testHeadMapContents() { |
505 |
NavigableMap map = map5(); |
506 |
SortedMap sm = map.headMap(four); |
507 |
assertTrue(sm.containsKey(one)); |
508 |
assertTrue(sm.containsKey(two)); |
509 |
assertTrue(sm.containsKey(three)); |
510 |
assertFalse(sm.containsKey(four)); |
511 |
assertFalse(sm.containsKey(five)); |
512 |
Iterator i = sm.keySet().iterator(); |
513 |
Object k; |
514 |
k = (Integer)(i.next()); |
515 |
assertEquals(one, k); |
516 |
k = (Integer)(i.next()); |
517 |
assertEquals(two, k); |
518 |
k = (Integer)(i.next()); |
519 |
assertEquals(three, k); |
520 |
assertFalse(i.hasNext()); |
521 |
sm.clear(); |
522 |
assertTrue(sm.isEmpty()); |
523 |
assertEquals(2, map.size()); |
524 |
assertEquals(four, map.firstKey()); |
525 |
} |
526 |
|
527 |
/** |
528 |
* headMap returns map with keys in requested range |
529 |
*/ |
530 |
public void testTailMapContents() { |
531 |
NavigableMap map = map5(); |
532 |
SortedMap sm = map.tailMap(two); |
533 |
assertFalse(sm.containsKey(one)); |
534 |
assertTrue(sm.containsKey(two)); |
535 |
assertTrue(sm.containsKey(three)); |
536 |
assertTrue(sm.containsKey(four)); |
537 |
assertTrue(sm.containsKey(five)); |
538 |
Iterator i = sm.keySet().iterator(); |
539 |
Object k; |
540 |
k = (Integer)(i.next()); |
541 |
assertEquals(two, k); |
542 |
k = (Integer)(i.next()); |
543 |
assertEquals(three, k); |
544 |
k = (Integer)(i.next()); |
545 |
assertEquals(four, k); |
546 |
k = (Integer)(i.next()); |
547 |
assertEquals(five, k); |
548 |
assertFalse(i.hasNext()); |
549 |
|
550 |
Iterator ei = sm.entrySet().iterator(); |
551 |
Map.Entry e; |
552 |
e = (Map.Entry)(ei.next()); |
553 |
assertEquals(two, e.getKey()); |
554 |
assertEquals("B", e.getValue()); |
555 |
e = (Map.Entry)(ei.next()); |
556 |
assertEquals(three, e.getKey()); |
557 |
assertEquals("C", e.getValue()); |
558 |
e = (Map.Entry)(ei.next()); |
559 |
assertEquals(four, e.getKey()); |
560 |
assertEquals("D", e.getValue()); |
561 |
e = (Map.Entry)(ei.next()); |
562 |
assertEquals(five, e.getKey()); |
563 |
assertEquals("E", e.getValue()); |
564 |
assertFalse(i.hasNext()); |
565 |
|
566 |
SortedMap ssm = sm.tailMap(four); |
567 |
assertEquals(four, ssm.firstKey()); |
568 |
assertEquals(five, ssm.lastKey()); |
569 |
assertTrue(ssm.remove(four) != null); |
570 |
assertEquals(1, ssm.size()); |
571 |
assertEquals(3, sm.size()); |
572 |
assertEquals(4, map.size()); |
573 |
} |
574 |
|
575 |
} |