-
Notifications
You must be signed in to change notification settings - Fork 41
/
map_fuzz_test.go
99 lines (88 loc) · 2.26 KB
/
map_fuzz_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2023 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swiss
import (
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func FuzzStringMap(f *testing.F) {
f.Add(uint8(1), 14, 50)
f.Add(uint8(2), 1, 1)
f.Add(uint8(2), 14, 14)
f.Add(uint8(2), 14, 15)
f.Add(uint8(2), 25, 100)
f.Add(uint8(2), 25, 1000)
f.Add(uint8(8), 0, 1)
f.Add(uint8(8), 1, 1)
f.Add(uint8(8), 14, 14)
f.Add(uint8(8), 14, 15)
f.Add(uint8(8), 25, 100)
f.Add(uint8(8), 25, 1000)
f.Fuzz(func(t *testing.T, keySz uint8, init, count int) {
// smaller key sizes generate more overwrites
fuzzTestStringMap(t, uint32(keySz), uint32(init), uint32(count))
})
}
func fuzzTestStringMap(t *testing.T, keySz, init, count uint32) {
const limit = 1024 * 1024
if count > limit || init > limit {
t.Skip()
}
m := NewMap[string, int](init)
if count == 0 {
return
}
// make tests deterministic
setConstSeed(m, 1)
keys := genStringData(int(keySz), int(count))
golden := make(map[string]int, init)
for i, k := range keys {
m.Put(k, i)
golden[k] = i
}
assert.Equal(t, len(golden), m.Count())
for k, exp := range golden {
act, ok := m.Get(k)
assert.True(t, ok)
assert.Equal(t, exp, act)
}
for _, k := range keys {
_, ok := golden[k]
assert.True(t, ok)
assert.True(t, m.Has(k))
}
deletes := keys[:count/2]
for _, k := range deletes {
delete(golden, k)
m.Delete(k)
}
assert.Equal(t, len(golden), m.Count())
for _, k := range deletes {
assert.False(t, m.Has(k))
}
for k, exp := range golden {
act, ok := m.Get(k)
assert.True(t, ok)
assert.Equal(t, exp, act)
}
}
type hasher struct {
hash func()
seed uintptr
}
func setConstSeed[K comparable, V any](m *Map[K, V], seed uintptr) {
h := (*hasher)((unsafe.Pointer)(&m.hash))
h.seed = seed
}