Skip to content

Commit

Permalink
Add Hamming distance test to the fuzzing function
Browse files Browse the repository at this point in the history
  • Loading branch information
psadac committed Dec 22, 2024
1 parent eb2b813 commit b25fcec
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions levenshtein_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func BenchmarkRandom(b *testing.B) {
// random rune sequences, applies a number of changes to one sequence, and then
// tests whether the three implementations produce the same result. The test fails
// if there is any discrepancy between the results of the different algorithms.
// Additionally, it also tests if the levenshtein distance is at most the hamming distance.
func FuzzComputeDistance(f *testing.F) {
const (
nbSeeds = 100 // number of seeds.
Expand All @@ -242,9 +243,27 @@ func FuzzComputeDistance(f *testing.F) {
if da != dar || da != ddg {
t.Errorf("ComputeDistance(%s,%s) returned %d, want %d (arbovm) or %d (dgryski)", a, b, da, dar, ddg)
}

dh := hammingDistance([]rune(a), []rune(b))
if da > dh {
t.Errorf("ComputeDistance(%s,%s) returned %d, want at most %d (hamming distance)", a, b, da, dh)
}
})
}

func hammingDistance(a, b []rune) int {
if len(a) > len(b) {
a, b = b, a
}
d := len(b) - len(a)
for i, r := range a {
if r != b[i] {
d++
}
}
return d
}

// Random runes generation functions
// ----------------------------------

Expand Down

0 comments on commit b25fcec

Please sign in to comment.