github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/convert_test.go (about) 1 package util 2 3 import ( 4 "strings" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 var ( 12 testString = " test 中 文\u2728->?\n*\U0001F63A " 13 testBytes = []byte(testString) 14 ) 15 16 func TestS2B(t *testing.T) { 17 t.Parallel() 18 for i := 0; i < 100; i++ { 19 s := RandString(64) 20 expected := []byte(s) 21 actual := S2B(s) 22 assert.Equal(t, expected, actual) 23 assert.Equal(t, len(expected), len(actual)) 24 } 25 26 expected := testString 27 actual := S2B(expected) 28 assert.Equal(t, []byte(expected), actual) 29 30 assert.Equal(t, true, S2B("") == nil) 31 assert.Equal(t, testBytes, S2B(testString)) 32 } 33 34 func TestB2S(t *testing.T) { 35 t.Parallel() 36 for i := 0; i < 100; i++ { 37 b, err := RandBytes(64) 38 assert.Nil(t, err) 39 assert.Equal(t, string(b), B2S(b)) 40 } 41 42 expected := testString 43 actual := B2S([]byte(expected)) 44 assert.Equal(t, expected, actual) 45 46 assert.Equal(t, true, B2S(nil) == "") 47 assert.Equal(t, testString, B2S(testBytes)) 48 } 49 50 func TestMustString(t *testing.T) { 51 now := time.Date(2022, 1, 2, 3, 4, 5, 0, time.UTC) 52 for _, v := range []struct { 53 in any 54 out string 55 }{ 56 {"Is string?", "Is string?"}, 57 {0, "0"}, 58 {0.005, "0.005"}, 59 {nil, ""}, 60 {true, "true"}, 61 {false, "false"}, 62 {[]byte(testString), testString}, 63 {[]int{0, 2, 1}, "[0 2 1]"}, 64 {map[string]any{"a": 0, "b": true, "C": []byte("c")}, "map[C:[99] a:0 b:true]"}, 65 {now, "2022-01-02 03:04:05"}, 66 } { 67 assert.Equal(t, v.out, MustString(v.in)) 68 } 69 assert.Equal(t, "2022-01-02T03:04:05Z", MustString(now, time.RFC3339)) 70 } 71 72 func TestMustInt(t *testing.T) { 73 for _, v := range []struct { 74 in any 75 out int 76 }{ 77 {"2", 2}, 78 {" 2 \n ", 2}, 79 {0b0010, 2}, 80 {10, 10}, 81 {0o77, 63}, 82 {0xff, 255}, 83 {-1, -1}, 84 {true, 1}, 85 {"0x", 0}, 86 {false, 0}, 87 {uint(11), 11}, 88 {uint64(11), 11}, 89 {int64(11), 11}, 90 {float32(11.0), 11}, 91 {1.005, 1}, 92 {nil, 0}, 93 } { 94 assert.Equal(t, v.out, MustInt(v.in)) 95 } 96 } 97 98 func TestB64Encode(t *testing.T) { 99 t.Parallel() 100 assert.Equal(t, "6Kej56CBL+e8lueggX4g6aG25pu/JiM=", B64Encode(S2B("解码/编码~ 顶替&#"))) 101 } 102 103 func TestB64UrlEncode(t *testing.T) { 104 t.Parallel() 105 assert.Equal(t, "6Kej56CBL-e8lueggX4g6aG25pu_JiM=", B64UrlEncode(S2B("解码/编码~ 顶替&#"))) 106 } 107 108 func TestB64Decode(t *testing.T) { 109 t.Parallel() 110 assert.Equal(t, []byte("解码/编码~ 顶替&#"), B64Decode("6Kej56CBL+e8lueggX4g6aG25pu/JiM=")) 111 } 112 113 func TestB64UrlDecode(t *testing.T) { 114 for _, v := range []struct { 115 in string 116 out []byte 117 }{ 118 {"6Kej56CBL-e8lueggX4g6aG25pu_JiM=", []byte("解码/编码~ 顶替&#")}, 119 {"123", nil}, 120 } { 121 assert.Equal(t, v.out, B64UrlDecode(v.in)) 122 } 123 } 124 125 func BenchmarkS2B(b *testing.B) { 126 s := strings.Repeat(testString, 10000) 127 b.ReportAllocs() 128 b.ResetTimer() 129 for i := 0; i < b.N; i++ { 130 _ = S2B(s) 131 } 132 } 133 134 func BenchmarkS2BStdStringToBytes(b *testing.B) { 135 s := strings.Repeat(testString, 10000) 136 b.ReportAllocs() 137 b.ResetTimer() 138 for i := 0; i < b.N; i++ { 139 _ = []byte(s) 140 } 141 }