github.com/ii64/gouring@v0.4.1/util_ptr_arith_test.go (about)

     1  package gouring
     2  
     3  import (
     4  	"encoding/binary"
     5  	"fmt"
     6  	"testing"
     7  	"unsafe"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestUserdata(t *testing.T) {
    13  	type test struct {
    14  		v   any
    15  		exp uint64
    16  	}
    17  	ts := []test{
    18  		{uint64(0), 0},
    19  		{uint64(0xff), 0xff},
    20  		{uint64(0xfffefd), 0xfffefd},
    21  		{uintptr(0xcafeba), 0xcafeba},
    22  		{unsafe.Pointer(nil), 0},
    23  	}
    24  	bo := binary.LittleEndian
    25  	for _, tc := range ts {
    26  		var u UserData
    27  		switch v := tc.v.(type) {
    28  		case uint64:
    29  			u.SetUint64(v)
    30  		case uintptr:
    31  			u.SetUintptr(v)
    32  		case unsafe.Pointer:
    33  			u.SetUnsafe(v)
    34  		default:
    35  			panic(fmt.Sprintf("unhandled type: %T", v))
    36  		}
    37  
    38  		assert.Equal(t, tc.exp, u.GetUint64())
    39  
    40  		var exp [8]byte
    41  		bo.PutUint64(exp[:], tc.exp)
    42  		assert.Equal(t, exp[:], u[:])
    43  	}
    44  }