ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java
Revision: 1.12
Committed: Tue Dec 1 06:03:49 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +6 -6 lines
Log Message:
Use MILLISECONDS.toNanos instead of multiplying by 1000*1000; use explicit assertEquals instead of assertTrue(...!= null); improve testPutWithTake

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 (UnsupportedOperationException success) {}
448 e = map.pollFirstEntry();
449 assertNull(e);
450 }
451
452 /**
453 * pollLastEntry returns entries in order
454 */
455 public void testPollLastEntry() {
456 ConcurrentNavigableMap map = map5();
457 Map.Entry e = map.pollLastEntry();
458 assertEquals(five, e.getKey());
459 assertEquals("E", e.getValue());
460 e = map.pollLastEntry();
461 assertEquals(four, e.getKey());
462 map.put(five, "E");
463 e = map.pollLastEntry();
464 assertEquals(five, e.getKey());
465 assertEquals("E", e.getValue());
466 e = map.pollLastEntry();
467 assertEquals(three, e.getKey());
468 map.remove(two);
469 e = map.pollLastEntry();
470 assertEquals(one, e.getKey());
471 try {
472 e.setValue("E");
473 shouldThrow();
474 } catch (UnsupportedOperationException success) {}
475 e = map.pollLastEntry();
476 assertNull(e);
477 }
478
479 /**
480 * size returns the correct values
481 */
482 public void testSize() {
483 ConcurrentNavigableMap map = map5();
484 ConcurrentNavigableMap empty = map0();
485 assertEquals(0, empty.size());
486 assertEquals(5, map.size());
487 }
488
489 /**
490 * toString contains toString of elements
491 */
492 public void testToString() {
493 ConcurrentNavigableMap map = map5();
494 String s = map.toString();
495 for (int i = 1; i <= 5; ++i) {
496 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
497 }
498 }
499
500 // Exception tests
501
502 /**
503 * get(null) of nonempty map throws NPE
504 */
505 public void testGet_NullPointerException() {
506 try {
507 ConcurrentNavigableMap c = map5();
508 c.get(null);
509 shouldThrow();
510 } catch (NullPointerException success) {}
511 }
512
513 /**
514 * containsKey(null) of nonempty map throws NPE
515 */
516 public void testContainsKey_NullPointerException() {
517 try {
518 ConcurrentNavigableMap c = map5();
519 c.containsKey(null);
520 shouldThrow();
521 } catch (NullPointerException success) {}
522 }
523
524 /**
525 * containsValue(null) throws NPE
526 */
527 public void testContainsValue_NullPointerException() {
528 try {
529 ConcurrentNavigableMap c = map0();
530 c.containsValue(null);
531 shouldThrow();
532 } catch (NullPointerException success) {}
533 }
534
535
536 /**
537 * put(null,x) throws NPE
538 */
539 public void testPut1_NullPointerException() {
540 try {
541 ConcurrentNavigableMap c = map5();
542 c.put(null, "whatever");
543 shouldThrow();
544 } catch (NullPointerException success) {}
545 }
546
547 /**
548 * putIfAbsent(null, x) throws NPE
549 */
550 public void testPutIfAbsent1_NullPointerException() {
551 try {
552 ConcurrentNavigableMap c = map5();
553 c.putIfAbsent(null, "whatever");
554 shouldThrow();
555 } catch (NullPointerException success) {}
556 }
557
558 /**
559 * replace(null, x) throws NPE
560 */
561 public void testReplace_NullPointerException() {
562 try {
563 ConcurrentNavigableMap c = map5();
564 c.replace(null, "whatever");
565 shouldThrow();
566 } catch (NullPointerException success) {}
567 }
568
569 /**
570 * replace(null, x, y) throws NPE
571 */
572 public void testReplaceValue_NullPointerException() {
573 try {
574 ConcurrentNavigableMap c = map5();
575 c.replace(null, one, "whatever");
576 shouldThrow();
577 } catch (NullPointerException success) {}
578 }
579
580 /**
581 * remove(null) throws NPE
582 */
583 public void testRemove1_NullPointerException() {
584 try {
585 ConcurrentNavigableMap c = map5();
586 c.remove(null);
587 shouldThrow();
588 } catch (NullPointerException success) {}
589 }
590
591 /**
592 * remove(null, x) throws NPE
593 */
594 public void testRemove2_NullPointerException() {
595 try {
596 ConcurrentNavigableMap c = map5();
597 c.remove(null, "whatever");
598 shouldThrow();
599 } catch (NullPointerException success) {}
600 }
601
602 /**
603 * A deserialized map equals original
604 */
605 public void testSerialization() throws Exception {
606 ConcurrentNavigableMap q = map5();
607
608 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
609 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
610 out.writeObject(q);
611 out.close();
612
613 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
614 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
615 ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
616 assertEquals(q.size(), r.size());
617 assertTrue(q.equals(r));
618 assertTrue(r.equals(q));
619 }
620
621
622
623 /**
624 * subMap returns map with keys in requested range
625 */
626 public void testSubMapContents() {
627 ConcurrentNavigableMap map = map5();
628 SortedMap sm = map.subMap(two, four);
629 assertEquals(two, sm.firstKey());
630 assertEquals(three, sm.lastKey());
631 assertEquals(2, sm.size());
632 assertFalse(sm.containsKey(one));
633 assertTrue(sm.containsKey(two));
634 assertTrue(sm.containsKey(three));
635 assertFalse(sm.containsKey(four));
636 assertFalse(sm.containsKey(five));
637 Iterator i = sm.keySet().iterator();
638 Object k;
639 k = (Integer)(i.next());
640 assertEquals(two, k);
641 k = (Integer)(i.next());
642 assertEquals(three, k);
643 assertFalse(i.hasNext());
644 Iterator j = sm.keySet().iterator();
645 j.next();
646 j.remove();
647 assertFalse(map.containsKey(two));
648 assertEquals(4, map.size());
649 assertEquals(1, sm.size());
650 assertEquals(three, sm.firstKey());
651 assertEquals(three, sm.lastKey());
652 assertEquals("C", sm.remove(three));
653 assertTrue(sm.isEmpty());
654 assertEquals(3, map.size());
655 }
656
657 public void testSubMapContents2() {
658 ConcurrentNavigableMap map = map5();
659 SortedMap sm = map.subMap(two, three);
660 assertEquals(1, sm.size());
661 assertEquals(two, sm.firstKey());
662 assertEquals(two, sm.lastKey());
663 assertFalse(sm.containsKey(one));
664 assertTrue(sm.containsKey(two));
665 assertFalse(sm.containsKey(three));
666 assertFalse(sm.containsKey(four));
667 assertFalse(sm.containsKey(five));
668 Iterator i = sm.keySet().iterator();
669 Object k;
670 k = (Integer)(i.next());
671 assertEquals(two, k);
672 assertFalse(i.hasNext());
673 Iterator j = sm.keySet().iterator();
674 j.next();
675 j.remove();
676 assertFalse(map.containsKey(two));
677 assertEquals(4, map.size());
678 assertEquals(0, sm.size());
679 assertTrue(sm.isEmpty());
680 assertSame(sm.remove(three), null);
681 assertEquals(4, map.size());
682 }
683
684 /**
685 * headMap returns map with keys in requested range
686 */
687 public void testHeadMapContents() {
688 ConcurrentNavigableMap map = map5();
689 SortedMap sm = map.headMap(four);
690 assertTrue(sm.containsKey(one));
691 assertTrue(sm.containsKey(two));
692 assertTrue(sm.containsKey(three));
693 assertFalse(sm.containsKey(four));
694 assertFalse(sm.containsKey(five));
695 Iterator i = sm.keySet().iterator();
696 Object k;
697 k = (Integer)(i.next());
698 assertEquals(one, k);
699 k = (Integer)(i.next());
700 assertEquals(two, k);
701 k = (Integer)(i.next());
702 assertEquals(three, k);
703 assertFalse(i.hasNext());
704 sm.clear();
705 assertTrue(sm.isEmpty());
706 assertEquals(2, map.size());
707 assertEquals(four, map.firstKey());
708 }
709
710 /**
711 * headMap returns map with keys in requested range
712 */
713 public void testTailMapContents() {
714 ConcurrentNavigableMap map = map5();
715 SortedMap sm = map.tailMap(two);
716 assertFalse(sm.containsKey(one));
717 assertTrue(sm.containsKey(two));
718 assertTrue(sm.containsKey(three));
719 assertTrue(sm.containsKey(four));
720 assertTrue(sm.containsKey(five));
721 Iterator i = sm.keySet().iterator();
722 Object k;
723 k = (Integer)(i.next());
724 assertEquals(two, k);
725 k = (Integer)(i.next());
726 assertEquals(three, k);
727 k = (Integer)(i.next());
728 assertEquals(four, k);
729 k = (Integer)(i.next());
730 assertEquals(five, k);
731 assertFalse(i.hasNext());
732
733 Iterator ei = sm.entrySet().iterator();
734 Map.Entry e;
735 e = (Map.Entry)(ei.next());
736 assertEquals(two, e.getKey());
737 assertEquals("B", e.getValue());
738 e = (Map.Entry)(ei.next());
739 assertEquals(three, e.getKey());
740 assertEquals("C", e.getValue());
741 e = (Map.Entry)(ei.next());
742 assertEquals(four, e.getKey());
743 assertEquals("D", e.getValue());
744 e = (Map.Entry)(ei.next());
745 assertEquals(five, e.getKey());
746 assertEquals("E", e.getValue());
747 assertFalse(i.hasNext());
748
749 SortedMap ssm = sm.tailMap(four);
750 assertEquals(four, ssm.firstKey());
751 assertEquals(five, ssm.lastKey());
752 assertEquals("D", ssm.remove(four));
753 assertEquals(1, ssm.size());
754 assertEquals(3, sm.size());
755 assertEquals(4, map.size());
756 }
757
758 /**
759 * clear removes all pairs
760 */
761 public void testDescendingClear() {
762 ConcurrentNavigableMap map = dmap5();
763 map.clear();
764 assertEquals(map.size(), 0);
765 }
766
767
768 /**
769 * Maps with same contents are equal
770 */
771 public void testDescendingEquals() {
772 ConcurrentNavigableMap map1 = dmap5();
773 ConcurrentNavigableMap map2 = dmap5();
774 assertEquals(map1, map2);
775 assertEquals(map2, map1);
776 map1.clear();
777 assertFalse(map1.equals(map2));
778 assertFalse(map2.equals(map1));
779 }
780
781 /**
782 * containsKey returns true for contained key
783 */
784 public void testDescendingContainsKey() {
785 ConcurrentNavigableMap map = dmap5();
786 assertTrue(map.containsKey(m1));
787 assertFalse(map.containsKey(zero));
788 }
789
790 /**
791 * containsValue returns true for held values
792 */
793 public void testDescendingContainsValue() {
794 ConcurrentNavigableMap map = dmap5();
795 assertTrue(map.containsValue("A"));
796 assertFalse(map.containsValue("Z"));
797 }
798
799 /**
800 * get returns the correct element at the given key,
801 * or null if not present
802 */
803 public void testDescendingGet() {
804 ConcurrentNavigableMap map = dmap5();
805 assertEquals("A", (String)map.get(m1));
806 ConcurrentNavigableMap empty = dmap0();
807 assertNull(empty.get(m1));
808 }
809
810 /**
811 * isEmpty is true of empty map and false for non-empty
812 */
813 public void testDescendingIsEmpty() {
814 ConcurrentNavigableMap empty = dmap0();
815 ConcurrentNavigableMap map = dmap5();
816 assertTrue(empty.isEmpty());
817 assertFalse(map.isEmpty());
818 }
819
820 /**
821 * firstKey returns first key
822 */
823 public void testDescendingFirstKey() {
824 ConcurrentNavigableMap map = dmap5();
825 assertEquals(m1, map.firstKey());
826 }
827
828 /**
829 * lastKey returns last key
830 */
831 public void testDescendingLastKey() {
832 ConcurrentNavigableMap map = dmap5();
833 assertEquals(m5, map.lastKey());
834 }
835
836
837 /**
838 * keySet returns a Set containing all the keys
839 */
840 public void testDescendingKeySet() {
841 ConcurrentNavigableMap map = dmap5();
842 Set s = map.keySet();
843 assertEquals(5, s.size());
844 assertTrue(s.contains(m1));
845 assertTrue(s.contains(m2));
846 assertTrue(s.contains(m3));
847 assertTrue(s.contains(m4));
848 assertTrue(s.contains(m5));
849 }
850
851 /**
852 * keySet is ordered
853 */
854 public void testDescendingKeySetOrder() {
855 ConcurrentNavigableMap map = dmap5();
856 Set s = map.keySet();
857 Iterator i = s.iterator();
858 Integer last = (Integer)i.next();
859 assertEquals(last, m1);
860 while (i.hasNext()) {
861 Integer k = (Integer)i.next();
862 assertTrue(last.compareTo(k) > 0);
863 last = k;
864 }
865 }
866
867 /**
868 * values collection contains all values
869 */
870 public void testDescendingValues() {
871 ConcurrentNavigableMap map = dmap5();
872 Collection s = map.values();
873 assertEquals(5, s.size());
874 assertTrue(s.contains("A"));
875 assertTrue(s.contains("B"));
876 assertTrue(s.contains("C"));
877 assertTrue(s.contains("D"));
878 assertTrue(s.contains("E"));
879 }
880
881 /**
882 * keySet.toArray returns contains all keys
883 */
884 public void testDescendingAscendingKeySetToArray() {
885 ConcurrentNavigableMap map = dmap5();
886 Set s = map.keySet();
887 Object[] ar = s.toArray();
888 assertTrue(s.containsAll(Arrays.asList(ar)));
889 assertEquals(5, ar.length);
890 ar[0] = m10;
891 assertFalse(s.containsAll(Arrays.asList(ar)));
892 }
893
894 /**
895 * descendingkeySet.toArray returns contains all keys
896 */
897 public void testDescendingDescendingKeySetToArray() {
898 ConcurrentNavigableMap map = dmap5();
899 Set s = map.descendingKeySet();
900 Object[] ar = s.toArray();
901 assertEquals(5, ar.length);
902 assertTrue(s.containsAll(Arrays.asList(ar)));
903 ar[0] = m10;
904 assertFalse(s.containsAll(Arrays.asList(ar)));
905 }
906
907 /**
908 * Values.toArray contains all values
909 */
910 public void testDescendingValuesToArray() {
911 ConcurrentNavigableMap map = dmap5();
912 Collection v = map.values();
913 Object[] ar = v.toArray();
914 ArrayList s = new ArrayList(Arrays.asList(ar));
915 assertEquals(5, ar.length);
916 assertTrue(s.contains("A"));
917 assertTrue(s.contains("B"));
918 assertTrue(s.contains("C"));
919 assertTrue(s.contains("D"));
920 assertTrue(s.contains("E"));
921 }
922
923
924 /**
925 * entrySet contains all pairs
926 */
927 public void testDescendingEntrySet() {
928 ConcurrentNavigableMap map = dmap5();
929 Set s = map.entrySet();
930 assertEquals(5, s.size());
931 Iterator it = s.iterator();
932 while (it.hasNext()) {
933 Map.Entry e = (Map.Entry) it.next();
934 assertTrue(
935 (e.getKey().equals(m1) && e.getValue().equals("A")) ||
936 (e.getKey().equals(m2) && e.getValue().equals("B")) ||
937 (e.getKey().equals(m3) && e.getValue().equals("C")) ||
938 (e.getKey().equals(m4) && e.getValue().equals("D")) ||
939 (e.getKey().equals(m5) && e.getValue().equals("E")));
940 }
941 }
942
943 /**
944 * putAll adds all key-value pairs from the given map
945 */
946 public void testDescendingPutAll() {
947 ConcurrentNavigableMap empty = dmap0();
948 ConcurrentNavigableMap map = dmap5();
949 empty.putAll(map);
950 assertEquals(5, empty.size());
951 assertTrue(empty.containsKey(m1));
952 assertTrue(empty.containsKey(m2));
953 assertTrue(empty.containsKey(m3));
954 assertTrue(empty.containsKey(m4));
955 assertTrue(empty.containsKey(m5));
956 }
957
958 /**
959 * putIfAbsent works when the given key is not present
960 */
961 public void testDescendingPutIfAbsent() {
962 ConcurrentNavigableMap map = dmap5();
963 map.putIfAbsent(six, "Z");
964 assertTrue(map.containsKey(six));
965 }
966
967 /**
968 * putIfAbsent does not add the pair if the key is already present
969 */
970 public void testDescendingPutIfAbsent2() {
971 ConcurrentNavigableMap map = dmap5();
972 assertEquals("A", map.putIfAbsent(m1, "Z"));
973 }
974
975 /**
976 * replace fails when the given key is not present
977 */
978 public void testDescendingReplace() {
979 ConcurrentNavigableMap map = dmap5();
980 assertNull(map.replace(six, "Z"));
981 assertFalse(map.containsKey(six));
982 }
983
984 /**
985 * replace succeeds if the key is already present
986 */
987 public void testDescendingReplace2() {
988 ConcurrentNavigableMap map = dmap5();
989 assertNotNull(map.replace(m1, "Z"));
990 assertEquals("Z", map.get(m1));
991 }
992
993
994 /**
995 * replace value fails when the given key not mapped to expected value
996 */
997 public void testDescendingReplaceValue() {
998 ConcurrentNavigableMap map = dmap5();
999 assertEquals("A", map.get(m1));
1000 assertFalse(map.replace(m1, "Z", "Z"));
1001 assertEquals("A", map.get(m1));
1002 }
1003
1004 /**
1005 * replace value succeeds when the given key mapped to expected value
1006 */
1007 public void testDescendingReplaceValue2() {
1008 ConcurrentNavigableMap map = dmap5();
1009 assertEquals("A", map.get(m1));
1010 assertTrue(map.replace(m1, "A", "Z"));
1011 assertEquals("Z", map.get(m1));
1012 }
1013
1014
1015 /**
1016 * remove removes the correct key-value pair from the map
1017 */
1018 public void testDescendingRemove() {
1019 ConcurrentNavigableMap map = dmap5();
1020 map.remove(m5);
1021 assertEquals(4, map.size());
1022 assertFalse(map.containsKey(m5));
1023 }
1024
1025 /**
1026 * remove(key,value) removes only if pair present
1027 */
1028 public void testDescendingRemove2() {
1029 ConcurrentNavigableMap map = dmap5();
1030 assertTrue(map.containsKey(m5));
1031 assertEquals("E", map.get(m5));
1032 map.remove(m5, "E");
1033 assertEquals(4, map.size());
1034 assertFalse(map.containsKey(m5));
1035 map.remove(m4, "A");
1036 assertEquals(4, map.size());
1037 assertTrue(map.containsKey(m4));
1038
1039 }
1040
1041 /**
1042 * lowerEntry returns preceding entry.
1043 */
1044 public void testDescendingLowerEntry() {
1045 ConcurrentNavigableMap map = dmap5();
1046 Map.Entry e1 = map.lowerEntry(m3);
1047 assertEquals(m2, e1.getKey());
1048
1049 Map.Entry e2 = map.lowerEntry(m6);
1050 assertEquals(m5, e2.getKey());
1051
1052 Map.Entry e3 = map.lowerEntry(m1);
1053 assertNull(e3);
1054
1055 Map.Entry e4 = map.lowerEntry(zero);
1056 assertNull(e4);
1057
1058 }
1059
1060 /**
1061 * higherEntry returns next entry.
1062 */
1063 public void testDescendingHigherEntry() {
1064 ConcurrentNavigableMap map = dmap5();
1065 Map.Entry e1 = map.higherEntry(m3);
1066 assertEquals(m4, e1.getKey());
1067
1068 Map.Entry e2 = map.higherEntry(zero);
1069 assertEquals(m1, e2.getKey());
1070
1071 Map.Entry e3 = map.higherEntry(m5);
1072 assertNull(e3);
1073
1074 Map.Entry e4 = map.higherEntry(m6);
1075 assertNull(e4);
1076
1077 }
1078
1079 /**
1080 * floorEntry returns preceding entry.
1081 */
1082 public void testDescendingFloorEntry() {
1083 ConcurrentNavigableMap map = dmap5();
1084 Map.Entry e1 = map.floorEntry(m3);
1085 assertEquals(m3, e1.getKey());
1086
1087 Map.Entry e2 = map.floorEntry(m6);
1088 assertEquals(m5, e2.getKey());
1089
1090 Map.Entry e3 = map.floorEntry(m1);
1091 assertEquals(m1, e3.getKey());
1092
1093 Map.Entry e4 = map.floorEntry(zero);
1094 assertNull(e4);
1095
1096 }
1097
1098 /**
1099 * ceilingEntry returns next entry.
1100 */
1101 public void testDescendingCeilingEntry() {
1102 ConcurrentNavigableMap map = dmap5();
1103 Map.Entry e1 = map.ceilingEntry(m3);
1104 assertEquals(m3, e1.getKey());
1105
1106 Map.Entry e2 = map.ceilingEntry(zero);
1107 assertEquals(m1, e2.getKey());
1108
1109 Map.Entry e3 = map.ceilingEntry(m5);
1110 assertEquals(m5, e3.getKey());
1111
1112 Map.Entry e4 = map.ceilingEntry(m6);
1113 assertNull(e4);
1114
1115 }
1116
1117 /**
1118 * pollFirstEntry returns entries in order
1119 */
1120 public void testDescendingPollFirstEntry() {
1121 ConcurrentNavigableMap map = dmap5();
1122 Map.Entry e = map.pollFirstEntry();
1123 assertEquals(m1, e.getKey());
1124 assertEquals("A", e.getValue());
1125 e = map.pollFirstEntry();
1126 assertEquals(m2, e.getKey());
1127 map.put(m1, "A");
1128 e = map.pollFirstEntry();
1129 assertEquals(m1, e.getKey());
1130 assertEquals("A", e.getValue());
1131 e = map.pollFirstEntry();
1132 assertEquals(m3, e.getKey());
1133 map.remove(m4);
1134 e = map.pollFirstEntry();
1135 assertEquals(m5, e.getKey());
1136 try {
1137 e.setValue("A");
1138 shouldThrow();
1139 } catch (UnsupportedOperationException success) {}
1140 e = map.pollFirstEntry();
1141 assertNull(e);
1142 }
1143
1144 /**
1145 * pollLastEntry returns entries in order
1146 */
1147 public void testDescendingPollLastEntry() {
1148 ConcurrentNavigableMap map = dmap5();
1149 Map.Entry e = map.pollLastEntry();
1150 assertEquals(m5, e.getKey());
1151 assertEquals("E", e.getValue());
1152 e = map.pollLastEntry();
1153 assertEquals(m4, e.getKey());
1154 map.put(m5, "E");
1155 e = map.pollLastEntry();
1156 assertEquals(m5, e.getKey());
1157 assertEquals("E", e.getValue());
1158 e = map.pollLastEntry();
1159 assertEquals(m3, e.getKey());
1160 map.remove(m2);
1161 e = map.pollLastEntry();
1162 assertEquals(m1, e.getKey());
1163 try {
1164 e.setValue("E");
1165 shouldThrow();
1166 } catch (UnsupportedOperationException success) {}
1167 e = map.pollLastEntry();
1168 assertNull(e);
1169 }
1170
1171 /**
1172 * size returns the correct values
1173 */
1174 public void testDescendingSize() {
1175 ConcurrentNavigableMap map = dmap5();
1176 ConcurrentNavigableMap empty = dmap0();
1177 assertEquals(0, empty.size());
1178 assertEquals(5, map.size());
1179 }
1180
1181 /**
1182 * toString contains toString of elements
1183 */
1184 public void testDescendingToString() {
1185 ConcurrentNavigableMap map = dmap5();
1186 String s = map.toString();
1187 for (int i = 1; i <= 5; ++i) {
1188 assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1189 }
1190 }
1191
1192 // Exception testDescendings
1193
1194 /**
1195 * get(null) of nm1mpty map throws NPE
1196 */
1197 public void testDescendingGet_NullPointerException() {
1198 try {
1199 ConcurrentNavigableMap c = dmap5();
1200 c.get(null);
1201 shouldThrow();
1202 } catch (NullPointerException success) {}
1203 }
1204
1205 /**
1206 * containsKey(null) of nm1mpty map throws NPE
1207 */
1208 public void testDescendingContainsKey_NullPointerException() {
1209 try {
1210 ConcurrentNavigableMap c = dmap5();
1211 c.containsKey(null);
1212 shouldThrow();
1213 } catch (NullPointerException success) {}
1214 }
1215
1216 /**
1217 * containsValue(null) throws NPE
1218 */
1219 public void testDescendingContainsValue_NullPointerException() {
1220 try {
1221 ConcurrentNavigableMap c = dmap0();
1222 c.containsValue(null);
1223 shouldThrow();
1224 } catch (NullPointerException success) {}
1225 }
1226
1227
1228 /**
1229 * put(null,x) throws NPE
1230 */
1231 public void testDescendingPut1_NullPointerException() {
1232 try {
1233 ConcurrentNavigableMap c = dmap5();
1234 c.put(null, "whatever");
1235 shouldThrow();
1236 } catch (NullPointerException success) {}
1237 }
1238
1239 /**
1240 * putIfAbsent(null, x) throws NPE
1241 */
1242 public void testDescendingPutIfAbsent1_NullPointerException() {
1243 try {
1244 ConcurrentNavigableMap c = dmap5();
1245 c.putIfAbsent(null, "whatever");
1246 shouldThrow();
1247 } catch (NullPointerException success) {}
1248 }
1249
1250 /**
1251 * replace(null, x) throws NPE
1252 */
1253 public void testDescendingReplace_NullPointerException() {
1254 try {
1255 ConcurrentNavigableMap c = dmap5();
1256 c.replace(null, "whatever");
1257 shouldThrow();
1258 } catch (NullPointerException success) {}
1259 }
1260
1261 /**
1262 * replace(null, x, y) throws NPE
1263 */
1264 public void testDescendingReplaceValue_NullPointerException() {
1265 try {
1266 ConcurrentNavigableMap c = dmap5();
1267 c.replace(null, m1, "whatever");
1268 shouldThrow();
1269 } catch (NullPointerException success) {}
1270 }
1271
1272 /**
1273 * remove(null) throws NPE
1274 */
1275 public void testDescendingRemove1_NullPointerException() {
1276 try {
1277 ConcurrentNavigableMap c = dmap5();
1278 c.remove(null);
1279 shouldThrow();
1280 } catch (NullPointerException success) {}
1281 }
1282
1283 /**
1284 * remove(null, x) throws NPE
1285 */
1286 public void testDescendingRemove2_NullPointerException() {
1287 try {
1288 ConcurrentNavigableMap c = dmap5();
1289 c.remove(null, "whatever");
1290 shouldThrow();
1291 } catch (NullPointerException success) {}
1292 }
1293
1294 /**
1295 * A deserialized map equals original
1296 */
1297 public void testDescendingSerialization() throws Exception {
1298 ConcurrentNavigableMap q = dmap5();
1299
1300 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1301 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1302 out.writeObject(q);
1303 out.close();
1304
1305 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1306 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1307 ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
1308 assertEquals(q.size(), r.size());
1309 assertTrue(q.equals(r));
1310 assertTrue(r.equals(q));
1311 }
1312
1313
1314 /**
1315 * subMap returns map with keys in requested range
1316 */
1317 public void testDescendingSubMapContents() {
1318 ConcurrentNavigableMap map = dmap5();
1319 SortedMap sm = map.subMap(m2, m4);
1320 assertEquals(m2, sm.firstKey());
1321 assertEquals(m3, sm.lastKey());
1322 assertEquals(2, sm.size());
1323 assertFalse(sm.containsKey(m1));
1324 assertTrue(sm.containsKey(m2));
1325 assertTrue(sm.containsKey(m3));
1326 assertFalse(sm.containsKey(m4));
1327 assertFalse(sm.containsKey(m5));
1328 Iterator i = sm.keySet().iterator();
1329 Object k;
1330 k = (Integer)(i.next());
1331 assertEquals(m2, k);
1332 k = (Integer)(i.next());
1333 assertEquals(m3, k);
1334 assertFalse(i.hasNext());
1335 Iterator j = sm.keySet().iterator();
1336 j.next();
1337 j.remove();
1338 assertFalse(map.containsKey(m2));
1339 assertEquals(4, map.size());
1340 assertEquals(1, sm.size());
1341 assertEquals(m3, sm.firstKey());
1342 assertEquals(m3, sm.lastKey());
1343 assertEquals("C", sm.remove(m3));
1344 assertTrue(sm.isEmpty());
1345 assertEquals(3, map.size());
1346 }
1347
1348 public void testDescendingSubMapContents2() {
1349 ConcurrentNavigableMap map = dmap5();
1350 SortedMap sm = map.subMap(m2, m3);
1351 assertEquals(1, sm.size());
1352 assertEquals(m2, sm.firstKey());
1353 assertEquals(m2, sm.lastKey());
1354 assertFalse(sm.containsKey(m1));
1355 assertTrue(sm.containsKey(m2));
1356 assertFalse(sm.containsKey(m3));
1357 assertFalse(sm.containsKey(m4));
1358 assertFalse(sm.containsKey(m5));
1359 Iterator i = sm.keySet().iterator();
1360 Object k;
1361 k = (Integer)(i.next());
1362 assertEquals(m2, k);
1363 assertFalse(i.hasNext());
1364 Iterator j = sm.keySet().iterator();
1365 j.next();
1366 j.remove();
1367 assertFalse(map.containsKey(m2));
1368 assertEquals(4, map.size());
1369 assertEquals(0, sm.size());
1370 assertTrue(sm.isEmpty());
1371 assertSame(sm.remove(m3), null);
1372 assertEquals(4, map.size());
1373 }
1374
1375 /**
1376 * headMap returns map with keys in requested range
1377 */
1378 public void testDescendingHeadMapContents() {
1379 ConcurrentNavigableMap map = dmap5();
1380 SortedMap sm = map.headMap(m4);
1381 assertTrue(sm.containsKey(m1));
1382 assertTrue(sm.containsKey(m2));
1383 assertTrue(sm.containsKey(m3));
1384 assertFalse(sm.containsKey(m4));
1385 assertFalse(sm.containsKey(m5));
1386 Iterator i = sm.keySet().iterator();
1387 Object k;
1388 k = (Integer)(i.next());
1389 assertEquals(m1, k);
1390 k = (Integer)(i.next());
1391 assertEquals(m2, k);
1392 k = (Integer)(i.next());
1393 assertEquals(m3, k);
1394 assertFalse(i.hasNext());
1395 sm.clear();
1396 assertTrue(sm.isEmpty());
1397 assertEquals(2, map.size());
1398 assertEquals(m4, map.firstKey());
1399 }
1400
1401 /**
1402 * headMap returns map with keys in requested range
1403 */
1404 public void testDescendingTailMapContents() {
1405 ConcurrentNavigableMap map = dmap5();
1406 SortedMap sm = map.tailMap(m2);
1407 assertFalse(sm.containsKey(m1));
1408 assertTrue(sm.containsKey(m2));
1409 assertTrue(sm.containsKey(m3));
1410 assertTrue(sm.containsKey(m4));
1411 assertTrue(sm.containsKey(m5));
1412 Iterator i = sm.keySet().iterator();
1413 Object k;
1414 k = (Integer)(i.next());
1415 assertEquals(m2, k);
1416 k = (Integer)(i.next());
1417 assertEquals(m3, k);
1418 k = (Integer)(i.next());
1419 assertEquals(m4, k);
1420 k = (Integer)(i.next());
1421 assertEquals(m5, k);
1422 assertFalse(i.hasNext());
1423
1424 Iterator ei = sm.entrySet().iterator();
1425 Map.Entry e;
1426 e = (Map.Entry)(ei.next());
1427 assertEquals(m2, e.getKey());
1428 assertEquals("B", e.getValue());
1429 e = (Map.Entry)(ei.next());
1430 assertEquals(m3, e.getKey());
1431 assertEquals("C", e.getValue());
1432 e = (Map.Entry)(ei.next());
1433 assertEquals(m4, e.getKey());
1434 assertEquals("D", e.getValue());
1435 e = (Map.Entry)(ei.next());
1436 assertEquals(m5, e.getKey());
1437 assertEquals("E", e.getValue());
1438 assertFalse(i.hasNext());
1439
1440 SortedMap ssm = sm.tailMap(m4);
1441 assertEquals(m4, ssm.firstKey());
1442 assertEquals(m5, ssm.lastKey());
1443 assertEquals("D", ssm.remove(m4));
1444 assertEquals(1, ssm.size());
1445 assertEquals(3, sm.size());
1446 assertEquals(4, map.size());
1447 }
1448
1449 }