github.com/jxskiss/gopkg/v2@v2.14.9-0.20240514120614-899f3e7952b4/unsafe/reflectx/rtype_test.go (about)

     1  package reflectx
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"unsafe"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  
    10  	"github.com/jxskiss/gopkg/v2/internal/linkname"
    11  	"github.com/jxskiss/gopkg/v2/internal/unsafeheader"
    12  )
    13  
    14  type simple struct {
    15  	A string
    16  }
    17  
    18  func TestRTypeMethods(t *testing.T) {
    19  	reflectTyp := reflect.TypeOf((*reflect.Type)(nil)).Elem()
    20  	rtyp := reflect.TypeOf((*RType)(nil))
    21  
    22  	assert.Equal(t, 31, reflectTyp.NumMethod())
    23  	assert.Equal(t, 33, rtyp.NumMethod())
    24  
    25  	for i := 0; i < reflectTyp.NumMethod(); i++ {
    26  		meth := reflectTyp.Method(i)
    27  		if !meth.IsExported() {
    28  			// private methods:
    29  			//   - common() *rtype
    30  			//   - uncommon() *uncommonType
    31  			continue
    32  		}
    33  		_, ok := rtyp.MethodByName(meth.Name)
    34  		assert.Truef(t, ok, "missing method %v", meth.Name)
    35  	}
    36  }
    37  
    38  func TestRType(t *testing.T) {
    39  	var (
    40  		oneI8    int8  = 1
    41  		twoI32   int32 = 2
    42  		threeInt int   = 3
    43  		strA           = "a"
    44  	)
    45  	types := []any{
    46  		int8(1),
    47  		&oneI8,
    48  		int32(2),
    49  		&twoI32,
    50  		int(3),
    51  		&threeInt,
    52  		"a",
    53  		&strA,
    54  		simple{"b"},
    55  		&simple{"b"},
    56  	}
    57  	for _, x := range types {
    58  		rtype1 := EfaceOf(&x).RType
    59  		rtype2 := RTypeOf(reflect.TypeOf(x))
    60  		rtype3 := RTypeOf(reflect.ValueOf(x))
    61  		assert.Equal(t, rtype1, rtype2)
    62  		assert.Equal(t, rtype2, rtype3)
    63  	}
    64  
    65  	assert.Equal(t, reflect.TypeOf(0).Size(), RTypeOf(0).Size())
    66  	assert.Equal(t, reflect.Int, RTypeOf(0).Kind())
    67  	assert.Equal(t, reflect.TypeOf("").Size(), RTypeOf("").Size())
    68  	assert.Equal(t, reflect.String, RTypeOf("").Kind())
    69  }
    70  
    71  func TestPtrTo(t *testing.T) {
    72  	var x int64
    73  	typ1 := PtrTo(RTypeOf(x)).ToReflectType()
    74  	typ2 := reflect.PtrTo(reflect.TypeOf(x))
    75  	assert.Equal(t, typ1, typ2)
    76  }
    77  
    78  func TestRTypeToType(t *testing.T) {
    79  	typ := RTypeOf(123)
    80  	t1 := reflect.TypeOf(123)
    81  	t2 := typ.ToReflectType()
    82  
    83  	if1 := unsafeheader.ToIface(t1)
    84  	if2 := unsafeheader.ToIface(t2)
    85  	assert.True(t, if1.Tab == if2.Tab)
    86  	assert.True(t, if1.Data == if2.Data)
    87  }
    88  
    89  func TestToRType(t *testing.T) {
    90  	var x int64
    91  	rtyp1 := RTypeOf(x)
    92  	rtyp2 := ToRType(reflect.TypeOf(x))
    93  	assert.Equal(t, rtyp1, rtyp2)
    94  }
    95  
    96  func TestRTypeOf(t *testing.T) {
    97  	x := any(123)
    98  	rtyp := EfaceOf(&x).RType
    99  	assert.Equal(t, rtyp, RTypeOf(123))
   100  	assert.Equal(t, rtyp, RTypeOf(reflect.TypeOf(123)))
   101  	assert.Equal(t, rtyp, RTypeOf(reflect.ValueOf(123)))
   102  	assert.Equal(t, rtyp, RTypeOf(RTypeOf(123)))
   103  }
   104  
   105  func BenchmarkRTypeSizeAndKind(b *testing.B) {
   106  	typ := RTypeOf("hello")
   107  	for i := 0; i < b.N; i++ {
   108  		_ = typ.Size()
   109  		_ = typ.Kind()
   110  	}
   111  }
   112  
   113  func BenchmarkRTypeSizeAndKind_linkname(b *testing.B) {
   114  	typ := RTypeOf("hello")
   115  	for i := 0; i < b.N; i++ {
   116  		_ = linkname.Reflect_rtype_Size(unsafe.Pointer(typ))
   117  		_ = linkname.Reflect_rtype_Kind(unsafe.Pointer(typ))
   118  	}
   119  }