[KT-41655] Add test for incomplete types

This commit is contained in:
Sergey Bogolepov
2020-09-10 14:20:46 +07:00
committed by Sergey Bogolepov
parent 6d3839c125
commit 612a503e31
5 changed files with 116 additions and 0 deletions
+14
View File
@@ -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"
@@ -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);
}
}
@@ -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
@@ -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])
}
}