-
Notifications
You must be signed in to change notification settings - Fork 1
/
didyoumean_test.go
147 lines (138 loc) · 3.45 KB
/
didyoumean_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
138
139
140
141
142
143
144
145
146
147
package didyoumean
import (
"testing"
)
type EditDistanceTest struct {
A string
B string
Distance int
}
type FirstMatchTest struct {
Key string
List []string
Match string
ThreadRate float64
CaseInsensitive bool
}
type MatchTest struct {
Key string
List []string
Match []string
ThreadRate float64
CaseInsensitive bool
}
var (
editDistanceTests = []EditDistanceTest{
{
A: "kitten",
B: "sitting",
Distance: 3,
}, {
A: "Saturday",
B: "Sunday",
Distance: 3,
}, {
A: "台灣 Taiwan",
B: "台味",
Distance: 7,
},
}
firstMatchTests = []FirstMatchTest{
{
Key: "insargrm",
List: []string{"facebook", "twitter", "instagram", "linkedin", "台灣 Taiwan"},
Match: "instagram",
ThreadRate: 0.4,
}, {
Key: "insargrm",
List: []string{"facebook", "twitter", "instagram", "linkedin", "台灣 Taiwan"},
Match: "",
ThreadRate: 0.3,
},
{
Key: "insarGrm",
List: []string{"facebook", "twiTter", "InstaGram", "linkedin", "台灣 Taiwan"},
Match: "",
ThreadRate: 0.4,
CaseInsensitive: false,
}, {
Key: "insarGrm",
List: []string{"facebook", "twitter", "InstaGram", "linkedin", "台灣 Taiwan"},
Match: "InstaGram",
ThreadRate: 0.5,
CaseInsensitive: false,
},
}
matchTests = []MatchTest{
{
Key: "insargrm",
List: []string{"facebook", "twitter", "instagram", "linkedin", "台灣 Taiwan"},
Match: []string{"instagram"},
ThreadRate: 0.4,
}, {
Key: "insargrm",
List: []string{"facebook", "twitter", "instagram", "linkedin", "台灣 Taiwan"},
Match: []string{},
ThreadRate: 0.3,
}, {
Key: "insarGrm",
List: []string{"facebook", "twiTter", "InstaGram", "linkedin", "台灣 Taiwan"},
Match: []string{},
ThreadRate: 0.4,
CaseInsensitive: false,
}, {
Key: "insarGrm",
List: []string{"facebook", "twitter", "InstaGram", "linkedin", "台灣 Taiwan"},
Match: []string{"InstaGram"},
ThreadRate: 0.5,
CaseInsensitive: false,
},
}
)
// TestFindEditDistance
func TestFindEditDistance(t *testing.T) {
for _, test := range editDistanceTests {
d := findEditDistance(test.A, test.B)
if d != test.Distance {
t.Fatalf("The distance should be %d but got %d", test.Distance, d)
}
}
}
// FuzzFindEditDistance
func FuzzFindEditDistance(f *testing.F) {
for _, test := range editDistanceTests {
f.Add(test.A, test.B)
}
f.Fuzz(func(t *testing.T, a string, b string) {
d := findEditDistance(a, b)
if d < 0 {
t.Errorf("distance should not be smaller than 0: %d", d)
}
})
}
// TestFirstMatch
func TestFirstMatch(t *testing.T) {
for _, test := range firstMatchTests {
ThresholdRate = test.ThreadRate
CaseInsensitive = test.CaseInsensitive
m := FirstMatch(test.Key, test.List)
if m != test.Match {
t.Fatalf("The match should be %s but got %s", test.Match, m)
}
}
}
// TestMatch
func TestMatch(t *testing.T) {
for _, test := range matchTests {
ThresholdRate = test.ThreadRate
m := Match(test.Key, test.List)
if len(m) != len(test.Match) {
t.Fatalf("The match should be equal")
}
for i, v := range m {
if m[i] != v {
t.Fatalf("The match should be %s but got %s", m[i], v)
}
}
}
}