Files
go-cuckoo/cuckoo_fuzz_test.go
M.V. Hutz b762417b80
All checks were successful
CI / lint (pull_request) Successful in 51s
CI / unit-test (pull_request) Successful in 25s
CI / mutation-test (pull_request) Successful in 2m44s
CI / fuzz-test (pull_request) Successful in 1m2s
chore: move from tools/dsa
Moved the implementation of this hash table from `tools/dsa` #1.
2026-03-16 21:10:08 -04:00

50 lines
1007 B
Go

package cuckoo_test
import (
"bytes"
"encoding/binary"
"testing"
"github.com/stretchr/testify/assert"
"git.maximhutz.com/tools/go-cuckoo"
)
func offsetHash(seed uint32) cuckoo.Hash[uint32] {
return func(x uint32) uint64 {
v := uint64(x) ^ uint64(seed)
v = (v ^ (v >> 30)) * 0xbf58476d1ce4e5b9
v = (v ^ (v >> 27)) * 0x94d049bb133111eb
return v ^ (v >> 31)
}
}
func FuzzInsertLookup(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte, seedA, seedB uint32) {
assert := assert.New(t)
table := cuckoo.NewCustomTable[uint32, uint32](
offsetHash(seedA),
offsetHash(seedB),
func(a, b uint32) bool { return a == b },
)
if seedA == seedB {
return
}
r := bytes.NewReader(data)
var key, value uint32
for binary.Read(r, binary.LittleEndian, &key) == nil &&
binary.Read(r, binary.LittleEndian, &value) == nil {
err := table.Put(key, value)
assert.NoError(err)
found, err := table.Get(key)
assert.NoError(err)
assert.Equal(value, found)
}
})
}