/
get_set_test.go
112 lines (98 loc) · 2.03 KB
/
get_set_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
package sbloom
import (
"hash/crc64"
"hash/fnv"
"math/rand"
"testing"
)
func randIndex(top uint64) uint64 {
hi, low := rand.Uint32(), rand.Uint32()
return (uint64(hi)<<32 | uint64(low)) % top
}
const maxSize = 10
var sizes []uint64
func init() {
for i := uint(0); i < maxSize; i++ {
sizes = append(sizes, 1<<i)
}
}
func BenchmarkSet(b *testing.B) {
const size = 1 << maxSize
x := make([]uint8, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := uint64(0); i < size*(1<<elemSize); i++ {
set(x, i)
}
b.SetBytes(size * (1 << (elemSize - 3)))
}
}
func BenchmarkGet(b *testing.B) {
const size = 1 << maxSize
x := make([]uint8, size)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := uint64(0); i < size*(1<<elemSize); i++ {
get(x, i)
}
b.SetBytes(size * (1 << (elemSize - 3)))
}
}
func BenchmarkFnvHash(b *testing.B) {
const bytes = 1024
s := newsHash(fnv.New64())
dat := make([]byte, bytes)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Hash(dat)
b.SetBytes(bytes)
}
}
func BenchmarkCrc64Hash(b *testing.B) {
const bytes = 1024
s := newsHash(crc64.New(crc64.MakeTable(crc64.ISO)))
dat := make([]byte, bytes)
b.ResetTimer()
for i := 0; i < b.N; i++ {
s.Hash(dat)
b.SetBytes(bytes)
}
}
func TestGetAndSetOnEveryBit(t *testing.T) {
for _, size := range sizes {
x := make([]uint8, size)
for j := uint64(0); j < size*(1<<elemSize); j++ {
if get(x, j) {
t.Errorf("0[%d] %08b", j, x[0])
}
}
for i := uint64(0); i < size*(1<<elemSize); i++ {
set(x, i)
//make sure every bit is right
for j := uint64(0); j < size*(1<<elemSize); j++ {
if get(x, j) != (j <= i) { //get is true when j <= i
t.Errorf("%d[%d] %08b", i, j, x[0])
}
}
}
}
}
func TestPanicGetLarge(t *testing.T) {
recov := func() {
if recover() == nil {
t.Fatal("no panic")
}
}
for _, size := range sizes {
func() {
defer recov()
x := make([]uint8, size)
get(x, size*(1<<elemSize))
}()
func() {
defer recov()
x := make([]uint8, size)
set(x, size*(1<<elemSize))
}()
}
}