ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListMapTest.java
Revision: 1.17
Committed: Wed Dec 23 00:47:16 2009 UTC (14 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.16: +1 -1 lines
Log Message:
typos

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     * lowerEntry returns preceding entry.
434     */
435     public void testLowerEntry() {
436     ConcurrentSkipListMap map = map5();
437     Map.Entry e1 = map.lowerEntry(three);
438     assertEquals(two, e1.getKey());
439    
440     Map.Entry e2 = map.lowerEntry(six);
441     assertEquals(five, e2.getKey());
442    
443     Map.Entry e3 = map.lowerEntry(one);
444     assertNull(e3);
445    
446     Map.Entry e4 = map.lowerEntry(zero);
447     assertNull(e4);
448     }
449    
450     /**
451     * higherEntry returns next entry.
452     */
453     public void testHigherEntry() {
454     ConcurrentSkipListMap map = map5();
455     Map.Entry e1 = map.higherEntry(three);
456     assertEquals(four, e1.getKey());
457    
458     Map.Entry e2 = map.higherEntry(zero);
459     assertEquals(one, e2.getKey());
460    
461     Map.Entry e3 = map.higherEntry(five);
462     assertNull(e3);
463    
464     Map.Entry e4 = map.higherEntry(six);
465     assertNull(e4);
466     }
467    
468     /**
469     * floorEntry returns preceding entry.
470     */
471     public void testFloorEntry() {
472     ConcurrentSkipListMap map = map5();
473     Map.Entry e1 = map.floorEntry(three);
474     assertEquals(three, e1.getKey());
475    
476     Map.Entry e2 = map.floorEntry(six);
477     assertEquals(five, e2.getKey());
478    
479     Map.Entry e3 = map.floorEntry(one);
480     assertEquals(one, e3.getKey());
481    
482     Map.Entry e4 = map.floorEntry(zero);
483     assertNull(e4);
484     }
485    
486     /**
487     * ceilingEntry returns next entry.
488     */
489     public void testCeilingEntry() {
490     ConcurrentSkipListMap map = map5();
491     Map.Entry e1 = map.ceilingEntry(three);
492     assertEquals(three, e1.getKey());
493    
494     Map.Entry e2 = map.ceilingEntry(zero);
495     assertEquals(one, e2.getKey());
496    
497     Map.Entry e3 = map.ceilingEntry(five);
498     assertEquals(five, e3.getKey());
499    
500     Map.Entry e4 = map.ceilingEntry(six);
501     assertNull(e4);
502     }
503    
504 dl 1.5 /**
505     * lowerEntry, higherEntry, ceilingEntry, and floorEntry return
506 jsr166 1.17 * immutable entries
507 dl 1.5 */
508     public void testEntryImmutablity() {
509     ConcurrentSkipListMap map = map5();
510     Map.Entry e = map.lowerEntry(three);
511     assertEquals(two, e.getKey());
512     try {
513     e.setValue("X");
514 jsr166 1.14 shouldThrow();
515 jsr166 1.10 } catch (UnsupportedOperationException success) {}
516 dl 1.5 e = map.higherEntry(zero);
517     assertEquals(one, e.getKey());
518     try {
519     e.setValue("X");
520 jsr166 1.14 shouldThrow();
521 jsr166 1.10 } catch (UnsupportedOperationException success) {}
522 dl 1.5 e = map.floorEntry(one);
523     assertEquals(one, e.getKey());
524     try {
525     e.setValue("X");
526 jsr166 1.14 shouldThrow();
527 jsr166 1.10 } catch (UnsupportedOperationException success) {}
528 dl 1.5 e = map.ceilingEntry(five);
529     assertEquals(five, e.getKey());
530     try {
531     e.setValue("X");
532 jsr166 1.14 shouldThrow();
533 jsr166 1.10 } catch (UnsupportedOperationException success) {}
534 dl 1.5 }
535    
536    
537 dl 1.1
538     /**
539     * lowerKey returns preceding element
540     */
541     public void testLowerKey() {
542     ConcurrentSkipListMap q = map5();
543     Object e1 = q.lowerKey(three);
544     assertEquals(two, e1);
545    
546     Object e2 = q.lowerKey(six);
547     assertEquals(five, e2);
548    
549     Object e3 = q.lowerKey(one);
550     assertNull(e3);
551    
552     Object e4 = q.lowerKey(zero);
553     assertNull(e4);
554     }
555    
556     /**
557     * higherKey returns next element
558     */
559     public void testHigherKey() {
560     ConcurrentSkipListMap q = map5();
561     Object e1 = q.higherKey(three);
562     assertEquals(four, e1);
563    
564     Object e2 = q.higherKey(zero);
565     assertEquals(one, e2);
566    
567     Object e3 = q.higherKey(five);
568     assertNull(e3);
569    
570     Object e4 = q.higherKey(six);
571     assertNull(e4);
572     }
573    
574     /**
575     * floorKey returns preceding element
576     */
577     public void testFloorKey() {
578     ConcurrentSkipListMap q = map5();
579     Object e1 = q.floorKey(three);
580     assertEquals(three, e1);
581    
582     Object e2 = q.floorKey(six);
583     assertEquals(five, e2);
584    
585     Object e3 = q.floorKey(one);
586     assertEquals(one, e3);
587    
588     Object e4 = q.floorKey(zero);
589     assertNull(e4);
590     }
591    
592     /**
593     * ceilingKey returns next element
594     */
595     public void testCeilingKey() {
596     ConcurrentSkipListMap q = map5();
597     Object e1 = q.ceilingKey(three);
598     assertEquals(three, e1);
599    
600     Object e2 = q.ceilingKey(zero);
601     assertEquals(one, e2);
602    
603     Object e3 = q.ceilingKey(five);
604     assertEquals(five, e3);
605    
606     Object e4 = q.ceilingKey(six);
607     assertNull(e4);
608     }
609    
610     /**
611     * pollFirstEntry returns entries in order
612     */
613     public void testPollFirstEntry() {
614     ConcurrentSkipListMap map = map5();
615     Map.Entry e = map.pollFirstEntry();
616     assertEquals(one, e.getKey());
617     assertEquals("A", e.getValue());
618     e = map.pollFirstEntry();
619     assertEquals(two, e.getKey());
620     map.put(one, "A");
621     e = map.pollFirstEntry();
622     assertEquals(one, e.getKey());
623     assertEquals("A", e.getValue());
624     e = map.pollFirstEntry();
625     assertEquals(three, e.getKey());
626     map.remove(four);
627     e = map.pollFirstEntry();
628     assertEquals(five, e.getKey());
629     try {
630     e.setValue("A");
631     shouldThrow();
632 jsr166 1.14 } catch (UnsupportedOperationException success) {}
633 dl 1.1 e = map.pollFirstEntry();
634     assertNull(e);
635     }
636    
637     /**
638     * pollLastEntry returns entries in order
639     */
640     public void testPollLastEntry() {
641     ConcurrentSkipListMap map = map5();
642     Map.Entry e = map.pollLastEntry();
643     assertEquals(five, e.getKey());
644     assertEquals("E", e.getValue());
645     e = map.pollLastEntry();
646     assertEquals(four, e.getKey());
647     map.put(five, "E");
648     e = map.pollLastEntry();
649     assertEquals(five, e.getKey());
650     assertEquals("E", e.getValue());
651     e = map.pollLastEntry();
652     assertEquals(three, e.getKey());
653     map.remove(two);
654     e = map.pollLastEntry();
655     assertEquals(one, e.getKey());
656     try {
657     e.setValue("E");
658     shouldThrow();
659 jsr166 1.14 } catch (UnsupportedOperationException success) {}
660 dl 1.1 e = map.pollLastEntry();
661     assertNull(e);
662     }
663    
664     /**
665     * size returns the correct values
666     */
667     public void testSize() {
668     ConcurrentSkipListMap map = map5();
669     ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
670 jsr166 1.12 assertEquals(0, empty.size());
671     assertEquals(5, map.size());
672 dl 1.1 }
673    
674     /**
675     * toString contains toString of elements
676     */
677     public void testToString() {
678     ConcurrentSkipListMap map = map5();
679     String s = map.toString();
680     for (int i = 1; i <= 5; ++i) {
681     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
682     }
683 jsr166 1.9 }
684 dl 1.1
685     // Exception tests
686    
687     /**
688     * get(null) of nonempty map throws NPE
689     */
690     public void testGet_NullPointerException() {
691     try {
692     ConcurrentSkipListMap c = map5();
693     c.get(null);
694     shouldThrow();
695 jsr166 1.14 } catch (NullPointerException success) {}
696 dl 1.1 }
697    
698     /**
699     * containsKey(null) of nonempty map throws NPE
700     */
701     public void testContainsKey_NullPointerException() {
702     try {
703     ConcurrentSkipListMap c = map5();
704     c.containsKey(null);
705     shouldThrow();
706 jsr166 1.14 } catch (NullPointerException success) {}
707 dl 1.1 }
708    
709     /**
710     * containsValue(null) throws NPE
711     */
712     public void testContainsValue_NullPointerException() {
713     try {
714     ConcurrentSkipListMap c = new ConcurrentSkipListMap();
715     c.containsValue(null);
716     shouldThrow();
717 jsr166 1.14 } catch (NullPointerException success) {}
718 dl 1.1 }
719    
720    
721     /**
722     * put(null,x) throws NPE
723     */
724     public void testPut1_NullPointerException() {
725     try {
726     ConcurrentSkipListMap c = map5();
727     c.put(null, "whatever");
728     shouldThrow();
729 jsr166 1.14 } catch (NullPointerException success) {}
730 dl 1.1 }
731    
732     /**
733     * putIfAbsent(null, x) throws NPE
734     */
735     public void testPutIfAbsent1_NullPointerException() {
736     try {
737     ConcurrentSkipListMap c = map5();
738     c.putIfAbsent(null, "whatever");
739     shouldThrow();
740 jsr166 1.14 } catch (NullPointerException success) {}
741 dl 1.1 }
742    
743     /**
744     * replace(null, x) throws NPE
745     */
746     public void testReplace_NullPointerException() {
747     try {
748     ConcurrentSkipListMap c = map5();
749     c.replace(null, "whatever");
750     shouldThrow();
751 jsr166 1.14 } catch (NullPointerException success) {}
752 dl 1.1 }
753    
754     /**
755     * replace(null, x, y) throws NPE
756     */
757     public void testReplaceValue_NullPointerException() {
758     try {
759     ConcurrentSkipListMap c = map5();
760     c.replace(null, one, "whatever");
761     shouldThrow();
762 jsr166 1.14 } catch (NullPointerException success) {}
763 dl 1.1 }
764    
765     /**
766     * remove(null) throws NPE
767     */
768     public void testRemove1_NullPointerException() {
769     try {
770     ConcurrentSkipListMap c = new ConcurrentSkipListMap();
771     c.put("sadsdf", "asdads");
772     c.remove(null);
773     shouldThrow();
774 jsr166 1.14 } catch (NullPointerException success) {}
775 dl 1.1 }
776    
777     /**
778     * remove(null, x) throws NPE
779     */
780     public void testRemove2_NullPointerException() {
781     try {
782     ConcurrentSkipListMap c = new ConcurrentSkipListMap();
783     c.put("sadsdf", "asdads");
784     c.remove(null, "whatever");
785     shouldThrow();
786 jsr166 1.14 } catch (NullPointerException success) {}
787 dl 1.1 }
788    
789     /**
790 dl 1.3 * remove(x, null) returns false
791     */
792     public void testRemove3() {
793 jsr166 1.14 ConcurrentSkipListMap c = new ConcurrentSkipListMap();
794     c.put("sadsdf", "asdads");
795     assertFalse(c.remove("sadsdf", null));
796 dl 1.3 }
797    
798     /**
799 dl 1.1 * A deserialized map equals original
800     */
801 jsr166 1.13 public void testSerialization() throws Exception {
802 dl 1.1 ConcurrentSkipListMap q = map5();
803    
804 jsr166 1.13 ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
805     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
806     out.writeObject(q);
807     out.close();
808    
809     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
810     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
811     ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject();
812     assertEquals(q.size(), r.size());
813     assertTrue(q.equals(r));
814     assertTrue(r.equals(q));
815 dl 1.1 }
816    
817    
818    
819     /**
820     * subMap returns map with keys in requested range
821     */
822     public void testSubMapContents() {
823     ConcurrentSkipListMap map = map5();
824 dl 1.7 NavigableMap sm = map.subMap(two, true, four, false);
825 dl 1.1 assertEquals(two, sm.firstKey());
826     assertEquals(three, sm.lastKey());
827     assertEquals(2, sm.size());
828     assertFalse(sm.containsKey(one));
829     assertTrue(sm.containsKey(two));
830     assertTrue(sm.containsKey(three));
831     assertFalse(sm.containsKey(four));
832     assertFalse(sm.containsKey(five));
833     Iterator i = sm.keySet().iterator();
834     Object k;
835     k = (Integer)(i.next());
836     assertEquals(two, k);
837     k = (Integer)(i.next());
838     assertEquals(three, k);
839     assertFalse(i.hasNext());
840     Iterator r = sm.descendingKeySet().iterator();
841     k = (Integer)(r.next());
842     assertEquals(three, k);
843     k = (Integer)(r.next());
844     assertEquals(two, k);
845     assertFalse(r.hasNext());
846 jsr166 1.9
847 dl 1.1 Iterator j = sm.keySet().iterator();
848     j.next();
849     j.remove();
850     assertFalse(map.containsKey(two));
851     assertEquals(4, map.size());
852     assertEquals(1, sm.size());
853     assertEquals(three, sm.firstKey());
854     assertEquals(three, sm.lastKey());
855 jsr166 1.15 assertEquals("C", sm.remove(three));
856 dl 1.1 assertTrue(sm.isEmpty());
857     assertEquals(3, map.size());
858     }
859    
860     public void testSubMapContents2() {
861     ConcurrentSkipListMap map = map5();
862 dl 1.7 NavigableMap sm = map.subMap(two, true, three, false);
863 dl 1.1 assertEquals(1, sm.size());
864     assertEquals(two, sm.firstKey());
865     assertEquals(two, sm.lastKey());
866     assertFalse(sm.containsKey(one));
867     assertTrue(sm.containsKey(two));
868     assertFalse(sm.containsKey(three));
869     assertFalse(sm.containsKey(four));
870     assertFalse(sm.containsKey(five));
871     Iterator i = sm.keySet().iterator();
872     Object k;
873     k = (Integer)(i.next());
874     assertEquals(two, k);
875     assertFalse(i.hasNext());
876     Iterator r = sm.descendingKeySet().iterator();
877     k = (Integer)(r.next());
878     assertEquals(two, k);
879     assertFalse(r.hasNext());
880 jsr166 1.9
881 dl 1.1 Iterator j = sm.keySet().iterator();
882     j.next();
883     j.remove();
884     assertFalse(map.containsKey(two));
885     assertEquals(4, map.size());
886     assertEquals(0, sm.size());
887     assertTrue(sm.isEmpty());
888 jsr166 1.15 assertSame(sm.remove(three), null);
889 dl 1.1 assertEquals(4, map.size());
890     }
891    
892     /**
893     * headMap returns map with keys in requested range
894     */
895     public void testHeadMapContents() {
896     ConcurrentSkipListMap map = map5();
897 dl 1.7 NavigableMap sm = map.headMap(four, false);
898 dl 1.1 assertTrue(sm.containsKey(one));
899     assertTrue(sm.containsKey(two));
900     assertTrue(sm.containsKey(three));
901     assertFalse(sm.containsKey(four));
902     assertFalse(sm.containsKey(five));
903     Iterator i = sm.keySet().iterator();
904     Object k;
905     k = (Integer)(i.next());
906     assertEquals(one, k);
907     k = (Integer)(i.next());
908     assertEquals(two, k);
909     k = (Integer)(i.next());
910     assertEquals(three, k);
911     assertFalse(i.hasNext());
912     sm.clear();
913     assertTrue(sm.isEmpty());
914     assertEquals(2, map.size());
915     assertEquals(four, map.firstKey());
916     }
917    
918     /**
919 dl 1.6 * tailMap returns map with keys in requested range
920 dl 1.1 */
921     public void testTailMapContents() {
922     ConcurrentSkipListMap map = map5();
923 dl 1.7 NavigableMap sm = map.tailMap(two, true);
924 dl 1.1 assertFalse(sm.containsKey(one));
925     assertTrue(sm.containsKey(two));
926     assertTrue(sm.containsKey(three));
927     assertTrue(sm.containsKey(four));
928     assertTrue(sm.containsKey(five));
929     Iterator i = sm.keySet().iterator();
930     Object k;
931     k = (Integer)(i.next());
932     assertEquals(two, k);
933     k = (Integer)(i.next());
934     assertEquals(three, k);
935     k = (Integer)(i.next());
936     assertEquals(four, k);
937     k = (Integer)(i.next());
938     assertEquals(five, k);
939     assertFalse(i.hasNext());
940     Iterator r = sm.descendingKeySet().iterator();
941     k = (Integer)(r.next());
942     assertEquals(five, k);
943     k = (Integer)(r.next());
944     assertEquals(four, k);
945     k = (Integer)(r.next());
946     assertEquals(three, k);
947     k = (Integer)(r.next());
948     assertEquals(two, k);
949     assertFalse(r.hasNext());
950    
951     Iterator ei = sm.entrySet().iterator();
952     Map.Entry e;
953     e = (Map.Entry)(ei.next());
954     assertEquals(two, e.getKey());
955     assertEquals("B", e.getValue());
956     e = (Map.Entry)(ei.next());
957     assertEquals(three, e.getKey());
958     assertEquals("C", e.getValue());
959     e = (Map.Entry)(ei.next());
960     assertEquals(four, e.getKey());
961     assertEquals("D", e.getValue());
962     e = (Map.Entry)(ei.next());
963     assertEquals(five, e.getKey());
964     assertEquals("E", e.getValue());
965     assertFalse(i.hasNext());
966    
967 dl 1.7 NavigableMap ssm = sm.tailMap(four, true);
968 dl 1.1 assertEquals(four, ssm.firstKey());
969     assertEquals(five, ssm.lastKey());
970 jsr166 1.15 assertEquals("D", ssm.remove(four));
971 dl 1.1 assertEquals(1, ssm.size());
972     assertEquals(3, sm.size());
973     assertEquals(4, map.size());
974     }
975 dl 1.6
976     Random rnd = new Random(666);
977     BitSet bs;
978    
979     /**
980     * Submaps of submaps subdivide correctly
981     */
982 jsr166 1.14 public void testRecursiveSubMaps() throws Exception {
983 jsr166 1.12 int mapSize = 1000;
984     Class cl = ConcurrentSkipListMap.class;
985 dl 1.6 NavigableMap<Integer, Integer> map = newMap(cl);
986     bs = new BitSet(mapSize);
987    
988     populate(map, mapSize);
989     check(map, 0, mapSize - 1, true);
990     check(map.descendingMap(), 0, mapSize - 1, false);
991    
992     mutateMap(map, 0, mapSize - 1);
993     check(map, 0, mapSize - 1, true);
994     check(map.descendingMap(), 0, mapSize - 1, false);
995    
996 dl 1.7 bashSubMap(map.subMap(0, true, mapSize, false),
997 dl 1.6 0, mapSize - 1, true);
998     }
999    
1000 jsr166 1.14 static NavigableMap<Integer, Integer> newMap(Class cl) throws Exception {
1001     NavigableMap<Integer, Integer> result =
1002     (NavigableMap<Integer, Integer>) cl.newInstance();
1003 dl 1.6 assertEquals(result.size(), 0);
1004     assertFalse(result.keySet().iterator().hasNext());
1005     return result;
1006     }
1007    
1008     void populate(NavigableMap<Integer, Integer> map, int limit) {
1009     for (int i = 0, n = 2 * limit / 3; i < n; i++) {
1010     int key = rnd.nextInt(limit);
1011     put(map, key);
1012     }
1013     }
1014    
1015     void mutateMap(NavigableMap<Integer, Integer> map, int min, int max) {
1016     int size = map.size();
1017     int rangeSize = max - min + 1;
1018    
1019     // Remove a bunch of entries directly
1020     for (int i = 0, n = rangeSize / 2; i < n; i++) {
1021     remove(map, min - 5 + rnd.nextInt(rangeSize + 10));
1022     }
1023    
1024     // Remove a bunch of entries with iterator
1025 jsr166 1.10 for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1026 dl 1.6 if (rnd.nextBoolean()) {
1027     bs.clear(it.next());
1028     it.remove();
1029     }
1030     }
1031    
1032     // Add entries till we're back to original size
1033     while (map.size() < size) {
1034     int key = min + rnd.nextInt(rangeSize);
1035     assertTrue(key >= min && key<= max);
1036     put(map, key);
1037     }
1038     }
1039    
1040     void mutateSubMap(NavigableMap<Integer, Integer> map, int min, int max) {
1041     int size = map.size();
1042     int rangeSize = max - min + 1;
1043    
1044     // Remove a bunch of entries directly
1045     for (int i = 0, n = rangeSize / 2; i < n; i++) {
1046     remove(map, min - 5 + rnd.nextInt(rangeSize + 10));
1047     }
1048    
1049     // Remove a bunch of entries with iterator
1050 jsr166 1.10 for (Iterator<Integer> it = map.keySet().iterator(); it.hasNext(); ) {
1051 dl 1.6 if (rnd.nextBoolean()) {
1052     bs.clear(it.next());
1053     it.remove();
1054     }
1055     }
1056    
1057     // Add entries till we're back to original size
1058     while (map.size() < size) {
1059     int key = min - 5 + rnd.nextInt(rangeSize + 10);
1060     if (key >= min && key<= max) {
1061     put(map, key);
1062     } else {
1063     try {
1064     map.put(key, 2 * key);
1065 jsr166 1.14 shouldThrow();
1066     } catch (IllegalArgumentException success) {}
1067 dl 1.6 }
1068     }
1069     }
1070    
1071     void put(NavigableMap<Integer, Integer> map, int key) {
1072     if (map.put(key, 2 * key) == null)
1073     bs.set(key);
1074     }
1075    
1076     void remove(NavigableMap<Integer, Integer> map, int key) {
1077     if (map.remove(key) != null)
1078     bs.clear(key);
1079     }
1080    
1081     void bashSubMap(NavigableMap<Integer, Integer> map,
1082     int min, int max, boolean ascending) {
1083     check(map, min, max, ascending);
1084     check(map.descendingMap(), min, max, !ascending);
1085    
1086     mutateSubMap(map, min, max);
1087     check(map, min, max, ascending);
1088     check(map.descendingMap(), min, max, !ascending);
1089    
1090     // Recurse
1091     if (max - min < 2)
1092     return;
1093     int midPoint = (min + max) / 2;
1094    
1095     // headMap - pick direction and endpoint inclusion randomly
1096     boolean incl = rnd.nextBoolean();
1097 dl 1.7 NavigableMap<Integer,Integer> hm = map.headMap(midPoint, incl);
1098 dl 1.6 if (ascending) {
1099     if (rnd.nextBoolean())
1100     bashSubMap(hm, min, midPoint - (incl ? 0 : 1), true);
1101     else
1102     bashSubMap(hm.descendingMap(), min, midPoint - (incl ? 0 : 1),
1103     false);
1104     } else {
1105     if (rnd.nextBoolean())
1106     bashSubMap(hm, midPoint + (incl ? 0 : 1), max, false);
1107     else
1108     bashSubMap(hm.descendingMap(), midPoint + (incl ? 0 : 1), max,
1109     true);
1110     }
1111    
1112     // tailMap - pick direction and endpoint inclusion randomly
1113     incl = rnd.nextBoolean();
1114 dl 1.7 NavigableMap<Integer,Integer> tm = map.tailMap(midPoint,incl);
1115 dl 1.6 if (ascending) {
1116     if (rnd.nextBoolean())
1117     bashSubMap(tm, midPoint + (incl ? 0 : 1), max, true);
1118     else
1119     bashSubMap(tm.descendingMap(), midPoint + (incl ? 0 : 1), max,
1120     false);
1121     } else {
1122     if (rnd.nextBoolean()) {
1123     bashSubMap(tm, min, midPoint - (incl ? 0 : 1), false);
1124     } else {
1125     bashSubMap(tm.descendingMap(), min, midPoint - (incl ? 0 : 1),
1126     true);
1127     }
1128     }
1129    
1130     // subMap - pick direction and endpoint inclusion randomly
1131     int rangeSize = max - min + 1;
1132     int[] endpoints = new int[2];
1133     endpoints[0] = min + rnd.nextInt(rangeSize);
1134     endpoints[1] = min + rnd.nextInt(rangeSize);
1135     Arrays.sort(endpoints);
1136     boolean lowIncl = rnd.nextBoolean();
1137     boolean highIncl = rnd.nextBoolean();
1138     if (ascending) {
1139 dl 1.7 NavigableMap<Integer,Integer> sm = map.subMap(
1140 dl 1.6 endpoints[0], lowIncl, endpoints[1], highIncl);
1141     if (rnd.nextBoolean())
1142     bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
1143     endpoints[1] - (highIncl ? 0 : 1), true);
1144     else
1145     bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
1146     endpoints[1] - (highIncl ? 0 : 1), false);
1147     } else {
1148 dl 1.7 NavigableMap<Integer,Integer> sm = map.subMap(
1149 dl 1.6 endpoints[1], highIncl, endpoints[0], lowIncl);
1150     if (rnd.nextBoolean())
1151     bashSubMap(sm, endpoints[0] + (lowIncl ? 0 : 1),
1152     endpoints[1] - (highIncl ? 0 : 1), false);
1153     else
1154     bashSubMap(sm.descendingMap(), endpoints[0] + (lowIncl ? 0 : 1),
1155     endpoints[1] - (highIncl ? 0 : 1), true);
1156     }
1157     }
1158    
1159     /**
1160     * min and max are both inclusive. If max < min, interval is empty.
1161     */
1162     void check(NavigableMap<Integer, Integer> map,
1163     final int min, final int max, final boolean ascending) {
1164     class ReferenceSet {
1165     int lower(int key) {
1166     return ascending ? lowerAscending(key) : higherAscending(key);
1167     }
1168     int floor(int key) {
1169     return ascending ? floorAscending(key) : ceilingAscending(key);
1170     }
1171     int ceiling(int key) {
1172     return ascending ? ceilingAscending(key) : floorAscending(key);
1173     }
1174     int higher(int key) {
1175     return ascending ? higherAscending(key) : lowerAscending(key);
1176     }
1177     int first() {
1178     return ascending ? firstAscending() : lastAscending();
1179     }
1180     int last() {
1181     return ascending ? lastAscending() : firstAscending();
1182     }
1183     int lowerAscending(int key) {
1184     return floorAscending(key - 1);
1185     }
1186     int floorAscending(int key) {
1187     if (key < min)
1188     return -1;
1189     else if (key > max)
1190     key = max;
1191    
1192     // BitSet should support this! Test would run much faster
1193     while (key >= min) {
1194     if (bs.get(key))
1195     return(key);
1196     key--;
1197     }
1198     return -1;
1199     }
1200     int ceilingAscending(int key) {
1201     if (key < min)
1202     key = min;
1203     else if (key > max)
1204     return -1;
1205     int result = bs.nextSetBit(key);
1206     return result > max ? -1 : result;
1207     }
1208     int higherAscending(int key) {
1209     return ceilingAscending(key + 1);
1210     }
1211     private int firstAscending() {
1212     int result = ceilingAscending(min);
1213     return result > max ? -1 : result;
1214     }
1215     private int lastAscending() {
1216     int result = floorAscending(max);
1217     return result < min ? -1 : result;
1218     }
1219     }
1220     ReferenceSet rs = new ReferenceSet();
1221    
1222     // Test contents using containsKey
1223     int size = 0;
1224     for (int i = min; i <= max; i++) {
1225     boolean bsContainsI = bs.get(i);
1226     assertEquals(bsContainsI, map.containsKey(i));
1227     if (bsContainsI)
1228     size++;
1229     }
1230     assertEquals(map.size(), size);
1231    
1232     // Test contents using contains keySet iterator
1233     int size2 = 0;
1234     int previousKey = -1;
1235     for (int key : map.keySet()) {
1236     assertTrue(bs.get(key));
1237     size2++;
1238     assertTrue(previousKey < 0 ||
1239     (ascending ? key - previousKey > 0 : key - previousKey < 0));
1240     previousKey = key;
1241     }
1242     assertEquals(size2, size);
1243    
1244     // Test navigation ops
1245     for (int key = min - 1; key <= max + 1; key++) {
1246     assertEq(map.lowerKey(key), rs.lower(key));
1247     assertEq(map.floorKey(key), rs.floor(key));
1248     assertEq(map.higherKey(key), rs.higher(key));
1249     assertEq(map.ceilingKey(key), rs.ceiling(key));
1250     }
1251    
1252     // Test extrema
1253     if (map.size() != 0) {
1254     assertEq(map.firstKey(), rs.first());
1255     assertEq(map.lastKey(), rs.last());
1256     } else {
1257     assertEq(rs.first(), -1);
1258     assertEq(rs.last(), -1);
1259     try {
1260     map.firstKey();
1261 jsr166 1.14 shouldThrow();
1262     } catch (NoSuchElementException success) {}
1263 dl 1.6 try {
1264     map.lastKey();
1265 jsr166 1.14 shouldThrow();
1266     } catch (NoSuchElementException success) {}
1267 dl 1.6 }
1268     }
1269    
1270     static void assertEq(Integer i, int j) {
1271     if (i == null)
1272     assertEquals(j, -1);
1273     else
1274     assertEquals((int) i, j);
1275     }
1276    
1277     static boolean eq(Integer i, int j) {
1278     return i == null ? j == -1 : i == j;
1279     }
1280 jsr166 1.9
1281 dl 1.1 }