-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbytes_test.go
335 lines (304 loc) · 14 KB
/
bytes_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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package utils
import (
"bytes"
"strings"
"testing"
"github.com/fufuok/utils/assert"
)
func TestGetBytes(t *testing.T) {
for _, v := range []struct {
in interface{}
def []byte
out []byte
}{
{"Fufu\n 中 文", nil, []byte("Fufu\n 中 文")},
{nil, nil, nil},
{nil, []byte("NULL"), []byte("NULL")},
{123, nil, []byte("123")},
{123, []byte("456"), []byte("123")},
{123.45, nil, []byte("123.45")},
{true, nil, []byte("true")},
{false, nil, []byte("false")},
{testString, nil, testBytes},
{[]int{0, 2, 1}, nil, []byte("[0 2 1]")},
} {
assert.Equal(t, v.out, GetBytes(v.in, v.def))
}
assert.Equal(t, 0, len(GetBytes(nil)))
assert.Equal(t, []byte{}, GetSafeBytes(nil))
}
func TestGetSafeBytes(t *testing.T) {
t.Parallel()
b := []byte("Fufu")
s := B2S(b)
safeB1 := []byte(s)
safeB2 := GetSafeS2B(s)
safeB3 := GetSafeBytes(b)
safeB4 := CopyBytes(b)
assert.Equal(t, "Fufu", s)
b[0] = 'X'
assert.Equal(t, "Xufu", s)
assert.Equal(t, []byte("Fufu"), safeB1)
assert.Equal(t, []byte("Fufu"), safeB2)
assert.Equal(t, []byte("Fufu"), safeB3)
assert.Equal(t, []byte("Fufu"), safeB4)
assert.Equal(t, []byte("default"), GetSafeS2B("", []byte("default")))
}
func TestCopyBytes(t *testing.T) {
t.Parallel()
assert.Equal(t, testBytes, CopyBytes(testBytes))
}
func TestCopyS2B(t *testing.T) {
t.Parallel()
assert.Equal(t, testBytes, CopyS2B(testString))
}
func TestJoinBytes(t *testing.T) {
t.Parallel()
assert.Equal(t, []byte("1,2,3"), JoinBytes([]byte("1"), []byte(","), []byte("2"), []byte(","), []byte("3")))
assert.Equal(
t,
bytes.Join([][]byte{[]byte("1"), []byte("2"), []byte("3")}, []byte(",")),
JoinBytes([]byte("1"), []byte(","), []byte("2"), []byte(","), []byte("3")),
)
}
func BenchmarkCopyS2B(b *testing.B) {
s := strings.Repeat(testString, 10)
b.ReportAllocs()
b.ResetTimer()
b.Run("utils", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = CopyS2B(s)
}
})
b.Run("default", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = []byte(s)
}
})
b.Run("append", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = append([]byte{}, s...)
}
})
}
// 仅补全函数, 实际直接使用 []byte(string)
// go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkCopyS2B
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
// BenchmarkCopyS2B/utils-8 10197265 109.8 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/utils-8 12149991 107.5 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/utils-8 12256554 110.6 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/default-8 10587153 116.1 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/default-8 10294596 113.3 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/default-8 9938052 123.7 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/append-8 9818898 121.1 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/append-8 10450903 119.1 ns/op 320 B/op 1 allocs/op
// BenchmarkCopyS2B/append-8 9462102 117.1 ns/op 320 B/op 1 allocs/op
func TestToLowerBytes(t *testing.T) {
t.Parallel()
assert.Equal(t, bytes.ToLower([]byte("A")), ToLowerBytes([]byte("A")))
assert.Equal(t, bytes.ToLower([]byte("/TesT/")), ToLowerBytes([]byte("/TesT/")))
assert.Equal(t, true, bytes.Equal(bytes.ToLower([]byte("/TesT/")), ToLowerBytes([]byte("/TesT/"))))
assert.Equal(t, false, bytes.Equal(bytes.ToUpper([]byte("/TesT/")), ToLowerBytes([]byte("/TesT/"))))
}
func BenchmarkToLowerBytes(b *testing.B) {
b.Run("utils", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = ToLowerBytes([]byte("/TesT/"))
}
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = bytes.ToLower([]byte("/TesT/"))
}
})
}
// go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkToLowerBytes
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
// BenchmarkToLowerBytes/utils-8 169237525 7.012 ns/op 0 B/op 0 allocs/op
// BenchmarkToLowerBytes/utils-8 164508339 7.176 ns/op 0 B/op 0 allocs/op
// BenchmarkToLowerBytes/utils-8 163584484 8.336 ns/op 0 B/op 0 allocs/op
// BenchmarkToLowerBytes/default-8 21546925 56.56 ns/op 8 B/op 1 allocs/op
// BenchmarkToLowerBytes/default-8 25344794 42.91 ns/op 8 B/op 1 allocs/op
// BenchmarkToLowerBytes/default-8 27910351 45.92 ns/op 8 B/op 1 allocs/op
func TestToUpperBytes(t *testing.T) {
t.Parallel()
assert.Equal(t, bytes.ToUpper([]byte("a")), ToUpperBytes([]byte("a")))
assert.Equal(t, bytes.ToUpper([]byte("/TesT/")), ToUpperBytes([]byte("/TesT/")))
assert.Equal(t, true, bytes.Equal(bytes.ToUpper([]byte("/TesT/")), ToUpperBytes([]byte("/TesT/"))))
assert.Equal(t, false, bytes.Equal(bytes.ToLower([]byte("/TesT/")), ToUpperBytes([]byte("/TesT/"))))
}
func BenchmarkToUpperBytes(b *testing.B) {
b.Run("utils", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = ToUpperBytes([]byte("/TesT/"))
}
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = bytes.ToUpper([]byte("/TesT/"))
}
})
}
// go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkToUpperBytes
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
// BenchmarkToUpperBytes/utils-8 170378092 7.061 ns/op 0 B/op 0 allocs/op
// BenchmarkToUpperBytes/utils-8 170443166 7.380 ns/op 0 B/op 0 allocs/op
// BenchmarkToUpperBytes/utils-8 172179099 7.373 ns/op 0 B/op 0 allocs/op
// BenchmarkToUpperBytes/default-8 24008786 46.90 ns/op 8 B/op 1 allocs/op
// BenchmarkToUpperBytes/default-8 29689668 44.83 ns/op 8 B/op 1 allocs/op
// BenchmarkToUpperBytes/default-8 29171811 42.58 ns/op 8 B/op 1 allocs/op
func TestTrimLeftBytes(t *testing.T) {
t.Parallel()
assert.Equal(t, []byte("test/////"), TrimLeftBytes([]byte("/////test/////"), '/'))
assert.Equal(t, []byte("test/"), TrimLeftBytes([]byte("/test/"), '/'))
assert.Equal(t, 0, len(TrimLeftBytes([]byte(" "), ' ')))
assert.Equal(t, 0, len(TrimLeftBytes([]byte(" "), ' ')))
assert.Equal(t, 0, len(TrimLeftBytes([]byte(""), ' ')))
assert.Equal(t, bytes.TrimLeft([]byte(" TesT "), " "), TrimLeftBytes([]byte(" TesT "), ' '))
}
func BenchmarkTrimLeftBytes(b *testing.B) {
b.Run("utils", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = TrimLeftBytes([]byte(" TesT "), ' ')
}
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = bytes.TrimLeft([]byte(" TesT "), " ")
}
})
}
// go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkTrimLeftBytes
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
// BenchmarkTrimLeftBytes/utils-8 347581670 3.051 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimLeftBytes/utils-8 362493957 3.227 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimLeftBytes/utils-8 385871320 3.401 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimLeftBytes/default-8 15500971 71.58 ns/op 24 B/op 1 allocs/op
// BenchmarkTrimLeftBytes/default-8 15734938 73.00 ns/op 24 B/op 1 allocs/op
// BenchmarkTrimLeftBytes/default-8 19290650 65.52 ns/op 24 B/op 1 allocs/op
func TestTrimRightBytes(t *testing.T) {
t.Parallel()
assert.Equal(t, []byte("/////test"), TrimRightBytes([]byte("/////test/////"), '/'))
assert.Equal(t, []byte("/test"), TrimRightBytes([]byte("/test/"), '/'))
assert.Equal(t, 0, len(TrimRightBytes([]byte(" "), ' ')))
assert.Equal(t, 0, len(TrimRightBytes([]byte(" "), ' ')))
assert.Equal(t, 0, len(TrimRightBytes([]byte(""), ' ')))
assert.Equal(t, bytes.TrimRight([]byte(" TesT "), " "), TrimRightBytes([]byte(" TesT "), ' '))
}
func BenchmarkTrimRightBytes(b *testing.B) {
b.Run("utils", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = TrimRightBytes([]byte(" TesT "), ' ')
}
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = bytes.TrimRight([]byte(" TesT "), " ")
}
})
}
// go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkTrimRightBytes
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
// BenchmarkTrimRightBytes/utils-8 381420740 3.423 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimRightBytes/utils-8 394509609 3.076 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimRightBytes/utils-8 396171007 3.118 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimRightBytes/default-8 17798528 66.47 ns/op 24 B/op 1 allocs/op
// BenchmarkTrimRightBytes/default-8 18517689 64.92 ns/op 24 B/op 1 allocs/op
// BenchmarkTrimRightBytes/default-8 18744405 65.73 ns/op 24 B/op 1 allocs/op
func TestTrimBytes(t *testing.T) {
assert.Equal(t, []byte("test"), TrimBytes([]byte("/////test/////"), '/'))
assert.Equal(t, []byte("test"), TrimBytes([]byte("/test/"), '/'))
assert.Equal(t, []byte("test"), TrimBytes([]byte("test"), '/'))
assert.Equal(t, 0, len(TrimBytes([]byte(" "), ' ')))
assert.Equal(t, 0, len(TrimBytes([]byte(" "), ' ')))
assert.Equal(t, 0, len(TrimBytes([]byte(""), ' ')))
assert.Equal(t, bytes.Trim([]byte(" TesT "), " "), TrimBytes([]byte(" TesT "), ' '))
}
func BenchmarkTrimBytes(b *testing.B) {
b.Run("utils", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = TrimBytes([]byte(" TesT "), ' ')
}
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = bytes.Trim([]byte(" TesT "), " ")
}
})
b.Run("trimspace", func(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = bytes.TrimSpace([]byte(" TesT "))
}
})
}
// go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkTrimBytes
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
// BenchmarkTrimBytes/utils-8 142327920 8.657 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimBytes/utils-8 150808749 8.059 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimBytes/utils-8 144292578 8.129 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimBytes/default-8 14513946 82.10 ns/op 24 B/op 1 allocs/op
// BenchmarkTrimBytes/default-8 14985987 79.10 ns/op 24 B/op 1 allocs/op
// BenchmarkTrimBytes/default-8 15338756 78.82 ns/op 24 B/op 1 allocs/op
// BenchmarkTrimBytes/trimspace-8 100000000 11.56 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimBytes/trimspace-8 100000000 11.92 ns/op 0 B/op 0 allocs/op
// BenchmarkTrimBytes/trimspace-8 100000000 10.94 ns/op 0 B/op 0 allocs/op
func TestEqualFoldBytes(t *testing.T) {
res := []byte(" tESt ")
assert.Equal(t, true, EqualFoldBytes(res, ToUpperBytes([]byte(" TesT "))))
assert.Equal(t, true, EqualFoldBytes(ToLowerBytes(res), ToUpperBytes([]byte(" TesT "))))
assert.Equal(t, false, EqualFoldBytes(res, TrimBytes([]byte(" TesT "), ' ')))
assert.Equal(t, false, EqualFoldBytes([]byte("\na"), []byte("*A")))
}
func BenchmarkEqualFoldBytes(b *testing.B) {
s := ToUpperBytes(CopyBytes(testBytes))
t := ToLowerBytes(CopyBytes(testBytes))
ok := false
b.Run("utils", func(b *testing.B) {
for n := 0; n < b.N; n++ {
ok = EqualFoldBytes(s, t)
assert.Equal(b, true, ok)
}
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
ok = bytes.EqualFold(s, t)
assert.Equal(b, true, ok)
}
})
}
// go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=BenchmarkEqualFoldBytes
// goos: linux
// goarch: amd64
// pkg: github.com/fufuok/utils
// cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz
// BenchmarkEqualFoldBytes/utils-8 11882070 95.38 ns/op 0 B/op 0 allocs/op
// BenchmarkEqualFoldBytes/utils-8 12616200 97.33 ns/op 0 B/op 0 allocs/op
// BenchmarkEqualFoldBytes/utils-8 12393940 99.75 ns/op 0 B/op 0 allocs/op
// BenchmarkEqualFoldBytes/default-8 5822217 209.0 ns/op 0 B/op 0 allocs/op
// BenchmarkEqualFoldBytes/default-8 6151662 197.9 ns/op 0 B/op 0 allocs/op
// BenchmarkEqualFoldBytes/default-8 5932452 199.1 ns/op 0 B/op 0 allocs/op
func TestCutBytes(t *testing.T) {
for _, tt := range cutTests {
if before, after, found := CutBytes([]byte(tt.s), []byte(tt.sep)); string(before) != tt.before || string(after) != tt.after || found != tt.found {
t.Errorf("Cut(%q, %q) = %q, %q, %v, want %q, %q, %v", tt.s, tt.sep, before, after, found, tt.before, tt.after, tt.found)
}
}
}