1package match
2
3import (
4	"reflect"
5	"testing"
6)
7
8func TestPrefixSuffixIndex(t *testing.T) {
9	for id, test := range []struct {
10		prefix   string
11		suffix   string
12		fixture  string
13		index    int
14		segments []int
15	}{
16		{
17			"a",
18			"c",
19			"abc",
20			0,
21			[]int{3},
22		},
23		{
24			"f",
25			"f",
26			"fffabfff",
27			0,
28			[]int{1, 2, 3, 6, 7, 8},
29		},
30		{
31			"ab",
32			"bc",
33			"abc",
34			0,
35			[]int{3},
36		},
37	} {
38		p := NewPrefixSuffix(test.prefix, test.suffix)
39		index, segments := p.Index(test.fixture)
40		if index != test.index {
41			t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
42		}
43		if !reflect.DeepEqual(segments, test.segments) {
44			t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments)
45		}
46	}
47}
48
49func BenchmarkIndexPrefixSuffix(b *testing.B) {
50	m := NewPrefixSuffix("qew", "sqw")
51
52	for i := 0; i < b.N; i++ {
53		_, s := m.Index(bench_pattern)
54		releaseSegments(s)
55	}
56}
57
58func BenchmarkIndexPrefixSuffixParallel(b *testing.B) {
59	m := NewPrefixSuffix("qew", "sqw")
60
61	b.RunParallel(func(pb *testing.PB) {
62		for pb.Next() {
63			_, s := m.Index(bench_pattern)
64			releaseSegments(s)
65		}
66	})
67}
68