diff --git a/mem/mem.go b/mem/mem.go index 8c43319..08e143b 100644 --- a/mem/mem.go +++ b/mem/mem.go @@ -11,18 +11,18 @@ const ( Terabyte ) -// SizeOf returns the size (in bytes) of the given type. +// Sizeof returns the size (in bytes) of the given type. // // Not to be confused with [unsafe.Sizeof] which returns the size of a type via an expression. -func SizeOf[T any]() uintptr { - return unsafe.Sizeof(ZeroValue[T]()) +func Sizeof[T any]() uintptr { + return unsafe.Sizeof(*(*T)(nil)) } -// AlignOf returns the alignment (in bytes) of the given type. +// Alignof returns the alignment (in bytes) of the given type. // // Not to be confused with [unsafe.AlignOf] which returns the alignment of a type via an expression. -func AlignOf[T any]() uintptr { - return unsafe.Alignof(ZeroValue[T]()) +func Alignof[T any]() uintptr { + return unsafe.Alignof(*(*T)(nil)) } // ZeroValue returns the zero value of a given type. @@ -34,7 +34,7 @@ func ZeroValue[T any]() (_ T) { // // BitCast panics if the sizes of the types differ. func BitCast[TOut any, TIn any](value *TIn) TOut { - if SizeOf[TOut]() != SizeOf[TIn]() { + if Sizeof[TOut]() != Sizeof[TIn]() { panic("bitcast: sizes of types must match") } return *((*TOut)(unsafe.Pointer(value))) diff --git a/testx/testx.go b/testx/testx.go index c60f499..5d1410b 100644 --- a/testx/testx.go +++ b/testx/testx.go @@ -7,10 +7,18 @@ func Expect(t *testing.T, cond bool, message ...any) { if !cond { if len(message) == 0 { - message = append(message, "assertion failed") + message = append(message, "expectation failed") } str := message[0].(string) t.Fatalf(str, message[1:]...) } } + +func ShouldPanic(t *testing.T, f func()) { + t.Helper() + + defer func() { recover() }() + f() + t.Fatal("expected panic") +}