[K/N] Migrate more cinterop tests
^KT-61259
This commit is contained in:
committed by
Space Team
parent
8ed7e31b5f
commit
5f51f5e1fc
@@ -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") })
|
||||
}
|
||||
-23
@@ -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") })
|
||||
}
|
||||
Reference in New Issue
Block a user