From e2dbeae85ee4dbf08c2881877b93d44bbe1b0f80 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 28 Nov 2016 18:19:06 +0700 Subject: [PATCH] backend: add tests for try-catch-finally and throwing exceptions also add some tests for bugs with unreachable code handling and variables --- backend.native/tests/build.gradle | 120 ++++++++++++++++++ .../tests/codegen/controlflow/unreachable1.kt | 8 ++ .../tests/codegen/dataflow/scope1.kt | 9 ++ backend.native/tests/codegen/try/catch3.kt | 11 ++ backend.native/tests/codegen/try/catch4.kt | 15 +++ backend.native/tests/codegen/try/catch5.kt | 25 ++++ backend.native/tests/codegen/try/catch6.kt | 22 ++++ backend.native/tests/codegen/try/catch8.kt | 10 ++ backend.native/tests/codegen/try/finally1.kt | 10 ++ backend.native/tests/codegen/try/finally2.kt | 14 ++ backend.native/tests/codegen/try/finally3.kt | 19 +++ backend.native/tests/codegen/try/finally4.kt | 25 ++++ backend.native/tests/codegen/try/finally5.kt | 15 +++ backend.native/tests/codegen/try/finally6.kt | 16 +++ backend.native/tests/codegen/try/finally7.kt | 16 +++ backend.native/tests/codegen/try/finally8.kt | 18 +++ backend.native/tests/codegen/try/finally9.kt | 21 +++ backend.native/tests/codegen/try/try1.kt | 9 ++ backend.native/tests/codegen/try/try2.kt | 10 ++ backend.native/tests/codegen/try/try3.kt | 9 ++ backend.native/tests/codegen/try/try4.kt | 10 ++ .../tests/runtime/exceptions/catch1.kt | 16 +++ .../tests/runtime/exceptions/catch2.kt | 20 +++ .../tests/runtime/exceptions/catch7.kt | 14 ++ 24 files changed, 462 insertions(+) create mode 100644 backend.native/tests/codegen/controlflow/unreachable1.kt create mode 100644 backend.native/tests/codegen/dataflow/scope1.kt create mode 100644 backend.native/tests/codegen/try/catch3.kt create mode 100644 backend.native/tests/codegen/try/catch4.kt create mode 100644 backend.native/tests/codegen/try/catch5.kt create mode 100644 backend.native/tests/codegen/try/catch6.kt create mode 100644 backend.native/tests/codegen/try/catch8.kt create mode 100644 backend.native/tests/codegen/try/finally1.kt create mode 100644 backend.native/tests/codegen/try/finally2.kt create mode 100644 backend.native/tests/codegen/try/finally3.kt create mode 100644 backend.native/tests/codegen/try/finally4.kt create mode 100644 backend.native/tests/codegen/try/finally5.kt create mode 100644 backend.native/tests/codegen/try/finally6.kt create mode 100644 backend.native/tests/codegen/try/finally7.kt create mode 100644 backend.native/tests/codegen/try/finally8.kt create mode 100644 backend.native/tests/codegen/try/finally9.kt create mode 100644 backend.native/tests/codegen/try/try1.kt create mode 100644 backend.native/tests/codegen/try/try2.kt create mode 100644 backend.native/tests/codegen/try/try3.kt create mode 100644 backend.native/tests/codegen/try/try4.kt create mode 100644 backend.native/tests/runtime/exceptions/catch1.kt create mode 100644 backend.native/tests/runtime/exceptions/catch2.kt create mode 100644 backend.native/tests/runtime/exceptions/catch7.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 349958c0ab4..8f4d4bf1cbc 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -420,3 +420,123 @@ task array_list1(type: RunKonanTest) { source = "runtime/collections/array_list1.kt" } +task catch1(type: RunKonanTest) { + goldValue = "Before\nCaught Throwable\nDone\n" + source = "runtime/exceptions/catch1.kt" +} + +task catch2(type: RunKonanTest) { + goldValue = "Before\nCaught Error\nDone\n" + source = "runtime/exceptions/catch2.kt" +} + +task catch7(type: RunKonanTest) { + goldValue = "Error happens\n" + source = "runtime/exceptions/catch7.kt" +} + +task catch3(type: RunKonanTest) { + goldValue = "Before\nCaught Throwable\nDone\n" + source = "codegen/try/catch3.kt" +} + +task catch4(type: RunKonanTest) { + goldValue = "Before\nCaught Error\nDone\n" + source = "codegen/try/catch4.kt" +} + +task catch5(type: RunKonanTest) { + goldValue = "Before\nCaught Error\nDone\n" + source = "codegen/try/catch5.kt" +} + +task catch6(type: RunKonanTest) { + goldValue = "Before\nCaught Error\nDone\n" + source = "codegen/try/catch6.kt" +} + +task catch8(type: RunKonanTest) { + goldValue = "Error happens\n" + source = "codegen/try/catch8.kt" +} + +task finally1(type: RunKonanTest) { + goldValue = "Try\nFinally\nDone\n" + source = "codegen/try/finally1.kt" +} + +task finally2(type: RunKonanTest) { + goldValue = "Try\nCaught Error\nFinally\nDone\n" + source = "codegen/try/finally2.kt" +} + +task finally3(type: RunKonanTest) { + goldValue = "Try\nFinally\nCaught Error\nDone\n" + source = "codegen/try/finally3.kt" +} + +task finally4(type: RunKonanTest) { + goldValue = "Try\nCatch\nFinally\nCaught Exception\nDone\n" + source = "codegen/try/finally4.kt" +} + +task finally5(type: RunKonanTest) { + goldValue = "Done\nFinally\n0\n" + source = "codegen/try/finally5.kt" +} + +task finally6(type: RunKonanTest) { + goldValue = "Done\nFinally\n1\n" + source = "codegen/try/finally6.kt" +} + +task finally7(type: RunKonanTest) { + goldValue = "Done\nFinally\n1\n" + source = "codegen/try/finally7.kt" +} + +task finally8(type: RunKonanTest) { + goldValue = "Finally 1\nFinally 2\n42\n" + source = "codegen/try/finally8.kt" +} + +/* +TODO: enable after implementing break and continue +task finally9(type: RunKonanTest) { + goldValue = "Finally 1\nFinally 2\nAfter\n" + source = "codegen/try/finally9.kt" +} +*/ + +/* +TODO: enable after improving LLVM variable naming +task scope1(type: RunKonanTest) { + goldValue = "1\n" + source = "codegen/dataflow/scope1.kt" +} +*/ + +task try1(type: RunKonanTest) { + goldValue = "5\n" + source = "codegen/try/try1.kt" +} + +task try2(type: RunKonanTest) { + goldValue = "6\n" + source = "codegen/try/try2.kt" +} + +task try3(type: RunKonanTest) { + goldValue = "6\n" + source = "codegen/try/try3.kt" +} + +task try4(type: RunKonanTest) { + goldValue = "Try\n5\n" + source = "codegen/try/try4.kt" +} + +task unreachable1(type: RunKonanTest) { + goldValue = "1\n" + source = "codegen/controlflow/unreachable1.kt" +} diff --git a/backend.native/tests/codegen/controlflow/unreachable1.kt b/backend.native/tests/codegen/controlflow/unreachable1.kt new file mode 100644 index 00000000000..63032ff8950 --- /dev/null +++ b/backend.native/tests/codegen/controlflow/unreachable1.kt @@ -0,0 +1,8 @@ +fun main(args: Array) { + println(foo()) +} + +fun foo(): Int { + return 1 + println("After return") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/dataflow/scope1.kt b/backend.native/tests/codegen/dataflow/scope1.kt new file mode 100644 index 00000000000..f14eb27cf86 --- /dev/null +++ b/backend.native/tests/codegen/dataflow/scope1.kt @@ -0,0 +1,9 @@ +var b = true + +fun main(args: Array) { + var x = 1 + if (b) { + var x = 2 + } + println(x) +} diff --git a/backend.native/tests/codegen/try/catch3.kt b/backend.native/tests/codegen/try/catch3.kt new file mode 100644 index 00000000000..dd27ebe2672 --- /dev/null +++ b/backend.native/tests/codegen/try/catch3.kt @@ -0,0 +1,11 @@ +fun main(args : Array) { + try { + println("Before") + throw Error("Error happens") + println("After") + } catch (e: Throwable) { + println("Caught Throwable") + } + + println("Done") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/catch4.kt b/backend.native/tests/codegen/try/catch4.kt new file mode 100644 index 00000000000..f646e8e757c --- /dev/null +++ b/backend.native/tests/codegen/try/catch4.kt @@ -0,0 +1,15 @@ +fun main(args : Array) { + try { + println("Before") + throw Error("Error happens") + println("After") + } catch (e: Exception) { + println("Caught Exception") + } catch (e: Error) { + println("Caught Error") + } catch (e: Throwable) { + println("Caught Throwable") + } + + println("Done") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/catch5.kt b/backend.native/tests/codegen/try/catch5.kt new file mode 100644 index 00000000000..473734c8b49 --- /dev/null +++ b/backend.native/tests/codegen/try/catch5.kt @@ -0,0 +1,25 @@ +fun main(args : Array) { + try { + try { + println("Before") + foo() + println("After") + } catch (e: Exception) { + println("Caught Exception") + } + + println("After nested try") + + } catch (e: Error) { + println("Caught Error") + } catch (e: Throwable) { + println("Caught Throwable") + } + + println("Done") +} + +fun foo() { + throw Error("Error happens") + println("After in foo()") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/catch6.kt b/backend.native/tests/codegen/try/catch6.kt new file mode 100644 index 00000000000..e4711332f31 --- /dev/null +++ b/backend.native/tests/codegen/try/catch6.kt @@ -0,0 +1,22 @@ +fun main(args : Array) { + + try { + println("Before") + foo() + println("After") + } catch (e: Exception) { + println("Caught Exception") + } catch (e: Error) { + println("Caught Error") + } + + println("Done") +} + +fun foo() { + try { + throw Error("Error happens") + } catch (e: Exception) { + println("Caught Exception") + } +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/catch8.kt b/backend.native/tests/codegen/try/catch8.kt new file mode 100644 index 00000000000..49327992216 --- /dev/null +++ b/backend.native/tests/codegen/try/catch8.kt @@ -0,0 +1,10 @@ +fun main(args : Array) { + try { + throw Error("Error happens") + } catch (e: Throwable) { + val message = e.message + if (message != null) { + println(message) + } + } +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally1.kt b/backend.native/tests/codegen/try/finally1.kt new file mode 100644 index 00000000000..e2c1c257e39 --- /dev/null +++ b/backend.native/tests/codegen/try/finally1.kt @@ -0,0 +1,10 @@ +fun main(args : Array) { + + try { + println("Try") + } finally { + println("Finally") + } + + println("Done") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally2.kt b/backend.native/tests/codegen/try/finally2.kt new file mode 100644 index 00000000000..3edb2b73220 --- /dev/null +++ b/backend.native/tests/codegen/try/finally2.kt @@ -0,0 +1,14 @@ +fun main(args : Array) { + + try { + println("Try") + throw Error("Error happens") + println("After throw") + } catch (e: Error) { + println("Caught Error") + } finally { + println("Finally") + } + + println("Done") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally3.kt b/backend.native/tests/codegen/try/finally3.kt new file mode 100644 index 00000000000..7e6ab694d7d --- /dev/null +++ b/backend.native/tests/codegen/try/finally3.kt @@ -0,0 +1,19 @@ +fun main(args : Array) { + + try { + try { + println("Try") + throw Error("Error happens") + println("After throw") + } finally { + println("Finally") + } + + println("After nested try") + + } catch (e: Error) { + println("Caught Error") + } + + println("Done") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally4.kt b/backend.native/tests/codegen/try/finally4.kt new file mode 100644 index 00000000000..0a9a317c731 --- /dev/null +++ b/backend.native/tests/codegen/try/finally4.kt @@ -0,0 +1,25 @@ +fun main(args : Array) { + + try { + try { + println("Try") + throw Error("Error happens") + println("After throw") + } catch (e: Error) { + println("Catch") + throw Exception() + println("After throw") + } finally { + println("Finally") + } + + println("After nested try") + + } catch (e: Error) { + println("Caught Error") + } catch (e: Exception) { + println("Caught Exception") + } + + println("Done") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally5.kt b/backend.native/tests/codegen/try/finally5.kt new file mode 100644 index 00000000000..ebd72f51907 --- /dev/null +++ b/backend.native/tests/codegen/try/finally5.kt @@ -0,0 +1,15 @@ +fun main(args : Array) { + println(foo()) +} + +fun foo(): Int { + try { + println("Done") + return 0 + } finally { + println("Finally") + } + + println("After") + return 1 +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally6.kt b/backend.native/tests/codegen/try/finally6.kt new file mode 100644 index 00000000000..b60e13a0899 --- /dev/null +++ b/backend.native/tests/codegen/try/finally6.kt @@ -0,0 +1,16 @@ +fun main(args : Array) { + println(foo()) +} + +fun foo(): Int { + try { + println("Done") + return 0 + } finally { + println("Finally") + return 1 + } + + println("After") + return 2 +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally7.kt b/backend.native/tests/codegen/try/finally7.kt new file mode 100644 index 00000000000..b9d7e936ac3 --- /dev/null +++ b/backend.native/tests/codegen/try/finally7.kt @@ -0,0 +1,16 @@ +fun main(args : Array) { + println(foo()) +} + +fun foo(): Int { + try { + println("Done") + throw Error() + } finally { + println("Finally") + return 1 + } + + println("After") + return 2 +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally8.kt b/backend.native/tests/codegen/try/finally8.kt new file mode 100644 index 00000000000..d7b876e06d7 --- /dev/null +++ b/backend.native/tests/codegen/try/finally8.kt @@ -0,0 +1,18 @@ +fun main(args : Array) { + println(foo()) +} + +fun foo(): Int { + try { + try { + return 42 + } finally { + println("Finally 1") + } + } finally { + println("Finally 2") + } + + println("After") + return 2 +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/finally9.kt b/backend.native/tests/codegen/try/finally9.kt new file mode 100644 index 00000000000..46f75d4471d --- /dev/null +++ b/backend.native/tests/codegen/try/finally9.kt @@ -0,0 +1,21 @@ +fun main(args : Array) { + do { + try { + break + } finally { + println("Finally 1") + } + } while (false) + + var stop = false + while (!stop) { + try { + stop = true + continue + } finally { + println("Finally 2") + } + } + + println("After") +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/try1.kt b/backend.native/tests/codegen/try/try1.kt new file mode 100644 index 00000000000..e4f54d70fb7 --- /dev/null +++ b/backend.native/tests/codegen/try/try1.kt @@ -0,0 +1,9 @@ +fun main(args : Array) { + val x = try { + 5 + } catch (e: Throwable) { + 6 + } + + println(x) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/try2.kt b/backend.native/tests/codegen/try/try2.kt new file mode 100644 index 00000000000..f40fc73e7a0 --- /dev/null +++ b/backend.native/tests/codegen/try/try2.kt @@ -0,0 +1,10 @@ +fun main(args : Array) { + val x = try { + throw Error() + 5 + } catch (e: Throwable) { + 6 + } + + println(x) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/try3.kt b/backend.native/tests/codegen/try/try3.kt new file mode 100644 index 00000000000..5a5c1a41c88 --- /dev/null +++ b/backend.native/tests/codegen/try/try3.kt @@ -0,0 +1,9 @@ +fun main(args : Array) { + val x = try { + throw Error() + } catch (e: Throwable) { + 6 + } + + println(x) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/try/try4.kt b/backend.native/tests/codegen/try/try4.kt new file mode 100644 index 00000000000..800ebade889 --- /dev/null +++ b/backend.native/tests/codegen/try/try4.kt @@ -0,0 +1,10 @@ +fun main(args : Array) { + val x = try { + println("Try") + 5 + } catch (e: Throwable) { + throw e + } + + println(x) +} \ No newline at end of file diff --git a/backend.native/tests/runtime/exceptions/catch1.kt b/backend.native/tests/runtime/exceptions/catch1.kt new file mode 100644 index 00000000000..53e8b65ab0d --- /dev/null +++ b/backend.native/tests/runtime/exceptions/catch1.kt @@ -0,0 +1,16 @@ +fun main(args : Array) { + try { + println("Before") + foo() + println("After") + } catch (e: Throwable) { + println("Caught Throwable") + } + + println("Done") +} + +fun foo() { + throw Error("Error happens") + println("After in foo()") +} \ No newline at end of file diff --git a/backend.native/tests/runtime/exceptions/catch2.kt b/backend.native/tests/runtime/exceptions/catch2.kt new file mode 100644 index 00000000000..86bf162ffde --- /dev/null +++ b/backend.native/tests/runtime/exceptions/catch2.kt @@ -0,0 +1,20 @@ +fun main(args : Array) { + try { + println("Before") + foo() + println("After") + } catch (e: Exception) { + println("Caught Exception") + } catch (e: Error) { + println("Caught Error") + } catch (e: Throwable) { + println("Caught Throwable") + } + + println("Done") +} + +fun foo() { + throw Error("Error happens") + println("After in foo()") +} \ No newline at end of file diff --git a/backend.native/tests/runtime/exceptions/catch7.kt b/backend.native/tests/runtime/exceptions/catch7.kt new file mode 100644 index 00000000000..766f17a369f --- /dev/null +++ b/backend.native/tests/runtime/exceptions/catch7.kt @@ -0,0 +1,14 @@ +fun main(args : Array) { + try { + foo() + } catch (e: Throwable) { + val message = e.message + if (message != null) { + println(message) + } + } +} + +fun foo() { + throw Error("Error happens") +} \ No newline at end of file