[K/N] Migrate more cinterop tests

^KT-61259
This commit is contained in:
Vladimir Sukharev
2024-01-13 12:23:35 +01:00
committed by Space Team
parent 8ed7e31b5f
commit 5f51f5e1fc
37 changed files with 1268 additions and 601 deletions
@@ -1019,20 +1019,6 @@ void createInterop(String name, Closure conf) {
}
}
createInterop("sysstat") {
// FIXME: creates empty file to workaround konan gradle plugin issue (should be able to have no def file)
def f = File.createTempFile("sysstat.empty", ".def")
it.packageName 'sysstat'
// FIXME: this option doesn't work due to an issue in plugin: it tries to resolve header in project dir.
//it.headers 'sys/stat.h'
f.write("headers = sys/stat.h")
it.defFile f
}
createInterop("cstdio") {
it.defFile 'interop/basics/cstdio.def'
}
createInterop("sockets") {
it.defFile 'interop/basics/sockets.def'
}
@@ -1342,31 +1328,6 @@ standaloneTest("interop_objc_allocException") {
UtilsKt.dependsOnPlatformLibs(it)
}
interopTest("interop0") {
disabled = (project.testTarget == 'linux_arm64')
useGoldenData = true
source = "interop/basics/0.kt"
interop = 'sysstat'
}
interopTest("interop2") {
useGoldenData = true
source = "interop/basics/2.kt"
interop = 'cstdio'
}
interopTest("interop4") {
useGoldenData = true
source = "interop/basics/4.kt"
interop = 'cstdio'
}
standaloneTest("interop5") {
useGoldenData = true
source = "interop/basics/5.kt"
UtilsKt.dependsOnPlatformLibs(it)
}
interopTest("interop_concurrentTerminate") {
source = "interop/concurrentTerminate/main.kt"
interop = 'concurrentTerminate'
@@ -1379,34 +1340,6 @@ interopTest("interop_embedStaticLibraries") {
interop = 'embedStaticLibraries'
}
interopTest("interop_threadStates") {
source = "interop/threadStates/threadStates.kt"
flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative']
interop = "threadStates"
}
interopTest("interop_threadStates_callbacksWithExceptions") {
source = "interop/threadStates/callbacksWithExceptions.kt"
interop = "threadStates"
flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative']
}
interopTest("interop_threadStates_unhandledException") {
source = "interop/threadStates/unhandledException.kt"
interop = "threadStates"
outputChecker = { it.contains("Error. Runnable state: true") }
expectedExitStatusChecker = { it != 0 }
flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative']
}
interopTest("interop_threadStates_unhandledExceptionInForeignThread") {
source = "interop/threadStates/unhandledExceptionInForeignThread.kt"
interop = "threadStates"
outputChecker = { it.contains("Error. Runnable state: true") }
expectedExitStatusChecker = { it != 0 }
flags = ['-opt-in=kotlin.native.internal.InternalForKotlinNative']
}
interopTest("interop_withSpaces") {
interop ='withSpaces'
source = "interop/basics/withSpaces.kt"
@@ -1446,11 +1379,6 @@ interopTest("interop_leakMemoryWithRunningThreadChecked") {
outputChecker = { s -> s.contains("Cannot run checkers when there are 1 alive runtimes at the shutdown") }
}
standaloneTest("interop_pinning") {
source = "interop/basics/pinning.kt"
flags = [ "-tr" ]
}
interopTest("interop_cppClass") {
disabled = PlatformInfo.isAppleTarget(project) // KT-58422
source = "interop/cpp/cppClass.kt"
@@ -1,15 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import sysstat.*
import kotlinx.cinterop.*
fun main(args: Array<String>) {
val statBuf = nativeHeap.alloc<stat>()
val res = stat("/", statBuf.ptr)
println(res)
println(statBuf.st_uid)
}
@@ -1,2 +0,0 @@
0
0
@@ -1,10 +0,0 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
import cstdio.*
fun main(args: Array<String>) {
puts("Hello")
}
@@ -1 +0,0 @@
Hello
@@ -1,23 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import cstdio.*
import kotlinx.cinterop.*
val stdout
get() = getStdout()
fun main(args: Array<String>) {
fprintf(stdout, "%s %s %d %d %d %lld %.1f %.1lf %d %d\n",
"a", "b".cstr, (-1).toByte(), 2.toShort(), 3, Long.MAX_VALUE, 0.1.toFloat(), 0.2, true, false)
memScoped {
val aVar = alloc<IntVar>()
val bVar = alloc<IntVar>()
val sscanfResult = sscanf("42", "%d%d", aVar.ptr, bVar.ptr)
printf("%d %d\n", sscanfResult, aVar.value)
}
}
@@ -1,2 +0,0 @@
a b -1 2 3 9223372036854775807 0.1 0.2 1 0
1 42
@@ -1,13 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import platform.posix.printf
val golden = immutableBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0)
fun main(args: Array<String>) {
printf("%s\n", golden)
}
@@ -1 +0,0 @@
Hello!
@@ -1,8 +0,0 @@
headers = stdio.h
compilerOpts.osx = -D_ANSI_SOURCE
---
static inline FILE* getStdout() {
return stdout;
}
@@ -1,248 +0,0 @@
/*
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class)
import kotlin.test.*
import kotlinx.cinterop.*
@Test fun pinnedByteArrayAddressOf() {
val arr = ByteArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedStringAddressOf() {
val str = "0000000000"
str.usePinned {
it.addressOf(0)
it.addressOf(9)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedCharArrayAddressOf() {
val arr = CharArray(10) { '0' }
arr.usePinned {
it.addressOf(0)
it.addressOf(9)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedShortArrayAddressOf() {
val arr = ShortArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedIntArrayAddressOf() {
val arr = IntArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedLongArrayAddressOf() {
val arr = LongArray(10) { 0 }
arr.usePinned {
assertEquals(0, it.addressOf(0).pointed.value)
assertEquals(0, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedUByteArrayAddressOf() {
val arr = UByteArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedUShortArrayAddressOf() {
val arr = UShortArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedUIntArrayAddressOf() {
val arr = UIntArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedULongArrayAddressOf() {
val arr = ULongArray(10) { 0U }
arr.usePinned {
assertEquals(0U, it.addressOf(0).pointed.value)
assertEquals(0U, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedFloatArrayAddressOf() {
val arr = FloatArray(10) { 0.0f }
arr.usePinned {
assertEquals(0.0f, it.addressOf(0).pointed.value)
assertEquals(0.0f, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@Test fun pinnedDoubleArrayAddressOf() {
val arr = DoubleArray(10) { 0.0 }
arr.usePinned {
assertEquals(0.0, it.addressOf(0).pointed.value)
assertEquals(0.0, it.addressOf(9).pointed.value)
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(10)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(-1)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MAX_VALUE)
}
assertFailsWith<IndexOutOfBoundsException> {
it.addressOf(Int.MIN_VALUE)
}
}
}
@@ -1,97 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class)
import kotlin.native.runtime.Debugging
import kotlin.test.*
import kotlinx.cinterop.staticCFunction
import threadStates.*
fun main() {
callbackWithException()
callbackWithFinally()
callbackWithFinallyNoCatch()
nestedCallbackWithException()
nestedCallbackWithFinally()
}
fun assertRunnableThreadState() {
assertTrue(Debugging.isThreadStateRunnable)
}
class CustomException() : Exception()
fun throwException() {
assertRunnableThreadState()
throw CustomException()
}
fun callbackWithException() {
try {
runCallback(staticCFunction(::throwException))
} catch (e: CustomException) {
assertRunnableThreadState()
return
} catch (e: Throwable) {
assertRunnableThreadState()
fail("Wrong exception type: ${e.message}")
}
fail("No exception thrown")
}
fun callbackWithFinally() {
try {
runCallback(staticCFunction(::throwException))
} catch (e: CustomException) {
assertRunnableThreadState()
return
} finally {
assertRunnableThreadState()
}
fail("No exception thrown")
}
fun callbackWithFinallyNoCatch() {
try {
try {
runCallback(staticCFunction(::throwException))
} finally {
assertRunnableThreadState()
}
assertRunnableThreadState()
} catch (_: CustomException) {}
}
fun nestedCallbackWithException() {
try {
runCallback(staticCFunction { ->
assertRunnableThreadState()
runCallback(staticCFunction(::throwException))
})
} catch (e: CustomException) {
assertRunnableThreadState()
return
} catch (e: Throwable) {
assertRunnableThreadState()
fail("Wrong exception type: ${e.message}")
}
fail("No exception thrown")
}
fun nestedCallbackWithFinally() {
try {
runCallback(staticCFunction { ->
assertRunnableThreadState()
runCallback(staticCFunction(::throwException))
})
} catch (e: CustomException) {
assertRunnableThreadState()
return
} finally {
assertRunnableThreadState()
}
fail("No exception thrown")
}
@@ -1,33 +0,0 @@
#include <future>
#include <thread>
#include <stdint.h>
#include <stdlib.h>
// Implemented in the runtime for test purposes.
extern "C" bool Kotlin_Debugging_isThreadStateNative();
extern "C" void assertNativeThreadState() {
if (!Kotlin_Debugging_isThreadStateNative()) {
printf("Incorrect thread state. Expected native thread state.");
abort();
}
}
extern "C" void runInNewThread(void(*callback)(void)) {
std::thread t([callback]() {
callback();
});
t.join();
}
extern "C" void runInForeignThread(void(*callback)(void)) {
std::thread t([callback]() {
// This thread is not attached to the Kotlin runtime.
auto future = std::async(std::launch::async, callback);
// The machinery of the direct interop doesn't filter out a Kotlin exception thrown by the callback.
// The get() call will re-throw this exception.
future.get();
});
t.join();
}
@@ -1,21 +0,0 @@
language = C
---
#include <stdint.h>
void assertNativeThreadState();
void runCallback(void(*callback)(void)) {
assertNativeThreadState();
callback();
assertNativeThreadState();
}
int32_t answer() {
assertNativeThreadState();
return 42;
}
void runInNewThread(void(*callback)(void));
void runInForeignThread(void(*callback)(void));
@@ -1,59 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:OptIn(kotlin.native.runtime.NativeRuntimeApi::class)
import kotlin.native.runtime.Debugging
import kotlin.test.*
import kotlinx.cinterop.*
import threadStates.*
fun main() {
nativeCall()
callback()
nestedCalls()
directStaticCFunctionCall()
callbackOnSeparateThread()
}
fun assertRunnableThreadState() {
assertTrue(Debugging.isThreadStateRunnable)
}
fun nativeCall() {
answer()
assertRunnableThreadState()
}
fun callback() {
runCallback(staticCFunction { ->
assertRunnableThreadState()
})
assertRunnableThreadState()
}
fun nestedCalls() {
runCallback(staticCFunction { ->
assertRunnableThreadState()
answer()
Unit
})
assertRunnableThreadState()
}
fun directStaticCFunctionCall() {
val funPtr = staticCFunction { ->
assertRunnableThreadState()
}
assertRunnableThreadState()
funPtr()
assertRunnableThreadState()
}
fun callbackOnSeparateThread() {
runInNewThread(staticCFunction { ->
assertRunnableThreadState()
})
}
@@ -1,23 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class)
import kotlin.native.concurrent.*
import kotlin.native.runtime.Debugging
import kotlinx.cinterop.staticCFunction
import threadStates.*
fun main() {
val hook = { throwable: Throwable ->
print("${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}")
}
if (Platform.memoryModel != MemoryModel.EXPERIMENTAL) {
hook.freeze()
}
setUnhandledExceptionHook(hook)
runInNewThread(staticCFunction<Unit> { throw Error("Error") })
}
@@ -1,23 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@file:OptIn(FreezingIsDeprecated::class, kotlin.experimental.ExperimentalNativeApi::class, kotlin.native.runtime.NativeRuntimeApi::class)
import kotlin.native.concurrent.*
import kotlin.native.runtime.Debugging
import kotlinx.cinterop.staticCFunction
import threadStates.*
fun main() {
val hook = { throwable: Throwable ->
print("${throwable::class.simpleName}. Runnable state: ${Debugging.isThreadStateRunnable}")
}
if (Platform.memoryModel != MemoryModel.EXPERIMENTAL) {
hook.freeze()
}
setUnhandledExceptionHook(hook)
runInForeignThread(staticCFunction<Unit> { throw Error("Error") })
}