[MATH-1002] Renamed methods to verifyValues, also copied unit tests to MathArraysTest.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1537714 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Thomas Neidhart 2013-10-31 21:54:21 +00:00
parent c1f3a7bd3b
commit 2b45b294b5
4 changed files with 84 additions and 16 deletions

View File

@ -94,7 +94,7 @@ If the output is not quite correct, check for invisible trailing spaces!
</action>
<action dev="sebb" type="add" issue="MATH-1002">
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(...).
</action>
<action dev="tn" type="fix" issue="MATH-1033" due-to="Yuan Qu">
The "KalmanFilter" wrongly enforced a column dimension of 1 for

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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
}
}
}