Maps

Tests that compare all elements of two map objects. This includes tests for HashMap and TreeMap.

Overview

The tester checks whether the objects to be compared are instances of a class that implements the Java Map interface. This includes the HashMap and TreeMap classes in the Java Collections Framework. For these objects the tester uses the Map methods to determine whether the two maps are of the same size, validates that they define the same key sets, and then validates that for each key-value pair in the first map there exists a matching key-value pair in the second map.

The class ExamplesMaps contains all test cases.

Code sources

Here is the complete source code for this test suite.

You can also download the entire souce code as a zip file.

Complete test results are shown here.

Brief Examples

Here is an example comparing map objects.

public class ExamplesMaps {

    HashMap replies = makeReplies();

    HashMap makeReplies() {
        HashMap tmp = new HashMap();
	tmp.put("who", who);
        tmp.put("why", why);
        tmp.put("when", when);
	return tmp;
    }

    HashMap replies2 = makeReplies2();

    HashMap makeReplies2(){
	HashMap tmp = new HashMap();
       tmp.put("why", why);
        tmp.put("who", who);
	return tmp;
    }

    TreeMap replies3 = makeReplies3();

    TreeMap makeReplies3() {
	TreeMap tmp = new TreeMap();
	tmp.put("who", who);
	tmp.put("why", why);
	tmp.put("when", when);
	return tmp;
    }

    TreeMap replies4 = makeReplies4();

    TreeMap makeReplies4(){
	TreeMap tmp = new TreeMap();
	tmp.put("why", why);
	tmp.put("who", who);
	return tmp;
    }

    public void testHashMap(Tester t) {
	t.checkFail(replies, replies2, "Test to fail: Different hash maps");

	replies2.put("when", when);

	t.checkExpect(replies, replies2, "Success: Same hash maps");
    }

    public void testTreeMap(Tester t) {
	t.checkFail(replies3, replies4, "Test to fail: Different tree maps");

	replies4.put("when", when);

	t.checkExpect(replies3, replies4, "Success: Same tree maps");
    }
}

back

Last updated: April 1, 2011 10:50 am