-
Notifications
You must be signed in to change notification settings - Fork 0
/
ops_test.go
95 lines (84 loc) · 2.06 KB
/
ops_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
package lsdp
import (
"math/rand"
"testing"
)
func TestNearest(t *testing.T) {
answers := []string{
"book",
"back",
"cook",
}
testdata := []struct {
Raw string
Answer string
}{
{"book", "book"},
{"pack", "back"},
{"sick", "back"},
{"cop", "cook"},
}
std := Weights{1, 1, 1}
for i, td := range testdata {
ans, score := Nearest(std, td.Raw, answers)
if ans != td.Answer {
t.Errorf(`Nearest error: i=%d found "%s"(%f), want "%s"`, i, ans, score, td.Answer)
}
}
}
func TestDistanceAll(t *testing.T) {
std := Weights{1, 1, 1}
testdata := []struct {
orig string
strs []string
dists []float64
}{
{"", []string{"a", "aa", "aaa"}, []float64{1, 2, 3}},
{"aa", []string{"aa", "a"}, []float64{0, 1}},
{"a", []string{"", "a", "ab"}, []float64{1, 0, 1}},
}
for i, td := range testdata {
ds := DistanceAll(std, td.orig, td.strs)
for j, _ := range ds {
if td.dists[j] != ds[j] {
t.Errorf("%d: DistanceAll() is %v, want %v", i, ds, td.dists)
break
}
}
}
}
func makeBenchInputStrings() []string {
strs := make([]string, 1<<6)
var alphaNum string
for i := 0; i < 10; i++ {
alphaNum += "abcdefghijklmnopqrstuvwxyz0123456789"
}
runes := []rune(alphaNum)
for i := 0; i < len(strs); i++ {
rand.Shuffle(len(runes), func(i, j int) {
runes[i], runes[j] = runes[j], runes[i]
})
strs[i] = string(runes)
}
return strs
}
var benchInputStrings = makeBenchInputStrings()
func BenchmarkNearest1(b *testing.B) { benchNearest(b, "a") }
func BenchmarkNearest2(b *testing.B) { benchNearest(b, "aaaaaaaaaaaa000000000000000000") }
func benchNearest(b *testing.B, s string) {
std := Weights{1, 1, 1}
input := makeBenchInputStrings()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Nearest(std, s, input)
}
}
func BenchmarkDistanceAll1(b *testing.B) { benchDistanceAll(b, "a") }
func BenchmarkDistanceAll2(b *testing.B) { benchDistanceAll(b, "aaaaaaaaaaaa000000000000000000") }
func benchDistanceAll(b *testing.B, s string) {
std := Weights{1, 1, 1}
b.ResetTimer()
for i := 0; i < b.N; i++ {
DistanceAll(std, s, benchInputStrings)
}
}