Skip to content

Commit

Permalink
Merge pull request #4 from devinmcgloin/improved-tests
Browse files Browse the repository at this point in the history
Improved testing
  • Loading branch information
Devin McGloin authored Oct 30, 2018
2 parents b714b96 + bcb83d6 commit 28c9617
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pkg/count/sketch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ func TestCardinality(t *testing.T) {

url := []byte("https://devinmcgloin.com")
b.Increment(url)
if b.Count(url) == 0 {
if b.Count(url) != 1 {
t.Errorf("expected estimate count to be %d got %d\n", 1, b.Count(url))
}

b.Increment(url)
if b.Count(url) != 2 {
t.Errorf("expected estimate count to be %d got %d\n", 2, b.Count(url))
}

items := generator.RandomStrings(500)
for _, v := range items {
b.Increment([]byte(v))
Expand Down
9 changes: 9 additions & 0 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,12 @@ func RandomStrings(n int) []string {
}
return words
}

// RandomSimilarStrings returns strings with some probabilistic overlap.
func RandomSimilarStrings(n int, length int) []string {
words := make([]string, n)
for i := 0; i < n; i++ {
words[i] = RandString(length)
}
return words
}
7 changes: 5 additions & 2 deletions pkg/min-hash/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ import (
func TestJaccard(t *testing.T) {

mh := New(0.05)
a := generator.RandomStrings(5000)
b := generator.RandomStrings(5000)
a := generator.RandomSimilarStrings(5000, 3)
b := generator.RandomSimilarStrings(5000, 3)

naive := NaiveJaccard(a, b)

estimate := mh.Estimate(toInterface(a), toInterface(b))
if naive == 0 {
t.Error("naive calculated zero overlap")
}
if math.Abs(naive-estimate) > mh.ErrorRate() {
t.Error()
}
Expand Down

0 comments on commit 28c9617

Please sign in to comment.