github.com/andy2046/gopie@v0.7.0/pkg/bloom/bloomscale_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 TestScaleBasic(t *testing.T) {
    11  	f := bloom.NewS(0.01)
    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 TestScaleUint(t *testing.T) {
    36  	f := bloom.NewS(0.01)
    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, 10000)
    42  	binary.BigEndian.PutUint32(n2, 10001)
    43  	binary.BigEndian.PutUint32(n3, 10002)
    44  	binary.BigEndian.PutUint32(n4, 10003)
    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  	for i := uint32(1); i < 1000; i++ {
    69  		buf := make([]byte, 4)
    70  		binary.BigEndian.PutUint32(buf, i)
    71  		f.Add(buf)
    72  	}
    73  	count := 0
    74  	for i := uint32(1000); i < 4000; i++ {
    75  		buf := make([]byte, 4)
    76  		binary.BigEndian.PutUint32(buf, i)
    77  		if f.Exist(buf) {
    78  			count++
    79  		}
    80  	}
    81  	t.Logf("FP rate is %v", f.FalsePositive())
    82  	if f.FalsePositive() > 0.01 {
    83  		t.Errorf("False Positive rate should not be > 0.01")
    84  	}
    85  
    86  	if count > 1 {
    87  		t.Errorf("Actual FP %d greater than expected FP", count)
    88  	}
    89  }
    90  
    91  func TestScaleString(t *testing.T) {
    92  	f := bloom.NewS(0.01)
    93  	s1 := "Filter"
    94  	s2 := "is"
    95  	s3 := "in"
    96  	s4 := "bloom"
    97  	f.AddString(s1)
    98  	s3b := f.ExistString(s3)
    99  	s1a := f.ExistString(s1)
   100  	s2a := f.ExistString(s2)
   101  	f.AddString(s3)
   102  	s3a := f.ExistString(s3)
   103  	s4a := f.ExistString(s4)
   104  	if !s1a {
   105  		t.Errorf("%q should Exist.", s1)
   106  	}
   107  	if s2a {
   108  		t.Errorf("%q should not Exist.", s2)
   109  	}
   110  	if s3b {
   111  		t.Errorf("%q should not Exist the first time we check.", s3)
   112  	}
   113  	if !s3a {
   114  		t.Errorf("%q should Exist the second time we check.", s3)
   115  	}
   116  	if s4a {
   117  		t.Errorf("%q should not Exist.", s4)
   118  	}
   119  }
   120  
   121  func TestScaleM(t *testing.T) {
   122  	f := bloom.NewS(0.01)
   123  	if f.M() < 512 {
   124  		t.Errorf("M() %v is not correct", f.M())
   125  	}
   126  }
   127  
   128  func TestScaleK(t *testing.T) {
   129  	f := bloom.NewS(0.01)
   130  	if f.K() < 3 {
   131  		t.Errorf("K() %v is not correct", f.K())
   132  	}
   133  }
   134  
   135  func BenchmarkScaleAddExist(b *testing.B) {
   136  	f := bloom.NewSGuess(uint64(b.N), 0.0001, 0.9)
   137  	key := make([]byte, 8)
   138  	b.ResetTimer()
   139  	for i := 0; i < b.N; i++ {
   140  		binary.BigEndian.PutUint64(key, uint64(i))
   141  		f.Add(key)
   142  		f.Exist(key)
   143  	}
   144  }