Skip to content

Commit

Permalink
Adding tests around the tolerane
Browse files Browse the repository at this point in the history
  • Loading branch information
Dynom committed Jul 11, 2018
1 parent 3094560 commit 9244d7f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
8 changes: 6 additions & 2 deletions finder/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,16 @@ func (t Finder) FindCtx(ctx context.Context, input string) (string, float64, boo
return best, hs, false
}

// meetsLengthTolerance checks if the input meets the length tolerance criteria
// meetsLengthTolerance checks if the input meets the length tolerance criteria. The percentage is based on the input
func meetsLengthTolerance(t float64, input, reference string) bool {
if t == 0 {
if t <= 0 {
return true
}

if t > 1 {
return false
}

inputLen := len(input)
refLen := len(reference)
threshold := int(math.Ceil(float64(inputLen) * t))
Expand Down
28 changes: 28 additions & 0 deletions finder/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ func TestContextCancel(t *testing.T) {
}
}

func TestMeetsLengthTolerance(t *testing.T) {
testData := []struct {
Expect bool
Input string
Reference string
Tolerance float64
}{
{Expect: true, Input: "foo", Reference: "bar", Tolerance: -1},
{Expect: true, Input: "foo", Reference: "bar", Tolerance: 0},
{Expect: true, Input: "foo", Reference: "bar", Tolerance: 1},
{Expect: false, Input: "foo", Reference: "bar", Tolerance: 2}, // erroneous situation

{Expect: true, Input: "smooth", Reference: "smoothie", Tolerance: 0.2},
{Expect: false, Input: "smooth", Reference: "smoothie", Tolerance: 0.1},

{Expect: true, Input: "abc", Reference: "defghi", Tolerance: 0.9},
{Expect: true, Input: "abc", Reference: "defg", Tolerance: 0.5},
}

for _, td := range testData {
r := meetsLengthTolerance(td.Tolerance, td.Input, td.Reference)
if r != td.Expect {
t.Errorf("Expected the tolerance to be %t\n%+v", td.Expect, td)
}
}

}

func BenchmarkSliceOrMap(b *testing.B) {
size := 50
var hashMap = make(map[int]int, size)
Expand Down

0 comments on commit 9244d7f

Please sign in to comment.