github.com/timandy/routine@v1.1.4-0.20240507073150-e4a3e1fe2ba5/g/reflect_test.go (about)

     1  // Copyright 2021-2024 TimAndy. All rights reserved.
     2  // Licensed under the Apache-2.0 license that can be found in the LICENSE file.
     3  
     4  package g
     5  
     6  import (
     7  	"testing"
     8  	"unsafe"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestPackEface(t *testing.T) {
    14  	value := 1
    15  	valueInterface := packEface(typeByString("int"), unsafe.Pointer(&value))
    16  	assert.Equal(t, value, valueInterface)
    17  	//
    18  	value = 2
    19  	assert.Equal(t, value, valueInterface)
    20  }
    21  
    22  func TestTypeByString(t *testing.T) {
    23  	gt := typeByString("runtime.g")
    24  	assert.NotNil(t, gt)
    25  	assert.Equal(t, "runtime.g", gt.String())
    26  	fGoid, ok := gt.FieldByName("goid")
    27  	assert.True(t, ok)
    28  	assert.Greater(t, int(fGoid.Offset), 0)
    29  	//
    30  	gt2 := typeByString("*runtime.g")
    31  	assert.NotNil(t, gt2)
    32  	assert.Equal(t, "*runtime.g", gt2.String())
    33  	fGoid2, ok2 := gt2.Elem().FieldByName("goid")
    34  	assert.True(t, ok2)
    35  	assert.Greater(t, int(fGoid2.Offset), 0)
    36  	assert.Equal(t, fGoid.Offset, fGoid2.Offset)
    37  	//
    38  	assert.Nil(t, typeByString("runtime.Pointer"))
    39  }