github.com/zhongdalu/gf@v1.0.0/g/encoding/gbinary/gbinary_z_le_test.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 package gbinary_test 8 9 import ( 10 "testing" 11 12 "github.com/zhongdalu/gf/g/encoding/gbinary" 13 "github.com/zhongdalu/gf/g/test/gtest" 14 ) 15 16 func Test_LeEncodeAndLeDecode(t *testing.T) { 17 for k, v := range testData { 18 ve := gbinary.LeEncode(v) 19 ve1 := gbinary.LeEncodeByLength(len(ve), v) 20 21 //t.Logf("%s:%v, encoded:%v\n", k, v, ve) 22 switch v.(type) { 23 case int: 24 gtest.Assert(gbinary.LeDecodeToInt(ve), v) 25 gtest.Assert(gbinary.LeDecodeToInt(ve1), v) 26 case int8: 27 gtest.Assert(gbinary.LeDecodeToInt8(ve), v) 28 gtest.Assert(gbinary.LeDecodeToInt8(ve1), v) 29 case int16: 30 gtest.Assert(gbinary.LeDecodeToInt16(ve), v) 31 gtest.Assert(gbinary.LeDecodeToInt16(ve1), v) 32 case int32: 33 gtest.Assert(gbinary.LeDecodeToInt32(ve), v) 34 gtest.Assert(gbinary.LeDecodeToInt32(ve1), v) 35 case int64: 36 gtest.Assert(gbinary.LeDecodeToInt64(ve), v) 37 gtest.Assert(gbinary.LeDecodeToInt64(ve1), v) 38 case uint: 39 gtest.Assert(gbinary.LeDecodeToUint(ve), v) 40 gtest.Assert(gbinary.LeDecodeToUint(ve1), v) 41 case uint8: 42 gtest.Assert(gbinary.LeDecodeToUint8(ve), v) 43 gtest.Assert(gbinary.LeDecodeToUint8(ve1), v) 44 case uint16: 45 gtest.Assert(gbinary.LeDecodeToUint16(ve1), v) 46 gtest.Assert(gbinary.LeDecodeToUint16(ve), v) 47 case uint32: 48 gtest.Assert(gbinary.LeDecodeToUint32(ve1), v) 49 gtest.Assert(gbinary.LeDecodeToUint32(ve), v) 50 case uint64: 51 gtest.Assert(gbinary.LeDecodeToUint64(ve), v) 52 gtest.Assert(gbinary.LeDecodeToUint64(ve1), v) 53 case bool: 54 gtest.Assert(gbinary.LeDecodeToBool(ve), v) 55 gtest.Assert(gbinary.LeDecodeToBool(ve1), v) 56 case string: 57 gtest.Assert(gbinary.LeDecodeToString(ve), v) 58 gtest.Assert(gbinary.LeDecodeToString(ve1), v) 59 case float32: 60 gtest.Assert(gbinary.LeDecodeToFloat32(ve), v) 61 gtest.Assert(gbinary.LeDecodeToFloat32(ve1), v) 62 case float64: 63 gtest.Assert(gbinary.LeDecodeToFloat64(ve), v) 64 gtest.Assert(gbinary.LeDecodeToFloat64(ve1), v) 65 default: 66 if v == nil { 67 continue 68 } 69 res := make([]byte, len(ve)) 70 err := gbinary.LeDecode(ve, res) 71 if err != nil { 72 t.Errorf("test data: %s, %v, error:%v", k, v, err) 73 } 74 gtest.Assert(res, v) 75 } 76 } 77 } 78 79 func Test_LeEncodeStruct(t *testing.T) { 80 user := User{"wenzi1", 999, "www.baidu.com"} 81 ve := gbinary.LeEncode(user) 82 s := gbinary.LeDecodeToString(ve) 83 gtest.Assert(string(s), s) 84 }