ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListMapTest.java
Revision: 1.28
Committed: Sat Nov 26 05:42:14 2011 UTC (12 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.27: +1 -1 lines
Log Message:
assertEquals argument order

File Contents

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