forked from brentp/intintmap
-
Notifications
You must be signed in to change notification settings - Fork 4
/
intintmap_test.go
264 lines (227 loc) · 5.05 KB
/
intintmap_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
package intmap
//
// This file contains the old intintmap_test.go code from https://github.com/brentp/intintmap,
// ported verbatim to new Map code with parametrized types
//
import (
"testing"
)
func TestMapSimple(t *testing.T) {
m := New[int64, int64](10)
var i int64
var v int64
var ok bool
// --------------------------------------------------------------------
// Put() and Get()
for i = 0; i < 20000; i += 2 {
m.Put(i, i)
}
for i = 0; i < 20000; i += 2 {
if v, ok = m.Get(i); !ok || v != i {
t.Errorf("didn't get expected value")
}
if _, ok = m.Get(i + 1); ok {
t.Errorf("didn't get expected 'not found' flag")
}
}
if m.Len() != int(20000/2) {
t.Errorf("size (%d) is not right, should be %d", m.Len(), int(20000/2))
}
// --------------------------------------------------------------------
// Keys()
m0 := make(map[int64]int64, 1000)
for i = 0; i < 20000; i += 2 {
m0[i] = i
}
n := len(m0)
m.ForEach(func(k int64, v int64) bool {
m0[k] = -k
return true
})
if n != len(m0) {
t.Errorf("get unexpected more keys")
}
for k, v := range m0 {
if k != -v {
t.Errorf("didn't get expected changed value")
}
}
// --------------------------------------------------------------------
// Items()
m0 = make(map[int64]int64, 1000)
for i = 0; i < 20000; i += 2 {
m0[i] = i
}
n = len(m0)
m.ForEach(func(k int64, v int64) bool {
m0[k] = -v
if k != v {
t.Errorf("didn't get expected key-value pair")
}
return true
})
if n != len(m0) {
t.Errorf("get unexpected more keys")
}
for k, v := range m0 {
if k != -v {
t.Errorf("didn't get expected changed value")
}
}
// --------------------------------------------------------------------
// Del()
for i = 0; i < 20000; i += 2 {
m.Del(i)
}
for i = 0; i < 20000; i += 2 {
if _, ok = m.Get(i); ok {
t.Errorf("didn't get expected 'not found' flag")
}
if _, ok = m.Get(i + 1); ok {
t.Errorf("didn't get expected 'not found' flag")
}
}
// --------------------------------------------------------------------
// Put() and Get()
for i = 0; i < 20000; i += 2 {
m.Put(i, i*2)
}
for i = 0; i < 20000; i += 2 {
if v, ok = m.Get(i); !ok || v != i*2 {
t.Errorf("didn't get expected value")
}
if _, ok = m.Get(i + 1); ok {
t.Errorf("didn't get expected 'not found' flag")
}
}
}
func TestMap(t *testing.T) {
m := New[int64, int64](10)
var ok bool
var v int64
step := int64(61)
var i int64
m.Put(0, 12345)
for i = 1; i < 100000000; i += step {
m.Put(i, i+7)
m.Put(-i, i-7)
if v, ok = m.Get(i); !ok || v != i+7 {
t.Errorf("expected %d as value for key %d, got %d", i+7, i, v)
}
if v, ok = m.Get(-i); !ok || v != i-7 {
t.Errorf("expected %d as value for key %d, got %d", i-7, -i, v)
}
}
for i = 1; i < 100000000; i += step {
if v, ok = m.Get(i); !ok || v != i+7 {
t.Errorf("expected %d as value for key %d, got %d", i+7, i, v)
}
if v, ok = m.Get(-i); !ok || v != i-7 {
t.Errorf("expected %d as value for key %d, got %d", i-7, -i, v)
}
for j := i + 1; j < i+step; j++ {
if v, ok = m.Get(j); ok {
t.Errorf("expected 'not found' flag for %d, found %d", j, v)
}
}
}
if v, ok = m.Get(0); !ok || v != 12345 {
t.Errorf("expected 12345 for key 0")
}
}
const MAX = 999999999
const STEP = 9534
func fillMap64(m *Map[int64, int64]) {
var j int64
for j = 0; j < MAX; j += STEP {
m.Put(j, -j)
for k := j; k < j+16; k++ {
m.Put(k, -k)
}
}
}
func fillStdMap(m map[int64]int64) {
var j int64
for j = 0; j < MAX; j += STEP {
m[j] = -j
for k := j; k < j+16; k++ {
m[k] = -k
}
}
}
func BenchmarkMap64Fill(b *testing.B) {
for i := 0; i < b.N; i++ {
m := New[int64, int64](2048)
fillMap64(m)
}
}
func BenchmarkStdMapFill(b *testing.B) {
for i := 0; i < b.N; i++ {
m := make(map[int64]int64, 2048)
fillStdMap(m)
}
}
func BenchmarkMap64Get10PercentHitRate(b *testing.B) {
var j, k, v, sum int64
var ok bool
m := New[int64, int64](2048)
fillMap64(m)
for i := 0; i < b.N; i++ {
sum = int64(0)
for j = 0; j < MAX; j += STEP {
for k = j; k < 10; k++ {
if v, ok = m.Get(k); ok {
sum += v
}
}
}
// log.Println("int int sum:", sum)
}
}
func BenchmarkStdMapGet10PercentHitRate(b *testing.B) {
var j, k, v, sum int64
var ok bool
m := make(map[int64]int64, 2048)
fillStdMap(m)
for i := 0; i < b.N; i++ {
sum = int64(0)
for j = 0; j < MAX; j += STEP {
for k = j; k < 10; k++ {
if v, ok = m[k]; ok {
sum += v
}
}
}
// log.Println("map sum:", sum)
}
}
func BenchmarkMap64Get100PercentHitRate(b *testing.B) {
var j, v, sum int64
var ok bool
m := New[int64, int64](2048)
fillMap64(m)
for i := 0; i < b.N; i++ {
sum = int64(0)
for j = 0; j < MAX; j += STEP {
if v, ok = m.Get(j); ok {
sum += v
}
}
// log.Println("int int sum:", sum)
}
}
func BenchmarkStdMapGet100PercentHitRate(b *testing.B) {
var j, v, sum int64
var ok bool
m := make(map[int64]int64, 2048)
fillStdMap(m)
for i := 0; i < b.N; i++ {
sum = int64(0)
for j = 0; j < MAX; j += STEP {
if v, ok = m[j]; ok {
sum += v
}
}
// log.Println("map sum:", sum)
}
}