-
Notifications
You must be signed in to change notification settings - Fork 0
/
lev_test.go
137 lines (119 loc) · 3.14 KB
/
lev_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package lev
import (
"fmt"
"math/rand"
"reflect"
"runtime"
"sort"
"testing"
)
type TestCase struct {
a, b string
want int // expected Levenshtein result
wantD int // expected Damerau-Levenshtein result
}
var testCases = []TestCase{
{"foo", "", 3, 3}, // three deletions
{"", "asdf", 4, 4}, // four additions
{"foo", "bar", 3, 3}, // three replacements
{"foo", "foobar", 3, 3}, // three additions ("bar")
{"foobar", "bar", 3, 3}, // three deletions ("foo")
{"foo", "foo", 0, 0}, // identical
{"fast", "past", 1, 1}, // one replacement
{"boot", "tube", 4, 4}, // four replacements
{"cabbages", "rabbit", 5, 5},
{"foot", "poof", 2, 2},
{"kitten", "sitting", 3, 3},
{"alphabet", "fgsdgszxc", 9, 9},
{"aaaaaaaaaa", "bbbbbbbbbb", 10, 10},
{"ab", "ba", 2, 1},
{"abab", "baba", 2, 2},
{"abcd", "badc", 3, 2},
{"blah", "flha", 3, 2},
}
// Tests a Levenshtein distance function
func testFn(t *testing.T, fn func(a, b string) int) {
t.Helper()
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s(%q, %q)", fnName, tc.a, tc.b), func(t *testing.T) {
got := fn(tc.a, tc.b)
if got != tc.want {
t.Errorf("%s(%q, %q): Got %d, want %d", fnName, tc.a, tc.b, got, tc.want)
t.FailNow()
}
})
}
}
// Tests a Damerau-Levenshtein distance function
func testFnD(t *testing.T, fn func(a, b string) int) {
t.Helper()
fnName := runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s(%q, %q)", fnName, tc.a, tc.b), func(t *testing.T) {
got := fn(tc.a, tc.b)
if got != tc.wantD {
t.Errorf("%s(%q, %q): Got %d, want %d", fnName, tc.a, tc.b, got, tc.wantD)
t.FailNow()
}
})
}
}
func TestDistance(t *testing.T) {
testFn(t, Distance)
}
func TestNaiveDistance(t *testing.T) {
testFn(t, naiveDistance)
}
func TestMatrixDistance(t *testing.T) {
testFn(t, matrixDistance)
}
func TestMatrixDistanceD(t *testing.T) {
testFnD(t, matrixDistanceD)
}
func TestDoubleRowDistance(t *testing.T) {
testFn(t, doubleRowDistance)
}
func TestSingleRowDistance(t *testing.T) {
testFn(t, singleRowDistance)
}
func BenchmarkNaiveDistance(b *testing.B) {
for n := 0; n < b.N; n++ {
naiveDistance("aaaaaaaaaa", "bbbbbbbbbb")
}
}
func BenchmarkMatrixDistance(b *testing.B) {
for n := 0; n < b.N; n++ {
matrixDistance("aaaaaaaaaa", "bbbbbbbbbb")
}
}
func BenchmarkDoubleRowDistance(b *testing.B) {
for n := 0; n < b.N; n++ {
doubleRowDistance("aaaaaaaaaa", "bbbbbbbbbb")
}
}
func BenchmarkSingleRowDistance(b *testing.B) {
for n := 0; n < b.N; n++ {
singleRowDistance("a", "b")
singleRowDistance("aa", "bb")
singleRowDistance("aaaaa", "bbbbbbbb")
singleRowDistance("aaaaaaa", "bbbbbbbbbb")
singleRowDistance("aaaaaaaaaa", "bbbbbbbbbb")
singleRowDistance("a", "bbbbbbbbbb")
singleRowDistance("aaaaaaaaaa", "b")
}
}
func TestMin(t *testing.T) {
for n := 0; n < 10000; n++ {
a := rand.Int()
b := rand.Int()
c := rand.Int()
arr := []int{a, b, c}
sort.Ints(arr)
want := arr[0]
got := min3(a, b, c)
if got != want {
t.Errorf("min(%d, %d, %d): Got %d, want %d", a, b, c, got, want)
}
}
}