github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_z_unit_byte_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 gconv_test
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/gogf/gf/v2/container/gvar"
    13  	"github.com/gogf/gf/v2/test/gtest"
    14  	"github.com/gogf/gf/v2/util/gconv"
    15  )
    16  
    17  var byteTests = []struct {
    18  	value   interface{}
    19  	expect  byte
    20  	expects []byte
    21  }{
    22  	{true, 1, []byte{1}},
    23  	{false, 0, []byte{0}},
    24  
    25  	{int(0), 0, []byte{0}},
    26  	{int(123), 123, []byte{123}},
    27  	{int8(123), 123, []byte{123}},
    28  	{int16(123), 123, []byte{123, 0}},
    29  	{int32(123123123), 179, []byte{179, 181, 86, 7}},
    30  	{int64(123123123123123123), 179, []byte{179, 243, 99, 1, 212, 107, 181, 1}},
    31  
    32  	{uint(0), 0, []byte{0}},
    33  	{uint(123), 123, []byte{123}},
    34  	{uint8(123), 123, []byte{123}},
    35  	{uint16(123), 123, []byte{123, 0}},
    36  	{uint32(123123123), 179, []byte{179, 181, 86, 7}},
    37  	{uint64(123123123123123123), 179, []byte{179, 243, 99, 1, 212, 107, 181, 1}},
    38  
    39  	{uintptr(0), 0, []byte{48}},
    40  	{uintptr(123), 123, []byte{49, 50, 51}},
    41  
    42  	{rune(0), 0, []byte{0, 0, 0, 0}},
    43  	{rune(49), 49, []byte{49, 0, 0, 0}},
    44  
    45  	{float32(123), 123, []byte{0, 0, 246, 66}},
    46  	{float64(123.456), 123, []byte{119, 190, 159, 26, 47, 221, 94, 64}},
    47  
    48  	{[]byte(""), 0, []byte("")},
    49  
    50  	{"Uranus", 0, []byte("Uranus")},
    51  
    52  	{complex(1, 2), 0,
    53  		[]byte{0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64}},
    54  
    55  	{[3]int{1, 2, 3}, 0, []byte{1, 2, 3}},
    56  	{[]int{1, 2, 3}, 0, []byte{1, 2, 3}},
    57  
    58  	{map[int]int{1: 1}, 0, []byte(`{"1":1}`)},
    59  	{map[string]string{"Earth": "印度洋"}, 0, []byte(`{"Earth":"印度洋"}`)},
    60  
    61  	{gvar.New(123), 123, []byte{123}},
    62  	{gvar.New(123.456), 123, []byte{119, 190, 159, 26, 47, 221, 94, 64}},
    63  }
    64  
    65  func TestByte(t *testing.T) {
    66  	gtest.C(t, func(t *gtest.T) {
    67  		for _, test := range byteTests {
    68  			t.AssertEQ(gconv.Byte(test.value), test.expect)
    69  		}
    70  	})
    71  }
    72  
    73  func TestBytes(t *testing.T) {
    74  	gtest.C(t, func(t *gtest.T) {
    75  		for _, test := range byteTests {
    76  			t.AssertEQ(gconv.Bytes(test.value), test.expects)
    77  		}
    78  	})
    79  
    80  	gtest.C(t, func(t *gtest.T) {
    81  		t.AssertEQ(gconv.Bytes(nil), nil)
    82  	})
    83  }