PageRenderTime 632ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/cyanogenmod/android_external_apache-harmony
Java | 2043 lines | 1570 code | 162 blank | 311 comment | 90 complexity | f492579f2282a5326f62229fabb46ccc MD5 | raw file

Large files files are truncated, but you can click here to view the full 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(collectio

Large files files are truncated, but you can click here to view the full file