1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.core;
18
19import junit.framework.TestCase;
20
21import java.util.HashMap;
22import java.util.Iterator;
23import android.test.suitebuilder.annotation.SmallTest;
24
25/**
26 * Test cases for Hashmap.
27 */
28public class HashMapTest extends TestCase {
29    private static final Integer ONE = new Integer(1);
30    private static final Integer TWO = new Integer(2);
31    private static final Integer THREE = new Integer(3);
32    private static final Integer FOUR = new Integer(4);
33
34    private void addItems(HashMap map) {
35        map.put("one", ONE);
36        map.put("two", TWO);
37        map.put("three", THREE);
38        map.put("four", FOUR);
39
40        assertEquals(4, map.size());
41
42        assertEquals(ONE, map.get("one"));
43        assertEquals(TWO, map.get("two"));
44        assertEquals(THREE, map.get("three"));
45        assertEquals(FOUR, map.get("four"));
46    }
47
48    /**
49     * checks if simple adding elements works.
50     */
51    @SmallTest
52    public void testAdd() throws Exception {
53        HashMap map = new HashMap();
54        addItems(map);
55    }
56
57    /**
58     * checks if clearing the map works.
59     */
60    @SmallTest
61    public void testClear() throws Exception {
62        HashMap map = new HashMap();
63
64        addItems(map);
65        map.clear();
66        assertEquals(0, map.size());
67    }
68
69    /**
70     * checks if removing an elemt works.
71     */
72    @SmallTest
73    public void testRemove() throws Exception {
74        HashMap map = new HashMap();
75
76        addItems(map);
77        map.remove("three");
78        assertNull(map.get("three"));
79    }
80
81    /**
82     * does some manipulation with a filled HashMap and checks
83     * if they work as intended
84     */
85    @SmallTest
86    public void testManipulate() throws Exception {
87        HashMap map = new HashMap();
88
89        assertTrue(map.isEmpty());
90        assertEquals(0, map.size());
91        assertNull(map.get(null));
92        assertNull(map.get("one"));
93        assertFalse(map.containsKey("one"));
94        assertFalse(map.containsValue(new Integer(1)));
95        assertNull(map.remove(null));
96        assertNull(map.remove("one"));
97
98        assertNull(map.put(null, new Integer(-1)));
99        assertNull(map.put("one", new Integer(1)));
100        assertNull(map.put("two", new Integer(2)));
101        assertNull(map.put("three", new Integer(3)));
102        assertEquals(-1, ((Integer) map.put(null, new Integer(0))).intValue());
103
104        assertEquals(0, ((Integer) map.get(null)).intValue());
105        assertEquals(1, ((Integer) map.get("one")).intValue());
106        assertEquals(2, ((Integer) map.get("two")).intValue());
107        assertEquals(3, ((Integer) map.get("three")).intValue());
108
109        assertTrue(map.containsKey(null));
110        assertTrue(map.containsKey("one"));
111        assertTrue(map.containsKey("two"));
112        assertTrue(map.containsKey("three"));
113
114        assertTrue(map.containsValue(new Integer(0)));
115        assertTrue(map.containsValue(new Integer(1)));
116        assertTrue(map.containsValue(new Integer(2)));
117        assertTrue(map.containsValue(new Integer(3)));
118
119        assertEquals(0, ((Integer) map.remove(null)).intValue());
120        assertEquals(1, ((Integer) map.remove("one")).intValue());
121        assertEquals(2, ((Integer) map.remove("two")).intValue());
122        assertEquals(3, ((Integer) map.remove("three")).intValue());
123
124        assertTrue(map.isEmpty());
125        assertEquals(0, map.size());
126        assertNull(map.get(null));
127        assertNull(map.get("one"));
128        assertFalse(map.containsKey("one"));
129        assertFalse(map.containsValue(new Integer(1)));
130        assertNull(map.remove(null));
131        assertNull(map.remove("one"));
132    }
133
134    /**
135     * checks if the key iterator of HashMaps work.
136     */
137    @SmallTest
138    public void testKeyIterator() throws Exception {
139        HashMap map = new HashMap();
140
141        boolean[] slots = new boolean[4];
142
143        addItems(map);
144
145        Iterator iter = map.keySet().iterator();
146
147        while (iter.hasNext()) {
148            int slot = 0;
149            Object key = iter.next();
150
151            if (key.equals("one"))
152                slot = 0;
153            else if (key.equals("two"))
154                slot = 1;
155            else if (key.equals("three"))
156                slot = 2;
157            else if (key.equals("four"))
158                slot = 3;
159            else
160                fail("Unkown key in hashmap");
161
162            if (slots[slot])
163                fail("key returned more than once");
164            else
165                slots[slot] = true;
166        }
167
168        assertTrue(slots[0]);
169        assertTrue(slots[1]);
170        assertTrue(slots[2]);
171        assertTrue(slots[3]);
172    }
173
174    /**
175     * checks if the value iterator works.
176     */
177    @SmallTest
178    public void testValueIterator() throws Exception {
179        HashMap map = new HashMap();
180
181        boolean[] slots = new boolean[4];
182
183        addItems(map);
184
185        Iterator iter = map.values().iterator();
186
187        while (iter.hasNext()) {
188            int slot = 0;
189            Object value = iter.next();
190
191            if (value.equals(ONE))
192                slot = 0;
193            else if (value.equals(TWO))
194                slot = 1;
195            else if (value.equals(THREE))
196                slot = 2;
197            else if (value.equals(FOUR))
198                slot = 3;
199            else
200                fail("Unkown value in hashmap");
201
202            if (slots[slot])
203                fail("value returned more than once");
204            else
205                slots[slot] = true;
206        }
207
208        assertTrue(slots[0]);
209        assertTrue(slots[1]);
210        assertTrue(slots[2]);
211        assertTrue(slots[3]);
212    }
213
214    /**
215     * checks if the entry iterator works for HashMaps.
216     */
217    @SmallTest
218    public void testEntryIterator() throws Exception {
219        HashMap map = new HashMap();
220
221        boolean[] slots = new boolean[4];
222
223        addItems(map);
224
225        Iterator iter = map.entrySet().iterator();
226
227        while (iter.hasNext()) {
228            int slot = 0;
229            Object entry = iter.next();
230
231            if (entry.toString().equals("one=1"))
232                slot = 0;
233            else if (entry.toString().equals("two=2"))
234                slot = 1;
235            else if (entry.toString().equals("three=3"))
236                slot = 2;
237            else if (entry.toString().equals("four=4"))
238                slot = 3;
239            else
240                fail("Unkown entry in hashmap");
241
242            if (slots[slot])
243                fail("entry returned more than once");
244            else
245                slots[slot] = true;
246        }
247
248        assertTrue(slots[0]);
249        assertTrue(slots[1]);
250        assertTrue(slots[2]);
251        assertTrue(slots[3]);
252    }
253
254    /**
255     * checks if the HashMap equals method works.
256     */
257    @SmallTest
258    public void testEquals() throws Exception {
259        HashMap map1 = new HashMap();
260        HashMap map2 = new HashMap();
261        HashMap map3 = new HashMap();
262
263        map1.put("one", "1");
264        map1.put("two", "2");
265        map1.put("three", "3");
266
267        map2.put("one", new String("1"));
268        map2.put(new String("two"), "2");
269        map2.put(new String("three"), new String("3"));
270
271        assertTrue(map1.equals(map2));
272
273        map3.put("one", "1");
274        map3.put("two", "1");
275        map3.put("three", "1");
276
277        assertFalse(map1.equals(map3));
278        assertFalse(map2.equals(map3));
279    }
280}
281
282