-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_test.go
47 lines (36 loc) · 1.67 KB
/
tree_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
package wordfilter
import "testing"
func TestTreeFiltering(t *testing.T) {
assertContains(t, "WORD", true, "word")
assertContains(t, "word", true, "word")
assertContains(t, "woRd", true, "word")
assertContains(t, "woRd", true, "restricted", "word")
assertContains(t, "A sentence with a word in the middle", true, "restricted", "word")
assertContains(t, "A sentence with a word\n in the middle", true, "word", "restricted")
assertContains(t, "A sentence with a word in the middle", true, "word", "restricted")
assertContains(t, "Only good words", false, "word", "restricted")
assertContains(t, "A good sentence 123 with numbers", false, "word", "restricted")
assertContains(t, "Trailing space ", false, "word", "restricted")
assertContains(t, "A bad sentence with numbers word1", true, "word1", "restricted")
}
func assertContains(t *testing.T, input string, expected bool, reserved ...string) {
t.Helper()
tree := New(reserved...)
actual := tree.Contains(input)
if actual != expected {
t.Errorf("\nExpected: %t\nActual: %t\n", expected, actual)
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func BenchmarkTreeTest(b *testing.B) {
// 256 chars
stringToCheck := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut " +
"labore et dolore magna aliqua. Ut EMIM ad minim veniam, quis nostrud exercitation ULLAMCO laboris nisi ut " +
"aliquip ex ea commodo consequat. Duis aute irure d RANDOM"
reservedTree := New("ANOTHER", "RANDOM", "WORD")
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = reservedTree.Contains(stringToCheck)
}
}