8func TestBloom(t *testing.T) {
9 if err := BloomValid(3, 10); err == nil {
10 t.Fatalf("missing error for invalid bloom filter size")
13 _, err := NewBloom(make([]byte, 3), 10)
15 t.Fatalf("missing error for invalid bloom filter size")
18 b, err := NewBloom(make([]byte, 256), 5)
20 t.Fatalf("newbloom: %s", err)
23 absent := func(v string) {
26 t.Fatalf("should be absent: %q", v)
30 present := func(v string) {
33 t.Fatalf("should be present: %q", v)
39 t.Fatalf("bloom filter already modified?")
45 for i := 'a'; i <= 'z'; i++ {
46 words = append(words, fmt.Sprintf("%c", i))
48 for _, w := range words {
53 for _, w := range words {
57 t.Fatalf("bloom filter was not modified?")
60 //log.Infof("ones: %d, m %d", b.Ones(), len(b.Bytes())*8)
63func TestBits(t *testing.T) {
64 b := &bits{width: 1, buf: []byte{0xff, 0xff}}
65 for i := 0; i < 16; i++ {
70 b = &bits{width: 2, buf: []byte{0xff, 0xff}}
71 for i := 0; i < 8; i++ {
72 if b.nextPos() != 0b11 {
73 t.Fatalf("pos not 0b11")
77 b = &bits{width: 1, buf: []byte{0b10101010, 0b10101010}}
78 for i := 0; i < 16; i++ {
79 if b.nextPos() != ((i + 1) % 2) {
83 b = &bits{width: 2, buf: []byte{0b10101010, 0b10101010}}
84 for i := 0; i < 8; i++ {
85 if b.nextPos() != 0b10 {
86 t.Fatalf("pos not 0b10")
91func TestSet(t *testing.T) {
100 for i := 0; i < 8; i++ {
106 for i := 8; i < 16; i++ {
111 for i := 16; i < 24; i++ {
116 for i := 24; i < 32; i++ {
124func TestOnes(t *testing.T) {
125 ones := func(b *Bloom, x int) {
129 t.Fatalf("ones: got %d, expected %d", n, x)
132 ones(&Bloom{data: []byte{0b10101010}}, 4)
133 ones(&Bloom{data: []byte{0b01010101}}, 4)
134 ones(&Bloom{data: []byte{0b11111111}}, 8)
135 ones(&Bloom{data: []byte{0b00000000}}, 0)