Author: celestin Date: Fri Jun 8 05:50:11 2012 New Revision: 1347883 URL: http://svn.apache.org/viewvc?rev=1347883&view=rev Log: 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). Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/exception/util/LocalizedFormats.java commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/exception/util/LocalizedFormats.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/exception/util/LocalizedFormats.java?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/exception/util/LocalizedFormats.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/exception/util/LocalizedFormats.java Fri Jun 8 05:50:11 2012 @@ -200,6 +200,7 @@ public enum LocalizedFormats implements 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 */ Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/ArrayRealVector.java Fri Jun 8 05:50:11 2012 @@ -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 Rea /** {@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); Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/OpenMapRealVector.java Fri Jun 8 05:50:11 2012 @@ -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 S @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; Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/linear/RealVector.java Fri Jun 8 05:50:11 2012 @@ -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); Modified: commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties (original) +++ commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties Fri Jun 8 05:50:11 2012 @@ -172,6 +172,7 @@ NOT_POSITIVE_DEGREES_OF_FREEDOM = les de 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}) Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java Fri Jun 8 05:50:11 2012 @@ -36,7 +36,7 @@ public class LocalizedFormatsTest { @Test public void testMessageNumber() { - Assert.assertEquals(309, LocalizedFormats.values().length); + Assert.assertEquals(310, LocalizedFormats.values().length); } @Test Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java Fri Jun 8 05:50:11 2012 @@ -49,6 +49,7 @@ import org.apache.commons.math3.analysis 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 RealVectorAbstract } @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 RealVectorAbstract 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 RealVectorAbstract 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); Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java?rev=1347883&r1=1347882&r2=1347883&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java Fri Jun 8 05:50:11 2012 @@ -215,7 +215,35 @@ public class RealVectorTest extends Real @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 }