-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaive_test.go
99 lines (81 loc) · 2.52 KB
/
naive_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
package main
import (
"testing"
)
func check(gotP, expectedP *Match, t *testing.T) {
if expectedP == nil && gotP == nil {
return
}
if expectedP == nil && gotP != nil {
t.Fatalf("expected nil got: %v", *gotP)
}
if expectedP != nil && gotP == nil {
t.Fatalf("got nil expected: %v", *expectedP)
}
got := *gotP
expected := *expectedP
if got.Distance != expected.Distance {
t.Fatalf("incorrect distance! expected: %d, got: %d", expected.Distance, got.Distance)
}
if len(got.Positions) != len(expected.Positions) {
t.Fatalf("number of matches incorrect! expected: %d (%v), got: %d (%v)",
len(expected.Positions), expected.Positions, len(got.Positions), got.Positions)
}
for i := 0; i < len(got.Positions); i++ {
if got.Positions[i] != expected.Positions[i] {
t.Fatalf("match at index %d incorrect! expected: %v, got: %v", i, expected.Positions[i], got.Positions[i])
}
}
}
var naive = naiveMatcher{}
func TestExactMatchNaive(t *testing.T) {
distance := 0
pattern := "abc"
text := "XXXabcYYY"
expected := &Match{[]StartEnd{StartEnd{3, 6}}, 0}
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
text = "XXXacYYY"
expected = nil
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
}
func TestExactOnlyFirstNaive(t *testing.T) {
distance := 0
pattern := "abc"
text := "XXXabcYYYabcZZZ"
expected := &Match{[]StartEnd{StartEnd{3, 6}}, 0}
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
}
func TestDistanceOneNaive(t *testing.T) {
distance := 1
pattern := "abc"
text := "XXXabbcYYY"
expected := &Match{[]StartEnd{StartEnd{3, 5}, StartEnd{6, 7}}, 1}
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
text = "XXXaZZbZcYYY"
expected = nil
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
}
func TestDistanceOneNotAllConsumedNaive(t *testing.T) {
distance := 1
pattern := "abc"
text := "XXXabYYY"
expected := &Match{[]StartEnd{StartEnd{3, 5}}, 1}
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
}
func TestDistanceTwoNaive(t *testing.T) {
distance := 2
pattern := "abc"
text := "XXXaZZbcYYY"
expected := &Match{[]StartEnd{StartEnd{3, 4}, StartEnd{6, 8}}, 2}
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
text = "XXXaZZZbcYYY"
expected = nil
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
}
func TestMadonna(t *testing.T) {
distance := 2
pattern := "madonna"
text := "drake madonna"
expected := &Match{[]StartEnd{StartEnd{6, 13}}, 0}
check(naive.fuzzyMatch(pattern, text, distance), expected, t)
}