IdentityHashMap2Test.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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 18package org.apache.harmony.luni.tests.java.util; 19 20import java.io.Serializable; 21import java.util.AbstractMap; 22import java.util.ArrayList; 23import java.util.Arrays; 24import java.util.Collection; 25import java.util.HashMap; 26import java.util.IdentityHashMap; 27import java.util.Iterator; 28import java.util.Map; 29import java.util.Set; 30import java.util.TreeMap; 31 32import tests.support.Support_MapTest2; 33 34import org.apache.harmony.testframework.serialization.SerializationTest; 35 36public class IdentityHashMap2Test extends junit.framework.TestCase { 37 private static final String ID = "hello"; 38 39 class MockMap extends AbstractMap { 40 public Set entrySet() { 41 return null; 42 } 43 public int size(){ 44 return 0; 45 } 46 } 47 /* 48 * TODO: change all the statements testing the keys and values with equals() 49 * method to check for reference equality instead 50 */ 51 52 IdentityHashMap hm; 53 54 final static int hmSize = 1000; 55 56 static Object[] objArray; 57 58 static Object[] objArray2; 59 { 60 objArray = new Object[hmSize]; 61 objArray2 = new Object[hmSize]; 62 for (int i = 0; i < objArray.length; i++) { 63 objArray[i] = new Integer(i); 64 objArray2[i] = objArray[i].toString(); 65 } 66 } 67 68 /** 69 * @tests java.util.IdentityHashMap#IdentityHashMap() 70 */ 71 public void test_Constructor() { 72 // Test for method java.util.IdentityHashMap() 73 new Support_MapTest2(new IdentityHashMap()).runTest(); 74 75 IdentityHashMap hm2 = new IdentityHashMap(); 76 assertEquals("Created incorrect IdentityHashMap", 0, hm2.size()); 77 } 78 79 /** 80 * @tests java.util.IdentityHashMap#IdentityHashMap(int) 81 */ 82 public void test_ConstructorI() { 83 // Test for method java.util.IdentityHashMap(int) 84 IdentityHashMap hm2 = new IdentityHashMap(5); 85 assertEquals("Created incorrect IdentityHashMap", 0, hm2.size()); 86 try { 87 new IdentityHashMap(-1); 88 } catch (IllegalArgumentException e) { 89 return; 90 } 91 fail( 92 "Failed to throw IllegalArgumentException for initial capacity < 0"); 93 94 IdentityHashMap empty = new IdentityHashMap(0); 95 assertNull("Empty IdentityHashMap access", empty.get("nothing")); 96 empty.put("something", "here"); 97 assertTrue("cannot get element", empty.get("something") == "here"); 98 } 99 100 /** 101 * @tests java.util.IdentityHashMap#IdentityHashMap(java.util.Map) 102 */ 103 public void test_ConstructorLjava_util_Map() { 104 // Test for method java.util.IdentityHashMap(java.util.Map) 105 Map myMap = new TreeMap(); 106 for (int counter = 0; counter < hmSize; counter++) 107 myMap.put(objArray2[counter], objArray[counter]); 108 IdentityHashMap hm2 = new IdentityHashMap(myMap); 109 for (int counter = 0; counter < hmSize; counter++) 110 assertTrue("Failed to construct correct IdentityHashMap", hm 111 .get(objArray2[counter]) == hm2.get(objArray2[counter])); 112 113 Map mockMap = new MockMap(); 114 hm2 = new IdentityHashMap(mockMap); 115 assertEquals("Size should be 0", 0, hm2.size()); 116 } 117 118 public void test_IdentityHashMap_Constructor_BigSize() { 119 try { 120 new IdentityHashMap(Integer.MAX_VALUE); 121 fail("should throw OutOfMemoryError"); 122 } catch (OutOfMemoryError e) { 123 // Expected 124 } 125 } 126 127 /** 128 * @tests java.util.IdentityHashMap#clear() 129 */ 130 public void test_clear() { 131 // Test for method void java.util.IdentityHashMap.clear() 132 hm.clear(); 133 assertEquals("Clear failed to reset size", 0, hm.size()); 134 for (int i = 0; i < hmSize; i++) 135 assertNull("Failed to clear all elements", 136 hm.get(objArray2[i])); 137 138 } 139 140 /** 141 * @tests java.util.IdentityHashMap#clone() 142 */ 143 public void test_clone() { 144 // Test for method java.lang.Object java.util.IdentityHashMap.clone() 145 IdentityHashMap hm2 = (IdentityHashMap) hm.clone(); 146 assertTrue("Clone answered equivalent IdentityHashMap", hm2 != hm); 147 for (int counter = 0; counter < hmSize; counter++) 148 assertTrue("Clone answered unequal IdentityHashMap", hm 149 .get(objArray2[counter]) == hm2.get(objArray2[counter])); 150 151 IdentityHashMap map = new IdentityHashMap(); 152 map.put("key", "value"); 153 // get the keySet() and values() on the original Map 154 Set keys = map.keySet(); 155 Collection values = map.values(); 156 assertEquals("values() does not work", 157 "value", values.iterator().next()); 158 assertEquals("keySet() does not work", 159 "key", keys.iterator().next()); 160 AbstractMap map2 = (AbstractMap) map.clone(); 161 map2.put("key", "value2"); 162 Collection values2 = map2.values(); 163 assertTrue("values() is identical", values2 != values); 164 // values() and keySet() on the cloned() map should be different 165 assertEquals("values() was not cloned", 166 "value2", values2.iterator().next()); 167 map2.clear(); 168 map2.put("key2", "value3"); 169 Set key2 = map2.keySet(); 170 assertTrue("keySet() is identical", key2 != keys); 171 assertEquals("keySet() was not cloned", 172 "key2", key2.iterator().next()); 173 } 174 175 /** 176 * @tests java.util.IdentityHashMap#containsKey(java.lang.Object) 177 */ 178 public void test_containsKeyLjava_lang_Object() { 179 // Test for method boolean 180 // java.util.IdentityHashMap.containsKey(java.lang.Object) 181 assertTrue("Returned false for valid key", hm 182 .containsKey(objArray2[23])); 183 assertTrue("Returned true for copy of valid key", !hm 184 .containsKey(new Integer(23).toString())); 185 assertTrue("Returned true for invalid key", !hm.containsKey("KKDKDKD")); 186 187 IdentityHashMap m = new IdentityHashMap(); 188 m.put(null, "test"); 189 assertTrue("Failed with null key", m.containsKey(null)); 190 assertTrue("Failed with missing key matching null hash", !m 191 .containsKey(new Integer(0))); 192 } 193 194 /** 195 * @tests java.util.IdentityHashMap#containsValue(java.lang.Object) 196 */ 197 public void test_containsValueLjava_lang_Object() { 198 // Test for method boolean 199 // java.util.IdentityHashMap.containsValue(java.lang.Object) 200 assertTrue("Returned false for valid value", hm 201 .containsValue(objArray[19])); 202 assertTrue("Returned true for invalid valie", !hm 203 .containsValue(new Integer(-9))); 204 } 205 206 /** 207 * @tests java.util.IdentityHashMap#entrySet() 208 */ 209 public void test_entrySet() { 210 // Test for method java.util.Set java.util.IdentityHashMap.entrySet() 211 Set s = hm.entrySet(); 212 Iterator i = s.iterator(); 213 assertTrue("Returned set of incorrect size", hm.size() == s.size()); 214 while (i.hasNext()) { 215 Map.Entry m = (Map.Entry) i.next(); 216 assertTrue("Returned incorrect entry set", hm.containsKey(m 217 .getKey()) 218 && hm.containsValue(m.getValue())); 219 } 220 } 221 222 /** 223 * @tests java.util.IdentityHashMap#get(java.lang.Object) 224 */ 225 public void test_getLjava_lang_Object() { 226 // Test for method java.lang.Object 227 // java.util.IdentityHashMap.get(java.lang.Object) 228 assertNull("Get returned non-null for non existent key", 229 hm.get("T")); 230 hm.put("T", "HELLO"); 231 assertEquals("Get returned incorecct value for existing key", "HELLO", hm.get("T") 232 ); 233 234 IdentityHashMap m = new IdentityHashMap(); 235 m.put(null, "test"); 236 assertEquals("Failed with null key", "test", m.get(null)); 237 assertNull("Failed with missing key matching null hash", m 238 .get(new Integer(0))); 239 } 240 241 /** 242 * @tests java.util.IdentityHashMap#isEmpty() 243 */ 244 public void test_isEmpty() { 245 // Test for method boolean java.util.IdentityHashMap.isEmpty() 246 assertTrue("Returned false for new map", new IdentityHashMap() 247 .isEmpty()); 248 assertTrue("Returned true for non-empty", !hm.isEmpty()); 249 } 250 251 /** 252 * @tests java.util.IdentityHashMap#keySet() 253 */ 254 public void test_keySet() { 255 // Test for method java.util.Set java.util.IdentityHashMap.keySet() 256 Set s = hm.keySet(); 257 assertTrue("Returned set of incorrect size()", s.size() == hm.size()); 258 for (int i = 0; i < objArray.length; i++) { 259 assertTrue("Returned set does not contain all keys", s 260 .contains(objArray2[i])); 261 } 262 263 IdentityHashMap m = new IdentityHashMap(); 264 m.put(null, "test"); 265 assertTrue("Failed with null key", m.keySet().contains(null)); 266 assertNull("Failed with null key", m.keySet().iterator().next()); 267 268 Map map = new IdentityHashMap(101); 269 map.put(new Integer(1), "1"); 270 map.put(new Integer(102), "102"); 271 map.put(new Integer(203), "203"); 272 Iterator it = map.keySet().iterator(); 273 Integer remove1 = (Integer) it.next(); 274 it.hasNext(); 275 it.remove(); 276 Integer remove2 = (Integer) it.next(); 277 it.remove(); 278 ArrayList list = new ArrayList(Arrays.asList(new Integer[] { 279 new Integer(1), new Integer(102), new Integer(203) })); 280 list.remove(remove1); 281 list.remove(remove2); 282 assertTrue("Wrong result", it.next().equals(list.get(0))); 283 assertEquals("Wrong size", 1, map.size()); 284 assertTrue("Wrong contents", map.keySet().iterator().next().equals( 285 list.get(0))); 286 287 Map map2 = new IdentityHashMap(101); 288 map2.put(new Integer(1), "1"); 289 map2.put(new Integer(4), "4"); 290 Iterator it2 = map2.keySet().iterator(); 291 Integer remove3 = (Integer) it2.next(); 292 Integer next; 293 if (remove3.intValue() == 1) 294 next = new Integer(4); 295 else 296 next = new Integer(1); 297 it2.hasNext(); 298 it2.remove(); 299 assertTrue("Wrong result 2", it2.next().equals(next)); 300 assertEquals("Wrong size 2", 1, map2.size()); 301 assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals( 302 next)); 303 } 304 305 /** 306 * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object) 307 */ 308 public void test_putLjava_lang_ObjectLjava_lang_Object() { 309 // Test for method java.lang.Object 310 // java.util.IdentityHashMap.put(java.lang.Object, java.lang.Object) 311 hm.put("KEY", "VALUE"); 312 assertEquals("Failed to install key/value pair", 313 "VALUE", hm.get("KEY")); 314 315 IdentityHashMap m = new IdentityHashMap(); 316 Short s0 = new Short((short) 0); 317 m.put(s0, "short"); 318 m.put(null, "test"); 319 Integer i0 = new Integer(0); 320 m.put(i0, "int"); 321 assertEquals("Failed adding to bucket containing null", 322 "short", m.get(s0)); 323 assertEquals("Failed adding to bucket containing null2", "int", m.get(i0) 324 ); 325 } 326 327 /** 328 * @tests java.util.IdentityHashMap#putAll(java.util.Map) 329 */ 330 public void test_putAllLjava_util_Map() { 331 // Test for method void java.util.IdentityHashMap.putAll(java.util.Map) 332 IdentityHashMap hm2 = new IdentityHashMap(); 333 hm2.putAll(hm); 334 for (int i = 0; i < 1000; i++) 335 assertTrue("Failed to clear all elements", hm2.get(objArray2[i]) 336 .equals((new Integer(i)))); 337 338 hm2 = new IdentityHashMap(); 339 Map mockMap = new MockMap(); 340 hm2.putAll(mockMap); 341 assertEquals("Size should be 0", 0, hm2.size()); 342 } 343 344 /** 345 * @tests java.util.IdentityHashMap#remove(java.lang.Object) 346 */ 347 public void test_removeLjava_lang_Object() { 348 // Test for method java.lang.Object 349 // java.util.IdentityHashMap.remove(java.lang.Object) 350 int size = hm.size(); 351 Integer x = ((Integer) hm.remove(objArray2[9])); 352 assertTrue("Remove returned incorrect value", x.equals(new Integer(9))); 353 assertNull("Failed to remove given key", hm.get(objArray2[9])); 354 assertTrue("Failed to decrement size", hm.size() == (size - 1)); 355 assertNull("Remove of non-existent key returned non-null", hm 356 .remove("LCLCLC")); 357 358 IdentityHashMap m = new IdentityHashMap(); 359 m.put(null, "test"); 360 assertNull("Failed with same hash as null", 361 m.remove(objArray[0])); 362 assertEquals("Failed with null key", "test", m.remove(null)); 363 } 364 365 /** 366 * @tests java.util.IdentityHashMap#size() 367 */ 368 public void test_size() { 369 // Test for method int java.util.IdentityHashMap.size() 370 assertEquals("Returned incorrect size, ", (objArray.length + 2), hm 371 .size()); 372 } 373 374 /** 375 * @tests java.util.IdentityHashMap#equals(java.lang.Object) 376 */ 377 public void test_equalsLjava_lang_Object() { 378 IdentityHashMap mapOne = new IdentityHashMap(); 379 IdentityHashMap mapTwo = new IdentityHashMap(); 380 IdentityHashMap mapThree = new IdentityHashMap(); 381 IdentityHashMap mapFour = new IdentityHashMap(); 382 383 String one = "one"; 384 String alsoOne = new String(one); // use the new operator to ensure a 385 // new reference is constructed 386 String two = "two"; 387 String alsoTwo = new String(two); // use the new operator to ensure a 388 // new reference is constructed 389 390 mapOne.put(one, two); 391 mapFour.put(one, two); 392 393 // these two are not equal to the above two 394 mapTwo.put(alsoOne, two); 395 mapThree.put(one, alsoTwo); 396 397 assertEquals("failure of equality of IdentityHashMaps", mapOne, mapFour); 398 assertTrue("failure of non-equality of IdentityHashMaps one and two", 399 !mapOne.equals(mapTwo)); 400 assertTrue("failure of non-equality of IdentityHashMaps one and three", 401 !mapOne.equals(mapThree)); 402 assertTrue("failure of non-equality of IdentityHashMaps two and three", 403 !mapTwo.equals(mapThree)); 404 405 HashMap hashMapTwo = new HashMap(); 406 HashMap hashMapThree = new HashMap(); 407 hashMapTwo.put(alsoOne, two); 408 hashMapThree.put(one, alsoTwo); 409 410 assertTrue( 411 "failure of non-equality of IdentityHashMaps one and Hashmap two", 412 !mapOne.equals(hashMapTwo)); 413 assertTrue( 414 "failure of non-equality of IdentityHashMaps one and Hashmap three", 415 !mapOne.equals(hashMapThree)); 416 } 417 418 /** 419 * @tests java.util.IdentityHashMap#values() 420 */ 421 public void test_values() { 422 // Test for method java.util.Collection 423 // java.util.IdentityHashMap.values() 424 Collection c = hm.values(); 425 assertTrue("Returned collection of incorrect size()", c.size() == hm 426 .size()); 427 for (int i = 0; i < objArray.length; i++) 428 assertTrue("Returned collection does not contain all keys", c 429 .contains(objArray[i])); 430 431 IdentityHashMap myIdentityHashMap = new IdentityHashMap(); 432 for (int i = 0; i < 100; i++) 433 myIdentityHashMap.put(objArray2[i], objArray[i]); 434 Collection values = myIdentityHashMap.values(); 435 values.remove(objArray[0]); 436 assertTrue( 437 "Removing from the values collection should remove from the original map", 438 !myIdentityHashMap.containsValue(objArray2[0])); 439 440 } 441 442 /** 443 * Sets up the fixture, for example, open a network connection. This method 444 * is called before a test is executed. 445 */ 446 protected void setUp() { 447 hm = new IdentityHashMap(); 448 for (int i = 0; i < objArray.length; i++) 449 hm.put(objArray2[i], objArray[i]); 450 hm.put("test", null); 451 hm.put(null, "test"); 452 } 453 454 455 private static final SerializationTest.SerializableAssert comparator = new 456 SerializationTest.SerializableAssert() { 457 458 public void assertDeserialized(Serializable initial, Serializable deserialized) { 459 IdentityHashMap<String, String> initialMap = (IdentityHashMap<String, String>) initial; 460 IdentityHashMap<String, String> deseriaMap = (IdentityHashMap<String, String>) deserialized; 461 assertEquals("should be equal", initialMap.size(), deseriaMap.size()); 462 } 463 464 }; 465} 466