github.com/andy2046/gopie@v0.7.0/pkg/bloom/bloombit_test.go (about) 1 package bloom_test 2 3 import ( 4 "encoding/binary" 5 "testing" 6 7 "github.com/andy2046/gopie/pkg/bloom" 8 ) 9 10 func TestBitBasic(t *testing.T) { 11 f := bloom.NewB(1000, 4) 12 e1 := []byte("Boss") 13 e2 := []byte("Joke") 14 e3 := []byte("Emotion") 15 f.Add(e1) 16 e3b := f.Exist(e3) 17 e1a := f.Exist(e1) 18 e2a := f.Exist(e2) 19 f.Add(e3) 20 e3a := f.Exist(e3) 21 if !e1a { 22 t.Errorf("%q should Exist.", e1) 23 } 24 if e2a { 25 t.Errorf("%q should not Exist.", e2) 26 } 27 if e3b { 28 t.Errorf("%q should not Exist the first time we check.", e3) 29 } 30 if !e3a { 31 t.Errorf("%q should Exist the second time we check.", e3) 32 } 33 } 34 35 func TestBitUint(t *testing.T) { 36 f := bloom.NewB(1000, 4) 37 n1 := make([]byte, 4) 38 n2 := make([]byte, 4) 39 n3 := make([]byte, 4) 40 n4 := make([]byte, 4) 41 binary.BigEndian.PutUint32(n1, 100) 42 binary.BigEndian.PutUint32(n2, 101) 43 binary.BigEndian.PutUint32(n3, 102) 44 binary.BigEndian.PutUint32(n4, 103) 45 f.Add(n1) 46 n3b := f.Exist(n3) 47 n1a := f.Exist(n1) 48 n2a := f.Exist(n2) 49 f.Add(n3) 50 n3a := f.Exist(n3) 51 n4a := f.Exist(n4) 52 if !n1a { 53 t.Errorf("%q should Exist.", n1) 54 } 55 if n2a { 56 t.Errorf("%q should not Exist.", n2) 57 } 58 if n3b { 59 t.Errorf("%q should not Exist the first time we check.", n3) 60 } 61 if !n3a { 62 t.Errorf("%q should Exist the second time we check.", n3) 63 } 64 if n4a { 65 t.Errorf("%q should not Exist.", n4) 66 } 67 } 68 69 func TestBitString(t *testing.T) { 70 f := bloom.NewBGuess(1000, 0.001) 71 s1 := "Filter" 72 s2 := "is" 73 s3 := "in" 74 s4 := "bloom" 75 f.AddString(s1) 76 s3b := f.ExistString(s3) 77 s1a := f.ExistString(s1) 78 s2a := f.ExistString(s2) 79 f.AddString(s3) 80 s3a := f.ExistString(s3) 81 s4a := f.ExistString(s4) 82 if !s1a { 83 t.Errorf("%q should Exist.", s1) 84 } 85 if s2a { 86 t.Errorf("%q should not Exist.", s2) 87 } 88 if s3b { 89 t.Errorf("%q should not Exist the first time we check.", s3) 90 } 91 if !s3a { 92 t.Errorf("%q should Exist the second time we check.", s3) 93 } 94 if s4a { 95 t.Errorf("%q should not Exist.", s4) 96 } 97 } 98 99 func TestBitM(t *testing.T) { 100 f := bloom.NewB(1000, 4) 101 if f.M() != 1024 { 102 t.Errorf("M() %v is not correct", f.M()) 103 } 104 } 105 106 func TestBitK(t *testing.T) { 107 f := bloom.NewB(1000, 4) 108 if f.K() != 4 { 109 t.Errorf("K() %v is not correct", f.K()) 110 } 111 } 112 113 func BenchmarkBitAddExist(b *testing.B) { 114 f := bloom.NewBGuess(uint64(b.N), 0.0001) 115 key := make([]byte, 8) 116 b.ResetTimer() 117 for i := 0; i < b.N; i++ { 118 binary.BigEndian.PutUint64(key, uint64(i)) 119 f.Add(key) 120 f.Exist(key) 121 } 122 }