-
Notifications
You must be signed in to change notification settings - Fork 9
/
gokmp_test.go
72 lines (61 loc) · 1.44 KB
/
gokmp_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
package gokmp
import (
"strings"
"testing"
)
// TESTS
// pretty much the worst case string for strings.Index
// wrt. string and pattern
const str = "aabaabaaaabbaabaabaaabbaabaabb"
const pattern = "aabb"
func TestFindAllStringIndex(t *testing.T) {
kmp, _ := NewKMP(pattern)
// fmt.Println(kmp)
ints := kmp.FindAllStringIndex(str)
test := []int{8, 19, 26}
for i, v := range ints {
if test[i] != v {
t.Errorf("FindAllStringIndex:\t%v != %v, (exp: %v != act: %v)", test[i], v, ints, test)
}
}
}
func TestFindStringIndex(t *testing.T) {
kmp, _ := NewKMP(pattern)
ints := kmp.FindStringIndex(str)
test := 8
if ints != test {
t.Errorf("FindStringIndex:\t%v != %v", ints, test)
}
}
func TestContainedIn(t *testing.T) {
kmp, _ := NewKMP(pattern)
if !kmp.ContainedIn(str) {
t.Errorf("ContainedIn:\tExpected: True != actual: False")
}
}
func TestOccurrences(t *testing.T) {
kmp, _ := NewKMP(pattern)
nr := kmp.Occurrences(str)
if nr != 3 {
t.Errorf("Occurences:\texp: %v != act: %v)", 3, nr)
}
}
func TestOccurrencesFail(t *testing.T) {
kmp, _ := NewKMP(pattern)
nr := kmp.Occurrences("pebble")
if nr != 0 {
t.Errorf("Occurences:\texp: %v != act: %v)", 0, nr)
}
}
// BENCHMARKS
func BenchmarkKMPIndexComparison(b *testing.B) {
kmp, _ := NewKMP(pattern)
for i := 0; i < b.N; i++ {
_ = kmp.FindStringIndex(str)
}
}
func BenchmarkStringsIndexComparison(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = strings.Index(str, pattern)
}
}