ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java
Revision: 1.1
Committed: Tue Dec 28 16:15:59 2004 UTC (19 years, 4 months ago) by dl
Branch: MAIN
Log Message:
Integrate tests for jsr166x classes

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 ConcurrentSkipListSubMapTest extends JSR166TestCase {
13     public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16     public static Test suite() {
17     return new TestSuite(ConcurrentSkipListSubMapTest.class);
18     }
19    
20     /**
21     * Create a map from Integers 1-5 to Strings "A"-"E".
22     */
23     private static ConcurrentNavigableMap map5() {
24     ConcurrentSkipListMap map = new ConcurrentSkipListMap();
25     assertTrue(map.isEmpty());
26     map.put(zero, "Z");
27     map.put(one, "A");
28     map.put(five, "E");
29     map.put(three, "C");
30     map.put(two, "B");
31     map.put(four, "D");
32     map.put(seven, "F");
33     assertFalse(map.isEmpty());
34     assertEquals(7, map.size());
35     return map.subMap(one, seven);
36     }
37    
38     private static ConcurrentNavigableMap map0() {
39     ConcurrentSkipListMap map = new ConcurrentSkipListMap();
40     assertTrue(map.isEmpty());
41     return map.tailMap(one);
42     }
43    
44     /**
45     * clear removes all pairs
46     */
47     public void testClear() {
48     ConcurrentNavigableMap map = map5();
49     map.clear();
50     assertEquals(map.size(), 0);
51     }
52    
53    
54     /**
55     * Maps with same contents are equal
56     */
57     public void testEquals() {
58     ConcurrentNavigableMap map1 = map5();
59     ConcurrentNavigableMap map2 = map5();
60     assertEquals(map1, map2);
61     assertEquals(map2, map1);
62     map1.clear();
63     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     ConcurrentNavigableMap map = map5();
72     assertTrue(map.containsKey(one));
73     assertFalse(map.containsKey(zero));
74     }
75    
76     /**
77     * containsValue returns true for held values
78     */
79     public void testContainsValue() {
80     ConcurrentNavigableMap map = map5();
81     assertTrue(map.containsValue("A"));
82     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     ConcurrentNavigableMap map = map5();
91     assertEquals("A", (String)map.get(one));
92     ConcurrentNavigableMap empty = map0();
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     ConcurrentNavigableMap empty = map0();
101     ConcurrentNavigableMap map = map5();
102     assertTrue(empty.isEmpty());
103     assertFalse(map.isEmpty());
104     }
105    
106     /**
107     * firstKey returns first key
108     */
109     public void testFirstKey() {
110     ConcurrentNavigableMap map = map5();
111     assertEquals(one, map.firstKey());
112     }
113    
114     /**
115     * lastKey returns last key
116     */
117     public void testLastKey() {
118     ConcurrentNavigableMap map = map5();
119     assertEquals(five, map.lastKey());
120     }
121    
122    
123     /**
124     * keySet returns a Set containing all the keys
125     */
126     public void testKeySet() {
127     ConcurrentNavigableMap map = map5();
128     Set s = map.keySet();
129     assertEquals(5, s.size());
130     assertTrue(s.contains(one));
131     assertTrue(s.contains(two));
132     assertTrue(s.contains(three));
133     assertTrue(s.contains(four));
134     assertTrue(s.contains(five));
135     }
136    
137     /**
138     * keySet is ordered
139     */
140     public void testKeySetOrder() {
141     ConcurrentNavigableMap map = map5();
142     Set s = map.keySet();
143     Iterator i = s.iterator();
144     Integer last = (Integer)i.next();
145     assertEquals(last, one);
146     while (i.hasNext()) {
147     Integer k = (Integer)i.next();
148     assertTrue(last.compareTo(k) < 0);
149     last = k;
150     }
151     }
152    
153     /**
154     * values collection contains all values
155     */
156     public void testValues() {
157     ConcurrentNavigableMap map = map5();
158     Collection s = map.values();
159     assertEquals(5, s.size());
160     assertTrue(s.contains("A"));
161     assertTrue(s.contains("B"));
162     assertTrue(s.contains("C"));
163     assertTrue(s.contains("D"));
164     assertTrue(s.contains("E"));
165     }
166    
167     /**
168     * entrySet contains all pairs
169     */
170     public void testEntrySet() {
171     ConcurrentNavigableMap map = map5();
172     Set s = map.entrySet();
173     assertEquals(5, s.size());
174     Iterator it = s.iterator();
175     while (it.hasNext()) {
176     Map.Entry e = (Map.Entry) it.next();
177     assertTrue(
178     (e.getKey().equals(one) && e.getValue().equals("A")) ||
179     (e.getKey().equals(two) && e.getValue().equals("B")) ||
180     (e.getKey().equals(three) && e.getValue().equals("C")) ||
181     (e.getKey().equals(four) && e.getValue().equals("D")) ||
182     (e.getKey().equals(five) && e.getValue().equals("E")));
183     }
184     }
185    
186     /**
187     * putAll adds all key-value pairs from the given map
188     */
189     public void testPutAll() {
190     ConcurrentNavigableMap empty = map0();
191     ConcurrentNavigableMap map = map5();
192     empty.putAll(map);
193     assertEquals(5, empty.size());
194     assertTrue(empty.containsKey(one));
195     assertTrue(empty.containsKey(two));
196     assertTrue(empty.containsKey(three));
197     assertTrue(empty.containsKey(four));
198     assertTrue(empty.containsKey(five));
199     }
200    
201     /**
202     * putIfAbsent works when the given key is not present
203     */
204     public void testPutIfAbsent() {
205     ConcurrentNavigableMap map = map5();
206     map.putIfAbsent(six, "Z");
207     assertTrue(map.containsKey(six));
208     }
209    
210     /**
211     * putIfAbsent does not add the pair if the key is already present
212     */
213     public void testPutIfAbsent2() {
214     ConcurrentNavigableMap map = map5();
215     assertEquals("A", map.putIfAbsent(one, "Z"));
216     }
217    
218     /**
219     * replace fails when the given key is not present
220     */
221     public void testReplace() {
222     ConcurrentNavigableMap map = map5();
223     assertNull(map.replace(six, "Z"));
224     assertFalse(map.containsKey(six));
225     }
226    
227     /**
228     * replace succeeds if the key is already present
229     */
230     public void testReplace2() {
231     ConcurrentNavigableMap map = map5();
232     assertNotNull(map.replace(one, "Z"));
233     assertEquals("Z", map.get(one));
234     }
235    
236    
237     /**
238     * replace value fails when the given key not mapped to expected value
239     */
240     public void testReplaceValue() {
241     ConcurrentNavigableMap map = map5();
242     assertEquals("A", map.get(one));
243     assertFalse(map.replace(one, "Z", "Z"));
244     assertEquals("A", map.get(one));
245     }
246    
247     /**
248     * replace value succeeds when the given key mapped to expected value
249     */
250     public void testReplaceValue2() {
251     ConcurrentNavigableMap map = map5();
252     assertEquals("A", map.get(one));
253     assertTrue(map.replace(one, "A", "Z"));
254     assertEquals("Z", map.get(one));
255     }
256    
257    
258     /**
259     * remove removes the correct key-value pair from the map
260     */
261     public void testRemove() {
262     ConcurrentNavigableMap map = map5();
263     map.remove(five);
264     assertEquals(4, map.size());
265     assertFalse(map.containsKey(five));
266     }
267    
268     /**
269     * remove(key,value) removes only if pair present
270     */
271     public void testRemove2() {
272     ConcurrentNavigableMap map = map5();
273     assertTrue(map.containsKey(five));
274     assertEquals("E", map.get(five));
275     map.remove(five, "E");
276     assertEquals(4, map.size());
277     assertFalse(map.containsKey(five));
278     map.remove(four, "A");
279     assertEquals(4, map.size());
280     assertTrue(map.containsKey(four));
281    
282     }
283    
284     /**
285     * lowerEntry returns preceding entry.
286     */
287     public void testLowerEntry() {
288     ConcurrentNavigableMap map = map5();
289     Map.Entry e1 = map.lowerEntry(three);
290     assertEquals(two, e1.getKey());
291    
292     Map.Entry e2 = map.lowerEntry(six);
293     assertEquals(five, e2.getKey());
294    
295     Map.Entry e3 = map.lowerEntry(one);
296     assertNull(e3);
297    
298     Map.Entry e4 = map.lowerEntry(zero);
299     assertNull(e4);
300    
301     }
302    
303     /**
304     * higherEntry returns next entry.
305     */
306     public void testHigherEntry() {
307     ConcurrentNavigableMap map = map5();
308     Map.Entry e1 = map.higherEntry(three);
309     assertEquals(four, e1.getKey());
310    
311     Map.Entry e2 = map.higherEntry(zero);
312     assertEquals(one, e2.getKey());
313    
314     Map.Entry e3 = map.higherEntry(five);
315     assertNull(e3);
316    
317     Map.Entry e4 = map.higherEntry(six);
318     assertNull(e4);
319    
320     }
321    
322     /**
323     * floorEntry returns preceding entry.
324     */
325     public void testFloorEntry() {
326     ConcurrentNavigableMap map = map5();
327     Map.Entry e1 = map.floorEntry(three);
328     assertEquals(three, e1.getKey());
329    
330     Map.Entry e2 = map.floorEntry(six);
331     assertEquals(five, e2.getKey());
332    
333     Map.Entry e3 = map.floorEntry(one);
334     assertEquals(one, e3.getKey());
335    
336     Map.Entry e4 = map.floorEntry(zero);
337     assertNull(e4);
338    
339     }
340    
341     /**
342     * ceilingEntry returns next entry.
343     */
344     public void testCeilingEntry() {
345     ConcurrentNavigableMap map = map5();
346     Map.Entry e1 = map.ceilingEntry(three);
347     assertEquals(three, e1.getKey());
348    
349     Map.Entry e2 = map.ceilingEntry(zero);
350     assertEquals(one, e2.getKey());
351    
352     Map.Entry e3 = map.ceilingEntry(five);
353     assertEquals(five, e3.getKey());
354    
355     Map.Entry e4 = map.ceilingEntry(six);
356     assertNull(e4);
357    
358     }
359    
360     /**
361     * pollFirstEntry returns entries in order
362     */
363     public void testPollFirstEntry() {
364     ConcurrentNavigableMap map = map5();
365     Map.Entry e = map.pollFirstEntry();
366     assertEquals(one, e.getKey());
367     assertEquals("A", e.getValue());
368     e = map.pollFirstEntry();
369     assertEquals(two, e.getKey());
370     map.put(one, "A");
371     e = map.pollFirstEntry();
372     assertEquals(one, e.getKey());
373     assertEquals("A", e.getValue());
374     e = map.pollFirstEntry();
375     assertEquals(three, e.getKey());
376     map.remove(four);
377     e = map.pollFirstEntry();
378     assertEquals(five, e.getKey());
379     try {
380     e.setValue("A");
381     shouldThrow();
382     } catch (Exception ok) {
383     }
384     e = map.pollFirstEntry();
385     assertNull(e);
386     }
387    
388     /**
389     * pollLastEntry returns entries in order
390     */
391     public void testPollLastEntry() {
392     ConcurrentNavigableMap map = map5();
393     Map.Entry e = map.pollLastEntry();
394     assertEquals(five, e.getKey());
395     assertEquals("E", e.getValue());
396     e = map.pollLastEntry();
397     assertEquals(four, e.getKey());
398     map.put(five, "E");
399     e = map.pollLastEntry();
400     assertEquals(five, e.getKey());
401     assertEquals("E", e.getValue());
402     e = map.pollLastEntry();
403     assertEquals(three, e.getKey());
404     map.remove(two);
405     e = map.pollLastEntry();
406     assertEquals(one, e.getKey());
407     try {
408     e.setValue("E");
409     shouldThrow();
410     } catch (Exception ok) {
411     }
412     e = map.pollLastEntry();
413     assertNull(e);
414     }
415    
416     /**
417     * size returns the correct values
418     */
419     public void testSize() {
420     ConcurrentNavigableMap map = map5();
421     ConcurrentNavigableMap empty = map0();
422     assertEquals(0, empty.size());
423     assertEquals(5, map.size());
424     }
425    
426     /**
427     * toString contains toString of elements
428     */
429     public void testToString() {
430     ConcurrentNavigableMap map = map5();
431     String s = map.toString();
432     for (int i = 1; i <= 5; ++i) {
433     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
434     }
435     }
436    
437     // Exception tests
438    
439     /**
440     * get(null) of nonempty map throws NPE
441     */
442     public void testGet_NullPointerException() {
443     try {
444     ConcurrentNavigableMap c = map5();
445     c.get(null);
446     shouldThrow();
447     } catch(NullPointerException e){}
448     }
449    
450     /**
451     * containsKey(null) of nonempty map throws NPE
452     */
453     public void testContainsKey_NullPointerException() {
454     try {
455     ConcurrentNavigableMap c = map5();
456     c.containsKey(null);
457     shouldThrow();
458     } catch(NullPointerException e){}
459     }
460    
461     /**
462     * containsValue(null) throws NPE
463     */
464     public void testContainsValue_NullPointerException() {
465     try {
466     ConcurrentNavigableMap c = map0();
467     c.containsValue(null);
468     shouldThrow();
469     } catch(NullPointerException e){}
470     }
471    
472    
473     /**
474     * put(null,x) throws NPE
475     */
476     public void testPut1_NullPointerException() {
477     try {
478     ConcurrentNavigableMap c = map5();
479     c.put(null, "whatever");
480     shouldThrow();
481     } catch(NullPointerException e){}
482     }
483    
484     /**
485     * putIfAbsent(null, x) throws NPE
486     */
487     public void testPutIfAbsent1_NullPointerException() {
488     try {
489     ConcurrentNavigableMap c = map5();
490     c.putIfAbsent(null, "whatever");
491     shouldThrow();
492     } catch(NullPointerException e){}
493     }
494    
495     /**
496     * replace(null, x) throws NPE
497     */
498     public void testReplace_NullPointerException() {
499     try {
500     ConcurrentNavigableMap c = map5();
501     c.replace(null, "whatever");
502     shouldThrow();
503     } catch(NullPointerException e){}
504     }
505    
506     /**
507     * replace(null, x, y) throws NPE
508     */
509     public void testReplaceValue_NullPointerException() {
510     try {
511     ConcurrentNavigableMap c = map5();
512     c.replace(null, one, "whatever");
513     shouldThrow();
514     } catch(NullPointerException e){}
515     }
516    
517     /**
518     * remove(null) throws NPE
519     */
520     public void testRemove1_NullPointerException() {
521     try {
522     ConcurrentNavigableMap c = map5();
523     c.remove(null);
524     shouldThrow();
525     } catch(NullPointerException e){}
526     }
527    
528     /**
529     * remove(null, x) throws NPE
530     */
531     public void testRemove2_NullPointerException() {
532     try {
533     ConcurrentNavigableMap c = map5();
534     c.remove(null, "whatever");
535     shouldThrow();
536     } catch(NullPointerException e){}
537     }
538    
539     /**
540     * A deserialized map equals original
541     */
542     public void testSerialization() {
543     ConcurrentNavigableMap q = map5();
544    
545     try {
546     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
547     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
548     out.writeObject(q);
549     out.close();
550    
551     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
552     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
553     ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
554     assertEquals(q.size(), r.size());
555     assertTrue(q.equals(r));
556     assertTrue(r.equals(q));
557     } catch(Exception e){
558     e.printStackTrace();
559     unexpectedException();
560     }
561     }
562    
563    
564    
565     /**
566     * subMap returns map with keys in requested range
567     */
568     public void testSubMapContents() {
569     ConcurrentNavigableMap map = map5();
570     SortedMap sm = map.subMap(two, four);
571     assertEquals(two, sm.firstKey());
572     assertEquals(three, sm.lastKey());
573     assertEquals(2, sm.size());
574     assertFalse(sm.containsKey(one));
575     assertTrue(sm.containsKey(two));
576     assertTrue(sm.containsKey(three));
577     assertFalse(sm.containsKey(four));
578     assertFalse(sm.containsKey(five));
579     Iterator i = sm.keySet().iterator();
580     Object k;
581     k = (Integer)(i.next());
582     assertEquals(two, k);
583     k = (Integer)(i.next());
584     assertEquals(three, k);
585     assertFalse(i.hasNext());
586     Iterator j = sm.keySet().iterator();
587     j.next();
588     j.remove();
589     assertFalse(map.containsKey(two));
590     assertEquals(4, map.size());
591     assertEquals(1, sm.size());
592     assertEquals(three, sm.firstKey());
593     assertEquals(three, sm.lastKey());
594     assertTrue(sm.remove(three) != null);
595     assertTrue(sm.isEmpty());
596     assertEquals(3, map.size());
597     }
598    
599     public void testSubMapContents2() {
600     ConcurrentNavigableMap map = map5();
601     SortedMap sm = map.subMap(two, three);
602     assertEquals(1, sm.size());
603     assertEquals(two, sm.firstKey());
604     assertEquals(two, sm.lastKey());
605     assertFalse(sm.containsKey(one));
606     assertTrue(sm.containsKey(two));
607     assertFalse(sm.containsKey(three));
608     assertFalse(sm.containsKey(four));
609     assertFalse(sm.containsKey(five));
610     Iterator i = sm.keySet().iterator();
611     Object k;
612     k = (Integer)(i.next());
613     assertEquals(two, k);
614     assertFalse(i.hasNext());
615     Iterator j = sm.keySet().iterator();
616     j.next();
617     j.remove();
618     assertFalse(map.containsKey(two));
619     assertEquals(4, map.size());
620     assertEquals(0, sm.size());
621     assertTrue(sm.isEmpty());
622     assertTrue(sm.remove(three) == null);
623     assertEquals(4, map.size());
624     }
625    
626     /**
627     * headMap returns map with keys in requested range
628     */
629     public void testHeadMapContents() {
630     ConcurrentNavigableMap map = map5();
631     SortedMap sm = map.headMap(four);
632     assertTrue(sm.containsKey(one));
633     assertTrue(sm.containsKey(two));
634     assertTrue(sm.containsKey(three));
635     assertFalse(sm.containsKey(four));
636     assertFalse(sm.containsKey(five));
637     Iterator i = sm.keySet().iterator();
638     Object k;
639     k = (Integer)(i.next());
640     assertEquals(one, k);
641     k = (Integer)(i.next());
642     assertEquals(two, k);
643     k = (Integer)(i.next());
644     assertEquals(three, k);
645     assertFalse(i.hasNext());
646     sm.clear();
647     assertTrue(sm.isEmpty());
648     assertEquals(2, map.size());
649     assertEquals(four, map.firstKey());
650     }
651    
652     /**
653     * headMap returns map with keys in requested range
654     */
655     public void testTailMapContents() {
656     ConcurrentNavigableMap map = map5();
657     SortedMap sm = map.tailMap(two);
658     assertFalse(sm.containsKey(one));
659     assertTrue(sm.containsKey(two));
660     assertTrue(sm.containsKey(three));
661     assertTrue(sm.containsKey(four));
662     assertTrue(sm.containsKey(five));
663     Iterator i = sm.keySet().iterator();
664     Object k;
665     k = (Integer)(i.next());
666     assertEquals(two, k);
667     k = (Integer)(i.next());
668     assertEquals(three, k);
669     k = (Integer)(i.next());
670     assertEquals(four, k);
671     k = (Integer)(i.next());
672     assertEquals(five, k);
673     assertFalse(i.hasNext());
674    
675     Iterator ei = sm.entrySet().iterator();
676     Map.Entry e;
677     e = (Map.Entry)(ei.next());
678     assertEquals(two, e.getKey());
679     assertEquals("B", e.getValue());
680     e = (Map.Entry)(ei.next());
681     assertEquals(three, e.getKey());
682     assertEquals("C", e.getValue());
683     e = (Map.Entry)(ei.next());
684     assertEquals(four, e.getKey());
685     assertEquals("D", e.getValue());
686     e = (Map.Entry)(ei.next());
687     assertEquals(five, e.getKey());
688     assertEquals("E", e.getValue());
689     assertFalse(i.hasNext());
690    
691     SortedMap ssm = sm.tailMap(four);
692     assertEquals(four, ssm.firstKey());
693     assertEquals(five, ssm.lastKey());
694     assertTrue(ssm.remove(four) != null);
695     assertEquals(1, ssm.size());
696     assertEquals(3, sm.size());
697     assertEquals(4, map.size());
698     }
699    
700     }