This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter_test.go
165 lines (145 loc) · 2.81 KB
/
counter_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
package hashcounter
import (
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
c = new(C)
m = map[string]uint16{}
)
func init() {
for i := 0; i < 1e5; i++ {
// 4 to 16 bytes
byts := make([]byte, rand.Intn(12)+4)
rand.Read(byts)
for j := 0; j < int(byts[0])&0xF; j++ {
c.Add(byts, 1)
m[string(byts)] = m[string(byts)] + 1
}
}
}
func TestLen(t *testing.T) {
// make sure length is correct
assert.Equal(t, len(m), c.Len())
}
func TestGet(t *testing.T) {
// make sure each key is correct
for k, v := range m {
v2, ok := c.Get([]byte(k))
require.True(t, ok)
assert.Equal(t, v, v2)
}
}
func TestRange(t *testing.T) {
mkeys := map[uint64]uint16{}
for k, v := range m {
mkeys[c.Key([]byte(k))] = v
}
l := 0
c.Range(func(k uint64, v uint16) bool {
assert.Equal(t, mkeys[k], v)
l++
return true
})
assert.Equal(t, c.Len(), l)
}
func TestMarshalUnmarshal(t *testing.T) {
b, err := c.MarshalBinary()
require.NoError(t, err)
c2 := new(C)
err = c2.UnmarshalBinary(b)
require.NoError(t, err)
require.Equal(t, c.Len(), c2.Len())
c.Range(func(k uint64, v uint16) bool {
v2, ok := c2.GetKey(k)
require.True(t, ok)
assert.Equal(t, v, v2)
return true
})
}
func TestMerge(t *testing.T) {
c2 := new(C)
m2 := map[string]uint16{}
for k, v := range m {
m2[k] = v
}
for i := 0; i < 100; i++ {
// 4 to 16 bytes
byts := make([]byte, rand.Intn(12)+4)
rand.Read(byts)
for j := 0; j < int(byts[0])&0xF; j++ {
c2.Add(byts, 1)
m2[string(byts)] = m2[string(byts)] + 1
}
}
c2.Merge(c)
require.Equal(t, len(m2), c2.Len())
// make sure each key is correct
for k, v := range m2 {
v2, ok := c2.Get([]byte(k))
require.True(t, ok)
assert.Equal(t, v, v2)
}
}
func TestReset(t *testing.T) {
c2 := new(C)
c2.Merge(c)
c2.Reset()
assert.Equal(t, 0, c2.Len())
}
func BenchmarkAdd(b *testing.B) {
vals := make([][]byte, b.N)
for i := 0; i < b.N; i++ {
byts := make([]byte, 4)
rand.Read(byts)
vals[i] = byts
}
c := new(C)
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Add(vals[i], 1)
}
}
func BenchmarkGet(b *testing.B) {
c := new(C)
vals := make([][]byte, b.N)
for i := 0; i < b.N; i++ {
byts := make([]byte, 4)
rand.Read(byts)
vals[i] = byts
c.Add(byts, 1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Get(vals[i])
}
}
func BenchmarkGetKey(b *testing.B) {
c := new(C)
vals := make([]uint64, b.N)
for i := 0; i < b.N; i++ {
byts := make([]byte, 4)
rand.Read(byts)
vals[i] = c.Key(byts)
c.Add(byts, 1)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.GetKey(vals[i])
}
}
func BenchmarkKey(b *testing.B) {
c := new(C)
vals := make([][]byte, b.N)
for i := 0; i < b.N; i++ {
byts := make([]byte, 4)
rand.Read(byts)
vals[i] = byts
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
c.Key(vals[i])
}
}