Add trivial test for CStructVar declared in source code

This commit is contained in:
Svyatoslav Scherbina
2020-11-02 16:20:22 +03:00
committed by Stanislav Erokhin
parent e061e0a687
commit d4beef6872
2 changed files with 44 additions and 0 deletions
@@ -4034,6 +4034,10 @@ task interop_convert(type: KonanLocalTest) {
source = "codegen/intrinsics/interop_convert.kt"
}
task interop_sourceCodeStruct(type: KonanLocalTest) {
source = "codegen/intrinsics/interop_sourceCodeStruct.kt"
}
/*
TODO: This test isn't run automatically
task interop_echo_server(type: RunInteropKonanTest) {
@@ -0,0 +1,40 @@
package codegen.intrinsics.interop_sourceCodeStruct
import kotlinx.cinterop.*
import kotlinx.cinterop.internal.*
import kotlin.test.*
// Just making sure this doesn't get accidentally forbidden or otherwise broken.
// (however defining structs this way is still strongly discouraged, please define
// structs in C headers or .def files instead).
@CStruct("struct { int p0; int p1; }")
class S(rawPtr: NativePtr) : CStructVar(rawPtr) {
companion object : CStructVar.Type(8, 4)
var x: Int
get() = memberAt<IntVar>(0).value
set(value) {
memberAt<IntVar>(0).value = value
}
var y: Int
get() = memberAt<IntVar>(4).value
set(value) {
memberAt<IntVar>(4).value = value
}
}
@Test
fun test() = memScoped {
val s = alloc<S>()
s.x = 123
assertEquals(123, s.x)
assertEquals(123, s.ptr.reinterpret<IntVar>()[0])
s.y = 321
assertEquals(321, s.y)
assertEquals(321, s.ptr.reinterpret<IntVar>()[1])
}