diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 536443cde..2829aa1d8 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -94,7 +94,7 @@ If the output is not quite correct, check for invisible trailing spaces! AbstractUnivariateStatistic.test(double[] values, int begin, int length, boolean allowEmpty) - has uses outside subclasses; implementation moved to MathArrays. + has uses outside subclasses; implementation moved to MathArrays.verifyValues(...). The "KalmanFilter" wrongly enforced a column dimension of 1 for diff --git a/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStatistic.java b/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStatistic.java index 9d5680d48..0d47ead99 100644 --- a/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStatistic.java +++ b/src/main/java/org/apache/commons/math3/stat/descriptive/AbstractUnivariateStatistic.java @@ -158,7 +158,7 @@ public abstract class AbstractUnivariateStatistic final double[] values, final int begin, final int length) throws MathIllegalArgumentException { - return MathArrays.test(values, begin, length, false); + return MathArrays.verifyValues(values, begin, length, false); } /** @@ -186,7 +186,7 @@ public abstract class AbstractUnivariateStatistic @Deprecated protected boolean test(final double[] values, final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { - return MathArrays.test(values, begin, length, allowEmpty); + return MathArrays.verifyValues(values, begin, length, allowEmpty); } /** @@ -225,7 +225,7 @@ public abstract class AbstractUnivariateStatistic final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { - return MathArrays.test(values, weights, begin, length, false); + return MathArrays.verifyValues(values, weights, begin, length, false); } /** @@ -266,7 +266,7 @@ public abstract class AbstractUnivariateStatistic protected boolean test(final double[] values, final double[] weights, final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { - return MathArrays.test(values, weights, begin, length, allowEmpty); + return MathArrays.verifyValues(values, weights, begin, length, allowEmpty); } } diff --git a/src/main/java/org/apache/commons/math3/util/MathArrays.java b/src/main/java/org/apache/commons/math3/util/MathArrays.java index 3cd70a621..87cbdce9f 100644 --- a/src/main/java/org/apache/commons/math3/util/MathArrays.java +++ b/src/main/java/org/apache/commons/math3/util/MathArrays.java @@ -1568,11 +1568,9 @@ public class MathArrays { * @throws MathIllegalArgumentException if the indices are invalid or the array is null * @since 3.3 */ - public static boolean test( - final double[] values, - final int begin, - final int length) throws MathIllegalArgumentException { - return test(values, begin, length, false); + public static boolean verifyValues(final double[] values, final int begin, final int length) + throws MathIllegalArgumentException { + return verifyValues(values, begin, length, false); } /** @@ -1596,7 +1594,7 @@ public class MathArrays { * @throws MathIllegalArgumentException if the indices are invalid or the array is null * @since 3.3 */ - public static boolean test(final double[] values, final int begin, + public static boolean verifyValues(final double[] values, final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { if (values == null) { @@ -1653,12 +1651,12 @@ public class MathArrays { * @throws MathIllegalArgumentException if the indices are invalid or the array is null * @since 3.3 */ - public static boolean test( + public static boolean verifyValues( final double[] values, final double[] weights, final int begin, final int length) throws MathIllegalArgumentException { - return test(values, weights, begin, length, false); + return verifyValues(values, weights, begin, length, false); } /** @@ -1694,7 +1692,7 @@ public class MathArrays { * are no positive weights. * @since 3.3 */ - public static boolean test(final double[] values, final double[] weights, + public static boolean verifyValues(final double[] values, final double[] weights, final int begin, final int length, final boolean allowEmpty) throws MathIllegalArgumentException { if (weights == null || values == null) { @@ -1726,6 +1724,6 @@ public class MathArrays { throw new MathIllegalArgumentException(LocalizedFormats.WEIGHT_AT_LEAST_ONE_NON_ZERO); } - return test(values, begin, length, allowEmpty); + return verifyValues(values, begin, length, allowEmpty); } } diff --git a/src/test/java/org/apache/commons/math3/util/MathArraysTest.java b/src/test/java/org/apache/commons/math3/util/MathArraysTest.java index 118cde97a..113b281ce 100644 --- a/src/test/java/org/apache/commons/math3/util/MathArraysTest.java +++ b/src/test/java/org/apache/commons/math3/util/MathArraysTest.java @@ -14,7 +14,6 @@ package org.apache.commons.math3.util; import java.util.Arrays; -import java.util.Iterator; import org.apache.commons.math3.TestUtils; import org.apache.commons.math3.exception.DimensionMismatchException; @@ -36,6 +35,12 @@ import org.junit.Test; */ public class MathArraysTest { + private double[] testArray = {0, 1, 2, 3, 4, 5}; + private double[] testWeightsArray = {0.3, 0.2, 1.3, 1.1, 1.0, 1.8}; + private double[] testNegativeWeightsArray = {-0.3, 0.2, -1.3, 1.1, 1.0, 1.8}; + private double[] nullArray = null; + private double[] singletonArray = {0}; + @Test public void testScale() { final double[] test = new double[] { -2.5, -1, 0, 1, 2.5 }; @@ -996,4 +1001,69 @@ public class MathArraysTest { final int[] natural = MathArrays.natural(0); Assert.assertEquals(0, natural.length); } + + @Test + public void testVerifyValuesPositive() { + for (int j = 0; j < 6; j++) { + for (int i = 1; i < (7 - j); i++) { + Assert.assertTrue(MathArrays.verifyValues(testArray, 0, i)); + } + } + Assert.assertTrue(MathArrays.verifyValues(singletonArray, 0, 1)); + Assert.assertTrue(MathArrays.verifyValues(singletonArray, 0, 0, true)); + } + + @Test + public void testVerifyValuesNegative() { + Assert.assertFalse(MathArrays.verifyValues(singletonArray, 0, 0)); + Assert.assertFalse(MathArrays.verifyValues(testArray, 0, 0)); + try { + MathArrays.verifyValues(singletonArray, 2, 1); // start past end + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + try { + MathArrays.verifyValues(testArray, 0, 7); // end past end + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + try { + MathArrays.verifyValues(testArray, -1, 1); // start negative + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + try { + MathArrays.verifyValues(testArray, 0, -1); // length negative + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + try { + MathArrays.verifyValues(nullArray, 0, 1); // null array + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + try { + MathArrays.verifyValues(testArray, nullArray, 0, 1); // null weights array + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + try { + MathArrays.verifyValues(singletonArray, testWeightsArray, 0, 1); // weights.length != value.length + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + try { + MathArrays.verifyValues(testArray, testNegativeWeightsArray, 0, 6); // can't have negative weights + Assert.fail("Expecting MathIllegalArgumentException"); + } catch (MathIllegalArgumentException ex) { + // expected + } + } }