ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListMapTest.java
Revision: 1.14
Committed: Sat Nov 21 17:38:05 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.13: +28 -44 lines
Log Message:
improve exception handling

File Contents

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