Woodstock Blog

a tech blog for general algorithmic interview questions

[Testing] Test hashCode() Function


How to test hashCode() function? Example:

public int hashCode(){
    int result = 17 + hashDouble(re);
    result = 31 * result + hashDouble(im);
    return result;


We need to test that the hash function is reflexive, symmetric, and transitive.


Not including ‘not equal’ test.

public void testEquals_Symmetric() {
    Person x = new Person("Foo Bar");  // equals and hashCode check name field value
    Person y = new Person("Foo Bar");
    Assert.assertTrue(x.equals(y) && y.equals(x));
    Assert.assertTrue(x.hashCode() == y.hashCode());