Add more tests for constructor call evaluation order

- break in arguments
- continue in arguments
- early return in arguments
- non-local return in arguments
- nested constructor call in arguments
This commit is contained in:
Dmitry Petrov
2017-10-03 15:42:12 +03:00
parent c3d74bdabb
commit 7e808bd3ea
9 changed files with 343 additions and 0 deletions
@@ -0,0 +1,39 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
var count = 0
while (true) {
Foo(
logged("i", if (count == 0) 1 else break),
logged("j", 2)
)
count++
}
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
var count = 0
while (true) {
count++
if (count > 1) break
Foo(
logged("i", if (count == 1) 1 else continue),
logged("j", 2)
)
}
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
inline fun ok(): String {
return foo(1, 1.0, 1.0f, 1L, "O", C(if (bar()) return "zap" else "K"))
}
fun box(): String {
val ok = ok()
if (ok != "OK") return "Fail: $ok"
val r = log.toString()
if (r != "<clinit>;bar;<init>;foo;") return "Fail: '$r'"
return "OK"
}
// FILE: C.kt
class C(val str: String) {
init {
log.append("<init>;")
}
companion object {
init {
log.append("<clinit>;")
}
}
}
// FILE: util.kt
fun foo(x: Int, a: Double, b: Float, y: Long, z: String, c: C) =
logged("foo;", z + c.str)
fun bar() = logged("bar;", false)
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
@@ -0,0 +1,36 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
for (count in 0..3) {
val test = Foo(count, Foo(1, "x", 2), if (count > 0) break else 3)
if (count > 0) return "Fail: count = $count"
if (test.toString() != "Foo(0,Foo(1,x,2),3)") return "Fail: ${test.toString()}"
}
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(val a: Int, val b: Any, val c: Int) {
init {
log.append("<init>")
}
override fun toString() = "Foo($a,$b,$c)"
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
run L1@{
var count = 0
run {
while (true) {
Foo(
logged("i", if (count == 0) 1 else return@L1),
logged("j", 2)
)
count++
}
}
}
val result = log.toString()
if (result != "<clinit>ij<init>") return "Fail: '$result'"
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(i: Int, j: Int) {
init {
log.append("<init>")
}
companion object {
init {
log.append("<clinit>")
}
}
}
@@ -0,0 +1,36 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: test.kt
fun box(): String {
for (count in 0..3) {
val test = Foo(count, Foo(1, "x", if (count > 0) break else 2), 3)
if (count > 0) return "Fail: count = $count"
if (test.toString() != "Foo(0,Foo(1,x,2),3)") return "Fail: ${test.toString()}"
}
return "OK"
}
// FILE: util.kt
val log = StringBuilder()
fun <T> logged(msg: String, value: T): T {
log.append(msg)
return value
}
// FILE: Foo.kt
class Foo(val a: Int, val b: Any, val c: Int) {
init {
log.append("<init>")
}
override fun toString() = "Foo($a,$b,$c)"
companion object {
init {
log.append("<clinit>")
}
}
}