-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_test.go
89 lines (84 loc) · 2.33 KB
/
string_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package kutils
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var (
sep = ","
splitElem = strings.Repeat("0", 40)
splittableElem = splitElem + sep + splitElem + sep + splitElem
strSplitInMiddle = []string{splitElem, splitElem, splittableElem, splitElem, splittableElem}
strNoSplit = []string{splitElem, splitElem, splitElem, splitElem, splitElem}
strAllSplittable = []string{splittableElem, splittableElem, splittableElem, splittableElem}
)
// BenchmarkSplitListElem/split_in_middle
// BenchmarkSplitListElem/split_in_middle-16 3464385 3
// 39.5 ns/op vs 51.0 ns/op using strings.Contains
// BenchmarkSplitListElem/no_split
// BenchmarkSplitListElem/no_split-16 5432838 2
// 22.2 ns/op vs 67.71 ns/op using strings.Contains
// BenchmarkSplitListElem/all_splittable
// BenchmarkSplitListElem/all_splittable-16 2987763 3
// 95.3 ns/op vs 16.8 ns/op using strings.Contains
func BenchmarkSplitListElem(b *testing.B) {
b.ResetTimer()
b.Run("split in middle", func(b *testing.B) {
for i := 0; i < b.N; i++ {
SplitListElem(strSplitInMiddle, sep)
}
})
b.Run("no split", func(b *testing.B) {
for i := 0; i < b.N; i++ {
SplitListElem(strNoSplit, sep)
}
})
b.Run("all splittable", func(b *testing.B) {
for i := 0; i < b.N; i++ {
SplitListElem(strAllSplittable, sep)
}
})
}
func TestSplitListElem(t *testing.T) {
type args struct {
lst []string
sep string
}
tests := []struct {
name string
args args
want []string
}{
{
"split in middle",
args{
lst: strSplitInMiddle,
sep: sep,
},
[]string{splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem},
},
{
"no split",
args{
lst: strNoSplit,
sep: sep,
},
[]string{splitElem, splitElem, splitElem, splitElem, splitElem},
},
{
"split in middle",
args{
lst: strAllSplittable,
sep: sep,
},
[]string{splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem, splitElem,
splitElem, splitElem, splitElem},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, SplitListElem(tt.args.lst, tt.args.sep), "SplitListElem(%v, %v)", tt.args.lst,
tt.args.sep)
})
}
}