add bencharks for indexing

This commit is contained in:
s.kamardin 2016-01-18 13:07:28 +03:00
parent 55776ffb29
commit b601cfa2a5
16 changed files with 336 additions and 0 deletions

View File

@ -21,6 +21,8 @@ func (self Any) Index(s string) (int, []int) {
switch found {
case -1:
sub = s
case 0:
return 0, []int{0}
default:
sub = s[:found]
}

View File

@ -35,3 +35,10 @@ func TestAnyIndex(t *testing.T) {
}
}
}
func BenchmarkIndexAny(b *testing.B) {
p := Any{bench_separators}
for i := 0; i < b.N; i++ {
p.Index(bench_pattern)
}
}

View File

@ -52,3 +52,10 @@ func TestContainsIndex(t *testing.T) {
}
}
}
func BenchmarkIndexContains(b *testing.B) {
m := Contains{bench_separators, true}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

View File

@ -38,3 +38,10 @@ func TestListIndex(t *testing.T) {
}
}
}
func BenchmarkIndexList(b *testing.B) {
m := List{"def", false}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

View File

@ -5,6 +5,9 @@ import (
"testing"
)
const bench_separators = "."
const bench_pattern = "abcdefghijklmnopqrstuvwxyz0123456789"
func TestMergeSegments(t *testing.T) {
for id, test := range []struct {
segments [][]int

View File

@ -35,3 +35,10 @@ func TestMaxIndex(t *testing.T) {
}
}
}
func BenchmarkIndexMax(b *testing.B) {
m := Max{10}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

View File

@ -35,3 +35,10 @@ func TestMinIndex(t *testing.T) {
}
}
}
func BenchmarkIndexMin(b *testing.B) {
m := Min{10}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

41
match/nothing_test.go Normal file
View File

@ -0,0 +1,41 @@
package match
import (
"reflect"
"testing"
)
func TestNothingIndex(t *testing.T) {
for id, test := range []struct {
fixture string
index int
segments []int
}{
{
"abc",
0,
[]int{0},
},
{
"",
0,
[]int{0},
},
} {
p := Nothing{}
index, segments := p.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
}
if !reflect.DeepEqual(segments, test.segments) {
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments)
}
}
}
func BenchmarkIndexNothing(b *testing.B) {
m := Max{10}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

View File

@ -45,3 +45,10 @@ func TestPrefixSuffixIndex(t *testing.T) {
}
}
}
func BenchmarkIndexPrefixSuffix(b *testing.B) {
m := PrefixSuffix{"qew", "sqw"}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

View File

@ -35,3 +35,10 @@ func TestPrefixIndex(t *testing.T) {
}
}
}
func BenchmarkIndexPrefix(b *testing.B) {
m := Prefix{"qew"}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

54
match/range_test.go Normal file
View File

@ -0,0 +1,54 @@
package match
import (
"reflect"
"testing"
)
func TestRangeIndex(t *testing.T) {
for id, test := range []struct {
lo, hi rune
not bool
fixture string
index int
segments []int
}{
{
'a', 'z',
false,
"abc",
0,
[]int{1},
},
{
'a', 'c',
false,
"abcd",
0,
[]int{1},
},
{
'a', 'c',
true,
"abcd",
3,
[]int{1},
},
} {
m := Range{test.lo, test.hi, test.not}
index, segments := m.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
}
if !reflect.DeepEqual(segments, test.segments) {
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments)
}
}
}
func BenchmarkIndexRange(b *testing.B) {
m := Range{'0', '9', false}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

51
match/row_test.go Normal file
View File

@ -0,0 +1,51 @@
package match
import (
"reflect"
"testing"
)
func TestRowIndex(t *testing.T) {
for id, test := range []struct {
matchers Matchers
length int
fixture string
index int
segments []int
}{
{
Matchers{
NewText("abc"),
NewText("def"),
Single{},
},
7,
"qweabcdefghij",
3,
[]int{7},
},
{
Matchers{
NewText("abc"),
NewText("def"),
Single{},
},
7,
"abcd",
-1,
nil,
},
} {
p := Row{
Matchers: test.matchers,
RunesLength: test.length,
}
index, segments := p.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
}
if !reflect.DeepEqual(segments, test.segments) {
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments)
}
}
}

44
match/single_test.go Normal file
View File

@ -0,0 +1,44 @@
package match
import (
"reflect"
"testing"
)
func TestSingleIndex(t *testing.T) {
for id, test := range []struct {
separators string
fixture string
index int
segments []int
}{
{
".",
".abc",
1,
[]int{1},
},
{
".",
".",
-1,
nil,
},
} {
p := Single{test.separators}
index, segments := p.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
}
if !reflect.DeepEqual(segments, test.segments) {
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments)
}
}
}
func BenchmarkIndexSingle(b *testing.B) {
m := Single{bench_separators}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

View File

@ -35,3 +35,10 @@ func TestSuffixIndex(t *testing.T) {
}
}
}
func BenchmarkIndexSuffix(b *testing.B) {
m := Suffix{"qwe"}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

41
match/super_test.go Normal file
View File

@ -0,0 +1,41 @@
package match
import (
"reflect"
"testing"
)
func TestSuperIndex(t *testing.T) {
for id, test := range []struct {
fixture string
index int
segments []int
}{
{
"abc",
0,
[]int{0, 1, 2, 3},
},
{
"",
0,
[]int{0},
},
} {
p := Super{}
index, segments := p.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
}
if !reflect.DeepEqual(segments, test.segments) {
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments)
}
}
}
func BenchmarkIndexSuper(b *testing.B) {
m := Super{}
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}

44
match/text_test.go Normal file
View File

@ -0,0 +1,44 @@
package match
import (
"reflect"
"testing"
)
func TestTextIndex(t *testing.T) {
for id, test := range []struct {
text string
fixture string
index int
segments []int
}{
{
"b",
"abc",
1,
[]int{1},
},
{
"f",
"abcd",
-1,
nil,
},
} {
m := NewText(test.text)
index, segments := m.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
}
if !reflect.DeepEqual(segments, test.segments) {
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments)
}
}
}
func BenchmarkIndexText(b *testing.B) {
m := NewText("foo")
for i := 0; i < b.N; i++ {
m.Index(bench_pattern)
}
}