FileDocCategorySizeDatePackage
HashMapTest.javaAPI DocAndroid 1.5 API7992Wed May 06 22:42:02 BST 2009android.core

HashMapTest

public class HashMapTest extends TestCase
Test cases for Hashmap.

Fields Summary
private static final Integer
ONE
private static final Integer
TWO
private static final Integer
THREE
private static final Integer
FOUR
Constructors Summary
Methods Summary
private voidaddItems(java.util.HashMap map)


        
        map.put("one", ONE);
        map.put("two", TWO);
        map.put("three", THREE);
        map.put("four", FOUR);

        assertEquals(4, map.size());

        assertEquals(ONE, map.get("one"));
        assertEquals(TWO, map.get("two"));
        assertEquals(THREE, map.get("three"));
        assertEquals(FOUR, map.get("four"));
    
public voidtestAdd()
checks if simple adding elements works.

        HashMap map = new HashMap();
        addItems(map);
    
public voidtestClear()
checks if clearing the map works.

        HashMap map = new HashMap();

        addItems(map);
        map.clear();
        assertEquals(0, map.size());
    
public voidtestEntryIterator()
checks if the entry iterator works for HashMaps.

        HashMap map = new HashMap();

        boolean[] slots = new boolean[4];

        addItems(map);

        Iterator iter = map.entrySet().iterator();

        while (iter.hasNext()) {
            int slot = 0;
            Object entry = iter.next();

            if (entry.toString().equals("one=1"))
                slot = 0;
            else if (entry.toString().equals("two=2"))
                slot = 1;
            else if (entry.toString().equals("three=3"))
                slot = 2;
            else if (entry.toString().equals("four=4"))
                slot = 3;
            else
                fail("Unkown entry in hashmap");

            if (slots[slot])
                fail("entry returned more than once");
            else
                slots[slot] = true;
        }

        assertTrue(slots[0]);
        assertTrue(slots[1]);
        assertTrue(slots[2]);
        assertTrue(slots[3]);
    
public voidtestEquals()
checks if the HashMap equals method works.

        HashMap map1 = new HashMap();
        HashMap map2 = new HashMap();
        HashMap map3 = new HashMap();

        map1.put("one", "1");
        map1.put("two", "2");
        map1.put("three", "3");

        map2.put("one", new String("1"));
        map2.put(new String("two"), "2");
        map2.put(new String("three"), new String("3"));

        assertTrue(map1.equals(map2));

        map3.put("one", "1");
        map3.put("two", "1");
        map3.put("three", "1");

        assertFalse(map1.equals(map3));
        assertFalse(map2.equals(map3));
    
public voidtestKeyIterator()
checks if the key iterator of HashMaps work.

        HashMap map = new HashMap();

        boolean[] slots = new boolean[4];

        addItems(map);

        Iterator iter = map.keySet().iterator();

        while (iter.hasNext()) {
            int slot = 0;
            Object key = iter.next();

            if (key.equals("one"))
                slot = 0;
            else if (key.equals("two"))
                slot = 1;
            else if (key.equals("three"))
                slot = 2;
            else if (key.equals("four"))
                slot = 3;
            else
                fail("Unkown key in hashmap");

            if (slots[slot])
                fail("key returned more than once");
            else
                slots[slot] = true;
        }

        assertTrue(slots[0]);
        assertTrue(slots[1]);
        assertTrue(slots[2]);
        assertTrue(slots[3]);
    
public voidtestManipulate()
does some manipulation with a filled HashMap and checks if they work as intended

        HashMap map = new HashMap();

        assertTrue(map.isEmpty());
        assertEquals(0, map.size());
        assertNull(map.get(null));
        assertNull(map.get("one"));
        assertFalse(map.containsKey("one"));
        assertFalse(map.containsValue(new Integer(1)));
        assertNull(map.remove(null));
        assertNull(map.remove("one"));

        assertNull(map.put(null, new Integer(-1)));
        assertNull(map.put("one", new Integer(1)));
        assertNull(map.put("two", new Integer(2)));
        assertNull(map.put("three", new Integer(3)));
        assertEquals(-1, ((Integer) map.put(null, new Integer(0))).intValue());

        assertEquals(0, ((Integer) map.get(null)).intValue());
        assertEquals(1, ((Integer) map.get("one")).intValue());
        assertEquals(2, ((Integer) map.get("two")).intValue());
        assertEquals(3, ((Integer) map.get("three")).intValue());

        assertTrue(map.containsKey(null));
        assertTrue(map.containsKey("one"));
        assertTrue(map.containsKey("two"));
        assertTrue(map.containsKey("three"));

        assertTrue(map.containsValue(new Integer(0)));
        assertTrue(map.containsValue(new Integer(1)));
        assertTrue(map.containsValue(new Integer(2)));
        assertTrue(map.containsValue(new Integer(3)));

        assertEquals(0, ((Integer) map.remove(null)).intValue());
        assertEquals(1, ((Integer) map.remove("one")).intValue());
        assertEquals(2, ((Integer) map.remove("two")).intValue());
        assertEquals(3, ((Integer) map.remove("three")).intValue());

        assertTrue(map.isEmpty());
        assertEquals(0, map.size());
        assertNull(map.get(null));
        assertNull(map.get("one"));
        assertFalse(map.containsKey("one"));
        assertFalse(map.containsValue(new Integer(1)));
        assertNull(map.remove(null));
        assertNull(map.remove("one"));
    
public voidtestRemove()
checks if removing an elemt works.

        HashMap map = new HashMap();

        addItems(map);
        map.remove("three");
        assertNull(map.get("three"));
    
public voidtestValueIterator()
checks if the value iterator works.

        HashMap map = new HashMap();

        boolean[] slots = new boolean[4];

        addItems(map);

        Iterator iter = map.values().iterator();

        while (iter.hasNext()) {
            int slot = 0;
            Object value = iter.next();

            if (value.equals(ONE))
                slot = 0;
            else if (value.equals(TWO))
                slot = 1;
            else if (value.equals(THREE))
                slot = 2;
            else if (value.equals(FOUR))
                slot = 3;
            else
                fail("Unkown value in hashmap");

            if (slots[slot])
                fail("value returned more than once");
            else
                slots[slot] = true;
        }

        assertTrue(slots[0]);
        assertTrue(slots[1]);
        assertTrue(slots[2]);
        assertTrue(slots[3]);