gouring/util_ptr_arith_test.go
Nugraha 7eedf9754b
all: use of unsafe pointer for ring, and adjustments
There's other adjustment/addition:
 * Function name CQE/SQE renamed to Cqe/Sqe.
 * SQ entry union sets value withput util to reduce function
   complexity, so leaving function budget for another stuff.
 * UserData has its own type, implement for uint64, uintptr,
   and unsafe.
 * TODO: decide what to use `Syscall` or `RawSyscall`.
 * Exposed `SeenCqe`.
 * Ignore *.test file extension.
 * Inline cost tool.

Signed-off-by: Nugraha <richiisei@gmail.com>
2022-07-28 19:45:51 +07:00

44 lines
745 B
Go

package gouring
import (
"encoding/binary"
"fmt"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func TestUserdata(t *testing.T) {
type test struct {
v any
exp uint64
}
ts := []test{
{uint64(0), 0},
{uint64(0xff), 0xff},
{uint64(0xfffefd), 0xfffefd},
{uintptr(0xcafeba), 0xcafeba},
{unsafe.Pointer(nil), 0},
}
bo := binary.LittleEndian
for _, tc := range ts {
var u UserData
switch v := tc.v.(type) {
case uint64:
u.SetUint64(v)
case uintptr:
u.SetUintptr(v)
case unsafe.Pointer:
u.SetUnsafe(v)
default:
panic(fmt.Sprintf("unhandled type: %T", v))
}
assert.Equal(t, tc.exp, u.GetUint64())
var exp [8]byte
bo.PutUint64(exp[:], tc.exp)
assert.Equal(t, exp[:], u[:])
}
}