github.com/wangyougui/gf/v2@v2.6.5/encoding/gbinary/gbinary_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gbinary_test 8 9 import ( 10 "math" 11 "testing" 12 13 "github.com/wangyougui/gf/v2/encoding/gbinary" 14 "github.com/wangyougui/gf/v2/test/gtest" 15 ) 16 17 type User struct { 18 Name string 19 Age int 20 Url string 21 } 22 23 var testData = map[string]interface{}{ 24 //"nil": nil, 25 "int": int(123), 26 "int8": int8(-99), 27 "int8.max": math.MaxInt8, 28 "int16": int16(123), 29 "int16.max": math.MaxInt16, 30 "int32": int32(-199), 31 "int32.max": math.MaxInt32, 32 "int64": int64(123), 33 "uint": uint(123), 34 "uint8": uint8(123), 35 "uint8.max": math.MaxUint8, 36 "uint16": uint16(9999), 37 "uint16.max": math.MaxUint16, 38 "uint32": uint32(123), 39 "uint64": uint64(123), 40 "bool.true": true, 41 "bool.false": false, 42 "string": "hehe haha", 43 "byte": []byte("hehe haha"), 44 "float32": float32(123.456), 45 "float32.max": math.MaxFloat32, 46 "float64": float64(123.456), 47 } 48 49 var testBitData = []int{0, 99, 122, 129, 222, 999, 22322} 50 51 func Test_EncodeAndDecode(t *testing.T) { 52 gtest.C(t, func(t *gtest.T) { 53 for k, v := range testData { 54 ve := gbinary.Encode(v) 55 ve1 := gbinary.EncodeByLength(len(ve), v) 56 57 //t.Logf("%s:%v, encoded:%v\n", k, v, ve) 58 switch v.(type) { 59 case int: 60 t.Assert(gbinary.DecodeToInt(ve), v) 61 t.Assert(gbinary.DecodeToInt(ve1), v) 62 case int8: 63 t.Assert(gbinary.DecodeToInt8(ve), v) 64 t.Assert(gbinary.DecodeToInt8(ve1), v) 65 case int16: 66 t.Assert(gbinary.DecodeToInt16(ve), v) 67 t.Assert(gbinary.DecodeToInt16(ve1), v) 68 case int32: 69 t.Assert(gbinary.DecodeToInt32(ve), v) 70 t.Assert(gbinary.DecodeToInt32(ve1), v) 71 case int64: 72 t.Assert(gbinary.DecodeToInt64(ve), v) 73 t.Assert(gbinary.DecodeToInt64(ve1), v) 74 case uint: 75 t.Assert(gbinary.DecodeToUint(ve), v) 76 t.Assert(gbinary.DecodeToUint(ve1), v) 77 case uint8: 78 t.Assert(gbinary.DecodeToUint8(ve), v) 79 t.Assert(gbinary.DecodeToUint8(ve1), v) 80 case uint16: 81 t.Assert(gbinary.DecodeToUint16(ve1), v) 82 t.Assert(gbinary.DecodeToUint16(ve), v) 83 case uint32: 84 t.Assert(gbinary.DecodeToUint32(ve1), v) 85 t.Assert(gbinary.DecodeToUint32(ve), v) 86 case uint64: 87 t.Assert(gbinary.DecodeToUint64(ve), v) 88 t.Assert(gbinary.DecodeToUint64(ve1), v) 89 case bool: 90 t.Assert(gbinary.DecodeToBool(ve), v) 91 t.Assert(gbinary.DecodeToBool(ve1), v) 92 case string: 93 t.Assert(gbinary.DecodeToString(ve), v) 94 t.Assert(gbinary.DecodeToString(ve1), v) 95 case float32: 96 t.Assert(gbinary.DecodeToFloat32(ve), v) 97 t.Assert(gbinary.DecodeToFloat32(ve1), v) 98 case float64: 99 t.Assert(gbinary.DecodeToFloat64(ve), v) 100 t.Assert(gbinary.DecodeToFloat64(ve1), v) 101 default: 102 if v == nil { 103 continue 104 } 105 res := make([]byte, len(ve)) 106 err := gbinary.Decode(ve, res) 107 if err != nil { 108 t.Errorf("test data: %s, %v, error:%v", k, v, err) 109 } 110 t.Assert(res, v) 111 } 112 } 113 }) 114 } 115 116 func Test_EncodeStruct(t *testing.T) { 117 gtest.C(t, func(t *gtest.T) { 118 user := User{"wenzi1", 999, "www.baidu.com"} 119 ve := gbinary.Encode(user) 120 s := gbinary.DecodeToString(ve) 121 t.Assert(s, s) 122 }) 123 } 124 125 func Test_Bits(t *testing.T) { 126 gtest.C(t, func(t *gtest.T) { 127 for i := range testBitData { 128 bits := make([]gbinary.Bit, 0) 129 res := gbinary.EncodeBits(bits, testBitData[i], 64) 130 131 t.Assert(gbinary.DecodeBits(res), testBitData[i]) 132 t.Assert(gbinary.DecodeBitsToUint(res), uint(testBitData[i])) 133 134 t.Assert(gbinary.DecodeBytesToBits(gbinary.EncodeBitsToBytes(res)), res) 135 } 136 }) 137 }