github.com/kaiya/goutils@v1.0.1-0.20230226104005-4ae4a4dc3688/strings/string_test.go (about) 1 package strings 2 3 import ( 4 "bytes" 5 "testing" 6 ) 7 8 // 测试强转换功能 9 func TestBytes2String(t *testing.T) { 10 x := []byte("Hello Gopher!") 11 y := Bytes2String(x) 12 z := string(x) 13 14 if y != z { 15 t.Fail() 16 } 17 } 18 19 // 测试强转换功能 20 func TestString2Bytes(t *testing.T) { 21 x := "Hello Gopher!" 22 y := String2Bytes(x) 23 z := []byte(x) 24 25 if !bytes.Equal(y, z) { 26 t.Fail() 27 } 28 } 29 30 // 测试标准转换string()性能 31 func Benchmark_NormalBytes2String(b *testing.B) { 32 x := []byte("Hello Gopher! Hello Gopher! Hello Gopher!") 33 for i := 0; i < b.N; i++ { 34 _ = string(x) 35 } 36 } 37 38 // 测试强转换[]byte到string性能 39 func Benchmark_Byte2String(b *testing.B) { 40 x := []byte("Hello Gopher! Hello Gopher! Hello Gopher!") 41 for i := 0; i < b.N; i++ { 42 _ = Bytes2String(x) 43 } 44 } 45 46 // 测试标准转换[]byte性能 47 func Benchmark_NormalString2Bytes(b *testing.B) { 48 x := "Hello Gopher! Hello Gopher! Hello Gopher!" 49 for i := 0; i < b.N; i++ { 50 _ = []byte(x) 51 } 52 } 53 54 // 测试强转换string到[]byte性能 55 func Benchmark_String2Bytes(b *testing.B) { 56 x := "Hello Gopher! Hello Gopher! Hello Gopher!" 57 for i := 0; i < b.N; i++ { 58 _ = String2Bytes(x) 59 } 60 }