Prohibit step size of min value, as it doesn't have a negated counterpart

#KT-17176
This commit is contained in:
Pap Lőrinc
2017-09-18 17:51:26 +03:00
committed by Ilya Gorbunov
parent c6c3b05d02
commit bdf623f711
3 changed files with 18 additions and 2 deletions
+3
View File
@@ -21,6 +21,7 @@ public open class CharProgression
) : Iterable<Char> {
init {
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero")
else if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse")
}
/**
@@ -75,6 +76,7 @@ public open class IntProgression
) : Iterable<Int> {
init {
if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero")
else if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse")
}
/**
@@ -129,6 +131,7 @@ public open class LongProgression
) : Iterable<Long> {
init {
if (step == 0L) throw kotlin.IllegalArgumentException("Step must be non-zero")
else if (step == Long.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must have an inverse")
}
/**
+8 -1
View File
@@ -35,7 +35,13 @@ class GenerateProgressions(out: PrintWriter) : BuiltInsSourceGenerator(out) {
LONG -> "0L"
else -> "0"
}
val checkZero = "if (step == $zero) throw kotlin.IllegalArgumentException(\"Step must be non-zero\")"
val checkZero = """if (step == $zero) throw kotlin.IllegalArgumentException("Step must be non-zero")"""
val minValue = when (kind) {
LONG -> "Long.MIN_VALUE"
else -> "Int.MIN_VALUE"
}
val checkMin = """else if (step == $minValue) throw kotlin.IllegalArgumentException("Step must have an inverse")"""
val hashCode = "=\n" + when (kind) {
CHAR ->
@@ -60,6 +66,7 @@ public open class $progression
) : Iterable<$t> {
init {
$checkZero
$checkMin
}
/**
+7 -1
View File
@@ -380,4 +380,10 @@ public class RangeTest {
assertFailsWithIllegalArgument { 0L downTo -5L step -2L }
assertFailsWithIllegalArgument { 'z' downTo 'a' step -2 }
}
}
@Test fun stepSizeIsTooLow() {
assertFailsWithIllegalArgument { CharProgression.fromClosedRange('a', 'b', Int.MIN_VALUE) }
assertFailsWithIllegalArgument { IntProgression.fromClosedRange(0, 1, Int.MIN_VALUE) }
assertFailsWithIllegalArgument { LongProgression.fromClosedRange(0, 1, Long.MIN_VALUE) }
}
}