| 1 | package match |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestAnyIndex(t *testing.T) { |
| 9 | for id, test := range []struct { |
| 10 | sep []rune |
| 11 | fixture string |
| 12 | index int |
| 13 | segments []int |
| 14 | }{ |
| 15 | { |
| 16 | []rune{'.'}, |
| 17 | "abc", |
| 18 | 0, |
| 19 | []int{0, 1, 2, 3}, |
| 20 | }, |
| 21 | { |
| 22 | []rune{'.'}, |
| 23 | "abc.def", |
| 24 | 0, |
| 25 | []int{0, 1, 2, 3}, |
| 26 | }, |
| 27 | } { |
| 28 | p := NewAny(test.sep) |
| 29 | index, segments := p.Index(test.fixture) |
| 30 | if index != test.index { |
| 31 | t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) |
| 32 | } |
| 33 | if !reflect.DeepEqual(segments, test.segments) { |
| 34 | t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func BenchmarkIndexAny(b *testing.B) { |
| 40 | m := NewAny(bench_separators) |
| 41 | |
| 42 | for i := 0; i < b.N; i++ { |
| 43 | _, s := m.Index(bench_pattern) |
| 44 | releaseSegments(s) |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | func BenchmarkIndexAnyParallel(b *testing.B) { |
| 49 | m := NewAny(bench_separators) |
| 50 | |
| 51 | b.RunParallel(func(pb *testing.PB) { |
| 52 | for pb.Next() { |
| 53 | _, s := m.Index(bench_pattern) |
| 54 | releaseSegments(s) |
| 55 | } |
| 56 | }) |
| 57 | } |