@@ -39,12 +39,14 @@ func Benchmark_EqualFoldBytes(b *testing.B) {
39
39
right := []byte (lowerStr )
40
40
var res bool
41
41
b .Run ("fiber" , func (b * testing.B ) {
42
+ b .ReportAllocs ()
42
43
for n := 0 ; n < b .N ; n ++ {
43
44
res = EqualFold (left , right )
44
45
}
45
46
require .True (b , res )
46
47
})
47
48
b .Run ("default" , func (b * testing.B ) {
49
+ b .ReportAllocs ()
48
50
for n := 0 ; n < b .N ; n ++ {
49
51
res = bytes .EqualFold (left , right )
50
52
}
@@ -56,15 +58,227 @@ func Benchmark_EqualFoldBytes(b *testing.B) {
56
58
func Benchmark_EqualFold (b * testing.B ) {
57
59
var res bool
58
60
b .Run ("fiber" , func (b * testing.B ) {
61
+ b .ReportAllocs ()
59
62
for n := 0 ; n < b .N ; n ++ {
60
63
res = EqualFold (upperStr , lowerStr )
61
64
}
62
65
require .True (b , res )
63
66
})
64
67
b .Run ("default" , func (b * testing.B ) {
68
+ b .ReportAllocs ()
65
69
for n := 0 ; n < b .N ; n ++ {
66
70
res = strings .EqualFold (upperStr , lowerStr )
67
71
}
68
72
require .True (b , res )
69
73
})
70
74
}
75
+
76
+ func Test_TrimRight (t * testing.T ) {
77
+ t .Parallel ()
78
+ testCases := []struct {
79
+ S1 string
80
+ S2 string
81
+ Cutset byte
82
+ }{
83
+ {S1 : "/test//////" , S2 : "/test" , Cutset : '/' },
84
+ {S1 : "/test" , S2 : "/test" , Cutset : '/' },
85
+ {S1 : " " , S2 : "" , Cutset : ' ' },
86
+ {S1 : " " , S2 : "" , Cutset : ' ' },
87
+ {S1 : "" , S2 : "" , Cutset : ' ' },
88
+ }
89
+
90
+ for _ , tc := range testCases {
91
+ res := TrimRight (tc .S1 , tc .Cutset )
92
+ require .Equal (t , tc .S2 , res , "string" )
93
+
94
+ resB := TrimRight ([]byte (tc .S1 ), tc .Cutset )
95
+ require .Equal (t , []byte (tc .S2 ), resB , "bytes" )
96
+ }
97
+ }
98
+
99
+ func Benchmark_TrimRight (b * testing.B ) {
100
+ var res string
101
+ word := "foobar "
102
+ expected := "foobar"
103
+
104
+ b .Run ("fiber" , func (b * testing.B ) {
105
+ b .ReportAllocs ()
106
+ for n := 0 ; n < b .N ; n ++ {
107
+ res = TrimRight (word , ' ' )
108
+ }
109
+ require .Equal (b , expected , res )
110
+ })
111
+ b .Run ("default" , func (b * testing.B ) {
112
+ b .ReportAllocs ()
113
+ for n := 0 ; n < b .N ; n ++ {
114
+ res = strings .TrimRight (word , " " )
115
+ }
116
+ require .Equal (b , expected , res )
117
+ })
118
+ }
119
+
120
+ func Benchmark_TrimRightBytes (b * testing.B ) {
121
+ var res []byte
122
+ word := []byte ("foobar " )
123
+ expected := []byte ("foobar" )
124
+
125
+ b .Run ("fiber" , func (b * testing.B ) {
126
+ b .ReportAllocs ()
127
+ for n := 0 ; n < b .N ; n ++ {
128
+ res = TrimRight (word , ' ' )
129
+ }
130
+ require .Equal (b , expected , res )
131
+ })
132
+ b .Run ("default" , func (b * testing.B ) {
133
+ b .ReportAllocs ()
134
+ for n := 0 ; n < b .N ; n ++ {
135
+ res = bytes .TrimRight (word , " " )
136
+ }
137
+ require .Equal (b , expected , res )
138
+ })
139
+ }
140
+
141
+ func Test_TrimLeft (t * testing.T ) {
142
+ t .Parallel ()
143
+ testCases := []struct {
144
+ S1 string
145
+ S2 string
146
+ Cutset byte
147
+ }{
148
+ {S1 : "////test/" , S2 : "test/" , Cutset : '/' },
149
+ {S1 : "test/" , S2 : "test/" , Cutset : '/' },
150
+ {S1 : " " , S2 : "" , Cutset : ' ' },
151
+ {S1 : " " , S2 : "" , Cutset : ' ' },
152
+ {S1 : "" , S2 : "" , Cutset : ' ' },
153
+ }
154
+
155
+ for _ , tc := range testCases {
156
+ res := TrimLeft (tc .S1 , tc .Cutset )
157
+ require .Equal (t , tc .S2 , res , "string" )
158
+
159
+ resB := TrimLeft ([]byte (tc .S1 ), tc .Cutset )
160
+ require .Equal (t , []byte (tc .S2 ), resB , "bytes" )
161
+ }
162
+ }
163
+
164
+ func Benchmark_TrimLeft (b * testing.B ) {
165
+ var res string
166
+ word := " foobar"
167
+ expected := "foobar"
168
+
169
+ b .Run ("fiber" , func (b * testing.B ) {
170
+ b .ReportAllocs ()
171
+ for n := 0 ; n < b .N ; n ++ {
172
+ res = TrimLeft (word , ' ' )
173
+ }
174
+ require .Equal (b , expected , res )
175
+ })
176
+ b .Run ("default" , func (b * testing.B ) {
177
+ b .ReportAllocs ()
178
+ for n := 0 ; n < b .N ; n ++ {
179
+ res = strings .TrimLeft (word , " " )
180
+ }
181
+ require .Equal (b , expected , res )
182
+ })
183
+ }
184
+
185
+ func Benchmark_TrimLeftBytes (b * testing.B ) {
186
+ var res []byte
187
+ word := []byte (" foobar" )
188
+ expected := []byte ("foobar" )
189
+
190
+ b .Run ("fiber" , func (b * testing.B ) {
191
+ b .ReportAllocs ()
192
+ for n := 0 ; n < b .N ; n ++ {
193
+ res = TrimLeft (word , ' ' )
194
+ }
195
+ require .Equal (b , expected , res )
196
+ })
197
+ b .Run ("default" , func (b * testing.B ) {
198
+ b .ReportAllocs ()
199
+ for n := 0 ; n < b .N ; n ++ {
200
+ res = bytes .TrimLeft (word , " " )
201
+ }
202
+ require .Equal (b , expected , res )
203
+ })
204
+ }
205
+
206
+ func Test_Trim (t * testing.T ) {
207
+ t .Parallel ()
208
+ testCases := []struct {
209
+ S1 string
210
+ S2 string
211
+ Cutset byte
212
+ }{
213
+ {S1 : " test " , S2 : "test" , Cutset : ' ' },
214
+ {S1 : "test" , S2 : "test" , Cutset : ' ' },
215
+ {S1 : ".test" , S2 : "test" , Cutset : '.' },
216
+ {S1 : " " , S2 : "" , Cutset : ' ' },
217
+ {S1 : " " , S2 : "" , Cutset : ' ' },
218
+ {S1 : "" , S2 : "" , Cutset : ' ' },
219
+ }
220
+
221
+ for _ , tc := range testCases {
222
+ res := Trim (tc .S1 , tc .Cutset )
223
+ require .Equal (t , tc .S2 , res , "string" )
224
+
225
+ resB := Trim ([]byte (tc .S1 ), tc .Cutset )
226
+ require .Equal (t , []byte (tc .S2 ), resB , "bytes" )
227
+ }
228
+ }
229
+
230
+ func Benchmark_Trim (b * testing.B ) {
231
+ var res string
232
+ word := " foobar "
233
+ expected := "foobar"
234
+
235
+ b .Run ("fiber" , func (b * testing.B ) {
236
+ b .ReportAllocs ()
237
+ for n := 0 ; n < b .N ; n ++ {
238
+ res = Trim (word , ' ' )
239
+ }
240
+ require .Equal (b , expected , res )
241
+ })
242
+ b .Run ("default" , func (b * testing.B ) {
243
+ b .ReportAllocs ()
244
+ for n := 0 ; n < b .N ; n ++ {
245
+ res = strings .Trim (word , " " )
246
+ }
247
+ require .Equal (b , expected , res )
248
+ })
249
+ b .Run ("default.trimspace" , func (b * testing.B ) {
250
+ b .ReportAllocs ()
251
+ for n := 0 ; n < b .N ; n ++ {
252
+ res = strings .TrimSpace (word )
253
+ }
254
+ require .Equal (b , expected , res )
255
+ })
256
+ }
257
+
258
+ func Benchmark_TrimBytes (b * testing.B ) {
259
+ var res []byte
260
+ word := []byte (" foobar " )
261
+ expected := []byte ("foobar" )
262
+
263
+ b .Run ("fiber" , func (b * testing.B ) {
264
+ b .ReportAllocs ()
265
+ for n := 0 ; n < b .N ; n ++ {
266
+ res = Trim (word , ' ' )
267
+ }
268
+ require .Equal (b , expected , res )
269
+ })
270
+ b .Run ("default" , func (b * testing.B ) {
271
+ b .ReportAllocs ()
272
+ for n := 0 ; n < b .N ; n ++ {
273
+ res = bytes .Trim (word , " " )
274
+ }
275
+ require .Equal (b , expected , res )
276
+ })
277
+ b .Run ("default.trimspace" , func (b * testing.B ) {
278
+ b .ReportAllocs ()
279
+ for n := 0 ; n < b .N ; n ++ {
280
+ res = bytes .TrimSpace (word )
281
+ }
282
+ require .Equal (b , expected , res )
283
+ })
284
+ }
0 commit comments