forked from agnivade/levenshtein
-
Notifications
You must be signed in to change notification settings - Fork 0
/
levenshtein_test.go
122 lines (115 loc) · 3.28 KB
/
levenshtein_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
package levenshtein_test
import (
"testing"
agnivade "github.com/agnivade/levenshtein"
arbovm "github.com/arbovm/levenshtein"
dgryski "github.com/dgryski/trifles/leven"
)
func TestSanity(t *testing.T) {
tests := []struct {
a, b string
want int
}{
{"", "hello", 5},
{"hello", "", 5},
{"hello", "hello", 0},
{"ab", "aa", 1},
{"ab", "ba", 2},
{"ab", "aaa", 2},
{"bbb", "a", 3},
{"kitten", "sitting", 3},
{"distance", "difference", 5},
{"levenshtein", "frankenstein", 6},
{"resume and cafe", "resumes and cafes", 2},
{"a very long string that is meant to exceed", "another very long string that is meant to exceed", 6},
}
for i, d := range tests {
n := agnivade.ComputeDistance(d.a, d.b)
if n != d.want {
t.Errorf("Test[%d]: ComputeDistance(%q,%q) returned %v, want %v",
i, d.a, d.b, n, d.want)
}
}
}
func TestUnicode(t *testing.T) {
tests := []struct {
a, b string
want int
}{
// Testing acutes and umlauts
{"resumé and café", "resumés and cafés", 2},
{"resume and cafe", "resumé and café", 2},
{"Hafþór Júlíus Björnsson", "Hafþor Julius Bjornsson", 4},
// Only 2 characters are less in the 2nd string
{"།་གམ་འས་པ་་མ།", "།་གམའས་པ་་མ", 2},
}
for i, d := range tests {
n := agnivade.ComputeDistance(d.a, d.b)
if n != d.want {
t.Errorf("Test[%d]: ComputeDistance(%q,%q) returned %v, want %v",
i, d.a, d.b, n, d.want)
}
}
}
// Benchmarks
// ----------------------------------------------
var sink int
func BenchmarkSimple(b *testing.B) {
tests := []struct {
a, b string
name string
}{
// ASCII
{"levenshtein", "frankenstein", "ASCII"},
// Testing acutes and umlauts
{"resumé and café", "resumés and cafés", "French"},
{"Hafþór Júlíus Björnsson", "Hafþor Julius Bjornsson", "Nordic"},
{"a very long string that is meant to exceed", "another very long string that is meant to exceed", "long string"},
// Only 2 characters are less in the 2nd string
{"།་གམ་འས་པ་་མ།", "།་གམའས་པ་་མ", "Tibetan"},
}
tmp := 0
for _, test := range tests {
b.Run(test.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
tmp = agnivade.ComputeDistance(test.a, test.b)
}
})
}
sink = tmp
}
func BenchmarkAll(b *testing.B) {
tests := []struct {
a, b string
name string
}{
// ASCII
{"levenshtein", "frankenstein", "ASCII"},
// Testing acutes and umlauts
{"resumé and café", "resumés and cafés", "French"},
{"Hafþór Júlíus Björnsson", "Hafþor Julius Bjornsson", "Nordic"},
// Only 2 characters are less in the 2nd string
{"།་གམ་འས་པ་་མ།", "།་གམའས་པ་་མ", "Tibetan"},
}
tmp := 0
for _, test := range tests {
b.Run(test.name, func(b *testing.B) {
b.Run("agniva", func(b *testing.B) {
for n := 0; n < b.N; n++ {
tmp = agnivade.ComputeDistance(test.a, test.b)
}
})
b.Run("arbovm", func(b *testing.B) {
for n := 0; n < b.N; n++ {
tmp = arbovm.Distance(test.a, test.b)
}
})
b.Run("dgryski", func(b *testing.B) {
for n := 0; n < b.N; n++ {
tmp = dgryski.Levenshtein([]rune(test.a), []rune(test.b))
}
})
})
}
sink = tmp
}