diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 75860edf0d4..9080910d320 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -3469,6 +3469,10 @@ createInterop("cmangling_keywords2") { it.defFile 'interop/basics/mangling_keywords2.def' } +createInterop("cunion") { + it.defFile 'interop/basics/cunion.def' +} + createInterop("auxiliaryCppSources") { // We need to provide empty def file to create correct `KonanInteropTask`. it.defFile 'interop/auxiliary_sources/auxiliaryCppSources.def' @@ -3718,6 +3722,12 @@ interopTest("interop_withSpaces") { } } +interopTest("interop_union") { + disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. + interop = 'cunion' + source = "interop/basics/union.kt" +} + task interop_convert(type: KonanLocalTest) { source = "codegen/intrinsics/interop_convert.kt" } diff --git a/backend.native/tests/interop/basics/cunion.def b/backend.native/tests/interop/basics/cunion.def new file mode 100644 index 00000000000..58407a348cd --- /dev/null +++ b/backend.native/tests/interop/basics/cunion.def @@ -0,0 +1,17 @@ +--- +typedef union { + short s; + long long ll; +} BasicUnion; + +typedef struct { + union { + int i; + float f; + } as; +} StructWithUnion; + +typedef union { + unsigned int i : 31; + unsigned char b : 1; +} Packed; \ No newline at end of file diff --git a/backend.native/tests/interop/basics/union.kt b/backend.native/tests/interop/basics/union.kt new file mode 100644 index 00000000000..43f958bd9f4 --- /dev/null +++ b/backend.native/tests/interop/basics/union.kt @@ -0,0 +1,25 @@ +import cunion.* +import kotlinx.cinterop.* +import kotlin.test.* + +fun main() { + memScoped { + val basicUnion = alloc() + for (value in Short.MIN_VALUE..Short.MAX_VALUE) { + basicUnion.ll = value.toLong() + assertEquals(value.toShort(), basicUnion.s) + } + } + memScoped { + val struct = alloc() + struct.`as`.i = Float.NaN.toRawBits() + assertEquals(Float.NaN, struct.`as`.f) + } + memScoped { + val union = alloc() + union.b = 1u + assertEquals(1u, union.i) + union.i = 0u + assertEquals(0u, union.b) + } +} \ No newline at end of file