common/expbloom: better test structure

This commit is contained in:
Marius van der Wijden 2023-09-18 14:40:32 +02:00
parent 696433e298
commit 69dc1202a7

View file

@ -1,44 +1,28 @@
package bloom package bloom
import ( import (
"encoding/binary"
"testing" "testing"
"time" "time"
) )
type hashable struct { type hashable []byte
b []byte
}
func (h hashable) BlockSize() int {
return len(h.b)
}
func (h hashable) Hash() []byte {
return h.b
}
func (h hashable) Sum([]byte) []byte {
return h.b
}
func (h hashable) Write(p []byte) (n int, err error) { panic("not implemented") }
func (h hashable) Sum(b []byte) []byte { panic("not implemented") }
func (h hashable) Reset() { panic("not implemented") }
func (h hashable) BlockSize() int { panic("not implemented") }
func (h hashable) Size() int { return 8 }
func (h hashable) Sum64() uint64 { func (h hashable) Sum64() uint64 {
return 1 hash := make([]byte, 8)
} copy(hash, h)
return binary.BigEndian.Uint64(hash[0:8])
func (h hashable) Write([]byte) (int, error) {
return 0, nil
}
func (h hashable) Reset() {}
func (h hashable) Size() int {
return len(h.b)
} }
func TestBloom(t *testing.T) { func TestBloom(t *testing.T) {
bloom, _ := NewExpiringBloom(3, 1024, 10*time.Millisecond) bloom, _ := NewExpiringBloom(3, 1024, 10*time.Millisecond)
testKey := hashable{[]byte{0x01}} testKey := hashable([]byte{0x01})
bloom.Add(testKey) bloom.Add(testKey)
if !bloom.Contains(testKey) { if !bloom.Contains(testKey) {
t.Fatal() t.Fatal()
@ -60,7 +44,7 @@ func TestBloom(t *testing.T) {
func TestBloom2(t *testing.T) { func TestBloom2(t *testing.T) {
bloom, _ := NewExpiringBloom(3, 1024, 10*time.Second) bloom, _ := NewExpiringBloom(3, 1024, 10*time.Second)
testKey := hashable{[]byte{0x01}} testKey := hashable([]byte{0x01})
// Add key in bloom 0 // Add key in bloom 0
bloom.Add(testKey) bloom.Add(testKey)
if !bloom.Contains(testKey) { if !bloom.Contains(testKey) {
@ -86,7 +70,7 @@ func TestBloom2(t *testing.T) {
func BenchmarkAdd(b *testing.B) { func BenchmarkAdd(b *testing.B) {
bloom, _ := NewExpiringBloom(2, 1024, 10*time.Second) bloom, _ := NewExpiringBloom(2, 1024, 10*time.Second)
testKey := hashable{[]byte{0x01}} testKey := hashable([]byte{0x01})
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
bloom.Add(testKey) bloom.Add(testKey)
} }