From 662f923d7430d1056ffc04e019125f32b570644f Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Mon, 16 Mar 2026 21:45:29 -0400 Subject: [PATCH] fix: `EqualFunc` not deterministic The `ExampleEqualFunc_badEqualFunc` was non-deterministic, because the hashes used in the `CustomTable` could (by chance) map "Rob" and "Robert" to the same slot. Updated the test to use a deterministic hash. --- compare_example_test.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/compare_example_test.go b/compare_example_test.go index 363bab7..b41cb3e 100644 --- a/compare_example_test.go +++ b/compare_example_test.go @@ -13,11 +13,22 @@ import ( func ExampleEqualFunc_badEqualFunc() { type User struct{ ID, Name string } + makeHash := func(seed uint64) cuckoo.Hash[User] { + return func(u User) uint64 { + digest := seed + + for _, c := range u.ID + u.Name { + digest ^= uint64(c) + } + + return digest + } + } + // Two users with the same ID are equal. isEqual := func(a, b User) bool { return a.ID == b.ID } - hashA, hashB := cuckoo.NewDefaultHash[User](), cuckoo.NewDefaultHash[User]() - userbase := cuckoo.NewCustomTable[User, bool](hashA, hashB, isEqual) + userbase := cuckoo.NewCustomTable[User, bool](makeHash(1), makeHash(2), isEqual) (userbase.Put(User{"1", "Robert Doe"}, true)) -- 2.49.1