github.com/zhongdalu/gf@v1.0.0/g/encoding/gbinary/gbinary_z_be_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_BeEncodeAndBeDecode(t *testing.T) {
    17  	for k, v := range testData {
    18  		ve := gbinary.BeEncode(v)
    19  		ve1 := gbinary.BeEncodeByLength(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.BeDecodeToInt(ve), v)
    25  			gtest.Assert(gbinary.BeDecodeToInt(ve1), v)
    26  		case int8:
    27  			gtest.Assert(gbinary.BeDecodeToInt8(ve), v)
    28  			gtest.Assert(gbinary.BeDecodeToInt8(ve1), v)
    29  		case int16:
    30  			gtest.Assert(gbinary.BeDecodeToInt16(ve), v)
    31  			gtest.Assert(gbinary.BeDecodeToInt16(ve1), v)
    32  		case int32:
    33  			gtest.Assert(gbinary.BeDecodeToInt32(ve), v)
    34  			gtest.Assert(gbinary.BeDecodeToInt32(ve1), v)
    35  		case int64:
    36  			gtest.Assert(gbinary.BeDecodeToInt64(ve), v)
    37  			gtest.Assert(gbinary.BeDecodeToInt64(ve1), v)
    38  		case uint:
    39  			gtest.Assert(gbinary.BeDecodeToUint(ve), v)
    40  			gtest.Assert(gbinary.BeDecodeToUint(ve1), v)
    41  		case uint8:
    42  			gtest.Assert(gbinary.BeDecodeToUint8(ve), v)
    43  			gtest.Assert(gbinary.BeDecodeToUint8(ve1), v)
    44  		case uint16:
    45  			gtest.Assert(gbinary.BeDecodeToUint16(ve1), v)
    46  			gtest.Assert(gbinary.BeDecodeToUint16(ve), v)
    47  		case uint32:
    48  			gtest.Assert(gbinary.BeDecodeToUint32(ve1), v)
    49  			gtest.Assert(gbinary.BeDecodeToUint32(ve), v)
    50  		case uint64:
    51  			gtest.Assert(gbinary.BeDecodeToUint64(ve), v)
    52  			gtest.Assert(gbinary.BeDecodeToUint64(ve1), v)
    53  		case bool:
    54  			gtest.Assert(gbinary.BeDecodeToBool(ve), v)
    55  			gtest.Assert(gbinary.BeDecodeToBool(ve1), v)
    56  		case string:
    57  			gtest.Assert(gbinary.BeDecodeToString(ve), v)
    58  			gtest.Assert(gbinary.BeDecodeToString(ve1), v)
    59  		case float32:
    60  			gtest.Assert(gbinary.BeDecodeToFloat32(ve), v)
    61  			gtest.Assert(gbinary.BeDecodeToFloat32(ve1), v)
    62  		case float64:
    63  			gtest.Assert(gbinary.BeDecodeToFloat64(ve), v)
    64  			gtest.Assert(gbinary.BeDecodeToFloat64(ve1), v)
    65  		default:
    66  			if v == nil {
    67  				continue
    68  			}
    69  			res := make([]byte, len(ve))
    70  			err := gbinary.BeDecode(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_BeEncodeStruct(t *testing.T) {
    80  	user := User{"wenzi1", 999, "www.baidu.com"}
    81  	ve := gbinary.BeEncode(user)
    82  	s := gbinary.BeDecodeToString(ve)
    83  	gtest.Assert(string(s), s)
    84  }