MATH-1647: Enforce precondition (index must be larger than 0).
diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java
index 8a17561..3805be4 100644
--- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java
+++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/random/HaltonSequenceGenerator.java
@@ -19,6 +19,7 @@
import java.util.function.Supplier;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
+import org.apache.commons.math4.legacy.exception.NotPositiveException;
import org.apache.commons.math4.legacy.exception.NullArgumentException;
import org.apache.commons.math4.legacy.exception.OutOfRangeException;
@@ -165,6 +166,10 @@
* @throws org.apache.commons.math4.legacy.exception.NotPositiveException NotPositiveException if index < 0
*/
public double[] skipTo(final int index) {
+ if (index < 0) {
+ throw new NotPositiveException(index);
+ }
+
count = index;
return get();
}
diff --git a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java
index 4dd800b..c1931f5 100644
--- a/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java
+++ b/commons-math-legacy/src/test/java/org/apache/commons/math4/legacy/random/HaltonSequenceGeneratorTest.java
@@ -18,6 +18,7 @@
import org.junit.Assert;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
+import org.apache.commons.math4.legacy.exception.NotPositiveException;
import org.apache.commons.math4.legacy.exception.NullArgumentException;
import org.apache.commons.math4.legacy.exception.OutOfRangeException;
import org.junit.Before;
@@ -131,4 +132,13 @@
}
}
+ @Test
+ public void testSkipToNegative() {
+ try {
+ generator.skipTo(-4584);
+ Assert.fail("an exception should have been thrown");
+ } catch (NotPositiveException e) {
+ // expected
+ }
+ }
}