2016-01-08 20:14:31 +03:00
|
|
|
package match
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBTree(t *testing.T) {
|
|
|
|
for id, test := range []struct {
|
|
|
|
tree BTree
|
|
|
|
str string
|
|
|
|
exp bool
|
|
|
|
}{
|
|
|
|
{
|
2016-01-15 19:50:12 +03:00
|
|
|
NewBTree(NewText("abc"), Super{}, Super{}),
|
2016-01-08 20:14:31 +03:00
|
|
|
"abc",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2016-01-15 19:50:12 +03:00
|
|
|
NewBTree(NewText("a"), Single{}, Single{}),
|
2016-01-08 20:14:31 +03:00
|
|
|
"aaa",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
2016-01-15 19:50:12 +03:00
|
|
|
NewBTree(NewText("b"), Single{}, nil),
|
2016-01-08 20:14:31 +03:00
|
|
|
"bbb",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
2016-01-15 19:50:12 +03:00
|
|
|
NewBTree(
|
|
|
|
NewText("c"),
|
|
|
|
NewBTree(
|
|
|
|
Single{},
|
|
|
|
Super{},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
nil,
|
|
|
|
),
|
2016-01-08 20:14:31 +03:00
|
|
|
"abc",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
act := test.tree.Match(test.str)
|
|
|
|
if act != test.exp {
|
|
|
|
t.Errorf("#%d match %q error: act: %t; exp: %t", id, test.str, act, test.exp)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 18:47:01 +03:00
|
|
|
|
|
|
|
type fakeMatcher struct {
|
|
|
|
len int
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fakeMatcher) Match(string) bool {
|
|
|
|
return true
|
|
|
|
}
|
2016-02-22 22:24:05 +03:00
|
|
|
|
|
|
|
var i = 3
|
|
|
|
|
2016-02-22 18:47:01 +03:00
|
|
|
func (f *fakeMatcher) Index(s string, seg []int) (int, []int) {
|
2016-02-22 22:24:05 +03:00
|
|
|
for x := 0; x < i; x++ {
|
|
|
|
seg = append(seg, x)
|
|
|
|
}
|
|
|
|
return 0, seg
|
2016-02-22 18:47:01 +03:00
|
|
|
}
|
|
|
|
func (f *fakeMatcher) Len() int {
|
|
|
|
return f.len
|
|
|
|
}
|
|
|
|
func (f *fakeMatcher) String() string {
|
|
|
|
return f.name
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkMatchBTree(b *testing.B) {
|
|
|
|
l := &fakeMatcher{4, "left_fake"}
|
|
|
|
r := &fakeMatcher{4, "right_fake"}
|
|
|
|
v := &fakeMatcher{2, "value_fake"}
|
|
|
|
|
|
|
|
// must be <= len(l + r + v)
|
2016-02-22 20:11:06 +03:00
|
|
|
fixture := "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij"
|
2016-02-22 18:47:01 +03:00
|
|
|
|
|
|
|
bt := NewBTree(v, l, r)
|
|
|
|
|
2016-02-22 22:17:07 +03:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
bt.Match(fixture)
|
|
|
|
}
|
|
|
|
})
|
2016-02-22 18:47:01 +03:00
|
|
|
}
|