TEST: test for default parameters

(cherry picked from commit 7999d68d812aedd5863f04b7fd2171e284b8f332)
This commit is contained in:
Vasily Levchenko
2016-12-26 18:02:32 +03:00
committed by vvlevchenko
parent b994fabadd
commit a8caf7c7d8
6 changed files with 155 additions and 0 deletions
+22
View File
@@ -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"
}
@@ -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 '<get-magic>(): 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 '<get-a>(): 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 '<get-a>(): 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<String>) {
// 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 '<get-a>(): Int' type=kotlin.Int origin=GET_PROPERTY
// $this: CALL '<get-magic>(): 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 '<get-one>(): A' type=defaults.A origin=GET_PROPERTY <---
// $this: GET_OBJECT 'companion object of A' type=defaults.A.Companion
// arg1: CALL '<get-one>(): 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 '<get-a>(): Int' type=kotlin.Int origin=GET_PROPERTY
// $this: CALL 'bar(A, Int = ...): A' type=defaults.A origin=null
// a: CALL '<get-one>(): 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")
}
@@ -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<String>) {
val v = foo()
if (v != 3) {
println("test failed $v expected 3")
throw Error()
}
}
@@ -0,0 +1,10 @@
fun Int.foo(inc0:Int, inc:Int = 0) = this + inc0 + inc
fun main(arg:Array<String>) {
val v = 42.foo(0)
if (v != 42) {
println("test failed v:$v expected:42")
throw Error()
}
}
@@ -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<String>){
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()
}
@@ -0,0 +1,5 @@
fun foo(a:Int, b:Int) = a - b
fun main(args:Array<String>) {
if (foo(b = 24, a = 42) != 18)
throw Error()
}