Optimize range operations for 'until' extension from stdlib (KT-9900)
NB: for-in-until loop is generated as precondition loop, because the corresponding range is right-exclusive (and thus we have no problems with integer overflows).
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
// IGNORE_BACKEND: JS
|
||||
// see KT-17700
|
||||
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
testChar()
|
||||
testNullableChar()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
private fun testChar() {
|
||||
var sum = ""
|
||||
for (ch in '1' until '5') {
|
||||
sum = sum + ch
|
||||
}
|
||||
assertEquals("1234", sum)
|
||||
}
|
||||
|
||||
private fun testNullableChar() {
|
||||
var sum = ""
|
||||
for (ch: Char? in '1' until '5') {
|
||||
sum = sum + (ch ?: break)
|
||||
}
|
||||
assertEquals("1234", sum)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
for (ch in (-10).toChar() until '\u0000') {
|
||||
throw AssertionError("This loop shoud not be executed")
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
testIntInIntUntilInt()
|
||||
testNullableIntInIntUntilInt()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
private fun testIntInIntUntilInt() {
|
||||
var sum = 0
|
||||
for (i in 1 until 5) {
|
||||
sum = sum * 10 + i
|
||||
}
|
||||
assertEquals(1234, sum)
|
||||
}
|
||||
|
||||
private fun testNullableIntInIntUntilInt() {
|
||||
var sum = 0
|
||||
for (i: Int? in 1 until 5) {
|
||||
sum = sum * 10 + (i?.toInt() ?: break)
|
||||
}
|
||||
assertEquals(1234, sum)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
for (i in 10 until 0) {
|
||||
throw AssertionError("This loop should not be executed")
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun testLongInLongUntilLong() {
|
||||
var sum = 0
|
||||
for (i in 1L until 5L) {
|
||||
sum = sum * 10 + i.toInt()
|
||||
}
|
||||
assertEquals(1234, sum)
|
||||
}
|
||||
|
||||
fun testLongInLongUntilInt() {
|
||||
var sum = 0
|
||||
for (i in 1L until 5.toInt()) {
|
||||
sum = sum * 10 + i.toInt()
|
||||
}
|
||||
assertEquals(1234, sum)
|
||||
}
|
||||
|
||||
fun testLongInIntUntilLong() {
|
||||
var sum = 0
|
||||
for (i in 1.toInt() until 5L) {
|
||||
sum = sum * 10 + i.toInt()
|
||||
}
|
||||
assertEquals(1234, sum)
|
||||
}
|
||||
|
||||
fun testNullableLongInIntUntilLong() {
|
||||
var sum = 0
|
||||
for (i: Long? in 1.toInt() until 5L) {
|
||||
sum = sum * 10 + (i?.toInt() ?: break)
|
||||
}
|
||||
assertEquals(1234, sum)
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
testLongInLongUntilLong()
|
||||
testLongInIntUntilLong()
|
||||
testLongInLongUntilInt()
|
||||
testNullableLongInIntUntilLong()
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
for (i in Int.MAX_VALUE until Int.MAX_VALUE) {
|
||||
throw AssertionError("This loop shoud not be executed")
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
for (i in 0 until Int.MIN_VALUE) {
|
||||
throw AssertionError("This loop shoud not be executed")
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
for (i in 0 until Long.MIN_VALUE) {
|
||||
throw AssertionError("This loop shoud not be executed")
|
||||
}
|
||||
return "OK"
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// WITH_RUNTIME
|
||||
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
fun box(): String {
|
||||
testIntInIntUntilSmartcastInt()
|
||||
return "OK"
|
||||
}
|
||||
|
||||
private fun testIntInIntUntilSmartcastInt() {
|
||||
var sum = 0
|
||||
|
||||
val a: Any = 5
|
||||
if (a is Int) {
|
||||
for (i: Int in 1 until a) {
|
||||
sum = sum * 10 + i
|
||||
}
|
||||
}
|
||||
|
||||
assertEquals(1234, sum)
|
||||
}
|
||||
Reference in New Issue
Block a user