mirror of
https://github.com/apache/commons-math.git
synced 2025-02-08 02:59:36 +00:00
MATH-795: extracted
- testSubVectorInvalidIndex1(), - testSubVectorInvalidIndex2(), - testSubVectorInvalidIndex3(), - testSubVectorInvalidIndex4() from RealVectorAbstractTest.testDataInOut(). This test revealed that positivity of the number of elements was not checked for in RealVector.getSubVector(int, int). This is corrected, and a NotPositiveException is now thrown (corresponding error message has been added to LocalizedFormats). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1347883 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1d0adbdc72
commit
290224dce3
@ -200,6 +200,7 @@ public enum LocalizedFormats implements Localizable {
|
||||
NOT_POSITIVE_DEGREES_OF_FREEDOM("degrees of freedom must be positive ({0})"),
|
||||
NOT_POSITIVE_ELEMENT_AT_INDEX("element {0} is not positive: {1}"),
|
||||
NOT_POSITIVE_EXPONENT("invalid exponent {0} (must be positive)"),
|
||||
NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE("number of elements should be positive ({0})"),
|
||||
EXPONENT("exponent ({0})"), /* keep */
|
||||
NOT_POSITIVE_LENGTH("length must be positive ({0})"),
|
||||
LENGTH("length ({0})"), /* keep */
|
||||
|
@ -21,6 +21,7 @@ import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.apache.commons.math3.analysis.UnivariateFunction;
|
||||
import org.apache.commons.math3.exception.NotPositiveException;
|
||||
import org.apache.commons.math3.exception.NullArgumentException;
|
||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math3.exception.NumberIsTooLargeException;
|
||||
@ -667,6 +668,9 @@ public class ArrayRealVector extends RealVector implements Serializable {
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public RealVector getSubVector(int index, int n) {
|
||||
if (n < 0) {
|
||||
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
|
||||
}
|
||||
ArrayRealVector out = new ArrayRealVector(n);
|
||||
try {
|
||||
System.arraycopy(data, index, out.data, 0, n);
|
||||
|
@ -19,6 +19,7 @@ package org.apache.commons.math3.linear;
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.math3.exception.MathArithmeticException;
|
||||
import org.apache.commons.math3.exception.NotPositiveException;
|
||||
import org.apache.commons.math3.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math3.util.OpenIntToDoubleHashMap;
|
||||
import org.apache.commons.math3.util.OpenIntToDoubleHashMap.Iterator;
|
||||
@ -365,6 +366,9 @@ public class OpenMapRealVector extends SparseRealVector
|
||||
@Override
|
||||
public OpenMapRealVector getSubVector(int index, int n) {
|
||||
checkIndex(index);
|
||||
if (n < 0) {
|
||||
throw new NotPositiveException(LocalizedFormats.NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE, n);
|
||||
}
|
||||
checkIndex(index + n - 1);
|
||||
OpenMapRealVector res = new OpenMapRealVector(n);
|
||||
int end = index + n;
|
||||
|
@ -123,6 +123,8 @@ public abstract class RealVector {
|
||||
* @return a vector containing n elements.
|
||||
* @throws org.apache.commons.math3.exception.OutOfRangeException
|
||||
* if the index is not valid.
|
||||
* @throws org.apache.commons.math3.exception.NotPositiveException
|
||||
* if the number of elements is not positive
|
||||
*/
|
||||
public abstract RealVector getSubVector(int index, int n);
|
||||
|
||||
|
@ -172,6 +172,7 @@ NOT_POSITIVE_DEGREES_OF_FREEDOM = les degr\u00e9s de libert\u00e9 doivent \u00ea
|
||||
DEGREES_OF_FREEDOM = degr\u00e9s de libert\u00e9 ({0})
|
||||
NOT_POSITIVE_ELEMENT_AT_INDEX = l''\u00e9l\u00e9ment {0} n''est pas positif : {1}
|
||||
NOT_POSITIVE_EXPONENT = exposant {0} invalide (doit \u00eatre positif)
|
||||
NUMBER_OF_ELEMENTS_SHOULD_BE_POSITIVE = le nombre d''\u00e9l\u00e9ments devrait \u00eatre positif ({0})
|
||||
EXPONENT = exposant ({0})
|
||||
NOT_POSITIVE_LENGTH = la longueur doit \u00eatre positive ({0})
|
||||
LENGTH = longueur ({0})
|
||||
|
@ -36,7 +36,7 @@ public class LocalizedFormatsTest {
|
||||
|
||||
@Test
|
||||
public void testMessageNumber() {
|
||||
Assert.assertEquals(309, LocalizedFormats.values().length);
|
||||
Assert.assertEquals(310, LocalizedFormats.values().length);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -49,6 +49,7 @@ import org.apache.commons.math3.analysis.function.Ulp;
|
||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math3.exception.MathArithmeticException;
|
||||
import org.apache.commons.math3.exception.MathIllegalArgumentException;
|
||||
import org.apache.commons.math3.exception.NotPositiveException;
|
||||
import org.apache.commons.math3.exception.NumberIsTooSmallException;
|
||||
import org.apache.commons.math3.exception.OutOfRangeException;
|
||||
import org.apache.commons.math3.util.FastMath;
|
||||
@ -300,7 +301,7 @@ public abstract class RealVectorAbstractTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetSubvector() {
|
||||
public void testGetSubVector() {
|
||||
final double x = getPreferredEntryValue();
|
||||
final double[] data = {x, x, x, 1d, x, 2d, x, x, 3d, x, x, x, 4d, x, x, x};
|
||||
final int index = 1;
|
||||
@ -311,6 +312,30 @@ public abstract class RealVectorAbstractTest {
|
||||
TestUtils.assertEquals("", expected, actual, 0d);
|
||||
}
|
||||
|
||||
@Test(expected = OutOfRangeException.class)
|
||||
public void testGetSubVectorInvalidIndex1() {
|
||||
final int n = 10;
|
||||
create(new double[n]).getSubVector(-1, 2);
|
||||
}
|
||||
|
||||
@Test(expected = OutOfRangeException.class)
|
||||
public void testGetSubVectorInvalidIndex2() {
|
||||
final int n = 10;
|
||||
create(new double[n]).getSubVector(n, 2);
|
||||
}
|
||||
|
||||
@Test(expected = OutOfRangeException.class)
|
||||
public void testGetSubVectorInvalidIndex3() {
|
||||
final int n = 10;
|
||||
create(new double[n]).getSubVector(0, n + 1);
|
||||
}
|
||||
|
||||
@Test(expected = NotPositiveException.class)
|
||||
public void testGetSubVectorInvalidIndex4() {
|
||||
final int n = 10;
|
||||
create(new double[n]).getSubVector(3, -2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDataInOut() {
|
||||
final RealVector v1 = create(vec1);
|
||||
@ -318,13 +343,6 @@ public abstract class RealVectorAbstractTest {
|
||||
final RealVector v4 = create(vec4);
|
||||
final RealVector v2_t = createAlien(vec2);
|
||||
|
||||
try {
|
||||
v4.getSubVector(3, 7);
|
||||
Assert.fail("OutOfRangeException expected");
|
||||
} catch (OutOfRangeException ex) {
|
||||
// expected behavior
|
||||
}
|
||||
|
||||
final RealVector v_set1 = v1.copy();
|
||||
v_set1.setEntry(1, 11.0);
|
||||
Assert.assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1), 0);
|
||||
|
@ -215,7 +215,35 @@ public class RealVectorTest extends RealVectorAbstractTest{
|
||||
@Test
|
||||
@Ignore("Abstract class RealVector does not implement getSubvector(int, int)")
|
||||
@Override
|
||||
public void testGetSubvector() {
|
||||
public void testGetSubVector() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Abstract class RealVector does not implement getSubvector(int, int)")
|
||||
@Override
|
||||
public void testGetSubVectorInvalidIndex1() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Abstract class RealVector does not implement getSubvector(int, int)")
|
||||
@Override
|
||||
public void testGetSubVectorInvalidIndex2() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Abstract class RealVector does not implement getSubvector(int, int)")
|
||||
@Override
|
||||
public void testGetSubVectorInvalidIndex3() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
@Test
|
||||
@Ignore("Abstract class RealVector does not implement getSubvector(int, int)")
|
||||
@Override
|
||||
public void testGetSubVectorInvalidIndex4() {
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user