[K/N] Add tests for flexible arrays in interop

^KT-48074
This commit is contained in:
Pavel Kunyavskiy
2021-10-12 15:03:11 +03:00
committed by Space
parent 0487261be8
commit 172373c57e
2 changed files with 67 additions and 0 deletions
@@ -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];
};
@@ -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<WithFlexibleArray>() + sizeOf<IntVar>() * SIZE, alignOf<WithFlexibleArray>()).reinterpret<WithFlexibleArray>()
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<WithZeroSizedArray>() + sizeOf<IntVar>() * SIZE, alignOf<WithZeroSizedArray>()).reinterpret<WithZeroSizedArray>()
assertEquals(4, sizeOf<WithZeroSizedArray>())
assertEquals(4, alignOf<WithZeroSizedArray>())
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<WithFlexibleArrayWithPadding>())
assertEquals(8, alignOf<WithFlexibleArrayWithPadding>())
val flex = alloc(sizeOf<WithFlexibleArrayWithPadding>() + sizeOf<LongVar>() * SIZE, alignOf<WithFlexibleArrayWithPadding>())
.reinterpret<WithFlexibleArrayWithPadding>()
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 <T : E> checkEnumSubTyping(e: T) = memScoped {