github.com/TeaOSLab/EdgeNode@v1.3.8/internal/utils/string_test.go (about) 1 package utils_test 2 3 import ( 4 "github.com/TeaOSLab/EdgeNode/internal/utils" 5 "github.com/iwind/TeaGo/assert" 6 "github.com/iwind/TeaGo/types" 7 "strings" 8 "testing" 9 ) 10 11 func TestBytesToString(t *testing.T) { 12 t.Log(utils.UnsafeBytesToString([]byte("Hello,World"))) 13 } 14 15 func TestStringToBytes(t *testing.T) { 16 t.Log(string(utils.UnsafeStringToBytes("Hello,World"))) 17 } 18 19 func BenchmarkBytesToString(b *testing.B) { 20 var data = []byte("Hello,World") 21 for i := 0; i < b.N; i++ { 22 _ = utils.UnsafeBytesToString(data) 23 } 24 } 25 26 func BenchmarkBytesToString2(b *testing.B) { 27 var data = []byte("Hello,World") 28 for i := 0; i < b.N; i++ { 29 _ = string(data) 30 } 31 } 32 33 func BenchmarkStringToBytes(b *testing.B) { 34 var s = strings.Repeat("Hello,World", 1024) 35 for i := 0; i < b.N; i++ { 36 _ = utils.UnsafeStringToBytes(s) 37 } 38 } 39 40 func BenchmarkStringToBytes2(b *testing.B) { 41 var s = strings.Repeat("Hello,World", 1024) 42 for i := 0; i < b.N; i++ { 43 _ = []byte(s) 44 } 45 } 46 47 func TestFormatAddress(t *testing.T) { 48 t.Log(utils.FormatAddress("127.0.0.1:1234")) 49 t.Log(utils.FormatAddress("127.0.0.1 : 1234")) 50 t.Log(utils.FormatAddress("127.0.0.1:1234")) 51 } 52 53 func TestFormatAddressList(t *testing.T) { 54 t.Log(utils.FormatAddressList([]string{ 55 "127.0.0.1:1234", 56 "127.0.0.1 : 1234", 57 "127.0.0.1:1234", 58 })) 59 } 60 61 func TestContainsSameStrings(t *testing.T) { 62 var a = assert.NewAssertion(t) 63 a.IsFalse(utils.EqualStrings([]string{"a"}, []string{"b"})) 64 a.IsFalse(utils.EqualStrings([]string{"a", "b"}, []string{"b"})) 65 a.IsFalse(utils.EqualStrings([]string{"a", "b"}, []string{"a", "b", "c"})) 66 a.IsTrue(utils.EqualStrings([]string{"a", "b"}, []string{"a", "b"})) 67 a.IsTrue(utils.EqualStrings([]string{"a", "b"}, []string{"b", "a"})) 68 } 69 70 func TestToValidUTF8string(t *testing.T) { 71 for _, s := range []string{ 72 "https://goedge.cn/", 73 "提升mysql数据表写入速度", 74 "😆", 75 string([]byte{'a', 'b', 130, 131, 132, 133, 134, 'c'}), 76 } { 77 var u = utils.ToValidUTF8string(s) 78 t.Log(s, "["+types.String(len(s))+"]", "=>", u, "["+types.String(len(u))+"]") 79 } 80 }