github.com/andy2046/gopie@v0.7.0/pkg/bloom/bloom_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 TestBasic(t *testing.T) {
    11  	f := bloom.New(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  	f.Remove(e1)
    34  	e1a = f.Exist(e1)
    35  	if e1a {
    36  		t.Errorf("%q should be Removed.", e1)
    37  	}
    38  }
    39  
    40  func TestUint(t *testing.T) {
    41  	f := bloom.New(1000, 4)
    42  	n1 := make([]byte, 4)
    43  	n2 := make([]byte, 4)
    44  	n3 := make([]byte, 4)
    45  	n4 := make([]byte, 4)
    46  	binary.BigEndian.PutUint32(n1, 100)
    47  	binary.BigEndian.PutUint32(n2, 101)
    48  	binary.BigEndian.PutUint32(n3, 102)
    49  	binary.BigEndian.PutUint32(n4, 103)
    50  	f.Add(n1)
    51  	n3b := f.Exist(n3)
    52  	n1a := f.Exist(n1)
    53  	n2a := f.Exist(n2)
    54  	f.Add(n3)
    55  	n3a := f.Exist(n3)
    56  	n4a := f.Exist(n4)
    57  	if !n1a {
    58  		t.Errorf("%q should Exist.", n1)
    59  	}
    60  	if n2a {
    61  		t.Errorf("%q should not Exist.", n2)
    62  	}
    63  	if n3b {
    64  		t.Errorf("%q should not Exist the first time we check.", n3)
    65  	}
    66  	if !n3a {
    67  		t.Errorf("%q should Exist the second time we check.", n3)
    68  	}
    69  	if n4a {
    70  		t.Errorf("%q should not Exist.", n4)
    71  	}
    72  	f.Remove(n1)
    73  	n1a = f.Exist(n1)
    74  	if n1a {
    75  		t.Errorf("%q should be Removed.", n1)
    76  	}
    77  }
    78  
    79  func TestString(t *testing.T) {
    80  	f := bloom.NewGuess(1000, 0.001)
    81  	s1 := "Filter"
    82  	s2 := "is"
    83  	s3 := "in"
    84  	s4 := "bloom"
    85  	f.AddString(s1)
    86  	s3b := f.ExistString(s3)
    87  	s1a := f.ExistString(s1)
    88  	s2a := f.ExistString(s2)
    89  	f.AddString(s3)
    90  	s3a := f.ExistString(s3)
    91  	s4a := f.ExistString(s4)
    92  	if !s1a {
    93  		t.Errorf("%q should Exist.", s1)
    94  	}
    95  	if s2a {
    96  		t.Errorf("%q should not Exist.", s2)
    97  	}
    98  	if s3b {
    99  		t.Errorf("%q should not Exist the first time we check.", s3)
   100  	}
   101  	if !s3a {
   102  		t.Errorf("%q should Exist the second time we check.", s3)
   103  	}
   104  	if s4a {
   105  		t.Errorf("%q should not Exist.", s4)
   106  	}
   107  	f.RemoveString(s1)
   108  	s1a = f.ExistString(s1)
   109  	if s1a {
   110  		t.Errorf("%q should be Removed.", s1)
   111  	}
   112  }
   113  
   114  func TestGuessFalsePositive(t *testing.T) {
   115  	n, p := uint64(100000), float64(0.001)
   116  	m, k := bloom.Guess(n, p)
   117  	f := bloom.NewGuess(n, p)
   118  	fp := f.GuessFalsePositive(n)
   119  	t.Logf("m=%v k=%v n=%v p=%v fp=%v", m, k, n, p, fp)
   120  	if fp > p {
   121  		t.Errorf("False Positive too high")
   122  	}
   123  }
   124  
   125  func TestM(t *testing.T) {
   126  	f := bloom.New(1000, 4)
   127  	if f.M() != 1024 {
   128  		t.Errorf("M() %v is not correct", f.M())
   129  	}
   130  }
   131  
   132  func TestK(t *testing.T) {
   133  	f := bloom.New(1000, 4)
   134  	if f.K() != 4 {
   135  		t.Errorf("K() %v is not correct", f.K())
   136  	}
   137  }
   138  
   139  func BenchmarkAddExist(b *testing.B) {
   140  	f := bloom.NewGuess(uint64(b.N), 0.0001)
   141  	key := make([]byte, 8)
   142  	b.ResetTimer()
   143  	for i := 0; i < b.N; i++ {
   144  		binary.BigEndian.PutUint64(key, uint64(i))
   145  		f.Add(key)
   146  		f.Exist(key)
   147  	}
   148  }