[K/N][tests] Migrate various runtime/ tests ^KT-61259
This commit is contained in:
committed by
Space Team
parent
7ad4e58a7a
commit
d88092aa94
@@ -0,0 +1,16 @@
|
||||
import kotlin.test.*
|
||||
import kotlin.native.concurrent.*
|
||||
|
||||
enum class A {
|
||||
A, B
|
||||
}
|
||||
|
||||
data class Foo(val kind: A)
|
||||
|
||||
// Enums are shared between threads so identity should be kept.
|
||||
fun box(): String = withWorker {
|
||||
val result = execute(TransferMode.SAFE, { Foo(A.B) }) { input ->
|
||||
input.kind === A.B
|
||||
}.result
|
||||
return if (result) "OK" else "FAIL"
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
fun box(): String {
|
||||
foo(false)
|
||||
try {
|
||||
foo(true)
|
||||
} catch (e: Error) {
|
||||
return "OK"
|
||||
}
|
||||
return "FAIL"
|
||||
}
|
||||
|
||||
private fun foo(b: Boolean): Any {
|
||||
var result = Any()
|
||||
if (b) {
|
||||
throw Error()
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* 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(ObsoleteWorkersApi::class)
|
||||
package runtime.basic.initializers6
|
||||
|
||||
import kotlin.test.*
|
||||
import kotlin.concurrent.AtomicInt
|
||||
import kotlin.concurrent.*
|
||||
import kotlin.native.concurrent.*
|
||||
|
||||
val aWorkerId = AtomicInt(0)
|
||||
val bWorkersCount = 3
|
||||
|
||||
val aWorkerUnlocker = AtomicInt(0)
|
||||
val bWorkerUnlocker = AtomicInt(0)
|
||||
|
||||
object A {
|
||||
init {
|
||||
// Must be called by aWorker only.
|
||||
assertEquals(aWorkerId.value, Worker.current.id)
|
||||
// Only allow b workers to run, when a worker has started initialization.
|
||||
bWorkerUnlocker.incrementAndGet()
|
||||
// Only proceed with initialization, when all b workers have started executing.
|
||||
while (aWorkerUnlocker.value < bWorkersCount) {}
|
||||
// And now wait a bit, to increase probability of races.
|
||||
Worker.current.park(100 * 1000L)
|
||||
}
|
||||
val a = produceA()
|
||||
val b = produceB()
|
||||
}
|
||||
|
||||
fun produceA(): String {
|
||||
// Must've been called by aWorker only.
|
||||
assertEquals(aWorkerId.value, Worker.current.id)
|
||||
return "A"
|
||||
}
|
||||
|
||||
fun produceB(): String {
|
||||
// Must've been called by aWorker only.
|
||||
assertEquals(aWorkerId.value, Worker.current.id)
|
||||
// Also check that it's ok to get A.a while initializing A.b.
|
||||
return "B+${A.a}"
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
val aWorker = Worker.start()
|
||||
aWorkerId.value = aWorker.id
|
||||
val bWorkers = Array(bWorkersCount, { _ -> Worker.start() })
|
||||
|
||||
val aFuture = aWorker.execute(TransferMode.SAFE, {}, {
|
||||
A.b
|
||||
})
|
||||
val bFutures = Array(bWorkers.size, {
|
||||
bWorkers[it].execute(TransferMode.SAFE, {}, {
|
||||
// Wait until A has started to initialize.
|
||||
while (bWorkerUnlocker.value < 1) {}
|
||||
// Now allow A initialization to continue.
|
||||
aWorkerUnlocker.incrementAndGet()
|
||||
// And this should not've tried to init A itself.
|
||||
A.a + A.b
|
||||
})
|
||||
})
|
||||
|
||||
for (future in bFutures) {
|
||||
assertEquals("AB+A", future.result)
|
||||
}
|
||||
assertEquals("B+A", aFuture.result)
|
||||
|
||||
for (worker in bWorkers) {
|
||||
worker.requestTermination().result
|
||||
}
|
||||
aWorker.requestTermination().result
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import kotlin.test.*
|
||||
|
||||
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
|
||||
@kotlin.native.internal.CanBePrecreated
|
||||
object CompileTime {
|
||||
|
||||
const val int = Int.MIN_VALUE
|
||||
const val byte = Byte.MIN_VALUE
|
||||
const val short = Short.MIN_VALUE
|
||||
const val long = Long.MIN_VALUE
|
||||
const val boolean = true
|
||||
const val float = 1.0f
|
||||
const val double = 1.0
|
||||
const val char = Char.MIN_VALUE
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
assertEquals(Int.MIN_VALUE, CompileTime.int)
|
||||
assertEquals(Byte.MIN_VALUE, CompileTime.byte)
|
||||
assertEquals(Short.MIN_VALUE, CompileTime.short)
|
||||
assertEquals(Long.MIN_VALUE, CompileTime.long)
|
||||
assertEquals(true, CompileTime.boolean)
|
||||
assertEquals(1.0f, CompileTime.float)
|
||||
assertEquals(1.0, CompileTime.double)
|
||||
assertEquals(Char.MIN_VALUE, CompileTime.char)
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import kotlin.test.*
|
||||
|
||||
private class Integer(val value: Int) {
|
||||
operator fun inc() = Integer(value + 1)
|
||||
}
|
||||
|
||||
private fun foo(x: Any, y: Any) {
|
||||
x.use()
|
||||
y.use()
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
var x = Integer(0)
|
||||
|
||||
for (i in 0..1) {
|
||||
val c = Integer(0)
|
||||
if (i == 0) x = c
|
||||
}
|
||||
|
||||
// x refcount is 1.
|
||||
|
||||
foo(x, ++x)
|
||||
return "OK"
|
||||
}
|
||||
|
||||
private fun Any?.use() {
|
||||
var x = this
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
var x = Any()
|
||||
|
||||
for (i in 0..1) {
|
||||
val c = Any()
|
||||
if (i == 0) x = c
|
||||
}
|
||||
|
||||
// x refcount is 1.
|
||||
|
||||
val y = try {
|
||||
x
|
||||
} finally {
|
||||
x = Any()
|
||||
}
|
||||
|
||||
y.use()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
private fun Any?.use() {
|
||||
var x = this
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
foo().use()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
private fun foo(): Any {
|
||||
var x = Any()
|
||||
|
||||
for (i in 0..1) {
|
||||
val c = Any()
|
||||
if (i == 0) x = c
|
||||
}
|
||||
|
||||
// x refcount is 1.
|
||||
|
||||
try {
|
||||
return x
|
||||
} finally {
|
||||
x = Any()
|
||||
}
|
||||
}
|
||||
|
||||
private fun Any?.use() {
|
||||
var x = this
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
import kotlin.test.*
|
||||
|
||||
fun box(): String {
|
||||
var x = Error()
|
||||
|
||||
for (i in 0..1) {
|
||||
val c = Error()
|
||||
if (i == 0) x = c
|
||||
}
|
||||
|
||||
// x refcount is 1.
|
||||
|
||||
try {
|
||||
try {
|
||||
throw x
|
||||
} finally {
|
||||
x = Error()
|
||||
}
|
||||
} catch (e: Error) {
|
||||
e.use()
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
|
||||
private fun Any?.use() {
|
||||
var x = this
|
||||
}
|
||||
Reference in New Issue
Block a user