github.com/gogf/gf/v2@v2.7.4/encoding/gbinary/gbinary_z_unit_le_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/gogf/gf.
     6  
     7  package gbinary_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/gogf/gf/v2/encoding/gbinary"
    13  	"github.com/gogf/gf/v2/test/gtest"
    14  )
    15  
    16  func Test_LeEncodeAndLeDecode(t *testing.T) {
    17  	gtest.C(t, func(t *gtest.T) {
    18  		for k, v := range testData {
    19  			ve := gbinary.LeEncode(v)
    20  			ve1 := gbinary.LeEncodeByLength(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.LeDecodeToInt(ve), v)
    26  				t.Assert(gbinary.LeDecodeToInt(ve1), v)
    27  			case int8:
    28  				t.Assert(gbinary.LeDecodeToInt8(ve), v)
    29  				t.Assert(gbinary.LeDecodeToInt8(ve1), v)
    30  			case int16:
    31  				t.Assert(gbinary.LeDecodeToInt16(ve), v)
    32  				t.Assert(gbinary.LeDecodeToInt16(ve1), v)
    33  			case int32:
    34  				t.Assert(gbinary.LeDecodeToInt32(ve), v)
    35  				t.Assert(gbinary.LeDecodeToInt32(ve1), v)
    36  			case int64:
    37  				t.Assert(gbinary.LeDecodeToInt64(ve), v)
    38  				t.Assert(gbinary.LeDecodeToInt64(ve1), v)
    39  			case uint:
    40  				t.Assert(gbinary.LeDecodeToUint(ve), v)
    41  				t.Assert(gbinary.LeDecodeToUint(ve1), v)
    42  			case uint8:
    43  				t.Assert(gbinary.LeDecodeToUint8(ve), v)
    44  				t.Assert(gbinary.LeDecodeToUint8(ve1), v)
    45  			case uint16:
    46  				t.Assert(gbinary.LeDecodeToUint16(ve1), v)
    47  				t.Assert(gbinary.LeDecodeToUint16(ve), v)
    48  			case uint32:
    49  				t.Assert(gbinary.LeDecodeToUint32(ve1), v)
    50  				t.Assert(gbinary.LeDecodeToUint32(ve), v)
    51  			case uint64:
    52  				t.Assert(gbinary.LeDecodeToUint64(ve), v)
    53  				t.Assert(gbinary.LeDecodeToUint64(ve1), v)
    54  			case bool:
    55  				t.Assert(gbinary.LeDecodeToBool(ve), v)
    56  				t.Assert(gbinary.LeDecodeToBool(ve1), v)
    57  			case string:
    58  				t.Assert(gbinary.LeDecodeToString(ve), v)
    59  				t.Assert(gbinary.LeDecodeToString(ve1), v)
    60  			case float32:
    61  				t.Assert(gbinary.LeDecodeToFloat32(ve), v)
    62  				t.Assert(gbinary.LeDecodeToFloat32(ve1), v)
    63  			case float64:
    64  				t.Assert(gbinary.LeDecodeToFloat64(ve), v)
    65  				t.Assert(gbinary.LeDecodeToFloat64(ve1), v)
    66  			default:
    67  				if v == nil {
    68  					continue
    69  				}
    70  				res := make([]byte, len(ve))
    71  				err := gbinary.LeDecode(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_LeEncodeStruct(t *testing.T) {
    82  	gtest.C(t, func(t *gtest.T) {
    83  		user := User{"wenzi1", 999, "www.baidu.com"}
    84  		ve := gbinary.LeEncode(user)
    85  		s := gbinary.LeDecodeToString(ve)
    86  		t.Assert(s, s)
    87  	})
    88  }