-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiter_test.go
281 lines (260 loc) · 5.66 KB
/
iter_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Modifications copyright (c) Arista Networks, Inc. 2024
// Underlying
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build go1.23
package gomap
import (
"hash/maphash"
"maps"
"sync"
"testing"
)
func TestRangeFuncs(t *testing.T) {
m := New(
func(a, b string) bool { return a == b },
maphash.String,
KeyElem[string, string]{"Avenue", "AVE"},
KeyElem[string, string]{"Street", "ST"},
KeyElem[string, string]{"Court", "CT"},
)
t.Run("All", func(t *testing.T) {
exp := map[string]string{
"Avenue": "AVE",
"Street": "ST",
"Court": "CT",
}
// ensure break works
for k, v := range m.All() {
if exp[k] != v {
t.Errorf("k=%q exp=%q got=%q", k, exp[k], v)
}
break
}
got := make(map[string]string)
for k, v := range m.All() {
got[k] = v
}
if !maps.Equal(exp, got) {
t.Errorf("expected: %v got: %v", exp, got)
}
})
t.Run("Keys", func(t *testing.T) {
exp := map[string]struct{}{
"Avenue": struct{}{},
"Street": struct{}{},
"Court": struct{}{},
}
// ensure break works
for k := range m.Keys() {
if _, ok := exp[k]; !ok {
t.Errorf("k=%q not found", k)
}
break
}
got := make(map[string]struct{})
for k := range m.Keys() {
got[k] = struct{}{}
}
if !maps.Equal(exp, got) {
t.Errorf("expected: %v got: %v", exp, got)
}
})
t.Run("Values", func(t *testing.T) {
exp := map[string]struct{}{
"AVE": struct{}{},
"ST": struct{}{},
"CT": struct{}{},
}
// ensure break works
for k := range m.Values() {
if _, ok := exp[k]; !ok {
t.Errorf("k=%q not found", k)
}
break
}
got := make(map[string]struct{})
for k := range m.Values() {
got[k] = struct{}{}
}
if !maps.Equal(exp, got) {
t.Errorf("expected: %v got: %v", exp, got)
}
})
}
func TestGetAllRace(t *testing.T) {
m := NewHint[int, int](100, func(a int, b int) bool { return a == b }, intHash)
for i := 0; i < 100; i++ {
m.Set(i, i)
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
v, ok := m.Get(i)
if !ok || v != i {
t.Errorf("expected: %d got: %d, %t", i, v, ok)
}
}
wg.Done()
}()
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
v, ok := m.Get(i)
if !ok || v != i {
t.Errorf("expected: %d got: %d, %t", i, v, ok)
}
}
wg.Done()
}()
wg.Add(1)
go func() {
outer:
for i := 0; i < 100; i++ {
for range m.All() {
continue outer
}
t.Error("Should have iterated at least once, but didn't.")
}
wg.Done()
}()
wg.Add(1)
go func() {
outer:
for i := 0; i < 100; i++ {
for range m.All() {
continue outer
}
t.Error("Should have iterated at least once, but didn't.")
}
wg.Done()
}()
wg.Wait()
}
func TestAll(t *testing.T) {
m := New[uint64, uint64](
func(a, b uint64) bool { return a == b },
badIntHash,
)
expected := make(map[uint64]uint64, 9)
for i := uint64(0); i < 9; i++ {
expected[i] = i
m.Set(i, i)
}
for k, v := range m.All() {
e, ok := expected[k]
if !ok {
t.Errorf("unexpected value in m: [%d: %d]", k, v)
continue
}
if e != v {
t.Errorf("wrong value for key %d. Expected: %d Got: %d", k, e, v)
continue
}
delete(expected, k)
}
if len(expected) > 0 {
t.Errorf("Values not found in m: %v", expected)
}
}
func TestAllDuringResize(t *testing.T) {
m := New[uint64, uint64](
func(a, b uint64) bool { return a == b },
badIntHash,
)
// insert numbers that initially hash to the same bucket, but will
// be split into different buckets on resize. Evens will end up in
// bucket[0], odds end up in bucket[1] thanks to badIntHash.
initial := map[uint64]uint64{0: 0, 1: 1, 2: 2, 3: 3}
for k, e := range initial {
m.Set(k, e)
}
additional := map[uint64]uint64{100: 100, 101: 101, 102: 102, 103: 103, 104: 104}
first := true
// start the iter
for k, v := range m.All() {
if first {
first = false
// Add some additional data to cause a resize
for k, e := range additional {
m.Set(k, e)
}
// Remove 1 value that in each of the initial and split
// buckets that we haven't seen yet
if k == 0 {
m.Delete(2)
delete(initial, 2)
} else {
m.Delete(0)
delete(initial, 0)
}
if k == 1 {
m.Delete(3)
delete(initial, 3)
} else {
m.Delete(1)
delete(initial, 1)
}
}
if k != v {
t.Errorf("expected key == elem, but got: %d != %d", k, v)
t.Error(m.debugString())
}
if _, ok := initial[k]; ok {
delete(initial, v)
continue
}
if _, ok := additional[k]; ok {
t.Logf("Saw key from additional: %d", k)
continue
}
t.Errorf("Unexpected value from iter: %d", k)
}
for k := range initial {
t.Errorf("iter missing key: %d", k)
}
}
func TestAllDuringGrow(t *testing.T) {
m := New[uint64, uint64](
func(a, b uint64) bool { return a == b },
badIntHash,
)
// Insert exactly 27 numbers so we end up in the middle of a grow.
expected := make(map[uint64]uint64, 27)
for i := uint64(0); i < 27; i++ {
expected[i] = i
m.Set(i, i)
}
for k, v := range m.All() {
t.Logf("Key: %d", k)
if k != v {
t.Errorf("expected key == elem, but got: %d != %d", k, v)
t.Error(m.debugString())
}
if _, ok := expected[k]; ok {
delete(expected, v)
continue
}
t.Errorf("Unexpected value from iter: %d", k)
}
for k := range expected {
t.Errorf("iter missing key: %d", k)
}
}
func BenchmarkAll(b *testing.B) {
m := New[string, int](
func(a, b string) bool { return a == b },
maphash.String,
KeyElem[string, int]{"one", 1},
KeyElem[string, int]{"two", 2},
KeyElem[string, int]{"three", 3},
)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for range m.All() {
}
}
}