SE251Ex:MultiMap TestMultiMap

From Marks Wiki
Jump to navigation Jump to search

Copy and paste the code below.

import java.util.Set;

import junit.framework.TestCase;

public class TestMultiMap extends TestCase {
    MultiMap<String, String> mmap;
    
    protected void setUp() {
        mmap = new MultiMap<String, String>();
    }
    
    protected void setUp2() {
        assertTrue(mmap.add("a", "one"));
        assertTrue(mmap.add("a", "two"));
        assertTrue(mmap.add("b", "two"));
        assertTrue(mmap.add("b", "three"));
        assertTrue(mmap.add("b", "four"));
        assertTrue(mmap.add("c", "one"));
        assertTrue(mmap.add("c", "three"));
        assertTrue(mmap.add("d", "four"));
    }
    
    public void testEmpty() {
        assertTrue(mmap.keySet().isEmpty());
        assertTrue(mmap.valueSet().isEmpty());
        Set<String> none = mmap.get("a");
        assertTrue(none.isEmpty());
        assertEquals("{}", mmap.toString());
    }
    
    public void testAdd() {
        assertTrue(mmap.add("a", "one"));
        assertTrue(mmap.add("a", "two"));
        assertTrue(mmap.add("b", "two"));
        assertFalse(mmap.add("a", "one"));
        assertTrue(mmap.add("c", "one"));
        assertFalse(mmap.add("b", "two"));
        
        Set<String> keys = mmap.keySet();
        assertEquals(3, keys.size());
        assertTrue(keys.contains("a"));
        assertTrue(keys.contains("b"));
        assertTrue(keys.contains("c"));
        
        Set<String> aValues = mmap.get("a");
        assertEquals(2, aValues.size());
        assertTrue(aValues.contains("one"));
        assertTrue(aValues.contains("two"));
        
        Set<String> bValues = mmap.get("b");
        assertEquals(1, bValues.size());
        assertTrue(bValues.contains("two"));
        
        Set<String> cValues = mmap.get("c");
        assertEquals(1, cValues.size());
        assertTrue(cValues.contains("one"));
    }
    
    public void testValueSet() {
        setUp2();
        
        Set<String> values = mmap.valueSet();
        assertEquals(4, values.size());
        assertTrue(values.contains("one"));
        assertTrue(values.contains("two"));
        assertTrue(values.contains("three"));
        assertTrue(values.contains("four"));
    }
    
    public void testRemove() {
        setUp2();
        
        assertTrue(mmap.remove("d", "four"));
        assertFalse(mmap.remove("d", "four"));
        assertTrue(mmap.remove("b", "four"));
        assertTrue(mmap.remove("a", "two"));
        assertFalse(mmap.remove("f", "one"));
        
        Set<String> keys = mmap.keySet();
        assertEquals(3, keys.size());
        assertTrue(keys.contains("a"));
        assertTrue(keys.contains("b"));
        assertTrue(keys.contains("c"));
        assertFalse(keys.contains("f"));

        Set<String> aValues = mmap.get("a");
        assertEquals(1, aValues.size());
        assertTrue(aValues.contains("one"));
        assertFalse(aValues.contains("two"));
        
        Set<String> bValues = mmap.get("b");
        assertEquals(2, bValues.size());
        assertTrue(bValues.contains("two"));
        assertTrue(bValues.contains("three"));
        assertFalse(bValues.contains("four"));
        
        Set<String> cValues = mmap.get("c");
        assertEquals(2, cValues.size());
        assertTrue(cValues.contains("one"));
        assertTrue(bValues.contains("three"));

        Set<String> values = mmap.valueSet();
        assertEquals(3, values.size());
        assertTrue(values.contains("one"));
        assertTrue(values.contains("two"));
        assertTrue(values.contains("three"));
        assertFalse(values.contains("four"));
    }
}