Files
kotlin-fork/compiler/testData/codegen/boxInline/suspend/inlineSuspendContinuation.kt
T
Ilmir Usmanov f60787d57c Move coroutines to kotlin.coroutines package: tests
Introduce COMMON_COROUTINES_TEST directive.
Every test with this directive is run twice: one time with
language version 1.2 and kotlin.coroutines.experimental package
and the other time with language version 1.3 and kotlin.coroutines
package. Each run is a separate method: with suffixes _1_2 and _1_3
respectively.
However, since codegen of release coroutines is not supported in JS
backend, we generate only one method: with suffix _1_2.
 #KT-23362
2018-04-23 21:51:59 +03:00

114 lines
2.5 KiB
Kotlin
Vendored

// FILE: test.kt
// COMMON_COROUTINES_TEST
// WITH_RUNTIME
// NO_CHECK_LAMBDA_INLINING
suspend inline fun test1(c: () -> Unit) {
c()
}
suspend inline fun test2(c: suspend () -> Unit) {
c()
}
suspend inline fun test3(crossinline c: suspend() -> Unit) {
c()
}
suspend inline fun test4(crossinline c: suspend() -> Unit) {
val l : suspend () -> Unit = { c() }
l()
}
interface SuspendRunnable {
suspend fun run()
}
suspend inline fun test5(crossinline c: suspend() -> Unit) {
val sr = object : SuspendRunnable {
override suspend fun run() {
c()
}
}
sr.run()
}
// FILE: box.kt
// COMMON_COROUTINES_TEST
import COROUTINES_PACKAGE.*
import COROUTINES_PACKAGE.intrinsics.*
import COROUTINES_PACKAGE.jvm.internal.*
object EmptyContinuation: Continuation<Unit> {
override val context: CoroutineContext
get() = EmptyCoroutineContext
override fun resume(value: Unit) {
}
override fun resumeWithException(exception: Throwable) {
throw exception
}
}
fun builder(c: suspend () -> Unit) {
c.startCoroutine(EmptyContinuation)
}
var continuationChanged = true
var savedContinuation: Continuation<Unit>? = null
suspend inline fun saveContinuation() = suspendCoroutineUninterceptedOrReturn<Unit> { c ->
savedContinuation = c
Unit
}
suspend inline fun checkContinuation(continuation: Continuation<Unit>) = suspendCoroutineUninterceptedOrReturn<Unit> { c ->
continuationChanged = (continuation !== c)
Unit
}
fun box() : String {
builder {
saveContinuation()
test1 {
checkContinuation(savedContinuation!!)
}
}
if (continuationChanged) return "FAIL 1"
continuationChanged = true
builder {
saveContinuation()
test2 {
checkContinuation(savedContinuation!!)
}
}
if (continuationChanged) return "FAIL 2"
continuationChanged = true
builder {
saveContinuation()
test3 {
checkContinuation(savedContinuation!!)
}
}
if (continuationChanged) return "FAIL 3"
continuationChanged = false
builder {
saveContinuation()
test4 {
checkContinuation(savedContinuation!!)
}
}
if (!continuationChanged) return "FAIL 4"
continuationChanged = false
builder {
saveContinuation()
test5 {
checkContinuation(savedContinuation!!)
}
}
if (!continuationChanged) return "FAIL 5"
return "OK"
}