fix: EqualFunc not deterministic (#2)
All checks were successful
CI / lint (push) Successful in 51s
CI / unit-test (push) Successful in 25s
CI / fuzz-test (push) Successful in 1m2s
CI / mutation-test (push) Successful in 41s

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.

Reviewed-on: #2
Co-authored-by: M.V. Hutz <git@maximhutz.me>
Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #2.
This commit is contained in:
2026-03-17 01:54:24 +00:00
committed by Maxim Hutz
parent 717408239b
commit ea805b28e4

View File

@@ -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))