[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)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user