Woodstock Blog

a tech blog for general algorithmic interview questions

[Testing] Test hashCode() Function

Question

How to test hashCode() function? Example:

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

Solution

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

Code

Not including ‘not equal’ test.

@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());
}