From 16ca88229822161010d9777ab842601e78bf2b86 Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 23 Jan 2017 11:04:42 +0700 Subject: [PATCH] backend/tests: add cast_null and unchecked_cast3 --- backend.native/tests/build.gradle | 10 ++++ .../tests/codegen/basics/cast_null.kt | 48 +++++++++++++++++++ .../tests/codegen/basics/unchecked_cast3.kt | 35 ++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 backend.native/tests/codegen/basics/cast_null.kt create mode 100644 backend.native/tests/codegen/basics/unchecked_cast3.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 7b54c0e32d0..36dc91b399e 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -212,6 +212,11 @@ task cast_simple(type: RunKonanTest) { source = "codegen/basics/cast_simple.kt" } +task cast_null(type: RunKonanTest) { + goldValue = "Ok\n" + source = "codegen/basics/cast_null.kt" +} + task unchecked_cast1(type: RunKonanTest) { goldValue = "17\n17\n42\n42\n" source = "codegen/basics/unchecked_cast1.kt" @@ -223,6 +228,11 @@ task unchecked_cast2(type: RunKonanTest) { source = "codegen/basics/unchecked_cast2.kt" } +task unchecked_cast3(type: RunKonanTest) { + goldValue = "Ok\n" + source = "codegen/basics/unchecked_cast3.kt" +} + task null_check(type: RunKonanTest) { source = "codegen/basics/null_check.kt" } diff --git a/backend.native/tests/codegen/basics/cast_null.kt b/backend.native/tests/codegen/basics/cast_null.kt new file mode 100644 index 00000000000..cb76a559f78 --- /dev/null +++ b/backend.native/tests/codegen/basics/cast_null.kt @@ -0,0 +1,48 @@ +fun main(args: Array) { + testCast(null, false) + testCastToNullable(null, true) + testCastToNullable(Test(), true) + testCastToNullable("", false) + testCastNotNullableToNullable(Test(), true) + testCastNotNullableToNullable("", false) + + println("Ok") +} + +class Test + +fun ensure(b: Boolean) { + if (!b) { + println("Error") + } +} + +fun testCast(x: Any?, expectSuccess: Boolean) { + try { + x as Test + } catch (e: Throwable) { + ensure(!expectSuccess) + return + } + ensure(expectSuccess) +} + +fun testCastToNullable(x: Any?, expectSuccess: Boolean) { + try { + x as Test? + } catch (e: Throwable) { + ensure(!expectSuccess) + return + } + ensure(expectSuccess) +} + +fun testCastNotNullableToNullable(x: Any, expectSuccess: Boolean) { + try { + x as Test? + } catch (e: Throwable) { + ensure(!expectSuccess) + return + } + ensure(expectSuccess) +} \ No newline at end of file diff --git a/backend.native/tests/codegen/basics/unchecked_cast3.kt b/backend.native/tests/codegen/basics/unchecked_cast3.kt new file mode 100644 index 00000000000..82a514d2226 --- /dev/null +++ b/backend.native/tests/codegen/basics/unchecked_cast3.kt @@ -0,0 +1,35 @@ +fun main(args: Array) { + testCast(Test(), true) + testCast(null, false) + testCastToNullable(null, true) + + println("Ok") +} + +class Test + +fun ensure(b: Boolean) { + if (!b) { + println("Error") + } +} + +fun testCast(x: Any?, expectSuccess: Boolean) { + try { + x as T + } catch (e: Throwable) { + ensure(!expectSuccess) + return + } + ensure(expectSuccess) +} + +fun testCastToNullable(x: Any?, expectSuccess: Boolean) { + try { + x as T? + } catch (e: Throwable) { + ensure(!expectSuccess) + return + } + ensure(expectSuccess) +} \ No newline at end of file