feat: add drop key functionality (#6)
Currently, the `Table.Drop()` function is deprecated because it is not implemented yet. Let's add that functionality. - Adds true drop functionality to the table, through `Table.Drop()`. - Adds tests for functionality. - Rewrites fuzz test using `go_fuzz_utils`, to test arbitrary usage patterns. - Rewrite `bucket` to allow a capacity of zero. - Rename `Table.Capacity()` to `Table.TotalCapacity()`, to reflect to different between the capacity of the buckets vs. the whole table. - Enforce 100% mutation test coverage. Reviewed-on: #6 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 #6.
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package cuckoo_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"maps"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
go_fuzz_utils "github.com/trailofbits/go-fuzz-utils"
|
||||
|
||||
"git.maximhutz.com/tools/go-cuckoo"
|
||||
)
|
||||
@@ -19,31 +19,60 @@ func offsetHash(seed uint32) cuckoo.Hash[uint32] {
|
||||
}
|
||||
}
|
||||
|
||||
type fuzzStep struct {
|
||||
drop bool
|
||||
key, value uint32
|
||||
}
|
||||
|
||||
type fuzzScenario struct {
|
||||
seedA, seedB uint32
|
||||
steps []fuzzStep
|
||||
}
|
||||
|
||||
func FuzzInsertLookup(f *testing.F) {
|
||||
f.Fuzz(func(t *testing.T, data []byte, seedA, seedB uint32) {
|
||||
f.Fuzz(func(t *testing.T, data []byte) {
|
||||
var scenario fuzzScenario
|
||||
assert := assert.New(t)
|
||||
|
||||
table := cuckoo.NewCustomTable[uint32, uint32](
|
||||
offsetHash(seedA),
|
||||
offsetHash(seedB),
|
||||
func(a, b uint32) bool { return a == b },
|
||||
)
|
||||
|
||||
if seedA == seedB {
|
||||
if tp, err := go_fuzz_utils.NewTypeProvider(data); err != nil {
|
||||
return
|
||||
} else if err := tp.Fill(&scenario); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
r := bytes.NewReader(data)
|
||||
var key, value uint32
|
||||
for binary.Read(r, binary.LittleEndian, &key) == nil &&
|
||||
binary.Read(r, binary.LittleEndian, &value) == nil {
|
||||
if scenario.seedA == scenario.seedB {
|
||||
return
|
||||
}
|
||||
|
||||
err := table.Put(key, value)
|
||||
assert.NoError(err)
|
||||
actual := cuckoo.NewCustomTable[uint32, uint32](
|
||||
offsetHash(scenario.seedA),
|
||||
offsetHash(scenario.seedB),
|
||||
func(a, b uint32) bool { return a == b },
|
||||
)
|
||||
|
||||
found, err := table.Get(key)
|
||||
assert.NoError(err)
|
||||
assert.Equal(value, found)
|
||||
expected := map[uint32]uint32{}
|
||||
|
||||
for _, step := range scenario.steps {
|
||||
if step.drop {
|
||||
err := actual.Drop(step.key)
|
||||
assert.NoError(err)
|
||||
|
||||
delete(expected, step.key)
|
||||
|
||||
_, err = actual.Get(step.key)
|
||||
assert.Error(err)
|
||||
} else {
|
||||
err := actual.Put(step.key, step.value)
|
||||
assert.NoError(err)
|
||||
|
||||
expected[step.key] = step.value
|
||||
|
||||
found, err := actual.Get(step.key)
|
||||
assert.NoError(err)
|
||||
assert.Equal(step.value, found)
|
||||
}
|
||||
|
||||
assert.Equal(expected, maps.Collect(actual.Entries()))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user