Files
kotlin-fork/kotlin-native/backend.native/tests/interop/cpp/cppClass.kt
T
Vsevolod Tolstopyatov f4e8ae5191 Explicitly declare stability levels of declarations in kotlinx.cinterop package
* @ExperimentalForeignApi for all declarations that operate on
    unmanaged memory (i.e. pointers and references)
* @BetaInteropApi for the rest of the interoperability declarations,
    namely Swift/CInterop-specific interfaces and convenience-functions

### Implementation details

* Some typealiases are not marked explicitly because it crashes the compiler,
    yet their experimentality is properly propagated
* License header is adjusted where it previously had been existing
* Deprecated with ERROR interop declarations that are deprecated for more than
    two years are removed
* WASM target interop declarations are deprecated
* Deliberately make Boolean.toByte and Byte.toBoolean foreign-experimental to scare
    people away

^KT-57728 fixed

Merge-request: KT-MR-9788
Merged-by: Vsevolod Tolstopyatov <qwwdfsad@gmail.com>
2023-05-04 13:52:21 +00:00

138 lines
3.4 KiB
Kotlin

@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlinx.cinterop.*
import kotlin.test.*
import cppClass.*
class FeatureTest {
@Test fun ctorDefault() {
memScoped {
val x = alloc<CppTest>()
CppTest.__init__(x.ptr)
assertEquals(42, x.iPub)
assertEquals(42, x.foo())
// dtor is not called, leak is intentional for the purpose of UT
}
}
@Test fun ctorWithParam() {
memScoped {
val x = alloc<CppTest>()
CppTest.__init__(x.ptr, 10, 3.8)
assertEquals(14, x.iPub)
}
}
@Test fun copyCtor(y: CppTest) {
val x = nativeHeap.alloc<CppTest>() {}
CppTest.__init__(x.ptr, y.ptr)
assertEquals(y.iPub, x.iPub)
nativeHeap.free(x)
}
/*
@Test fun reinitWithCtorAndDtor(y: CppTest) {
val count = CppTest.getCount()
val x = nativeHeap.alloc< CppTest>() {}
CppTest.__init__(x.ptr, y.ptr)
assertEquals( CppTest.getCount(), count + 1)
assertEquals(y.iPub, x.iPub)
CppTest.__destroy__(x.ptr)
y.iPub = -11
assertEquals(y.iPub, -11)
CppTest.__init__(x.ptr, y.ptr)
assertEquals(x.iPub, -11)
CppTest.__destroy__(x.ptr)
assertEquals( CppTest.getCount(), count)
nativeHeap.free(x)
}
@Test fun publicField(x : CppTest) {
x.iPub = 21
assertEquals(22, x.foo(x.ptr))
}
@Test fun lvRefParameter() {
memScoped {
val x = alloc<ns__NoName>()
var i = alloc<IntVar>()
i.value = 758
assertEquals(x.noNameMember(i.ptr), 759)
assertEquals(i.value, 759)
}
}
@Test fun staticField() {
val save = CppTest.getCount()
assertEquals( CppTest.getCount(), CppTest.counter)
CppTest.counter = 654
assertEquals( CppTest.getCount(), 654)
assertEquals( CppTest.getCount(), CppTest.counter)
CppTest.counter = save
assertEquals( CppTest.getCount(), save)
}
*/
}
fun main() {
val testRun = FeatureTest()
testRun.ctorDefault()
testRun.ctorWithParam()
// By value for C++ requires further design of stubs mechanism.
// So not supported for now.
//val a0 = retByValue(null)
//println("a0.useContents {iPub} = ${a0.useContents {iPub}}" )
//println("a0.useContents { foo() } = ${a0.useContents { foo() }}" )
// retByValue(null)!!.getValue().foo()
// val a1 = interpretPointed<CppTest>(retByValue(null).rawValue)
// println(a1.foo())
/*
val a1 = interpretPointed< CppTest>(ns__create().rawValue)
testRun.publicField(a1)
testRun.staticField()
testRun.lvRefParameter()
a1.iPub = 112
testRun.copyCtor(a1)
testRun.reinitWithCtorAndDtor(a1)
*/
}
/*
fun testStatic() {
println(" CppTest.s_fun() returns ${ CppTest.s_fun()}")
println(" CppTest.s_fun() returns ${ CppTest.s_fun()}")
println(" CppTest.s_fun() returns ${ CppTest.s_fun()}")
}
fun testCtor() {
println("testCtor")
val cxx = nativeHeap.alloc< CppTest>() {
memcpy(ptr, ns__create(), typeOf< CppTest>().size.convert()) // use placement new here
}
cxx.foo(null)
nativeHeap.free(cxx)
}
fun test2() {
println("test2")
val x = ns__bar(null)
// val theS = interpretPointed< CppTest>(ns__bar(null).rawValue)
// theS.foo(null)
println("x.useContents {iPub} = ${x.useContents {iPub}}" )
}
*/