ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java
Revision: 1.21
Committed: Tue Feb 21 01:54:04 2012 UTC (12 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.20: +2 -2 lines
Log Message:
use third person in javadoc first sentence

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 junit.framework.*;
8 import java.util.concurrent.ConcurrentNavigableMap;
9 import java.util.concurrent.ConcurrentSkipListMap;
10 import java.util.*;
11
12 public class ConcurrentSkipListSubMapTest 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(ConcurrentSkipListSubMapTest.class);
18 }
19
20 /**
21 * Creates a map from Integers 1-5 to Strings "A"-"E".
22 */
23 private static ConcurrentNavigableMap map5() {
24 ConcurrentSkipListMap map = new ConcurrentSkipListMap();
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 /**
39 * Creates a map from Integers -5 to -1 to Strings "A"-"E".
40 */
41 private static ConcurrentNavigableMap dmap5() {
42 ConcurrentSkipListMap map = new ConcurrentSkipListMap();
43 assertTrue(map.isEmpty());
44 map.put(m1, "A");
45 map.put(m5, "E");
46 map.put(m3, "C");
47 map.put(m2, "B");
48 map.put(m4, "D");
49 assertFalse(map.isEmpty());
50 assertEquals(5, map.size());
51 return map.descendingMap();
52 }
53
54 private static ConcurrentNavigableMap map0() {
55 ConcurrentSkipListMap map = new ConcurrentSkipListMap();
56 assertTrue(map.isEmpty());
57 return map.tailMap(one, true);
58 }
59
60 private static ConcurrentNavigableMap dmap0() {
61 ConcurrentSkipListMap map = new ConcurrentSkipListMap();
62 assertTrue(map.isEmpty());
63 return map;
64 }
65
66 /**
67 * clear removes all pairs
68 */
69 public void testClear() {
70 ConcurrentNavigableMap map = map5();
71 map.clear();
72 assertEquals(0, map.size());
73 }
74
75 /**
76 * Maps with same contents are equal
77 */
78 public void testEquals() {
79 ConcurrentNavigableMap map1 = map5();
80 ConcurrentNavigableMap map2 = map5();
81 assertEquals(map1, map2);
82 assertEquals(map2, map1);
83 map1.clear();
84 assertFalse(map1.equals(map2));
85 assertFalse(map2.equals(map1));
86 }
87
88 /**
89 * containsKey returns true for contained key
90 */
91 public void testContainsKey() {
92 ConcurrentNavigableMap map = map5();
93 assertTrue(map.containsKey(one));
94 assertFalse(map.containsKey(zero));
95 }
96
97 /**
98 * containsValue returns true for held values
99 */
100 public void testContainsValue() {
101 ConcurrentNavigableMap map = map5();
102 assertTrue(map.containsValue("A"));
103 assertFalse(map.containsValue("Z"));
104 }
105
106 /**
107 * get returns the correct element at the given key,
108 * or null if not present
109 */
110 public void testGet() {
111 ConcurrentNavigableMap map = map5();
112 assertEquals("A", (String)map.get(one));
113 ConcurrentNavigableMap empty = map0();
114 assertNull(empty.get(one));
115 }
116
117 /**
118 * isEmpty is true of empty map and false for non-empty
119 */
120 public void testIsEmpty() {
121 ConcurrentNavigableMap empty = map0();
122 ConcurrentNavigableMap map = map5();
123 assertTrue(empty.isEmpty());
124 assertFalse(map.isEmpty());
125 }
126
127 /**
128 * firstKey returns first key
129 */
130 public void testFirstKey() {
131 ConcurrentNavigableMap map = map5();
132 assertEquals(one, map.firstKey());
133 }
134
135 /**
136 * lastKey returns last key
137 */
138 public void testLastKey() {
139 ConcurrentNavigableMap map = map5();
140 assertEquals(five, map.lastKey());
141 }
142
143 /**
144 * keySet returns a Set containing all the keys
145 */
146 public void testKeySet() {
147 ConcurrentNavigableMap map = map5();
148 Set s = map.keySet();
149 assertEquals(5, s.size());
150 assertTrue(s.contains(one));
151 assertTrue(s.contains(two));
152 assertTrue(s.contains(three));
153 assertTrue(s.contains(four));
154 assertTrue(s.contains(five));
155 }
156
157 /**
158 * keySet is ordered
159 */
160 public void testKeySetOrder() {
161 ConcurrentNavigableMap map = map5();
162 Set s = map.keySet();
163 Iterator i = s.iterator();
164 Integer last = (Integer)i.next();
165 assertEquals(last, one);
166 while (i.hasNext()) {
167 Integer k = (Integer)i.next();
168 assertTrue(last.compareTo(k) < 0);
169 last = k;
170 }
171 }
172
173 /**
174 * values collection contains all values
175 */
176 public void testValues() {
177 ConcurrentNavigableMap map = map5();
178 Collection s = map.values();
179 assertEquals(5, s.size());
180 assertTrue(s.contains("A"));
181 assertTrue(s.contains("B"));
182 assertTrue(s.contains("C"));
183 assertTrue(s.contains("D"));
184 assertTrue(s.contains("E"));
185 }
186
187 /**
188 * keySet.toArray returns contains all keys
189 */
190 public void testKeySetToArray() {
191 ConcurrentNavigableMap map = map5();
192 Set s = map.keySet();
193 Object[] ar = s.toArray();
194 assertTrue(s.containsAll(Arrays.asList(ar)));
195 assertEquals(5, ar.length);
196 ar[0] = m10;
197 assertFalse(s.containsAll(Arrays.asList(ar)));
198 }
199
200 /**
201 * descendingkeySet.toArray returns contains all keys
202 */
203 public void testDescendingKeySetToArray() {
204 ConcurrentNavigableMap map = map5();
205 Set s = map.descendingKeySet();
206 Object[] ar = s.toArray();
207 assertEquals(5, ar.length);
208 assertTrue(s.containsAll(Arrays.asList(ar)));
209 ar[0] = m10;
210 assertFalse(s.containsAll(Arrays.asList(ar)));
211 }
212
213 /**
214 * Values.toArray contains all values
215 */
216 public void testValuesToArray() {
217 ConcurrentNavigableMap map = map5();
218 Collection v = map.values();
219 Object[] ar = v.toArray();
220 ArrayList s = new ArrayList(Arrays.asList(ar));
221 assertEquals(5, ar.length);
222 assertTrue(s.contains("A"));
223 assertTrue(s.contains("B"));
224 assertTrue(s.contains("C"));
225 assertTrue(s.contains("D"));
226 assertTrue(s.contains("E"));
227 }
228
229 /**
230 * entrySet contains all pairs
231 */
232 public void testEntrySet() {
233 ConcurrentNavigableMap map = map5();
234 Set s = map.entrySet();
235 assertEquals(5, s.size());
236 Iterator it = s.iterator();
237 while (it.hasNext()) {
238 Map.Entry e = (Map.Entry) it.next();
239 assertTrue(
240 (e.getKey().equals(one) && e.getValue().equals("A")) ||
241 (e.getKey().equals(two) && e.getValue().equals("B")) ||
242 (e.getKey().equals(three) && e.getValue().equals("C")) ||
243 (e.getKey().equals(four) && e.getValue().equals("D")) ||
244 (e.getKey().equals(five) && e.getValue().equals("E")));
245 }
246 }
247
248 /**
249 * putAll adds all key-value pairs from the given map
250 */
251 public void testPutAll() {
252 ConcurrentNavigableMap empty = map0();
253 ConcurrentNavigableMap map = map5();
254 empty.putAll(map);
255 assertEquals(5, empty.size());
256 assertTrue(empty.containsKey(one));
257 assertTrue(empty.containsKey(two));
258 assertTrue(empty.containsKey(three));
259 assertTrue(empty.containsKey(four));
260 assertTrue(empty.containsKey(five));
261 }
262
263 /**
264 * putIfAbsent works when the given key is not present
265 */
266 public void testPutIfAbsent() {
267 ConcurrentNavigableMap map = map5();
268 map.putIfAbsent(six, "Z");
269 assertTrue(map.containsKey(six));
270 }
271
272 /**
273 * putIfAbsent does not add the pair if the key is already present
274 */
275 public void testPutIfAbsent2() {
276 ConcurrentNavigableMap map = map5();
277 assertEquals("A", map.putIfAbsent(one, "Z"));
278 }
279
280 /**
281 * replace fails when the given key is not present
282 */
283 public void testReplace() {
284 ConcurrentNavigableMap map = map5();
285 assertNull(map.replace(six, "Z"));
286 assertFalse(map.containsKey(six));
287 }
288
289 /**
290 * replace succeeds if the key is already present
291 */
292 public void testReplace2() {
293 ConcurrentNavigableMap map = map5();
294 assertNotNull(map.replace(one, "Z"));
295 assertEquals("Z", map.get(one));
296 }
297
298 /**
299 * replace value fails when the given key not mapped to expected value
300 */
301 public void testReplaceValue() {
302 ConcurrentNavigableMap map = map5();
303 assertEquals("A", map.get(one));
304 assertFalse(map.replace(one, "Z", "Z"));
305 assertEquals("A", map.get(one));
306 }
307
308 /**
309 * replace value succeeds when the given key mapped to expected value
310 */
311 public void testReplaceValue2() {
312 ConcurrentNavigableMap map = map5();
313 assertEquals("A", map.get(one));
314 assertTrue(map.replace(one, "A", "Z"));
315 assertEquals("Z", map.get(one));
316 }
317
318 /**
319 * remove removes the correct key-value pair from the map
320 */
321 public void testRemove() {
322 ConcurrentNavigableMap map = map5();
323 map.remove(five);
324 assertEquals(4, map.size());
325 assertFalse(map.containsKey(five));
326 }
327
328 /**
329 * remove(key,value) removes only if pair present
330 */
331 public void testRemove2() {
332 ConcurrentNavigableMap map = map5();
333 assertTrue(map.containsKey(five));
334 assertEquals("E", map.get(five));
335 map.remove(five, "E");
336 assertEquals(4, map.size());
337 assertFalse(map.containsKey(five));
338 map.remove(four, "A");
339 assertEquals(4, map.size());
340 assertTrue(map.containsKey(four));
341 }
342
343 /**
344 * lowerEntry returns preceding entry.
345 */
346 public void testLowerEntry() {
347 ConcurrentNavigableMap map = map5();
348 Map.Entry e1 = map.lowerEntry(three);
349 assertEquals(two, e1.getKey());
350
351 Map.Entry e2 = map.lowerEntry(six);
352 assertEquals(five, e2.getKey());
353
354 Map.Entry e3 = map.lowerEntry(one);
355 assertNull(e3);
356
357 Map.Entry e4 = map.lowerEntry(zero);
358 assertNull(e4);
359 }
360
361 /**
362 * higherEntry returns next entry.
363 */
364 public void testHigherEntry() {
365 ConcurrentNavigableMap map = map5();
366 Map.Entry e1 = map.higherEntry(three);
367 assertEquals(four, e1.getKey());
368
369 Map.Entry e2 = map.higherEntry(zero);
370 assertEquals(one, e2.getKey());
371
372 Map.Entry e3 = map.higherEntry(five);
373 assertNull(e3);
374
375 Map.Entry e4 = map.higherEntry(six);
376 assertNull(e4);
377 }
378
379 /**
380 * floorEntry returns preceding entry.
381 */
382 public void testFloorEntry() {
383 ConcurrentNavigableMap map = map5();
384 Map.Entry e1 = map.floorEntry(three);
385 assertEquals(three, e1.getKey());
386
387 Map.Entry e2 = map.floorEntry(six);
388 assertEquals(five, e2.getKey());
389
390 Map.Entry e3 = map.floorEntry(one);
391 assertEquals(one, e3.getKey());
392
393 Map.Entry e4 = map.floorEntry(zero);
394 assertNull(e4);
395 }
396
397 /**
398 * ceilingEntry returns next entry.
399 */
400 public void testCeilingEntry() {
401 ConcurrentNavigableMap map = map5();
402 Map.Entry e1 = map.ceilingEntry(three);
403 assertEquals(three, e1.getKey());
404
405 Map.Entry e2 = map.ceilingEntry(zero);
406 assertEquals(one, e2.getKey());
407
408 Map.Entry e3 = map.ceilingEntry(five);
409 assertEquals(five, e3.getKey());
410
411 Map.Entry e4 = map.ceilingEntry(six);
412 assertNull(e4);
413 }
414
415 /**
416 * pollFirstEntry returns entries in order
417 */
418 public void testPollFirstEntry() {
419 ConcurrentNavigableMap map = map5();
420 Map.Entry e = map.pollFirstEntry();
421 assertEquals(one, e.getKey());
422 assertEquals("A", e.getValue());
423 e = map.pollFirstEntry();
424 assertEquals(two, e.getKey());
425 map.put(one, "A");
426 e = map.pollFirstEntry();
427 assertEquals(one, e.getKey());
428 assertEquals("A", e.getValue());
429 e = map.pollFirstEntry();
430 assertEquals(three, e.getKey());
431 map.remove(four);
432 e = map.pollFirstEntry();
433 assertEquals(five, e.getKey());
434 try {
435 e.setValue("A");
436 shouldThrow();
437 } catch (UnsupportedOperationException success) {}
438 e = map.pollFirstEntry();
439 assertNull(e);
440 }
441
442 /**
443 * pollLastEntry returns entries in order
444 */
445 public void testPollLastEntry() {
446 ConcurrentNavigableMap map = map5();
447 Map.Entry e = map.pollLastEntry();
448 assertEquals(five, e.getKey());
449 assertEquals("E", e.getValue());
450 e = map.pollLastEntry();
451 assertEquals(four, e.getKey());
452 map.put(five, "E");
453 e = map.pollLastEntry();
454 assertEquals(five, e.getKey());
455 assertEquals("E", e.getValue());
456 e = map.pollLastEntry();
457 assertEquals(three, e.getKey());
458 map.remove(two);
459 e = map.pollLastEntry();
460 assertEquals(one, e.getKey());
461 try {
462 e.setValue("E");
463 shouldThrow();
464 } catch (UnsupportedOperationException success) {}
465 e = map.pollLastEntry();
466 assertNull(e);
467 }
468
469 /**
470 * size returns the correct values
471 */
472 public void testSize() {
473 ConcurrentNavigableMap map = map5();
474 ConcurrentNavigableMap empty = map0();
475 assertEquals(0, empty.size());
476 assertEquals(5, map.size());
477 }
478
479 /**
480 * toString contains toString of elements
481 */
482 public void testToString() {
483 ConcurrentNavigableMap map = map5();
484 String s = map.toString();
485 for (int i = 1; i <= 5; ++i) {
486 assertTrue(s.contains(String.valueOf(i)));
487 }
488 }
489
490 // Exception tests
491
492 /**
493 * get(null) of nonempty map throws NPE
494 */
495 public void testGet_NullPointerException() {
496 try {
497 ConcurrentNavigableMap c = map5();
498 c.get(null);
499 shouldThrow();
500 } catch (NullPointerException success) {}
501 }
502
503 /**
504 * containsKey(null) of nonempty map throws NPE
505 */
506 public void testContainsKey_NullPointerException() {
507 try {
508 ConcurrentNavigableMap c = map5();
509 c.containsKey(null);
510 shouldThrow();
511 } catch (NullPointerException success) {}
512 }
513
514 /**
515 * containsValue(null) throws NPE
516 */
517 public void testContainsValue_NullPointerException() {
518 try {
519 ConcurrentNavigableMap c = map0();
520 c.containsValue(null);
521 shouldThrow();
522 } catch (NullPointerException success) {}
523 }
524
525 /**
526 * put(null,x) throws NPE
527 */
528 public void testPut1_NullPointerException() {
529 try {
530 ConcurrentNavigableMap c = map5();
531 c.put(null, "whatever");
532 shouldThrow();
533 } catch (NullPointerException success) {}
534 }
535
536 /**
537 * putIfAbsent(null, x) throws NPE
538 */
539 public void testPutIfAbsent1_NullPointerException() {
540 try {
541 ConcurrentNavigableMap c = map5();
542 c.putIfAbsent(null, "whatever");
543 shouldThrow();
544 } catch (NullPointerException success) {}
545 }
546
547 /**
548 * replace(null, x) throws NPE
549 */
550 public void testReplace_NullPointerException() {
551 try {
552 ConcurrentNavigableMap c = map5();
553 c.replace(null, "whatever");
554 shouldThrow();
555 } catch (NullPointerException success) {}
556 }
557
558 /**
559 * replace(null, x, y) throws NPE
560 */
561 public void testReplaceValue_NullPointerException() {
562 try {
563 ConcurrentNavigableMap c = map5();
564 c.replace(null, one, "whatever");
565 shouldThrow();
566 } catch (NullPointerException success) {}
567 }
568
569 /**
570 * remove(null) throws NPE
571 */
572 public void testRemove1_NullPointerException() {
573 try {
574 ConcurrentNavigableMap c = map5();
575 c.remove(null);
576 shouldThrow();
577 } catch (NullPointerException success) {}
578 }
579
580 /**
581 * remove(null, x) throws NPE
582 */
583 public void testRemove2_NullPointerException() {
584 try {
585 ConcurrentNavigableMap c = map5();
586 c.remove(null, "whatever");
587 shouldThrow();
588 } catch (NullPointerException success) {}
589 }
590
591 /**
592 * A deserialized map equals original
593 */
594 public void testSerialization() throws Exception {
595 NavigableMap x = map5();
596 NavigableMap y = serialClone(x);
597
598 assertTrue(x != y);
599 assertEquals(x.size(), y.size());
600 assertEquals(x.toString(), y.toString());
601 assertEquals(x, y);
602 assertEquals(y, x);
603 }
604
605 /**
606 * subMap returns map with keys in requested range
607 */
608 public void testSubMapContents() {
609 ConcurrentNavigableMap map = map5();
610 SortedMap sm = map.subMap(two, four);
611 assertEquals(two, sm.firstKey());
612 assertEquals(three, sm.lastKey());
613 assertEquals(2, sm.size());
614 assertFalse(sm.containsKey(one));
615 assertTrue(sm.containsKey(two));
616 assertTrue(sm.containsKey(three));
617 assertFalse(sm.containsKey(four));
618 assertFalse(sm.containsKey(five));
619 Iterator i = sm.keySet().iterator();
620 Object k;
621 k = (Integer)(i.next());
622 assertEquals(two, k);
623 k = (Integer)(i.next());
624 assertEquals(three, k);
625 assertFalse(i.hasNext());
626 Iterator j = sm.keySet().iterator();
627 j.next();
628 j.remove();
629 assertFalse(map.containsKey(two));
630 assertEquals(4, map.size());
631 assertEquals(1, sm.size());
632 assertEquals(three, sm.firstKey());
633 assertEquals(three, sm.lastKey());
634 assertEquals("C", sm.remove(three));
635 assertTrue(sm.isEmpty());
636 assertEquals(3, map.size());
637 }
638
639 public void testSubMapContents2() {
640 ConcurrentNavigableMap map = map5();
641 SortedMap sm = map.subMap(two, three);
642 assertEquals(1, sm.size());
643 assertEquals(two, sm.firstKey());
644 assertEquals(two, sm.lastKey());
645 assertFalse(sm.containsKey(one));
646 assertTrue(sm.containsKey(two));
647 assertFalse(sm.containsKey(three));
648 assertFalse(sm.containsKey(four));
649 assertFalse(sm.containsKey(five));
650 Iterator i = sm.keySet().iterator();
651 Object k;
652 k = (Integer)(i.next());
653 assertEquals(two, k);
654 assertFalse(i.hasNext());
655 Iterator j = sm.keySet().iterator();
656 j.next();
657 j.remove();
658 assertFalse(map.containsKey(two));
659 assertEquals(4, map.size());
660 assertEquals(0, sm.size());
661 assertTrue(sm.isEmpty());
662 assertSame(sm.remove(three), null);
663 assertEquals(4, map.size());
664 }
665
666 /**
667 * headMap returns map with keys in requested range
668 */
669 public void testHeadMapContents() {
670 ConcurrentNavigableMap map = map5();
671 SortedMap sm = map.headMap(four);
672 assertTrue(sm.containsKey(one));
673 assertTrue(sm.containsKey(two));
674 assertTrue(sm.containsKey(three));
675 assertFalse(sm.containsKey(four));
676 assertFalse(sm.containsKey(five));
677 Iterator i = sm.keySet().iterator();
678 Object k;
679 k = (Integer)(i.next());
680 assertEquals(one, k);
681 k = (Integer)(i.next());
682 assertEquals(two, k);
683 k = (Integer)(i.next());
684 assertEquals(three, k);
685 assertFalse(i.hasNext());
686 sm.clear();
687 assertTrue(sm.isEmpty());
688 assertEquals(2, map.size());
689 assertEquals(four, map.firstKey());
690 }
691
692 /**
693 * headMap returns map with keys in requested range
694 */
695 public void testTailMapContents() {
696 ConcurrentNavigableMap map = map5();
697 SortedMap sm = map.tailMap(two);
698 assertFalse(sm.containsKey(one));
699 assertTrue(sm.containsKey(two));
700 assertTrue(sm.containsKey(three));
701 assertTrue(sm.containsKey(four));
702 assertTrue(sm.containsKey(five));
703 Iterator i = sm.keySet().iterator();
704 Object k;
705 k = (Integer)(i.next());
706 assertEquals(two, k);
707 k = (Integer)(i.next());
708 assertEquals(three, k);
709 k = (Integer)(i.next());
710 assertEquals(four, k);
711 k = (Integer)(i.next());
712 assertEquals(five, k);
713 assertFalse(i.hasNext());
714
715 Iterator ei = sm.entrySet().iterator();
716 Map.Entry e;
717 e = (Map.Entry)(ei.next());
718 assertEquals(two, e.getKey());
719 assertEquals("B", e.getValue());
720 e = (Map.Entry)(ei.next());
721 assertEquals(three, e.getKey());
722 assertEquals("C", e.getValue());
723 e = (Map.Entry)(ei.next());
724 assertEquals(four, e.getKey());
725 assertEquals("D", e.getValue());
726 e = (Map.Entry)(ei.next());
727 assertEquals(five, e.getKey());
728 assertEquals("E", e.getValue());
729 assertFalse(i.hasNext());
730
731 SortedMap ssm = sm.tailMap(four);
732 assertEquals(four, ssm.firstKey());
733 assertEquals(five, ssm.lastKey());
734 assertEquals("D", ssm.remove(four));
735 assertEquals(1, ssm.size());
736 assertEquals(3, sm.size());
737 assertEquals(4, map.size());
738 }
739
740 /**
741 * clear removes all pairs
742 */
743 public void testDescendingClear() {
744 ConcurrentNavigableMap map = dmap5();
745 map.clear();
746 assertEquals(0, map.size());
747 }
748
749 /**
750 * Maps with same contents are equal
751 */
752 public void testDescendingEquals() {
753 ConcurrentNavigableMap map1 = dmap5();
754 ConcurrentNavigableMap map2 = dmap5();
755 assertEquals(map1, map2);
756 assertEquals(map2, map1);
757 map1.clear();
758 assertFalse(map1.equals(map2));
759 assertFalse(map2.equals(map1));
760 }
761
762 /**
763 * containsKey returns true for contained key
764 */
765 public void testDescendingContainsKey() {
766 ConcurrentNavigableMap map = dmap5();
767 assertTrue(map.containsKey(m1));
768 assertFalse(map.containsKey(zero));
769 }
770
771 /**
772 * containsValue returns true for held values
773 */
774 public void testDescendingContainsValue() {
775 ConcurrentNavigableMap map = dmap5();
776 assertTrue(map.containsValue("A"));
777 assertFalse(map.containsValue("Z"));
778 }
779
780 /**
781 * get returns the correct element at the given key,
782 * or null if not present
783 */
784 public void testDescendingGet() {
785 ConcurrentNavigableMap map = dmap5();
786 assertEquals("A", (String)map.get(m1));
787 ConcurrentNavigableMap empty = dmap0();
788 assertNull(empty.get(m1));
789 }
790
791 /**
792 * isEmpty is true of empty map and false for non-empty
793 */
794 public void testDescendingIsEmpty() {
795 ConcurrentNavigableMap empty = dmap0();
796 ConcurrentNavigableMap map = dmap5();
797 assertTrue(empty.isEmpty());
798 assertFalse(map.isEmpty());
799 }
800
801 /**
802 * firstKey returns first key
803 */
804 public void testDescendingFirstKey() {
805 ConcurrentNavigableMap map = dmap5();
806 assertEquals(m1, map.firstKey());
807 }
808
809 /**
810 * lastKey returns last key
811 */
812 public void testDescendingLastKey() {
813 ConcurrentNavigableMap map = dmap5();
814 assertEquals(m5, map.lastKey());
815 }
816
817 /**
818 * keySet returns a Set containing all the keys
819 */
820 public void testDescendingKeySet() {
821 ConcurrentNavigableMap map = dmap5();
822 Set s = map.keySet();
823 assertEquals(5, s.size());
824 assertTrue(s.contains(m1));
825 assertTrue(s.contains(m2));
826 assertTrue(s.contains(m3));
827 assertTrue(s.contains(m4));
828 assertTrue(s.contains(m5));
829 }
830
831 /**
832 * keySet is ordered
833 */
834 public void testDescendingKeySetOrder() {
835 ConcurrentNavigableMap map = dmap5();
836 Set s = map.keySet();
837 Iterator i = s.iterator();
838 Integer last = (Integer)i.next();
839 assertEquals(last, m1);
840 while (i.hasNext()) {
841 Integer k = (Integer)i.next();
842 assertTrue(last.compareTo(k) > 0);
843 last = k;
844 }
845 }
846
847 /**
848 * values collection contains all values
849 */
850 public void testDescendingValues() {
851 ConcurrentNavigableMap map = dmap5();
852 Collection s = map.values();
853 assertEquals(5, s.size());
854 assertTrue(s.contains("A"));
855 assertTrue(s.contains("B"));
856 assertTrue(s.contains("C"));
857 assertTrue(s.contains("D"));
858 assertTrue(s.contains("E"));
859 }
860
861 /**
862 * keySet.toArray returns contains all keys
863 */
864 public void testDescendingAscendingKeySetToArray() {
865 ConcurrentNavigableMap map = dmap5();
866 Set s = map.keySet();
867 Object[] ar = s.toArray();
868 assertTrue(s.containsAll(Arrays.asList(ar)));
869 assertEquals(5, ar.length);
870 ar[0] = m10;
871 assertFalse(s.containsAll(Arrays.asList(ar)));
872 }
873
874 /**
875 * descendingkeySet.toArray returns contains all keys
876 */
877 public void testDescendingDescendingKeySetToArray() {
878 ConcurrentNavigableMap map = dmap5();
879 Set s = map.descendingKeySet();
880 Object[] ar = s.toArray();
881 assertEquals(5, ar.length);
882 assertTrue(s.containsAll(Arrays.asList(ar)));
883 ar[0] = m10;
884 assertFalse(s.containsAll(Arrays.asList(ar)));
885 }
886
887 /**
888 * Values.toArray contains all values
889 */
890 public void testDescendingValuesToArray() {
891 ConcurrentNavigableMap map = dmap5();
892 Collection v = map.values();
893 Object[] ar = v.toArray();
894 ArrayList s = new ArrayList(Arrays.asList(ar));
895 assertEquals(5, ar.length);
896 assertTrue(s.contains("A"));
897 assertTrue(s.contains("B"));
898 assertTrue(s.contains("C"));
899 assertTrue(s.contains("D"));
900 assertTrue(s.contains("E"));
901 }
902
903 /**
904 * entrySet contains all pairs
905 */
906 public void testDescendingEntrySet() {
907 ConcurrentNavigableMap map = dmap5();
908 Set s = map.entrySet();
909 assertEquals(5, s.size());
910 Iterator it = s.iterator();
911 while (it.hasNext()) {
912 Map.Entry e = (Map.Entry) it.next();
913 assertTrue(
914 (e.getKey().equals(m1) && e.getValue().equals("A")) ||
915 (e.getKey().equals(m2) && e.getValue().equals("B")) ||
916 (e.getKey().equals(m3) && e.getValue().equals("C")) ||
917 (e.getKey().equals(m4) && e.getValue().equals("D")) ||
918 (e.getKey().equals(m5) && e.getValue().equals("E")));
919 }
920 }
921
922 /**
923 * putAll adds all key-value pairs from the given map
924 */
925 public void testDescendingPutAll() {
926 ConcurrentNavigableMap empty = dmap0();
927 ConcurrentNavigableMap map = dmap5();
928 empty.putAll(map);
929 assertEquals(5, empty.size());
930 assertTrue(empty.containsKey(m1));
931 assertTrue(empty.containsKey(m2));
932 assertTrue(empty.containsKey(m3));
933 assertTrue(empty.containsKey(m4));
934 assertTrue(empty.containsKey(m5));
935 }
936
937 /**
938 * putIfAbsent works when the given key is not present
939 */
940 public void testDescendingPutIfAbsent() {
941 ConcurrentNavigableMap map = dmap5();
942 map.putIfAbsent(six, "Z");
943 assertTrue(map.containsKey(six));
944 }
945
946 /**
947 * putIfAbsent does not add the pair if the key is already present
948 */
949 public void testDescendingPutIfAbsent2() {
950 ConcurrentNavigableMap map = dmap5();
951 assertEquals("A", map.putIfAbsent(m1, "Z"));
952 }
953
954 /**
955 * replace fails when the given key is not present
956 */
957 public void testDescendingReplace() {
958 ConcurrentNavigableMap map = dmap5();
959 assertNull(map.replace(six, "Z"));
960 assertFalse(map.containsKey(six));
961 }
962
963 /**
964 * replace succeeds if the key is already present
965 */
966 public void testDescendingReplace2() {
967 ConcurrentNavigableMap map = dmap5();
968 assertNotNull(map.replace(m1, "Z"));
969 assertEquals("Z", map.get(m1));
970 }
971
972 /**
973 * replace value fails when the given key not mapped to expected value
974 */
975 public void testDescendingReplaceValue() {
976 ConcurrentNavigableMap map = dmap5();
977 assertEquals("A", map.get(m1));
978 assertFalse(map.replace(m1, "Z", "Z"));
979 assertEquals("A", map.get(m1));
980 }
981
982 /**
983 * replace value succeeds when the given key mapped to expected value
984 */
985 public void testDescendingReplaceValue2() {
986 ConcurrentNavigableMap map = dmap5();
987 assertEquals("A", map.get(m1));
988 assertTrue(map.replace(m1, "A", "Z"));
989 assertEquals("Z", map.get(m1));
990 }
991
992 /**
993 * remove removes the correct key-value pair from the map
994 */
995 public void testDescendingRemove() {
996 ConcurrentNavigableMap map = dmap5();
997 map.remove(m5);
998 assertEquals(4, map.size());
999 assertFalse(map.containsKey(m5));
1000 }
1001
1002 /**
1003 * remove(key,value) removes only if pair present
1004 */
1005 public void testDescendingRemove2() {
1006 ConcurrentNavigableMap map = dmap5();
1007 assertTrue(map.containsKey(m5));
1008 assertEquals("E", map.get(m5));
1009 map.remove(m5, "E");
1010 assertEquals(4, map.size());
1011 assertFalse(map.containsKey(m5));
1012 map.remove(m4, "A");
1013 assertEquals(4, map.size());
1014 assertTrue(map.containsKey(m4));
1015 }
1016
1017 /**
1018 * lowerEntry returns preceding entry.
1019 */
1020 public void testDescendingLowerEntry() {
1021 ConcurrentNavigableMap map = dmap5();
1022 Map.Entry e1 = map.lowerEntry(m3);
1023 assertEquals(m2, e1.getKey());
1024
1025 Map.Entry e2 = map.lowerEntry(m6);
1026 assertEquals(m5, e2.getKey());
1027
1028 Map.Entry e3 = map.lowerEntry(m1);
1029 assertNull(e3);
1030
1031 Map.Entry e4 = map.lowerEntry(zero);
1032 assertNull(e4);
1033 }
1034
1035 /**
1036 * higherEntry returns next entry.
1037 */
1038 public void testDescendingHigherEntry() {
1039 ConcurrentNavigableMap map = dmap5();
1040 Map.Entry e1 = map.higherEntry(m3);
1041 assertEquals(m4, e1.getKey());
1042
1043 Map.Entry e2 = map.higherEntry(zero);
1044 assertEquals(m1, e2.getKey());
1045
1046 Map.Entry e3 = map.higherEntry(m5);
1047 assertNull(e3);
1048
1049 Map.Entry e4 = map.higherEntry(m6);
1050 assertNull(e4);
1051 }
1052
1053 /**
1054 * floorEntry returns preceding entry.
1055 */
1056 public void testDescendingFloorEntry() {
1057 ConcurrentNavigableMap map = dmap5();
1058 Map.Entry e1 = map.floorEntry(m3);
1059 assertEquals(m3, e1.getKey());
1060
1061 Map.Entry e2 = map.floorEntry(m6);
1062 assertEquals(m5, e2.getKey());
1063
1064 Map.Entry e3 = map.floorEntry(m1);
1065 assertEquals(m1, e3.getKey());
1066
1067 Map.Entry e4 = map.floorEntry(zero);
1068 assertNull(e4);
1069 }
1070
1071 /**
1072 * ceilingEntry returns next entry.
1073 */
1074 public void testDescendingCeilingEntry() {
1075 ConcurrentNavigableMap map = dmap5();
1076 Map.Entry e1 = map.ceilingEntry(m3);
1077 assertEquals(m3, e1.getKey());
1078
1079 Map.Entry e2 = map.ceilingEntry(zero);
1080 assertEquals(m1, e2.getKey());
1081
1082 Map.Entry e3 = map.ceilingEntry(m5);
1083 assertEquals(m5, e3.getKey());
1084
1085 Map.Entry e4 = map.ceilingEntry(m6);
1086 assertNull(e4);
1087 }
1088
1089 /**
1090 * pollFirstEntry returns entries in order
1091 */
1092 public void testDescendingPollFirstEntry() {
1093 ConcurrentNavigableMap map = dmap5();
1094 Map.Entry e = map.pollFirstEntry();
1095 assertEquals(m1, e.getKey());
1096 assertEquals("A", e.getValue());
1097 e = map.pollFirstEntry();
1098 assertEquals(m2, e.getKey());
1099 map.put(m1, "A");
1100 e = map.pollFirstEntry();
1101 assertEquals(m1, e.getKey());
1102 assertEquals("A", e.getValue());
1103 e = map.pollFirstEntry();
1104 assertEquals(m3, e.getKey());
1105 map.remove(m4);
1106 e = map.pollFirstEntry();
1107 assertEquals(m5, e.getKey());
1108 try {
1109 e.setValue("A");
1110 shouldThrow();
1111 } catch (UnsupportedOperationException success) {}
1112 e = map.pollFirstEntry();
1113 assertNull(e);
1114 }
1115
1116 /**
1117 * pollLastEntry returns entries in order
1118 */
1119 public void testDescendingPollLastEntry() {
1120 ConcurrentNavigableMap map = dmap5();
1121 Map.Entry e = map.pollLastEntry();
1122 assertEquals(m5, e.getKey());
1123 assertEquals("E", e.getValue());
1124 e = map.pollLastEntry();
1125 assertEquals(m4, e.getKey());
1126 map.put(m5, "E");
1127 e = map.pollLastEntry();
1128 assertEquals(m5, e.getKey());
1129 assertEquals("E", e.getValue());
1130 e = map.pollLastEntry();
1131 assertEquals(m3, e.getKey());
1132 map.remove(m2);
1133 e = map.pollLastEntry();
1134 assertEquals(m1, e.getKey());
1135 try {
1136 e.setValue("E");
1137 shouldThrow();
1138 } catch (UnsupportedOperationException success) {}
1139 e = map.pollLastEntry();
1140 assertNull(e);
1141 }
1142
1143 /**
1144 * size returns the correct values
1145 */
1146 public void testDescendingSize() {
1147 ConcurrentNavigableMap map = dmap5();
1148 ConcurrentNavigableMap empty = dmap0();
1149 assertEquals(0, empty.size());
1150 assertEquals(5, map.size());
1151 }
1152
1153 /**
1154 * toString contains toString of elements
1155 */
1156 public void testDescendingToString() {
1157 ConcurrentNavigableMap map = dmap5();
1158 String s = map.toString();
1159 for (int i = 1; i <= 5; ++i) {
1160 assertTrue(s.contains(String.valueOf(i)));
1161 }
1162 }
1163
1164 // Exception testDescendings
1165
1166 /**
1167 * get(null) of empty map throws NPE
1168 */
1169 public void testDescendingGet_NullPointerException() {
1170 try {
1171 ConcurrentNavigableMap c = dmap5();
1172 c.get(null);
1173 shouldThrow();
1174 } catch (NullPointerException success) {}
1175 }
1176
1177 /**
1178 * containsKey(null) of empty map throws NPE
1179 */
1180 public void testDescendingContainsKey_NullPointerException() {
1181 try {
1182 ConcurrentNavigableMap c = dmap5();
1183 c.containsKey(null);
1184 shouldThrow();
1185 } catch (NullPointerException success) {}
1186 }
1187
1188 /**
1189 * containsValue(null) throws NPE
1190 */
1191 public void testDescendingContainsValue_NullPointerException() {
1192 try {
1193 ConcurrentNavigableMap c = dmap0();
1194 c.containsValue(null);
1195 shouldThrow();
1196 } catch (NullPointerException success) {}
1197 }
1198
1199 /**
1200 * put(null,x) throws NPE
1201 */
1202 public void testDescendingPut1_NullPointerException() {
1203 try {
1204 ConcurrentNavigableMap c = dmap5();
1205 c.put(null, "whatever");
1206 shouldThrow();
1207 } catch (NullPointerException success) {}
1208 }
1209
1210 /**
1211 * putIfAbsent(null, x) throws NPE
1212 */
1213 public void testDescendingPutIfAbsent1_NullPointerException() {
1214 try {
1215 ConcurrentNavigableMap c = dmap5();
1216 c.putIfAbsent(null, "whatever");
1217 shouldThrow();
1218 } catch (NullPointerException success) {}
1219 }
1220
1221 /**
1222 * replace(null, x) throws NPE
1223 */
1224 public void testDescendingReplace_NullPointerException() {
1225 try {
1226 ConcurrentNavigableMap c = dmap5();
1227 c.replace(null, "whatever");
1228 shouldThrow();
1229 } catch (NullPointerException success) {}
1230 }
1231
1232 /**
1233 * replace(null, x, y) throws NPE
1234 */
1235 public void testDescendingReplaceValue_NullPointerException() {
1236 try {
1237 ConcurrentNavigableMap c = dmap5();
1238 c.replace(null, m1, "whatever");
1239 shouldThrow();
1240 } catch (NullPointerException success) {}
1241 }
1242
1243 /**
1244 * remove(null) throws NPE
1245 */
1246 public void testDescendingRemove1_NullPointerException() {
1247 try {
1248 ConcurrentNavigableMap c = dmap5();
1249 c.remove(null);
1250 shouldThrow();
1251 } catch (NullPointerException success) {}
1252 }
1253
1254 /**
1255 * remove(null, x) throws NPE
1256 */
1257 public void testDescendingRemove2_NullPointerException() {
1258 try {
1259 ConcurrentNavigableMap c = dmap5();
1260 c.remove(null, "whatever");
1261 shouldThrow();
1262 } catch (NullPointerException success) {}
1263 }
1264
1265 /**
1266 * A deserialized map equals original
1267 */
1268 public void testDescendingSerialization() throws Exception {
1269 NavigableMap x = dmap5();
1270 NavigableMap y = serialClone(x);
1271
1272 assertTrue(x != y);
1273 assertEquals(x.size(), y.size());
1274 assertEquals(x.toString(), y.toString());
1275 assertEquals(x, y);
1276 assertEquals(y, x);
1277 }
1278
1279 /**
1280 * subMap returns map with keys in requested range
1281 */
1282 public void testDescendingSubMapContents() {
1283 ConcurrentNavigableMap map = dmap5();
1284 SortedMap sm = map.subMap(m2, m4);
1285 assertEquals(m2, sm.firstKey());
1286 assertEquals(m3, sm.lastKey());
1287 assertEquals(2, sm.size());
1288 assertFalse(sm.containsKey(m1));
1289 assertTrue(sm.containsKey(m2));
1290 assertTrue(sm.containsKey(m3));
1291 assertFalse(sm.containsKey(m4));
1292 assertFalse(sm.containsKey(m5));
1293 Iterator i = sm.keySet().iterator();
1294 Object k;
1295 k = (Integer)(i.next());
1296 assertEquals(m2, k);
1297 k = (Integer)(i.next());
1298 assertEquals(m3, k);
1299 assertFalse(i.hasNext());
1300 Iterator j = sm.keySet().iterator();
1301 j.next();
1302 j.remove();
1303 assertFalse(map.containsKey(m2));
1304 assertEquals(4, map.size());
1305 assertEquals(1, sm.size());
1306 assertEquals(m3, sm.firstKey());
1307 assertEquals(m3, sm.lastKey());
1308 assertEquals("C", sm.remove(m3));
1309 assertTrue(sm.isEmpty());
1310 assertEquals(3, map.size());
1311 }
1312
1313 public void testDescendingSubMapContents2() {
1314 ConcurrentNavigableMap map = dmap5();
1315 SortedMap sm = map.subMap(m2, m3);
1316 assertEquals(1, sm.size());
1317 assertEquals(m2, sm.firstKey());
1318 assertEquals(m2, sm.lastKey());
1319 assertFalse(sm.containsKey(m1));
1320 assertTrue(sm.containsKey(m2));
1321 assertFalse(sm.containsKey(m3));
1322 assertFalse(sm.containsKey(m4));
1323 assertFalse(sm.containsKey(m5));
1324 Iterator i = sm.keySet().iterator();
1325 Object k;
1326 k = (Integer)(i.next());
1327 assertEquals(m2, k);
1328 assertFalse(i.hasNext());
1329 Iterator j = sm.keySet().iterator();
1330 j.next();
1331 j.remove();
1332 assertFalse(map.containsKey(m2));
1333 assertEquals(4, map.size());
1334 assertEquals(0, sm.size());
1335 assertTrue(sm.isEmpty());
1336 assertSame(sm.remove(m3), null);
1337 assertEquals(4, map.size());
1338 }
1339
1340 /**
1341 * headMap returns map with keys in requested range
1342 */
1343 public void testDescendingHeadMapContents() {
1344 ConcurrentNavigableMap map = dmap5();
1345 SortedMap sm = map.headMap(m4);
1346 assertTrue(sm.containsKey(m1));
1347 assertTrue(sm.containsKey(m2));
1348 assertTrue(sm.containsKey(m3));
1349 assertFalse(sm.containsKey(m4));
1350 assertFalse(sm.containsKey(m5));
1351 Iterator i = sm.keySet().iterator();
1352 Object k;
1353 k = (Integer)(i.next());
1354 assertEquals(m1, k);
1355 k = (Integer)(i.next());
1356 assertEquals(m2, k);
1357 k = (Integer)(i.next());
1358 assertEquals(m3, k);
1359 assertFalse(i.hasNext());
1360 sm.clear();
1361 assertTrue(sm.isEmpty());
1362 assertEquals(2, map.size());
1363 assertEquals(m4, map.firstKey());
1364 }
1365
1366 /**
1367 * headMap returns map with keys in requested range
1368 */
1369 public void testDescendingTailMapContents() {
1370 ConcurrentNavigableMap map = dmap5();
1371 SortedMap sm = map.tailMap(m2);
1372 assertFalse(sm.containsKey(m1));
1373 assertTrue(sm.containsKey(m2));
1374 assertTrue(sm.containsKey(m3));
1375 assertTrue(sm.containsKey(m4));
1376 assertTrue(sm.containsKey(m5));
1377 Iterator i = sm.keySet().iterator();
1378 Object k;
1379 k = (Integer)(i.next());
1380 assertEquals(m2, k);
1381 k = (Integer)(i.next());
1382 assertEquals(m3, k);
1383 k = (Integer)(i.next());
1384 assertEquals(m4, k);
1385 k = (Integer)(i.next());
1386 assertEquals(m5, k);
1387 assertFalse(i.hasNext());
1388
1389 Iterator ei = sm.entrySet().iterator();
1390 Map.Entry e;
1391 e = (Map.Entry)(ei.next());
1392 assertEquals(m2, e.getKey());
1393 assertEquals("B", e.getValue());
1394 e = (Map.Entry)(ei.next());
1395 assertEquals(m3, e.getKey());
1396 assertEquals("C", e.getValue());
1397 e = (Map.Entry)(ei.next());
1398 assertEquals(m4, e.getKey());
1399 assertEquals("D", e.getValue());
1400 e = (Map.Entry)(ei.next());
1401 assertEquals(m5, e.getKey());
1402 assertEquals("E", e.getValue());
1403 assertFalse(i.hasNext());
1404
1405 SortedMap ssm = sm.tailMap(m4);
1406 assertEquals(m4, ssm.firstKey());
1407 assertEquals(m5, ssm.lastKey());
1408 assertEquals("D", ssm.remove(m4));
1409 assertEquals(1, ssm.size());
1410 assertEquals(3, sm.size());
1411 assertEquals(4, map.size());
1412 }
1413
1414 }