MATH-1647: Enforce precondition (index must be larger than 0).

This commit is contained in:
rednikeeg 2022-08-19 21:27:11 +03:00 committed by Gilles Sadowski
parent 3c1c92d3eb
commit 01ba89bf26
2 changed files with 15 additions and 0 deletions

View File

@ -19,6 +19,7 @@ package org.apache.commons.math4.legacy.random;
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 @@ public class HaltonSequenceGenerator implements Supplier<double[]> {
* @throws org.apache.commons.math4.legacy.exception.NotPositiveException NotPositiveException if index &lt; 0
*/
public double[] skipTo(final int index) {
if (index < 0) {
throw new NotPositiveException(index);
}
count = index;
return get();
}

View File

@ -18,6 +18,7 @@ package org.apache.commons.math4.legacy.random;
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 @@ public class HaltonSequenceGeneratorTest {
}
}
@Test
public void testSkipToNegative() {
try {
generator.skipTo(-4584);
Assert.fail("an exception should have been thrown");
} catch (NotPositiveException e) {
// expected
}
}
}