From 1618e19b8d31df1e382bd1959601d5b421e01b1b Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Fri, 14 Feb 2020 15:59:42 +0700 Subject: [PATCH] [Interop] Add test for different types of struct fields --- backend.native/tests/build.gradle | 10 ++++++ .../tests/interop/basics/cstructs.def | 33 ++++++++++++++++++ .../tests/interop/basics/structs.kt | 34 +++++++++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 backend.native/tests/interop/basics/cstructs.def create mode 100644 backend.native/tests/interop/basics/structs.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index f700e9f685f..2c97b6c6244 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -3345,6 +3345,10 @@ createInterop("cvalues") { it.defFile 'interop/basics/cvalues.def' } +createInterop("cstructs") { + it.defFile 'interop/basics/cstructs.def' +} + createInterop("ccallbacksAndVarargs") { it.defFile 'interop/basics/ccallbacksAndVarargs.def' } @@ -3520,6 +3524,12 @@ interopTest("interop_values") { interop = 'cvalues' } +interopTest("interop_structs") { + disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. + source = "interop/basics/structs.kt" + interop = 'cstructs' +} + interopTest("interop_callbacksAndVarargs") { disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. source = "interop/basics/callbacksAndVarargs.kt" diff --git a/backend.native/tests/interop/basics/cstructs.def b/backend.native/tests/interop/basics/cstructs.def new file mode 100644 index 00000000000..b5e35bf6e59 --- /dev/null +++ b/backend.native/tests/interop/basics/cstructs.def @@ -0,0 +1,33 @@ +--- +typedef float __attribute__ ((__vector_size__ (16))) KVector4f; + +typedef struct { + int i; +} Trivial; + +enum E { + R, G, B +}; + +struct Complex { + unsigned int ui; + KVector4f vec4f; + Trivial t; + struct Complex* next; + enum E e; + int arr[2]; +}; + +struct Complex produceComplex() { + struct Complex complex; + complex.ui = 128; + KVector4f vec = { 1.0, 1.0, 1.0, 1.0 }; + complex.vec4f = vec; + Trivial t = { .i = 1 }; + complex.t = t; + complex.next = 0; + complex.e = R; + complex.arr[0] = -51; + complex.arr[1] = -19; + return complex; +}; \ No newline at end of file diff --git a/backend.native/tests/interop/basics/structs.kt b/backend.native/tests/interop/basics/structs.kt new file mode 100644 index 00000000000..6b7a8272ec5 --- /dev/null +++ b/backend.native/tests/interop/basics/structs.kt @@ -0,0 +1,34 @@ +import cstructs.* +import kotlinx.cinterop.* +import kotlin.test.* + +fun main() { + produceComplex().useContents { + assertEquals(ui, 128u) + ui = 333u + assertEquals(ui, 333u) + + assertEquals(t.i, 1) + t.i += 15 + assertEquals(t.i, 16) + + assertEquals(next, null) + next = this.ptr + assertEquals(next, this.ptr) + + assertEquals(vec4f, vectorOf(1.0f, 1.0f, 1.0f, 1.0f)) + vec4f = vectorOf(0.0f, 0.0f, 0.0f, 0.0f) + assertEquals(vec4f, vectorOf(0.0f, 0.0f, 0.0f, 0.0f)) + + assertEquals(e, E.R) + e = E.G + assertEquals(e, E.G) + + assertEquals(arr[0], -51) + assertEquals(arr[1], -19) + arr[0] = 51 + arr[1] = 19 + assertEquals(arr[0], 51) + assertEquals(arr[1], 19) + } +} \ No newline at end of file