From bb874a2aba25a6c24f5c042fb2c81e46ceeebd7e Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 20:13:11 -0400 Subject: [PATCH] test: add Drop to fuzz test, add drop test cases --- Makefile | 4 ++- cuckoo_fuzz_test.go | 31 +++++++++++++++---- cuckoo_test.go | 14 ++++++++- .../fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 | 4 +++ 4 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 diff --git a/Makefile b/Makefile index 57a7650..2ea9847 100644 --- a/Makefile +++ b/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 diff --git a/cuckoo_fuzz_test.go b/cuckoo_fuzz_test.go index 22cf07b..8c9f427 100644 --- a/cuckoo_fuzz_test.go +++ b/cuckoo_fuzz_test.go @@ -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 } }) } diff --git a/cuckoo_test.go b/cuckoo_test.go index 26bcf71..1c66b60 100644 --- a/cuckoo_test.go +++ b/cuckoo_test.go @@ -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)) +} diff --git a/testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 b/testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 new file mode 100644 index 0000000..12889a1 --- /dev/null +++ b/testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 @@ -0,0 +1,4 @@ +go test fuzz v1 +[]byte("00000000000000000000000000000000000000000000000000000000000000000000000000000000") +uint32(51) +uint32(38)