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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package libcore.java.util;
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;
30 import java.util.NoSuchElementException;
32 import java.util.SortedMap;
33 import java.util.TreeMap;
34 import tests.support.Support_MapTest2;
36 @TestTargetClass(TreeMap.class)
37 public class OldTreeMapTest extends junit.framework.TestCase {
39 public static class ReversedComparator implements Comparator {
40 public int compare(Object o1, Object o2) {
41 return -(((Comparable) o1).compareTo(o2));
44 public boolean equals(Object o1, Object o2) {
45 return (((Comparable) o1).compareTo(o2)) == 0;
49 // Regression for Harmony-1026
50 public static class MockComparator<T extends Comparable<T>> implements
51 Comparator<T>, Serializable {
53 public int compare(T o1, T o2) {
57 if (null == o1 || null == o2) {
62 return c1.compareTo(c2);
66 // Regression for Harmony-1161
67 class MockComparatorNullTolerable implements Comparator<String> {
69 public int compare(String o1, String o2) {
79 return o1.compareTo(o2);
85 Object objArray[] = new Object[1000];
88 * @tests java.util.TreeMap#TreeMap()
91 level = TestLevel.COMPLETE,
96 public void test_Constructor() {
97 // Test for method java.util.TreeMap()
98 new Support_MapTest2(new TreeMap()).runTest();
100 assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
104 * @tests java.util.TreeMap#TreeMap(java.util.Comparator)
107 level = TestLevel.COMPLETE,
110 args = {java.util.Comparator.class}
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()));
128 * @tests java.util.TreeMap#TreeMap(java.util.Map)
131 level = TestLevel.COMPLETE,
134 args = {java.util.Map.class}
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));
145 HashMap hm = new HashMap();
146 hm.put(new Integer(1), "one");
147 hm.put("one", new Integer(1));
151 fail("ClassCastException expected");
152 } catch (ClassCastException e) {
157 new TreeMap((Map)null);
158 fail("NullPointerException expected");
159 } catch (NullPointerException e) {
165 * @tests java.util.TreeMap#TreeMap(java.util.SortedMap)
168 level = TestLevel.COMPLETE,
171 args = {java.util.SortedMap.class}
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()));
188 new TreeMap((SortedMap)null);
189 fail("NullPointerException expected");
190 } catch (NullPointerException e) {
196 * @tests java.util.TreeMap#containsKey(java.lang.Object)
199 level = TestLevel.COMPLETE,
201 method = "containsKey",
202 args = {java.lang.Object.class}
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"));
211 tm.containsKey(new Double(3.14));
212 fail("ClassCastException expected");
213 } catch (ClassCastException e) {
218 tm.containsKey(null);
219 fail("NullPointerException expected");
220 } catch (NullPointerException e) {
226 * @tests java.util.TreeMap#firstKey()
229 level = TestLevel.COMPLETE,
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());
240 fail("NoSuchElementException expected");
241 } catch (NoSuchElementException e) {
247 * @tests java.util.TreeMap#get(java.lang.Object)
250 level = TestLevel.COMPLETE,
253 args = {java.lang.Object.class}
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();
260 assertTrue("Failed to get mapping", tm.get("Hello") == o);
263 tm.get(new Double(3.14));
264 fail("ClassCastException expected");
265 } catch (ClassCastException e) {
271 fail("NullPointerException expected");
272 } catch (NullPointerException e) {
278 * @tests java.util.TreeMap#headMap(java.lang.Object)
281 level = TestLevel.COMPLETE,
284 args = {java.lang.Object.class}
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");
297 fail("IllegalArgumentException expected");
298 } catch (IllegalArgumentException e) {
304 fail("ClassCastException expected");
305 } catch (ClassCastException e) {
311 fail("NullPointerException expected");
312 } catch (NullPointerException e) {
316 // Regression for Harmony-1026
317 TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
318 new MockComparator());
325 SortedMap<Integer, Double> smap = map.headMap(null);
326 assertEquals(0, smap.size());
328 Set<Integer> keySet = smap.keySet();
329 assertEquals(0, keySet.size());
331 Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
332 assertEquals(0, entrySet.size());
334 Collection<Double> valueCollection = smap.values();
335 assertEquals(0, valueCollection.size());
337 // Regression for Harmony-1066
338 assertTrue(head instanceof Serializable);
340 // Regression for ill-behaved collator
341 Collator c = new Collator() {
343 public int compare(String o1, String o2) {
347 return o1.compareTo(o2);
351 public CollationKey getCollationKey(String string) {
356 public int hashCode() {
361 TreeMap<String, String> treemap = new TreeMap<String, String>(c);
362 assertEquals(0, treemap.headMap(null).size());
366 * @tests java.util.TreeMap#lastKey()
369 level = TestLevel.COMPLETE,
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()));
381 fail("NoSuchElementException expected");
382 } catch (NoSuchElementException e) {
388 * @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object)
391 level = TestLevel.COMPLETE,
394 args = {java.lang.Object.class, java.lang.Object.class}
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();
401 assertTrue("Failed to put mapping", tm.get("Hello") == o);
404 tm.put(null, "null");
405 fail("NullPointerException expected");
406 } catch (NullPointerException e) {
410 // regression for Harmony-780
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) {
421 assertNull(tm.put(new Integer(1), new Object()));
423 // regression for Harmony-2474
429 * @tests java.util.TreeMap#putAll(java.util.Map)
432 level = TestLevel.COMPLETE,
435 args = {java.util.Map.class}
437 public void test_putAllLjava_util_Map() {
438 // Test for method void java.util.TreeMap.putAll(java.util.Map)
439 TreeMap x = new TreeMap();
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())
447 x.put(new Integer(1), "one");
448 x.put(new Integer(2), "two");
452 fail("ClassCastException expected");
453 } catch (ClassCastException e) {
459 fail("NullPointerException expected");
460 } catch (NullPointerException e) {
466 * @tests java.util.TreeMap#remove(java.lang.Object)
469 level = TestLevel.COMPLETE,
472 args = {java.lang.Object.class}
474 public void test_removeLjava_lang_Object() {
475 // Test for method java.lang.Object
476 // java.util.TreeMap.remove(java.lang.Object)
478 assertTrue("Failed to remove mapping", !tm.containsKey("990"));
481 tm.remove(new Double(3.14));
482 fail("ClassCastException expected");
483 } catch (ClassCastException e) {
489 fail("NullPointerException expected");
490 } catch (NullPointerException e) {
496 * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
499 level = TestLevel.COMPLETE,
502 args = {java.lang.Object.class, java.lang.Object.class}
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]
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]));
516 tm.subMap(objArray[9].toString(), objArray[1].toString());
517 fail("end key less than start key should throw IllegalArgumentException");
518 } catch (IllegalArgumentException e) {
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,
529 assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size());
531 // Regression test for typo in lastKey method
532 SortedMap<String, String> map = new TreeMap<String, String>();
535 map.put("3", "three");
536 assertEquals("3", map.lastKey());
537 SortedMap<String, String> sub = map.subMap("1", "3");
538 assertEquals("2", sub.lastKey());
541 tm.subMap(this, this);
542 fail("ClassCastException expected");
543 } catch (ClassCastException e) {
548 tm.subMap(objArray[9].toString(), null);
549 fail("NullPointerException expected");
550 } catch (NullPointerException e) {
555 tm.subMap(null, objArray[9].toString());
556 fail("NullPointerException expected");
557 } catch (NullPointerException e) {
563 * @tests java.util.TreeMap#tailMap(java.lang.Object)
566 level = TestLevel.COMPLETE,
569 args = {java.lang.Object.class}
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]));
582 SortedMap sort = tm.tailMap("99");
586 fail("IllegalArgumentException expected");
587 } catch (IllegalArgumentException e) {
593 fail("ClassCastException expected");
594 } catch (ClassCastException e) {
600 fail("NullPointerException expected");
601 } catch (NullPointerException e) {
605 // Regression for Harmony-1066
606 assertTrue(tail instanceof Serializable);
609 * Sets up the fixture, for example, open a network connection. This method
610 * is called before a test is executed.
613 protected void setUp() {
615 for (int i = 0; i < objArray.length; i++) {
616 Object x = objArray[i] = new Integer(i);
617 tm.put(x.toString(), x);