chore: move from tools/dsa (#1)
Moved the implementation of this hash table from `tools/dsa` #1. Reviewed-on: #1 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 #1.
This commit is contained in:
49
cuckoo_fuzz_test.go
Normal file
49
cuckoo_fuzz_test.go
Normal file
@@ -0,0 +1,49 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user