-
Notifications
You must be signed in to change notification settings - Fork 0
/
true_damerau_levenshtein_test.go
84 lines (75 loc) · 1.93 KB
/
true_damerau_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
package stringdist_test
import (
"math/rand"
"reflect"
"strings"
"testing"
"testing/quick"
"github.com/alextanhongpin/stringdist"
)
type TestString struct {
source string
target string
}
func (TestString) Generate(r *rand.Rand, size int) reflect.Value {
p := TestString{}
a, b := rand.Int()%256, rand.Int()%256
var sb strings.Builder
var alphabets = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}
for i := 0; i < a; i++ {
sb.WriteString(alphabets[i%26])
}
p.source = sb.String()
sb.Reset()
for i := 0; i < b; i++ {
sb.WriteString(alphabets[i%26])
}
p.target = sb.String()
return reflect.ValueOf(p)
}
// Test if the values are within the upper and lower boundary.
func TestTrueDamerauLevenshteinQuickCheck(t *testing.T) {
t.Skip()
T := t
dl := stringdist.NewTrueDamerauLevenshtein()
f := func(ts TestString) bool {
s, t := ts.source, ts.target
m, n := len(s), len(t)
// At least the difference of the size of the two string
lower := max(m, n) - min(m, n)
// At most the max of the longer string.
upper := max(m, n)
dist := dl.Calculate(s, t)
T.Log(dist, s, t, lower, upper)
return dist >= lower && dist <= upper
}
if err := quick.Check(f, nil); err != nil {
t.Fatal(err)
}
}
func TestTrueDamerauLevenshteinDistance(t *testing.T) {
dl := stringdist.NewTrueDamerauLevenshtein()
tests := []struct {
source, target string
dist int
}{
{"a", "", 1},
{"", "a", 1},
{"ac", "ca", 2},
{"CA", "ABC", 3},
{"4XHYWD", "YLKTW9", 5},
{"YLKTW9", "4XHYWD", 5},
{"hello", "hello", 0},
{"kitten", "sitting", 3},
{"", "xyz", 3},
{"x", "xyz", 2},
{"xy", "xyz", 1},
{"yx", "xyz", 2},
{"car", "rac", 2},
}
for _, tt := range tests {
if dist := dl.Calculate(tt.source, tt.target); dist != tt.dist {
t.Fatalf("expected %d, got %d for source %s, target %s", tt.dist, dist, tt.source, tt.target)
}
}
}