ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java
Revision: 1.10
Committed: Sat Nov 21 10:25:05 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +24 -35 lines
Log Message:
improve exception handling

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