github.com/wangyougui/gf/v2@v2.6.5/encoding/gbinary/gbinary_z_unit_be_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  	"testing"
    11  
    12  	"github.com/wangyougui/gf/v2/encoding/gbinary"
    13  	"github.com/wangyougui/gf/v2/test/gtest"
    14  )
    15  
    16  func Test_BeEncodeAndBeDecode(t *testing.T) {
    17  	gtest.C(t, func(t *gtest.T) {
    18  		for k, v := range testData {
    19  			ve := gbinary.BeEncode(v)
    20  			ve1 := gbinary.BeEncodeByLength(len(ve), v)
    21  
    22  			//t.Logf("%s:%v, encoded:%v\n", k, v, ve)
    23  			switch v.(type) {
    24  			case int:
    25  				t.Assert(gbinary.BeDecodeToInt(ve), v)
    26  				t.Assert(gbinary.BeDecodeToInt(ve1), v)
    27  			case int8:
    28  				t.Assert(gbinary.BeDecodeToInt8(ve), v)
    29  				t.Assert(gbinary.BeDecodeToInt8(ve1), v)
    30  			case int16:
    31  				t.Assert(gbinary.BeDecodeToInt16(ve), v)
    32  				t.Assert(gbinary.BeDecodeToInt16(ve1), v)
    33  			case int32:
    34  				t.Assert(gbinary.BeDecodeToInt32(ve), v)
    35  				t.Assert(gbinary.BeDecodeToInt32(ve1), v)
    36  			case int64:
    37  				t.Assert(gbinary.BeDecodeToInt64(ve), v)
    38  				t.Assert(gbinary.BeDecodeToInt64(ve1), v)
    39  			case uint:
    40  				t.Assert(gbinary.BeDecodeToUint(ve), v)
    41  				t.Assert(gbinary.BeDecodeToUint(ve1), v)
    42  			case uint8:
    43  				t.Assert(gbinary.BeDecodeToUint8(ve), v)
    44  				t.Assert(gbinary.BeDecodeToUint8(ve1), v)
    45  			case uint16:
    46  				t.Assert(gbinary.BeDecodeToUint16(ve1), v)
    47  				t.Assert(gbinary.BeDecodeToUint16(ve), v)
    48  			case uint32:
    49  				t.Assert(gbinary.BeDecodeToUint32(ve1), v)
    50  				t.Assert(gbinary.BeDecodeToUint32(ve), v)
    51  			case uint64:
    52  				t.Assert(gbinary.BeDecodeToUint64(ve), v)
    53  				t.Assert(gbinary.BeDecodeToUint64(ve1), v)
    54  			case bool:
    55  				t.Assert(gbinary.BeDecodeToBool(ve), v)
    56  				t.Assert(gbinary.BeDecodeToBool(ve1), v)
    57  			case string:
    58  				t.Assert(gbinary.BeDecodeToString(ve), v)
    59  				t.Assert(gbinary.BeDecodeToString(ve1), v)
    60  			case float32:
    61  				t.Assert(gbinary.BeDecodeToFloat32(ve), v)
    62  				t.Assert(gbinary.BeDecodeToFloat32(ve1), v)
    63  			case float64:
    64  				t.Assert(gbinary.BeDecodeToFloat64(ve), v)
    65  				t.Assert(gbinary.BeDecodeToFloat64(ve1), v)
    66  			default:
    67  				if v == nil {
    68  					continue
    69  				}
    70  				res := make([]byte, len(ve))
    71  				err := gbinary.BeDecode(ve, res)
    72  				if err != nil {
    73  					t.Errorf("test data: %s, %v, error:%v", k, v, err)
    74  				}
    75  				t.Assert(res, v)
    76  			}
    77  		}
    78  	})
    79  }
    80  
    81  func Test_BeEncodeStruct(t *testing.T) {
    82  	gtest.C(t, func(t *gtest.T) {
    83  		user := User{"wenzi1", 999, "www.baidu.com"}
    84  		ve := gbinary.BeEncode(user)
    85  		s := gbinary.BeDecodeToString(ve)
    86  		t.Assert(string(s), s)
    87  	})
    88  }