Maintain proper evaluation order for 'a in x .. y'

As of Kotlin 1.0 and 1.1, expression 'a in x .. y' is considered
equivalent to 'x.rangeTo(y).a', and should be evaluated in the following
order:
1. x
2. y
3. a
4. compare x with a
5. compare y with a (if needed)
This commit is contained in:
Dmitry Petrov
2017-07-07 12:31:23 +03:00
parent fc3e9318d9
commit 905a16e1df
14 changed files with 298 additions and 44 deletions
@@ -0,0 +1,35 @@
// WITH_RUNTIME
val order = StringBuilder()
inline fun expectOrder(at: String, expected: String, body: () -> Unit) {
order.setLength(0)
body()
if (order.toString() != expected) {
throw AssertionError("$at: expected: '$expected', actual: '$order'")
}
}
class Z(val x: Int) : Comparable<Z> {
override fun compareTo(other: Z): Int {
order.append("c:$x,${other.x} ")
return x.compareTo(other.x)
}
}
fun z(i: Int): Z {
order.append("z:$i ")
return Z(i)
}
fun box(): String {
expectOrder("z0 in z1 .. z3", "z:1 z:3 z:0 c:1,0 ") { z(0) in z(1) .. z(3) }
expectOrder("z2 in z1 .. z3", "z:1 z:3 z:2 c:1,2 c:3,2 ") { z(2) in z(1) .. z(3) }
expectOrder("z4 in z1 .. z3", "z:1 z:4 z:2 c:1,2 c:4,2 ") { z(2) in z(1) .. z(4) }
expectOrder("z0 !in z1 .. z3", "z:1 z:3 z:0 c:1,0 ") { z(0) !in z(1) .. z(3) }
expectOrder("z2 !in z1 .. z3", "z:1 z:3 z:2 c:1,2 c:3,2 ") { z(2) !in z(1) .. z(3) }
expectOrder("z4 !in z1 .. z3", "z:1 z:4 z:2 c:1,2 c:4,2 ") { z(2) !in z(1) .. z(4) }
return "OK"
}