forked from tylertreat/BoomFilters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuckets_test.go
More file actions
101 lines (81 loc) · 2.09 KB
/
buckets_test.go
File metadata and controls
101 lines (81 loc) · 2.09 KB
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
package boom
import "testing"
// Ensures that MaxBucketValue returns the correct maximum based on the bucket
// size.
func TestMaxBucketValue(t *testing.T) {
b := NewBuckets(10, 2)
if max := b.MaxBucketValue(); max != 3 {
t.Errorf("Expected 3, got %d", max)
}
}
// Ensures that Count returns the number of buckets.
func TestBucketsCount(t *testing.T) {
b := NewBuckets(10, 2)
if count := b.Count(); count != 10 {
t.Errorf("Expected 10, got %d", count)
}
}
// Ensures that Increment increments the bucket value by the correct delta and
// clamps to zero and the maximum, Get returns the correct bucket value, and
// Set sets the bucket value correctly.
func TestBucketsIncrementAndGetAndSet(t *testing.T) {
b := NewBuckets(5, 2)
if b.Increment(0, 1) != b {
t.Error("Returned Buckets should be the same instance")
}
if v := b.Get(0); v != 1 {
t.Errorf("Expected 1, got %d", v)
}
b.Increment(1, -1)
if v := b.Get(1); v != 0 {
t.Errorf("Expected 0, got %d", v)
}
if b.Set(2, 100) != b {
t.Error("Returned Buckets should be the same instance")
}
if v := b.Get(2); v != 3 {
t.Errorf("Expected 3, got %d", v)
}
b.Increment(3, 2)
if v := b.Get(3); v != 2 {
t.Errorf("Expected 2, got %d", v)
}
}
// Ensures that Reset restores the Buckets to the original state.
func TestBucketsReset(t *testing.T) {
b := NewBuckets(5, 2)
for i := 0; i < 5; i++ {
b.Increment(uint(i), 1)
}
if b.Reset() != b {
t.Error("Returned Buckets should be the same instance")
}
for i := 0; i < 5; i++ {
if c := b.Get(uint(i)); c != 0 {
t.Errorf("Expected 0, got %d", c)
}
}
}
func BenchmarkBucketsIncrement(b *testing.B) {
buckets := NewBuckets(10000, 10)
for n := 0; n < b.N; n++ {
buckets.Increment(uint(n)%10000, 1)
}
}
func BenchmarkBucketsSet(b *testing.B) {
buckets := NewBuckets(10000, 10)
for n := 0; n < b.N; n++ {
buckets.Set(uint(n)%10000, 1)
}
}
func BenchmarkBucketsGet(b *testing.B) {
b.StopTimer()
buckets := NewBuckets(10000, 10)
for n := 0; n < b.N; n++ {
buckets.Set(uint(n)%10000, 1)
}
b.StartTimer()
for n := 0; n < b.N; n++ {
buckets.Get(uint(n) % 10000)
}
}