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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
11 |
|
|
12 |
|
public class TreeSubMapTest extends JSR166TestCase { |
13 |
|
public static void main(String[] args) { |
14 |
< |
junit.textui.TestRunner.run (suite()); |
14 |
> |
junit.textui.TestRunner.run(suite()); |
15 |
|
} |
16 |
|
public static Test suite() { |
17 |
< |
return new TestSuite(TreeSubMapTest.class); |
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(); |
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"); |
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, true, seven, false); |
35 |
> |
return map.subMap(one, true, seven, false); |
36 |
|
} |
37 |
|
|
38 |
< |
private static NavigableMap map0() { |
39 |
< |
TreeMap map = new TreeMap(); |
38 |
> |
private static NavigableMap map0() { |
39 |
> |
TreeMap map = new TreeMap(); |
40 |
|
assertTrue(map.isEmpty()); |
41 |
< |
return map.navigableTailMap(one, true); |
41 |
> |
return map.tailMap(one, true); |
42 |
|
} |
43 |
|
|
44 |
|
/** |
45 |
< |
* clear removes all pairs |
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); |
71 |
> |
map.clear(); |
72 |
> |
assertEquals(map.size(), 0); |
73 |
|
} |
74 |
|
|
75 |
|
|
76 |
|
/** |
77 |
< |
* Maps with same contents are equal |
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(); |
84 |
> |
map1.clear(); |
85 |
|
assertFalse(map1.equals(map2)); |
86 |
|
assertFalse(map2.equals(map1)); |
87 |
|
} |
88 |
|
|
89 |
|
/** |
90 |
< |
* containsKey returns true for contained key |
90 |
> |
* containsKey returns true for contained key |
91 |
|
*/ |
92 |
|
public void testContainsKey() { |
93 |
|
NavigableMap map = map5(); |
94 |
< |
assertTrue(map.containsKey(one)); |
94 |
> |
assertTrue(map.containsKey(one)); |
95 |
|
assertFalse(map.containsKey(zero)); |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
< |
* containsValue returns true for held values |
99 |
> |
* containsValue returns true for held values |
100 |
|
*/ |
101 |
|
public void testContainsValue() { |
102 |
|
NavigableMap map = map5(); |
103 |
< |
assertTrue(map.containsValue("A")); |
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 |
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)); |
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 |
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()); |
124 |
> |
assertTrue(empty.isEmpty()); |
125 |
|
assertFalse(map.isEmpty()); |
126 |
|
} |
127 |
|
|
128 |
|
/** |
129 |
< |
* firstKey returns first key |
129 |
> |
* firstKey returns first key |
130 |
|
*/ |
131 |
|
public void testFirstKey() { |
132 |
|
NavigableMap map = map5(); |
133 |
< |
assertEquals(one, map.firstKey()); |
133 |
> |
assertEquals(one, map.firstKey()); |
134 |
|
} |
135 |
|
|
136 |
|
/** |
137 |
< |
* lastKey returns last key |
137 |
> |
* lastKey returns last key |
138 |
|
*/ |
139 |
|
public void testLastKey() { |
140 |
|
NavigableMap map = map5(); |
141 |
< |
assertEquals(five, map.lastKey()); |
141 |
> |
assertEquals(five, map.lastKey()); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
/** |
146 |
< |
* keySet returns a Set containing all the keys |
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)); |
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 |
160 |
> |
* keySet is ordered |
161 |
|
*/ |
162 |
|
public void testKeySetOrder() { |
163 |
|
NavigableMap map = map5(); |
164 |
< |
Set s = map.keySet(); |
164 |
> |
Set s = map.keySet(); |
165 |
|
Iterator i = s.iterator(); |
166 |
|
Integer last = (Integer)i.next(); |
167 |
|
assertEquals(last, one); |
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")); |
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 |
|
/** |
191 |
|
*/ |
192 |
|
public void testEntrySet() { |
193 |
|
NavigableMap map = map5(); |
194 |
< |
Set s = map.entrySet(); |
195 |
< |
assertEquals(5, s.size()); |
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( |
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")) || |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* putAll adds all key-value pairs from the given map |
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)); |
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 |
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)); |
228 |
> |
map.remove(five); |
229 |
> |
assertEquals(4, map.size()); |
230 |
> |
assertFalse(map.containsKey(five)); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
246 |
|
|
247 |
|
Map.Entry e4 = map.lowerEntry(zero); |
248 |
|
assertNull(e4); |
227 |
– |
|
249 |
|
} |
250 |
|
|
251 |
|
/** |
264 |
|
|
265 |
|
Map.Entry e4 = map.higherEntry(six); |
266 |
|
assertNull(e4); |
246 |
– |
|
267 |
|
} |
268 |
|
|
269 |
|
/** |
282 |
|
|
283 |
|
Map.Entry e4 = map.floorEntry(zero); |
284 |
|
assertNull(e4); |
265 |
– |
|
285 |
|
} |
286 |
|
|
287 |
|
/** |
300 |
|
|
301 |
|
Map.Entry e4 = map.ceilingEntry(six); |
302 |
|
assertNull(e4); |
284 |
– |
|
303 |
|
} |
304 |
|
|
305 |
|
/** |
324 |
|
try { |
325 |
|
e.setValue("A"); |
326 |
|
shouldThrow(); |
327 |
< |
} catch (Exception ok) { |
310 |
< |
} |
327 |
> |
} catch (UnsupportedOperationException success) {} |
328 |
|
assertTrue(map.isEmpty()); |
329 |
|
Map.Entry f = map.firstEntry(); |
330 |
|
assertNull(f); |
354 |
|
try { |
355 |
|
e.setValue("E"); |
356 |
|
shouldThrow(); |
357 |
< |
} catch (Exception ok) { |
341 |
< |
} |
357 |
> |
} catch (UnsupportedOperationException success) {} |
358 |
|
e = map.pollLastEntry(); |
359 |
|
assertNull(e); |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* size returns the correct values |
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()); |
368 |
> |
assertEquals(0, empty.size()); |
369 |
> |
assertEquals(5, map.size()); |
370 |
|
} |
371 |
|
|
372 |
|
/** |
378 |
|
for (int i = 1; i <= 5; ++i) { |
379 |
|
assertTrue(s.indexOf(String.valueOf(i)) >= 0); |
380 |
|
} |
381 |
< |
} |
381 |
> |
} |
382 |
|
|
383 |
|
// Exception tests |
384 |
|
|
390 |
|
NavigableMap c = map5(); |
391 |
|
c.get(null); |
392 |
|
shouldThrow(); |
393 |
< |
} catch(NullPointerException e){} |
393 |
> |
} catch (NullPointerException success) {} |
394 |
|
} |
395 |
|
|
396 |
|
/** |
401 |
|
NavigableMap c = map5(); |
402 |
|
c.containsKey(null); |
403 |
|
shouldThrow(); |
404 |
< |
} catch(NullPointerException e){} |
404 |
> |
} catch (NullPointerException success) {} |
405 |
|
} |
406 |
|
|
407 |
|
/** |
412 |
|
NavigableMap c = map5(); |
413 |
|
c.put(null, "whatever"); |
414 |
|
shouldThrow(); |
415 |
< |
} catch(NullPointerException e){} |
415 |
> |
} catch (NullPointerException success) {} |
416 |
|
} |
417 |
|
|
418 |
|
/** |
423 |
|
NavigableMap c = map5(); |
424 |
|
c.remove(null); |
425 |
|
shouldThrow(); |
426 |
< |
} catch(NullPointerException e){} |
426 |
> |
} catch (NullPointerException success) {} |
427 |
|
} |
428 |
|
|
429 |
|
/** |
430 |
|
* A deserialized map equals original |
431 |
|
*/ |
432 |
< |
public void testSerialization() { |
432 |
> |
public void testSerialization() throws Exception { |
433 |
|
NavigableMap q = map5(); |
434 |
|
|
435 |
< |
try { |
436 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
437 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
438 |
< |
out.writeObject(q); |
439 |
< |
out.close(); |
440 |
< |
|
441 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
442 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
443 |
< |
NavigableMap r = (NavigableMap)in.readObject(); |
444 |
< |
assertFalse(r.isEmpty()); |
445 |
< |
assertEquals(q.size(), r.size()); |
446 |
< |
assertTrue(q.equals(r)); |
431 |
< |
assertTrue(r.equals(q)); |
432 |
< |
} catch(Exception e){ |
433 |
< |
e.printStackTrace(); |
434 |
< |
unexpectedException(); |
435 |
< |
} |
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 |
|
|
477 |
|
assertEquals(1, sm.size()); |
478 |
|
assertEquals(three, sm.firstKey()); |
479 |
|
assertEquals(three, sm.lastKey()); |
480 |
< |
assertTrue(sm.remove(three) != null); |
480 |
> |
assertEquals("C", sm.remove(three)); |
481 |
|
assertTrue(sm.isEmpty()); |
482 |
|
assertEquals(3, map.size()); |
483 |
|
} |
505 |
|
assertEquals(4, map.size()); |
506 |
|
assertEquals(0, sm.size()); |
507 |
|
assertTrue(sm.isEmpty()); |
508 |
< |
assertTrue(sm.remove(three) == null); |
508 |
> |
assertSame(sm.remove(three), null); |
509 |
|
assertEquals(4, map.size()); |
510 |
|
} |
511 |
|
|
577 |
|
SortedMap ssm = sm.tailMap(four); |
578 |
|
assertEquals(four, ssm.firstKey()); |
579 |
|
assertEquals(five, ssm.lastKey()); |
580 |
< |
assertTrue(ssm.remove(four) != null); |
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 |
< |
|
1122 |
> |
|
1123 |
|
} |