Support default parameter values for inline class constructors and funs

#KT-26908
 #KT-26554

Move default parameter value tests to separate directory
This commit is contained in:
Dmitry Petrov
2018-10-02 18:04:45 +03:00
parent 8ce1d09f8a
commit f68ce4b35b
26 changed files with 435 additions and 135 deletions
@@ -0,0 +1,15 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z(val z: Int)
class Test(val z: Z = Z(42)) {
fun test() = z.z
}
fun box(): String {
if (Test().test() != 42) throw AssertionError()
if (Test(Z(123)).test() != 123) throw AssertionError()
return "OK"
}
@@ -0,0 +1,24 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR, JS_IR
inline class Z(val z: Int)
interface ITest {
fun testDefault(z: Z = Z(42)) = z.z
fun testOverridden(z: Z = Z(42)): Int
}
class Test : ITest {
override fun testOverridden(z: Z) = z.z
}
fun box(): String {
if (Test().testDefault() != 42) throw AssertionError()
if (Test().testDefault(Z(123)) != 123) throw AssertionError()
if (Test().testOverridden() != 42) throw AssertionError()
if (Test().testOverridden(Z(123)) != 123) throw AssertionError()
return "OK"
}
@@ -0,0 +1,13 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z(val z: Int)
fun test(z: Z = Z(42)) = z.z
fun box(): String {
if (test() != 42) throw AssertionError()
if (test(Z(123)) != 123) throw AssertionError()
return "OK"
}
@@ -0,0 +1,29 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
interface IFoo {
fun foo(): String
}
inline class Z(val z: Int) : IFoo {
override fun foo() = z.toString()
}
fun testNullable(z: Z? = Z(42)) = z!!.z
fun testAny(z: Any = Z(42)) = (z as Z).z
fun testInterface(z: IFoo = Z(42)) = z.foo()
fun box(): String {
if (testNullable() != 42) throw AssertionError()
if (testNullable(Z(123)) != 123) throw AssertionError()
if (testAny() != 42) throw AssertionError()
if (testAny(Z(123)) != 123) throw AssertionError()
if (testInterface() != "42") throw AssertionError()
if (testInterface(Z(123)) != "123") throw AssertionError()
return "OK"
}
@@ -0,0 +1,32 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z(val int: Int)
inline class L(val long: Long)
inline class Str(val string: String)
inline class Obj(val obj: Any)
inline fun <R> withDefaultZ(fn: (Z) -> R, x: Z = Z(42)) = fn(x)
inline fun <R> withDefaultL(fn: (L) -> R, x: L = L(42L)) = fn(x)
inline fun <R> withDefaultL2(x: L = L(42L), fn: (L) -> R) = fn(x)
inline fun <R> withDefaultStr(fn: (Str) -> R, x: Str = Str("abc")) = fn(x)
inline fun <R> withDefaultObj(fn: (Obj) -> R, x: Obj = Obj("abc")) = fn(x)
inline fun <R> withDefaultObj2(x: Obj = Obj("abc"), fn: (Obj) -> R) = fn(x)
fun testWithDefaultZ() = withDefaultZ({ Z(it.int + 1) })
fun testWithDefaultL() = withDefaultL({ L(it.long + 1L) })
fun testWithDefaultL2() = withDefaultL2(fn = { L(it.long + 1L) })
fun testWithDefaultStr() = withDefaultStr({ Str(it.string + "1") })
fun testWithDefaultObj() = withDefaultObj({ Obj(it.obj.toString() + "1") })
fun testWithDefaultObj2() = withDefaultObj2(fn = { Obj(it.obj.toString() + "1") })
fun box(): String {
if (testWithDefaultZ().int != 43) throw AssertionError()
if (testWithDefaultL().long != 43L) throw AssertionError()
if (testWithDefaultL2().long != 43L) throw AssertionError()
if (testWithDefaultStr().string != "abc1") throw AssertionError()
if (testWithDefaultObj().obj != "abc1") throw AssertionError()
if (testWithDefaultObj2().obj != "abc1") throw AssertionError()
return "OK"
}
@@ -0,0 +1,13 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z(val x: Int) {
fun test(y: Int = 42) = x + y
}
fun box(): String {
if (Z(800).test() != 842) throw AssertionError()
if (Z(400).test(32) != 432) throw AssertionError()
return "OK"
}
@@ -0,0 +1,14 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z(val x: Int = 1234)
inline class L(val x: Long = 1234L)
inline class S(val x: String = "foobar")
fun box(): String {
if (Z().x != 1234) throw AssertionError()
if (L().x != 1234L) throw AssertionError()
if (S().x != "foobar") throw AssertionError()
return "OK"
}
@@ -0,0 +1,12 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JVM_IR
inline class Z(val x: Int) {
constructor(x: Long = 42L) : this(x.toInt())
}
fun box(): String {
if (Z().x != 42) throw AssertionError()
return "OK"
}
@@ -0,0 +1,20 @@
// !LANGUAGE: +InlineClasses
// IGNORE_BACKEND: JS, JS_IR, JVM_IR
// WITH_RUNTIME
data class RGBA(val rgba: Int)
inline class RgbaArray(val array: IntArray) {
val size: Int get() = array.size
fun fill(value: RGBA, start: Int = 0, end: Int = this.size): Unit = array.fill(value.rgba, start, end)
}
fun box(): String {
val rgbas = RgbaArray(IntArray(10))
rgbas.fill(RGBA(123456))
for (i in rgbas.array.indices) {
if (rgbas.array[i] != 123456) throw AssertionError()
}
return "OK"
}