...
1 package match
2
3 import (
4 "testing"
5 )
6
7 func TestBTree(t *testing.T) {
8 for id, test := range []struct {
9 tree BTree
10 str string
11 exp bool
12 }{
13 {
14 NewBTree(NewText("abc"), NewSuper(), NewSuper()),
15 "abc",
16 true,
17 },
18 {
19 NewBTree(NewText("a"), NewSingle(nil), NewSingle(nil)),
20 "aaa",
21 true,
22 },
23 {
24 NewBTree(NewText("b"), NewSingle(nil), nil),
25 "bbb",
26 false,
27 },
28 {
29 NewBTree(
30 NewText("c"),
31 NewBTree(
32 NewSingle(nil),
33 NewSuper(),
34 nil,
35 ),
36 nil,
37 ),
38 "abc",
39 true,
40 },
41 } {
42 act := test.tree.Match(test.str)
43 if act != test.exp {
44 t.Errorf("#%d match %q error: act: %t; exp: %t", id, test.str, act, test.exp)
45 continue
46 }
47 }
48 }
49
50 type fakeMatcher struct {
51 len int
52 name string
53 }
54
55 func (f *fakeMatcher) Match(string) bool {
56 return true
57 }
58
59 var i = 3
60
61 func (f *fakeMatcher) Index(s string) (int, []int) {
62 seg := make([]int, 0, i)
63 for x := 0; x < i; x++ {
64 seg = append(seg, x)
65 }
66 return 0, seg
67 }
68 func (f *fakeMatcher) Len() int {
69 return f.len
70 }
71 func (f *fakeMatcher) String() string {
72 return f.name
73 }
74
75 func BenchmarkMatchBTree(b *testing.B) {
76 l := &fakeMatcher{4, "left_fake"}
77 r := &fakeMatcher{4, "right_fake"}
78 v := &fakeMatcher{2, "value_fake"}
79
80
81 fixture := "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
82
83 bt := NewBTree(v, l, r)
84
85 b.RunParallel(func(pb *testing.PB) {
86 for pb.Next() {
87 bt.Match(fixture)
88 }
89 })
90 }
91
View as plain text