-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstringmatching_test.go
54 lines (41 loc) · 1.25 KB
/
stringmatching_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
package stringmatch
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestNaiveMatch(t *testing.T) {
str := "addfassaddfdadd"
assert.Equal(t, []int{0, 7, 12}, NaiveMatching(str, "add"))
}
func TestRabinKarpMatcher(t *testing.T) {
str := "addfassaddfdadd"
matches, err := RabinKarpMatcher(str, "add")
assert.NoError(t, err)
assert.Equal(t, []int{0, 7, 12}, matches)
str = "123112233112"
matches, err = RabinKarpMatcher(str, "11")
assert.NoError(t, err)
assert.Equal(t, []int{3, 9}, matches)
}
func TestFiniteAutomationMatcher(t *testing.T) {
str := "abababacaba"
matches := FiniteAutomationMatcher(str, "ababaca", 128)
assert.Equal(t, []int{2}, matches)
str = "addfassaddfdadd"
matches = FiniteAutomationMatcher(str, "add", 128)
assert.Equal(t, []int{0, 7, 12}, matches)
str = "123112233112"
matches = FiniteAutomationMatcher(str, "11", 128)
assert.Equal(t, []int{3, 9}, matches)
}
func TestKMPMatcher(t *testing.T) {
str := "abababacaba"
matches := KMPMatcher(str, "ababaca")
assert.Equal(t, []int{2}, matches)
str = "addfassaddfdadd"
matches = FiniteAutomationMatcher(str, "add", 128)
assert.Equal(t, []int{0, 7, 12}, matches)
str3 := "123112233112"
matches3 := KMPMatcher(str3, "11")
assert.Equal(t, []int{3, 9}, matches3)
}