github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/map_elem_bytes_test.go (about)

     1  package test
     2  
     3  import (
     4  	"testing"
     5  	"github.com/v2pro/plz/reflect2"
     6  	"github.com/v2pro/plz/test"
     7  	"github.com/v2pro/plz/countlog"
     8  	"github.com/v2pro/plz/test/must"
     9  )
    10  
    11  func Test_map_elem_bytes(t *testing.T) {
    12  	t.Run("SetIndex", testOp(func(api reflect2.API) interface{} {
    13  		obj := map[int][]byte{}
    14  		valType := api.TypeOf(obj).(reflect2.MapType)
    15  		valType.SetIndex(obj, 2, []byte("hello"))
    16  		valType.SetIndex(obj, 3, nil)
    17  		return obj
    18  	}))
    19  	t.Run("UnsafeSetIndex", test.Case(func(ctx *countlog.Context) {
    20  		obj := map[int][]byte{}
    21  		valType := reflect2.TypeOf(obj).(reflect2.MapType)
    22  		hello := []byte("hello")
    23  		valType.UnsafeSetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(2), reflect2.PtrOf(hello))
    24  		valType.UnsafeSetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(3), nil)
    25  		must.Equal([]byte("hello"), obj[2])
    26  		must.Nil(obj[3])
    27  	}))
    28  	t.Run("UnsafeGetIndex", test.Case(func(ctx *countlog.Context) {
    29  		obj := map[int][]byte{2: []byte("hello")}
    30  		valType := reflect2.TypeOf(obj).(reflect2.MapType)
    31  		elem := valType.UnsafeGetIndex(reflect2.PtrOf(obj), reflect2.PtrOf(2))
    32  		must.Equal([]byte("hello"), valType.Elem().PackEFace(elem))
    33  	}))
    34  }