diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go index 143f23f0e0cc36..2bcc7833d88bef 100644 --- a/misc/cgo/test/cgo_test.go +++ b/misc/cgo/test/cgo_test.go @@ -79,6 +79,7 @@ func TestCthread(t *testing.T) { testCthread(t) } func TestEnum(t *testing.T) { testEnum(t) } func TestNamedEnum(t *testing.T) { testNamedEnum(t) } func TestCastToEnum(t *testing.T) { testCastToEnum(t) } +func TestUnion(t *testing.T) { testUnion(t) } func TestErrno(t *testing.T) { testErrno(t) } func TestFpVar(t *testing.T) { testFpVar(t) } func TestHandle(t *testing.T) { testHandle(t) } diff --git a/misc/cgo/test/test.go b/misc/cgo/test/test.go index 3b8f548b13dd20..aaebdd0519fb0a 100644 --- a/misc/cgo/test/test.go +++ b/misc/cgo/test/test.go @@ -95,6 +95,11 @@ enum E { Enum2 = 2, }; +union U { + float f32; + uint64_t u64; +}; + typedef unsigned char cgo_uuid_t[20]; void uuid_generate(cgo_uuid_t x) { @@ -1034,6 +1039,24 @@ func testCastToEnum(t *testing.T) { } } +func testUnion(t *testing.T) { + var u C.union_U + + goU64 := (*uint64)(unsafe.Pointer(&u)) + *goU64 = math.MaxUint64 + + if *goU64 != math.MaxUint64 { + t.Error("failed to assign value to union", *goU64) + } + + goF32 := (*float32)(unsafe.Pointer(&u)) + *goF32 = 1.0 + + if *goF32 != 1.0 { + t.Error("failed to assign value to union", *goF32) + } +} + func testAtol(t *testing.T) { l := Atol("123") if l != 123 {