首次提交代码

This commit is contained in:
algotao
2025-11-03 14:37:59 +08:00
parent e60f64721c
commit d76c196fb1
311 changed files with 81709 additions and 0 deletions

View File

@@ -0,0 +1,127 @@
package bloom
import (
"math/rand"
"strconv"
"testing"
"github.com/RoaringBitmap/roaring/roaring64"
)
func TestBasic(t *testing.T) {
f := NewWithEstimates(1000000000, 0.00001)
n1 := "Bess"
n2 := "Jane"
n3 := "Tony"
n4 := "Algo"
f.AddString(n1)
f.AddString(n2)
f.AddString(n3)
f.Flush()
n1b := f.TestString(n1)
n2b := f.TestString(n2)
n3b := f.TestString(n3)
n4b := f.TestString(n4)
if !n1b {
t.Errorf("%v should be in.", n1)
}
if !n2b {
t.Errorf("%v should be in.", n2)
}
if !n3b {
t.Errorf("%v should be in.", n3)
}
if n4b {
t.Errorf("%v should be not in.", n4)
}
}
func TestFile(t *testing.T) {
f := NewWithEstimates(1000000000, 0.00001)
n1 := "Bess"
n2 := "Jane"
n3 := "Tony"
n4 := "Algo"
f.AddString(n1)
f.AddString(n2)
f.AddString(n3)
const tmpfile = "//tmp//bloomtest.bin"
err := f.SaveToFile(tmpfile)
if err != nil {
t.Errorf("save file error %v", err)
}
f, err = LoadFromFile(tmpfile, false)
if err != nil {
t.Errorf("load file error %v", err)
}
n1b := f.TestString(n1)
n2b := f.TestString(n2)
n3b := f.TestString(n3)
n4b := f.TestString(n4)
if !n1b {
t.Errorf("%v should be in.", n1)
}
if !n2b {
t.Errorf("%v should be in.", n2)
}
if !n3b {
t.Errorf("%v should be in.", n3)
}
if n4b {
t.Errorf("%v should be not in.", n4)
}
}
func Test10W(t *testing.T) {
f := NewWithEstimates(100000, 0.00001)
for i := uint64(0); i < 100000; i++ {
f.AddString(strconv.FormatUint(i, 10))
}
const tmpfile = "//tmp//bloomtest.bin"
err := f.SaveToFile(tmpfile)
if err != nil {
t.Errorf("save file error %v", err)
}
f, err = LoadFromFile(tmpfile, false)
if err != nil {
t.Errorf("load file error %v", err)
}
for i := uint64(0); i < 100000; i++ {
ns := f.TestString(strconv.FormatUint(i, 10))
if !ns {
t.Errorf("%v should be in.", ns)
}
}
}
func TestStat(t *testing.T) {
f := NewWithEstimates(1000000000, 0.00000001)
t.Errorf("%v", f.GetStat())
}
func BenchmarkNormal(b *testing.B) {
f := NewWithEstimates(1000000000, 0.00001)
for n := 0; n < b.N; n++ {
f.AddString(strconv.FormatUint(uint64(n), 10))
}
}
func BenchmarkRoaringBitmap(b *testing.B) {
f := roaring64.New()
r := rand.New(rand.NewSource(99))
x := uint64(0)
b.Run("Add", func(b *testing.B) {
for n := 0; n < b.N; n++ {
x = r.Uint64() % 23962645944
f.Add(x)
}
})
b.Errorf("%v aa\n", f.GetSizeInBytes())
}