-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautomaton_test.go
79 lines (68 loc) · 1.49 KB
/
automaton_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
package automaton
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCheck1(t *testing.T) {
var tests = []struct {
src []string
dest []byte
expected []CheckResult
}{
{
[]string{"12321", "abc", "ffdsfs"},
[]byte("11abc22ffdsfs"),
[]CheckResult{{2, 5, 1}, {7, 13, 2}},
},
{
[]string{"ab", "abc", "abcd"},
[]byte("abcd"),
[]CheckResult{{0, 2, 0}, {0, 3, 1}, {0, 4, 2}},
},
{
[]string{"abcd", "abc", "ab"},
[]byte("abcd"),
[]CheckResult{{0, 2, 2}, {0, 3, 1}, {0, 4, 0}},
},
{ //bug?
[]string{"cd", "bcd", "abcd"},
[]byte("abcd"),
[]CheckResult{{0, 4, 2}},
},
{ //bug?
[]string{"abcd", "bcd", "cd"},
[]byte("abcd"),
[]CheckResult{{1, 4, 0}},
},
{
[]string{"ab", "abc", "abcd"},
[]byte("acbcd"),
nil,
},
}
for _, test := range tests {
auto := NewAutomaton(test.src)
results := auto.Check(test.dest)
if !assert.Equal(t, len(results), len(test.expected)) {
t.Fatalf("in line with expectations,{%+v}\n", test)
}
for i := 0; i < len(results); i++ {
if !assert.Equal(t, results[i], test.expected[i]) {
t.Fatalf("in line with expectations,{%+v}\n", test)
}
}
}
}
func BenchmarkTemplateReplace(b *testing.B) {
text := "{{.Ywdxz}} is {{.Count}} of {{.Material}}"
auto := NewAutomaton([]string{"{{.Material}}", "{{.Count}}", "{{.Ywdxz}}"})
Text := text
for i := 0; i < 10000; i++ {
Text += text
}
src := []byte(Text)
b.ResetTimer()
for i := 0; i < b.N; i++ {
auto.Check(src)
}
}