ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListMapTest.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 ConcurrentSkipListMapTest 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(ConcurrentSkipListMapTest.class);
18     }
19    
20     /**
21     * Create a map from Integers 1-5 to Strings "A"-"E".
22     */
23     private static ConcurrentSkipListMap map5() {
24     ConcurrentSkipListMap map = new ConcurrentSkipListMap();
25     assertTrue(map.isEmpty());
26     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     assertFalse(map.isEmpty());
32     assertEquals(5, map.size());
33     return map;
34     }
35    
36     /**
37     * clear removes all pairs
38     */
39     public void testClear() {
40     ConcurrentSkipListMap map = map5();
41     map.clear();
42     assertEquals(map.size(), 0);
43     }
44    
45     /**
46     *
47     */
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     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     ConcurrentSkipListMap 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     ConcurrentSkipListMap 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     ConcurrentSkipListMap map = map5();
91     assertEquals("A", (String)map.get(one));
92     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     assertTrue(empty.isEmpty());
103     assertFalse(map.isEmpty());
104     }
105    
106     /**
107     * firstKey returns first key
108     */
109     public void testFirstKey() {
110     ConcurrentSkipListMap map = map5();
111     assertEquals(one, map.firstKey());
112     }
113    
114     /**
115     * lastKey returns last key
116     */
117     public void testLastKey() {
118     ConcurrentSkipListMap map = map5();
119     assertEquals(five, map.lastKey());
120     }
121    
122    
123     /**
124     * keySet.toArray returns contains all keys
125     */
126     public void testKeySetToArray() {
127     ConcurrentSkipListMap map = map5();
128     Set s = map.keySet();
129     Object[] ar = s.toArray();
130     assertTrue(s.containsAll(Arrays.asList(ar)));
131     assertEquals(5, ar.length);
132     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     Set s = map.descendingKeySet();
142     Object[] ar = s.toArray();
143     assertEquals(5, ar.length);
144     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     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     }
162    
163     /**
164     * keySet is ordered
165     */
166     public void testKeySetOrder() {
167     ConcurrentSkipListMap map = map5();
168     Set s = map.keySet();
169     Iterator i = s.iterator();
170     Integer last = (Integer)i.next();
171     assertEquals(last, one);
172     while (i.hasNext()) {
173     Integer k = (Integer)i.next();
174     assertTrue(last.compareTo(k) < 0);
175     last = k;
176     }
177     }
178    
179     /**
180     * descendingKeySet is ordered
181     */
182     public void testDescendingKeySetOrder() {
183     ConcurrentSkipListMap map = map5();
184     Set s = map.descendingKeySet();
185     Iterator i = s.iterator();
186     Integer last = (Integer)i.next();
187     assertEquals(last, five);
188     while (i.hasNext()) {
189     Integer k = (Integer)i.next();
190     assertTrue(last.compareTo(k) > 0);
191     last = k;
192     }
193     }
194    
195     /**
196     * values collection contains all values
197     */
198     public void testValues() {
199     ConcurrentSkipListMap map = map5();
200     Collection s = map.values();
201     assertEquals(5, s.size());
202     assertTrue(s.contains("A"));
203     assertTrue(s.contains("B"));
204     assertTrue(s.contains("C"));
205     assertTrue(s.contains("D"));
206     assertTrue(s.contains("E"));
207     }
208    
209     /**
210     * entrySet contains all pairs
211     */
212     public void testEntrySet() {
213     ConcurrentSkipListMap map = map5();
214     Set s = map.entrySet();
215     assertEquals(5, s.size());
216     Iterator it = s.iterator();
217     while (it.hasNext()) {
218     Map.Entry e = (Map.Entry) it.next();
219     assertTrue(
220     (e.getKey().equals(one) && e.getValue().equals("A")) ||
221     (e.getKey().equals(two) && e.getValue().equals("B")) ||
222     (e.getKey().equals(three) && e.getValue().equals("C")) ||
223     (e.getKey().equals(four) && e.getValue().equals("D")) ||
224     (e.getKey().equals(five) && e.getValue().equals("E")));
225     }
226     }
227    
228     /**
229     * descendingEntrySet contains all pairs
230     */
231     public void testDescendingEntrySet() {
232     ConcurrentSkipListMap map = map5();
233     Set s = map.descendingEntrySet();
234     assertEquals(5, s.size());
235     Iterator it = s.iterator();
236     while (it.hasNext()) {
237     Map.Entry e = (Map.Entry) it.next();
238     assertTrue(
239     (e.getKey().equals(one) && e.getValue().equals("A")) ||
240     (e.getKey().equals(two) && e.getValue().equals("B")) ||
241     (e.getKey().equals(three) && e.getValue().equals("C")) ||
242     (e.getKey().equals(four) && e.getValue().equals("D")) ||
243     (e.getKey().equals(five) && e.getValue().equals("E")));
244     }
245     }
246    
247     /**
248     * entrySet.toArray contains all entries
249     */
250     public void testEntrySetToArray() {
251     ConcurrentSkipListMap map = map5();
252     Set s = map.entrySet();
253     Object[] ar = s.toArray();
254     assertEquals(5, ar.length);
255     for (int i = 0; i < 5; ++i) {
256     assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
257     assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
258     }
259     }
260    
261     /**
262     * descendingEntrySet.toArray contains all entries
263     */
264     public void testDescendingEntrySetToArray() {
265     ConcurrentSkipListMap map = map5();
266     Set s = map.descendingEntrySet();
267     Object[] ar = s.toArray();
268     assertEquals(5, ar.length);
269     for (int i = 0; i < 5; ++i) {
270     assertTrue(map.containsKey(((Map.Entry)(ar[i])).getKey()));
271     assertTrue(map.containsValue(((Map.Entry)(ar[i])).getValue()));
272     }
273     }
274    
275     /**
276     * putAll adds all key-value pairs from the given map
277     */
278     public void testPutAll() {
279     ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
280     ConcurrentSkipListMap map = map5();
281     empty.putAll(map);
282     assertEquals(5, empty.size());
283     assertTrue(empty.containsKey(one));
284     assertTrue(empty.containsKey(two));
285     assertTrue(empty.containsKey(three));
286     assertTrue(empty.containsKey(four));
287     assertTrue(empty.containsKey(five));
288     }
289    
290     /**
291     * putIfAbsent works when the given key is not present
292     */
293     public void testPutIfAbsent() {
294     ConcurrentSkipListMap map = map5();
295     map.putIfAbsent(six, "Z");
296     assertTrue(map.containsKey(six));
297     }
298    
299     /**
300     * putIfAbsent does not add the pair if the key is already present
301     */
302     public void testPutIfAbsent2() {
303     ConcurrentSkipListMap map = map5();
304     assertEquals("A", map.putIfAbsent(one, "Z"));
305     }
306    
307     /**
308     * replace fails when the given key is not present
309     */
310     public void testReplace() {
311     ConcurrentSkipListMap map = map5();
312     assertNull(map.replace(six, "Z"));
313     assertFalse(map.containsKey(six));
314     }
315    
316     /**
317     * replace succeeds if the key is already present
318     */
319     public void testReplace2() {
320     ConcurrentSkipListMap map = map5();
321     assertNotNull(map.replace(one, "Z"));
322     assertEquals("Z", map.get(one));
323     }
324    
325    
326     /**
327     * replace value fails when the given key not mapped to expected value
328     */
329     public void testReplaceValue() {
330     ConcurrentSkipListMap map = map5();
331     assertEquals("A", map.get(one));
332     assertFalse(map.replace(one, "Z", "Z"));
333     assertEquals("A", map.get(one));
334     }
335    
336     /**
337     * replace value succeeds when the given key mapped to expected value
338     */
339     public void testReplaceValue2() {
340     ConcurrentSkipListMap map = map5();
341     assertEquals("A", map.get(one));
342     assertTrue(map.replace(one, "A", "Z"));
343     assertEquals("Z", map.get(one));
344     }
345    
346    
347     /**
348     * remove removes the correct key-value pair from the map
349     */
350     public void testRemove() {
351     ConcurrentSkipListMap map = map5();
352     map.remove(five);
353     assertEquals(4, map.size());
354     assertFalse(map.containsKey(five));
355     }
356    
357     /**
358     * remove(key,value) removes only if pair present
359     */
360     public void testRemove2() {
361     ConcurrentSkipListMap map = map5();
362     assertTrue(map.containsKey(five));
363     assertEquals("E", map.get(five));
364     map.remove(five, "E");
365     assertEquals(4, map.size());
366     assertFalse(map.containsKey(five));
367     map.remove(four, "A");
368     assertEquals(4, map.size());
369     assertTrue(map.containsKey(four));
370    
371     }
372    
373     /**
374     * lowerEntry returns preceding entry.
375     */
376     public void testLowerEntry() {
377     ConcurrentSkipListMap map = map5();
378     Map.Entry e1 = map.lowerEntry(three);
379     assertEquals(two, e1.getKey());
380    
381     Map.Entry e2 = map.lowerEntry(six);
382     assertEquals(five, e2.getKey());
383    
384     Map.Entry e3 = map.lowerEntry(one);
385     assertNull(e3);
386    
387     Map.Entry e4 = map.lowerEntry(zero);
388     assertNull(e4);
389    
390     }
391    
392     /**
393     * higherEntry returns next entry.
394     */
395     public void testHigherEntry() {
396     ConcurrentSkipListMap map = map5();
397     Map.Entry e1 = map.higherEntry(three);
398     assertEquals(four, e1.getKey());
399    
400     Map.Entry e2 = map.higherEntry(zero);
401     assertEquals(one, e2.getKey());
402    
403     Map.Entry e3 = map.higherEntry(five);
404     assertNull(e3);
405    
406     Map.Entry e4 = map.higherEntry(six);
407     assertNull(e4);
408    
409     }
410    
411     /**
412     * floorEntry returns preceding entry.
413     */
414     public void testFloorEntry() {
415     ConcurrentSkipListMap map = map5();
416     Map.Entry e1 = map.floorEntry(three);
417     assertEquals(three, e1.getKey());
418    
419     Map.Entry e2 = map.floorEntry(six);
420     assertEquals(five, e2.getKey());
421    
422     Map.Entry e3 = map.floorEntry(one);
423     assertEquals(one, e3.getKey());
424    
425     Map.Entry e4 = map.floorEntry(zero);
426     assertNull(e4);
427    
428     }
429    
430     /**
431     * ceilingEntry returns next entry.
432     */
433     public void testCeilingEntry() {
434     ConcurrentSkipListMap map = map5();
435     Map.Entry e1 = map.ceilingEntry(three);
436     assertEquals(three, e1.getKey());
437    
438     Map.Entry e2 = map.ceilingEntry(zero);
439     assertEquals(one, e2.getKey());
440    
441     Map.Entry e3 = map.ceilingEntry(five);
442     assertEquals(five, e3.getKey());
443    
444     Map.Entry e4 = map.ceilingEntry(six);
445     assertNull(e4);
446    
447     }
448    
449    
450     /**
451     * lowerKey returns preceding element
452     */
453     public void testLowerKey() {
454     ConcurrentSkipListMap q = map5();
455     Object e1 = q.lowerKey(three);
456     assertEquals(two, e1);
457    
458     Object e2 = q.lowerKey(six);
459     assertEquals(five, e2);
460    
461     Object e3 = q.lowerKey(one);
462     assertNull(e3);
463    
464     Object e4 = q.lowerKey(zero);
465     assertNull(e4);
466    
467     }
468    
469     /**
470     * higherKey returns next element
471     */
472     public void testHigherKey() {
473     ConcurrentSkipListMap q = map5();
474     Object e1 = q.higherKey(three);
475     assertEquals(four, e1);
476    
477     Object e2 = q.higherKey(zero);
478     assertEquals(one, e2);
479    
480     Object e3 = q.higherKey(five);
481     assertNull(e3);
482    
483     Object e4 = q.higherKey(six);
484     assertNull(e4);
485    
486     }
487    
488     /**
489     * floorKey returns preceding element
490     */
491     public void testFloorKey() {
492     ConcurrentSkipListMap q = map5();
493     Object e1 = q.floorKey(three);
494     assertEquals(three, e1);
495    
496     Object e2 = q.floorKey(six);
497     assertEquals(five, e2);
498    
499     Object e3 = q.floorKey(one);
500     assertEquals(one, e3);
501    
502     Object e4 = q.floorKey(zero);
503     assertNull(e4);
504    
505     }
506    
507     /**
508     * ceilingKey returns next element
509     */
510     public void testCeilingKey() {
511     ConcurrentSkipListMap q = map5();
512     Object e1 = q.ceilingKey(three);
513     assertEquals(three, e1);
514    
515     Object e2 = q.ceilingKey(zero);
516     assertEquals(one, e2);
517    
518     Object e3 = q.ceilingKey(five);
519     assertEquals(five, e3);
520    
521     Object e4 = q.ceilingKey(six);
522     assertNull(e4);
523    
524     }
525    
526     /**
527     * pollFirstEntry returns entries in order
528     */
529     public void testPollFirstEntry() {
530     ConcurrentSkipListMap map = map5();
531     Map.Entry e = map.pollFirstEntry();
532     assertEquals(one, e.getKey());
533     assertEquals("A", e.getValue());
534     e = map.pollFirstEntry();
535     assertEquals(two, e.getKey());
536     map.put(one, "A");
537     e = map.pollFirstEntry();
538     assertEquals(one, e.getKey());
539     assertEquals("A", e.getValue());
540     e = map.pollFirstEntry();
541     assertEquals(three, e.getKey());
542     map.remove(four);
543     e = map.pollFirstEntry();
544     assertEquals(five, e.getKey());
545     try {
546     e.setValue("A");
547     shouldThrow();
548     } catch (Exception ok) {
549     }
550     e = map.pollFirstEntry();
551     assertNull(e);
552     }
553    
554     /**
555     * pollLastEntry returns entries in order
556     */
557     public void testPollLastEntry() {
558     ConcurrentSkipListMap map = map5();
559     Map.Entry e = map.pollLastEntry();
560     assertEquals(five, e.getKey());
561     assertEquals("E", e.getValue());
562     e = map.pollLastEntry();
563     assertEquals(four, e.getKey());
564     map.put(five, "E");
565     e = map.pollLastEntry();
566     assertEquals(five, e.getKey());
567     assertEquals("E", e.getValue());
568     e = map.pollLastEntry();
569     assertEquals(three, e.getKey());
570     map.remove(two);
571     e = map.pollLastEntry();
572     assertEquals(one, e.getKey());
573     try {
574     e.setValue("E");
575     shouldThrow();
576     } catch (Exception ok) {
577     }
578     e = map.pollLastEntry();
579     assertNull(e);
580     }
581    
582     /**
583     * size returns the correct values
584     */
585     public void testSize() {
586     ConcurrentSkipListMap map = map5();
587     ConcurrentSkipListMap empty = new ConcurrentSkipListMap();
588     assertEquals(0, empty.size());
589     assertEquals(5, map.size());
590     }
591    
592     /**
593     * toString contains toString of elements
594     */
595     public void testToString() {
596     ConcurrentSkipListMap map = map5();
597     String s = map.toString();
598     for (int i = 1; i <= 5; ++i) {
599     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
600     }
601     }
602    
603     // Exception tests
604    
605     /**
606     * get(null) of nonempty map throws NPE
607     */
608     public void testGet_NullPointerException() {
609     try {
610     ConcurrentSkipListMap c = map5();
611     c.get(null);
612     shouldThrow();
613     } catch(NullPointerException e){}
614     }
615    
616     /**
617     * containsKey(null) of nonempty map throws NPE
618     */
619     public void testContainsKey_NullPointerException() {
620     try {
621     ConcurrentSkipListMap c = map5();
622     c.containsKey(null);
623     shouldThrow();
624     } catch(NullPointerException e){}
625     }
626    
627     /**
628     * containsValue(null) throws NPE
629     */
630     public void testContainsValue_NullPointerException() {
631     try {
632     ConcurrentSkipListMap c = new ConcurrentSkipListMap();
633     c.containsValue(null);
634     shouldThrow();
635     } catch(NullPointerException e){}
636     }
637    
638    
639     /**
640     * put(null,x) throws NPE
641     */
642     public void testPut1_NullPointerException() {
643     try {
644     ConcurrentSkipListMap c = map5();
645     c.put(null, "whatever");
646     shouldThrow();
647     } catch(NullPointerException e){}
648     }
649    
650     /**
651     * putIfAbsent(null, x) throws NPE
652     */
653     public void testPutIfAbsent1_NullPointerException() {
654     try {
655     ConcurrentSkipListMap c = map5();
656     c.putIfAbsent(null, "whatever");
657     shouldThrow();
658     } catch(NullPointerException e){}
659     }
660    
661     /**
662     * replace(null, x) throws NPE
663     */
664     public void testReplace_NullPointerException() {
665     try {
666     ConcurrentSkipListMap c = map5();
667     c.replace(null, "whatever");
668     shouldThrow();
669     } catch(NullPointerException e){}
670     }
671    
672     /**
673     * replace(null, x, y) throws NPE
674     */
675     public void testReplaceValue_NullPointerException() {
676     try {
677     ConcurrentSkipListMap c = map5();
678     c.replace(null, one, "whatever");
679     shouldThrow();
680     } catch(NullPointerException e){}
681     }
682    
683     /**
684     * remove(null) throws NPE
685     */
686     public void testRemove1_NullPointerException() {
687     try {
688     ConcurrentSkipListMap c = new ConcurrentSkipListMap();
689     c.put("sadsdf", "asdads");
690     c.remove(null);
691     shouldThrow();
692     } catch(NullPointerException e){}
693     }
694    
695     /**
696     * remove(null, x) throws NPE
697     */
698     public void testRemove2_NullPointerException() {
699     try {
700     ConcurrentSkipListMap c = new ConcurrentSkipListMap();
701     c.put("sadsdf", "asdads");
702     c.remove(null, "whatever");
703     shouldThrow();
704     } catch(NullPointerException e){}
705     }
706    
707     /**
708     * A deserialized map equals original
709     */
710     public void testSerialization() {
711     ConcurrentSkipListMap q = map5();
712    
713     try {
714     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
715     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
716     out.writeObject(q);
717     out.close();
718    
719     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
720     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
721     ConcurrentSkipListMap r = (ConcurrentSkipListMap)in.readObject();
722     assertEquals(q.size(), r.size());
723     assertTrue(q.equals(r));
724     assertTrue(r.equals(q));
725     } catch(Exception e){
726     e.printStackTrace();
727     unexpectedException();
728     }
729     }
730    
731    
732    
733     /**
734     * subMap returns map with keys in requested range
735     */
736     public void testSubMapContents() {
737     ConcurrentSkipListMap map = map5();
738     NavigableMap sm = map.subMap(two, four);
739     assertEquals(two, sm.firstKey());
740     assertEquals(three, sm.lastKey());
741     assertEquals(2, sm.size());
742     assertFalse(sm.containsKey(one));
743     assertTrue(sm.containsKey(two));
744     assertTrue(sm.containsKey(three));
745     assertFalse(sm.containsKey(four));
746     assertFalse(sm.containsKey(five));
747     Iterator i = sm.keySet().iterator();
748     Object k;
749     k = (Integer)(i.next());
750     assertEquals(two, k);
751     k = (Integer)(i.next());
752     assertEquals(three, k);
753     assertFalse(i.hasNext());
754     Iterator r = sm.descendingKeySet().iterator();
755     k = (Integer)(r.next());
756     assertEquals(three, k);
757     k = (Integer)(r.next());
758     assertEquals(two, k);
759     assertFalse(r.hasNext());
760    
761     Iterator j = sm.keySet().iterator();
762     j.next();
763     j.remove();
764     assertFalse(map.containsKey(two));
765     assertEquals(4, map.size());
766     assertEquals(1, sm.size());
767     assertEquals(three, sm.firstKey());
768     assertEquals(three, sm.lastKey());
769     assertTrue(sm.remove(three) != null);
770     assertTrue(sm.isEmpty());
771     assertEquals(3, map.size());
772     }
773    
774     public void testSubMapContents2() {
775     ConcurrentSkipListMap map = map5();
776     NavigableMap sm = map.subMap(two, three);
777     assertEquals(1, sm.size());
778     assertEquals(two, sm.firstKey());
779     assertEquals(two, sm.lastKey());
780     assertFalse(sm.containsKey(one));
781     assertTrue(sm.containsKey(two));
782     assertFalse(sm.containsKey(three));
783     assertFalse(sm.containsKey(four));
784     assertFalse(sm.containsKey(five));
785     Iterator i = sm.keySet().iterator();
786     Object k;
787     k = (Integer)(i.next());
788     assertEquals(two, k);
789     assertFalse(i.hasNext());
790     Iterator r = sm.descendingKeySet().iterator();
791     k = (Integer)(r.next());
792     assertEquals(two, k);
793     assertFalse(r.hasNext());
794    
795     Iterator j = sm.keySet().iterator();
796     j.next();
797     j.remove();
798     assertFalse(map.containsKey(two));
799     assertEquals(4, map.size());
800     assertEquals(0, sm.size());
801     assertTrue(sm.isEmpty());
802     assertTrue(sm.remove(three) == null);
803     assertEquals(4, map.size());
804     }
805    
806     /**
807     * headMap returns map with keys in requested range
808     */
809     public void testHeadMapContents() {
810     ConcurrentSkipListMap map = map5();
811     NavigableMap sm = map.headMap(four);
812     assertTrue(sm.containsKey(one));
813     assertTrue(sm.containsKey(two));
814     assertTrue(sm.containsKey(three));
815     assertFalse(sm.containsKey(four));
816     assertFalse(sm.containsKey(five));
817     Iterator i = sm.keySet().iterator();
818     Object k;
819     k = (Integer)(i.next());
820     assertEquals(one, k);
821     k = (Integer)(i.next());
822     assertEquals(two, k);
823     k = (Integer)(i.next());
824     assertEquals(three, k);
825     assertFalse(i.hasNext());
826     sm.clear();
827     assertTrue(sm.isEmpty());
828     assertEquals(2, map.size());
829     assertEquals(four, map.firstKey());
830     }
831    
832     /**
833     * headMap returns map with keys in requested range
834     */
835     public void testTailMapContents() {
836     ConcurrentSkipListMap map = map5();
837     NavigableMap sm = map.tailMap(two);
838     assertFalse(sm.containsKey(one));
839     assertTrue(sm.containsKey(two));
840     assertTrue(sm.containsKey(three));
841     assertTrue(sm.containsKey(four));
842     assertTrue(sm.containsKey(five));
843     Iterator i = sm.keySet().iterator();
844     Object k;
845     k = (Integer)(i.next());
846     assertEquals(two, k);
847     k = (Integer)(i.next());
848     assertEquals(three, k);
849     k = (Integer)(i.next());
850     assertEquals(four, k);
851     k = (Integer)(i.next());
852     assertEquals(five, k);
853     assertFalse(i.hasNext());
854     Iterator r = sm.descendingKeySet().iterator();
855     k = (Integer)(r.next());
856     assertEquals(five, k);
857     k = (Integer)(r.next());
858     assertEquals(four, k);
859     k = (Integer)(r.next());
860     assertEquals(three, k);
861     k = (Integer)(r.next());
862     assertEquals(two, k);
863     assertFalse(r.hasNext());
864    
865     Iterator ei = sm.entrySet().iterator();
866     Map.Entry e;
867     e = (Map.Entry)(ei.next());
868     assertEquals(two, e.getKey());
869     assertEquals("B", e.getValue());
870     e = (Map.Entry)(ei.next());
871     assertEquals(three, e.getKey());
872     assertEquals("C", e.getValue());
873     e = (Map.Entry)(ei.next());
874     assertEquals(four, e.getKey());
875     assertEquals("D", e.getValue());
876     e = (Map.Entry)(ei.next());
877     assertEquals(five, e.getKey());
878     assertEquals("E", e.getValue());
879     assertFalse(i.hasNext());
880    
881     NavigableMap ssm = sm.tailMap(four);
882     assertEquals(four, ssm.firstKey());
883     assertEquals(five, ssm.lastKey());
884     assertTrue(ssm.remove(four) != null);
885     assertEquals(1, ssm.size());
886     assertEquals(3, sm.size());
887     assertEquals(4, map.size());
888     }
889    
890     }