test: add Drop to fuzz test, add drop test cases
This commit is contained in:
4
Makefile
4
Makefile
@@ -16,10 +16,12 @@ test-unit: ## Run unit tests with coverage
|
||||
test-mutation: ## Run mutation tests with gremlins
|
||||
gremlins unleash
|
||||
|
||||
FUZZ_TIME ?= 30
|
||||
|
||||
test-fuzz: ## Run all fuzz tests for 30s each
|
||||
@for func in $$(grep -r --include='*_test.go' -oh 'func Fuzz\w*' . | sed 's/func //'); do \
|
||||
echo "Fuzzing $$func..."; \
|
||||
go test ./... -fuzz="^$$func$$" -fuzztime=30s; \
|
||||
go test ./... -fuzz="^$$func$$" -fuzztime=$(FUZZ_TIME)s; \
|
||||
done
|
||||
|
||||
test: test-unit test-mutation test-fuzz ## Run all tests
|
||||
|
||||
@@ -3,6 +3,7 @@ package cuckoo_test
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"maps"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
@@ -23,27 +24,45 @@ 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](
|
||||
actual := cuckoo.NewCustomTable[uint32, uint32](
|
||||
offsetHash(seedA),
|
||||
offsetHash(seedB),
|
||||
func(a, b uint32) bool { return a == b },
|
||||
)
|
||||
|
||||
expected := map[uint32]uint32{}
|
||||
|
||||
if seedA == seedB {
|
||||
return
|
||||
}
|
||||
|
||||
r := bytes.NewReader(data)
|
||||
var key, value uint32
|
||||
var drop bool
|
||||
for binary.Read(r, binary.LittleEndian, &key) == nil &&
|
||||
binary.Read(r, binary.LittleEndian, &value) == nil {
|
||||
|
||||
err := table.Put(key, value)
|
||||
assert.NoError(err)
|
||||
if drop {
|
||||
err := actual.Drop(key)
|
||||
assert.NoError(err)
|
||||
|
||||
found, err := table.Get(key)
|
||||
assert.NoError(err)
|
||||
assert.Equal(value, found)
|
||||
delete(expected, key)
|
||||
|
||||
_, err = actual.Get(key)
|
||||
assert.Error(err)
|
||||
} else {
|
||||
err := actual.Put(key, value)
|
||||
assert.NoError(err)
|
||||
|
||||
expected[key] = value
|
||||
|
||||
found, err := actual.Get(key)
|
||||
assert.NoError(err)
|
||||
assert.Equal(value, found)
|
||||
}
|
||||
assert.Equal(expected, maps.Collect(actual.Entries()))
|
||||
|
||||
drop = !drop
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -129,7 +129,7 @@ func TestRemove(t *testing.T) {
|
||||
assert.True(table.Has(0))
|
||||
}
|
||||
|
||||
func TestDropItem(t *testing.T) {
|
||||
func TestDropExistingItem(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
key, value := 0, true
|
||||
table := cuckoo.NewTable[int, bool]()
|
||||
@@ -141,3 +141,15 @@ func TestDropItem(t *testing.T) {
|
||||
assert.Equal(0, table.Size())
|
||||
assert.False(table.Has(key))
|
||||
}
|
||||
|
||||
func TestDropNoItem(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
key := 0
|
||||
table := cuckoo.NewTable[int, bool]()
|
||||
|
||||
err := table.Drop(key)
|
||||
|
||||
assert.NoError(err)
|
||||
assert.Equal(0, table.Size())
|
||||
assert.False(table.Has(key))
|
||||
}
|
||||
|
||||
4
testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7
vendored
Normal file
4
testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
go test fuzz v1
|
||||
[]byte("00000000000000000000000000000000000000000000000000000000000000000000000000000000")
|
||||
uint32(51)
|
||||
uint32(38)
|
||||
Reference in New Issue
Block a user