From 172373c57eb0f84624f40a0baf47911d784b7f76 Mon Sep 17 00:00:00 2001 From: Pavel Kunyavskiy Date: Tue, 12 Oct 2021 15:03:11 +0300 Subject: [PATCH] [K/N] Add tests for flexible arrays in interop ^KT-48074 --- .../tests/interop/basics/cstructs.def | 24 +++++++++++ .../tests/interop/basics/structs.kt | 43 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/kotlin-native/backend.native/tests/interop/basics/cstructs.def b/kotlin-native/backend.native/tests/interop/basics/cstructs.def index 429f787b7bf..e70c8458b4c 100644 --- a/kotlin-native/backend.native/tests/interop/basics/cstructs.def +++ b/kotlin-native/backend.native/tests/interop/basics/cstructs.def @@ -38,4 +38,28 @@ struct Complex produceComplex() { .b = 1 }; return complex; +}; + +struct WithFlexibleArray { + int size; + int data[]; +}; + +struct WithFlexibleArrayWithPadding { + int size; + char c; + long long data[]; +}; + +void fillArray(struct WithFlexibleArrayWithPadding *flex, int count) { + flex->size = count; + flex->c = '!'; + for (int i = 0; i < count; i++) { + flex->data[i] = (((long long)i) << 32) | (i * 100); + } +} + +struct WithZeroSizedArray { + int size; + int data[0]; }; \ No newline at end of file diff --git a/kotlin-native/backend.native/tests/interop/basics/structs.kt b/kotlin-native/backend.native/tests/interop/basics/structs.kt index e5351c22d7b..7beaed45590 100644 --- a/kotlin-native/backend.native/tests/interop/basics/structs.kt +++ b/kotlin-native/backend.native/tests/interop/basics/structs.kt @@ -63,6 +63,49 @@ fun main() { // Check that generics doesn't break anything. checkEnumSubTyping(E.R) checkIntSubTyping(630090) + + memScoped { + val SIZE = 10 + val flex = alloc(sizeOf() + sizeOf() * SIZE, alignOf()).reinterpret() + flex.size = SIZE + for (i in 0 until SIZE) { + flex.data[i] = i + } + assertEquals(SIZE, flex.size) + for (i in 0 until SIZE) { + assertEquals(i, flex.data[i]) + } + } + + memScoped { + val SIZE = 10 + val flex = alloc(sizeOf() + sizeOf() * SIZE, alignOf()).reinterpret() + assertEquals(4, sizeOf()) + assertEquals(4, alignOf()) + flex.size = SIZE + for (i in 0 until SIZE) { + flex.data[i] = i + } + assertEquals(SIZE, flex.size) + for (i in 0 until SIZE) { + assertEquals(i, flex.data[i]) + } + } + + memScoped { + val SIZE = 10 + assertEquals(8, sizeOf()) + assertEquals(8, alignOf()) + val flex = alloc(sizeOf() + sizeOf() * SIZE, alignOf()) + .reinterpret() + fillArray(flex.ptr, SIZE) + assertEquals(SIZE, flex.size) + assertEquals('!'.code.toByte(), flex.c); + for (i in 0 until SIZE) { + assertEquals((i.toLong() shl 32) or (i.toLong() * 100), flex.data[i]) + } + } + } fun checkEnumSubTyping(e: T) = memScoped {