如何对 hashCode() 进行单元测试?
如何在单元测试中测试 hashCode() 函数?
public int hashCode(){
int result = 17 + hashDouble(re);
result = 31 * result + hashDouble(im);
return result;
}
How can I test the hashCode() function in unit testing?
public int hashCode(){
int result = 17 + hashDouble(re);
result = 31 * result + hashDouble(im);
return result;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
每当我重写 equals 和 hash 代码时,我都会遵循 Joshua Bloch 在“Effective Java”第 3 章中的建议编写单元测试。我确保 equals 和 hash 代码是自反的、对称的和传递的。我还确保“不等于”适用于所有数据成员。
当我检查 equals 调用时,我还确保 hashCode 的行为符合预期。像这样:
Whenever I override equals and hash code, I write unit tests that follow Joshua Bloch's recommendations in "Effective Java" Chapter 3. I make sure that equals and hash code are reflexive, symmetric, and transitive. I also make sure that "not equals" works properly for all the data members.
When I check the call to equals, I also make sure that the hashCode behaves as it should. Like this:
当您编写一般的数学函数(如哈希码)时,您会在测试中测试一些示例,直到您确信该函数按预期工作。有多少示例取决于您的功能。
对于哈希码函数,我认为您至少测试两个被认为相等的不同对象具有相同的哈希码。 与进一步一样
,您应该测试两个不同的值是否具有不同的哈希码,以避免像
return 1;
那样实现hashcode()
。When you write a mathematical function in general (like hash code) you test some examples in your tests until you are convinced that the function works as expected. How many examples that are depends on your function.
For a hash code function I'd think you test at least that two distinct objects, that are considered equal have the same hash code. Like
Further you should test that two different values have different hash codes to avoid implementing
hashcode()
likereturn 1;
.hashCode 被覆盖,以使 HashSet/HashMap 等具有相同字段的实例相同。
因此 Junit 测试应该断言具有相同值的两个不同实例返回相同的 hashCode。
hashCode is overrided so as to make instances with same fields identical for HashSet/HashMap etc.
So Junit test should assert that two different instances with same values return identical hashCode.
创建许多(数百万个)可重现的随机对象,并将所有 hashCode 添加到一个 Set 中,并检查您是否获得了几乎和许多不同的值作为生成 id 的数量。为了使它们可随机再现,请使用固定的随机种子。
另外,检查您是否可以将这些项目添加到 HashSet 中并再次找到它们。 (使用具有相同值的不同对象)
确保您的 equals() 与您的 hashCode() 行为匹配。我还会检查您的字段是否都是最终的。
Create many (millions of) reproduceably random objects and add all the hashCodes to a Set and check you get almost and many unqiue values as the number of generate ids. To make them reproduceable random use a fixed random seed.
Additionally check you can add these Items to a HashSet and find them again. (Using a differnt object with the same values)
Make sure your equals() matches your hashCode() behaviour. I would also check that your fields are all final.
我认为不需要对哈希码方法进行单元测试。特别是如果它是由您的 IDE 或
HashCodeBuilder
(apache 公共资源)I don't think there's a need to unit-test a hashcode method. Especially if it is generated by either your IDE or a
HashCodeBuilder
(apache commons)除了 @duffymo 测试哈希码的自反性、对称性和传递性之外,另一种测试方法是通过“Map”,这是哈希码实际上派上用场的地方。
Apart from @duffymo test for hashcode being reflexive, symmetric, and transitive, another way of testing would be via "Map" which is where hashcodes actually come handy.