public static void AssertEqualsHashCode(Object o, Object o2)
{
if (o.Equals(o2)) {
if (!o2.Equals(o)) {
Assert.Fail(
String.Empty + o + " equals " + o2 + " but not vice versa");
}
// Test for the guarantee that equal objects
// must have equal hash codes
if (o2.GetHashCode() != o.GetHashCode()) {
// Don't use Assert.AreEqual directly because it has
// quite a lot of overhead
Assert.Fail(
String.Empty + o + " and " + o2 + " don't have equal hash codes");
}
} else {
if (o2.Equals(o)) {
Assert.Fail(String.Empty + o + " does not equal " + o2 +
" but not vice versa");
}
// At least check that GetHashCode doesn't throw
try {
o.GetHashCode();
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
try {
o2.GetHashCode();
} catch (Exception ex) {
Assert.Fail(ex.ToString());
throw new InvalidOperationException(String.Empty, ex);
}
}
}