| 1 | package match |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestRowIndex(t *testing.T) { |
| 9 | for id, test := range []struct { |
| 10 | matchers Matchers |
| 11 | length int |
| 12 | fixture string |
| 13 | index int |
| 14 | segments []int |
| 15 | }{ |
| 16 | { |
| 17 | Matchers{ |
| 18 | NewText("abc"), |
| 19 | NewText("def"), |
| 20 | NewSingle(nil), |
| 21 | }, |
| 22 | 7, |
| 23 | "qweabcdefghij", |
| 24 | 3, |
| 25 | []int{7}, |
| 26 | }, |
| 27 | { |
| 28 | Matchers{ |
| 29 | NewText("abc"), |
| 30 | NewText("def"), |
| 31 | NewSingle(nil), |
| 32 | }, |
| 33 | 7, |
| 34 | "abcd", |
| 35 | -1, |
| 36 | nil, |
| 37 | }, |
| 38 | } { |
| 39 | p := NewRow(test.length, test.matchers...) |
| 40 | index, segments := p.Index(test.fixture) |
| 41 | if index != test.index { |
| 42 | t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) |
| 43 | } |
| 44 | if !reflect.DeepEqual(segments, test.segments) { |
| 45 | t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func BenchmarkRowIndex(b *testing.B) { |
| 51 | m := NewRow( |
| 52 | 7, |
| 53 | Matchers{ |
| 54 | NewText("abc"), |
| 55 | NewText("def"), |
| 56 | NewSingle(nil), |
| 57 | }..., |
| 58 | ) |
| 59 | |
| 60 | for i := 0; i < b.N; i++ { |
| 61 | _, s := m.Index(bench_pattern) |
| 62 | releaseSegments(s) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | func BenchmarkIndexRowParallel(b *testing.B) { |
| 67 | m := NewRow( |
| 68 | 7, |
| 69 | Matchers{ |
| 70 | NewText("abc"), |
| 71 | NewText("def"), |
| 72 | NewSingle(nil), |
| 73 | }..., |
| 74 | ) |
| 75 | |
| 76 | b.RunParallel(func(pb *testing.PB) { |
| 77 | for pb.Next() { |
| 78 | _, s := m.Index(bench_pattern) |
| 79 | releaseSegments(s) |
| 80 | } |
| 81 | }) |
| 82 | } |