Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: How would you unit test GetHashCode?

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

like image 783
Svish Avatar asked Nov 08 '09 15:11

Svish


1 Answers

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.

like image 172
Jon Skeet Avatar answered Sep 27 '22 22:09

Jon Skeet