OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / libcore / java / util / OldTreeMapTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package libcore.java.util;
19
20 import dalvik.annotation.TestLevel;
21 import dalvik.annotation.TestTargetClass;
22 import dalvik.annotation.TestTargetNew;
23 import java.io.Serializable;
24 import java.text.CollationKey;
25 import java.text.Collator;
26 import java.util.Collection;
27 import java.util.Comparator;
28 import java.util.HashMap;
29 import java.util.Map;
30 import java.util.NoSuchElementException;
31 import java.util.Set;
32 import java.util.SortedMap;
33 import java.util.TreeMap;
34 import tests.support.Support_MapTest2;
35
36 @TestTargetClass(TreeMap.class)
37 public class OldTreeMapTest extends junit.framework.TestCase {
38
39     public static class ReversedComparator implements Comparator {
40         public int compare(Object o1, Object o2) {
41             return -(((Comparable) o1).compareTo(o2));
42         }
43
44         public boolean equals(Object o1, Object o2) {
45             return (((Comparable) o1).compareTo(o2)) == 0;
46         }
47     }
48
49     // Regression for Harmony-1026
50     public static class MockComparator<T extends Comparable<T>> implements
51             Comparator<T>, Serializable {
52
53         public int compare(T o1, T o2) {
54             if (o1 == o2) {
55                 return 0;
56             }
57             if (null == o1 || null == o2) {
58                 return -1;
59             }
60             T c1 = o1;
61             T c2 = o2;
62             return c1.compareTo(c2);
63         }
64     }
65
66     // Regression for Harmony-1161
67     class MockComparatorNullTolerable implements Comparator<String> {
68
69         public int compare(String o1, String o2) {
70             if (o1 == o2) {
71                 return 0;
72             }
73             if (null == o1) {
74                 return -1;
75             }
76             if (null == o2) {
77                 return 1;
78             }
79             return o1.compareTo(o2);
80         }
81     }
82
83     TreeMap tm;
84
85     Object objArray[] = new Object[1000];
86
87     /**
88      * @tests java.util.TreeMap#TreeMap()
89      */
90     @TestTargetNew(
91         level = TestLevel.COMPLETE,
92         notes = "",
93         method = "TreeMap",
94         args = {}
95     )
96     public void test_Constructor() {
97         // Test for method java.util.TreeMap()
98         new Support_MapTest2(new TreeMap()).runTest();
99
100         assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
101     }
102
103     /**
104      * @tests java.util.TreeMap#TreeMap(java.util.Comparator)
105      */
106     @TestTargetNew(
107         level = TestLevel.COMPLETE,
108         notes = "",
109         method = "TreeMap",
110         args = {java.util.Comparator.class}
111     )
112     public void test_ConstructorLjava_util_Comparator() {
113         // Test for method java.util.TreeMap(java.util.Comparator)
114         Comparator comp = new ReversedComparator();
115         TreeMap reversedTreeMap = new TreeMap(comp);
116         assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
117                 .comparator() == comp);
118         reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
119         reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
120         assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
121                 reversedTreeMap.firstKey().equals(new Integer(2).toString()));
122         assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
123                 reversedTreeMap.lastKey().equals(new Integer(1).toString()));
124
125     }
126
127     /**
128      * @tests java.util.TreeMap#TreeMap(java.util.Map)
129      */
130     @TestTargetNew(
131         level = TestLevel.COMPLETE,
132         notes = "",
133         method = "TreeMap",
134         args = {java.util.Map.class}
135     )
136     public void test_ConstructorLjava_util_Map() {
137         // Test for method java.util.TreeMap(java.util.Map)
138         TreeMap myTreeMap = new TreeMap(new HashMap(tm));
139         assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
140         for (Object element : objArray) {
141             assertTrue("Map has incorrect mappings", myTreeMap.get(
142                     element.toString()).equals(element));
143         }
144
145         HashMap hm = new HashMap();
146         hm.put(new Integer(1), "one");
147         hm.put("one", new Integer(1));
148
149         try {
150             new TreeMap(hm);
151             fail("ClassCastException expected");
152         } catch (ClassCastException e) {
153             //expected
154         }
155
156         try {
157             new TreeMap((Map)null);
158             fail("NullPointerException expected");
159         } catch (NullPointerException e) {
160             //expected
161         }
162     }
163
164     /**
165      * @tests java.util.TreeMap#TreeMap(java.util.SortedMap)
166      */
167     @TestTargetNew(
168         level = TestLevel.COMPLETE,
169         notes = "",
170         method = "TreeMap",
171         args = {java.util.SortedMap.class}
172     )
173     public void test_ConstructorLjava_util_SortedMap() {
174         // Test for method java.util.TreeMap(java.util.SortedMap)
175         Comparator comp = new ReversedComparator();
176         TreeMap reversedTreeMap = new TreeMap(comp);
177         reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
178         reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
179         TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
180         assertTrue("New tree map does not answer correct comparator",
181                 anotherTreeMap.comparator() == comp);
182         assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
183                 anotherTreeMap.firstKey().equals(new Integer(2).toString()));
184         assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
185                 anotherTreeMap.lastKey().equals(new Integer(1).toString()));
186
187         try {
188             new TreeMap((SortedMap)null);
189             fail("NullPointerException expected");
190         } catch (NullPointerException e) {
191             //expected
192         }
193     }
194
195     /**
196      * @tests java.util.TreeMap#containsKey(java.lang.Object)
197      */
198     @TestTargetNew(
199         level = TestLevel.COMPLETE,
200         notes = "",
201         method = "containsKey",
202         args = {java.lang.Object.class}
203     )
204     public void test_containsKeyLjava_lang_Object() {
205         // Test for method boolean
206         // java.util.TreeMap.containsKey(java.lang.Object)
207         assertTrue("Returned false for valid key", tm.containsKey("95"));
208         assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX"));
209
210         try {
211             tm.containsKey(new Double(3.14));
212             fail("ClassCastException expected");
213         } catch (ClassCastException e) {
214             //expected
215         }
216
217         try {
218             tm.containsKey(null);
219             fail("NullPointerException expected");
220         } catch (NullPointerException e) {
221             //expected
222         }
223     }
224
225     /**
226      * @tests java.util.TreeMap#firstKey()
227      */
228     @TestTargetNew(
229         level = TestLevel.COMPLETE,
230         notes = "",
231         method = "firstKey",
232         args = {}
233     )
234     public void test_firstKey() {
235         // Test for method java.lang.Object java.util.TreeMap.firstKey()
236         assertEquals("Returned incorrect first key", "0", tm.firstKey());
237         tm = new TreeMap();
238         try {
239             tm.firstKey();
240             fail("NoSuchElementException expected");
241         } catch (NoSuchElementException e) {
242             //expected
243         }
244     }
245
246     /**
247      * @tests java.util.TreeMap#get(java.lang.Object)
248      */
249     @TestTargetNew(
250         level = TestLevel.COMPLETE,
251         notes = "",
252         method = "get",
253         args = {java.lang.Object.class}
254     )
255     public void test_getLjava_lang_Object() {
256         // Test for method java.lang.Object
257         // java.util.TreeMap.get(java.lang.Object)
258         Object o = new Object();
259         tm.put("Hello", o);
260         assertTrue("Failed to get mapping", tm.get("Hello") == o);
261
262         try {
263             tm.get(new Double(3.14));
264             fail("ClassCastException expected");
265         } catch (ClassCastException e) {
266             //expected
267         }
268
269         try {
270             tm.get(null);
271             fail("NullPointerException expected");
272         } catch (NullPointerException e) {
273             //expected
274         }
275     }
276
277     /**
278      * @tests java.util.TreeMap#headMap(java.lang.Object)
279      */
280     @TestTargetNew(
281         level = TestLevel.COMPLETE,
282         notes = "",
283         method = "headMap",
284         args = {java.lang.Object.class}
285     )
286     public void test_headMapLjava_lang_Object() {
287         // Test for method java.util.SortedMap
288         // java.util.TreeMap.headMap(java.lang.Object)
289         Map head = tm.headMap("100");
290         assertEquals("Returned map of incorrect size", 3, head.size());
291         assertTrue("Returned incorrect elements", head.containsKey("0")
292                 && head.containsValue(new Integer("1"))
293                 && head.containsKey("10"));
294         SortedMap sort = tm.headMap("100");
295         try {
296             sort.headMap("50");
297             fail("IllegalArgumentException expected");
298         } catch (IllegalArgumentException e) {
299             //expected
300         }
301
302         try {
303             tm.headMap(this);
304             fail("ClassCastException expected");
305         } catch (ClassCastException e) {
306             //expected
307         }
308
309         try {
310             tm.headMap(null);
311             fail("NullPointerException expected");
312         } catch (NullPointerException e) {
313             //expected
314         }
315
316         // Regression for Harmony-1026
317         TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
318                 new MockComparator());
319         map.put(1, 2.1);
320         map.put(2, 3.1);
321         map.put(3, 4.5);
322         map.put(7, 21.3);
323         map.put(null, null);
324
325         SortedMap<Integer, Double> smap = map.headMap(null);
326         assertEquals(0, smap.size());
327
328         Set<Integer> keySet = smap.keySet();
329         assertEquals(0, keySet.size());
330
331         Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
332         assertEquals(0, entrySet.size());
333
334         Collection<Double> valueCollection = smap.values();
335         assertEquals(0, valueCollection.size());
336
337         // Regression for Harmony-1066
338         assertTrue(head instanceof Serializable);
339
340         // Regression for ill-behaved collator
341         Collator c = new Collator() {
342             @Override
343             public int compare(String o1, String o2) {
344                 if (o1 == null) {
345                     return 0;
346                 }
347                 return o1.compareTo(o2);
348             }
349
350             @Override
351             public CollationKey getCollationKey(String string) {
352                 return null;
353             }
354
355             @Override
356             public int hashCode() {
357                 return 0;
358             }
359         };
360
361         TreeMap<String, String> treemap = new TreeMap<String, String>(c);
362         assertEquals(0, treemap.headMap(null).size());
363     }
364
365     /**
366      * @tests java.util.TreeMap#lastKey()
367      */
368     @TestTargetNew(
369         level = TestLevel.COMPLETE,
370         notes = "",
371         method = "lastKey",
372         args = {}
373     )
374     public void test_lastKey() {
375         // Test for method java.lang.Object java.util.TreeMap.lastKey()
376         assertTrue("Returned incorrect last key", tm.lastKey().equals(
377                 objArray[objArray.length - 1].toString()));
378         tm = new TreeMap();
379         try {
380             tm.lastKey();
381             fail("NoSuchElementException expected");
382         } catch (NoSuchElementException e) {
383             //expected
384         }
385     }
386
387     /**
388      * @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object)
389      */
390     @TestTargetNew(
391         level = TestLevel.COMPLETE,
392         notes = "",
393         method = "put",
394         args = {java.lang.Object.class, java.lang.Object.class}
395     )
396     public void test_putLjava_lang_ObjectLjava_lang_Object() {
397         // Test for method java.lang.Object
398         // java.util.TreeMap.put(java.lang.Object, java.lang.Object)
399         Object o = new Object();
400         tm.put("Hello", o);
401         assertTrue("Failed to put mapping", tm.get("Hello") == o);
402
403         try {
404             tm.put(null, "null");
405             fail("NullPointerException expected");
406         } catch (NullPointerException e) {
407             //expected
408         }
409
410         // regression for Harmony-780
411         tm = new TreeMap();
412         try {
413             assertNull(tm.put(new Object(), new Object()));
414             tm.put(new Integer(1), new Object());
415             fail("should throw ClassCastException");
416         } catch (ClassCastException e) {
417             // expected
418         }
419
420         tm = new TreeMap();
421         assertNull(tm.put(new Integer(1), new Object()));
422
423         // regression for Harmony-2474
424         tm = new TreeMap();
425         tm.remove(o);
426     }
427
428     /**
429      * @tests java.util.TreeMap#putAll(java.util.Map)
430      */
431     @TestTargetNew(
432         level = TestLevel.COMPLETE,
433         notes = "",
434         method = "putAll",
435         args = {java.util.Map.class}
436     )
437     public void test_putAllLjava_util_Map() {
438         // Test for method void java.util.TreeMap.putAll(java.util.Map)
439         TreeMap x = new TreeMap();
440         x.putAll(tm);
441         assertTrue("Map incorrect size after put", x.size() == tm.size());
442         for (Object element : objArray) {
443             assertTrue("Failed to put all elements", x.get(element.toString())
444                     .equals(element));
445         }
446         x = new TreeMap();
447         x.put(new Integer(1), "one");
448         x.put(new Integer(2), "two");
449
450         try {
451             tm.putAll(x);
452             fail("ClassCastException expected");
453         } catch (ClassCastException e) {
454             //expected
455         }
456
457         try {
458             tm.putAll(null);
459             fail("NullPointerException expected");
460         } catch (NullPointerException e) {
461             //expected
462         }
463     }
464
465     /**
466      * @tests java.util.TreeMap#remove(java.lang.Object)
467      */
468     @TestTargetNew(
469         level = TestLevel.COMPLETE,
470         notes = "",
471         method = "remove",
472         args = {java.lang.Object.class}
473     )
474     public void test_removeLjava_lang_Object() {
475         // Test for method java.lang.Object
476         // java.util.TreeMap.remove(java.lang.Object)
477         tm.remove("990");
478         assertTrue("Failed to remove mapping", !tm.containsKey("990"));
479
480         try {
481             tm.remove(new Double(3.14));
482             fail("ClassCastException expected");
483         } catch (ClassCastException e) {
484             //expected
485         }
486
487         try {
488             tm.remove(null);
489             fail("NullPointerException expected");
490         } catch (NullPointerException e) {
491             //expected
492         }
493     }
494
495     /**
496      * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
497      */
498     @TestTargetNew(
499         level = TestLevel.COMPLETE,
500         notes = "",
501         method = "subMap",
502         args = {java.lang.Object.class, java.lang.Object.class}
503     )
504     public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
505         // Test for method java.util.SortedMap
506         // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
507         SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
508                 .toString());
509         assertEquals("subMap is of incorrect size", 9, subMap.size());
510         for (int counter = 100; counter < 109; counter++) {
511             assertTrue("SubMap contains incorrect elements", subMap.get(
512                     objArray[counter].toString()).equals(objArray[counter]));
513         }
514
515         try {
516             tm.subMap(objArray[9].toString(), objArray[1].toString());
517             fail("end key less than start key should throw IllegalArgumentException");
518         } catch (IllegalArgumentException e) {
519             // Expected
520         }
521
522         // Regression for Harmony-1161
523         TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
524                 new MockComparatorNullTolerable());
525         treeMapWithNull.put("key1", "value1");
526         treeMapWithNull.put(null, "value2");
527         SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
528                 "key1");
529         assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size());
530
531         // Regression test for typo in lastKey method
532         SortedMap<String, String> map = new TreeMap<String, String>();
533         map.put("1", "one");
534         map.put("2", "two");
535         map.put("3", "three");
536         assertEquals("3", map.lastKey());
537         SortedMap<String, String> sub = map.subMap("1", "3");
538         assertEquals("2", sub.lastKey());
539
540         try {
541             tm.subMap(this, this);
542             fail("ClassCastException expected");
543         } catch (ClassCastException e) {
544             //expected
545         }
546
547         try {
548             tm.subMap(objArray[9].toString(), null);
549             fail("NullPointerException expected");
550         } catch (NullPointerException e) {
551             //expected
552         }
553
554         try {
555             tm.subMap(null, objArray[9].toString());
556             fail("NullPointerException expected");
557         } catch (NullPointerException e) {
558             //expected
559         }
560     }
561
562     /**
563      * @tests java.util.TreeMap#tailMap(java.lang.Object)
564      */
565     @TestTargetNew(
566         level = TestLevel.COMPLETE,
567         notes = "",
568         method = "tailMap",
569         args = {java.lang.Object.class}
570     )
571     public void test_tailMapLjava_lang_Object() {
572         // Test for method java.util.SortedMap
573         // java.util.TreeMap.tailMap(java.lang.Object)
574         Map tail = tm.tailMap(objArray[900].toString());
575         assertTrue("Returned map of incorrect size : " + tail.size(), tail
576                 .size() == (objArray.length - 900) + 9);
577         for (int i = 900; i < objArray.length; i++) {
578             assertTrue("Map contains incorrect entries", tail
579                     .containsValue(objArray[i]));
580         }
581
582         SortedMap sort = tm.tailMap("99");
583
584         try {
585             sort.tailMap("101");
586             fail("IllegalArgumentException expected");
587         } catch (IllegalArgumentException e) {
588             //expected
589         }
590
591         try {
592             tm.tailMap(this);
593             fail("ClassCastException expected");
594         } catch (ClassCastException e) {
595             //expected
596         }
597
598         try {
599             tm.tailMap(null);
600             fail("NullPointerException expected");
601         } catch (NullPointerException e) {
602             //expected
603         }
604
605         // Regression for Harmony-1066
606         assertTrue(tail instanceof Serializable);
607     }
608     /**
609      * Sets up the fixture, for example, open a network connection. This method
610      * is called before a test is executed.
611      */
612     @Override
613     protected void setUp() {
614         tm = new TreeMap();
615         for (int i = 0; i < objArray.length; i++) {
616             Object x = objArray[i] = new Integer(i);
617             tm.put(x.toString(), x);
618         }
619     }
620
621 }