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

/luni/src/test/api/common/org/apache/harmony/luni/tests/java/util/TreeMapTest.java

https://bitbucket.org/generalplus/android_external_apache-harmony
Java | 2043 lines | 1570 code | 162 blank | 311 comment | 90 complexity | f492579f2282a5326f62229fabb46ccc 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 org.apache.harmony.luni.tests.java.util;
  18. import java.io.BufferedInputStream;
  19. import java.io.BufferedOutputStream;
  20. import java.io.ByteArrayInputStream;
  21. import java.io.ByteArrayOutputStream;
  22. import java.io.ObjectInputStream;
  23. import java.io.ObjectOutputStream;
  24. import java.io.Serializable;
  25. import java.text.CollationKey;
  26. import java.text.Collator;
  27. import java.util.AbstractMap;
  28. import java.util.Arrays;
  29. import java.util.BitSet;
  30. import java.util.Collection;
  31. import java.util.Comparator;
  32. import java.util.HashMap;
  33. import java.util.Iterator;
  34. import java.util.LinkedList;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.NavigableMap;
  38. import java.util.NavigableSet;
  39. import java.util.NoSuchElementException;
  40. import java.util.Random;
  41. import java.util.Set;
  42. import java.util.SortedMap;
  43. import java.util.TreeMap;
  44. import java.util.Map.Entry;
  45. import org.apache.harmony.testframework.serialization.SerializationTest;
  46. import tests.support.Support_MapTest2;
  47. import tests.support.Support_UnmodifiableCollectionTest;
  48. public class TreeMapTest extends junit.framework.TestCase {
  49. public static class ReversedComparator implements Comparator {
  50. public int compare(Object o1, Object o2) {
  51. return -(((Comparable) o1).compareTo(o2));
  52. }
  53. public boolean equals(Object o1, Object o2) {
  54. return (((Comparable) o1).compareTo(o2)) == 0;
  55. }
  56. }
  57. // Regression for Harmony-1026
  58. public static class MockComparator<T extends Comparable<T>> implements
  59. Comparator<T>, Serializable {
  60. public int compare(T o1, T o2) {
  61. if (o1 == o2) {
  62. return 0;
  63. }
  64. if (null == o1 || null == o2) {
  65. return -1;
  66. }
  67. T c1 = o1;
  68. T c2 = o2;
  69. return c1.compareTo(c2);
  70. }
  71. }
  72. // Regression for Harmony-1161
  73. class MockComparatorNullTolerable implements Comparator<String> {
  74. public int compare(String o1, String o2) {
  75. if (o1 == o2) {
  76. return 0;
  77. }
  78. if (null == o1) {
  79. return -1;
  80. }
  81. if (null == o2) { // comparator should be symmetric
  82. return 1;
  83. }
  84. return o1.compareTo(o2);
  85. }
  86. }
  87. TreeMap tm;
  88. Object objArray[] = new Object[1000];
  89. /**
  90. * @tests java.util.TreeMap#TreeMap()
  91. */
  92. public void test_Constructor() {
  93. // Test for method java.util.TreeMap()
  94. new Support_MapTest2(new TreeMap()).runTest();
  95. assertTrue("New treeMap non-empty", new TreeMap().isEmpty());
  96. }
  97. /**
  98. * @tests java.util.TreeMap#TreeMap(java.util.Comparator)
  99. */
  100. public void test_ConstructorLjava_util_Comparator() {
  101. // Test for method java.util.TreeMap(java.util.Comparator)
  102. Comparator comp = new ReversedComparator();
  103. TreeMap reversedTreeMap = new TreeMap(comp);
  104. assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
  105. .comparator() == comp);
  106. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  107. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  108. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  109. reversedTreeMap.firstKey().equals(new Integer(2).toString()));
  110. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  111. reversedTreeMap.lastKey().equals(new Integer(1).toString()));
  112. }
  113. /**
  114. * @tests java.util.TreeMap#TreeMap(java.util.Map)
  115. */
  116. public void test_ConstructorLjava_util_Map() {
  117. // Test for method java.util.TreeMap(java.util.Map)
  118. TreeMap myTreeMap = new TreeMap(new HashMap(tm));
  119. assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length);
  120. for (Object element : objArray) {
  121. assertTrue("Map has incorrect mappings", myTreeMap.get(
  122. element.toString()).equals(element));
  123. }
  124. }
  125. /**
  126. * @tests java.util.TreeMap#TreeMap(java.util.SortedMap)
  127. */
  128. public void test_ConstructorLjava_util_SortedMap() {
  129. // Test for method java.util.TreeMap(java.util.SortedMap)
  130. Comparator comp = new ReversedComparator();
  131. TreeMap reversedTreeMap = new TreeMap(comp);
  132. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  133. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  134. TreeMap anotherTreeMap = new TreeMap(reversedTreeMap);
  135. assertTrue("New tree map does not answer correct comparator",
  136. anotherTreeMap.comparator() == comp);
  137. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  138. anotherTreeMap.firstKey().equals(new Integer(2).toString()));
  139. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  140. anotherTreeMap.lastKey().equals(new Integer(1).toString()));
  141. }
  142. /**
  143. * @tests java.util.TreeMap#clear()
  144. */
  145. public void test_clear() {
  146. // Test for method void java.util.TreeMap.clear()
  147. tm.clear();
  148. assertEquals("Cleared map returned non-zero size", 0, tm.size());
  149. }
  150. /**
  151. * @tests java.util.TreeMap#clone()
  152. */
  153. public void test_clone() {
  154. // Test for method java.lang.Object java.util.TreeMap.clone()
  155. TreeMap clonedMap = (TreeMap) tm.clone();
  156. assertTrue("Cloned map does not equal the original map", clonedMap
  157. .equals(tm));
  158. assertTrue("Cloned map is the same reference as the original map",
  159. clonedMap != tm);
  160. for (Object element : objArray) {
  161. assertTrue("Cloned map contains incorrect elements", clonedMap
  162. .get(element.toString()) == tm.get(element.toString()));
  163. }
  164. TreeMap map = new TreeMap();
  165. map.put("key", "value");
  166. // get the keySet() and values() on the original Map
  167. Set keys = map.keySet();
  168. Collection values = map.values();
  169. assertEquals("values() does not work", "value", values.iterator()
  170. .next());
  171. assertEquals("keySet() does not work", "key", keys.iterator().next());
  172. AbstractMap map2 = (AbstractMap) map.clone();
  173. map2.put("key", "value2");
  174. Collection values2 = map2.values();
  175. assertTrue("values() is identical", values2 != values);
  176. // values() and keySet() on the cloned() map should be different
  177. assertEquals("values() was not cloned", "value2", values2.iterator()
  178. .next());
  179. map2.clear();
  180. map2.put("key2", "value3");
  181. Set key2 = map2.keySet();
  182. assertTrue("keySet() is identical", key2 != keys);
  183. assertEquals("keySet() was not cloned", "key2", key2.iterator().next());
  184. }
  185. /**
  186. * @tests java.util.TreeMap#comparator()
  187. */
  188. public void test_comparator() {
  189. // Test for method java.util.Comparator java.util.TreeMap.comparator()\
  190. Comparator comp = new ReversedComparator();
  191. TreeMap reversedTreeMap = new TreeMap(comp);
  192. assertTrue("TreeMap answered incorrect comparator", reversedTreeMap
  193. .comparator() == comp);
  194. reversedTreeMap.put(new Integer(1).toString(), new Integer(1));
  195. reversedTreeMap.put(new Integer(2).toString(), new Integer(2));
  196. assertTrue("TreeMap does not use comparator (firstKey was incorrect)",
  197. reversedTreeMap.firstKey().equals(new Integer(2).toString()));
  198. assertTrue("TreeMap does not use comparator (lastKey was incorrect)",
  199. reversedTreeMap.lastKey().equals(new Integer(1).toString()));
  200. }
  201. /**
  202. * @tests java.util.TreeMap#containsKey(java.lang.Object)
  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. /**
  211. * @tests java.util.TreeMap#containsValue(java.lang.Object)
  212. */
  213. public void test_containsValueLjava_lang_Object() {
  214. // Test for method boolean
  215. // java.util.TreeMap.containsValue(java.lang.Object)
  216. assertTrue("Returned false for valid value", tm
  217. .containsValue(objArray[986]));
  218. assertTrue("Returned true for invalid value", !tm
  219. .containsValue(new Object()));
  220. }
  221. /**
  222. * @tests java.util.TreeMap#entrySet()
  223. */
  224. public void test_entrySet() {
  225. // Test for method java.util.Set java.util.TreeMap.entrySet()
  226. Set anEntrySet = tm.entrySet();
  227. Iterator entrySetIterator = anEntrySet.iterator();
  228. assertTrue("EntrySet is incorrect size",
  229. anEntrySet.size() == objArray.length);
  230. Map.Entry entry;
  231. while (entrySetIterator.hasNext()) {
  232. entry = (Map.Entry) entrySetIterator.next();
  233. assertTrue("EntrySet does not contain correct mappings", tm
  234. .get(entry.getKey()) == entry.getValue());
  235. }
  236. }
  237. /**
  238. * @tests java.util.TreeMap#firstKey()
  239. */
  240. public void test_firstKey() {
  241. // Test for method java.lang.Object java.util.TreeMap.firstKey()
  242. assertEquals("Returned incorrect first key", "0", tm.firstKey());
  243. }
  244. /**
  245. * @tests java.util.TreeMap#get(java.lang.Object)
  246. */
  247. public void test_getLjava_lang_Object() {
  248. // Test for method java.lang.Object
  249. // java.util.TreeMap.get(java.lang.Object)
  250. Object o = new Object();
  251. tm.put("Hello", o);
  252. assertTrue("Failed to get mapping", tm.get("Hello") == o);
  253. // Test for the same key & same value
  254. tm = new TreeMap();
  255. Object o2 = new Object();
  256. Integer key1 = 1;
  257. Integer key2 = 2;
  258. assertNull(tm.put(key1, o));
  259. assertNull(tm.put(key2, o));
  260. assertEquals(2, tm.values().size());
  261. assertEquals(2, tm.keySet().size());
  262. assertSame(tm.get(key1), tm.get(key2));
  263. assertSame(o, tm.put(key1, o2));
  264. assertSame(o2, tm.get(key1));
  265. }
  266. /**
  267. * @tests java.util.TreeMap#headMap(java.lang.Object)
  268. */
  269. public void test_headMapLjava_lang_Object() {
  270. // Test for method java.util.SortedMap
  271. // java.util.TreeMap.headMap(java.lang.Object)
  272. Map head = tm.headMap("100");
  273. assertEquals("Returned map of incorrect size", 3, head.size());
  274. assertTrue("Returned incorrect elements", head.containsKey("0")
  275. && head.containsValue(new Integer("1"))
  276. && head.containsKey("10"));
  277. // Regression for Harmony-1026
  278. TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(
  279. new MockComparator());
  280. map.put(1, 2.1);
  281. map.put(2, 3.1);
  282. map.put(3, 4.5);
  283. map.put(7, 21.3);
  284. map.put(null, null);
  285. SortedMap<Integer, Double> smap = map.headMap(null);
  286. assertEquals(0, smap.size());
  287. Set<Integer> keySet = smap.keySet();
  288. assertEquals(0, keySet.size());
  289. Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet();
  290. assertEquals(0, entrySet.size());
  291. Collection<Double> valueCollection = smap.values();
  292. assertEquals(0, valueCollection.size());
  293. // Regression for Harmony-1066
  294. assertTrue(head instanceof Serializable);
  295. // Regression for ill-behaved collator
  296. Collator c = new Collator() {
  297. @Override
  298. public int compare(String o1, String o2) {
  299. if (o1 == null) {
  300. return 0;
  301. }
  302. return o1.compareTo(o2);
  303. }
  304. @Override
  305. public CollationKey getCollationKey(String string) {
  306. return null;
  307. }
  308. @Override
  309. public int hashCode() {
  310. return 0;
  311. }
  312. };
  313. TreeMap<String, String> treemap = new TreeMap<String, String>(c);
  314. assertEquals(0, treemap.headMap(null).size());
  315. treemap = new TreeMap();
  316. SortedMap<String, String> headMap = treemap.headMap("100");
  317. headMap.headMap("100");
  318. SortedMap<Integer,Integer> intMap,sub;
  319. int size = 16;
  320. intMap = new TreeMap<Integer,Integer>();
  321. for(int i=0; i<size; i++) {
  322. intMap.put(i,i);
  323. }
  324. sub = intMap.headMap(-1);
  325. assertEquals("size should be zero",sub.size(),0);
  326. assertTrue("submap should be empty",sub.isEmpty());
  327. try{
  328. sub.firstKey();
  329. fail("java.util.NoSuchElementException should be thrown");
  330. } catch(java.util.NoSuchElementException e) {
  331. }
  332. TreeMap t = new TreeMap();
  333. try {
  334. SortedMap th = t.headMap(null);
  335. fail("Should throw a NullPointerException");
  336. } catch( NullPointerException npe) {
  337. // expected
  338. }
  339. try{
  340. sub.lastKey();
  341. fail("java.util.NoSuchElementException should be thrown");
  342. } catch(java.util.NoSuchElementException e) {
  343. }
  344. size = 256;
  345. intMap = new TreeMap<Integer,Integer>();
  346. for(int i=0; i<size; i++) {
  347. intMap.put(i,i);
  348. }
  349. sub = intMap.headMap(-1);
  350. assertEquals("size should be zero",sub.size(),0);
  351. assertTrue("submap should be empty",sub.isEmpty());
  352. try{
  353. sub.firstKey();
  354. fail("java.util.NoSuchElementException should be thrown");
  355. } catch(java.util.NoSuchElementException e) {
  356. }
  357. try{
  358. sub.lastKey();
  359. fail("java.util.NoSuchElementException should be thrown");
  360. } catch(java.util.NoSuchElementException e) {
  361. }
  362. }
  363. /**
  364. * @tests java.util.TreeMap#keySet()
  365. */
  366. public void test_keySet() {
  367. // Test for method java.util.Set java.util.TreeMap.keySet()
  368. Set ks = tm.keySet();
  369. assertTrue("Returned set of incorrect size",
  370. ks.size() == objArray.length);
  371. for (int i = 0; i < tm.size(); i++) {
  372. assertTrue("Returned set is missing keys", ks.contains(new Integer(
  373. i).toString()));
  374. }
  375. }
  376. /**
  377. * @tests java.util.TreeMap#lastKey()
  378. */
  379. public void test_lastKey() {
  380. // Test for method java.lang.Object java.util.TreeMap.lastKey()
  381. assertTrue("Returned incorrect last key", tm.lastKey().equals(
  382. objArray[objArray.length - 1].toString()));
  383. assertNotSame(objArray[objArray.length - 1].toString(), tm.lastKey());
  384. assertEquals(objArray[objArray.length - 2].toString(), tm
  385. .headMap("999").lastKey());
  386. assertEquals(objArray[objArray.length - 1].toString(), tm
  387. .tailMap("123").lastKey());
  388. assertEquals(objArray[objArray.length - 2].toString(), tm.subMap("99",
  389. "999").lastKey());
  390. }
  391. public void test_lastKey_after_subMap() {
  392. TreeMap<String, String> tm = new TreeMap<String, String>();
  393. tm.put("001", "VAL001");
  394. tm.put("003", "VAL003");
  395. tm.put("002", "VAL002");
  396. SortedMap<String, String> sm = tm;
  397. String firstKey = (String) sm.firstKey();
  398. String lastKey="";
  399. for (int i = 1; i <= tm.size(); i++) {
  400. try{
  401. lastKey = (String) sm.lastKey();
  402. }
  403. catch(NoSuchElementException excep){
  404. fail("NoSuchElementException thrown when there are elements in the map");
  405. }
  406. sm = sm.subMap(firstKey, lastKey);
  407. }
  408. }
  409. /**
  410. * @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object)
  411. */
  412. public void test_putLjava_lang_ObjectLjava_lang_Object() {
  413. // Test for method java.lang.Object
  414. // java.util.TreeMap.put(java.lang.Object, java.lang.Object)
  415. Object o = new Object();
  416. tm.put("Hello", o);
  417. assertTrue("Failed to put mapping", tm.get("Hello") == o);
  418. // regression for Harmony-780
  419. tm = new TreeMap();
  420. assertNull(tm.put(new Object(), new Object()));
  421. try {
  422. tm.put(new Integer(1), new Object());
  423. fail("should throw ClassCastException");
  424. } catch (ClassCastException e) {
  425. // expected
  426. }
  427. tm = new TreeMap();
  428. assertNull(tm.put(new Integer(1), new Object()));
  429. try {
  430. tm.put(new Object(), new Object());
  431. fail("Should throw a ClassCastException");
  432. } catch (ClassCastException e) {
  433. // expected
  434. }
  435. // regression for Harmony-2474
  436. // but RI6 changes its behavior
  437. // so the test changes too
  438. tm = new TreeMap();
  439. try {
  440. tm.remove(o);
  441. fail("should throw ClassCastException");
  442. } catch (ClassCastException e) {
  443. //expected
  444. }
  445. }
  446. /**
  447. * @tests java.util.TreeMap#putAll(java.util.Map)
  448. */
  449. public void test_putAllLjava_util_Map() {
  450. // Test for method void java.util.TreeMap.putAll(java.util.Map)
  451. TreeMap x = new TreeMap();
  452. x.putAll(tm);
  453. assertTrue("Map incorrect size after put", x.size() == tm.size());
  454. for (Object element : objArray) {
  455. assertTrue("Failed to put all elements", x.get(element.toString())
  456. .equals(element));
  457. }
  458. }
  459. /**
  460. * @tests java.util.TreeMap#remove(java.lang.Object)
  461. */
  462. public void test_removeLjava_lang_Object() {
  463. // Test for method java.lang.Object
  464. // java.util.TreeMap.remove(java.lang.Object)
  465. tm.remove("990");
  466. assertTrue("Failed to remove mapping", !tm.containsKey("990"));
  467. }
  468. /**
  469. * @tests java.util.TreeMap#size()
  470. */
  471. public void test_size() {
  472. // Test for method int java.util.TreeMap.size()
  473. assertEquals("Returned incorrect size", 1000, tm.size());
  474. assertEquals("Returned incorrect size", 447, tm.headMap("500").size());
  475. assertEquals("Returned incorrect size", 1000, tm.headMap("null").size());
  476. assertEquals("Returned incorrect size", 0, tm.headMap("").size());
  477. assertEquals("Returned incorrect size", 448, tm.headMap("500a").size());
  478. assertEquals("Returned incorrect size", 553, tm.tailMap("500").size());
  479. assertEquals("Returned incorrect size", 0, tm.tailMap("null").size());
  480. assertEquals("Returned incorrect size", 1000, tm.tailMap("").size());
  481. assertEquals("Returned incorrect size", 552, tm.tailMap("500a").size());
  482. assertEquals("Returned incorrect size", 111, tm.subMap("500", "600")
  483. .size());
  484. try {
  485. tm.subMap("null", "600");
  486. fail("Should throw an IllegalArgumentException");
  487. } catch (IllegalArgumentException e) {
  488. // expected
  489. }
  490. assertEquals("Returned incorrect size", 1000, tm.subMap("", "null")
  491. .size());
  492. }
  493. /**
  494. * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
  495. */
  496. public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
  497. // Test for method java.util.SortedMap
  498. // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
  499. SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
  500. .toString());
  501. assertEquals("subMap is of incorrect size", 9, subMap.size());
  502. for (int counter = 100; counter < 109; counter++) {
  503. assertTrue("SubMap contains incorrect elements", subMap.get(
  504. objArray[counter].toString()).equals(objArray[counter]));
  505. }
  506. try {
  507. tm.subMap(objArray[9].toString(), objArray[1].toString());
  508. fail("end key less than start key should throw IllegalArgumentException");
  509. } catch (IllegalArgumentException e) {
  510. // Expected
  511. }
  512. // Regression for Harmony-1161
  513. TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
  514. new MockComparatorNullTolerable());
  515. treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
  516. treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
  517. SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
  518. "key1"); //$NON-NLS-1$
  519. assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
  520. // Regression test for typo in lastKey method
  521. SortedMap<String, String> map = new TreeMap<String, String>();
  522. map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
  523. map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
  524. map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
  525. assertEquals("3", map.lastKey());
  526. SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$
  527. assertEquals("2", sub.lastKey()); //$NON-NLS-1$
  528. TreeMap t = new TreeMap();
  529. try {
  530. SortedMap th = t.subMap(null,new Object());
  531. fail("Should throw a NullPointerException");
  532. } catch( NullPointerException npe) {
  533. // expected
  534. }
  535. }
  536. /**
  537. * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
  538. */
  539. public void test_subMap_Iterator() {
  540. TreeMap<String, String> map = new TreeMap<String, String>();
  541. String[] keys = { "1", "2", "3" };
  542. String[] values = { "one", "two", "three" };
  543. for (int i = 0; i < keys.length; i++) {
  544. map.put(keys[i], values[i]);
  545. }
  546. assertEquals(3, map.size());
  547. Map subMap = map.subMap("", "test");
  548. assertEquals(3, subMap.size());
  549. Set entrySet = subMap.entrySet();
  550. Iterator iter = entrySet.iterator();
  551. int size = 0;
  552. while (iter.hasNext()) {
  553. Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
  554. .next();
  555. assertTrue(map.containsKey(entry.getKey()));
  556. assertTrue(map.containsValue(entry.getValue()));
  557. size++;
  558. }
  559. assertEquals(map.size(), size);
  560. Set<String> keySet = subMap.keySet();
  561. iter = keySet.iterator();
  562. size = 0;
  563. while (iter.hasNext()) {
  564. String key = (String) iter.next();
  565. assertTrue(map.containsKey(key));
  566. size++;
  567. }
  568. assertEquals(map.size(), size);
  569. }
  570. /**
  571. * @tests java.util.TreeMap#tailMap(java.lang.Object)
  572. */
  573. public void test_tailMapLjava_lang_Object() {
  574. // Test for method java.util.SortedMap
  575. // java.util.TreeMap.tailMap(java.lang.Object)
  576. Map tail = tm.tailMap(objArray[900].toString());
  577. assertTrue("Returned map of incorrect size : " + tail.size(), tail
  578. .size() == (objArray.length - 900) + 9);
  579. for (int i = 900; i < objArray.length; i++) {
  580. assertTrue("Map contains incorrect entries", tail
  581. .containsValue(objArray[i]));
  582. }
  583. // Regression for Harmony-1066
  584. assertTrue(tail instanceof Serializable);
  585. SortedMap<Integer,Integer> intMap,sub;
  586. int size = 16;
  587. intMap = new TreeMap<Integer,Integer>();
  588. for(int i=0; i<size; i++) {
  589. intMap.put(i,i);
  590. }
  591. sub = intMap.tailMap(size);
  592. assertEquals("size should be zero",sub.size(),0);
  593. assertTrue("submap should be empty",sub.isEmpty());
  594. try{
  595. sub.firstKey();
  596. fail("java.util.NoSuchElementException should be thrown");
  597. } catch(java.util.NoSuchElementException e) {
  598. }
  599. TreeMap t = new TreeMap();
  600. try {
  601. SortedMap th = t.tailMap(null);
  602. fail("Should throw a NullPointerException");
  603. } catch( NullPointerException npe) {
  604. // expected
  605. }
  606. try{
  607. sub.lastKey();
  608. fail("java.util.NoSuchElementException should be thrown");
  609. } catch(java.util.NoSuchElementException e) {
  610. }
  611. size = 256;
  612. intMap = new TreeMap<Integer,Integer>();
  613. for(int i=0; i<size; i++) {
  614. intMap.put(i,i);
  615. }
  616. sub = intMap.tailMap(size);
  617. assertEquals("size should be zero",sub.size(),0);
  618. assertTrue("submap should be empty",sub.isEmpty());
  619. try{
  620. sub.firstKey();
  621. fail("java.util.NoSuchElementException should be thrown");
  622. } catch(java.util.NoSuchElementException e) {
  623. }
  624. try{
  625. sub.lastKey();
  626. fail("java.util.NoSuchElementException should be thrown");
  627. } catch(java.util.NoSuchElementException e) {
  628. }
  629. }
  630. /**
  631. * @tests java.util.TreeMap#values()
  632. */
  633. public void test_values() {
  634. // Test for method java.util.Collection java.util.TreeMap.values()
  635. Collection vals = tm.values();
  636. vals.iterator();
  637. assertTrue("Returned collection of incorrect size",
  638. vals.size() == objArray.length);
  639. for (Object element : objArray) {
  640. assertTrue("Collection contains incorrect elements", vals
  641. .contains(element));
  642. }
  643. assertEquals(1000, vals.size());
  644. int j = 0;
  645. for (Iterator iter = vals.iterator(); iter.hasNext();) {
  646. Object element = (Object) iter.next();
  647. j++;
  648. }
  649. assertEquals(1000, j);
  650. vals = tm.descendingMap().values();
  651. vals.iterator();
  652. assertTrue("Returned collection of incorrect size",
  653. vals.size() == objArray.length);
  654. for (Object element : objArray) {
  655. assertTrue("Collection contains incorrect elements", vals
  656. .contains(element));
  657. }
  658. assertEquals(1000, vals.size());
  659. j = 0;
  660. for (Iterator iter = vals.iterator(); iter.hasNext();) {
  661. Object element = (Object) iter.next();
  662. j++;
  663. }
  664. assertEquals(1000, j);
  665. TreeMap myTreeMap = new TreeMap();
  666. for (int i = 0; i < 100; i++) {
  667. myTreeMap.put(objArray[i], objArray[i]);
  668. }
  669. Collection values = myTreeMap.values();
  670. new Support_UnmodifiableCollectionTest(
  671. "Test Returned Collection From TreeMap.values()", values)
  672. .runTest();
  673. values.remove(new Integer(0));
  674. assertTrue(
  675. "Removing from the values collection should remove from the original map",
  676. !myTreeMap.containsValue(new Integer(0)));
  677. assertEquals(99, values.size());
  678. j = 0;
  679. for (Iterator iter = values.iterator(); iter.hasNext();) {
  680. Object element = (Object) iter.next();
  681. j++;
  682. }
  683. assertEquals(99, j);
  684. }
  685. /**
  686. * @tests java.util.TreeMap the values() method in sub maps
  687. */
  688. public void test_subMap_values_size() {
  689. TreeMap myTreeMap = new TreeMap();
  690. for (int i = 0; i < 1000; i++) {
  691. myTreeMap.put(i, objArray[i]);
  692. }
  693. // Test for method values() in subMaps
  694. Collection vals = myTreeMap.subMap(200, 400).values();
  695. assertTrue("Returned collection of incorrect size", vals.size() == 200);
  696. for (int i = 200; i < 400; i++) {
  697. assertTrue("Collection contains incorrect elements" + i, vals
  698. .contains(objArray[i]));
  699. }
  700. assertEquals(200,vals.toArray().length);
  701. vals.remove(objArray[300]);
  702. assertTrue(
  703. "Removing from the values collection should remove from the original map",
  704. !myTreeMap.containsValue(objArray[300]));
  705. assertTrue("Returned collection of incorrect size", vals.size() == 199);
  706. assertEquals(199,vals.toArray().length);
  707. myTreeMap.put(300, objArray[300]);
  708. // Test for method values() in subMaps
  709. vals = myTreeMap.headMap(400).values();
  710. assertEquals("Returned collection of incorrect size", vals.size(), 400);
  711. for (int i = 0; i < 400; i++) {
  712. assertTrue("Collection contains incorrect elements "+i, vals
  713. .contains(objArray[i]));
  714. }
  715. assertEquals(400,vals.toArray().length);
  716. vals.remove(objArray[300]);
  717. assertTrue(
  718. "Removing from the values collection should remove from the original map",
  719. !myTreeMap.containsValue(objArray[300]));
  720. assertTrue("Returned collection of incorrect size", vals.size() == 399);
  721. assertEquals(399,vals.toArray().length);
  722. myTreeMap.put(300, objArray[300]);
  723. // Test for method values() in subMaps
  724. vals = myTreeMap.tailMap(400).values();
  725. assertEquals("Returned collection of incorrect size", vals.size(), 600);
  726. for (int i = 400; i < 1000; i++) {
  727. assertTrue("Collection contains incorrect elements "+i, vals
  728. .contains(objArray[i]));
  729. }
  730. assertEquals(600,vals.toArray().length);
  731. vals.remove(objArray[600]);
  732. assertTrue(
  733. "Removing from the values collection should remove from the original map",
  734. !myTreeMap.containsValue(objArray[600]));
  735. assertTrue("Returned collection of incorrect size", vals.size() == 599);
  736. assertEquals(599,vals.toArray().length);
  737. myTreeMap.put(600, objArray[600]);
  738. // Test for method values() in subMaps
  739. vals = myTreeMap.descendingMap().headMap(400).values();
  740. assertEquals("Returned collection of incorrect size", vals.size(), 599);
  741. for (int i = 401; i < 1000; i++) {
  742. assertTrue("Collection contains incorrect elements "+i, vals
  743. .contains(objArray[i]));
  744. }
  745. assertEquals(599,vals.toArray().length);
  746. vals.remove(objArray[600]);
  747. assertTrue(
  748. "Removing from the values collection should remove from the original map",
  749. !myTreeMap.containsValue(objArray[600]));
  750. assertTrue("Returned collection of incorrect size", vals.size() == 598);
  751. assertEquals(598,vals.toArray().length);
  752. myTreeMap.put(600, objArray[600]);
  753. // Test for method values() in subMaps
  754. vals = myTreeMap.descendingMap().tailMap(400).values();
  755. assertEquals("Returned collection of incorrect size", vals.size(), 401);
  756. for (int i = 0; i <= 400; i++) {
  757. assertTrue("Collection contains incorrect elements "+i, vals
  758. .contains(objArray[i]));
  759. }
  760. assertEquals(401,vals.toArray().length);
  761. vals.remove(objArray[300]);
  762. assertTrue(
  763. "Removing from the values collection should remove from the original map",
  764. !myTreeMap.containsValue(objArray[300]));
  765. assertTrue("Returned collection of incorrect size", vals.size() == 400);
  766. assertEquals(400,vals.toArray().length);
  767. }
  768. /**
  769. * @tests java.util.TreeMap#subMap()
  770. */
  771. public void test_subMap_Iterator2() {
  772. TreeMap<String, String> map = new TreeMap<String, String>();
  773. String[] keys = { "1", "2", "3" };
  774. String[] values = { "one", "two", "three" };
  775. for (int i = 0; i < keys.length; i++) {
  776. map.put(keys[i], values[i]);
  777. }
  778. assertEquals(3, map.size());
  779. Map subMap = map.subMap("", "test");
  780. assertEquals(3, subMap.size());
  781. Set entrySet = subMap.entrySet();
  782. Iterator iter = entrySet.iterator();
  783. int size = 0;
  784. while (iter.hasNext()) {
  785. Map.Entry<String, String> entry = (Map.Entry<String, String>) iter
  786. .next();
  787. assertTrue(map.containsKey(entry.getKey()));
  788. assertTrue(map.containsValue(entry.getValue()));
  789. size++;
  790. }
  791. assertEquals(map.size(), size);
  792. Set<String> keySet = subMap.keySet();
  793. iter = keySet.iterator();
  794. size = 0;
  795. while (iter.hasNext()) {
  796. String key = (String) iter.next();
  797. assertTrue(map.containsKey(key));
  798. size++;
  799. }
  800. assertEquals(map.size(), size);
  801. }
  802. /**
  803. * @tests java.util.TreeMap#SerializationTest()
  804. */
  805. // Regression for Harmony-1066
  806. public void test_SubMap_Serializable() throws Exception {
  807. TreeMap<Integer, Double> map = new TreeMap<Integer, Double>();
  808. map.put(1, 2.1);
  809. map.put(2, 3.1);
  810. map.put(3, 4.5);
  811. map.put(7, 21.3);
  812. SortedMap<Integer, Double> headMap = map.headMap(3);
  813. assertTrue(headMap instanceof Serializable);
  814. assertFalse(headMap instanceof TreeMap);
  815. assertTrue(headMap instanceof SortedMap);
  816. assertFalse(headMap.entrySet() instanceof Serializable);
  817. assertFalse(headMap.keySet() instanceof Serializable);
  818. assertFalse(headMap.values() instanceof Serializable);
  819. // This assertion will fail on RI. This is a bug of RI.
  820. SerializationTest.verifySelf(headMap);
  821. }
  822. /**
  823. * @tests {@link java.util.TreeMap#firstEntry()}
  824. */
  825. public void test_firstEntry() throws Exception {
  826. Integer testint = new Integer(-1);
  827. Integer testint10000 = new Integer(-10000);
  828. Integer testint9999 = new Integer(-9999);
  829. assertEquals(objArray[0].toString(), tm.firstEntry().getKey());
  830. assertEquals(objArray[0], tm.firstEntry().getValue());
  831. tm.put(testint.toString(), testint);
  832. assertEquals(testint.toString(), tm.firstEntry().getKey());
  833. assertEquals(testint, tm.firstEntry().getValue());
  834. tm.put(testint10000.toString(), testint10000);
  835. assertEquals(testint.toString(), tm.firstEntry().getKey());
  836. assertEquals(testint, tm.firstEntry().getValue());
  837. tm.put(testint9999.toString(), testint9999);
  838. assertEquals(testint.toString(), tm.firstEntry().getKey());
  839. Entry entry = tm.firstEntry();
  840. assertEquals(testint, entry.getValue());
  841. assertEntry(entry);
  842. tm.clear();
  843. assertNull(tm.firstEntry());
  844. }
  845. /**
  846. * @tests {@link java.util.TreeMap#lastEntry()
  847. */
  848. public void test_lastEntry() throws Exception {
  849. Integer testint10000 = new Integer(10000);
  850. Integer testint9999 = new Integer(9999);
  851. assertEquals(objArray[999].toString(), tm.lastEntry().getKey());
  852. assertEquals(objArray[999], tm.lastEntry().getValue());
  853. tm.put(testint10000.toString(), testint10000);
  854. assertEquals(objArray[999].toString(), tm.lastEntry().getKey());
  855. assertEquals(objArray[999], tm.lastEntry().getValue());
  856. tm.put(testint9999.toString(), testint9999);
  857. assertEquals(testint9999.toString(), tm.lastEntry().getKey());
  858. Entry entry = tm.lastEntry();
  859. assertEquals(testint9999, entry.getValue());
  860. assertEntry(entry);
  861. tm.clear();
  862. assertNull(tm.lastEntry());
  863. }
  864. /**
  865. * @tests {@link java.util.TreeMap#pollFirstEntry()
  866. */
  867. public void test_pollFirstEntry() throws Exception {
  868. Integer testint = new Integer(-1);
  869. Integer testint10000 = new Integer(-10000);
  870. Integer testint9999 = new Integer(-9999);
  871. assertEquals(objArray[0].toString(), tm.pollFirstEntry().getKey());
  872. assertEquals(objArray[1], tm.pollFirstEntry().getValue());
  873. assertEquals(objArray[10], tm.pollFirstEntry().getValue());
  874. tm.put(testint.toString(), testint);
  875. tm.put(testint10000.toString(), testint10000);
  876. assertEquals(testint.toString(), tm.pollFirstEntry().getKey());
  877. assertEquals(testint10000, tm.pollFirstEntry().getValue());
  878. tm.put(testint9999.toString(), testint9999);
  879. assertEquals(testint9999.toString(), tm.pollFirstEntry().getKey());
  880. Entry entry = tm.pollFirstEntry();
  881. assertEntry(entry);
  882. assertEquals(objArray[100], entry.getValue());
  883. tm.clear();
  884. assertNull(tm.pollFirstEntry());
  885. }
  886. /**
  887. * @tests {@link java.util.TreeMap#pollLastEntry()
  888. */
  889. public void test_pollLastEntry() throws Exception {
  890. Integer testint10000 = new Integer(10000);
  891. Integer testint9999 = new Integer(9999);
  892. assertEquals(objArray[999].toString(), tm.pollLastEntry().getKey());
  893. assertEquals(objArray[998], tm.pollLastEntry().getValue());
  894. assertEquals(objArray[997], tm.pollLastEntry().getValue());
  895. tm.put(testint10000.toString(), testint10000);
  896. assertEquals(objArray[996], tm.pollLastEntry().getValue());
  897. tm.put(testint9999.toString(), testint9999);
  898. assertEquals(testint9999.toString(), tm.pollLastEntry().getKey());
  899. Entry entry = tm.pollLastEntry();
  900. assertEquals(objArray[995], entry.getValue());
  901. assertEntry(entry);
  902. tm.clear();
  903. assertNull(tm.pollLastEntry());
  904. }
  905. /**
  906. * @tests {@link java.util.TreeMap#lowerEntry(Object)
  907. */
  908. public void test_lowerEntry() throws Exception {
  909. Integer testint10000 = new Integer(10000);
  910. Integer testint9999 = new Integer(9999);
  911. assertEquals(objArray[999], tm.lowerEntry(testint9999.toString())
  912. .getValue());
  913. assertEquals(objArray[100], tm.lowerEntry(testint10000.toString())
  914. .getValue());
  915. tm.put(testint10000.toString(), testint10000);
  916. tm.put(testint9999.toString(), testint9999);
  917. assertEquals(objArray[999], tm.lowerEntry(testint9999.toString())
  918. .getValue());
  919. Entry entry = tm.lowerEntry(testint10000.toString());
  920. assertEquals(objArray[100], entry.getValue());
  921. assertEntry(entry);
  922. try {
  923. tm.lowerEntry(testint10000);
  924. fail("should throw ClassCastException");
  925. } catch (ClassCastException e) {
  926. // expected
  927. }
  928. try {
  929. tm.lowerEntry(null);
  930. fail("should throw NullPointerException");
  931. } catch (NullPointerException e) {
  932. // expected
  933. }
  934. tm.clear();
  935. assertNull(tm.lowerEntry(testint9999.toString()));
  936. assertNull(tm.lowerEntry(null));
  937. }
  938. /**
  939. * @tests {@link java.util.TreeMap#lowerKey(Object)
  940. */
  941. public void test_lowerKey() throws Exception {
  942. Integer testint10000 = new Integer(10000);
  943. Integer testint9999 = new Integer(9999);
  944. assertEquals(objArray[999].toString(), tm.lowerKey(testint9999
  945. .toString()));
  946. assertEquals(objArray[100].toString(), tm.lowerKey(testint10000
  947. .toString()));
  948. tm.put(testint10000.toString(), testint10000);
  949. tm.put(testint9999.toString(), testint9999);
  950. assertEquals(objArray[999].toString(), tm.lowerKey(testint9999
  951. .toString()));
  952. assertEquals(objArray[100].toString(), tm.lowerKey(testint10000
  953. .toString()));
  954. try {
  955. tm.lowerKey(testint10000);
  956. fail("should throw ClassCastException");
  957. } catch (ClassCastException e) {
  958. // expected
  959. }
  960. try {
  961. tm.lowerKey(null);
  962. fail("should throw NullPointerException");
  963. } catch (NullPointerException e) {
  964. // expected
  965. }
  966. tm.clear();
  967. assertNull(tm.lowerKey(testint9999.toString()));
  968. assertNull(tm.lowerKey(null));
  969. }
  970. /**
  971. * @tests {@link java.util.TreeMap#floorEntry(Object)
  972. */
  973. public void test_floorEntry() throws Exception {
  974. Integer testint10000 = new Integer(10000);
  975. Integer testint9999 = new Integer(9999);
  976. assertEquals(objArray[999], tm.floorEntry(testint9999.toString())
  977. .getValue());
  978. assertEquals(objArray[100], tm.floorEntry(testint10000.toString())
  979. .getValue());
  980. tm.put(testint10000.toString(), testint10000);
  981. tm.put(testint9999.toString(), testint9999);
  982. assertEquals(testint9999, tm.floorEntry(testint9999.toString())
  983. .getValue());
  984. Entry entry = tm.floorEntry(testint10000.toString());
  985. assertEquals(testint10000, entry.getValue());
  986. assertEntry(entry);
  987. try {
  988. tm.floorEntry(testint10000);
  989. fail("should throw ClassCastException");
  990. } catch (ClassCastException e) {
  991. // expected
  992. }
  993. try {
  994. tm.floorEntry(null);
  995. fail("should throw NullPointerException");
  996. } catch (NullPointerException e) {
  997. // expected
  998. }
  999. tm.clear();
  1000. assertNull(tm.floorEntry(testint9999.toString()));
  1001. assertNull(tm.floorEntry(null));
  1002. }
  1003. /**
  1004. * @tests {@link java.util.TreeMap#floorKey(Object)
  1005. */
  1006. public void test_floorKey() throws Exception {
  1007. Integer testint10000 = new Integer(10000);
  1008. Integer testint9999 = new Integer(9999);
  1009. assertEquals(objArray[999].toString(), tm.floorKey(testint9999
  1010. .toString()));
  1011. assertEquals(objArray[100].toString(), tm.floorKey(testint10000
  1012. .toString()));
  1013. tm.put(testint10000.toString(), testint10000);
  1014. tm.put(testint9999.toString(), testint9999);
  1015. assertEquals(testint9999.toString(), tm
  1016. .floorKey(testint9999.toString()));
  1017. assertEquals(testint10000.toString(), tm.floorKey(testint10000
  1018. .toString()));
  1019. try {
  1020. tm.floorKey(testint10000);
  1021. fail("should throw ClassCastException");
  1022. } catch (ClassCastException e) {
  1023. // expected
  1024. }
  1025. try {
  1026. tm.floorKey(null);
  1027. fail("should throw NullPointerException");
  1028. } catch (NullPointerException e) {
  1029. // expected
  1030. }
  1031. tm.clear();
  1032. assertNull(tm.floorKey(testint9999.toString()));
  1033. assertNull(tm.floorKey(null));
  1034. }
  1035. /**
  1036. * @tests {@link java.util.TreeMap#ceilingEntry(Object)
  1037. */
  1038. public void test_ceilingEntry() throws Exception {
  1039. Integer testint100 = new Integer(100);
  1040. Integer testint = new Integer(-1);
  1041. assertEquals(objArray[0], tm.ceilingEntry(testint.toString())
  1042. .getValue());
  1043. assertEquals(objArray[100], tm.ceilingEntry(testint100.toString())
  1044. .getValue());
  1045. tm.put(testint.toString(), testint);
  1046. tm.put(testint100.toString(), testint);
  1047. assertEquals(testint, tm.ceilingEntry(testint.toString()).getValue());
  1048. Entry entry = tm.ceilingEntry(testint100.toString());
  1049. assertEquals(testint, entry.getValue());
  1050. assertEntry(entry);
  1051. try {
  1052. tm.ceilingEntry(testint100);
  1053. fail("should throw ClassCastException");
  1054. } catch (ClassCastException e) {
  1055. // expected
  1056. }
  1057. try {
  1058. tm.ceilingEntry(null);
  1059. fail("should throw NullPointerException");
  1060. } catch (NullPointerException e) {
  1061. // expected
  1062. }
  1063. tm.clear();
  1064. assertNull(tm.ceilingEntry(testint.toString()));
  1065. assertNull(tm.ceilingEntry(null));
  1066. }
  1067. /**
  1068. * @tests {@link java.util.TreeMap#ceilingKey(Object)
  1069. */
  1070. public void test_ceilingKey() throws Exception {
  1071. Integer testint100 = new Integer(100);
  1072. Integer testint = new Integer(-1);
  1073. assertEquals(objArray[0].toString(), tm.ceilingKey(testint.toString()));
  1074. assertEquals(objArray[100].toString(), tm.ceilingKey(testint100
  1075. .toString()));
  1076. tm.put(testint.toString(), testint);
  1077. tm.put(testint100.toString(), testint);
  1078. assertEquals(testint.toString(), tm.ceilingKey(testint.toString()));
  1079. assertEquals(testint100.toString(), tm
  1080. .ceilingKey(testint100.toString()));
  1081. try {
  1082. tm.ceilingKey(testint100);
  1083. fail("should throw ClassCastException");
  1084. } catch (ClassCastException e) {
  1085. // expected
  1086. }
  1087. try {
  1088. tm.ceilingKey(null);
  1089. fail("should throw NullPointerException");
  1090. } catch (NullPointerException e) {
  1091. // expected
  1092. }
  1093. tm.clear();
  1094. assertNull(tm.ceilingKey(testint.toString()));
  1095. assertNull(tm.ceilingKey(null));
  1096. }
  1097. /**
  1098. * @tests {@link java.util.TreeMap#higherEntry(Object)
  1099. */
  1100. public void test_higherEntry() throws Exception {
  1101. Integer testint9999 = new Integer(9999);
  1102. Integer testint10000 = new Integer(10000);
  1103. Integer testint100 = new Integer(100);
  1104. Integer testint = new Integer(-1);
  1105. assertEquals(objArray[0], tm.higherEntry(testint.toString()).getValue());
  1106. assertEquals(objArray[101], tm.higherEntry(testint100.toString())
  1107. .getValue());
  1108. assertEquals(objArray[101], tm.higherEntry(testint10000.toString())
  1109. .getValue());
  1110. tm.put(testint9999.toString(), testint);
  1111. tm.put(testint100.toString(), testint);
  1112. tm.put(testint10000.toString(), testint);
  1113. assertEquals(objArray[0], tm.higherEntry(testint.toString()).getValue());
  1114. assertEquals(testint, tm.higherEntry(testint100.toString()).getValue());
  1115. Entry entry = tm.higherEntry(testint10000.toString());
  1116. assertEquals(objArray[101], entry.getValue());
  1117. assertEntry(entry);
  1118. assertNull(tm.higherEntry(testint9999.toString()));
  1119. try {
  1120. tm.higherEntry(testint100);
  1121. fail("should throw ClassCastException");
  1122. } catch (ClassCastException e) {
  1123. // expected
  1124. }
  1125. try {
  1126. tm.higherEntry(null);
  1127. fail("should throw NullPointerException");
  1128. } catch (NullPointerException e) {
  1129. // expected
  1130. }
  1131. tm.clear();
  1132. assertNull(tm.higherEntry(testint.toString()));
  1133. assertNull(tm.higherEntry(null));
  1134. }
  1135. /**
  1136. * @tests {@link java.util.TreeMap#higherKey(Object)
  1137. */
  1138. public void test_higherKey() throws Exception {
  1139. Integer testint9999 = new Integer(9999);
  1140. Integer testint10000 = new Integer(10000);
  1141. Integer testint100 = new Integer(100);
  1142. Integer testint = new Integer(-1);
  1143. assertEquals(objArray[0].toString(), tm.higherKey(testint.toString()));
  1144. assertEquals(objArray[101].toString(), tm.higherKey(testint100
  1145. .toString()));
  1146. assertEquals(objArray[101].toString(), tm.higherKey(testint10000
  1147. .toString()));
  1148. tm.put(testint9999.toString(), testint);
  1149. tm.put(testint100.toString(), testint);
  1150. tm.put(testint10000.toString(), testint);
  1151. assertEquals(objArray[0].toString(), tm.higherKey(testint.toString()));
  1152. assertEquals(testint10000.toString(), tm.higherKey(testint100
  1153. .toString()));
  1154. assertEquals(objArray[101].toString(), tm.higherKey(testint10000
  1155. .toString()));
  1156. assertNull(tm.higherKey(testint9999.toString()));
  1157. try {
  1158. tm.higherKey(testint100);
  1159. fail("should throw ClassCastException");
  1160. } catch (ClassCastException e) {
  1161. // expected
  1162. }
  1163. try {
  1164. tm.higherKey(null);
  1165. fail("should throw NullPointerException");
  1166. } catch (NullPointerException e) {
  1167. // expected
  1168. }
  1169. tm.clear();
  1170. assertNull(tm.higherKey(testint.toString()));
  1171. assertNull(tm.higherKey(null));
  1172. }
  1173. public void test_navigableKeySet() throws Exception {
  1174. Integer testint9999 = new Integer(9999);
  1175. Integer testint10000 = new Integer(10000);
  1176. Integer testint100 = new Integer(100);
  1177. Integer testint0 = new Integer(0);
  1178. NavigableSet set = tm.navigableKeySet();
  1179. assertFalse(set.contains(testint9999.toString()));
  1180. tm.put(testint9999.toString(), testint9999);
  1181. assertTrue(set.contains(testint9999.toString()));
  1182. tm.remove(testint9999.toString());
  1183. assertFalse(set.contains(testint9999.toString()));
  1184. try {
  1185. set.add(new Object());
  1186. fail("should throw UnsupportedOperationException");
  1187. } catch (UnsupportedOperationException e) {
  1188. // expected
  1189. }
  1190. try {
  1191. set.add(null);
  1192. fail("should throw UnsupportedOperationException");
  1193. } catch (UnsupportedOperationException e) {
  1194. // expected
  1195. }
  1196. try {
  1197. set.addAll(null);
  1198. fail("should throw UnsupportedOperationException");
  1199. } catch (NullPointerException e) {
  1200. // expected
  1201. }
  1202. Collection collection = new LinkedList();
  1203. set.addAll(collection);
  1204. try {
  1205. collection.add(new Object());
  1206. set.addAll(collection);
  1207. fail("should throw UnsupportedOperationException");
  1208. } catch (UnsupportedOperationException e) {
  1209. // expected
  1210. }
  1211. set.remove(testint100.toString());
  1212. assertFalse(tm.containsKey(testint100.toString()));
  1213. assertTrue(tm.containsKey(testint0.toString()));
  1214. Iterator iter = set.iterator();
  1215. iter.next();
  1216. iter.remove();
  1217. assertFalse(tm.containsKey(testint0.toString()));
  1218. collection.add(new Integer(200).toString());
  1219. set.retainAll(collection);
  1220. assertEquals(1, tm.size());
  1221. set.removeAll(collection);
  1222. assertEquals(0, tm.size());
  1223. tm.put(testint10000.toString(), testint10000);
  1224. assertEquals(1, tm.size());
  1225. set.clear();
  1226. assertEquals(0, tm.size());
  1227. }
  1228. private void assertEntry(Entry entry) {
  1229. try {
  1230. entry.setValue(new Object());
  1231. fail("should throw UnsupportedOperationException");
  1232. } catch (UnsupportedOperationException e) {
  1233. // expected
  1234. }
  1235. assertEquals((entry.getKey() == null ? 0 : entry.getKey().hashCode())
  1236. ^ (entry.getValue() == null ? 0 : entry.getValue().hashCode()),
  1237. entry.hashCode());
  1238. assertEquals(entry.toString(), entry.getKey() + "=" + entry.getValue());
  1239. }
  1240. /**
  1241. * @tests java.util.TreeMap#subMap(java.lang.Object,boolean,
  1242. * java.lang.Object,boolean)
  1243. */
  1244. public void test_subMapLjava_lang_ObjectZLjava_lang_ObjectZ() {
  1245. // normal case
  1246. SortedMap subMap = tm.subMap(objArray[100].toString(), true,
  1247. objArray[109].toString(), true);
  1248. assertEquals("subMap is of incorrect size", 10, subMap.size());
  1249. subMap = tm.subMap(objArray[100].toString(), true, objArray[109]
  1250. .toString(), false);
  1251. assertEquals("subMap is of incorrect size", 9, subMap.size());
  1252. for (int counter = 100; counter < 109; counter++) {
  1253. assertTrue("SubMap contains incorrect elements", subMap.get(
  1254. objArray[counter].toString()).equals(objArray[counter]));
  1255. }
  1256. subMap = tm.subMap(objArray[100].toString(), false, objArray[109]
  1257. .toString(), true);
  1258. assertEquals("subMap is of incorrect size", 9, subMap.size());
  1259. assertNull(subMap.get(objArray[100].toString()));
  1260. // Exceptions
  1261. try {
  1262. tm.subMap(objArray[9].toString(), true, objArray[1].toString(),
  1263. true);
  1264. fail("should throw IllegalArgumentException");
  1265. } catch (IllegalArgumentException e) {
  1266. // expected
  1267. }
  1268. try {
  1269. tm.subMap(objArray[9].toString(), false, objArray[1].toString(),
  1270. false);
  1271. fail("should throw IllegalArgumentException");
  1272. } catch (IllegalArgumentException e) {
  1273. // expected
  1274. }
  1275. try {
  1276. tm.subMap(null, true, null, true);
  1277. fail("should throw NullPointerException");
  1278. } catch (NullPointerException e) {
  1279. // expected
  1280. }
  1281. try {
  1282. tm.subMap(null, false, objArray[100], true);
  1283. fail("should throw NullPointerException");
  1284. } catch (NullPointerException e) {
  1285. // expected
  1286. }
  1287. try {
  1288. tm.subMap(new LinkedList(), false, objArray[100], true);
  1289. fail("should throw ClassCastException");
  1290. } catch (ClassCastException e) {
  1291. // expected
  1292. }
  1293. // use integer elements to test
  1294. TreeMap<Integer, String> treeMapInt = new TreeMap<Integer, String>();
  1295. assertEquals(0, treeMapInt.subMap(new Integer(-1), true,
  1296. new Integer(100), true).size());
  1297. for (int i = 0; i < 100; i++) {
  1298. treeMapInt.put(new Integer(i), new Integer(i).toString());
  1299. }
  1300. SortedMap<Integer, String> result = treeMapInt.subMap(new Integer(-1),
  1301. true, new Integer(100), true);
  1302. assertEquals(100, result.size());
  1303. result.put(new Integer(-1), new Integer(-1).toString());
  1304. assertEquals(101, result.size());
  1305. assertEquals(101, treeMapInt.size());
  1306. result = treeMapInt
  1307. .subMap(new Integer(50), true, new Integer(60), true);
  1308. assertEquals(11, result.size());
  1309. try {
  1310. result.put(new Integer(-2), new Integer(-2).toString());
  1311. fail("should throw IllegalArgumentException");
  1312. } catch (IllegalArgumentException e) {
  1313. // expected
  1314. }
  1315. assertEquals(11, result.size());
  1316. treeMapInt.remove(new Integer(50));
  1317. assertEquals(100, treeMapInt.size());
  1318. assertEquals(10, result.size());
  1319. result.remove(new Integer(60));
  1320. assertEquals(99, treeMapInt.size());
  1321. assertEquals(9, result.size());
  1322. SortedMap<Integer, String> result2 = null;
  1323. try {
  1324. result2 = result.subMap(new Integer(-2), new Integer(100));
  1325. fail("should throw IllegalArgumentException");
  1326. } catch (IllegalArgumentException e) {
  1327. // expected
  1328. }
  1329. result2 = result.subMap(new Integer(50), new Integer(60));
  1330. assertEquals(9, result2.size());
  1331. // sub map of sub map
  1332. NavigableMap<Integer, Object> mapIntObj = new TreeMap<Integer, Object>();
  1333. for (int i = 0; i < 10; ++i) {
  1334. mapIntObj.put(i, new Object());
  1335. }
  1336. mapIntObj = mapIntObj.subMap(5, false, 9, true);
  1337. assertEquals(4, mapIntObj.size());
  1338. mapIntObj = mapIntObj.subMap(5, false, 9, true);
  1339. assertEquals(4, mapIntObj.size());
  1340. mapIntObj = mapIntObj.subMap(5, false, 6, false);
  1341. assertEquals(0, mapIntObj.size());
  1342. // a special comparator dealing with null key
  1343. tm = new TreeMap(new Comparator() {
  1344. public int compare(Object o1, Object o2) {
  1345. if (o1 == null) {
  1346. return -1;
  1347. }
  1348. return ((String) o1).compareTo((String) o2);
  1349. }
  1350. });
  1351. tm.put(new String("1st"), 1);
  1352. tm.put(new String("2nd"), 2);
  1353. tm.put(new String("3rd"), 3);
  1354. String nullKey = null;
  1355. tm.put(nullKey, -1);
  1356. SortedMap s = tm.subMap(null, "3rd");
  1357. assertEquals(3, s.size());
  1358. assertTrue(s.containsValue(-1));
  1359. assertTrue(s.containsValue(1));
  1360. assertTrue(s.containsValue(2));
  1361. assertFalse(s.containsKey(null));
  1362. // RI fails here
  1363. // assertTrue(s.containsKey("1st"));
  1364. // assertTrue(s.containsKey("2nd"));
  1365. s = tm.descendingMap();
  1366. s = s.subMap("3rd", null);
  1367. // assertEquals(4, s.size());
  1368. // assertTrue(s.containsValue(-1));
  1369. // assertTrue(s.containsValue(1));
  1370. // assertTrue(s.containsValue(2));
  1371. // assertTrue(s.containsValue(3));
  1372. assertFalse(s.containsKey(null));
  1373. assertTrue(s.containsKey("1st"));
  1374. assertTrue(s.containsKey("2nd"));
  1375. assertTrue(s.containsKey("3rd"));
  1376. }
  1377. public void test_subMap_NullTolerableComparator() {
  1378. // Null Tolerable Comparator
  1379. TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
  1380. new MockComparatorNullTolerable());
  1381. treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
  1382. treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
  1383. SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
  1384. true, "key1", true); //$NON-NLS-1$
  1385. // RI fails here
  1386. assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$
  1387. assertEquals("value1", subMapWithNull.get("key1"));
  1388. assertEquals("value2", subMapWithNull.get(null));
  1389. treeMapWithNull.put("key0", "value2");
  1390. treeMapWithNull.put("key3", "value3");
  1391. treeMapWithNull.put("key4", "value4");
  1392. treeMapWithNull.put("key5", "value5");
  1393. treeMapWithNull.put("key6", "value6");
  1394. assertEquals("Size of subMap should be 3:", 3, subMapWithNull.size()); //$NON-NLS-1$
  1395. subMapWithNull = treeMapWithNull.subMap(null, false, "key1", true); //$NON-NLS-1$
  1396. assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$
  1397. }
  1398. /**
  1399. * @tests java.util.TreeMap#headMap(java.lang.Object,boolea)
  1400. */
  1401. public void test_headMapLjava_lang_ObjectZL() {
  1402. // normal case
  1403. SortedMap subMap = tm.headMap(objArray[100].toString(), true);
  1404. assertEquals("subMap is of incorrect size", 4, subMap.size());
  1405. subMap = tm.headMap(objArray[109].toString(), true);
  1406. assertEquals("subMap is of incorrect size", 13, subMap.size());
  1407. for (int counter = 100; counter < 109; counter++) {
  1408. assertTrue("SubMap contains incorrect elements", subMap.get(
  1409. objArray[counter].toString()).equals(objArray[counter]));
  1410. }
  1411. subMap = tm.headMap(objArray[100].toString(), false);
  1412. assertEquals("subMap is of incorrect size", 3, subMap.size());
  1413. assertNull(subMap.get(objArray[100].toString()));
  1414. // Exceptions
  1415. assertEquals(0, tm.headMap("", true).size());
  1416. assertEquals(0, tm.headMap("", false).size());
  1417. try {
  1418. tm.headMap(null, true);
  1419. fail("should throw NullPointerException");
  1420. } catch (NullPointerException e) {
  1421. // expected
  1422. }
  1423. try {
  1424. tm.headMap(null, false);
  1425. fail("should throw NullPointerException");
  1426. } catch (NullPointerException e) {
  1427. // expected
  1428. }
  1429. try {
  1430. tm.headMap(new Object(), true);
  1431. fail("should throw ClassCastException");
  1432. } catch (ClassCastException e) {
  1433. // expected
  1434. }
  1435. try {
  1436. tm.headMap(new Object(), false);
  1437. fail("should throw ClassCastException");
  1438. } catch (ClassCastException e) {
  1439. // expected
  1440. }
  1441. // use integer elements to test
  1442. TreeMap<Integer, String> treeMapInt = new TreeMap<Integer, String>();
  1443. assertEquals(0, treeMapInt.headMap(new Integer(-1), true).size());
  1444. for (int i = 0; i < 100; i++) {
  1445. treeMapInt.put(new Integer(i), new Integer(i).toString());
  1446. }
  1447. SortedMap<Integer, String> result = treeMapInt
  1448. .headMap(new Integer(101));
  1449. assertEquals(100, result.size());
  1450. try {
  1451. result.put(new Integer(101), new Integer(101).toString());
  1452. fail("should throw IllegalArgumentException");
  1453. } catch (IllegalArgumentException e) {
  1454. // expected
  1455. }
  1456. assertEquals(100, result.size());
  1457. assertEquals(100, treeMapInt.size());
  1458. result = treeMapInt.headMap(new Integer(50), true);
  1459. assertEquals(51, result.size());
  1460. result.put(new Integer(-1), new Integer(-1).toString());
  1461. assertEquals(52, result.size());
  1462. treeMapInt.remove(new Integer(40));
  1463. assertEquals(100, treeMapInt.size());
  1464. assertEquals(51, result.size());
  1465. result.remove(new Integer(30));
  1466. assertEquals(99, treeMapInt.size());
  1467. assertEquals(50, result.size());
  1468. SortedMap<Integer, String> result2 = null;
  1469. try {
  1470. result.subMap(new Integer(-2), new Integer(100));
  1471. fail("should throw IllegalArgumentException");
  1472. } catch (IllegalArgumentException e) {
  1473. // expected
  1474. }
  1475. try {
  1476. result.subMap(new Integer(1), new Integer(100));
  1477. fail("should throw IllegalArgumentException");
  1478. } catch (IllegalArgumentException e) {
  1479. // expected
  1480. }
  1481. result2 = result.subMap(new Integer(-2), new Integer(48));
  1482. assertEquals(47,result2.size());
  1483. result2 = result.subMap(new Integer(40), new Integer(50));
  1484. assertEquals(9, result2.size());
  1485. // Null Tolerable Comparator
  1486. TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
  1487. new MockComparatorNullTolerable());
  1488. treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
  1489. treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
  1490. SortedMap<String, String> subMapWithNull = treeMapWithNull.headMap(
  1491. null, true); //$NON-NLS-1$
  1492. assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
  1493. assertEquals(null, subMapWithNull.get("key1"));
  1494. assertEquals("value2", subMapWithNull.get(null));
  1495. treeMapWithNull.put("key0", "value2");
  1496. treeMapWithNull.put("key3", "value3");
  1497. treeMapWithNull.put("key4", "value4");
  1498. treeMapWithNull.put("key5", "value5");
  1499. treeMapWithNull.put("key6", "value6");
  1500. assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
  1501. subMapWithNull = treeMapWithNull.subMap(null, false, "key1", true); //$NON-NLS-1$
  1502. assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$
  1503. // head map of head map
  1504. NavigableMap<Integer, Object> mapIntObj = new TreeMap<Integer, Object>();
  1505. for (int i = 0; i < 10; ++i) {
  1506. mapIntObj.put(i, new Object());
  1507. }
  1508. mapIntObj = mapIntObj.headMap(5, false);
  1509. assertEquals(5, mapIntObj.size());
  1510. mapIntObj = mapIntObj.headMap(5, false);
  1511. assertEquals(5, mapIntObj.size());
  1512. mapIntObj = mapIntObj.tailMap(5, false);
  1513. assertEquals(0, mapIntObj.size());
  1514. }
  1515. /**
  1516. * @tests java.util.TreeMap#tailMap(java.lang.Object,boolea)
  1517. */
  1518. public void test_tailMapLjava_lang_ObjectZL() {
  1519. // normal case
  1520. SortedMap subMap = tm.tailMap(objArray[100].toString(), true);
  1521. assertEquals("subMap is of incorrect size", 997, subMap.size());
  1522. subMap = tm.tailMap(objArray[109].toString(), true);
  1523. assertEquals("subMap is of incorrect size", 988, subMap.size());
  1524. for (int counter = 119; counter > 110; counter--) {
  1525. assertTrue("SubMap contains incorrect elements", subMap.get(
  1526. objArray[counter].toString()).equals(objArray[counter]));
  1527. }
  1528. subMap = tm.tailMap(objArray[100].toString(), false);
  1529. assertEquals("subMap is of incorrect size", 996, subMap.size());
  1530. assertNull(subMap.get(objArray[100].toString()));
  1531. // Exceptions
  1532. assertEquals(1000, tm.tailMap("", true).size());
  1533. assertEquals(1000, tm.tailMap("", false).size());
  1534. try {
  1535. tm.tailMap(null, true);
  1536. fail("should throw NullPointerException");
  1537. } catch (NullPointerException e) {
  1538. // expected
  1539. }
  1540. try {
  1541. tm.tailMap(null, false);
  1542. fail("should throw NullPointerException");
  1543. } catch (NullPointerException e) {
  1544. // expected
  1545. }
  1546. try {
  1547. tm.tailMap(new Object(), true);
  1548. fail("should throw ClassCastException");
  1549. } catch (ClassCastException e) {
  1550. // expected
  1551. }
  1552. try {
  1553. tm.tailMap(new Object(), false);
  1554. fail("should throw ClassCastException");
  1555. } catch (ClassCastException e) {
  1556. // expected
  1557. }
  1558. // use integer elements to test
  1559. TreeMap<Integer, String> treeMapInt = new TreeMap<Integer, String>();
  1560. assertEquals(0, treeMapInt.tailMap(new Integer(-1), true).size());
  1561. for (int i = 0; i < 100; i++) {
  1562. treeMapInt.put(new Integer(i), new Integer(i).toString());
  1563. }
  1564. SortedMap<Integer, String> result = treeMapInt.tailMap(new Integer(1));
  1565. assertEquals(99, result.size());
  1566. try {
  1567. result.put(new Integer(-1), new Integer(-1).toString());
  1568. fail("should throw IllegalArgumentException");
  1569. } catch (IllegalArgumentException e) {
  1570. // expected
  1571. }
  1572. assertEquals(99, result.size());
  1573. assertEquals(100, treeMapInt.size());
  1574. result = treeMapInt.tailMap(new Integer(50), true);
  1575. assertEquals(50, result.size());
  1576. result.put(new Integer(101), new Integer(101).toString());
  1577. assertEquals(51, result.size());
  1578. treeMapInt.remove(new Integer(60));
  1579. assertEquals(100, treeMapInt.size());
  1580. assertEquals(50, result.size());
  1581. result.remove(new Integer(70));
  1582. assertEquals(99, treeMapInt.size());
  1583. assertEquals(49, result.size());
  1584. SortedMap<Integer, String> result2 = null;
  1585. try {
  1586. result2 = result.subMap(new Integer(-2), new Integer(100));
  1587. fail("should throw IllegalArgumentException");
  1588. } catch (IllegalArgumentException e) {
  1589. // expected
  1590. }
  1591. result2 = result.subMap(new Integer(60), new Integer(70));
  1592. assertEquals(9, result2.size());
  1593. // Null Tolerable Comparator
  1594. TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
  1595. new MockComparatorNullTolerable());
  1596. treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
  1597. treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
  1598. SortedMap<String, String> subMapWithNull = treeMapWithNull.tailMap(
  1599. "key1", true); //$NON-NLS-1$
  1600. assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
  1601. assertEquals("value1", subMapWithNull.get("key1"));
  1602. assertEquals(null, subMapWithNull.get(null));
  1603. treeMapWithNull.put("key0", "value2");
  1604. treeMapWithNull.put("key3", "value3");
  1605. treeMapWithNull.put("key4", "value4");
  1606. treeMapWithNull.put("key5", "value5");
  1607. treeMapWithNull.put("key6", "value6");
  1608. assertEquals("Size of subMap should be 5:", 5, subMapWithNull.size()); //$NON-NLS-1$
  1609. subMapWithNull = treeMapWithNull.subMap(null, false, "key1", true); //$NON-NLS-1$
  1610. assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$
  1611. // tail map of tail map
  1612. NavigableMap<Integer, Object> mapIntObj = new TreeMap<Integer, Object>();
  1613. for (int i = 0; i < 10; ++i) {
  1614. mapIntObj.put(i, new Object());
  1615. }
  1616. mapIntObj = mapIntObj.tailMap(5, false);
  1617. assertEquals(4, mapIntObj.size());
  1618. mapIntObj = mapIntObj.tailMap(5, false);
  1619. assertEquals(4, mapIntObj.size());
  1620. mapIntObj = mapIntObj.headMap(5, false);
  1621. assertEquals(0, mapIntObj.size());
  1622. }
  1623. public void test_descendingMap_subMap() throws Exception {
  1624. TreeMap<Integer, Object> tm = new TreeMap<Integer, Object>();
  1625. for (int i = 0; i < 10; ++i) {
  1626. tm.put(i, new Object());
  1627. }
  1628. NavigableMap<Integer, Object> descMap = tm.descendingMap();
  1629. assertEquals(7, descMap.subMap(8, true, 1, false).size());
  1630. assertEquals(4, descMap.headMap(6, true).size());
  1631. assertEquals(2, descMap.tailMap(2, false).size());
  1632. // sub map of sub map of descendingMap
  1633. NavigableMap<Integer, Object> mapIntObj = new TreeMap<Integer, Object>();
  1634. for (int i = 0; i < 10; ++i) {
  1635. mapIntObj.put(i, new Object());
  1636. }
  1637. mapIntObj = mapIntObj.descendingMap();
  1638. NavigableMap<Integer, Object> subMapIntObj = mapIntObj.subMap(9, true,
  1639. 5, false);
  1640. assertEquals(4, subMapIntObj.size());
  1641. subMapIntObj = subMapIntObj.subMap(9, true, 5, false);
  1642. assertEquals(4, subMapIntObj.size());
  1643. subMapIntObj = subMapIntObj.subMap(6, false, 5, false);
  1644. assertEquals(0, subMapIntObj.size());
  1645. subMapIntObj = mapIntObj.headMap(5, false);
  1646. assertEquals(4, subMapIntObj.size());
  1647. subMapIntObj = subMapIntObj.headMap(5, false);
  1648. assertEquals(4, subMapIntObj.size());
  1649. subMapIntObj = subMapIntObj.tailMap(5, false);
  1650. assertEquals(0, subMapIntObj.size());
  1651. subMapIntObj = mapIntObj.tailMap(5, false);
  1652. assertEquals(5, subMapIntObj.size());
  1653. subMapIntObj = subMapIntObj.tailMap(5, false);
  1654. assertEquals(5, subMapIntObj.size());
  1655. subMapIntObj = subMapIntObj.headMap(5, false);
  1656. assertEquals(0, subMapIntObj.size());
  1657. }
  1658. /**
  1659. * This test is about an old bug of RI.The bug is that: if the TreeMap has
  1660. * no comparator or its comparator is not null-tolerable, it can not put a
  1661. * null-key entry into this TreeMap.But RI can do it when the TreeMap is
  1662. * empty, and can not do it when the TreeMap is not empty.It is best for
  1663. * Harmony to follow RI's behavior for legacy reason. This test is to test
  1664. * the "illegal" TreeMap with the first null key. It can be easily removed
  1665. * when the bug is fixed in the future.
  1666. */
  1667. public void test_illegalFirstNullKey() {
  1668. // if the comparator is null
  1669. TreeMap<String, String> map = new TreeMap<String, String>();
  1670. map.put((String) null, "NullValue");
  1671. illegalFirstNullKeyMapTester(map);
  1672. //illegalFirstNullKeyMapTester(map.descendingMap());
  1673. // if the comparator is not null, but do not permit null key
  1674. map = new TreeMap<String, String>(new Comparator<String>() {
  1675. public int compare(String object1, String object2) {
  1676. return object1.compareTo(object2);
  1677. }
  1678. });
  1679. map.put((String) null, "NullValue");
  1680. illegalFirstNullKeyMapTester(map);
  1681. //illegalFirstNullKeyMapTester(map.descendingMap());
  1682. // add a few more testcases here based on this situation
  1683. TreeMap<Integer, String> map2 = new TreeMap<Integer, String>();
  1684. map2.put(null, "");
  1685. map2.firstEntry();
  1686. try {
  1687. map2.headMap(100).firstKey();
  1688. fail("should throw NPE");
  1689. } catch (NullPointerException e) {
  1690. // expected
  1691. }
  1692. try {
  1693. map2.tailMap(100).firstKey();
  1694. fail("should throw NPE");
  1695. } catch (NullPointerException e) {
  1696. // expected
  1697. }
  1698. // can pass
  1699. map2.lastEntry();
  1700. }
  1701. private void illegalFirstNullKeyMapTester(NavigableMap<String, String> map) {
  1702. try {
  1703. map.get(null);
  1704. fail("Should throw NullPointerException");
  1705. } catch (NullPointerException e) {
  1706. // expected
  1707. }
  1708. try {
  1709. map.put("NormalKey", "value");
  1710. fail("Should throw NullPointerException");
  1711. } catch (NullPointerException e) {
  1712. // expected
  1713. }
  1714. Set<String> keySet = map.keySet();
  1715. assertTrue(!keySet.isEmpty());
  1716. assertEquals(1, keySet.size());
  1717. for (String key : keySet) {
  1718. assertEquals(key, null);
  1719. try {
  1720. map.get(key);
  1721. fail("Should throw NullPointerException");
  1722. } catch (NullPointerException e) {
  1723. // ignore
  1724. }
  1725. }
  1726. Set<Entry<String, String>> entrySet = map.entrySet();
  1727. assertTrue(!entrySet.isEmpty());
  1728. assertEquals(1, entrySet.size());
  1729. for (Entry<String, String> entry : entrySet) {
  1730. assertEquals(null, entry.getKey());
  1731. assertEquals("NullValue", entry.getValue());
  1732. }
  1733. Collection<String> values = map.values();
  1734. assertTrue(!values.isEmpty());
  1735. assertEquals(1, values.size());
  1736. for (String value : values) {
  1737. assertEquals("NullValue", value);
  1738. }
  1739. try {
  1740. map.headMap(null, true);
  1741. fail("Should throw NullPointerException");
  1742. } catch (NullPointerException e) {
  1743. // ignore
  1744. }
  1745. try {
  1746. map.headMap(null, false);
  1747. fail("Should throw NullPointerException");
  1748. } catch (NullPointerException e) {
  1749. // ignore
  1750. }
  1751. try {
  1752. map.subMap(null, false, null, false);
  1753. fail("Should throw NullPointerException");
  1754. } catch (NullPointerException e) {
  1755. // ignore
  1756. }
  1757. try {
  1758. map.subMap(null, true, null, true);
  1759. fail("Should throw NullPointerException");
  1760. } catch (NullPointerException e) {
  1761. // ignore
  1762. }
  1763. try {
  1764. map.tailMap(null, true);
  1765. fail("Should throw NullPointerException");
  1766. } catch (NullPointerException e) {
  1767. // ignore
  1768. }
  1769. try {
  1770. map.tailMap(null, false);
  1771. fail("Should throw NullPointerException");
  1772. } catch (NullPointerException e) {
  1773. // ignore
  1774. }
  1775. }
  1776. /**
  1777. * Tests equals() method.
  1778. * Tests that no ClassCastException will be thrown in all cases.
  1779. * Regression test for HARMONY-1639.
  1780. */
  1781. public void test_equals() throws Exception {
  1782. // comparing TreeMaps with different object types
  1783. Map m1 = new TreeMap();
  1784. Map m2 = new TreeMap();
  1785. m1.put("key1", "val1");
  1786. m1.put("key2", "val2");
  1787. m2.put(new Integer(1), "val1");
  1788. m2.put(new Integer(2), "val2");
  1789. assertFalse("Maps should not be equal 1", m1.equals(m2));
  1790. assertFalse("Maps should not be equal 2", m2.equals(m1));
  1791. // comparing TreeMap with HashMap
  1792. m1 = new TreeMap();
  1793. m2 = new HashMap();
  1794. m1.put("key", "val");
  1795. m2.put(new Object(), "val");
  1796. assertFalse("Maps should not be equal 3", m1.equals(m2));
  1797. assertFalse("Maps should not be equal 4", m2.equals(m1));
  1798. // comparing TreeMaps with not-comparable objects inside
  1799. m1 = new TreeMap();
  1800. m2 = new TreeMap();
  1801. m1.put(new Object(), "val1");
  1802. m2.put(new Object(), "val1");
  1803. assertFalse("Maps should not be equal 5", m1.equals(m2));
  1804. assertFalse("Maps should not be equal 6", m2.equals(m1));
  1805. }
  1806. public void test_remove_from_iterator() throws Exception {
  1807. Set set = tm.keySet();
  1808. Iterator iter = set.iterator();
  1809. iter.next();
  1810. iter.remove();
  1811. try{
  1812. iter.remove();
  1813. fail("should throw IllegalStateException");
  1814. }catch (IllegalStateException e){
  1815. // expected
  1816. }
  1817. }
  1818. /**
  1819. * Tests entrySet().contains() method behaviour with respect to entries
  1820. * with null values.
  1821. * Regression test for HARMONY-5788.
  1822. */
  1823. public void test_entrySet_contains() throws Exception {
  1824. TreeMap master = new TreeMap<String, String>();
  1825. TreeMap test_map = new TreeMap<String, String>();
  1826. master.put("null", null);
  1827. Object[] entry = master.entrySet().toArray();
  1828. assertFalse("Empty map should not contain the null-valued entry",
  1829. test_map.entrySet().contains(entry[0]));
  1830. Map<String, String> submap = test_map.subMap("a","z");
  1831. entry = master.entrySet().toArray();
  1832. assertFalse("Empty submap should not contain the null-valued entry",
  1833. submap.entrySet().contains(entry[0]));
  1834. test_map.put("null", null);
  1835. assertTrue("entrySet().containsAll(...) should work with null values",
  1836. test_map.entrySet().containsAll(master.entrySet()));
  1837. master.clear();
  1838. master.put("null", '0');
  1839. entry = master.entrySet().toArray();
  1840. assertFalse("Null-valued entry should not equal non-null-valued entry",
  1841. test_map.entrySet().contains(entry[0]));
  1842. }
  1843. public void test_iterator_next_(){
  1844. Map m = tm.subMap("0", "1");
  1845. Iterator it = m.entrySet().iterator();
  1846. assertEquals("0=0",it.next().toString());
  1847. while(it.hasNext()){}
  1848. try {
  1849. it.next();
  1850. fail("should throw java.util.NoSuchElementException");
  1851. }catch (Exception e){
  1852. assertTrue(e instanceof java.util.NoSuchElementException);
  1853. }
  1854. }
  1855. public void test_empty_subMap() throws Exception {
  1856. TreeMap<Float, List<Integer>> tm = new TreeMap<Float, List<Integer>>();
  1857. SortedMap<Float, List<Integer>> sm = tm.tailMap(1.1f);
  1858. assertTrue(sm.values().size() == 0);
  1859. }
  1860. public static TreeMap treeMap = new TreeMap();
  1861. public void test_values_1(){
  1862. treeMap.put("firstKey", "firstValue");
  1863. treeMap.put("secondKey", "secondValue");
  1864. treeMap.put("thirdKey", "thirdValue");
  1865. Object firstKey = treeMap.firstKey();
  1866. SortedMap subMap = ((SortedMap)treeMap).subMap(firstKey, firstKey);
  1867. Iterator iter = subMap.values().iterator();
  1868. }
  1869. /**
  1870. * Sets up the fixture, for example, open a network connection. This method
  1871. * is called before a test is executed.
  1872. */
  1873. @Override
  1874. protected void setUp() {
  1875. tm = new TreeMap();
  1876. for (int i = 0; i < objArray.length; i++) {
  1877. Object x = objArray[i] = new Integer(i);
  1878. tm.put(x.toString(), x);
  1879. }
  1880. }
  1881. }