ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/TreeSubMapTest.java
Revision: 1.26
Committed: Wed Jan 27 01:57:25 2021 UTC (3 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.25: +5 -5 lines
Log Message:
use diamond <> pervasively

File Contents

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