From d4acdda95b61c20fd2541b9d038ac107f63f770d Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 19:54:16 -0400 Subject: [PATCH 01/12] feat: implement drop functionality - Added `drop()` function in buckets. - Implemented `Drop()` function for Table. --- bucket.go | 11 +++++++++++ table.go | 13 +++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/bucket.go b/bucket.go index 63a6681..9bf30a2 100644 --- a/bucket.go +++ b/bucket.go @@ -26,6 +26,17 @@ func (b bucket[K, V]) get(key K) (value V, found bool) { return slot.value, slot.occupied && b.compare(slot.key, key) } +func (b bucket[K, V]) drop(key K) (occupied bool) { + slot := &b.slots[b.location(key)] + + if slot.occupied && b.compare(slot.key, key) { + slot.occupied = false + return true + } + + return false +} + func (b *bucket[K, V]) resize(capacity uint64) { b.slots = make([]slot[K, V], capacity) b.capacity = capacity diff --git a/table.go b/table.go index 78ef2ae..bc56a17 100644 --- a/table.go +++ b/table.go @@ -111,10 +111,15 @@ func (t *Table[K, V]) Put(key K, value V) (err error) { // Drop removes a value for a key in the table. Returns an error if its value // cannot be removed. -// -// Deprecated: Do not use. -func (t Table[K, V]) Drop(_ K) { - panic("Not implemented") +func (t Table[K, V]) Drop(key K) (err error) { + t.bucketA.drop(key) + t.bucketB.drop(key) + + if t.load() < t.minLoadFactor { + return t.resize() + } + + return nil } // Entries returns an unordered sequence of all key-value pairs in the table. -- 2.49.1 From 74ed81761cb4b680236157c64a4e92665b7d092f Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 20:01:14 -0400 Subject: [PATCH 02/12] fix: drop() decrements bucket size --- bucket.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bucket.go b/bucket.go index 9bf30a2..9724893 100644 --- a/bucket.go +++ b/bucket.go @@ -26,11 +26,12 @@ func (b bucket[K, V]) get(key K) (value V, found bool) { return slot.value, slot.occupied && b.compare(slot.key, key) } -func (b bucket[K, V]) drop(key K) (occupied bool) { +func (b *bucket[K, V]) drop(key K) (occupied bool) { slot := &b.slots[b.location(key)] if slot.occupied && b.compare(slot.key, key) { slot.occupied = false + b.size-- return true } -- 2.49.1 From c1314f8a3ced54c39c9619f613d670404590f328 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 20:01:50 -0400 Subject: [PATCH 03/12] fix: resize of Table.Drop() now decreases size - Generalized Table.resize() to accept a capacity size for each bucket. --- cuckoo_internal_test.go | 2 +- cuckoo_test.go | 19 ++++++++++++++++--- table.go | 20 ++++++++++---------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cuckoo_internal_test.go b/cuckoo_internal_test.go index fde14ee..f2b24a3 100644 --- a/cuckoo_internal_test.go +++ b/cuckoo_internal_test.go @@ -25,6 +25,6 @@ func TestLoad(t *testing.T) { for i := range 16 { err := table.Put(i, true) assert.NoError(err) - assert.Equal(float64(table.Size())/float64(table.Capacity()), table.load()) + assert.Equal(float64(table.Size())/float64(table.TotalCapacity()), table.load()) } } diff --git a/cuckoo_test.go b/cuckoo_test.go index 3f9c7ec..26bcf71 100644 --- a/cuckoo_test.go +++ b/cuckoo_test.go @@ -64,7 +64,7 @@ func TestStartingCapacity(t *testing.T) { assert := assert.New(t) table := cuckoo.NewTable[int, bool](cuckoo.Capacity(64)) - assert.Equal(uint64(128), table.Capacity()) + assert.Equal(uint64(128), table.TotalCapacity()) } func TestResizeCapacity(t *testing.T) { @@ -74,12 +74,12 @@ func TestResizeCapacity(t *testing.T) { cuckoo.GrowthFactor(2), ) - for table.Capacity() == 16 { + for table.TotalCapacity() == 16 { err := table.Put(rand.Int(), true) assert.NoError(err) } - assert.Equal(uint64(32), table.Capacity()) + assert.Equal(uint64(32), table.TotalCapacity()) } func TestPutMany(t *testing.T) { @@ -128,3 +128,16 @@ func TestRemove(t *testing.T) { assert.True(table.Has(0)) } + +func TestDropItem(t *testing.T) { + assert := assert.New(t) + key, value := 0, true + table := cuckoo.NewTable[int, bool]() + (table.Put(key, value)) + + err := table.Drop(key) + + assert.NoError(err) + assert.Equal(0, table.Size()) + assert.False(table.Has(key)) +} diff --git a/table.go b/table.go index bc56a17..945e102 100644 --- a/table.go +++ b/table.go @@ -16,9 +16,9 @@ type Table[K, V any] struct { minLoadFactor float64 } -// Capacity returns the number of slots allocated for the [Table]. To get the +// TotalCapacity returns the number of slots allocated for the [Table]. To get the // number of slots filled, look at [Table.Size]. -func (t Table[K, V]) Capacity() uint64 { +func (t Table[K, V]) TotalCapacity() uint64 { return t.bucketA.capacity + t.bucketB.capacity } @@ -32,21 +32,21 @@ func log2(n uint64) (m int) { } func (t Table[K, V]) maxEvictions() int { - return 3 * log2(t.Capacity()) + return 3 * log2(t.TotalCapacity()) } func (t Table[K, V]) load() float64 { - return float64(t.Size()) / float64(t.Capacity()) + return float64(t.Size()) / float64(t.TotalCapacity()) } -func (t *Table[K, V]) resize() error { +func (t *Table[K, V]) resize(capacity uint64) error { entries := make([]entry[K, V], 0, t.Size()) for k, v := range t.Entries() { entries = append(entries, entry[K, V]{k, v}) } - t.bucketA.resize(t.growthFactor * t.bucketA.capacity) - t.bucketB.resize(t.growthFactor * t.bucketB.capacity) + t.bucketA.resize(capacity) + t.bucketB.resize(capacity) for _, entry := range entries { if err := t.Put(entry.key, entry.value); err != nil { @@ -99,10 +99,10 @@ func (t *Table[K, V]) Put(key K, value V) (err error) { } if t.load() < t.minLoadFactor { - return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.Capacity(), t.load()) + return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.TotalCapacity(), t.load()) } - if err := t.resize(); err != nil { + if err := t.resize(t.growthFactor * t.bucketA.capacity); err != nil { return err } @@ -116,7 +116,7 @@ func (t Table[K, V]) Drop(key K) (err error) { t.bucketB.drop(key) if t.load() < t.minLoadFactor { - return t.resize() + return t.resize(t.bucketA.capacity / t.growthFactor) } return nil -- 2.49.1 From e2ba398a620fedc5d7f1668a2a2e99d90f4811c6 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 20:05:35 -0400 Subject: [PATCH 04/12] fix: Table.Drop() was not a pointer receiver --- table.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/table.go b/table.go index 945e102..a978902 100644 --- a/table.go +++ b/table.go @@ -111,7 +111,7 @@ func (t *Table[K, V]) Put(key K, value V) (err error) { // Drop removes a value for a key in the table. Returns an error if its value // cannot be removed. -func (t Table[K, V]) Drop(key K) (err error) { +func (t *Table[K, V]) Drop(key K) (err error) { t.bucketA.drop(key) t.bucketB.drop(key) -- 2.49.1 From bb874a2aba25a6c24f5c042fb2c81e46ceeebd7e Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 20:13:11 -0400 Subject: [PATCH 05/12] 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) -- 2.49.1 From cdb5efb4a3721d72d71e7f452fa9d3d4dccc9bd6 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 20:46:31 -0400 Subject: [PATCH 06/12] fix: allow 0 capacity for table - Added guards at the bucket level, to ensure that getting an item in an empty bucket doesn't cause an error. - Added grow() and shrink() functions to Table, to prevent a capacity of 0 from not being able to grow. - Updated the fuzz test to use `go_fuzz-utils`. --- bucket.go | 16 +++++ cuckoo_fuzz_test.go | 60 +++++++++++-------- go.mod | 1 + go.sum | 2 + table.go | 28 ++++++++- .../fuzz/FuzzInsertLookup/663e746d1518e2f5 | 2 + .../fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 | 4 -- 7 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 delete mode 100644 testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 diff --git a/bucket.go b/bucket.go index 9724893..15dd2d7 100644 --- a/bucket.go +++ b/bucket.go @@ -22,11 +22,19 @@ func (b bucket[K, V]) location(key K) uint64 { } func (b bucket[K, V]) get(key K) (value V, found bool) { + if b.capacity == 0 { + return + } + slot := b.slots[b.location(key)] return slot.value, slot.occupied && b.compare(slot.key, key) } func (b *bucket[K, V]) drop(key K) (occupied bool) { + if b.capacity == 0 { + return + } + slot := &b.slots[b.location(key)] if slot.occupied && b.compare(slot.key, key) { @@ -45,6 +53,10 @@ func (b *bucket[K, V]) resize(capacity uint64) { } func (b bucket[K, V]) update(key K, value V) (updated bool) { + if b.capacity == 0 { + return + } + slot := &b.slots[b.location(key)] if slot.occupied && b.compare(slot.key, key) { @@ -56,6 +68,10 @@ func (b bucket[K, V]) update(key K, value V) (updated bool) { } func (b *bucket[K, V]) evict(insertion entry[K, V]) (evicted entry[K, V], eviction bool) { + if b.capacity == 0 { + return insertion, true + } + slot := &b.slots[b.location(insertion.key)] if !slot.occupied { diff --git a/cuckoo_fuzz_test.go b/cuckoo_fuzz_test.go index 8c9f427..fd97b2f 100644 --- a/cuckoo_fuzz_test.go +++ b/cuckoo_fuzz_test.go @@ -1,12 +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" ) @@ -20,49 +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) + if tp, err := go_fuzz_utils.NewTypeProvider(data); err != nil { + return + } else if err := tp.Fill(&scenario); err != nil { + return + } + + if scenario.seedA == scenario.seedB { + return + } + actual := cuckoo.NewCustomTable[uint32, uint32]( - offsetHash(seedA), - offsetHash(seedB), + offsetHash(scenario.seedA), + offsetHash(scenario.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 { - - if drop { - err := actual.Drop(key) + for _, step := range scenario.steps { + if step.drop { + err := actual.Drop(step.key) assert.NoError(err) - delete(expected, key) + delete(expected, step.key) - _, err = actual.Get(key) + _, err = actual.Get(step.key) assert.Error(err) } else { - err := actual.Put(key, value) + err := actual.Put(step.key, step.value) assert.NoError(err) - expected[key] = value + expected[step.key] = step.value - found, err := actual.Get(key) + found, err := actual.Get(step.key) assert.NoError(err) - assert.Equal(value, found) + assert.Equal(step.value, found) } - assert.Equal(expected, maps.Collect(actual.Entries())) - drop = !drop + assert.Equal(expected, maps.Collect(actual.Entries())) } }) } diff --git a/go.mod b/go.mod index 492bb0f..79459b8 100644 --- a/go.mod +++ b/go.mod @@ -7,5 +7,6 @@ require github.com/stretchr/testify v1.11.1 require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589 gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c4c1710..9c6f565 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589 h1:UmBZCTPdDYore2IEHN+U4eIqEaRq6METh9pKiPumkqc= +github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589/go.mod h1:zh+T+w9XT/3o4E0WLEGCdmLJ8Yqx/zY3o538tQY3OjY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/table.go b/table.go index a978902..9b64def 100644 --- a/table.go +++ b/table.go @@ -28,7 +28,7 @@ func (t Table[K, V]) Size() int { } func log2(n uint64) (m int) { - return bits.Len64(n) - 1 + return max(0, bits.Len64(n)-1) } func (t Table[K, V]) maxEvictions() int { @@ -36,6 +36,12 @@ func (t Table[K, V]) maxEvictions() int { } func (t Table[K, V]) load() float64 { + // When there are no slots in the table, we still treat the load as 100%. + // Every slot in the table is full. + if t.TotalCapacity() == 0 { + return 1.0 + } + return float64(t.Size()) / float64(t.TotalCapacity()) } @@ -57,6 +63,22 @@ func (t *Table[K, V]) resize(capacity uint64) error { return nil } +func (t *Table[K, V]) grow() error { + var newCapacity uint64 + + if t.TotalCapacity() == 0 { + newCapacity = 1 + } else { + newCapacity = t.bucketA.capacity * t.growthFactor + } + + return t.resize(newCapacity) +} + +func (t *Table[K, V]) shrink() error { + return t.resize(t.bucketA.capacity / t.growthFactor) +} + // Get fetches the value for a key in the [Table]. Returns an error if no value // is found. func (t Table[K, V]) Get(key K) (value V, err error) { @@ -102,7 +124,7 @@ func (t *Table[K, V]) Put(key K, value V) (err error) { return fmt.Errorf("bad hash: resize on load %d/%d = %f", t.Size(), t.TotalCapacity(), t.load()) } - if err := t.resize(t.growthFactor * t.bucketA.capacity); err != nil { + if err := t.grow(); err != nil { return err } @@ -116,7 +138,7 @@ func (t *Table[K, V]) Drop(key K) (err error) { t.bucketB.drop(key) if t.load() < t.minLoadFactor { - return t.resize(t.bucketA.capacity / t.growthFactor) + return t.shrink() } return nil diff --git a/testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 b/testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 new file mode 100644 index 0000000..fd87a4f --- /dev/null +++ b/testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 @@ -0,0 +1,2 @@ +go test fuzz v1 +[]byte("B000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000") diff --git a/testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 b/testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 deleted file mode 100644 index 12889a1..0000000 --- a/testdata/fuzz/FuzzInsertLookup/7753b9a0c9a15ea7 +++ /dev/null @@ -1,4 +0,0 @@ -go test fuzz v1 -[]byte("00000000000000000000000000000000000000000000000000000000000000000000000000000000") -uint32(51) -uint32(38) -- 2.49.1 From bd3f0aad04c3977afe863b478ac531cc1a3b8889 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 20:54:54 -0400 Subject: [PATCH 07/12] chore: removed testdata for fuzz test --- testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 diff --git a/testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 b/testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 deleted file mode 100644 index fd87a4f..0000000 --- a/testdata/fuzz/FuzzInsertLookup/663e746d1518e2f5 +++ /dev/null @@ -1,2 +0,0 @@ -go test fuzz v1 -[]byte("B000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000") -- 2.49.1 From 0c81bc5cae71fd71a07ddf441a78e863173a3ba1 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 21:06:12 -0400 Subject: [PATCH 08/12] docs: added comments to give context to functions --- bucket.go | 2 ++ table.go | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/bucket.go b/bucket.go index 15dd2d7..6a4171c 100644 --- a/bucket.go +++ b/bucket.go @@ -17,6 +17,8 @@ type bucket[K, V any] struct { compare EqualFunc[K] } +// location determines where in the bucket a certain key would be placed. If the +// capacity is 0, this will panic. func (b bucket[K, V]) location(key K) uint64 { return b.hash(key) % b.capacity } diff --git a/table.go b/table.go index 9b64def..3e2a1a6 100644 --- a/table.go +++ b/table.go @@ -45,6 +45,9 @@ func (t Table[K, V]) load() float64 { return float64(t.Size()) / float64(t.TotalCapacity()) } +// resize clears all buckets, changes the sizes of them to a specific capacity, +// and fills them back up again. It is a helper function for [Table.grow] and +// [Table.shrink]; use them instead. func (t *Table[K, V]) resize(capacity uint64) error { entries := make([]entry[K, V], 0, t.Size()) for k, v := range t.Entries() { @@ -63,6 +66,8 @@ func (t *Table[K, V]) resize(capacity uint64) error { return nil } +// grow increases the table's capacity by the [Table.growthFactor]. If the +// capacity is 0, it increases it to 1. func (t *Table[K, V]) grow() error { var newCapacity uint64 @@ -75,6 +80,8 @@ func (t *Table[K, V]) grow() error { return t.resize(newCapacity) } +// shrink reduces the table's capacity by the [Table.growthFactor]. It may +// reduce it down to 0. func (t *Table[K, V]) shrink() error { return t.resize(t.bucketA.capacity / t.growthFactor) } -- 2.49.1 From f636ce8911746b95d073e727ff41fc1485ead6ae Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 21:08:23 -0400 Subject: [PATCH 09/12] chore: go mod tidy --- go.mod | 2 ++ go.sum | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 79459b8..89a64a2 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.25.6 require github.com/stretchr/testify v1.11.1 +require github.com/kr/pretty v0.3.1 // indirect + require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect diff --git a/go.sum b/go.sum index 9c6f565..db6ed83 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,21 @@ +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= +github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589 h1:UmBZCTPdDYore2IEHN+U4eIqEaRq6METh9pKiPumkqc= github.com/trailofbits/go-fuzz-utils v0.0.0-20260318143407-0907cafe7589/go.mod h1:zh+T+w9XT/3o4E0WLEGCdmLJ8Yqx/zY3o538tQY3OjY= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -- 2.49.1 From afd3472d1d95d707520888680643c3d97b14b22e Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 21:20:18 -0400 Subject: [PATCH 10/12] ci: enforce 100% mutation coverage This will strongly indicate whether the test cases actually cover. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 2ea9847..54c38ee 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ test-unit: ## Run unit tests with coverage go test ./... -cover -v test-mutation: ## Run mutation tests with gremlins - gremlins unleash + gremlins unleash --threshold 1.0 FUZZ_TIME ?= 30 -- 2.49.1 From cd115ba7a20dd84e9a95539a37d0e47d74ac2164 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 21:23:16 -0400 Subject: [PATCH 11/12] fix: mutation test threshold should be in config There isn't a `--threshold` flag for `go-gremlins`. --- .gremlins.yaml | 4 +++- Makefile | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gremlins.yaml b/.gremlins.yaml index 7a1872f..3c777aa 100644 --- a/.gremlins.yaml +++ b/.gremlins.yaml @@ -4,4 +4,6 @@ unleash: timeout-coefficient: 50 workers: 4 - dry-run: false \ No newline at end of file + dry-run: false + threshold: + efficacy: 100 \ No newline at end of file diff --git a/Makefile b/Makefile index 54c38ee..2ea9847 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ test-unit: ## Run unit tests with coverage go test ./... -cover -v test-mutation: ## Run mutation tests with gremlins - gremlins unleash --threshold 1.0 + gremlins unleash FUZZ_TIME ?= 30 -- 2.49.1 From 09b5d9d8934337721501a3efbf4d761680b19ec7 Mon Sep 17 00:00:00 2001 From: "M.V. Hutz" Date: Thu, 19 Mar 2026 21:57:22 -0400 Subject: [PATCH 12/12] feat: 100% mutation test coverage - Fixed mutation coverage to be 1.0, instead of 100. --- .gremlins.yaml | 2 +- cuckoo_test.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 1 deletion(-) diff --git a/.gremlins.yaml b/.gremlins.yaml index 3c777aa..bbef13a 100644 --- a/.gremlins.yaml +++ b/.gremlins.yaml @@ -6,4 +6,4 @@ unleash: workers: 4 dry-run: false threshold: - efficacy: 100 \ No newline at end of file + efficacy: 1.0 \ No newline at end of file diff --git a/cuckoo_test.go b/cuckoo_test.go index 1c66b60..b4dfddb 100644 --- a/cuckoo_test.go +++ b/cuckoo_test.go @@ -1,6 +1,7 @@ package cuckoo_test import ( + "errors" "maps" "math/rand/v2" "testing" @@ -153,3 +154,88 @@ func TestDropNoItem(t *testing.T) { assert.Equal(0, table.Size()) assert.False(table.Has(key)) } + +func TestDropItemCapacity(t *testing.T) { + assert := assert.New(t) + key := 0 + table := cuckoo.NewTable[int, bool]( + cuckoo.Capacity(64), + cuckoo.GrowthFactor(2), + ) + + startingCapacity := table.TotalCapacity() + err := table.Drop(key) + endingCapacity := table.TotalCapacity() + + assert.NoError(err) + assert.Equal(0, table.Size()) + assert.Equal(uint64(128), startingCapacity) + assert.Equal(uint64(64), endingCapacity) +} + +func TestPutNoCapacity(t *testing.T) { + assert := assert.New(t) + key, value := 0, true + table := cuckoo.NewTable[int, bool]( + cuckoo.Capacity(0), + ) + + err := table.Put(key, value) + + assert.NoError(err) + assert.Equal(1, table.Size()) + assert.True(table.Has(key)) +} + +func TestBadHashCapacity(t *testing.T) { + assert := assert.New(t) + table := cuckoo.NewCustomTable[int, bool]( + func(int) uint64 { return 0 }, + func(int) uint64 { return 0 }, + func(a, b int) bool { return a == b }, + cuckoo.Capacity(20), + ) + + err1 := table.Put(0, true) + err2 := table.Put(1, true) + err3 := table.Put(2, true) + + assert.NoError(err1) + assert.NoError(err2) + assert.Error(err3) + + assert.Equal(uint64(80), table.TotalCapacity()) +} + +func TestDropResizeCapacity(t *testing.T) { + assert := assert.New(t) + table := cuckoo.NewTable[int, bool]( + cuckoo.Capacity(10), + ) + + err1 := table.Put(0, true) + err2 := table.Put(1, true) + err3 := table.Drop(1) + + assert.NoError(errors.Join(err1, err2, err3)) + assert.Equal(uint64(20), table.TotalCapacity()) +} + +func TestNewTableBy(t *testing.T) { + type User struct { + _ func() + id string + name string + } + + assert := assert.New(t) + table := cuckoo.NewTableBy[User, bool]( + func(u User) string { return u.id }, + ) + + err := table.Put(User{nil, "1", "Robert"}, true) + + assert.NoError(err) + assert.Equal(1, table.Size()) + assert.True(table.Has(User{nil, "1", "Robbie"})) +} -- 2.49.1