[Interop][Test] Add tests for unions

This commit is contained in:
Sergey Bogolepov
2020-05-06 15:41:21 +07:00
committed by Sergey Bogolepov
parent 9f9609ac0f
commit 47e505825e
3 changed files with 52 additions and 0 deletions
+10
View File
@@ -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"
}
@@ -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;
@@ -0,0 +1,25 @@
import cunion.*
import kotlinx.cinterop.*
import kotlin.test.*
fun main() {
memScoped {
val basicUnion = alloc<BasicUnion>()
for (value in Short.MIN_VALUE..Short.MAX_VALUE) {
basicUnion.ll = value.toLong()
assertEquals(value.toShort(), basicUnion.s)
}
}
memScoped {
val struct = alloc<StructWithUnion>()
struct.`as`.i = Float.NaN.toRawBits()
assertEquals(Float.NaN, struct.`as`.f)
}
memScoped {
val union = alloc<Packed>()
union.b = 1u
assertEquals(1u, union.i)
union.i = 0u
assertEquals(0u, union.b)
}
}