From 6aa3493de2e7bb84578e0b8007d96d585f895513 Mon Sep 17 00:00:00 2001 From: Igor Chevdar Date: Fri, 21 Apr 2017 13:49:22 +0300 Subject: [PATCH] Added some tests on coroutines --- backend.native/tests/build.gradle | 106 ++++++++++++++++++ .../coroutines/controlFlow_finally1.kt | 49 ++++++++ .../coroutines/controlFlow_finally2.kt | 50 +++++++++ .../coroutines/controlFlow_finally3.kt | 49 ++++++++ .../coroutines/controlFlow_finally4.kt | 49 ++++++++ .../codegen/coroutines/controlFlow_if1.kt | 43 +++++++ .../codegen/coroutines/controlFlow_if2.kt | 46 ++++++++ .../codegen/coroutines/controlFlow_inline1.kt | 32 ++++++ .../codegen/coroutines/controlFlow_inline2.kt | 33 ++++++ .../codegen/coroutines/controlFlow_inline3.kt | 46 ++++++++ .../coroutines/controlFlow_tryCatch1.kt | 48 ++++++++ .../coroutines/controlFlow_tryCatch2.kt | 47 ++++++++ .../coroutines/controlFlow_tryCatch3.kt | 53 +++++++++ .../coroutines/controlFlow_tryCatch4.kt | 54 +++++++++ .../coroutines/controlFlow_tryCatch5.kt | 55 +++++++++ .../codegen/coroutines/controlFlow_while1.kt | 56 +++++++++ .../codegen/coroutines/controlFlow_while2.kt | 56 +++++++++ .../tests/codegen/coroutines/correctOrder1.kt | 38 +++++++ .../tests/codegen/coroutines/returnsUnit1.kt | 37 ++++++ .../tests/codegen/coroutines/simple.kt | 27 +++++ .../tests/codegen/coroutines/withReceiver.kt | 29 +++++ 21 files changed, 1003 insertions(+) create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_finally1.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_finally2.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_finally3.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_finally4.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_if1.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_if2.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_inline1.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_inline2.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_inline3.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_tryCatch1.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_tryCatch2.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_tryCatch3.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_tryCatch4.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_tryCatch5.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_while1.kt create mode 100644 backend.native/tests/codegen/coroutines/controlFlow_while2.kt create mode 100644 backend.native/tests/codegen/coroutines/correctOrder1.kt create mode 100644 backend.native/tests/codegen/coroutines/returnsUnit1.kt create mode 100644 backend.native/tests/codegen/coroutines/simple.kt create mode 100644 backend.native/tests/codegen/coroutines/withReceiver.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 04cbdb16b90..a3e5afc5b3c 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -796,6 +796,112 @@ task lateinit_notInitialized(type: RunKonanTest) { source = "codegen/lateinit/notInitialized.kt" } +task coroutines_simple(type: RunKonanTest) { + goldValue = "42\n" + source = "codegen/coroutines/simple.kt" +} + +task coroutines_withReceiver(type: RunKonanTest) { + goldValue = "42\n" + source = "codegen/coroutines/withReceiver.kt" +} + +task coroutines_correctOrder1(type: RunKonanTest) { + goldValue = "f1\ns1\nf2\n160\n" + source = "codegen/coroutines/correctOrder1.kt" +} + +task coroutines_controlFlow_if1(type: RunKonanTest) { + goldValue = "f1\ns1\nf3\n84\n" + source = "codegen/coroutines/controlFlow_if1.kt" +} + +task coroutines_controlFlow_if2(type: RunKonanTest) { + goldValue = "f1\ns1\nf2\n43\n" + source = "codegen/coroutines/controlFlow_if2.kt" +} + +// Enable after finally blocks lowering. + +task coroutines_controlFlow_finally1(type: RunKonanTest) { + disabled = true + goldValue = "f1\ns1\n117\n" + source = "codegen/coroutines/controlFlow_finally1.kt" +} + +task coroutines_controlFlow_finally2(type: RunKonanTest) { + disabled = true + goldValue = "f1\ns1\n117\n" + source = "codegen/coroutines/controlFlow_finally2.kt" +} + +task coroutines_controlFlow_finally3(type: RunKonanTest) { + disabled = true + goldValue = "f1\ns1\n117\n" + source = "codegen/coroutines/controlFlow_finally3.kt" +} + +task coroutines_controlFlow_finally4(type: RunKonanTest) { + disabled = true + goldValue = "s1\nfinally\n117\n" + source = "codegen/coroutines/controlFlow_finally4.kt" +} + +task coroutines_controlFlow_inline1(type: RunKonanTest) { + goldValue = "42\n" + source = "codegen/coroutines/controlFlow_inline1.kt" +} + +task coroutines_controlFlow_inline2(type: RunKonanTest) { + goldValue = "s1\n42\n" + source = "codegen/coroutines/controlFlow_inline2.kt" +} + +task coroutines_controlFlow_inline3(type: RunKonanTest) { + goldValue = "f1\ns1\n42\n" + source = "codegen/coroutines/controlFlow_inline3.kt" +} + +task coroutines_controlFlow_tryCatch1(type: RunKonanTest) { + goldValue = "s1\n42\n" + source = "codegen/coroutines/controlFlow_tryCatch1.kt" +} + +task coroutines_controlFlow_tryCatch2(type: RunKonanTest) { + goldValue = "s1\n42\n" + source = "codegen/coroutines/controlFlow_tryCatch2.kt" +} + +task coroutines_controlFlow_tryCatch3(type: RunKonanTest) { + goldValue = "s2\nf2\n1\n" + source = "codegen/coroutines/controlFlow_tryCatch3.kt" +} + +task coroutines_controlFlow_tryCatch4(type: RunKonanTest) { + goldValue = "s2\nf2\n1\n" + source = "codegen/coroutines/controlFlow_tryCatch4.kt" +} + +task coroutines_controlFlow_tryCatch5(type: RunKonanTest) { + goldValue = "s2\ns1\nError\n42\n" + source = "codegen/coroutines/controlFlow_tryCatch5.kt" +} + +task coroutines_controlFlow_while1(type: RunKonanTest) { + goldValue = "s3\ns3\ns3\ns3\n3\n" + source = "codegen/coroutines/controlFlow_while1.kt" +} + +task coroutines_controlFlow_while2(type: RunKonanTest) { + goldValue = "s3\ns3\ns3\n3\n" + source = "codegen/coroutines/controlFlow_while2.kt" +} + +task coroutines_returnsUnit1(type: RunKonanTest) { + goldValue = "117\ns1\n0\n" + source = "codegen/coroutines/returnsUnit1.kt" +} + task AbstractMutableCollection(type: RunKonanTest) { expectedExitStatus = 0 source = "runtime/collections/AbstractMutableCollection.kt" diff --git a/backend.native/tests/codegen/coroutines/controlFlow_finally1.kt b/backend.native/tests/codegen/coroutines/controlFlow_finally1.kt new file mode 100644 index 00000000000..bab264a3993 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_finally1.kt @@ -0,0 +1,49 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = try { + f1() + } catch (t: Throwable) { + f2() + } finally { + s1() + } + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_finally2.kt b/backend.native/tests/codegen/coroutines/controlFlow_finally2.kt new file mode 100644 index 00000000000..ea07c1c3de2 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_finally2.kt @@ -0,0 +1,50 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + val x = try { + f1() + } catch (t: Throwable) { + f2() + } finally { + s1() + } + result = x + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_finally3.kt b/backend.native/tests/codegen/coroutines/controlFlow_finally3.kt new file mode 100644 index 00000000000..69de84b4935 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_finally3.kt @@ -0,0 +1,49 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + try { + result = f1() + } catch (t: Throwable) { + result = f2() + } finally { + s1() + } + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_finally4.kt b/backend.native/tests/codegen/coroutines/controlFlow_finally4.kt new file mode 100644 index 00000000000..7b6c4ca709f --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_finally4.kt @@ -0,0 +1,49 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + try { + result = s1() + } catch (t: Throwable) { + result = f2() + } finally { + println("finally") + } + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_if1.kt b/backend.native/tests/codegen/coroutines/controlFlow_if1.kt new file mode 100644 index 00000000000..1cc96abd2a8 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_if1.kt @@ -0,0 +1,43 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = f3(if (f1() > 100) s1() else f2(), 42) + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_if2.kt b/backend.native/tests/codegen/coroutines/controlFlow_if2.kt new file mode 100644 index 00000000000..9ee2feadc5a --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_if2.kt @@ -0,0 +1,46 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + if (f1() > 100) + result = s1() + f2() + else + result = 42 + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_inline1.kt b/backend.native/tests/codegen/coroutines/controlFlow_inline1.kt new file mode 100644 index 00000000000..a21e93077c2 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_inline1.kt @@ -0,0 +1,32 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +inline suspend fun inline_s2(): Int { + return 42 +} + +fun main(args: Array) { + var result = 0 + + builder { + result = inline_s2() + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_inline2.kt b/backend.native/tests/codegen/coroutines/controlFlow_inline2.kt new file mode 100644 index 00000000000..b31935883f4 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_inline2.kt @@ -0,0 +1,33 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +inline suspend fun inline_s2(): Int { + var x = s1() + return x +} + +fun main(args: Array) { + var result = 0 + + builder { + result = inline_s2() + } + + println(result) +} diff --git a/backend.native/tests/codegen/coroutines/controlFlow_inline3.kt b/backend.native/tests/codegen/coroutines/controlFlow_inline3.kt new file mode 100644 index 00000000000..fb2cfb70513 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_inline3.kt @@ -0,0 +1,46 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +inline suspend fun inline_s2(): Int { + var x = 0 + if (f1() > 0) + x = s1() + else x = f2() + return x +} + +fun main(args: Array) { + var result = 0 + + builder { + result = inline_s2() + } + + println(result) +} diff --git a/backend.native/tests/codegen/coroutines/controlFlow_tryCatch1.kt b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch1.kt new file mode 100644 index 00000000000..29e7828cedc --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch1.kt @@ -0,0 +1,48 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + val x = try { + s1() + } catch (t: Throwable) { + f2() + } + result = x + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_tryCatch2.kt b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch2.kt new file mode 100644 index 00000000000..291453cf780 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch2.kt @@ -0,0 +1,47 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = try { + s1() + } catch (t: Throwable) { + f2() + } + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_tryCatch3.kt b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch3.kt new file mode 100644 index 00000000000..bcc84ca968c --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch3.kt @@ -0,0 +1,53 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +suspend fun s2(): Int = suspendCoroutineOrReturn { x -> + println("s2") + x.resumeWithException(Error()) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = try { + s2() + } catch (t: Throwable) { + f2() + } + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_tryCatch4.kt b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch4.kt new file mode 100644 index 00000000000..a87aa67c7d8 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch4.kt @@ -0,0 +1,54 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +suspend fun s2(): Int = suspendCoroutineOrReturn { x -> + println("s2") + x.resumeWithException(Error()) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + val x = try { + s2() + } catch (t: Throwable) { + f2() + } + result = x + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_tryCatch5.kt b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch5.kt new file mode 100644 index 00000000000..3d82c0c945f --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_tryCatch5.kt @@ -0,0 +1,55 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +suspend fun s2(): Int = suspendCoroutineOrReturn { x -> + println("s2") + x.resumeWithException(Error("Error")) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = try { + s2() + } catch (t: Throwable) { + val x = s1() + println(t.message) + x + } + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_while1.kt b/backend.native/tests/codegen/coroutines/controlFlow_while1.kt new file mode 100644 index 00000000000..847cfd14491 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_while1.kt @@ -0,0 +1,56 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +suspend fun s2(): Int = suspendCoroutineOrReturn { x -> + println("s2") + x.resumeWithException(Error("Error")) + COROUTINE_SUSPENDED +} + +suspend fun s3(value: Int): Int = suspendCoroutineOrReturn { x -> + println("s3") + x.resume(value) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + while (s3(result) < 3) + ++result + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/controlFlow_while2.kt b/backend.native/tests/codegen/coroutines/controlFlow_while2.kt new file mode 100644 index 00000000000..c8d085fb70c --- /dev/null +++ b/backend.native/tests/codegen/coroutines/controlFlow_while2.kt @@ -0,0 +1,56 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +suspend fun s2(): Int = suspendCoroutineOrReturn { x -> + println("s2") + x.resumeWithException(Error("Error")) + COROUTINE_SUSPENDED +} + +suspend fun s3(value: Int): Int = suspendCoroutineOrReturn { x -> + println("s3") + x.resume(value) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun f3(x: Int, y: Int): Int { + println("f3") + return x + y +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + while (result < 3) + result = s3(result) + 1 + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/correctOrder1.kt b/backend.native/tests/codegen/coroutines/correctOrder1.kt new file mode 100644 index 00000000000..c98946e8287 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/correctOrder1.kt @@ -0,0 +1,38 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Int = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(42) + COROUTINE_SUSPENDED +} + +fun f1(): Int { + println("f1") + return 117 +} + +fun f2(): Int { + println("f2") + return 1 +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = f1() + s1() + f2() + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/returnsUnit1.kt b/backend.native/tests/codegen/coroutines/returnsUnit1.kt new file mode 100644 index 00000000000..41fe6e70499 --- /dev/null +++ b/backend.native/tests/codegen/coroutines/returnsUnit1.kt @@ -0,0 +1,37 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun s1(): Unit = suspendCoroutineOrReturn { x -> + println("s1") + x.resume(Unit) + COROUTINE_SUSPENDED +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +inline suspend fun inline_s2(x: Int): Unit { + println(x) + s1() +} + +suspend fun s3(x: Int) { + inline_s2(x) +} + +fun main(args: Array) { + var result = 0 + + builder { + s3(117) + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/simple.kt b/backend.native/tests/codegen/coroutines/simple.kt new file mode 100644 index 00000000000..f0d4dd92c9f --- /dev/null +++ b/backend.native/tests/codegen/coroutines/simple.kt @@ -0,0 +1,27 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +suspend fun suspendHere(): Int = suspendCoroutineOrReturn { x -> + x.resume(42) + COROUTINE_SUSPENDED +} + +fun builder(c: suspend () -> Unit) { + c.startCoroutine(EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = suspendHere() + } + + println(result) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/coroutines/withReceiver.kt b/backend.native/tests/codegen/coroutines/withReceiver.kt new file mode 100644 index 00000000000..b8b9f81dd1d --- /dev/null +++ b/backend.native/tests/codegen/coroutines/withReceiver.kt @@ -0,0 +1,29 @@ +import kotlin.coroutines.experimental.* +import kotlin.coroutines.experimental.intrinsics.* + +open class EmptyContinuation(override val context: CoroutineContext = EmptyCoroutineContext) : Continuation { + companion object : EmptyContinuation() + override fun resume(value: Any?) {} + override fun resumeWithException(exception: Throwable) { throw exception } +} + +class Controller { + suspend fun suspendHere(): Int = suspendCoroutineOrReturn { x -> + x.resume(42) + COROUTINE_SUSPENDED + } +} + +fun builder(c: suspend Controller.() -> Unit) { + c.startCoroutine(Controller(), EmptyContinuation) +} + +fun main(args: Array) { + var result = 0 + + builder { + result = suspendHere() + } + + println(result) +} \ No newline at end of file