[K/N] Migrate misc runtime/ tests to new testing infra ^KT-61259

This commit is contained in:
Alexander Shabalin
2023-10-30 11:47:39 +01:00
committed by Space Team
parent 364dcd30c8
commit fd1579186f
7 changed files with 170 additions and 179 deletions
@@ -0,0 +1,103 @@
/*
* Copyright 2010-2023 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.
*/
package test.kotlinx.cinterop
import kotlinx.cinterop.*
import kotlin.test.*
class AllocTest {
@Test
fun boolean() = memScoped {
assertEquals(true, alloc(true).value)
assertEquals(false, alloc(false).value)
}
@Test
fun byte() = memScoped<Unit> {
assertEquals(Byte.MIN_VALUE, alloc(Byte.MIN_VALUE).value)
assertEquals(Byte.MAX_VALUE, alloc(Byte.MAX_VALUE).value)
assertEquals(0.toByte(), alloc(0.toByte()).value)
assertEquals(1.toByte(), alloc(1.toByte()).value)
assertEquals(1, alloc<Byte>(1).value)
}
@Test
fun short() = memScoped<Unit> {
assertEquals(Short.MIN_VALUE, alloc(Short.MIN_VALUE).value)
assertEquals(Short.MAX_VALUE, alloc(Short.MAX_VALUE).value)
assertEquals(0.toShort(), alloc(0.toShort()).value)
assertEquals(2.toShort(), alloc(2.toShort()).value)
assertEquals(2, alloc<Short>(2).value)
}
@Test
fun int() = memScoped<Unit> {
assertEquals(Int.MIN_VALUE, alloc(Int.MIN_VALUE).value)
assertEquals(Int.MAX_VALUE, alloc(Int.MAX_VALUE).value)
assertEquals(0.toInt(), alloc(0.toInt()).value)
assertEquals(3.toInt(), alloc(3.toInt()).value)
assertEquals(3, alloc<Int>(3).value)
}
@Test
fun long() = memScoped<Unit> {
assertEquals(Long.MIN_VALUE, alloc(Long.MIN_VALUE).value)
assertEquals(Long.MAX_VALUE, alloc(Long.MAX_VALUE).value)
assertEquals(0L, alloc(0L).value)
assertEquals(4L, alloc(4L).value)
assertEquals(4, alloc<Long>(4).value)
}
@Test
fun uByte() = memScoped<Unit> {
assertEquals(UByte.MIN_VALUE, alloc(UByte.MIN_VALUE).value)
assertEquals(UByte.MAX_VALUE, alloc(UByte.MAX_VALUE).value)
assertEquals(5.toUByte(), alloc(5.toUByte()).value)
assertEquals(5u, alloc<UByte>(5u).value)
}
@Test
fun uShort() = memScoped<Unit> {
assertEquals(UShort.MIN_VALUE, alloc(UShort.MIN_VALUE).value)
assertEquals(UShort.MAX_VALUE, alloc(UShort.MAX_VALUE).value)
assertEquals(6.toUShort(), alloc(6.toUShort()).value)
assertEquals(6u, alloc<UShort>(6u).value)
}
@Test
fun uInt() = memScoped<Unit> {
assertEquals(UInt.MIN_VALUE, alloc(UInt.MIN_VALUE).value)
assertEquals(UInt.MAX_VALUE, alloc(UInt.MAX_VALUE).value)
assertEquals(7.toUInt(), alloc(7.toUInt()).value)
assertEquals(7u, alloc<UInt>(7u).value)
}
@Test
fun uLong() = memScoped<Unit> {
assertEquals(ULong.MIN_VALUE, alloc(ULong.MIN_VALUE).value)
assertEquals(ULong.MAX_VALUE, alloc(ULong.MAX_VALUE).value)
assertEquals(8uL, alloc(8uL).value)
assertEquals(8u, alloc<ULong>(8u).value)
}
@Test
fun float() = memScoped<Unit> {
assertEquals(Float.MIN_VALUE, alloc(Float.MIN_VALUE).value)
assertEquals(Float.MAX_VALUE, alloc(Float.MAX_VALUE).value)
assertEquals(0.0f, alloc(0.0f).value)
assertEquals(9.0f, alloc(9.0f).value)
assertEquals(9.0f, alloc<Float>(9.0f).value)
}
@Test
fun double() = memScoped<Unit> {
assertEquals(Double.MIN_VALUE, alloc(Double.MIN_VALUE).value)
assertEquals(Double.MAX_VALUE, alloc(Double.MAX_VALUE).value)
assertEquals(0.0, alloc(0.0).value)
assertEquals(10.0, alloc(10.0).value)
assertEquals(10.0, alloc<Double>(10.0).value)
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2023 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.
*/
package test.kotlinx.cinterop
import kotlinx.cinterop.*
import kotlin.native.concurrent.*
import kotlin.test.*
private class Box(val value: Int)
class StableRefTest {
@Test
fun crossThreadPassing() = withWorker {
val future = execute(TransferMode.SAFE, {}) {
StableRef.create(Box(42))
}
assertEquals(42, future.result.get().value)
}
@Test
fun crossThreadPassingAsPointer() = withWorker {
val mainThreadRef = StableRef.create(Box(42))
// Simulate this going through interop as raw C pointer.
val pointerValue: Long = mainThreadRef.asCPointer().toLong()
val future = execute(TransferMode.SAFE, { pointerValue }) {
val pointer: COpaquePointer = it.toCPointer()!!
val otherThreadRef: StableRef<Box> = pointer.asStableRef()
otherThreadRef.get().value
}
assertEquals(42, future.result)
}
}
@@ -0,0 +1,32 @@
/*
* Copyright 2010-2023 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.
*/
package test.native.ref
import kotlin.native.ref.WeakReference
import kotlin.test.*
class WeakReferenceTest {
@Test
fun string() {
val v = "Hello"
val weak = WeakReference(v)
assertEquals(v, weak.value)
}
@Test
fun int() {
val v = 0
val weak = WeakReference(v)
assertEquals(0, weak.value)
}
@Test
fun long() {
val v = Long.MAX_VALUE
val weak = WeakReference(v)
assertEquals(Long.MAX_VALUE, weak.value)
}
}