| 1 | package match |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | "unicode/utf8" |
| 7 | ) |
| 8 | |
| 9 | var bench_separators = []rune{'.'} |
| 10 | |
| 11 | const bench_pattern = "abcdefghijklmnopqrstuvwxyz0123456789" |
| 12 | |
| 13 | func TestAppendMerge(t *testing.T) { |
| 14 | for id, test := range []struct { |
| 15 | segments [2][]int |
| 16 | exp []int |
| 17 | }{ |
| 18 | { |
| 19 | [2][]int{ |
| 20 | {0, 6, 7}, |
| 21 | {0, 1, 3}, |
| 22 | }, |
| 23 | []int{0, 1, 3, 6, 7}, |
| 24 | }, |
| 25 | { |
| 26 | [2][]int{ |
| 27 | {0, 1, 3, 6, 7}, |
| 28 | {0, 1, 10}, |
| 29 | }, |
| 30 | []int{0, 1, 3, 6, 7, 10}, |
| 31 | }, |
| 32 | } { |
| 33 | act := appendMerge(test.segments[0], test.segments[1]) |
| 34 | if !reflect.DeepEqual(act, test.exp) { |
| 35 | t.Errorf("#%d merge sort segments unexpected:\nact: %v\nexp:%v", id, act, test.exp) |
| 36 | continue |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func BenchmarkAppendMerge(b *testing.B) { |
| 42 | s1 := []int{0, 1, 3, 6, 7} |
| 43 | s2 := []int{0, 1, 3} |
| 44 | |
| 45 | for i := 0; i < b.N; i++ { |
| 46 | appendMerge(s1, s2) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | func BenchmarkAppendMergeParallel(b *testing.B) { |
| 51 | s1 := []int{0, 1, 3, 6, 7} |
| 52 | s2 := []int{0, 1, 3} |
| 53 | |
| 54 | b.RunParallel(func(pb *testing.PB) { |
| 55 | for pb.Next() { |
| 56 | appendMerge(s1, s2) |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | func BenchmarkReverse(b *testing.B) { |
| 62 | for i := 0; i < b.N; i++ { |
| 63 | reverseSegments([]int{1, 2, 3, 4}) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func getTable() []int { |
| 68 | table := make([]int, utf8.MaxRune+1) |
| 69 | for i := 0; i <= utf8.MaxRune; i++ { |
| 70 | table[i] = utf8.RuneLen(rune(i)) |
| 71 | } |
| 72 | |
| 73 | return table |
| 74 | } |
| 75 | |
| 76 | var table = getTable() |
| 77 | |
| 78 | const runeToLen = 'q' |
| 79 | |
| 80 | func BenchmarkRuneLenFromTable(b *testing.B) { |
| 81 | for i := 0; i < b.N; i++ { |
| 82 | _ = table[runeToLen] |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | func BenchmarkRuneLenFromUTF8(b *testing.B) { |
| 87 | for i := 0; i < b.N; i++ { |
| 88 | _ = utf8.RuneLen(runeToLen) |
| 89 | } |
| 90 | } |