Skip to content

Commit

Permalink
Add unit tests to check threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
seiflotfy committed May 4, 2016
1 parent fc59a03 commit be0a13a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/sketches/bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func (d *BloomSketch) Add(values [][]byte) (bool, error) {
success := true
dict := make(map[string]uint)
if d.threshold != nil {
d.threshold.Add(values)
s, err := d.threshold.Add(values)
success = s
if err != nil {
return false, err
}
if !d.threshold.IsFull() {
return true, nil
}
Expand Down
51 changes: 50 additions & 1 deletion src/sketches/bloom_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package sketches

import (
"fmt"
"strconv"
"testing"

"datamodel"
pb "datamodel/protobuf"
"utils"
"testutils"
"utils"
)

func TestAddBloom(t *testing.T) {
Expand Down Expand Up @@ -51,17 +52,65 @@ func TestAddBloom(t *testing.T) {
} else {
tmp := res.(*pb.MembershipResult)
mres := tmp.GetMemberships()
// FIXME: Flatten to avoid O(n^2) complexity
for key := range check {
for i := 0; i < len(mres); i++ {
if mres[i].GetValue() == key &&
mres[i].GetIsMember() != check[key] {
// FIXME: Use t.Errorf (using + is ugly)
t.Error("expected member == "+strconv.FormatBool(check[key])+", got", mres[i].GetIsMember())
}
}
}
}
}

func TestAddBloomThreshold(t *testing.T) {
testutils.SetupTests()
defer testutils.TearDownTests()

info := datamodel.NewEmptyInfo()
info.Properties.MaxUniqueItems = utils.Int64p(1024)
info.Name = utils.Stringp("marvel")
sketch, err := NewBloomSketch(info)

if err != nil {
t.Error("expected avengers to have no error, got", err)
}

var rValues [][]byte
thresholdSize := int64(sketch.threshold.size)
for i := int64(0); i < info.GetProperties().GetMaxUniqueItems()/3; i++ {
value := fmt.Sprintf("value-%d", i)
values := [][]byte{
[]byte(value),
}
if _, err := sketch.Add(values); err != nil {
t.Error("expected no errors, got", err)
}

rValues = append(rValues, []byte(value))

// Threshold should be nil once more than 10% is filled
if sketch.threshold != nil && i >= thresholdSize {
t.Error("expected threshold == nil for i ==", i)
}

if res, err := sketch.Get(rValues); err != nil {
t.Error("expected no errors, got", err)
} else {
tmp := res.(*pb.MembershipResult)
mres := tmp.GetMemberships()
for i := 0; i < len(mres); i++ {
if !mres[i].GetIsMember() {
t.Fatalf("expected %s ==> member == true, got false", mres[i].GetValue())
break
}
}
}
}
}

func TestStressBloom(t *testing.T) {
testutils.SetupTests()
defer testutils.TearDownTests()
Expand Down

0 comments on commit be0a13a

Please sign in to comment.