Skip to content

Commit

Permalink
[Interop][Test] Add tests for unions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbogolepov committed May 6, 2020
1 parent 9f9609a commit 47e5058
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend.native/tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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"
}
Expand Down
17 changes: 17 additions & 0 deletions backend.native/tests/interop/basics/cunion.def
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 25 additions & 0 deletions backend.native/tests/interop/basics/union.kt
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 47e5058

Please sign in to comment.