Repository navigation
Expand file tree
/
Copy pathseq_test.go
More file actions
161 lines (148 loc) · 3.87 KB
/
Copy pathseq_test.go
File metadata and controls
161 lines (148 loc) · 3.87 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package utils
import (
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// refSplitTrim is the reference for SplitTrimSeq: split, trim, drop empties.
func refSplitTrim(s string, sep byte) []string {
var out []string
for part := range strings.SplitSeq(s, string(sep)) {
if part = TrimSpace(part); part != "" {
out = append(out, part)
}
}
return out
}
func splitTrimSamples() []string {
return []string{
"",
",",
" , ,",
"\t,\r\n",
"a",
" a ",
"a,b",
"a, b ,c",
",,a,,",
"a,",
",a",
"gzip, deflate, br",
"text/html;q=0.9, */*;q=0.8",
"keep-alive,\tUpgrade",
" leading, trailing ",
"no separator at all",
}
}
func Test_SplitTrimSeq(t *testing.T) {
t.Parallel()
for _, in := range splitTrimSamples() {
for _, sep := range []byte{',', ';', ' '} {
want := refSplitTrim(in, sep)
var got []string
for elem := range SplitTrimSeq(in, sep) {
got = append(got, elem)
}
require.Equal(t, want, got, "SplitTrimSeq(%q, %q)", in, sep)
var gotBytes []string
for elem := range SplitTrimSeq([]byte(in), sep) {
gotBytes = append(gotBytes, string(elem))
}
require.Equal(t, want, gotBytes, "SplitTrimSeq(%q, %q) bytes", in, sep)
}
}
// Breaking out of the loop stops the iteration after the first element.
count := 0
seq := SplitTrimSeq("a, b, c", ',')
for elem := range seq {
require.Equal(t, "a", elem)
count++
break
}
require.Equal(t, 1, count)
// A retained sequence starts over on every traversal, even after a break.
for range 2 {
var got []string
for elem := range seq {
got = append(got, elem)
}
require.Equal(t, []string{"a", "b", "c"}, got)
}
// Elements alias the input rather than copying it.
in := []byte(" gzip , br")
for elem := range SplitTrimSeq(in, ',') {
require.Same(t, &in[1], &elem[0])
break
}
}
// countSplitTrimSeq and countSplitSeq range outside the b.Loop body, which
// disables inlining for calls made directly inside it.
func countSplitTrimSeq(s string, sep byte) int {
n := 0
for range SplitTrimSeq(s, sep) {
n++
}
return n
}
func countSplitSeq(s, sep string) int {
n := 0
for part := range strings.SplitSeq(s, sep) {
if TrimSpace(part) != "" {
n++
}
}
return n
}
func Benchmark_SplitTrimSeq(b *testing.B) {
inputs := []struct {
name string
value string
}{
{"accept-encoding", "gzip, deflate, br"},
{"accept", "text/html, application/xhtml+xml, application/xml;q=0.9, image/avif, image/webp, */*;q=0.8"},
}
var count int
for _, input := range inputs {
b.Run(input.name+"/fiber", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
count += countSplitTrimSeq(input.value, ',')
}
})
b.Run(input.name+"/default", func(b *testing.B) {
b.ReportAllocs()
for b.Loop() {
count += countSplitSeq(input.value, ",")
}
})
}
_ = count
}
// Test_SplitTrimSeq_StdlibParity holds SplitTrimSeq to strings.SplitSeq with TrimSpace applied and empty elements dropped.
func Test_SplitTrimSeq_StdlibParity(t *testing.T) {
t.Parallel()
for _, sep := range []byte{',', ';', ' ', '\t', 'a', 0x00, 0xff} {
alphabet := string([]byte{sep, ' ', '\t', '\n', '"', 'a'})
forEachString(alphabet, 6, func(s string) {
var want []string
for part := range strings.SplitSeq(s, string([]byte{sep})) {
if part = strings.TrimSpace(part); part != "" {
want = append(want, part)
}
}
var got, gotBytes []string
for elem := range SplitTrimSeq(s, sep) {
got = append(got, elem)
}
for elem := range SplitTrimSeq([]byte(s), sep) {
gotBytes = append(gotBytes, string(elem))
}
if strings.Join(got, "\x00") != strings.Join(want, "\x00") || len(got) != len(want) {
t.Fatalf("SplitTrimSeq(%q, %#x) = %q, want %q", s, sep, got, want)
}
if strings.Join(gotBytes, "\x00") != strings.Join(want, "\x00") || len(gotBytes) != len(want) {
t.Fatalf("SplitTrimSeq(bytes %q, %#x) = %q, want %q", s, sep, gotBytes, want)
}
})
}
}