You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ce...@apache.org on 2012/06/07 07:45:44 UTC

svn commit: r1347401 - /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java

Author: celestin
Date: Thu Jun  7 05:45:44 2012
New Revision: 1347401

URL: http://svn.apache.org/viewvc?rev=1347401&view=rev
Log:
MATH-795: splitted tests for
  - RealVector.setEntry(int, double),
  - RealVector.addToEntry(int, double).

Modified:
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java

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=1347401&r1=1347400&r2=1347401&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 Thu Jun  7 05:45:44 2012
@@ -52,6 +52,7 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.exception.NumberIsTooSmallException;
 import org.apache.commons.math3.exception.OutOfRangeException;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.MathArrays;
 import org.junit.Test;
 
 
@@ -164,6 +165,96 @@ public abstract class RealVectorAbstract
         create(data1).getEntry(data1.length);
     }
 
+    @Test
+    public void testSetEntry() {
+        final double[] expected = MathArrays.copyOf(data1);
+        final RealVector actual = create(data1);
+
+        /*
+         * Try setting to any value.
+         */
+        for (int i = 0; i < data1.length; i++) {
+            final double oldValue = data1[i];
+            final double newValue = oldValue + 1d;
+            expected[i] = newValue;
+            actual.setEntry(i, newValue);
+            TestUtils.assertEquals("while setting entry #" + i, expected,
+                actual, 0d);
+            expected[i] = oldValue;
+            actual.setEntry(i, oldValue);
+        }
+
+        /*
+         * Try setting to the preferred value.
+         */
+        final double x = getPreferredEntryValue();
+        for (int i = 0; i < data1.length; i++) {
+            final double oldValue = data1[i];
+            final double newValue = x;
+            expected[i] = newValue;
+            actual.setEntry(i, newValue);
+            TestUtils.assertEquals("while setting entry #" + i, expected,
+                actual, 0d);
+            expected[i] = oldValue;
+            actual.setEntry(i, oldValue);
+        }
+    }
+
+    @Test(expected=OutOfRangeException.class)
+    public void testSetEntryInvalidIndex1() {
+        create(data1).setEntry(-1, getPreferredEntryValue());
+    }
+
+    @Test(expected=OutOfRangeException.class)
+    public void testSetEntryInvalidIndex2() {
+        create(data1).setEntry(data1.length, getPreferredEntryValue());
+    }
+
+    @Test
+    public void testAddToEntry() {
+        final double[] expected = MathArrays.copyOf(data1);
+        final RealVector actual = create(data1);
+
+        /*
+         * Try adding any value.
+         */
+        double increment = 1d;
+        for (int i = 0; i < data1.length; i++) {
+            final double oldValue = data1[i];
+            expected[i] += increment;
+            actual.addToEntry(i, increment);
+            TestUtils.assertEquals("while incrementing entry #" + i, expected,
+                actual, 0d);
+            expected[i] = oldValue;
+            actual.setEntry(i, oldValue);
+        }
+
+        /*
+         * Try incrementing so that result is equal to preferred value.
+         */
+        final double x = getPreferredEntryValue();
+        for (int i = 0; i < data1.length; i++) {
+            final double oldValue = data1[i];
+            increment = x - oldValue;
+            expected[i] = x;
+            actual.addToEntry(i, increment);
+            TestUtils.assertEquals("while incrementing entry #" + i, expected,
+                actual, 0d);
+            expected[i] = oldValue;
+            actual.setEntry(i, oldValue);
+        }
+    }
+
+    @Test(expected=OutOfRangeException.class)
+    public void testAddToEntryInvalidIndex1() {
+        create(data1).addToEntry(-1, getPreferredEntryValue());
+    }
+
+    @Test(expected=OutOfRangeException.class)
+    public void testAddToEntryInvalidIndex2() {
+        create(data1).addToEntry(data1.length, getPreferredEntryValue());
+    }
+
     private void doTestAppendVector(final String message, final RealVector v1,
         final RealVector v2, final double delta) {
 
@@ -842,19 +933,6 @@ public abstract class RealVectorAbstract
     }
 
     @Test
-    public void testAddToEntry() {
-        final double[] v = new double[] { 1, 2, 3 };
-        final RealVector x = create(v);
-        final double inc = 7;
-        for (int i = 0; i < x.getDimension(); i++) {
-            x.addToEntry(i, inc);
-        }
-        for (int i = 0; i < x.getDimension(); i++) {
-            Assert.assertEquals(v[i] + inc, x.getEntry(i), 0);
-        }
-    }
-
-    @Test
     public void testMinMax() {
         final RealVector v1 = create(new double[] {0, -6, 4, 12, 7});
         Assert.assertEquals(1, v1.getMinIndex());