| 1 | package match |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestRangeIndex(t *testing.T) { |
| 9 | for id, test := range []struct { |
| 10 | lo, hi rune |
| 11 | not bool |
| 12 | fixture string |
| 13 | index int |
| 14 | segments []int |
| 15 | }{ |
| 16 | { |
| 17 | 'a', 'z', |
| 18 | false, |
| 19 | "abc", |
| 20 | 0, |
| 21 | []int{1}, |
| 22 | }, |
| 23 | { |
| 24 | 'a', 'c', |
| 25 | false, |
| 26 | "abcd", |
| 27 | 0, |
| 28 | []int{1}, |
| 29 | }, |
| 30 | { |
| 31 | 'a', 'c', |
| 32 | true, |
| 33 | "abcd", |
| 34 | 3, |
| 35 | []int{1}, |
| 36 | }, |
| 37 | } { |
| 38 | m := NewRange(test.lo, test.hi, test.not) |
| 39 | index, segments := m.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 | |
| 49 | func BenchmarkIndexRange(b *testing.B) { |
| 50 | m := NewRange('0', '9', false) |
| 51 | |
| 52 | for i := 0; i < b.N; i++ { |
| 53 | _, s := m.Index(bench_pattern) |
| 54 | releaseSegments(s) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func BenchmarkIndexRangeParallel(b *testing.B) { |
| 59 | m := NewRange('0', '9', false) |
| 60 | |
| 61 | b.RunParallel(func(pb *testing.PB) { |
| 62 | for pb.Next() { |
| 63 | _, s := m.Index(bench_pattern) |
| 64 | releaseSegments(s) |
| 65 | } |
| 66 | }) |
| 67 | } |