views:

351

answers:

6

Testing the Equals method is pretty much straight forward (as far as I know). But how on earth do you test the GetHashCode method?

A: 

I would pre-supply a known/expected hash and compare what the result of GetHashCode is.

Myles
That makes the test very fragile. For example, you should be able to make GetHashCode return the negated value of what it would have given in the previous version, and the method is still valid. Test what you care about - which is comparing hash codes of equal and non-equal values.
Jon Skeet
Good call, thanks for the comment.
Myles
+6  A: 

Test that two distinct objects which are equal have the same hash code (for various values). Check that non-equal objects give different hash codes, varying one aspect/property at a time. While the hash codes don't have to be different, you'd be really unlucky to pick different values for properties which happen to give the same hash code unless you've got a bug.

Jon Skeet
It's quite easy to get the same hash code for some built in types. For example new Point(1,1).GetHashCode() and new Point(2,2).GetHashCode() gives the same value...
Guffa
Just because someone else's code fails to produce well distributed hash values doesn't mean it's not a good test for your code.
Pete Kirkham
@Pete: Exactly. That's why I don't use XOR for my hash codes...
Jon Skeet
@Tony: What do you usually use then?
Svish
@Svish: I can't remember the name, but repeated multiplication and addition - find answers by me about GetHashCode and I'm sure you'll see plenty of examples :)
Jon Skeet
Yeah. Point pretty obviously has a bad implementation of the hash function ;)
TomTom
+3  A: 

It would be fairly similar to Equals(). You'd want to make sure two objects which were the "same" at least had the same hash code. That means if .Equals() returns true, the hash codes should be identical as well. As far as what the proper hashcode values are, that depends on how you're hashing.

Dave Markle
+1 - that is definitely one thing to test. Forget distribution, but same objects MUST have the same hash code.
TomTom
A: 

You create separate instances with the same value and check that the GetHashCode for the instances returns the same value, and that repeated calls on the same instance returns the same value.

That is the only requirement for a hash code to work. To work well the hash codes should of course have a good distribution, but testing for that requires a lot of testing...

Guffa
+2  A: 

From personal experience. Aside from obvious things like same objects giving you same hash codes, you need to create large enough array of unique objects and count unique hash codes among them. If unique hash codes make less than, say 50% of overall object count, then you are in trouble, as your hash function is not good.

        List<int> hashList = new List<int>(testObjectList.Count);
        for (int i = 0; i < testObjectList.Count; i++)
        {
            hashList.Add(testObjectList[i]);
        }

        hashList.Sort();
        int differentValues = 0;
        int curValue = hashList[0];
        for (int i = 1; i < hashList.Count; i++)
        {
            if (hashList[i] != curValue)
            {
                differentValues++;
                curValue = hashList[i];
            }
        }

        Assert.Greater(differentValues, hashList.Count/2);
Dmitry
+2  A: 

Gallio/MbUnit v3.2 comes with convenient contract verifiers which are able to test your implementation of GetHashCode() and IEquatable<T>. More specifically you may be interested by the EqualityContract and the HashCodeAcceptanceContract. See here, here and there for more details.

public class Spot
{
  private readonly int x;
  private readonly int y;

  public Spot(int x, int y)
  {
    this.x = x;
    this.y = y;
  }

  public override int GetHashCode()
  {
    int h = -2128831035;
    h = (h * 16777619) ^ x;
    h = (h * 16777619) ^ y;
    return h;
  }
}

Then you declare your contract verifier like this:

[TestFixture]
public class SpotTest
{
  [VerifyContract]
  public readonly IContract HashCodeAcceptanceTests = new HashCodeAcceptanceContract<Spot>()
  {
    CollisionProbabilityLimit = CollisionProbability.VeryLow,
    UniformDistributionQuality = UniformDistributionQuality.Excellent,
    DistinctInstances = DataGenerators.Join(Enumerable.Range(0, 1000), Enumerable.Range(0, 1000)).Select(o => new Spot(o.First, o.Second))
  };
}
Yann Trevin