fix: EqualFunc not deterministic
All checks were successful
CI / lint (pull_request) Successful in 50s
CI / unit-test (pull_request) Successful in 25s
CI / fuzz-test (pull_request) Successful in 1m1s
CI / mutation-test (pull_request) Successful in 43s

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.
This commit is contained in:
2026-03-16 21:45:29 -04:00
parent 717408239b
commit 662f923d74

View File

@@ -13,11 +13,22 @@ import (
func ExampleEqualFunc_badEqualFunc() { func ExampleEqualFunc_badEqualFunc() {
type User struct{ ID, Name string } 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. // Two users with the same ID are equal.
isEqual := func(a, b User) bool { return a.ID == b.ID } isEqual := func(a, b User) bool { return a.ID == b.ID }
hashA, hashB := cuckoo.NewDefaultHash[User](), cuckoo.NewDefaultHash[User]() userbase := cuckoo.NewCustomTable[User, bool](makeHash(1), makeHash(2), isEqual)
userbase := cuckoo.NewCustomTable[User, bool](hashA, hashB, isEqual)
(userbase.Put(User{"1", "Robert Doe"}, true)) (userbase.Put(User{"1", "Robert Doe"}, true))