Last active
August 29, 2015 14:05
-
-
Save grant/c20c49053c5f105c3945 to your computer and use it in GitHub Desktop.
Hashmap Equals
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Asserts that two maps are equal. | |
*/ | |
static public void assertEquals(Map<?, ?> actual, Map<?, ?> expected) { | |
if (actual == expected) { | |
return; | |
} | |
if (actual == null || expected == null) { | |
fail("Maps not equal: expected: " + expected + " and actual: " + actual); | |
} | |
if (actual.size() != expected.size()) { | |
fail("Maps do not have the same size:" + actual.size() + " != " + expected.size()); | |
} | |
Set<?> entrySet = actual.entrySet(); | |
for (Iterator<?> iterator = entrySet.iterator(); iterator.hasNext();) { | |
Map.Entry<?, ?> entry = (Map.Entry<?, ?>) iterator.next(); | |
Object key = entry.getKey(); | |
Object value = entry.getValue(); | |
Object expectedValue = expected.get(key); | |
assertEquals(value, expectedValue, "Maps do not match for key:" + key + " actual:" + value | |
+ " expected:" + expectedValue); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment