forked from gramework/gramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_test.go
88 lines (81 loc) · 1.82 KB
/
conv_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
package gramework
import (
"bytes"
"testing"
)
func TestBytesToString(t *testing.T) {
tests := []struct {
slice []byte
want string
}{
{
slice: []byte("Hello world"),
want: "Hello world",
},
{
slice: []byte("Hello world2304823049823049823409238402394823094823049428304"),
want: "Hello world2304823049823049823409238402394823094823049428304",
},
}
for _, tt := range tests {
if got := BytesToString(tt.slice); got != tt.want {
t.Errorf("BytesToString() = %v, want %v", got, tt.want)
}
}
}
func TestStringToBytes(t *testing.T) {
tests := []struct {
str string
want []byte
}{
{
str: "Hello world",
want: []byte("Hello world"),
},
{
str: "Hello world2304823049823049823409238402394823094823049428304",
want: []byte("Hello world2304823049823049823409238402394823094823049428304"),
},
}
for _, tt := range tests {
if got := StringToBytes(tt.str); bytes.Compare(got, tt.want) != 0 {
t.Errorf("BytesToString() = %v, want %v", got, tt.want)
}
}
}
func BenchmarkBytesToString(b *testing.B) {
s := []byte("Hello world2304823049823049823409238402394823094823049428304")
b.ResetTimer()
var a = ""
for i := 0; i < b.N; i++ {
a = BytesToString(s)
}
_ = a
}
func BenchmarkBytesToStringBuiltin(b *testing.B) {
s := []byte("Hello world2304823049823049823409238402394823094823049428304")
b.ResetTimer()
var a = ""
for i := 0; i < b.N; i++ {
a = string(s)
}
_ = a
}
func BenchmarkStringToBytes(b *testing.B) {
s := "Hello world2304823049823049823409238402394823094823049428304"
b.ResetTimer()
var a []byte
for i := 0; i < b.N; i++ {
a = StringToBytes(s)
}
_ = a
}
func BenchmarkStringToBytesBuiltin(b *testing.B) {
s := "Hello world2304823049823049823409238402394823094823049428304"
b.ResetTimer()
var a []byte
for i := 0; i < b.N; i++ {
a = []byte(s)
}
_ = a
}