-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbytes_test.go
More file actions
91 lines (78 loc) · 1.77 KB
/
bytes_test.go
File metadata and controls
91 lines (78 loc) · 1.77 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
package stringx
import (
"strings"
"testing"
)
func TestFromBytes(t *testing.T) {
tests := []struct {
name string
arg []byte
want string
}{
{"convert nil byte slice", nil, ""},
{"convert empty byte slice", []byte{}, ""},
{"convert byte slice to string", []byte("hello"), "hello"},
{"convert unicode byte slice", []byte("你好世界"), "你好世界"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FromBytes(tt.arg); got != tt.want {
t.Errorf("FromBytes() = %v, want %v", got, tt.want)
}
})
}
}
var fromBytesResult string
func BenchmarkFromBytes(b *testing.B) {
// 10MB
data := make([]byte, 1024*1024*10)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
fromBytesResult = FromBytes(data)
}
}
func BenchmarkBytesToString(b *testing.B) {
// 10MB
data := make([]byte, 1024*1024*10)
b.ReportAllocs()
for i := 0; i < b.N; i++ {
fromBytesResult = string(data)
}
}
func TestToBytes(t *testing.T) {
tests := []struct {
name string
arg string
want []byte
}{
{"convert empty string", "", []byte{}},
{"convert string to byte slice", "hello", []byte("hello")},
{"convert unicode string", "你好世界", []byte("你好世界")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ToBytes(tt.arg); string(got) != string(tt.want) {
t.Errorf("ToBytes() = %v, want %v", got, tt.want)
}
})
}
}
var toBytesResult []byte
func BenchmarkToBytes(b *testing.B) {
// 10MB
s := strings.Repeat("a", 1024*1024*10)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
toBytesResult = ToBytes(s)
}
}
func BenchmarkStringToBytes(b *testing.B) {
// 10MB
s := strings.Repeat("a", 1024*1024*10)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
toBytesResult = []byte(s)
}
}