From fd1579186fac2460237afb582dd23867e7bf0cdf Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Mon, 30 Oct 2023 11:47:39 +0100 Subject: [PATCH] [K/N] Migrate misc runtime/ tests to new testing infra ^KT-61259 --- .../backend.native/tests/build.gradle | 8 -- .../runtime/interop/interop_alloc_value.kt | 98 ----------------- .../memory/stable_ref_cross_thread_check.kt | 61 ----------- .../tests/runtime/memory/weak1.kt | 12 -- .../test/kotlinx.cinterop/AllocTest.kt | 103 ++++++++++++++++++ .../test/kotlinx.cinterop/StableRefTest.kt | 35 ++++++ .../test/native/ref/WeakReferenceTest.kt | 32 ++++++ 7 files changed, 170 insertions(+), 179 deletions(-) delete mode 100644 kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt delete mode 100644 kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt create mode 100644 kotlin-native/runtime/test/kotlinx.cinterop/AllocTest.kt create mode 100644 kotlin-native/runtime/test/kotlinx.cinterop/StableRefTest.kt create mode 100644 kotlin-native/runtime/test/native/ref/WeakReferenceTest.kt diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index c7d213468ea..f14078fbc99 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -2880,10 +2880,6 @@ standaloneTest("memory_only_gc") { source = "runtime/memory/only_gc.kt" } -tasks.register("memory_stable_ref_cross_thread_check", KonanLocalTest) { - source = "runtime/memory/stable_ref_cross_thread_check.kt" -} - standaloneTest("leakMemory") { source = "runtime/memory/leak_memory.kt" } @@ -4277,10 +4273,6 @@ tasks.register("interop_sourceCodeStruct", KonanLocalTest) { source = "codegen/intrinsics/interop_sourceCodeStruct.kt" } -tasks.register("interop_alloc_value", KonanLocalTest) { - source = "runtime/interop/interop_alloc_value.kt" -} - standaloneTest("isExperimentalMM") { source = "codegen/intrinsics/isExperimentalMM.kt" flags = [ "-tr" ] diff --git a/kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt b/kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt deleted file mode 100644 index 01f3bafd42b..00000000000 --- a/kotlin-native/backend.native/tests/runtime/interop/interop_alloc_value.kt +++ /dev/null @@ -1,98 +0,0 @@ -@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) - -package runtime.interop.interop_alloc_value - -import kotlinx.cinterop.* -import kotlin.test.* - -@Test -fun testBoolean() = memScoped { - assertEquals(true, alloc(true).value) - assertEquals(false, alloc(false).value) -} - -@Test -fun testByte() = memScoped { - 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(1).value) -} - -@Test -fun testShort() = memScoped { - 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(2).value) -} - -@Test -fun testInt() = memScoped { - 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(3).value) -} - -@Test -fun testLong() = memScoped { - 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(4).value) -} - -@Test -fun testUByte() = memScoped { - 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(5u).value) -} - -@Test -fun testUShort() = memScoped { - 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(6u).value) -} - -@Test -fun testUInt() = memScoped { - 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(7u).value) -} - -@Test -fun testULong() = memScoped { - 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(8u).value) -} - -@Test -fun testFloat() = memScoped { - 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(9.0f).value) -} - -@Test -fun testDouble() = memScoped { - 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(10.0).value) -} diff --git a/kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt b/kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt deleted file mode 100644 index 4dc2ee2b568..00000000000 --- a/kotlin-native/backend.native/tests/runtime/memory/stable_ref_cross_thread_check.kt +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Copyright 2010-2020 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(kotlin.experimental.ExperimentalNativeApi::class, FreezingIsDeprecated::class, ObsoleteWorkersApi::class, kotlinx.cinterop.ExperimentalForeignApi::class) - -package runtime.memory.stable_ref_cross_thread_check - -import kotlin.test.* - -import kotlin.native.concurrent.* -import kotlinx.cinterop.* - -class Holder(val value: Int) - -@Test -fun runTest1() { - val worker = Worker.start() - - val future = worker.execute(TransferMode.SAFE, { }) { - StableRef.create(Holder(42)) - } - val ref = future.result - if (kotlin.native.Platform.memoryModel == kotlin.native.MemoryModel.EXPERIMENTAL) { - val value = ref.get() - assertEquals(value.value, 42) - } else { - assertFailsWith { - val value = ref.get() - println(value.value) - } - } - - worker.requestTermination().result -} - -@Test -fun runTest2() { - val worker = Worker.start() - - val mainThreadRef = StableRef.create(Holder(42)) - // Simulate this going through interop as raw C pointer. - val pointerValue: Long = mainThreadRef.asCPointer().toLong() - val future = worker.execute(TransferMode.SAFE, { pointerValue }) { - val pointer: COpaquePointer = it.toCPointer()!! - if (kotlin.native.Platform.memoryModel == kotlin.native.MemoryModel.EXPERIMENTAL) { - val otherThreadRef: StableRef = pointer.asStableRef() - assertEquals(otherThreadRef.get().value, 42) - } else { - assertFailsWith { - // Even attempting to convert a pointer to StableRef should fail. - val otherThreadRef: StableRef = pointer.asStableRef() - println(otherThreadRef.get().value) - } - } - Unit - } - future.result - - worker.requestTermination().result -} diff --git a/kotlin-native/backend.native/tests/runtime/memory/weak1.kt b/kotlin-native/backend.native/tests/runtime/memory/weak1.kt index 754dd2ef368..813b0e0c09e 100644 --- a/kotlin-native/backend.native/tests/runtime/memory/weak1.kt +++ b/kotlin-native/backend.native/tests/runtime/memory/weak1.kt @@ -20,15 +20,3 @@ class Node(var next: Node?) kotlin.native.ref.WeakReference(node1) println("OK") } - -@Test fun runTest2() { - val string = "Hello" - val refString = WeakReference(string) - assertEquals(string, refString.value) - val zero = 0 - val refZero = WeakReference(zero) - assertEquals(0, refZero.value) - val long = Long.MAX_VALUE - val refLong = WeakReference(long) - assertEquals(Long.MAX_VALUE, refLong.value) -} diff --git a/kotlin-native/runtime/test/kotlinx.cinterop/AllocTest.kt b/kotlin-native/runtime/test/kotlinx.cinterop/AllocTest.kt new file mode 100644 index 00000000000..87f3a81f1ef --- /dev/null +++ b/kotlin-native/runtime/test/kotlinx.cinterop/AllocTest.kt @@ -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 { + 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(1).value) + } + + @Test + fun short() = memScoped { + 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(2).value) + } + + @Test + fun int() = memScoped { + 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(3).value) + } + + @Test + fun long() = memScoped { + 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(4).value) + } + + @Test + fun uByte() = memScoped { + 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(5u).value) + } + + @Test + fun uShort() = memScoped { + 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(6u).value) + } + + @Test + fun uInt() = memScoped { + 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(7u).value) + } + + @Test + fun uLong() = memScoped { + 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(8u).value) + } + + @Test + fun float() = memScoped { + 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(9.0f).value) + } + + @Test + fun double() = memScoped { + 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(10.0).value) + } +} \ No newline at end of file diff --git a/kotlin-native/runtime/test/kotlinx.cinterop/StableRefTest.kt b/kotlin-native/runtime/test/kotlinx.cinterop/StableRefTest.kt new file mode 100644 index 00000000000..bfee3573dc3 --- /dev/null +++ b/kotlin-native/runtime/test/kotlinx.cinterop/StableRefTest.kt @@ -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 = pointer.asStableRef() + otherThreadRef.get().value + } + assertEquals(42, future.result) + } +} \ No newline at end of file diff --git a/kotlin-native/runtime/test/native/ref/WeakReferenceTest.kt b/kotlin-native/runtime/test/native/ref/WeakReferenceTest.kt new file mode 100644 index 00000000000..395b481ce58 --- /dev/null +++ b/kotlin-native/runtime/test/native/ref/WeakReferenceTest.kt @@ -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) + } +} \ No newline at end of file