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