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

File Contents

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