/
format_test.go
56 lines (49 loc) · 1.12 KB
/
format_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
package tool
import (
"bytes"
"testing"
)
func TestStringToBytes(t *testing.T) {
x := "Hello Gopher!"
y := StringToBytes(x)
z := []byte(x)
if !bytes.Equal(y, z) {
t.Fail()
}
// unexpected fault address 0x55b15c
// fatal error: fault
// [signal SIGSEGV: segmentation violation code=0x2 addr=0x55b15c pc=0x507cf8]
// y[0] = 'A'
}
func TestBytesToString(t *testing.T) {
x := []byte("Hello Gopher")
y := BytesToString(x)
z := string(x)
if y != z {
t.Fail()
}
}
func Benchmark_NormalBytesToString(b *testing.B) {
x := []byte("Hello Gopher!Hello Gopher!Hello Gopher!Hello Gopher!")
for i := 0; i < b.N; i++ {
_ = string(x)
}
}
func Benchmark_NormalStringToBytes(b *testing.B) {
x := "Hello Gopher!Hello Gopher!Hello Gopher!Hello Gopher!"
for i := 0; i < b.N; i++ {
_ = []byte(x)
}
}
func Benchmark_BytesToString(b *testing.B) {
x := []byte("Hello Gopher!Hello Gopher!Hello Gopher!Hello Gopher!")
for i := 0; i < b.N; i++ {
_ = BytesToString(x)
}
}
func Benchmark_StringToBytes(b *testing.B) {
x := "Hello Gopher!Hello Gopher!Hello Gopher!Hello Gopher!"
for i := 0; i < b.N; i++ {
_ = StringToBytes(x)
}
}