-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathahocorasick_test.go
99 lines (84 loc) · 2.12 KB
/
ahocorasick_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 ahocorasick
import (
"fmt"
"math/rand"
"regexp"
"testing"
"time"
)
var RegMatcher = regexp.MustCompile(BuildSensitiveStr("./sensitive_words.csv"))
var words = BuildSensitiveArray("./sensitive_words.csv")
var AcMatcher = NewMatcher(words)
var hasSensStr string
var noSensStr string
func init() {
rand.Seed(time.Now().Unix())
hasSensStr = fmt.Sprintf("AA%sBB%s%sCC", words[rand.Intn(len(words))], words[rand.Intn(len(words))], words[rand.Intn(len(words))])
noSensStr = "你真是个伟人哈哈呵呵火啊物abcd"
}
func TestACMatcher_Match(t *testing.T) {
ret1 := AcMatcher.Match(hasSensStr)
if len(ret1) == 0 {
t.Fatal(hasSensStr)
}
ret2 := AcMatcher.Match(noSensStr)
if len(ret2) != 0 {
t.Fatal(noSensStr)
}
}
func TestACMatcher_Replace(t *testing.T) {
fmt.Println("origin:" + hasSensStr)
rep := AcMatcher.Replace(hasSensStr, "*")
fmt.Println("AC replace:" + rep)
fmt.Println("Regexp replace:", RegMatcher.ReplaceAllString(hasSensStr, "*"))
}
func TestACMatcher_Has(t *testing.T) {
ret1 := AcMatcher.Has(hasSensStr)
if ret1 == false {
t.Fatal(hasSensStr)
}
ret2 := AcMatcher.Has(noSensStr)
if ret2 == true {
t.Fatal(noSensStr)
}
}
func BenchmarkRegMatcher_Reg_Has(b *testing.B) {
for idx := 1; idx < 50; idx++ {
RegMatcher.MatchString(hasSensStr)
}
}
func BenchmarkRegMatcher_Reg_No(b *testing.B) {
for idx := 1; idx < 50; idx++ {
RegMatcher.MatchString(noSensStr)
}
}
func BenchmarkACMatcher_Ac_Has(b *testing.B) {
for idx := 1; idx < 50000; idx++ {
AcMatcher.Has(hasSensStr)
}
}
func BenchmarkACMatcher_Ac_No(b *testing.B) {
for idx := 1; idx < 50000; idx++ {
AcMatcher.Has(noSensStr)
}
}
func BenchmarkACMatcher_Ac_MatchHas(b *testing.B) {
for idx := 1; idx < 50000; idx++ {
AcMatcher.Match(hasSensStr)
}
}
func BenchmarkACMatcher_Ac_MatchNo(b *testing.B) {
for idx := 1; idx < 50000; idx++ {
AcMatcher.Match(noSensStr)
}
}
func BenchmarkRegMatcher_Replace(b *testing.B) {
for idx := 1; idx < 50; idx++ {
RegMatcher.ReplaceAllString(hasSensStr, "*")
}
}
func BenchmarkACMatcher_Replace(b *testing.B) {
for idx := 1; idx < 50000; idx++ {
AcMatcher.Replace(hasSensStr, "*")
}
}