ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java
Revision: 1.25
Committed: Sat Apr 25 04:55:30 2015 UTC (9 years ago) by jsr166
Branch: MAIN
Changes since 1.24: +1 -1 lines
Log Message:
improve main methods; respect system properties; actually fail if a test fails

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