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