Debian Codegraph @

seq_test.go

raw
1 package utils
2
3 import (
4 "strings"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8 )
9
10 // refSplitTrim is the reference for SplitTrimSeq: split, trim, drop empties.
11 func refSplitTrim(s string, sep byte) []string {
12 var out []string
13 for part := range strings.SplitSeq(s, string(sep)) {
14 if part = TrimSpace(part); part != "" {
15 out = append(out, part)
16 }
17 }
18 return out
19 }
20
21 func splitTrimSamples() []string {
22 return []string{
23 "",
24 ",",
25 " , ,",
26 "\t,\r\n",
27 "a",
28 " a ",
29 "a,b",
30 "a, b ,c",
31 ",,a,,",
32 "a,",
33 ",a",
34 "gzip, deflate, br",
35 "text/html;q=0.9, */*;q=0.8",
36 "keep-alive,\tUpgrade",
37 " leading, trailing ",
38 "no separator at all",
39 }
40 }
41
42 func Test_SplitTrimSeq(t *testing.T) {
43 t.Parallel()
44 for _, in := range splitTrimSamples() {
45 for _, sep := range []byte{',', ';', ' '} {
46 want := refSplitTrim(in, sep)
47 var got []string
48 for elem := range SplitTrimSeq(in, sep) {
49 got = append(got, elem)
50 }
51 require.Equal(t, want, got, "SplitTrimSeq(%q, %q)", in, sep)
52
53 var gotBytes []string
54 for elem := range SplitTrimSeq([]byte(in), sep) {
55 gotBytes = append(gotBytes, string(elem))
56 }
57 require.Equal(t, want, gotBytes, "SplitTrimSeq(%q, %q) bytes", in, sep)
58 }
59 }
60
61 // Breaking out of the loop stops the iteration after the first element.
62 count := 0
63 seq := SplitTrimSeq("a, b, c", ',')
64 for elem := range seq {
65 require.Equal(t, "a", elem)
66 count++
67 break
68 }
69 require.Equal(t, 1, count)
70
71 // A retained sequence starts over on every traversal, even after a break.
72 for range 2 {
73 var got []string
74 for elem := range seq {
75 got = append(got, elem)
76 }
77 require.Equal(t, []string{"a", "b", "c"}, got)
78 }
79
80 // Elements alias the input rather than copying it.
81 in := []byte(" gzip , br")
82 for elem := range SplitTrimSeq(in, ',') {
83 require.Same(t, &in[1], &elem[0])
84 break
85 }
86 }
87
88 // countSplitTrimSeq and countSplitSeq range outside the b.Loop body, which
89 // disables inlining for calls made directly inside it.
90 func countSplitTrimSeq(s string, sep byte) int {
91 n := 0
92 for range SplitTrimSeq(s, sep) {
93 n++
94 }
95 return n
96 }
97
98 func countSplitSeq(s, sep string) int {
99 n := 0
100 for part := range strings.SplitSeq(s, sep) {
101 if TrimSpace(part) != "" {
102 n++
103 }
104 }
105 return n
106 }
107
108 func Benchmark_SplitTrimSeq(b *testing.B) {
109 inputs := []struct {
110 name string
111 value string
112 }{
113 {"accept-encoding", "gzip, deflate, br"},
114 {"accept", "text/html, application/xhtml+xml, application/xml;q=0.9, image/avif, image/webp, */*;q=0.8"},
115 }
116 var count int
117 for _, input := range inputs {
118 b.Run(input.name+"/fiber", func(b *testing.B) {
119 b.ReportAllocs()
120 for b.Loop() {
121 count += countSplitTrimSeq(input.value, ',')
122 }
123 })
124 b.Run(input.name+"/default", func(b *testing.B) {
125 b.ReportAllocs()
126 for b.Loop() {
127 count += countSplitSeq(input.value, ",")
128 }
129 })
130 }
131 _ = count
132 }