Home | History | Annotate | Download | only in util
      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 
     18 package org.apache.harmony.tests.java.util;
     19 
     20 import org.apache.harmony.testframework.serialization.SerializationTest;
     21 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
     22 import tests.support.Support_MapTest2;
     23 import java.io.Serializable;
     24 import java.util.AbstractMap;
     25 import java.util.ArrayList;
     26 import java.util.Arrays;
     27 import java.util.Collection;
     28 import java.util.HashMap;
     29 import java.util.HashSet;
     30 import java.util.IdentityHashMap;
     31 import java.util.Iterator;
     32 import java.util.Map;
     33 import java.util.Set;
     34 import java.util.TreeMap;
     35 import java.util.TreeSet;
     36 
     37 public class IdentityHashMapTest extends junit.framework.TestCase {
     38     private static final String ID = "hello";
     39 
     40     class MockMap extends AbstractMap {
     41         public Set entrySet() {
     42             return null;
     43         }
     44         public int size(){
     45             return 0;
     46         }
     47     }
     48     /*
     49      * TODO: change all the statements testing the keys and values with equals()
     50      * method to check for reference equality instead
     51      */
     52 
     53     IdentityHashMap hm;
     54 
     55     final static int hmSize = 1000;
     56 
     57     Object[] objArray;
     58 
     59     Object[] objArray2;
     60 
     61     /**
     62      * java.util.IdentityHashMap#IdentityHashMap()
     63      */
     64     public void test_Constructor() {
     65         // Test for method java.util.IdentityHashMap()
     66         new Support_MapTest2(new IdentityHashMap()).runTest();
     67 
     68         IdentityHashMap hm2 = new IdentityHashMap();
     69         assertEquals("Created incorrect IdentityHashMap", 0, hm2.size());
     70     }
     71 
     72     /**
     73      * java.util.IdentityHashMap#IdentityHashMap(int)
     74      */
     75     public void test_ConstructorI() {
     76         // Test for method java.util.IdentityHashMap(int)
     77         IdentityHashMap hm2 = new IdentityHashMap(5);
     78         assertEquals("Created incorrect IdentityHashMap", 0, hm2.size());
     79         try {
     80             new IdentityHashMap(-1);
     81             fail("Failed to throw IllegalArgumentException for initial capacity < 0");
     82         } catch (IllegalArgumentException e) {
     83             //expected
     84         }
     85 
     86         IdentityHashMap empty = new IdentityHashMap(0);
     87         assertNull("Empty IdentityHashMap access", empty.get("nothing"));
     88         empty.put("something", "here");
     89         assertTrue("cannot get element", empty.get("something") == "here");
     90     }
     91 
     92     /**
     93      * java.util.IdentityHashMap#IdentityHashMap(java.util.Map)
     94      */
     95     public void test_ConstructorLjava_util_Map() {
     96         // Test for method java.util.IdentityHashMap(java.util.Map)
     97         Map myMap = new TreeMap();
     98         for (int counter = 0; counter < hmSize; counter++)
     99             myMap.put(objArray2[counter], objArray[counter]);
    100         IdentityHashMap hm2 = new IdentityHashMap(myMap);
    101         for (int counter = 0; counter < hmSize; counter++)
    102             assertTrue("Failed to construct correct IdentityHashMap", hm
    103                     .get(objArray2[counter]) == hm2.get(objArray2[counter]));
    104 
    105         Map mockMap = new MockMap();
    106         hm2 = new IdentityHashMap(mockMap);
    107         assertEquals("Size should be 0", 0, hm2.size());
    108 
    109         try {
    110             new IdentityHashMap(null);
    111             fail("NullPointerException expected");
    112         } catch (NullPointerException e) {
    113             //expected
    114         }
    115     }
    116 
    117     /**
    118      * java.util.IdentityHashMap#clear()
    119      */
    120     public void test_clear() {
    121         // Test for method void java.util.IdentityHashMap.clear()
    122         hm.clear();
    123         assertEquals("Clear failed to reset size", 0, hm.size());
    124         for (int i = 0; i < hmSize; i++)
    125             assertNull("Failed to clear all elements",
    126                     hm.get(objArray2[i]));
    127 
    128     }
    129 
    130     /**
    131      * java.util.IdentityHashMap#clone()
    132      */
    133     public void test_clone() {
    134         // Test for method java.lang.Object java.util.IdentityHashMap.clone()
    135         IdentityHashMap hm2 = (IdentityHashMap) hm.clone();
    136         assertTrue("Clone answered equivalent IdentityHashMap", hm2 != hm);
    137         for (int counter = 0; counter < hmSize; counter++)
    138             assertTrue("Clone answered unequal IdentityHashMap", hm
    139                     .get(objArray2[counter]) == hm2.get(objArray2[counter]));
    140 
    141         IdentityHashMap map = new IdentityHashMap();
    142         map.put("key", "value");
    143         // get the keySet() and values() on the original Map
    144         Set keys = map.keySet();
    145         Collection values = map.values();
    146         assertEquals("values() does not work",
    147                 "value", values.iterator().next());
    148         assertEquals("keySet() does not work",
    149                 "key", keys.iterator().next());
    150         AbstractMap map2 = (AbstractMap) map.clone();
    151         map2.put("key", "value2");
    152         Collection values2 = map2.values();
    153         assertTrue("values() is identical", values2 != values);
    154         // values() and keySet() on the cloned() map should be different
    155         assertEquals("values() was not cloned",
    156                 "value2", values2.iterator().next());
    157         map2.clear();
    158         map2.put("key2", "value3");
    159         Set key2 = map2.keySet();
    160         assertTrue("keySet() is identical", key2 != keys);
    161         assertEquals("keySet() was not cloned",
    162                 "key2", key2.iterator().next());
    163     }
    164 
    165     /**
    166      * java.util.IdentityHashMap#containsKey(java.lang.Object)
    167      */
    168     public void test_containsKeyLjava_lang_Object() {
    169         // Test for method boolean
    170         // java.util.IdentityHashMap.containsKey(java.lang.Object)
    171         assertTrue("Returned false for valid key", hm
    172                 .containsKey(objArray2[23]));
    173         assertTrue("Returned true for copy of valid key", !hm
    174                 .containsKey(new Integer(23).toString()));
    175         assertTrue("Returned true for invalid key", !hm.containsKey("KKDKDKD"));
    176 
    177         IdentityHashMap m = new IdentityHashMap();
    178         m.put(null, "test");
    179         assertTrue("Failed with null key", m.containsKey(null));
    180         assertTrue("Failed with missing key matching null hash", !m
    181                 .containsKey(new Integer(0)));
    182     }
    183 
    184     /**
    185      * java.util.IdentityHashMap#containsValue(java.lang.Object)
    186      */
    187     public void test_containsValueLjava_lang_Object() {
    188         // Test for method boolean
    189         // java.util.IdentityHashMap.containsValue(java.lang.Object)
    190         assertTrue("Returned false for valid value", hm
    191                 .containsValue(objArray[19]));
    192         assertTrue("Returned true for invalid valie", !hm
    193                 .containsValue(new Integer(-9)));
    194     }
    195 
    196     /**
    197      * java.util.IdentityHashMap#entrySet()
    198      */
    199     public void test_entrySet() {
    200         // Test for method java.util.Set java.util.IdentityHashMap.entrySet()
    201         Set s = hm.entrySet();
    202         Iterator i = s.iterator();
    203         assertTrue("Returned set of incorrect size", hm.size() == s.size());
    204         while (i.hasNext()) {
    205             Map.Entry m = (Map.Entry) i.next();
    206             assertTrue("Returned incorrect entry set", hm.containsKey(m
    207                     .getKey())
    208                     && hm.containsValue(m.getValue()));
    209         }
    210     }
    211 
    212     /**
    213      * java.util.IdentityHashMap#get(java.lang.Object)
    214      */
    215     public void test_getLjava_lang_Object() {
    216         // Test for method java.lang.Object
    217         // java.util.IdentityHashMap.get(java.lang.Object)
    218         assertNull("Get returned non-null for non existent key",
    219                 hm.get("T"));
    220         hm.put("T", "HELLO");
    221         assertEquals("Get returned incorecct value for existing key", "HELLO", hm.get("T")
    222                 );
    223 
    224         IdentityHashMap m = new IdentityHashMap();
    225         m.put(null, "test");
    226         assertEquals("Failed with null key", "test", m.get(null));
    227         assertNull("Failed with missing key matching null hash", m
    228                 .get(new Integer(0)));
    229     }
    230 
    231     /**
    232      * java.util.IdentityHashMap#isEmpty()
    233      */
    234     public void test_isEmpty() {
    235         // Test for method boolean java.util.IdentityHashMap.isEmpty()
    236         assertTrue("Returned false for new map", new IdentityHashMap()
    237                 .isEmpty());
    238         assertTrue("Returned true for non-empty", !hm.isEmpty());
    239     }
    240 
    241     /**
    242      * java.util.IdentityHashMap#keySet()
    243      */
    244     public void test_keySet() {
    245         // Test for method java.util.Set java.util.IdentityHashMap.keySet()
    246         Set s = hm.keySet();
    247         assertTrue("Returned set of incorrect size()", s.size() == hm.size());
    248         for (int i = 0; i < objArray.length; i++) {
    249             assertTrue("Returned set does not contain all keys", s
    250                     .contains(objArray2[i]));
    251         }
    252 
    253         IdentityHashMap m = new IdentityHashMap();
    254         m.put(null, "test");
    255         assertTrue("Failed with null key", m.keySet().contains(null));
    256         assertNull("Failed with null key", m.keySet().iterator().next());
    257 
    258         Map map = new IdentityHashMap(101);
    259         map.put(new Integer(1), "1");
    260         map.put(new Integer(102), "102");
    261         map.put(new Integer(203), "203");
    262         Iterator it = map.keySet().iterator();
    263         Integer remove1 = (Integer) it.next();
    264         it.hasNext();
    265         it.remove();
    266         Integer remove2 = (Integer) it.next();
    267         it.remove();
    268         ArrayList list = new ArrayList(Arrays.asList(new Integer[] {
    269                 new Integer(1), new Integer(102), new Integer(203) }));
    270         list.remove(remove1);
    271         list.remove(remove2);
    272         assertTrue("Wrong result", it.next().equals(list.get(0)));
    273         assertEquals("Wrong size", 1, map.size());
    274         assertTrue("Wrong contents", map.keySet().iterator().next().equals(
    275                 list.get(0)));
    276 
    277         Map map2 = new IdentityHashMap(101);
    278         map2.put(new Integer(1), "1");
    279         map2.put(new Integer(4), "4");
    280         Iterator it2 = map2.keySet().iterator();
    281         Integer remove3 = (Integer) it2.next();
    282         Integer next;
    283         if (remove3.intValue() == 1)
    284             next = new Integer(4);
    285         else
    286             next = new Integer(1);
    287         it2.hasNext();
    288         it2.remove();
    289         assertTrue("Wrong result 2", it2.next().equals(next));
    290         assertEquals("Wrong size 2", 1, map2.size());
    291         assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals(
    292                 next));
    293     }
    294 
    295     /**
    296      * java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
    297      */
    298     public void test_putLjava_lang_ObjectLjava_lang_Object() {
    299         // Test for method java.lang.Object
    300         // java.util.IdentityHashMap.put(java.lang.Object, java.lang.Object)
    301         hm.put("KEY", "VALUE");
    302         assertEquals("Failed to install key/value pair",
    303                 "VALUE", hm.get("KEY"));
    304 
    305         IdentityHashMap m = new IdentityHashMap();
    306         Short s0 = new Short((short) 0);
    307         m.put(s0, "short");
    308         m.put(null, "test");
    309         Integer i0 = new Integer(0);
    310         m.put(i0, "int");
    311         assertEquals("Failed adding to bucket containing null",
    312                 "short", m.get(s0));
    313         assertEquals("Failed adding to bucket containing null2", "int", m.get(i0)
    314                 );
    315 
    316         IdentityHashMap<Object, Object> map = new IdentityHashMap<Object, Object>();
    317 
    318         // Test null as a key.
    319         Object value = "Some value";
    320         map.put(null, value);
    321         assertSame("Assert 0: Failure getting null key", value, map.get(null));
    322 
    323         // Test null as a value
    324         Object key = "Some key";
    325         map.put(key, null);
    326         assertNull("Assert 1: Failure getting null value", map.get(key));
    327     }
    328 
    329     /**
    330      * java.util.IdentityHashMap#putAll(java.util.Map)
    331      */
    332     public void test_putAllLjava_util_Map() {
    333         // Test for method void java.util.IdentityHashMap.putAll(java.util.Map)
    334         IdentityHashMap hm2 = new IdentityHashMap();
    335         hm2.putAll(hm);
    336         for (int i = 0; i < 1000; i++)
    337             assertTrue("Failed to clear all elements", hm2.get(objArray2[i])
    338                     .equals((new Integer(i))));
    339 
    340         hm2 = new IdentityHashMap();
    341         Map mockMap = new MockMap();
    342         hm2.putAll(mockMap);
    343         assertEquals("Size should be 0", 0, hm2.size());
    344 
    345         try {
    346             hm2.putAll(null);
    347             fail("NullPointerException expected");
    348         } catch (NullPointerException e) {
    349             //expected
    350         }
    351     }
    352 
    353     /**
    354      * java.util.IdentityHashMap#remove(java.lang.Object)
    355      */
    356     public void test_removeLjava_lang_Object() {
    357         // Test for method java.lang.Object
    358         // java.util.IdentityHashMap.remove(java.lang.Object)
    359         int size = hm.size();
    360         Integer x = ((Integer) hm.remove(objArray2[9]));
    361         assertTrue("Remove returned incorrect value", x.equals(new Integer(9)));
    362         assertNull("Failed to remove given key", hm.get(objArray2[9]));
    363         assertTrue("Failed to decrement size", hm.size() == (size - 1));
    364         assertNull("Remove of non-existent key returned non-null", hm
    365                 .remove("LCLCLC"));
    366 
    367         IdentityHashMap m = new IdentityHashMap();
    368         m.put(null, "test");
    369         assertNull("Failed with same hash as null",
    370                 m.remove(objArray[0]));
    371         assertEquals("Failed with null key", "test", m.remove(null));
    372 
    373         // Regression for HARMONY-37
    374         IdentityHashMap<String, String> hashMap = new IdentityHashMap<String, String>();
    375         hashMap.remove("absent");
    376         assertEquals("Assert 0: Size is incorrect", 0, hashMap.size());
    377 
    378         hashMap.put("key", "value");
    379         hashMap.remove("key");
    380         assertEquals("Assert 1: After removing non-null element size is incorrect", 0, hashMap.size());
    381 
    382         hashMap.put(null, null);
    383         assertEquals("Assert 2: adding literal null failed", 1, hashMap.size());
    384         hashMap.remove(null);
    385         assertEquals("Assert 3: After removing null element size is incorrect", 0, hashMap.size());
    386     }
    387 
    388     /**
    389      * java.util.IdentityHashMap#size()
    390      */
    391     public void test_size() {
    392         // Test for method int java.util.IdentityHashMap.size()
    393         assertEquals("Returned incorrect size, ", (objArray.length + 2), hm
    394                 .size());
    395     }
    396 
    397     /**
    398      * java.util.IdentityHashMap#equals(java.lang.Object)
    399      */
    400     public void test_equalsLjava_lang_Object() {
    401         IdentityHashMap mapOne = new IdentityHashMap();
    402         IdentityHashMap mapTwo = new IdentityHashMap();
    403         IdentityHashMap mapThree = new IdentityHashMap();
    404         IdentityHashMap mapFour = new IdentityHashMap();
    405 
    406         String one = "one";
    407         String alsoOne = new String(one); // use the new operator to ensure a
    408         // new reference is constructed
    409         String two = "two";
    410         String alsoTwo = new String(two); // use the new operator to ensure a
    411         // new reference is constructed
    412 
    413         mapOne.put(one, two);
    414         mapFour.put(one, two);
    415 
    416         // these two are not equal to the above two
    417         mapTwo.put(alsoOne, two);
    418         mapThree.put(one, alsoTwo);
    419 
    420         assertEquals("failure of equality of IdentityHashMaps", mapOne, mapFour);
    421         assertTrue("failure of non-equality of IdentityHashMaps one and two",
    422                 !mapOne.equals(mapTwo));
    423         assertTrue("failure of non-equality of IdentityHashMaps one and three",
    424                 !mapOne.equals(mapThree));
    425         assertTrue("failure of non-equality of IdentityHashMaps two and three",
    426                 !mapTwo.equals(mapThree));
    427 
    428         HashMap hashMapTwo = new HashMap();
    429         HashMap hashMapThree = new HashMap();
    430         hashMapTwo.put(alsoOne, two);
    431         hashMapThree.put(one, alsoTwo);
    432 
    433         assertTrue(
    434                 "failure of non-equality of IdentityHashMaps one and Hashmap two",
    435                 !mapOne.equals(hashMapTwo));
    436         assertTrue(
    437                 "failure of non-equality of IdentityHashMaps one and Hashmap three",
    438                 !mapOne.equals(hashMapThree));
    439     }
    440 
    441     /**
    442      * java.util.IdentityHashMap#Serialization()
    443      */
    444     public void test_Serialization() throws Exception {
    445         IdentityHashMap<String, String> map = new IdentityHashMap<String, String>();
    446         map.put(ID, "world");
    447         // BEGIN android-added
    448         // Regression test for null key in serialized IdentityHashMap (1178549)
    449         // Together with this change the IdentityHashMap.golden.ser resource
    450         // was replaced by a version that contains a map with a null key.
    451         map.put(null, "null");
    452         // END android-added
    453         SerializationTest.verifySelf(map, comparator);
    454         SerializationTest.verifyGolden(this, map, comparator);
    455     }
    456 
    457     /**
    458      * Sets up the fixture, for example, open a network connection. This method
    459      * is called before a test is executed.
    460      */
    461     protected void setUp() {
    462         objArray = new Object[hmSize];
    463         objArray2 = new Object[hmSize];
    464         for (int i = 0; i < objArray.length; i++) {
    465             objArray[i] = new Integer(i);
    466             // android-changed: the containsKey test requires unique strings.
    467             objArray2[i] = new String(objArray[i].toString());
    468         }
    469 
    470         hm = new IdentityHashMap();
    471         for (int i = 0; i < objArray.length; i++)
    472             hm.put(objArray2[i], objArray[i]);
    473         hm.put("test", null);
    474         hm.put(null, "test");
    475     }
    476 
    477     /**
    478      * Tears down the fixture, for example, close a network connection. This
    479      * method is called after a test is executed.
    480      */
    481     protected void tearDown() {
    482         objArray = null;
    483         objArray2 = null;
    484         hm = null;
    485     }
    486 
    487     private static final SerializationTest.SerializableAssert comparator = new
    488                              SerializationTest.SerializableAssert() {
    489 
    490         public void assertDeserialized(Serializable initial, Serializable deserialized) {
    491             IdentityHashMap<String, String> initialMap = (IdentityHashMap<String, String>) initial;
    492             IdentityHashMap<String, String> deseriaMap = (IdentityHashMap<String, String>) deserialized;
    493             assertEquals("should be equal", initialMap.size(), deseriaMap.size());
    494         }
    495 
    496     };
    497 
    498     /**
    499      * java.util.IdentityHashMap#containsKey(java.lang.Object)
    500      * java.util.IdentityHashMap#containsValue(java.lang.Object)
    501      * java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
    502      * java.util.IdentityHashMap#get(java.lang.Object)
    503      */
    504     public void test_null_Keys_and_Values() {
    505         // tests with null keys and values
    506         IdentityHashMap map = new IdentityHashMap();
    507         Object result;
    508 
    509         // null key and null value
    510         result = map.put(null, null);
    511         assertTrue("testA can not find null key", map.containsKey(null));
    512         assertTrue("testA can not find null value", map.containsValue(null));
    513         assertNull("testA can not get null value for null key",
    514                 map.get(null));
    515         assertNull("testA put returned wrong value", result);
    516 
    517         // null value
    518         String value = "a value";
    519         result = map.put(null, value);
    520         assertTrue("testB can not find null key", map.containsKey(null));
    521         assertTrue("testB can not find a value with null key", map
    522                 .containsValue(value));
    523         assertTrue("testB can not get value for null key",
    524                 map.get(null) == value);
    525         assertNull("testB put returned wrong value", result);
    526 
    527         // a null key
    528         String key = "a key";
    529         result = map.put(key, null);
    530         assertTrue("testC can not find a key with null value", map
    531                 .containsKey(key));
    532         assertTrue("testC can not find null value", map.containsValue(null));
    533         assertNull("testC can not get null value for key", map.get(key));
    534         assertNull("testC put returned wrong value", result);
    535 
    536         // another null key
    537         String anothervalue = "another value";
    538         result = map.put(null, anothervalue);
    539         assertTrue("testD can not find null key", map.containsKey(null));
    540         assertTrue("testD can not find a value with null key", map
    541                 .containsValue(anothervalue));
    542         assertTrue("testD can not get value for null key",
    543                 map.get(null) == anothervalue);
    544         assertTrue("testD put returned wrong value", result == value);
    545 
    546         // remove a null key
    547         result = map.remove(null);
    548         assertTrue("testE remove returned wrong value", result == anothervalue);
    549         assertTrue("testE should not find null key", !map.containsKey(null));
    550         assertTrue("testE should not find a value with null key", !map
    551                 .containsValue(anothervalue));
    552         assertNull("testE should not get value for null key",
    553                 map.get(null));
    554     }
    555 
    556     /**
    557      * java.util.IdentityHashMap#remove(java.lang.Object)
    558      * java.util.IdentityHashMap#keySet()
    559      */
    560     public void test_remove() {
    561         IdentityHashMap map = new IdentityHashMap();
    562         map.put(null, null);
    563         map.put("key1", "value1");
    564         map.put("key2", "value2");
    565         map.remove("key1");
    566 
    567         assertTrue("Did not remove key1", !map.containsKey("key1"));
    568         assertTrue("Did not remove the value for key1", !map
    569                 .containsValue("value1"));
    570 
    571         assertTrue("Modified key2", map.get("key2") != null
    572                 && map.get("key2") == "value2");
    573         assertNull("Modified null entry", map.get(null));
    574     }
    575 
    576     /**
    577      * java.util.IdentityHashMap#entrySet()
    578      * java.util.IdentityHashMap#keySet()
    579      * java.util.IdentityHashMap#values()
    580      */
    581     public void test_sets() {
    582         // tests with null keys and values
    583         IdentityHashMap map = new IdentityHashMap();
    584 
    585         // null key and null value
    586         map.put("key", "value");
    587         map.put(null, null);
    588         map.put("a key", null);
    589         map.put("another key", null);
    590 
    591         Set keyset = map.keySet();
    592         Collection valueset = map.values();
    593         Set entries = map.entrySet();
    594         Iterator it = entries.iterator();
    595         while (it.hasNext()) {
    596             Map.Entry entry = (Map.Entry) it.next();
    597             assertTrue("EntrySetIterator can not find entry ", entries
    598                     .contains(entry));
    599 
    600             assertTrue("entry key not found in map", map.containsKey(entry
    601                     .getKey()));
    602             assertTrue("entry value not found in map", map.containsValue(entry
    603                     .getValue()));
    604 
    605             assertTrue("entry key not found in the keyset", keyset
    606                     .contains(entry.getKey()));
    607             assertTrue("entry value not found in the valueset", valueset
    608                     .contains(entry.getValue()));
    609         }
    610     }
    611 
    612     /**
    613      * java.util.IdentityHashMap#entrySet()
    614      * java.util.IdentityHashMap#remove(java.lang.Object)
    615      */
    616     public void test_entrySet_removeAll() {
    617         IdentityHashMap map = new IdentityHashMap();
    618         for (int i = 0; i < 1000; i++) {
    619             map.put(new Integer(i), new Integer(i));
    620         }
    621         Set set = map.entrySet();
    622 
    623         set.removeAll(set);
    624         assertEquals("did not remove all elements in the map", 0, map.size());
    625         assertTrue("did not remove all elements in the entryset", set.isEmpty());
    626 
    627         Iterator it = set.iterator();
    628         assertTrue("entrySet iterator still has elements", !it.hasNext());
    629     }
    630 
    631     /**
    632      * java.util.IdentityHashMap#keySet()
    633      * java.util.IdentityHashMap#clear()
    634      */
    635     public void test_keySet_clear() {
    636         IdentityHashMap map = new IdentityHashMap();
    637         for (int i = 0; i < 1000; i++) {
    638             map.put(new Integer(i), new Integer(i));
    639         }
    640         Set set = map.keySet();
    641         set.clear();
    642 
    643         assertEquals("did not remove all elements in the map", 0, map.size());
    644         assertTrue("did not remove all elements in the keyset", set.isEmpty());
    645 
    646         Iterator it = set.iterator();
    647         assertTrue("keySet iterator still has elements", !it.hasNext());
    648     }
    649 
    650     /**
    651      * java.util.IdentityHashMap#values()
    652      */
    653     public void test_values() {
    654 
    655         IdentityHashMap map = new IdentityHashMap();
    656         for (int i = 0; i < 10; i++) {
    657             map.put(new Integer(i), new Integer(i));
    658         }
    659 
    660         Integer key = new Integer(20);
    661         Integer value = new Integer(40);
    662         map.put(key, value);
    663 
    664         Collection vals = map.values();
    665         boolean result = vals.remove(key);
    666         assertTrue("removed entries incorrectly", map.size() == 11 && !result);
    667         assertTrue("removed key incorrectly", map.containsKey(key));
    668         assertTrue("removed value incorrectly", map.containsValue(value));
    669 
    670         result = vals.remove(value);
    671         assertTrue("Did not remove entry as expected", map.size() == 10
    672                 && result);
    673         assertTrue("Did not remove key as expected", !map.containsKey(key));
    674         assertTrue("Did not remove value as expected", !map
    675                 .containsValue(value));
    676 
    677         // put an equivalent key to a value
    678         key = new Integer(1);
    679         value = new Integer(100);
    680         map.put(key, value);
    681 
    682         result = vals.remove(key);
    683         assertTrue("TestB. removed entries incorrectly", map.size() == 11
    684                 && !result);
    685         assertTrue("TestB. removed key incorrectly", map.containsKey(key));
    686         assertTrue("TestB. removed value incorrectly", map.containsValue(value));
    687 
    688         result = vals.remove(value);
    689         assertTrue("TestB. Did not remove entry as expected", map.size() == 10
    690                 && result);
    691         assertTrue("TestB. Did not remove key as expected", !map
    692                 .containsKey(key));
    693         assertTrue("TestB. Did not remove value as expected", !map
    694                 .containsValue(value));
    695 
    696         vals.clear();
    697         assertEquals("Did not remove all entries as expected", 0, map.size());
    698     }
    699 
    700     /**
    701      * java.util.IdentityHashMap#keySet()
    702      * java.util.IdentityHashMap#remove(java.lang.Object)
    703      */
    704     public void test_keySet_removeAll() {
    705         IdentityHashMap map = new IdentityHashMap();
    706         for (int i = 0; i < 1000; i++) {
    707             map.put(new Integer(i), new Integer(i));
    708         }
    709         Set set = map.keySet();
    710         set.removeAll(set);
    711 
    712         assertEquals("did not remove all elements in the map", 0, map.size());
    713         assertTrue("did not remove all elements in the keyset", set.isEmpty());
    714 
    715         Iterator it = set.iterator();
    716         assertTrue("keySet iterator still has elements", !it.hasNext());
    717     }
    718 
    719     /**
    720      * java.util.IdentityHashMap#keySet()
    721      */
    722     public void test_keySet_retainAll() {
    723         IdentityHashMap map = new IdentityHashMap();
    724         for (int i = 0; i < 1000; i++) {
    725             map.put(new Integer(i), new Integer(i));
    726         }
    727         Set set = map.keySet();
    728 
    729         // retain all the elements
    730         boolean result = set.retainAll(set);
    731         assertTrue("retain all should return false", !result);
    732         assertEquals("did not retain all", 1000, set.size());
    733 
    734         // send empty set to retainAll
    735         result = set.retainAll(new TreeSet());
    736         assertTrue("retain all should return true", result);
    737         assertEquals("did not remove all elements in the map", 0, map.size());
    738         assertTrue("did not remove all elements in the keyset", set.isEmpty());
    739 
    740         Iterator it = set.iterator();
    741         assertTrue("keySet iterator still has elements", !it.hasNext());
    742     }
    743 
    744     /**
    745      * java.util.IdentityHashMap#keySet()
    746      * java.util.IdentityHashMap#remove(java.lang.Object)
    747      */
    748     public void test_keyset_remove() {
    749         IdentityHashMap map = new IdentityHashMap();
    750 
    751         Integer key = new Integer(21);
    752 
    753         map.put(new Integer(1), null);
    754         map.put(new Integer(11), null);
    755         map.put(key, null);
    756         map.put(new Integer(31), null);
    757         map.put(new Integer(41), null);
    758         map.put(new Integer(51), null);
    759         map.put(new Integer(61), null);
    760         map.put(new Integer(71), null);
    761         map.put(new Integer(81), null);
    762         map.put(new Integer(91), null);
    763 
    764         Set set = map.keySet();
    765 
    766         Set newset = new HashSet();
    767         Iterator it = set.iterator();
    768         while (it.hasNext()) {
    769             Object element = it.next();
    770             if (element == key) {
    771                 it.remove();
    772             } else
    773                 newset.add(element);
    774         }
    775         int size = newset.size();
    776         assertTrue("keyset and newset don't have same size",
    777                 newset.size() == size);
    778         assertTrue("element is in newset ", !newset.contains(key));
    779         assertTrue("element not removed from keyset", !set.contains(key));
    780         assertTrue("element not removed from map", !map.containsKey(key));
    781 
    782         assertTrue("newset and keyset do not have same elements 1", newset
    783                 .equals(set));
    784         assertTrue("newset and keyset do not have same elements 2", set
    785                 .equals(newset));
    786     }
    787 
    788     public void test_clone_scenario1() {
    789         IdentityHashMap hashMap = new IdentityHashMap();
    790         assertEquals(0, hashMap.hashCode());
    791         Object cloneHashMap = hashMap.clone();
    792         ((IdentityHashMap) cloneHashMap).put("key", "value");
    793         assertEquals(0, hashMap.hashCode());
    794         assertTrue(0 != cloneHashMap.hashCode());
    795     }
    796 
    797     public void test_clone_scenario2() {
    798         IdentityHashMap hashMap = new IdentityHashMap();
    799         assertEquals(0, hashMap.hashCode());
    800         Object cloneHashMap = hashMap.clone();
    801         hashMap.put("key", "value");
    802         assertEquals(1, hashMap.size());
    803         assertEquals(0, ((IdentityHashMap) cloneHashMap).size());
    804         assertEquals("value", hashMap.get("key"));
    805         assertNull(((IdentityHashMap) cloneHashMap).get("key"));
    806         assertTrue(0 != hashMap.hashCode());
    807         assertEquals(0, cloneHashMap.hashCode());
    808     }
    809 
    810     public void test_clone_scenario3() {
    811         IdentityHashMap hashMap = new IdentityHashMap();
    812         assertEquals(0, hashMap.hashCode());
    813         hashMap.put("key", "value");
    814         Object cloneHashMap = hashMap.clone();
    815         assertEquals(1, hashMap.size());
    816         assertEquals(1, ((IdentityHashMap) cloneHashMap).size());
    817         assertEquals("value", hashMap.get("key"));
    818         assertEquals("value", ((IdentityHashMap) cloneHashMap).get("key"));
    819         assertEquals(hashMap.hashCode(), cloneHashMap.hashCode());
    820     }
    821 
    822     public void test_clone_scenario4() {
    823         IdentityHashMap hashMap = new IdentityHashMap();
    824         Object cloneHashMap = hashMap.clone();
    825         assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
    826         hashMap.put((Object) null, cloneHashMap);
    827         assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
    828         assertEquals(cloneHashMap, hashMap.get((Object) null));
    829     }
    830 
    831     public void test_clone_scenario5() throws Exception {
    832         IdentityHashMap hashMap = new IdentityHashMap();
    833         Object cloneHashMap = hashMap.clone();
    834         assertNull(hashMap.remove((Object) null));
    835         ((IdentityHashMap) cloneHashMap).put((Object) null, cloneHashMap);
    836         assertNull(hashMap.remove((Object) null));
    837         assertEquals(cloneHashMap, ((IdentityHashMap) cloneHashMap)
    838                 .get((Object) null));
    839     }
    840 
    841     /*
    842     * Regression test for HARMONY-6419
    843     */
    844     public void test_underlyingMap() {
    845         IdentityHashMap<String, String> ihm = new IdentityHashMap<String, String>();
    846         String key = "key";
    847         String value = "value";
    848         ihm.put(key, value);
    849 
    850         Set<Map.Entry<String, String>> set = ihm.entrySet();
    851         assertEquals(1, set.size());
    852 
    853         Map.Entry<String, String> entry = set.iterator().next();
    854 
    855         String newValue = "newvalue";
    856         entry.setValue(newValue);
    857         assertSame(newValue, ihm.get(key));
    858     }
    859 
    860     // comparator for IdentityHashMap objects
    861     private static final SerializableAssert COMPARATOR = new SerializableAssert() {
    862         public void assertDeserialized(Serializable initial,
    863                 Serializable deserialized) {
    864 
    865             IdentityHashMap init = (IdentityHashMap) initial;
    866             IdentityHashMap desr = (IdentityHashMap) deserialized;
    867 
    868             assertEquals("Size", init.size(), desr.size());
    869         }
    870     };
    871 }
    872