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,27 @@
// 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")
}
fun list(): List<Int> {
order.append("L")
return emptyList()
}
fun x(i: Int): Int {
order.append("X")
return i
}
fun box(): String {
expectOrder("1 in []", "LX") { x(1) in list() }
expectOrder("1 !in []", "LX") { x(1) !in list() }
return "OK"
}
@@ -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"
}
@@ -0,0 +1,31 @@
// 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")
}
fun low(i: Int): Int {
order.append("L")
return i
}
fun high(i: Int): Int {
order.append("H")
return i
}
fun x(i: Int): Int {
order.append("X")
return i
}
fun box(): String {
expectOrder("0 in 1 .. 3", "HLX") { x(0) in high(3) downTo low(1) }
expectOrder("0 !in 1 .. 3", "HLX") { x(0) !in high(3) downTo low(1) }
return "OK"
}
@@ -0,0 +1,31 @@
// 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")
}
fun low(i: Int): Int {
order.append("L")
return i
}
fun high(i: Int): Int {
order.append("H")
return i
}
fun x(i: Int): Int {
order.append("X")
return i
}
fun box(): String {
expectOrder("0 in 1 .. 3", "LHX") { x(0) in low(1) .. high(3) }
expectOrder("0 !in 1 .. 3", "LHX") { x(0) !in low(1) .. high(3) }
return "OK"
}