From 612a503e31fa369f9d25f8f5b12ece7acbe6c1f6 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Thu, 10 Sep 2020 14:20:46 +0700 Subject: [PATCH] [KT-41655] Add test for incomplete types --- backend.native/tests/build.gradle | 14 ++++++ .../interop/incomplete_types/library.cpp | 50 +++++++++++++++++++ .../interop/incomplete_types/library.def | 0 .../tests/interop/incomplete_types/library.h | 27 ++++++++++ .../tests/interop/incomplete_types/main.kt | 25 ++++++++++ 5 files changed, 116 insertions(+) create mode 100644 backend.native/tests/interop/incomplete_types/library.cpp create mode 100644 backend.native/tests/interop/incomplete_types/library.def create mode 100644 backend.native/tests/interop/incomplete_types/library.h create mode 100644 backend.native/tests/interop/incomplete_types/main.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index aa08502b283..d7547314be5 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -3583,6 +3583,12 @@ createInterop("auxiliaryCppSources") { it.extraOpts "-Xsource-compiler-option", "-std=c++17" } +createInterop("incomplete_types") { + it.defFile 'interop/incomplete_types/library.def' + it.headers "$projectDir/interop/incomplete_types/library.h" + it.extraOpts "-Xcompile-source", "$projectDir/interop/incomplete_types/library.cpp" +} + createInterop("embedStaticLibraries") { it.defFile 'interop/embedStaticLibraries/embedStaticLibraries.def' it.headers "$projectDir/interop/embedStaticLibraries/embedStaticLibraries.h" @@ -3838,6 +3844,14 @@ interopTest("interop_auxiliarySources") { interop = 'auxiliaryCppSources' } +interopTest("interop_incompleteTypes") { + disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. + source = "interop/incomplete_types/main.kt" + interop = 'incomplete_types' +} + + + interopTest("interop_embedStaticLibraries") { disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. source = "interop/embedStaticLibraries/main.kt" diff --git a/backend.native/tests/interop/incomplete_types/library.cpp b/backend.native/tests/interop/incomplete_types/library.cpp new file mode 100644 index 00000000000..60d1bc8750f --- /dev/null +++ b/backend.native/tests/interop/incomplete_types/library.cpp @@ -0,0 +1,50 @@ +#include "library.h" + +extern "C" { + +struct S { + const char* name; +}; + +struct S s = { + .name = "initial" +}; + +void setContent(struct S* s, const char* name) { + s->name = name; +} + +const char* getContent(struct S* s) { + return s->name; +} + +union U { + float f; + double d; +}; + +void setDouble(union U* u, double value) { + u->d = value; +} + +double getDouble(union U* u) { + return u->d; +} + +union U u = { + .d = 0.0 +}; + +char array[5] = { 0, 0, 0, 0, 0 }; + +void setArrayValue(char* array, char value) { + for (int i = 0; i < 5; ++i) { + array[i] = value; + } +} + +int arrayLength() { + return sizeof(array) / sizeof(char); +} + +} \ No newline at end of file diff --git a/backend.native/tests/interop/incomplete_types/library.def b/backend.native/tests/interop/incomplete_types/library.def new file mode 100644 index 00000000000..e69de29bb2d diff --git a/backend.native/tests/interop/incomplete_types/library.h b/backend.native/tests/interop/incomplete_types/library.h new file mode 100644 index 00000000000..ff02c8894a7 --- /dev/null +++ b/backend.native/tests/interop/incomplete_types/library.h @@ -0,0 +1,27 @@ +#ifdef __cplusplus +extern "C" { +#endif + +// Forward declaration. +struct S; +extern struct S s; + +const char* getContent(struct S* s); +void setContent(struct S* s, const char* name); + +union U; +extern union U u; + +double getDouble(union U* u); +void setDouble(union U* u, double value); + +// Global array of unknown size. +extern char array[]; + + +int arrayLength(); +void setArrayValue(char* array, char value); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/backend.native/tests/interop/incomplete_types/main.kt b/backend.native/tests/interop/incomplete_types/main.kt new file mode 100644 index 00000000000..8118c449c29 --- /dev/null +++ b/backend.native/tests/interop/incomplete_types/main.kt @@ -0,0 +1,25 @@ +import library.* +import kotlinx.cinterop.* +import kotlin.test.* + +fun main() { + assertNotNull(s.ptr) + assertNotNull(u.ptr) + assertNotNull(array) + + assertEquals("initial", getContent(s.ptr)?.toKString()) + setContent(s.ptr, "yo") + assertEquals("yo", getContent(s.ptr)?.toKString()) + + assertEquals(0.0, getDouble(u.ptr)) + setDouble(u.ptr, Double.MIN_VALUE) + assertEquals(Double.MIN_VALUE, getDouble(u.ptr)) + + for (i in 0 until arrayLength()) { + assertEquals(0x0, array[i]) + } + setArrayValue(array, 0x1) + for (i in 0 until arrayLength()) { + assertEquals(0x1, array[i]) + } +} \ No newline at end of file