Prohibited NaN steps.

This commit is contained in:
Evgeny Gerashchenko
2013-02-13 16:21:20 +04:00
parent 4043491ad2
commit 021496c07d
4 changed files with 18 additions and 0 deletions
+2
View File
@@ -139,11 +139,13 @@ public fun LongRange.step(step: Long): LongProgression {
}
public fun FloatRange.step(step: Float): FloatProgression {
if (java.lang.Float.isNaN(step)) throw IllegalArgumentException("Step must not be NaN")
checkStepIsPositive(step > 0, step)
return FloatProgression(start, end, step)
}
public fun DoubleRange.step(step: Double): DoubleProgression {
if (java.lang.Double.isNaN(step)) throw IllegalArgumentException("Step must not be NaN")
checkStepIsPositive(step > 0, step)
return DoubleProgression(start, end, step)
}
@@ -184,5 +184,15 @@ public class RangeTest {
failsWith(javaClass<IllegalArgumentException>()) { 'z' downTo 'a' step -2 }
failsWith(javaClass<IllegalArgumentException>()) { 0.0 downTo -5.0 step -0.5 }
failsWith(javaClass<IllegalArgumentException>()) { 0.0.toFloat() downTo -5.0.toFloat() step -0.5.toFloat() }
// NaN increment or step
failsWith(javaClass<IllegalArgumentException>()) { DoubleProgression(0.0, 5.0, jDouble.NaN) }
failsWith(javaClass<IllegalArgumentException>()) { FloatProgression(0.0.toFloat(), 5.0.toFloat(), jFloat.NaN) }
failsWith(javaClass<IllegalArgumentException>()) { 0.0..5.0 step jDouble.NaN }
failsWith(javaClass<IllegalArgumentException>()) { 0.0.toFloat()..5.0.toFloat() step jFloat.NaN }
failsWith(javaClass<IllegalArgumentException>()) { 5.0 downTo 0.0 step jDouble.NaN }
failsWith(javaClass<IllegalArgumentException>()) { 5.0.toFloat() downTo 0.0.toFloat() step jFloat.NaN }
}
}
+3
View File
@@ -25,6 +25,9 @@ public class DoubleProgression implements Progression<Double> {
private final double increment;
public DoubleProgression(double start, double end, double increment) {
if (Double.isNaN(increment)) {
throw new IllegalArgumentException("Increment must be not NaN");
}
if (increment == 0.0 || increment == -0.0) {
throw new IllegalArgumentException("Increment must be non-zero: " + increment);
}
+3
View File
@@ -25,6 +25,9 @@ public class FloatProgression implements Progression<Float> {
private final float increment;
public FloatProgression(float start, float end, float increment) {
if (Float.isNaN(increment)) {
throw new IllegalArgumentException("Increment must be not NaN");
}
if (increment == 0.0f || increment == -0.0f) {
throw new IllegalArgumentException("Increment must be non-zero: " + increment);
}