github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/reflect2/test/int_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  	"unsafe"
     9  	"github.com/v2pro/plz/test/must"
    10  )
    11  
    12  func Test_int(t *testing.T) {
    13  	t.Run("New", testOp(func(api reflect2.API) interface{} {
    14  		valType := api.TypeOf(1)
    15  		obj := valType.New()
    16  		*obj.(*int) = 100
    17  		return obj
    18  	}))
    19  	t.Run("PackEFace", test.Case(func(ctx *countlog.Context) {
    20  		valType := reflect2.TypeOf(1)
    21  		hundred := 100
    22  		must.Equal(&hundred, valType.PackEFace(unsafe.Pointer(&hundred)))
    23  	}))
    24  	t.Run("Indirect", test.Case(func(ctx *countlog.Context) {
    25  		valType := reflect2.TypeOf(1)
    26  		hundred := 100
    27  		must.Equal(100, valType.Indirect(&hundred))
    28  	}))
    29  	t.Run("Indirect", test.Case(func(ctx *countlog.Context) {
    30  		valType := reflect2.TypeOf(1)
    31  		hundred := 100
    32  		must.Equal(100, valType.UnsafeIndirect(unsafe.Pointer(&hundred)))
    33  	}))
    34  	t.Run("Set", testOp(func(api reflect2.API) interface{} {
    35  		valType := api.TypeOf(1)
    36  		i := 1
    37  		j := 10
    38  		valType.Set(&i, &j)
    39  		return i
    40  	}))
    41  }