ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ConcurrentSkipListSubMapTest.java
Revision: 1.6
Committed: Mon Nov 2 20:28:31 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +10 -10 lines
Log Message:
whitespace

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 jsr166 1.6 junit.textui.TestRunner.run (suite());
15 dl 1.1 }
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 jsr166 1.6 private static ConcurrentNavigableMap map5() {
24 dl 1.1 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 dl 1.5 return map.subMap(one, true, seven, false);
36     }
37    
38     /**
39     * Create a map from Integers -5 to -1 to Strings "A"-"E".
40     */
41 jsr166 1.6 private static ConcurrentNavigableMap dmap5() {
42 dl 1.5 ConcurrentSkipListMap map = new ConcurrentSkipListMap();
43     assertTrue(map.isEmpty());
44     map.put(m1, "A");
45     map.put(m5, "E");
46     map.put(m3, "C");
47     map.put(m2, "B");
48     map.put(m4, "D");
49     assertFalse(map.isEmpty());
50     assertEquals(5, map.size());
51     return map.descendingMap();
52 dl 1.1 }
53    
54 jsr166 1.6 private static ConcurrentNavigableMap map0() {
55 dl 1.1 ConcurrentSkipListMap map = new ConcurrentSkipListMap();
56     assertTrue(map.isEmpty());
57 dl 1.5 return map.tailMap(one, true);
58     }
59    
60 jsr166 1.6 private static ConcurrentNavigableMap dmap0() {
61 dl 1.5 ConcurrentSkipListMap map = new ConcurrentSkipListMap();
62     assertTrue(map.isEmpty());
63     return map;
64 dl 1.1 }
65    
66     /**
67     * clear removes all pairs
68     */
69     public void testClear() {
70     ConcurrentNavigableMap map = map5();
71     map.clear();
72     assertEquals(map.size(), 0);
73     }
74    
75    
76     /**
77     * Maps with same contents are equal
78     */
79     public void testEquals() {
80     ConcurrentNavigableMap map1 = map5();
81     ConcurrentNavigableMap map2 = map5();
82     assertEquals(map1, map2);
83     assertEquals(map2, map1);
84     map1.clear();
85     assertFalse(map1.equals(map2));
86     assertFalse(map2.equals(map1));
87     }
88    
89     /**
90     * containsKey returns true for contained key
91     */
92     public void testContainsKey() {
93     ConcurrentNavigableMap map = map5();
94     assertTrue(map.containsKey(one));
95     assertFalse(map.containsKey(zero));
96     }
97    
98     /**
99     * containsValue returns true for held values
100     */
101     public void testContainsValue() {
102     ConcurrentNavigableMap map = map5();
103     assertTrue(map.containsValue("A"));
104     assertFalse(map.containsValue("Z"));
105     }
106    
107     /**
108     * get returns the correct element at the given key,
109     * or null if not present
110     */
111     public void testGet() {
112     ConcurrentNavigableMap map = map5();
113     assertEquals("A", (String)map.get(one));
114     ConcurrentNavigableMap empty = map0();
115     assertNull(empty.get(one));
116     }
117    
118     /**
119     * isEmpty is true of empty map and false for non-empty
120     */
121     public void testIsEmpty() {
122     ConcurrentNavigableMap empty = map0();
123     ConcurrentNavigableMap map = map5();
124     assertTrue(empty.isEmpty());
125     assertFalse(map.isEmpty());
126     }
127    
128     /**
129     * firstKey returns first key
130     */
131     public void testFirstKey() {
132     ConcurrentNavigableMap map = map5();
133     assertEquals(one, map.firstKey());
134     }
135    
136     /**
137     * lastKey returns last key
138     */
139     public void testLastKey() {
140     ConcurrentNavigableMap map = map5();
141     assertEquals(five, map.lastKey());
142     }
143    
144    
145     /**
146     * keySet returns a Set containing all the keys
147     */
148     public void testKeySet() {
149     ConcurrentNavigableMap map = map5();
150     Set s = map.keySet();
151     assertEquals(5, s.size());
152     assertTrue(s.contains(one));
153     assertTrue(s.contains(two));
154     assertTrue(s.contains(three));
155     assertTrue(s.contains(four));
156     assertTrue(s.contains(five));
157     }
158    
159     /**
160     * keySet is ordered
161     */
162     public void testKeySetOrder() {
163     ConcurrentNavigableMap map = map5();
164     Set s = map.keySet();
165     Iterator i = s.iterator();
166     Integer last = (Integer)i.next();
167     assertEquals(last, one);
168     while (i.hasNext()) {
169     Integer k = (Integer)i.next();
170     assertTrue(last.compareTo(k) < 0);
171     last = k;
172     }
173     }
174    
175     /**
176     * values collection contains all values
177     */
178     public void testValues() {
179     ConcurrentNavigableMap map = map5();
180     Collection s = map.values();
181     assertEquals(5, s.size());
182     assertTrue(s.contains("A"));
183     assertTrue(s.contains("B"));
184     assertTrue(s.contains("C"));
185     assertTrue(s.contains("D"));
186     assertTrue(s.contains("E"));
187     }
188    
189     /**
190 dl 1.3 * keySet.toArray returns contains all keys
191     */
192     public void testKeySetToArray() {
193     ConcurrentNavigableMap map = map5();
194     Set s = map.keySet();
195     Object[] ar = s.toArray();
196     assertTrue(s.containsAll(Arrays.asList(ar)));
197     assertEquals(5, ar.length);
198     ar[0] = m10;
199     assertFalse(s.containsAll(Arrays.asList(ar)));
200     }
201    
202     /**
203     * descendingkeySet.toArray returns contains all keys
204     */
205     public void testDescendingKeySetToArray() {
206     ConcurrentNavigableMap map = map5();
207     Set s = map.descendingKeySet();
208     Object[] ar = s.toArray();
209     assertEquals(5, ar.length);
210     assertTrue(s.containsAll(Arrays.asList(ar)));
211     ar[0] = m10;
212     assertFalse(s.containsAll(Arrays.asList(ar)));
213     }
214    
215     /**
216     * Values.toArray contains all values
217     */
218     public void testValuesToArray() {
219     ConcurrentNavigableMap map = map5();
220     Collection v = map.values();
221     Object[] ar = v.toArray();
222     ArrayList s = new ArrayList(Arrays.asList(ar));
223     assertEquals(5, ar.length);
224     assertTrue(s.contains("A"));
225     assertTrue(s.contains("B"));
226     assertTrue(s.contains("C"));
227     assertTrue(s.contains("D"));
228     assertTrue(s.contains("E"));
229     }
230    
231    
232     /**
233 dl 1.1 * entrySet contains all pairs
234     */
235     public void testEntrySet() {
236     ConcurrentNavigableMap map = map5();
237     Set s = map.entrySet();
238     assertEquals(5, s.size());
239     Iterator it = s.iterator();
240     while (it.hasNext()) {
241     Map.Entry e = (Map.Entry) it.next();
242 jsr166 1.6 assertTrue(
243 dl 1.1 (e.getKey().equals(one) && e.getValue().equals("A")) ||
244     (e.getKey().equals(two) && e.getValue().equals("B")) ||
245     (e.getKey().equals(three) && e.getValue().equals("C")) ||
246     (e.getKey().equals(four) && e.getValue().equals("D")) ||
247     (e.getKey().equals(five) && e.getValue().equals("E")));
248     }
249     }
250    
251     /**
252     * putAll adds all key-value pairs from the given map
253     */
254     public void testPutAll() {
255     ConcurrentNavigableMap empty = map0();
256     ConcurrentNavigableMap map = map5();
257     empty.putAll(map);
258     assertEquals(5, empty.size());
259     assertTrue(empty.containsKey(one));
260     assertTrue(empty.containsKey(two));
261     assertTrue(empty.containsKey(three));
262     assertTrue(empty.containsKey(four));
263     assertTrue(empty.containsKey(five));
264     }
265    
266     /**
267     * putIfAbsent works when the given key is not present
268     */
269     public void testPutIfAbsent() {
270     ConcurrentNavigableMap map = map5();
271     map.putIfAbsent(six, "Z");
272     assertTrue(map.containsKey(six));
273     }
274    
275     /**
276     * putIfAbsent does not add the pair if the key is already present
277     */
278     public void testPutIfAbsent2() {
279     ConcurrentNavigableMap map = map5();
280     assertEquals("A", map.putIfAbsent(one, "Z"));
281     }
282    
283     /**
284     * replace fails when the given key is not present
285     */
286     public void testReplace() {
287     ConcurrentNavigableMap map = map5();
288     assertNull(map.replace(six, "Z"));
289     assertFalse(map.containsKey(six));
290     }
291    
292     /**
293     * replace succeeds if the key is already present
294     */
295     public void testReplace2() {
296     ConcurrentNavigableMap map = map5();
297     assertNotNull(map.replace(one, "Z"));
298     assertEquals("Z", map.get(one));
299     }
300    
301    
302     /**
303     * replace value fails when the given key not mapped to expected value
304     */
305     public void testReplaceValue() {
306     ConcurrentNavigableMap map = map5();
307     assertEquals("A", map.get(one));
308     assertFalse(map.replace(one, "Z", "Z"));
309     assertEquals("A", map.get(one));
310     }
311    
312     /**
313     * replace value succeeds when the given key mapped to expected value
314     */
315     public void testReplaceValue2() {
316     ConcurrentNavigableMap map = map5();
317     assertEquals("A", map.get(one));
318     assertTrue(map.replace(one, "A", "Z"));
319     assertEquals("Z", map.get(one));
320     }
321    
322    
323     /**
324     * remove removes the correct key-value pair from the map
325     */
326     public void testRemove() {
327     ConcurrentNavigableMap map = map5();
328     map.remove(five);
329     assertEquals(4, map.size());
330     assertFalse(map.containsKey(five));
331     }
332    
333     /**
334     * remove(key,value) removes only if pair present
335     */
336     public void testRemove2() {
337     ConcurrentNavigableMap map = map5();
338     assertTrue(map.containsKey(five));
339     assertEquals("E", map.get(five));
340     map.remove(five, "E");
341     assertEquals(4, map.size());
342     assertFalse(map.containsKey(five));
343     map.remove(four, "A");
344     assertEquals(4, map.size());
345     assertTrue(map.containsKey(four));
346    
347     }
348    
349     /**
350     * lowerEntry returns preceding entry.
351     */
352     public void testLowerEntry() {
353     ConcurrentNavigableMap map = map5();
354     Map.Entry e1 = map.lowerEntry(three);
355     assertEquals(two, e1.getKey());
356    
357     Map.Entry e2 = map.lowerEntry(six);
358     assertEquals(five, e2.getKey());
359    
360     Map.Entry e3 = map.lowerEntry(one);
361     assertNull(e3);
362    
363     Map.Entry e4 = map.lowerEntry(zero);
364     assertNull(e4);
365    
366     }
367    
368     /**
369     * higherEntry returns next entry.
370     */
371     public void testHigherEntry() {
372     ConcurrentNavigableMap map = map5();
373     Map.Entry e1 = map.higherEntry(three);
374     assertEquals(four, e1.getKey());
375    
376     Map.Entry e2 = map.higherEntry(zero);
377     assertEquals(one, e2.getKey());
378    
379     Map.Entry e3 = map.higherEntry(five);
380     assertNull(e3);
381    
382     Map.Entry e4 = map.higherEntry(six);
383     assertNull(e4);
384    
385     }
386    
387     /**
388     * floorEntry returns preceding entry.
389     */
390     public void testFloorEntry() {
391     ConcurrentNavigableMap map = map5();
392     Map.Entry e1 = map.floorEntry(three);
393     assertEquals(three, e1.getKey());
394    
395     Map.Entry e2 = map.floorEntry(six);
396     assertEquals(five, e2.getKey());
397    
398     Map.Entry e3 = map.floorEntry(one);
399     assertEquals(one, e3.getKey());
400    
401     Map.Entry e4 = map.floorEntry(zero);
402     assertNull(e4);
403    
404     }
405    
406     /**
407     * ceilingEntry returns next entry.
408     */
409     public void testCeilingEntry() {
410     ConcurrentNavigableMap map = map5();
411     Map.Entry e1 = map.ceilingEntry(three);
412     assertEquals(three, e1.getKey());
413    
414     Map.Entry e2 = map.ceilingEntry(zero);
415     assertEquals(one, e2.getKey());
416    
417     Map.Entry e3 = map.ceilingEntry(five);
418     assertEquals(five, e3.getKey());
419    
420     Map.Entry e4 = map.ceilingEntry(six);
421     assertNull(e4);
422    
423     }
424    
425     /**
426     * pollFirstEntry returns entries in order
427     */
428     public void testPollFirstEntry() {
429     ConcurrentNavigableMap map = map5();
430     Map.Entry e = map.pollFirstEntry();
431     assertEquals(one, e.getKey());
432     assertEquals("A", e.getValue());
433     e = map.pollFirstEntry();
434     assertEquals(two, e.getKey());
435     map.put(one, "A");
436     e = map.pollFirstEntry();
437     assertEquals(one, e.getKey());
438     assertEquals("A", e.getValue());
439     e = map.pollFirstEntry();
440     assertEquals(three, e.getKey());
441     map.remove(four);
442     e = map.pollFirstEntry();
443     assertEquals(five, e.getKey());
444     try {
445     e.setValue("A");
446     shouldThrow();
447     } catch (Exception ok) {
448     }
449     e = map.pollFirstEntry();
450     assertNull(e);
451     }
452    
453     /**
454     * pollLastEntry returns entries in order
455     */
456     public void testPollLastEntry() {
457     ConcurrentNavigableMap map = map5();
458     Map.Entry e = map.pollLastEntry();
459     assertEquals(five, e.getKey());
460     assertEquals("E", e.getValue());
461     e = map.pollLastEntry();
462     assertEquals(four, e.getKey());
463     map.put(five, "E");
464     e = map.pollLastEntry();
465     assertEquals(five, e.getKey());
466     assertEquals("E", e.getValue());
467     e = map.pollLastEntry();
468     assertEquals(three, e.getKey());
469     map.remove(two);
470     e = map.pollLastEntry();
471     assertEquals(one, e.getKey());
472     try {
473     e.setValue("E");
474     shouldThrow();
475     } catch (Exception ok) {
476     }
477     e = map.pollLastEntry();
478     assertNull(e);
479     }
480    
481     /**
482     * size returns the correct values
483     */
484     public void testSize() {
485     ConcurrentNavigableMap map = map5();
486     ConcurrentNavigableMap empty = map0();
487     assertEquals(0, empty.size());
488     assertEquals(5, map.size());
489     }
490    
491     /**
492     * toString contains toString of elements
493     */
494     public void testToString() {
495     ConcurrentNavigableMap map = map5();
496     String s = map.toString();
497     for (int i = 1; i <= 5; ++i) {
498     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
499     }
500 jsr166 1.6 }
501 dl 1.1
502     // Exception tests
503    
504     /**
505     * get(null) of nonempty map throws NPE
506     */
507     public void testGet_NullPointerException() {
508     try {
509     ConcurrentNavigableMap c = map5();
510     c.get(null);
511     shouldThrow();
512     } catch(NullPointerException e){}
513     }
514    
515     /**
516     * containsKey(null) of nonempty map throws NPE
517     */
518     public void testContainsKey_NullPointerException() {
519     try {
520     ConcurrentNavigableMap c = map5();
521     c.containsKey(null);
522     shouldThrow();
523     } catch(NullPointerException e){}
524     }
525    
526     /**
527     * containsValue(null) throws NPE
528     */
529     public void testContainsValue_NullPointerException() {
530     try {
531     ConcurrentNavigableMap c = map0();
532     c.containsValue(null);
533     shouldThrow();
534     } catch(NullPointerException e){}
535     }
536    
537    
538     /**
539     * put(null,x) throws NPE
540     */
541     public void testPut1_NullPointerException() {
542     try {
543     ConcurrentNavigableMap c = map5();
544     c.put(null, "whatever");
545     shouldThrow();
546     } catch(NullPointerException e){}
547     }
548    
549     /**
550     * putIfAbsent(null, x) throws NPE
551     */
552     public void testPutIfAbsent1_NullPointerException() {
553     try {
554     ConcurrentNavigableMap c = map5();
555     c.putIfAbsent(null, "whatever");
556     shouldThrow();
557     } catch(NullPointerException e){}
558     }
559    
560     /**
561     * replace(null, x) throws NPE
562     */
563     public void testReplace_NullPointerException() {
564     try {
565     ConcurrentNavigableMap c = map5();
566     c.replace(null, "whatever");
567     shouldThrow();
568     } catch(NullPointerException e){}
569     }
570    
571     /**
572     * replace(null, x, y) throws NPE
573     */
574     public void testReplaceValue_NullPointerException() {
575     try {
576     ConcurrentNavigableMap c = map5();
577     c.replace(null, one, "whatever");
578     shouldThrow();
579     } catch(NullPointerException e){}
580     }
581    
582     /**
583     * remove(null) throws NPE
584     */
585     public void testRemove1_NullPointerException() {
586     try {
587     ConcurrentNavigableMap c = map5();
588     c.remove(null);
589     shouldThrow();
590     } catch(NullPointerException e){}
591     }
592    
593     /**
594     * remove(null, x) throws NPE
595     */
596     public void testRemove2_NullPointerException() {
597     try {
598     ConcurrentNavigableMap c = map5();
599     c.remove(null, "whatever");
600     shouldThrow();
601     } catch(NullPointerException e){}
602     }
603    
604     /**
605     * A deserialized map equals original
606     */
607     public void testSerialization() {
608     ConcurrentNavigableMap q = map5();
609    
610     try {
611     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
612     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
613     out.writeObject(q);
614     out.close();
615    
616     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
617     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
618     ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
619     assertEquals(q.size(), r.size());
620     assertTrue(q.equals(r));
621     assertTrue(r.equals(q));
622     } catch(Exception e){
623     e.printStackTrace();
624     unexpectedException();
625     }
626     }
627    
628    
629    
630     /**
631     * subMap returns map with keys in requested range
632     */
633     public void testSubMapContents() {
634     ConcurrentNavigableMap map = map5();
635     SortedMap sm = map.subMap(two, four);
636     assertEquals(two, sm.firstKey());
637     assertEquals(three, sm.lastKey());
638     assertEquals(2, sm.size());
639     assertFalse(sm.containsKey(one));
640     assertTrue(sm.containsKey(two));
641     assertTrue(sm.containsKey(three));
642     assertFalse(sm.containsKey(four));
643     assertFalse(sm.containsKey(five));
644     Iterator i = sm.keySet().iterator();
645     Object k;
646     k = (Integer)(i.next());
647     assertEquals(two, k);
648     k = (Integer)(i.next());
649     assertEquals(three, k);
650     assertFalse(i.hasNext());
651     Iterator j = sm.keySet().iterator();
652     j.next();
653     j.remove();
654     assertFalse(map.containsKey(two));
655     assertEquals(4, map.size());
656     assertEquals(1, sm.size());
657     assertEquals(three, sm.firstKey());
658     assertEquals(three, sm.lastKey());
659     assertTrue(sm.remove(three) != null);
660     assertTrue(sm.isEmpty());
661     assertEquals(3, map.size());
662     }
663    
664     public void testSubMapContents2() {
665     ConcurrentNavigableMap map = map5();
666     SortedMap sm = map.subMap(two, three);
667     assertEquals(1, sm.size());
668     assertEquals(two, sm.firstKey());
669     assertEquals(two, sm.lastKey());
670     assertFalse(sm.containsKey(one));
671     assertTrue(sm.containsKey(two));
672     assertFalse(sm.containsKey(three));
673     assertFalse(sm.containsKey(four));
674     assertFalse(sm.containsKey(five));
675     Iterator i = sm.keySet().iterator();
676     Object k;
677     k = (Integer)(i.next());
678     assertEquals(two, k);
679     assertFalse(i.hasNext());
680     Iterator j = sm.keySet().iterator();
681     j.next();
682     j.remove();
683     assertFalse(map.containsKey(two));
684     assertEquals(4, map.size());
685     assertEquals(0, sm.size());
686     assertTrue(sm.isEmpty());
687     assertTrue(sm.remove(three) == null);
688     assertEquals(4, map.size());
689     }
690    
691     /**
692     * headMap returns map with keys in requested range
693     */
694     public void testHeadMapContents() {
695     ConcurrentNavigableMap map = map5();
696     SortedMap sm = map.headMap(four);
697     assertTrue(sm.containsKey(one));
698     assertTrue(sm.containsKey(two));
699     assertTrue(sm.containsKey(three));
700     assertFalse(sm.containsKey(four));
701     assertFalse(sm.containsKey(five));
702     Iterator i = sm.keySet().iterator();
703     Object k;
704     k = (Integer)(i.next());
705     assertEquals(one, k);
706     k = (Integer)(i.next());
707     assertEquals(two, k);
708     k = (Integer)(i.next());
709     assertEquals(three, k);
710     assertFalse(i.hasNext());
711     sm.clear();
712     assertTrue(sm.isEmpty());
713     assertEquals(2, map.size());
714     assertEquals(four, map.firstKey());
715     }
716    
717     /**
718     * headMap returns map with keys in requested range
719     */
720     public void testTailMapContents() {
721     ConcurrentNavigableMap map = map5();
722     SortedMap sm = map.tailMap(two);
723     assertFalse(sm.containsKey(one));
724     assertTrue(sm.containsKey(two));
725     assertTrue(sm.containsKey(three));
726     assertTrue(sm.containsKey(four));
727     assertTrue(sm.containsKey(five));
728     Iterator i = sm.keySet().iterator();
729     Object k;
730     k = (Integer)(i.next());
731     assertEquals(two, k);
732     k = (Integer)(i.next());
733     assertEquals(three, k);
734     k = (Integer)(i.next());
735     assertEquals(four, k);
736     k = (Integer)(i.next());
737     assertEquals(five, k);
738     assertFalse(i.hasNext());
739    
740     Iterator ei = sm.entrySet().iterator();
741     Map.Entry e;
742     e = (Map.Entry)(ei.next());
743     assertEquals(two, e.getKey());
744     assertEquals("B", e.getValue());
745     e = (Map.Entry)(ei.next());
746     assertEquals(three, e.getKey());
747     assertEquals("C", e.getValue());
748     e = (Map.Entry)(ei.next());
749     assertEquals(four, e.getKey());
750     assertEquals("D", e.getValue());
751     e = (Map.Entry)(ei.next());
752     assertEquals(five, e.getKey());
753     assertEquals("E", e.getValue());
754     assertFalse(i.hasNext());
755    
756     SortedMap ssm = sm.tailMap(four);
757     assertEquals(four, ssm.firstKey());
758     assertEquals(five, ssm.lastKey());
759     assertTrue(ssm.remove(four) != null);
760     assertEquals(1, ssm.size());
761     assertEquals(3, sm.size());
762     assertEquals(4, map.size());
763     }
764 dl 1.5
765     /**
766     * clear removes all pairs
767     */
768     public void testDescendingClear() {
769     ConcurrentNavigableMap map = dmap5();
770     map.clear();
771     assertEquals(map.size(), 0);
772     }
773    
774    
775     /**
776     * Maps with same contents are equal
777     */
778     public void testDescendingEquals() {
779     ConcurrentNavigableMap map1 = dmap5();
780     ConcurrentNavigableMap map2 = dmap5();
781     assertEquals(map1, map2);
782     assertEquals(map2, map1);
783     map1.clear();
784     assertFalse(map1.equals(map2));
785     assertFalse(map2.equals(map1));
786     }
787    
788     /**
789     * containsKey returns true for contained key
790     */
791     public void testDescendingContainsKey() {
792     ConcurrentNavigableMap map = dmap5();
793     assertTrue(map.containsKey(m1));
794     assertFalse(map.containsKey(zero));
795     }
796    
797     /**
798     * containsValue returns true for held values
799     */
800     public void testDescendingContainsValue() {
801     ConcurrentNavigableMap map = dmap5();
802     assertTrue(map.containsValue("A"));
803     assertFalse(map.containsValue("Z"));
804     }
805    
806     /**
807     * get returns the correct element at the given key,
808     * or null if not present
809     */
810     public void testDescendingGet() {
811     ConcurrentNavigableMap map = dmap5();
812     assertEquals("A", (String)map.get(m1));
813     ConcurrentNavigableMap empty = dmap0();
814     assertNull(empty.get(m1));
815     }
816    
817     /**
818     * isEmpty is true of empty map and false for non-empty
819     */
820     public void testDescendingIsEmpty() {
821     ConcurrentNavigableMap empty = dmap0();
822     ConcurrentNavigableMap map = dmap5();
823     assertTrue(empty.isEmpty());
824     assertFalse(map.isEmpty());
825     }
826    
827     /**
828     * firstKey returns first key
829     */
830     public void testDescendingFirstKey() {
831     ConcurrentNavigableMap map = dmap5();
832     assertEquals(m1, map.firstKey());
833     }
834    
835     /**
836     * lastKey returns last key
837     */
838     public void testDescendingLastKey() {
839     ConcurrentNavigableMap map = dmap5();
840     assertEquals(m5, map.lastKey());
841     }
842    
843    
844     /**
845     * keySet returns a Set containing all the keys
846     */
847     public void testDescendingKeySet() {
848     ConcurrentNavigableMap map = dmap5();
849     Set s = map.keySet();
850     assertEquals(5, s.size());
851     assertTrue(s.contains(m1));
852     assertTrue(s.contains(m2));
853     assertTrue(s.contains(m3));
854     assertTrue(s.contains(m4));
855     assertTrue(s.contains(m5));
856     }
857    
858     /**
859     * keySet is ordered
860     */
861     public void testDescendingKeySetOrder() {
862     ConcurrentNavigableMap map = dmap5();
863     Set s = map.keySet();
864     Iterator i = s.iterator();
865     Integer last = (Integer)i.next();
866     assertEquals(last, m1);
867     while (i.hasNext()) {
868     Integer k = (Integer)i.next();
869     assertTrue(last.compareTo(k) > 0);
870     last = k;
871     }
872     }
873    
874     /**
875     * values collection contains all values
876     */
877     public void testDescendingValues() {
878     ConcurrentNavigableMap map = dmap5();
879     Collection s = map.values();
880     assertEquals(5, s.size());
881     assertTrue(s.contains("A"));
882     assertTrue(s.contains("B"));
883     assertTrue(s.contains("C"));
884     assertTrue(s.contains("D"));
885     assertTrue(s.contains("E"));
886     }
887    
888     /**
889     * keySet.toArray returns contains all keys
890     */
891     public void testDescendingAscendingKeySetToArray() {
892     ConcurrentNavigableMap map = dmap5();
893     Set s = map.keySet();
894     Object[] ar = s.toArray();
895     assertTrue(s.containsAll(Arrays.asList(ar)));
896     assertEquals(5, ar.length);
897     ar[0] = m10;
898     assertFalse(s.containsAll(Arrays.asList(ar)));
899     }
900    
901     /**
902     * descendingkeySet.toArray returns contains all keys
903     */
904     public void testDescendingDescendingKeySetToArray() {
905     ConcurrentNavigableMap map = dmap5();
906     Set s = map.descendingKeySet();
907     Object[] ar = s.toArray();
908     assertEquals(5, ar.length);
909     assertTrue(s.containsAll(Arrays.asList(ar)));
910     ar[0] = m10;
911     assertFalse(s.containsAll(Arrays.asList(ar)));
912     }
913    
914     /**
915     * Values.toArray contains all values
916     */
917     public void testDescendingValuesToArray() {
918     ConcurrentNavigableMap map = dmap5();
919     Collection v = map.values();
920     Object[] ar = v.toArray();
921     ArrayList s = new ArrayList(Arrays.asList(ar));
922     assertEquals(5, ar.length);
923     assertTrue(s.contains("A"));
924     assertTrue(s.contains("B"));
925     assertTrue(s.contains("C"));
926     assertTrue(s.contains("D"));
927     assertTrue(s.contains("E"));
928     }
929    
930    
931     /**
932     * entrySet contains all pairs
933     */
934     public void testDescendingEntrySet() {
935     ConcurrentNavigableMap map = dmap5();
936     Set s = map.entrySet();
937     assertEquals(5, s.size());
938     Iterator it = s.iterator();
939     while (it.hasNext()) {
940     Map.Entry e = (Map.Entry) it.next();
941 jsr166 1.6 assertTrue(
942 dl 1.5 (e.getKey().equals(m1) && e.getValue().equals("A")) ||
943     (e.getKey().equals(m2) && e.getValue().equals("B")) ||
944     (e.getKey().equals(m3) && e.getValue().equals("C")) ||
945     (e.getKey().equals(m4) && e.getValue().equals("D")) ||
946     (e.getKey().equals(m5) && e.getValue().equals("E")));
947     }
948     }
949    
950     /**
951     * putAll adds all key-value pairs from the given map
952     */
953     public void testDescendingPutAll() {
954     ConcurrentNavigableMap empty = dmap0();
955     ConcurrentNavigableMap map = dmap5();
956     empty.putAll(map);
957     assertEquals(5, empty.size());
958     assertTrue(empty.containsKey(m1));
959     assertTrue(empty.containsKey(m2));
960     assertTrue(empty.containsKey(m3));
961     assertTrue(empty.containsKey(m4));
962     assertTrue(empty.containsKey(m5));
963     }
964    
965     /**
966     * putIfAbsent works when the given key is not present
967     */
968     public void testDescendingPutIfAbsent() {
969     ConcurrentNavigableMap map = dmap5();
970     map.putIfAbsent(six, "Z");
971     assertTrue(map.containsKey(six));
972     }
973    
974     /**
975     * putIfAbsent does not add the pair if the key is already present
976     */
977     public void testDescendingPutIfAbsent2() {
978     ConcurrentNavigableMap map = dmap5();
979     assertEquals("A", map.putIfAbsent(m1, "Z"));
980     }
981    
982     /**
983     * replace fails when the given key is not present
984     */
985     public void testDescendingReplace() {
986     ConcurrentNavigableMap map = dmap5();
987     assertNull(map.replace(six, "Z"));
988     assertFalse(map.containsKey(six));
989     }
990    
991     /**
992     * replace succeeds if the key is already present
993     */
994     public void testDescendingReplace2() {
995     ConcurrentNavigableMap map = dmap5();
996     assertNotNull(map.replace(m1, "Z"));
997     assertEquals("Z", map.get(m1));
998     }
999    
1000    
1001     /**
1002     * replace value fails when the given key not mapped to expected value
1003     */
1004     public void testDescendingReplaceValue() {
1005     ConcurrentNavigableMap map = dmap5();
1006     assertEquals("A", map.get(m1));
1007     assertFalse(map.replace(m1, "Z", "Z"));
1008     assertEquals("A", map.get(m1));
1009     }
1010    
1011     /**
1012     * replace value succeeds when the given key mapped to expected value
1013     */
1014     public void testDescendingReplaceValue2() {
1015     ConcurrentNavigableMap map = dmap5();
1016     assertEquals("A", map.get(m1));
1017     assertTrue(map.replace(m1, "A", "Z"));
1018     assertEquals("Z", map.get(m1));
1019     }
1020    
1021    
1022     /**
1023     * remove removes the correct key-value pair from the map
1024     */
1025     public void testDescendingRemove() {
1026     ConcurrentNavigableMap map = dmap5();
1027     map.remove(m5);
1028     assertEquals(4, map.size());
1029     assertFalse(map.containsKey(m5));
1030     }
1031    
1032     /**
1033     * remove(key,value) removes only if pair present
1034     */
1035     public void testDescendingRemove2() {
1036     ConcurrentNavigableMap map = dmap5();
1037     assertTrue(map.containsKey(m5));
1038     assertEquals("E", map.get(m5));
1039     map.remove(m5, "E");
1040     assertEquals(4, map.size());
1041     assertFalse(map.containsKey(m5));
1042     map.remove(m4, "A");
1043     assertEquals(4, map.size());
1044     assertTrue(map.containsKey(m4));
1045    
1046     }
1047    
1048     /**
1049     * lowerEntry returns preceding entry.
1050     */
1051     public void testDescendingLowerEntry() {
1052     ConcurrentNavigableMap map = dmap5();
1053     Map.Entry e1 = map.lowerEntry(m3);
1054     assertEquals(m2, e1.getKey());
1055    
1056     Map.Entry e2 = map.lowerEntry(m6);
1057     assertEquals(m5, e2.getKey());
1058    
1059     Map.Entry e3 = map.lowerEntry(m1);
1060     assertNull(e3);
1061    
1062     Map.Entry e4 = map.lowerEntry(zero);
1063     assertNull(e4);
1064    
1065     }
1066    
1067     /**
1068     * higherEntry returns next entry.
1069     */
1070     public void testDescendingHigherEntry() {
1071     ConcurrentNavigableMap map = dmap5();
1072     Map.Entry e1 = map.higherEntry(m3);
1073     assertEquals(m4, e1.getKey());
1074    
1075     Map.Entry e2 = map.higherEntry(zero);
1076     assertEquals(m1, e2.getKey());
1077    
1078     Map.Entry e3 = map.higherEntry(m5);
1079     assertNull(e3);
1080    
1081     Map.Entry e4 = map.higherEntry(m6);
1082     assertNull(e4);
1083    
1084     }
1085    
1086     /**
1087     * floorEntry returns preceding entry.
1088     */
1089     public void testDescendingFloorEntry() {
1090     ConcurrentNavigableMap map = dmap5();
1091     Map.Entry e1 = map.floorEntry(m3);
1092     assertEquals(m3, e1.getKey());
1093    
1094     Map.Entry e2 = map.floorEntry(m6);
1095     assertEquals(m5, e2.getKey());
1096    
1097     Map.Entry e3 = map.floorEntry(m1);
1098     assertEquals(m1, e3.getKey());
1099    
1100     Map.Entry e4 = map.floorEntry(zero);
1101     assertNull(e4);
1102    
1103     }
1104    
1105     /**
1106     * ceilingEntry returns next entry.
1107     */
1108     public void testDescendingCeilingEntry() {
1109     ConcurrentNavigableMap map = dmap5();
1110     Map.Entry e1 = map.ceilingEntry(m3);
1111     assertEquals(m3, e1.getKey());
1112    
1113     Map.Entry e2 = map.ceilingEntry(zero);
1114     assertEquals(m1, e2.getKey());
1115    
1116     Map.Entry e3 = map.ceilingEntry(m5);
1117     assertEquals(m5, e3.getKey());
1118    
1119     Map.Entry e4 = map.ceilingEntry(m6);
1120     assertNull(e4);
1121    
1122     }
1123    
1124     /**
1125     * pollFirstEntry returns entries in order
1126     */
1127     public void testDescendingPollFirstEntry() {
1128     ConcurrentNavigableMap map = dmap5();
1129     Map.Entry e = map.pollFirstEntry();
1130     assertEquals(m1, e.getKey());
1131     assertEquals("A", e.getValue());
1132     e = map.pollFirstEntry();
1133     assertEquals(m2, e.getKey());
1134     map.put(m1, "A");
1135     e = map.pollFirstEntry();
1136     assertEquals(m1, e.getKey());
1137     assertEquals("A", e.getValue());
1138     e = map.pollFirstEntry();
1139     assertEquals(m3, e.getKey());
1140     map.remove(m4);
1141     e = map.pollFirstEntry();
1142     assertEquals(m5, e.getKey());
1143     try {
1144     e.setValue("A");
1145     shouldThrow();
1146     } catch (Exception ok) {
1147     }
1148     e = map.pollFirstEntry();
1149     assertNull(e);
1150     }
1151    
1152     /**
1153     * pollLastEntry returns entries in order
1154     */
1155     public void testDescendingPollLastEntry() {
1156     ConcurrentNavigableMap map = dmap5();
1157     Map.Entry e = map.pollLastEntry();
1158     assertEquals(m5, e.getKey());
1159     assertEquals("E", e.getValue());
1160     e = map.pollLastEntry();
1161     assertEquals(m4, e.getKey());
1162     map.put(m5, "E");
1163     e = map.pollLastEntry();
1164     assertEquals(m5, e.getKey());
1165     assertEquals("E", e.getValue());
1166     e = map.pollLastEntry();
1167     assertEquals(m3, e.getKey());
1168     map.remove(m2);
1169     e = map.pollLastEntry();
1170     assertEquals(m1, e.getKey());
1171     try {
1172     e.setValue("E");
1173     shouldThrow();
1174     } catch (Exception ok) {
1175     }
1176     e = map.pollLastEntry();
1177     assertNull(e);
1178     }
1179    
1180     /**
1181     * size returns the correct values
1182     */
1183     public void testDescendingSize() {
1184     ConcurrentNavigableMap map = dmap5();
1185     ConcurrentNavigableMap empty = dmap0();
1186     assertEquals(0, empty.size());
1187     assertEquals(5, map.size());
1188     }
1189    
1190     /**
1191     * toString contains toString of elements
1192     */
1193     public void testDescendingToString() {
1194     ConcurrentNavigableMap map = dmap5();
1195     String s = map.toString();
1196     for (int i = 1; i <= 5; ++i) {
1197     assertTrue(s.indexOf(String.valueOf(i)) >= 0);
1198     }
1199 jsr166 1.6 }
1200 dl 1.5
1201     // Exception testDescendings
1202    
1203     /**
1204     * get(null) of nm1mpty map throws NPE
1205     */
1206     public void testDescendingGet_NullPointerException() {
1207     try {
1208     ConcurrentNavigableMap c = dmap5();
1209     c.get(null);
1210     shouldThrow();
1211     } catch(NullPointerException e){}
1212     }
1213    
1214     /**
1215     * containsKey(null) of nm1mpty map throws NPE
1216     */
1217     public void testDescendingContainsKey_NullPointerException() {
1218     try {
1219     ConcurrentNavigableMap c = dmap5();
1220     c.containsKey(null);
1221     shouldThrow();
1222     } catch(NullPointerException e){}
1223     }
1224    
1225     /**
1226     * containsValue(null) throws NPE
1227     */
1228     public void testDescendingContainsValue_NullPointerException() {
1229     try {
1230     ConcurrentNavigableMap c = dmap0();
1231     c.containsValue(null);
1232     shouldThrow();
1233     } catch(NullPointerException e){}
1234     }
1235    
1236    
1237     /**
1238     * put(null,x) throws NPE
1239     */
1240     public void testDescendingPut1_NullPointerException() {
1241     try {
1242     ConcurrentNavigableMap c = dmap5();
1243     c.put(null, "whatever");
1244     shouldThrow();
1245     } catch(NullPointerException e){}
1246     }
1247    
1248     /**
1249     * putIfAbsent(null, x) throws NPE
1250     */
1251     public void testDescendingPutIfAbsent1_NullPointerException() {
1252     try {
1253     ConcurrentNavigableMap c = dmap5();
1254     c.putIfAbsent(null, "whatever");
1255     shouldThrow();
1256     } catch(NullPointerException e){}
1257     }
1258    
1259     /**
1260     * replace(null, x) throws NPE
1261     */
1262     public void testDescendingReplace_NullPointerException() {
1263     try {
1264     ConcurrentNavigableMap c = dmap5();
1265     c.replace(null, "whatever");
1266     shouldThrow();
1267     } catch(NullPointerException e){}
1268     }
1269    
1270     /**
1271     * replace(null, x, y) throws NPE
1272     */
1273     public void testDescendingReplaceValue_NullPointerException() {
1274     try {
1275     ConcurrentNavigableMap c = dmap5();
1276     c.replace(null, m1, "whatever");
1277     shouldThrow();
1278     } catch(NullPointerException e){}
1279     }
1280    
1281     /**
1282     * remove(null) throws NPE
1283     */
1284     public void testDescendingRemove1_NullPointerException() {
1285     try {
1286     ConcurrentNavigableMap c = dmap5();
1287     c.remove(null);
1288     shouldThrow();
1289     } catch(NullPointerException e){}
1290     }
1291    
1292     /**
1293     * remove(null, x) throws NPE
1294     */
1295     public void testDescendingRemove2_NullPointerException() {
1296     try {
1297     ConcurrentNavigableMap c = dmap5();
1298     c.remove(null, "whatever");
1299     shouldThrow();
1300     } catch(NullPointerException e){}
1301     }
1302    
1303     /**
1304     * A deserialized map equals original
1305     */
1306     public void testDescendingSerialization() {
1307     ConcurrentNavigableMap q = dmap5();
1308    
1309     try {
1310     ByteArrayOutputStream bout = new ByteArrayOutputStream(10000);
1311     ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout));
1312     out.writeObject(q);
1313     out.close();
1314    
1315     ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
1316     ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin));
1317     ConcurrentNavigableMap r = (ConcurrentNavigableMap)in.readObject();
1318     assertEquals(q.size(), r.size());
1319     assertTrue(q.equals(r));
1320     assertTrue(r.equals(q));
1321     } catch(Exception e){
1322     e.printStackTrace();
1323     unexpectedException();
1324     }
1325     }
1326    
1327    
1328    
1329     /**
1330     * subMap returns map with keys in requested range
1331     */
1332     public void testDescendingSubMapContents() {
1333     ConcurrentNavigableMap map = dmap5();
1334     SortedMap sm = map.subMap(m2, m4);
1335     assertEquals(m2, sm.firstKey());
1336     assertEquals(m3, sm.lastKey());
1337     assertEquals(2, sm.size());
1338     assertFalse(sm.containsKey(m1));
1339     assertTrue(sm.containsKey(m2));
1340     assertTrue(sm.containsKey(m3));
1341     assertFalse(sm.containsKey(m4));
1342     assertFalse(sm.containsKey(m5));
1343     Iterator i = sm.keySet().iterator();
1344     Object k;
1345     k = (Integer)(i.next());
1346     assertEquals(m2, k);
1347     k = (Integer)(i.next());
1348     assertEquals(m3, k);
1349     assertFalse(i.hasNext());
1350     Iterator j = sm.keySet().iterator();
1351     j.next();
1352     j.remove();
1353     assertFalse(map.containsKey(m2));
1354     assertEquals(4, map.size());
1355     assertEquals(1, sm.size());
1356     assertEquals(m3, sm.firstKey());
1357     assertEquals(m3, sm.lastKey());
1358     assertTrue(sm.remove(m3) != null);
1359     assertTrue(sm.isEmpty());
1360     assertEquals(3, map.size());
1361     }
1362    
1363     public void testDescendingSubMapContents2() {
1364     ConcurrentNavigableMap map = dmap5();
1365     SortedMap sm = map.subMap(m2, m3);
1366     assertEquals(1, sm.size());
1367     assertEquals(m2, sm.firstKey());
1368     assertEquals(m2, sm.lastKey());
1369     assertFalse(sm.containsKey(m1));
1370     assertTrue(sm.containsKey(m2));
1371     assertFalse(sm.containsKey(m3));
1372     assertFalse(sm.containsKey(m4));
1373     assertFalse(sm.containsKey(m5));
1374     Iterator i = sm.keySet().iterator();
1375     Object k;
1376     k = (Integer)(i.next());
1377     assertEquals(m2, k);
1378     assertFalse(i.hasNext());
1379     Iterator j = sm.keySet().iterator();
1380     j.next();
1381     j.remove();
1382     assertFalse(map.containsKey(m2));
1383     assertEquals(4, map.size());
1384     assertEquals(0, sm.size());
1385     assertTrue(sm.isEmpty());
1386     assertTrue(sm.remove(m3) == null);
1387     assertEquals(4, map.size());
1388     }
1389    
1390     /**
1391     * headMap returns map with keys in requested range
1392     */
1393     public void testDescendingHeadMapContents() {
1394     ConcurrentNavigableMap map = dmap5();
1395     SortedMap sm = map.headMap(m4);
1396     assertTrue(sm.containsKey(m1));
1397     assertTrue(sm.containsKey(m2));
1398     assertTrue(sm.containsKey(m3));
1399     assertFalse(sm.containsKey(m4));
1400     assertFalse(sm.containsKey(m5));
1401     Iterator i = sm.keySet().iterator();
1402     Object k;
1403     k = (Integer)(i.next());
1404     assertEquals(m1, k);
1405     k = (Integer)(i.next());
1406     assertEquals(m2, k);
1407     k = (Integer)(i.next());
1408     assertEquals(m3, k);
1409     assertFalse(i.hasNext());
1410     sm.clear();
1411     assertTrue(sm.isEmpty());
1412     assertEquals(2, map.size());
1413     assertEquals(m4, map.firstKey());
1414     }
1415    
1416     /**
1417     * headMap returns map with keys in requested range
1418     */
1419     public void testDescendingTailMapContents() {
1420     ConcurrentNavigableMap map = dmap5();
1421     SortedMap sm = map.tailMap(m2);
1422     assertFalse(sm.containsKey(m1));
1423     assertTrue(sm.containsKey(m2));
1424     assertTrue(sm.containsKey(m3));
1425     assertTrue(sm.containsKey(m4));
1426     assertTrue(sm.containsKey(m5));
1427     Iterator i = sm.keySet().iterator();
1428     Object k;
1429     k = (Integer)(i.next());
1430     assertEquals(m2, k);
1431     k = (Integer)(i.next());
1432     assertEquals(m3, k);
1433     k = (Integer)(i.next());
1434     assertEquals(m4, k);
1435     k = (Integer)(i.next());
1436     assertEquals(m5, k);
1437     assertFalse(i.hasNext());
1438    
1439     Iterator ei = sm.entrySet().iterator();
1440     Map.Entry e;
1441     e = (Map.Entry)(ei.next());
1442     assertEquals(m2, e.getKey());
1443     assertEquals("B", e.getValue());
1444     e = (Map.Entry)(ei.next());
1445     assertEquals(m3, e.getKey());
1446     assertEquals("C", e.getValue());
1447     e = (Map.Entry)(ei.next());
1448     assertEquals(m4, e.getKey());
1449     assertEquals("D", e.getValue());
1450     e = (Map.Entry)(ei.next());
1451     assertEquals(m5, e.getKey());
1452     assertEquals("E", e.getValue());
1453     assertFalse(i.hasNext());
1454    
1455     SortedMap ssm = sm.tailMap(m4);
1456     assertEquals(m4, ssm.firstKey());
1457     assertEquals(m5, ssm.lastKey());
1458     assertTrue(ssm.remove(m4) != null);
1459     assertEquals(1, ssm.size());
1460     assertEquals(3, sm.size());
1461     assertEquals(4, map.size());
1462     }
1463 jsr166 1.6
1464 dl 1.1 }