From a8caf7c7d8dff6f0ee9da5cf434552bc7eb0c797 Mon Sep 17 00:00:00 2001 From: Vasily Levchenko Date: Mon, 26 Dec 2016 18:02:32 +0300 Subject: [PATCH] TEST: test for default parameters (cherry picked from commit 7999d68d812aedd5863f04b7fd2171e284b8f332) --- backend.native/tests/build.gradle | 22 +++++ .../tests/codegen/function/defaults.kt | 97 +++++++++++++++++++ .../tests/codegen/function/defaults1.kt | 9 ++ .../tests/codegen/function/defaults2.kt | 10 ++ .../tests/codegen/function/defaults3.kt | 12 +++ .../tests/codegen/function/named.kt | 5 + 6 files changed, 155 insertions(+) create mode 100644 backend.native/tests/codegen/function/defaults.kt create mode 100644 backend.native/tests/codegen/function/defaults1.kt create mode 100644 backend.native/tests/codegen/function/defaults2.kt create mode 100644 backend.native/tests/codegen/function/defaults3.kt create mode 100644 backend.native/tests/codegen/function/named.kt diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index ecef3b711a2..4c1e69b0965 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -173,6 +173,23 @@ task sum2(type: RunKonanTest) { source = "codegen/function/sum_imm.kt" } +task defaults(type: RunKonanTest) { + source = "codegen/function/defaults.kt" +} + +task defaults1(type: RunKonanTest) { + source = "codegen/function/defaults1.kt" +} + +task defaults2(type: RunKonanTest) { + source = "codegen/function/defaults2.kt" +} + +task defaults3(type: RunKonanTest) { + source = "codegen/function/defaults3.kt" +} + + task sum_3const(type: RunKonanTest) { source = "codegen/function/sum_3const.kt" } @@ -307,6 +324,11 @@ task bool_yes(type: RunKonanTest) { source = "codegen/function/boolean.kt" } +task named(type: RunKonanTest) { + source = "codegen/function/named.kt" +} + + task plus_eq(type: RunKonanTest) { source = "codegen/function/plus_eq.kt" } diff --git a/backend.native/tests/codegen/function/defaults.kt b/backend.native/tests/codegen/function/defaults.kt new file mode 100644 index 00000000000..a4562f3ddc5 --- /dev/null +++ b/backend.native/tests/codegen/function/defaults.kt @@ -0,0 +1,97 @@ +/** + * Created by minamoto on 12/26/16. + */ +//package defaults + +open class A(val a:Int) { + override fun equals(other: Any?): Boolean { + if (other == null || other as? A == null) return false + return (other as A).a == a // Where is smart casting? + } + + companion object { + val zero = A(0) + val one = A(1) + val magic = A(42) + } + + +} +// FUN public fun foo(a: defaults.A = ...): kotlin.Int +// a: EXPRESSION_BODY +// CALL '(): A' type=defaults.A origin=GET_PROPERTY +// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion +// BLOCK_BODY +// RETURN type=kotlin.Nothing from='foo(A = ...): Int' +// CALL '(): Int' type=kotlin.Int origin=GET_PROPERTY +// $this: GET_VAR 'value-parameter a: A = ...' type=defaults.A origin=null +fun foo(a: A = A.magic, b:Int = 0xdeadbeef.toInt()) = a.a + +// FUN public fun bar(a: defaults.A, inc: kotlin.Int = ...): defaults.A +// inc: EXPRESSION_BODY +// CONST Int type=kotlin.Int value='0' +// BLOCK_BODY +// RETURN type=kotlin.Nothing from='bar(A, Int = ...): A' +// CALL 'constructor A(Int)' type=defaults.A origin=null +// a: CALL 'plus(Int): Int' type=kotlin.Int origin=PLUS +// $this: CALL '(): Int' type=kotlin.Int origin=GET_PROPERTY +// $this: GET_VAR 'value-parameter a: A' type=defaults.A origin=null +// other: GET_VAR 'value-parameter inc: Int = ...' type=kotlin.Int origin=null +fun bar(a:A, inc:Int = 0) = A(a.a + inc) + + +fun main(args:Array) { + +// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL 'foo(A = ...): Int' type=kotlin.Int origin=null +// arg1: CALL '(): Int' type=kotlin.Int origin=GET_PROPERTY +// $this: CALL '(): A' type=defaults.A origin=GET_PROPERTY +// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion + if (foo() != A.magic.a) { + println("magic failed") + throw Error() + } + +// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL 'foo(A = ...): Int' type=kotlin.Int origin=null +// a: CALL 'constructor A(Int)' type=defaults.A origin=null +// a: CONST Int type=kotlin.Int value='1' +// arg1: CONST Int type=kotlin.Int value='1' + if (foo(A(1)) != 1) { + println("one failed: foo(A(1))") + throw Error() + } + +// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL 'bar(A, Int = ...): A' type=defaults.A origin=null +// a: CALL '(): A' type=defaults.A origin=GET_PROPERTY <--- +// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion +// arg1: CALL '(): A' type=defaults.A origin=GET_PROPERTY +// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion + + if (bar(A.one) != A.one) { + println("A one failed") + throw Error() + } + + +// if: CALL 'NOT(Boolean): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL 'EQEQ(Any?, Any?): Boolean' type=kotlin.Boolean origin=EXCLEQ +// arg0: CALL '(): Int' type=kotlin.Int origin=GET_PROPERTY +// $this: CALL 'bar(A, Int = ...): A' type=defaults.A origin=null +// a: CALL '(): A' type=defaults.A origin=GET_PROPERTY +// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion +// inc: CONST Int type=kotlin.Int value='1' +// arg1: CONST Int type=kotlin.Int value='2' + if (bar(A.one, 1).a != 2) { + println("A one + 1 failed") + throw Error() + } + println("all tests passed") +} + + + diff --git a/backend.native/tests/codegen/function/defaults1.kt b/backend.native/tests/codegen/function/defaults1.kt new file mode 100644 index 00000000000..d8b2a04ab63 --- /dev/null +++ b/backend.native/tests/codegen/function/defaults1.kt @@ -0,0 +1,9 @@ +fun foo(x:Int = 0, y:Int = x + 1, z:Int = x + y + 1) = x + y + z + +fun main(arg:Array) { + val v = foo() + if (v != 3) { + println("test failed $v expected 3") + throw Error() + } +} \ No newline at end of file diff --git a/backend.native/tests/codegen/function/defaults2.kt b/backend.native/tests/codegen/function/defaults2.kt new file mode 100644 index 00000000000..8f6830471dc --- /dev/null +++ b/backend.native/tests/codegen/function/defaults2.kt @@ -0,0 +1,10 @@ + +fun Int.foo(inc0:Int, inc:Int = 0) = this + inc0 + inc + +fun main(arg:Array) { + val v = 42.foo(0) + if (v != 42) { + println("test failed v:$v expected:42") + throw Error() + } +} diff --git a/backend.native/tests/codegen/function/defaults3.kt b/backend.native/tests/codegen/function/defaults3.kt new file mode 100644 index 00000000000..9be86df16f0 --- /dev/null +++ b/backend.native/tests/codegen/function/defaults3.kt @@ -0,0 +1,12 @@ +fun foo(a:Int = 2, b:String = "Hello", c:Int = 4):String = "$b-$c$a" +fun foo(a:Int = 3, b:Int = a + 1, c:Int = a + b) = a + b + c + +fun main(arg:Array){ + val a = foo(b="Universe") + if (a != "Universe-42") + throw Error() + + val b = foo(b = 5) + if (b != (/* a = */ 3 + /* b = */ 5 + /* c = */ (3 + 5))) + throw Error() +} \ No newline at end of file diff --git a/backend.native/tests/codegen/function/named.kt b/backend.native/tests/codegen/function/named.kt new file mode 100644 index 00000000000..00d18ab29c4 --- /dev/null +++ b/backend.native/tests/codegen/function/named.kt @@ -0,0 +1,5 @@ +fun foo(a:Int, b:Int) = a - b +fun main(args:Array) { + if (foo(b = 24, a = 42) != 18) + throw Error() +} \ No newline at end of file