-
Notifications
You must be signed in to change notification settings - Fork 2
/
sketch_test.go
53 lines (50 loc) · 1.04 KB
/
sketch_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
package cache
import "testing"
func TestCountMinSketch(t *testing.T) {
const max = 15
cm := &countMinSketch{}
cm.init(max)
for i := 0; i < max; i++ {
// Increase value at i j times
for j := i; j > 0; j-- {
cm.add(uint64(i))
}
}
for i := 0; i < max; i++ {
n := cm.estimate(uint64(i))
if int(n) != i {
t.Fatalf("unexpected estimate(%d): %d, want: %d", i, n, i)
}
}
cm.reset()
for i := 0; i < max; i++ {
n := cm.estimate(uint64(i))
if int(n) != i/2 {
t.Fatalf("unexpected estimate(%d): %d, want: %d", i, n, i/2)
}
}
cm.reset()
for i := 0; i < max; i++ {
n := cm.estimate(uint64(i))
if int(n) != i/4 {
t.Fatalf("unexpected estimate(%d): %d, want: %d", i, n, i/4)
}
}
for i := 0; i < 100; i++ {
cm.add(1)
}
n := cm.estimate(1)
if n != 15 {
t.Fatalf("unexpected estimate(%d): %d, want: %d", 1, n, 15)
}
}
func BenchmarkCountMinSketchReset(b *testing.B) {
cm := &countMinSketch{}
cm.init(1<<15 - 1)
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
cm.add(0xCAFECAFECAFECAFE)
cm.reset()
}
}