OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / luni / tests / java / util / IdentityHashMapTest.java
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.luni.tests.java.util;
19
20 import java.io.Serializable;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.IdentityHashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.TreeSet;
28
29 import org.apache.harmony.testframework.serialization.SerializationTest;
30 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
31
32 public class IdentityHashMapTest extends junit.framework.TestCase {
33
34         /**
35          * @tests java.util.IdentityHashMap#containsKey(java.lang.Object)
36          * @tests java.util.IdentityHashMap#containsValue(java.lang.Object)
37          * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
38          * @tests java.util.IdentityHashMap#get(java.lang.Object)
39          */
40         public void test_null_Keys_and_Values() {
41                 // tests with null keys and values
42                 IdentityHashMap map = new IdentityHashMap();
43                 Object result;
44
45                 // null key and null value
46                 result = map.put(null, null);
47                 assertTrue("testA can not find null key", map.containsKey(null));
48                 assertTrue("testA can not find null value", map.containsValue(null));
49                 assertNull("testA can not get null value for null key",
50                                 map.get(null));
51                 assertNull("testA put returned wrong value", result);
52
53                 // null value
54                 String value = "a value";
55                 result = map.put(null, value);
56                 assertTrue("testB can not find null key", map.containsKey(null));
57                 assertTrue("testB can not find a value with null key", map
58                                 .containsValue(value));
59                 assertTrue("testB can not get value for null key",
60                                 map.get(null) == value);
61                 assertNull("testB put returned wrong value", result);
62
63                 // a null key
64                 String key = "a key";
65                 result = map.put(key, null);
66                 assertTrue("testC can not find a key with null value", map
67                                 .containsKey(key));
68                 assertTrue("testC can not find null value", map.containsValue(null));
69                 assertNull("testC can not get null value for key", map.get(key));
70                 assertNull("testC put returned wrong value", result);
71
72                 // another null key
73                 String anothervalue = "another value";
74                 result = map.put(null, anothervalue);
75                 assertTrue("testD can not find null key", map.containsKey(null));
76                 assertTrue("testD can not find a value with null key", map
77                                 .containsValue(anothervalue));
78                 assertTrue("testD can not get value for null key",
79                                 map.get(null) == anothervalue);
80                 assertTrue("testD put returned wrong value", result == value);
81
82                 // remove a null key
83                 result = map.remove(null);
84                 assertTrue("testE remove returned wrong value", result == anothervalue);
85                 assertTrue("testE should not find null key", !map.containsKey(null));
86                 assertTrue("testE should not find a value with null key", !map
87                                 .containsValue(anothervalue));
88                 assertNull("testE should not get value for null key",
89                                 map.get(null));
90         }
91
92     /**
93      * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
94      */
95     public void test_putLjava_lang_ObjectLjava_lang_Object() {
96         IdentityHashMap<Object, Object> map = new IdentityHashMap<Object, Object>();
97
98         // Test null as a key.
99         Object value = "Some value";
100         map.put(null, value);
101         assertSame("Assert 0: Failure getting null key", value, map.get(null));
102
103         // Test null as a value
104         Object key = "Some key";
105         map.put(key, null);
106         assertNull("Assert 1: Failure getting null value", map.get(key));
107     }
108
109         /**
110          * @tests java.util.IdentityHashMap#remove(java.lang.Object)
111          * @tests java.util.IdentityHashMap#keySet()
112          */
113         public void test_remove() {
114                 IdentityHashMap map = new IdentityHashMap();
115                 map.put(null, null);
116                 map.put("key1", "value1");
117                 map.put("key2", "value2");
118                 map.remove("key1");
119
120                 assertTrue("Did not remove key1", !map.containsKey("key1"));
121                 assertTrue("Did not remove the value for key1", !map
122                                 .containsValue("value1"));
123
124                 assertTrue("Modified key2", map.get("key2") != null
125                                 && map.get("key2") == "value2");
126                 assertNull("Modified null entry", map.get(null));
127         }
128
129     /**
130      * @tests java.util.IdentityHashMapTest#remove(java.lang.Object)
131      */
132     public void test_removeLjava_lang_Object() {
133         // Regression for HARMONY-37
134         IdentityHashMap<String, String> hashMap = new IdentityHashMap<String, String>();
135         hashMap.remove("absent");
136         assertEquals("Assert 0: Size is incorrect", 0, hashMap.size());
137
138         hashMap.put("key", "value");
139         hashMap.remove("key");
140         assertEquals("Assert 1: After removing non-null element size is incorrect", 0, hashMap.size());
141
142         hashMap.put(null, null);
143         assertEquals("Assert 2: adding literal null failed", 1, hashMap.size());
144         hashMap.remove(null);
145         assertEquals("Assert 3: After removing null element size is incorrect", 0, hashMap.size());
146     }
147
148         /**
149          * @tests java.util.IdentityHashMap#entrySet()
150          * @tests java.util.IdentityHashMap#keySet()
151          * @tests java.util.IdentityHashMap#values()
152          */
153         public void test_sets() {
154                 // tests with null keys and values
155                 IdentityHashMap map = new IdentityHashMap();
156
157                 // null key and null value
158                 map.put("key", "value");
159                 map.put(null, null);
160                 map.put("a key", null);
161                 map.put("another key", null);
162
163                 Set keyset = map.keySet();
164                 Collection valueset = map.values();
165                 Set entries = map.entrySet();
166                 Iterator it = entries.iterator();
167                 while (it.hasNext()) {
168                         Map.Entry entry = (Map.Entry) it.next();
169                         assertTrue("EntrySetIterator can not find entry ", entries
170                                         .contains(entry));
171
172                         assertTrue("entry key not found in map", map.containsKey(entry
173                                         .getKey()));
174                         assertTrue("entry value not found in map", map.containsValue(entry
175                                         .getValue()));
176
177                         assertTrue("entry key not found in the keyset", keyset
178                                         .contains(entry.getKey()));
179                         assertTrue("entry value not found in the valueset", valueset
180                                         .contains(entry.getValue()));
181                 }
182         }
183
184         /**
185          * @tests java.util.IdentityHashMap#entrySet()
186          * @tests java.util.IdentityHashMap#remove(java.lang.Object)
187          */
188         public void test_entrySet_removeAll() {
189                 IdentityHashMap map = new IdentityHashMap();
190                 for (int i = 0; i < 1000; i++) {
191                         map.put(new Integer(i), new Integer(i));
192                 }
193                 Set set = map.entrySet();
194
195                 set.removeAll(set);
196                 assertEquals("did not remove all elements in the map", 0, map.size());
197                 assertTrue("did not remove all elements in the entryset", set.isEmpty());
198
199                 Iterator it = set.iterator();
200                 assertTrue("entrySet iterator still has elements", !it.hasNext());
201         }
202
203         /**
204          * @tests java.util.IdentityHashMap#keySet()
205          * @tests java.util.IdentityHashMap#clear()
206          */
207         public void test_keySet_clear() {
208                 IdentityHashMap map = new IdentityHashMap();
209                 for (int i = 0; i < 1000; i++) {
210                         map.put(new Integer(i), new Integer(i));
211                 }
212                 Set set = map.keySet();
213                 set.clear();
214
215                 assertEquals("did not remove all elements in the map", 0, map.size());
216                 assertTrue("did not remove all elements in the keyset", set.isEmpty());
217
218                 Iterator it = set.iterator();
219                 assertTrue("keySet iterator still has elements", !it.hasNext());
220         }
221
222         /**
223          * @tests java.util.IdentityHashMap#values()
224          */
225         public void test_values() {
226
227                 IdentityHashMap map = new IdentityHashMap();
228                 for (int i = 0; i < 10; i++) {
229                         map.put(new Integer(i), new Integer(i));
230                 }
231
232                 Integer key = new Integer(20);
233                 Integer value = new Integer(40);
234                 map.put(key, value);
235
236                 Collection vals = map.values();
237                 boolean result = vals.remove(key);
238                 assertTrue("removed entries incorrectly", map.size() == 11 && !result);
239                 assertTrue("removed key incorrectly", map.containsKey(key));
240                 assertTrue("removed value incorrectly", map.containsValue(value));
241
242                 result = vals.remove(value);
243                 assertTrue("Did not remove entry as expected", map.size() == 10
244                                 && result);
245                 assertTrue("Did not remove key as expected", !map.containsKey(key));
246                 assertTrue("Did not remove value as expected", !map
247                                 .containsValue(value));
248
249                 // put an equivalent key to a value
250                 key = new Integer(1);
251                 value = new Integer(100);
252                 map.put(key, value);
253
254                 result = vals.remove(key);
255                 assertTrue("TestB. removed entries incorrectly", map.size() == 11
256                                 && !result);
257                 assertTrue("TestB. removed key incorrectly", map.containsKey(key));
258                 assertTrue("TestB. removed value incorrectly", map.containsValue(value));
259
260                 result = vals.remove(value);
261                 assertTrue("TestB. Did not remove entry as expected", map.size() == 10
262                                 && result);
263                 assertTrue("TestB. Did not remove key as expected", !map
264                                 .containsKey(key));
265                 assertTrue("TestB. Did not remove value as expected", !map
266                                 .containsValue(value));
267
268                 vals.clear();
269                 assertEquals("Did not remove all entries as expected", 0, map.size());
270         }
271
272         /**
273          * @tests java.util.IdentityHashMap#keySet()
274          * @tests java.util.IdentityHashMap#remove(java.lang.Object)
275          */
276         public void test_keySet_removeAll() {
277                 IdentityHashMap map = new IdentityHashMap();
278                 for (int i = 0; i < 1000; i++) {
279                         map.put(new Integer(i), new Integer(i));
280                 }
281                 Set set = map.keySet();
282                 set.removeAll(set);
283
284                 assertEquals("did not remove all elements in the map", 0, map.size());
285                 assertTrue("did not remove all elements in the keyset", set.isEmpty());
286
287                 Iterator it = set.iterator();
288                 assertTrue("keySet iterator still has elements", !it.hasNext());
289         }
290
291         /**
292          * @tests java.util.IdentityHashMap#keySet()
293          */
294         public void test_keySet_retainAll() {
295                 IdentityHashMap map = new IdentityHashMap();
296                 for (int i = 0; i < 1000; i++) {
297                         map.put(new Integer(i), new Integer(i));
298                 }
299                 Set set = map.keySet();
300
301                 // retain all the elements
302                 boolean result = set.retainAll(set);
303                 assertTrue("retain all should return false", !result);
304                 assertEquals("did not retain all", 1000, set.size());
305
306                 // send empty set to retainAll
307                 result = set.retainAll(new TreeSet());
308                 assertTrue("retain all should return true", result);
309                 assertEquals("did not remove all elements in the map", 0, map.size());
310                 assertTrue("did not remove all elements in the keyset", set.isEmpty());
311
312                 Iterator it = set.iterator();
313                 assertTrue("keySet iterator still has elements", !it.hasNext());
314         }
315
316         /**
317          * @tests java.util.IdentityHashMap#keySet()
318          * @tests java.util.IdentityHashMap#remove(java.lang.Object)
319          */
320         public void test_keyset_remove() {
321                 IdentityHashMap map = new IdentityHashMap();
322
323                 Integer key = new Integer(21);
324
325                 map.put(new Integer(1), null);
326                 map.put(new Integer(11), null);
327                 map.put(key, null);
328                 map.put(new Integer(31), null);
329                 map.put(new Integer(41), null);
330                 map.put(new Integer(51), null);
331                 map.put(new Integer(61), null);
332                 map.put(new Integer(71), null);
333                 map.put(new Integer(81), null);
334                 map.put(new Integer(91), null);
335
336                 Set set = map.keySet();
337
338                 Set newset = new HashSet();
339                 Iterator it = set.iterator();
340                 while (it.hasNext()) {
341                         Object element = it.next();
342                         if (element == key) {
343                                 it.remove();
344                         } else
345                                 newset.add(element);
346                 }
347                 int size = newset.size();
348                 assertTrue("keyset and newset don't have same size",
349                                 newset.size() == size);
350                 assertTrue("element is in newset ", !newset.contains(key));
351                 assertTrue("element not removed from keyset", !set.contains(key));
352                 assertTrue("element not removed from map", !map.containsKey(key));
353
354                 assertTrue("newset and keyset do not have same elements 1", newset
355                                 .equals(set));
356                 assertTrue("newset and keyset do not have same elements 2", set
357                                 .equals(newset));
358         }
359
360     public void test_clone_scenario1() {
361         IdentityHashMap hashMap = new IdentityHashMap();
362         assertEquals(0, hashMap.hashCode());
363         Object cloneHashMap = hashMap.clone();
364         ((IdentityHashMap) cloneHashMap).put("key", "value");
365         assertEquals(0, hashMap.hashCode());
366         assertTrue(0 != cloneHashMap.hashCode());
367     }
368
369     public void test_clone_scenario2() {
370         IdentityHashMap hashMap = new IdentityHashMap();
371         assertEquals(0, hashMap.hashCode());
372         Object cloneHashMap = hashMap.clone();
373         hashMap.put("key", "value");
374         assertEquals(1, hashMap.size());
375         assertEquals(0, ((IdentityHashMap) cloneHashMap).size());
376         assertEquals("value", hashMap.get("key"));
377         assertNull(((IdentityHashMap) cloneHashMap).get("key"));
378         assertTrue(0 != hashMap.hashCode());
379         assertEquals(0, cloneHashMap.hashCode());
380     }
381
382     public void test_clone_scenario3() {
383         IdentityHashMap hashMap = new IdentityHashMap();
384         assertEquals(0, hashMap.hashCode());
385         hashMap.put("key", "value");
386         Object cloneHashMap = hashMap.clone();
387         assertEquals(1, hashMap.size());
388         assertEquals(1, ((IdentityHashMap) cloneHashMap).size());
389         assertEquals("value", hashMap.get("key"));
390         assertEquals("value", ((IdentityHashMap) cloneHashMap).get("key"));
391         assertEquals(hashMap.hashCode(), cloneHashMap.hashCode());
392     }
393
394     public void test_clone_scenario4() {
395         IdentityHashMap hashMap = new IdentityHashMap();
396         Object cloneHashMap = hashMap.clone();
397         assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
398         hashMap.put((Object) null, cloneHashMap);
399         assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
400         assertEquals(cloneHashMap, hashMap.get((Object) null));
401     }
402
403     public void test_clone_scenario5() throws Exception {
404         IdentityHashMap hashMap = new IdentityHashMap();
405         Object cloneHashMap = hashMap.clone();
406         assertNull(hashMap.remove((Object) null));
407         ((IdentityHashMap) cloneHashMap).put((Object) null, cloneHashMap);
408         assertNull(hashMap.remove((Object) null));
409         assertEquals(cloneHashMap, ((IdentityHashMap) cloneHashMap)
410                 .get((Object) null));
411     }
412
413     // comparator for IdentityHashMap objects
414     private static final SerializableAssert COMPARATOR = new SerializableAssert() {
415         public void assertDeserialized(Serializable initial,
416                 Serializable deserialized) {
417
418             IdentityHashMap init = (IdentityHashMap) initial;
419             IdentityHashMap desr = (IdentityHashMap) deserialized;
420
421             assertEquals("Size", init.size(), desr.size());
422         }
423     };
424
425     /**
426      * @tests serialization/deserialization compatibility with RI.
427      */
428     public void testSerializationCompatibility() throws Exception {
429         IdentityHashMap<String, String> identityHashMap = new IdentityHashMap<String, String>();
430         identityHashMap.put("key1", "value1");
431         identityHashMap.put("key2", "value2");
432         identityHashMap.put("key3", "value3");
433
434         SerializationTest.verifyGolden(this, identityHashMap, COMPARATOR);
435     }
436 }