PageRenderTime 25ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 1ms

/luni/src/test/java/libcore/java/util/OldTreeMapTest.java

https://bitbucket.org/tamacjp/android_libcore
Java | 501 lines | 353 code | 67 blank | 81 comment | 29 complexity | 22cc084fdd5c005625627cba8c6d2858 MD5 | raw file
  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. package libcore.java.util;
  18. import java.io.Serializable;
  19. import java.text.CollationKey;
  20. import java.text.Collator;
  21. import java.util.Collection;
  22. import java.util.Comparator;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import java.util.NoSuchElementException;
  26. import java.util.Set;
  27. import java.util.SortedMap;
  28. import java.util.TreeMap;
  29. import tests.support.Support_MapTest2;
  30. public class OldTreeMapTest extends junit.framework.TestCase {
  31. public static class ReversedComparator implements Comparator {
  32. public int compare(Object o1, Object o2) {
  33. return -(((Comparable) o1).compareTo(o2));
  34. }
  35. public boolean equals(Object o1, Object o2) {
  36. return (((Comparable) o1).compareTo(o2)) == 0;
  37. }
  38. }
  39. // Regression for Harmony-1026
  40. public static class MockComparator<T extends Comparable<T>> implements
  41. Comparator<T>, Serializable {
  42. public int compare(T o1, T o2) {
  43. if (o1 == o2) {
  44. return 0;
  45. }
  46. if (null == o1 || null == o2) {
  47. return -1;
  48. }
  49. T c1 = o1;
  50. T c2 = o2;
  51. return c1.compareTo(c2);
  52. }
  53. }
  54. // Regression for Harmony-1161
  55. class MockComparatorNullTolerable implements Comparator<String> {
  56. public int compare(String o1, String o2) {
  57. if (o1 == o2) {
  58. return 0;
  59. }
  60. if (null == o1) {
  61. return -1;
  62. }
  63. if (null == o2) {
  64. return 1;
  65. }
  66. return o1.compareTo(o2);
  67. }
  68. }
  69. TreeMap tm;
  70. Object objArray[] = new Object[1000];
  71. public void test_Constructor() {
  72. // Test for method java.util.TreeMap()
  73. new Support_MapTest2(new TreeMap()).runTest();
  74. assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
  75. }
  76. public void test_ConstructorLjava_util_Comparator() {
  77. // Test for method java.util.TreeMap(java.util.Comparator)
  78. Comparator comp = new ReversedComparator();
  79. TreeMap reversedTreeMap = new TreeMap(comp);
  80. assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
  81. .comparator() == comp);
  82. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  83. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  84. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  85. reversedTreeMap.firstKey().equals(new Integer(2).toString()));
  86. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  87. reversedTreeMap.lastKey().equals(new Integer(1).toString()));
  88. }
  89. public void test_ConstructorLjava_util_Map() {
  90. // Test for method java.util.TreeMap(java.util.Map)
  91. TreeMap myTreeMap = new TreeMap(new HashMap(tm));
  92. assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
  93. for (Object element : objArray) {
  94. assertTrue("Map has incorrect mappings", myTreeMap.get(
  95. element.toString()).equals(element));
  96. }
  97. HashMap hm = new HashMap();
  98. hm.put(new Integer(1), "one");
  99. hm.put("one", new Integer(1));
  100. try {
  101. new TreeMap(hm);
  102. fail("ClassCastException expected");
  103. } catch (ClassCastException e) {
  104. //expected
  105. }
  106. try {
  107. new TreeMap((Map)null);
  108. fail("NullPointerException expected");
  109. } catch (NullPointerException e) {
  110. //expected
  111. }
  112. }
  113. public void test_ConstructorLjava_util_SortedMap() {
  114. // Test for method java.util.TreeMap(java.util.SortedMap)
  115. Comparator comp = new ReversedComparator();
  116. TreeMap reversedTreeMap = new TreeMap(comp);
  117. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  118. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  119. TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
  120. assertTrue("New tree map does not answer correct comparator",
  121. anotherTreeMap.comparator() == comp);
  122. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  123. anotherTreeMap.firstKey().equals(new Integer(2).toString()));
  124. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  125. anotherTreeMap.lastKey().equals(new Integer(1).toString()));
  126. try {
  127. new TreeMap((SortedMap)null);
  128. fail("NullPointerException expected");
  129. } catch (NullPointerException e) {
  130. //expected
  131. }
  132. }
  133. public void test_containsKeyLjava_lang_Object() {
  134. // Test for method boolean
  135. // java.util.TreeMap.containsKey(java.lang.Object)
  136. assertTrue("Returned false for valid key", tm.containsKey("95"));
  137. assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX"));
  138. try {
  139. tm.containsKey(new Double(3.14));
  140. fail("ClassCastException expected");
  141. } catch (ClassCastException e) {
  142. //expected
  143. }
  144. try {
  145. tm.containsKey(null);
  146. fail("NullPointerException expected");
  147. } catch (NullPointerException e) {
  148. //expected
  149. }
  150. }
  151. public void test_firstKey() {
  152. // Test for method java.lang.Object java.util.TreeMap.firstKey()
  153. assertEquals("Returned incorrect first key", "0", tm.firstKey());
  154. tm = new TreeMap();
  155. try {
  156. tm.firstKey();
  157. fail("NoSuchElementException expected");
  158. } catch (NoSuchElementException e) {
  159. //expected
  160. }
  161. }
  162. public void test_getLjava_lang_Object() {
  163. // Test for method java.lang.Object
  164. // java.util.TreeMap.get(java.lang.Object)
  165. Object o = new Object();
  166. tm.put("Hello", o);
  167. assertTrue("Failed to get mapping", tm.get("Hello") == o);
  168. try {
  169. tm.get(new Double(3.14));
  170. fail("ClassCastException expected");
  171. } catch (ClassCastException e) {
  172. //expected
  173. }
  174. try {
  175. tm.get(null);
  176. fail("NullPointerException expected");
  177. } catch (NullPointerException e) {
  178. //expected
  179. }
  180. }
  181. public void test_headMapLjava_lang_Object() {
  182. // Test for method java.util.SortedMap
  183. // java.util.TreeMap.headMap(java.lang.Object)
  184. Map head = tm.headMap("100");
  185. assertEquals("Returned map of incorrect size", 3, head.size());
  186. assertTrue("Returned incorrect elements", head.containsKey("0")
  187. && head.containsValue(new Integer("1"))
  188. && head.containsKey("10"));
  189. SortedMap sort = tm.headMap("100");
  190. try {
  191. sort.headMap("50");
  192. fail("IllegalArgumentException expected");
  193. } catch (IllegalArgumentException e) {
  194. //expected
  195. }
  196. try {
  197. tm.headMap(this);
  198. fail("ClassCastException expected");
  199. } catch (ClassCastException e) {
  200. //expected
  201. }
  202. try {
  203. tm.headMap(null);
  204. fail("NullPointerException expected");
  205. } catch (NullPointerException e) {
  206. //expected
  207. }
  208. // Regression for Harmony-1026
  209. TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
  210. new MockComparator());
  211. map.put(1, 2.1);
  212. map.put(2, 3.1);
  213. map.put(3, 4.5);
  214. map.put(7, 21.3);
  215. map.put(null, null);
  216. SortedMap<Integer, Double> smap = map.headMap(null);
  217. assertEquals(0, smap.size());
  218. Set<Integer> keySet = smap.keySet();
  219. assertEquals(0, keySet.size());
  220. Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
  221. assertEquals(0, entrySet.size());
  222. Collection<Double> valueCollection = smap.values();
  223. assertEquals(0, valueCollection.size());
  224. // Regression for Harmony-1066
  225. assertTrue(head instanceof Serializable);
  226. // Regression for ill-behaved collator
  227. Collator c = new Collator() {
  228. @Override
  229. public int compare(String o1, String o2) {
  230. if (o1 == null) {
  231. return 0;
  232. }
  233. return o1.compareTo(o2);
  234. }
  235. @Override
  236. public CollationKey getCollationKey(String string) {
  237. return null;
  238. }
  239. @Override
  240. public int hashCode() {
  241. return 0;
  242. }
  243. };
  244. TreeMap<String, String> treemap = new TreeMap<String, String>(c);
  245. assertEquals(0, treemap.headMap(null).size());
  246. }
  247. public void test_lastKey() {
  248. // Test for method java.lang.Object java.util.TreeMap.lastKey()
  249. assertTrue("Returned incorrect last key", tm.lastKey().equals(
  250. objArray[objArray.length - 1].toString()));
  251. tm = new TreeMap();
  252. try {
  253. tm.lastKey();
  254. fail("NoSuchElementException expected");
  255. } catch (NoSuchElementException e) {
  256. //expected
  257. }
  258. }
  259. public void test_putLjava_lang_ObjectLjava_lang_Object() {
  260. // Test for method java.lang.Object
  261. // java.util.TreeMap.put(java.lang.Object, java.lang.Object)
  262. Object o = new Object();
  263. tm.put("Hello", o);
  264. assertTrue("Failed to put mapping", tm.get("Hello") == o);
  265. try {
  266. tm.put(null, "null");
  267. fail("NullPointerException expected");
  268. } catch (NullPointerException e) {
  269. //expected
  270. }
  271. // regression for Harmony-780
  272. tm = new TreeMap();
  273. try {
  274. assertNull(tm.put(new Object(), new Object()));
  275. tm.put(new Integer(1), new Object());
  276. fail("should throw ClassCastException");
  277. } catch (ClassCastException e) {
  278. // expected
  279. }
  280. tm = new TreeMap();
  281. assertNull(tm.put(new Integer(1), new Object()));
  282. }
  283. /**
  284. * Android's TreeMap requires every element to be comparable, even if
  285. * there's no other element to compare it to. This is more strict than the
  286. * RI. See Harmony-2474.
  287. */
  288. public void testRemoveNonComparableFromEmptyMap() {
  289. tm = new TreeMap();
  290. try {
  291. tm.remove(new Object()); // succeeds on RI
  292. } catch (ClassCastException expected) {
  293. // expected on libcore only
  294. }
  295. }
  296. public void test_putAllLjava_util_Map() {
  297. // Test for method void java.util.TreeMap.putAll(java.util.Map)
  298. TreeMap x = new TreeMap();
  299. x.putAll(tm);
  300. assertTrue("Map incorrect size after put", x.size() == tm.size());
  301. for (Object element : objArray) {
  302. assertTrue("Failed to put all elements", x.get(element.toString())
  303. .equals(element));
  304. }
  305. x = new TreeMap();
  306. x.put(new Integer(1), "one");
  307. x.put(new Integer(2), "two");
  308. try {
  309. tm.putAll(x);
  310. fail("ClassCastException expected");
  311. } catch (ClassCastException e) {
  312. //expected
  313. }
  314. try {
  315. tm.putAll(null);
  316. fail("NullPointerException expected");
  317. } catch (NullPointerException e) {
  318. //expected
  319. }
  320. }
  321. public void test_removeLjava_lang_Object() {
  322. // Test for method java.lang.Object
  323. // java.util.TreeMap.remove(java.lang.Object)
  324. tm.remove("990");
  325. assertTrue("Failed to remove mapping", !tm.containsKey("990"));
  326. try {
  327. tm.remove(new Double(3.14));
  328. fail("ClassCastException expected");
  329. } catch (ClassCastException e) {
  330. //expected
  331. }
  332. try {
  333. tm.remove(null);
  334. fail("NullPointerException expected");
  335. } catch (NullPointerException e) {
  336. //expected
  337. }
  338. }
  339. public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
  340. // Test for method java.util.SortedMap
  341. // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
  342. SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
  343. .toString());
  344. assertEquals("subMap is of incorrect size", 9, subMap.size());
  345. for (int counter = 100; counter < 109; counter++) {
  346. assertTrue("SubMap contains incorrect elements", subMap.get(
  347. objArray[counter].toString()).equals(objArray[counter]));
  348. }
  349. try {
  350. tm.subMap(objArray[9].toString(), objArray[1].toString());
  351. fail("end key less than start key should throw IllegalArgumentException");
  352. } catch (IllegalArgumentException e) {
  353. // Expected
  354. }
  355. // Regression for Harmony-1161
  356. TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
  357. new MockComparatorNullTolerable());
  358. treeMapWithNull.put("key1", "value1");
  359. treeMapWithNull.put(null, "value2");
  360. SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
  361. "key1");
  362. assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size());
  363. // Regression test for typo in lastKey method
  364. SortedMap<String, String> map = new TreeMap<String, String>();
  365. map.put("1", "one");
  366. map.put("2", "two");
  367. map.put("3", "three");
  368. assertEquals("3", map.lastKey());
  369. SortedMap<String, String> sub = map.subMap("1", "3");
  370. assertEquals("2", sub.lastKey());
  371. try {
  372. tm.subMap(this, this);
  373. fail("ClassCastException expected");
  374. } catch (ClassCastException e) {
  375. //expected
  376. }
  377. try {
  378. tm.subMap(objArray[9].toString(), null);
  379. fail("NullPointerException expected");
  380. } catch (NullPointerException e) {
  381. //expected
  382. }
  383. try {
  384. tm.subMap(null, objArray[9].toString());
  385. fail("NullPointerException expected");
  386. } catch (NullPointerException e) {
  387. //expected
  388. }
  389. }
  390. public void test_tailMapLjava_lang_Object() {
  391. // Test for method java.util.SortedMap
  392. // java.util.TreeMap.tailMap(java.lang.Object)
  393. Map tail = tm.tailMap(objArray[900].toString());
  394. assertTrue("Returned map of incorrect size : " + tail.size(), tail
  395. .size() == (objArray.length - 900) + 9);
  396. for (int i = 900; i < objArray.length; i++) {
  397. assertTrue("Map contains incorrect entries", tail
  398. .containsValue(objArray[i]));
  399. }
  400. SortedMap sort = tm.tailMap("99");
  401. try {
  402. sort.tailMap("101");
  403. fail("IllegalArgumentException expected");
  404. } catch (IllegalArgumentException e) {
  405. //expected
  406. }
  407. try {
  408. tm.tailMap(this);
  409. fail("ClassCastException expected");
  410. } catch (ClassCastException e) {
  411. //expected
  412. }
  413. try {
  414. tm.tailMap(null);
  415. fail("NullPointerException expected");
  416. } catch (NullPointerException e) {
  417. //expected
  418. }
  419. // Regression for Harmony-1066
  420. assertTrue(tail instanceof Serializable);
  421. }
  422. /**
  423. * Sets up the fixture, for example, open a network connection. This method
  424. * is called before a test is executed.
  425. */
  426. @Override
  427. protected void setUp() {
  428. tm = new TreeMap();
  429. for (int i = 0; i < objArray.length; i++) {
  430. Object x = objArray[i] = new Integer(i);
  431. tm.put(x.toString(), x);
  432. }
  433. }
  434. }