fix: EqualFunc not deterministic (#2)
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:
@@ -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))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user