From 0fa29579f5af17e360e54c8081719288eb993f2d Mon Sep 17 00:00:00 2001 From: Sebastien Brisard Date: Thu, 31 May 2012 05:51:16 +0000 Subject: [PATCH] MATH-795: factored out testDataInOut(). git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1344571 13f79535-47bb-0310-9956-ffa450edef68 --- .../math3/linear/ArrayRealVectorTest.java | 95 ++------------- .../math3/linear/RealVectorAbstractTest.java | 115 +++++++++++++++++- .../commons/math3/linear/RealVectorTest.java | 5 + .../math3/linear/SparseRealVectorTest.java | 93 +------------- 4 files changed, 131 insertions(+), 177 deletions(-) diff --git a/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java b/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java index 0e8b7efe4..b58a81148 100644 --- a/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java @@ -49,7 +49,6 @@ 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.OutOfRangeException; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Test; @@ -60,18 +59,6 @@ import org.junit.Test; * @version $Id$ */ public class ArrayRealVectorTest extends RealVectorAbstractTest { - - // - protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}}; - protected double[] vec1 = {1d, 2d, 3d}; - protected double[] vec2 = {4d, 5d, 6d}; - protected double[] vec3 = {7d, 8d, 9d}; - protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; - protected double[] vec5 = { -4d, 0d, 3d, 1d, -6d, 3d}; - protected double[] vec_null = {0d, 0d, 0d}; - protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; - protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}}; - // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; @@ -508,29 +495,12 @@ public class ArrayRealVectorTest extends RealVectorAbstractTest { } + @Override @Test public void testDataInOut() { + super.testDataInOut(); ArrayRealVector v1 = new ArrayRealVector(vec1); - ArrayRealVector v2 = new ArrayRealVector(vec2); - ArrayRealVector v4 = new ArrayRealVector(vec4); - RealVectorTestImpl v2_t = new RealVectorTestImpl(vec2); - - RealVector v_append_1 = v1.append(v2); - Assert.assertEquals("testData len", 6, v_append_1.getDimension()); - Assert.assertEquals("testData is 4.0 ", 4.0, v_append_1.getEntry(3), 0); - - RealVector v_append_2 = v1.append(2.0); - Assert.assertEquals("testData len", 4, v_append_2.getDimension()); - Assert.assertEquals("testData is 2.0 ", 2.0, v_append_2.getEntry(3), 0); - - RealVector v_append_4 = v1.append(v2_t); - Assert.assertEquals("testData len", 6, v_append_4.getDimension()); - Assert.assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3), 0); - - RealVector v_append_5 = v1.append((RealVector) v2); - Assert.assertEquals("testData len", 6, v_append_5.getDimension()); - Assert.assertEquals("testData is 4.0 ", 4.0, v_append_5.getEntry(3), 0); RealVector v_copy = v1.copy(); Assert.assertEquals("testData len", 3, v_copy.getDimension()); @@ -539,62 +509,6 @@ public class ArrayRealVectorTest extends RealVectorAbstractTest { double[] a_double = v1.toArray(); Assert.assertEquals("testData len", 3, a_double.length); Assert.assertNotSame("testData not same object ", v1.getDataRef(), a_double); - - -// ArrayRealVector vout4 = (ArrayRealVector) v1.clone(); -// Assert.assertEquals("testData len", 3, vout4.getDimension()); -// Assert.assertEquals("testData not same object ", v1.getDataRef(), vout4.getDataRef()); - - - RealVector vout5 = v4.getSubVector(3, 3); - Assert.assertEquals("testData len", 3, vout5.getDimension()); - Assert.assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1), 0); - try { - v4.getSubVector(3, 7); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - ArrayRealVector v_set1 = v1.copy(); - v_set1.setEntry(1, 11.0); - Assert.assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1), 0); - try { - v_set1.setEntry(3, 11.0); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - ArrayRealVector v_set3 = v1.copy(); - v_set3.set(13.0); - Assert.assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2), 0); - - try { - v_set3.getEntry(23); - Assert.fail("ArrayIndexOutOfBoundsException expected"); - } catch (ArrayIndexOutOfBoundsException ex) { - // expected behavior - } - - ArrayRealVector v_set4 = v4.copy(); - v_set4.setSubVector(3, v2_t); - Assert.assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3), 0); - Assert.assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6), 0); - try { - v_set4.setSubVector(7, v2_t); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - - ArrayRealVector vout10 = v1.copy(); - ArrayRealVector vout10_2 = v1.copy(); - Assert.assertEquals(vout10, vout10_2); - vout10_2.setEntry(0, 1.1); - Assert.assertNotSame(vout10, vout10_2); - } @Test @@ -1435,4 +1349,9 @@ public class ArrayRealVectorTest extends RealVectorAbstractTest { public RealVector create(final double[] data) { return new ArrayRealVector(data, true); } + + @Override + public RealVector createAlien(double[] data) { + return new RealVectorTestImpl(data); + } } diff --git a/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java b/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java index 7b75348cb..fe864679a 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java @@ -26,9 +26,122 @@ import org.junit.Test; public abstract class RealVectorAbstractTest { - + /** + * Creates a new instance of {@link RealVector}, with specified entries. + * The returned vector must be of the type currently tested. + * + * @param data the entries of the vector to be created + * @return a new {@link RealVector} of the type to be tested + */ public abstract RealVector create(double[] data); + /** + * Creates a new instance of {@link RealVector}, with specified entries. + * The type of the returned vector must be different from the type currently + * tested. + * + * @param data the entries of the vector to be created + * @return a new {@link RealVector} of an alien type + */ + public abstract RealVector createAlien(double[] data); + + protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}}; + protected double[] vec1 = {1d, 2d, 3d}; + protected double[] vec2 = {4d, 5d, 6d}; + protected double[] vec3 = {7d, 8d, 9d}; + protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; + protected double[] vec5 = { -4d, 0d, 3d, 1d, -6d, 3d}; + protected double[] vec_null = {0d, 0d, 0d}; + protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; + protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}}; + + @Test + public void testDataInOut() { + final RealVector v1 = create(vec1); + final RealVector v2 = create(vec2); + final RealVector v4 = create(vec4); + final RealVector v2_t = createAlien(vec2); + + final RealVector v_append_1 = v1.append(v2); + Assert.assertEquals("testData len", 6, v_append_1.getDimension()); + Assert.assertEquals("testData is 4.0 ", 4.0, v_append_1.getEntry(3), 0); + + final RealVector v_append_2 = v1.append(2.0); + Assert.assertEquals("testData len", 4, v_append_2.getDimension()); + Assert.assertEquals("testData is 2.0 ", 2.0, v_append_2.getEntry(3), 0); + + final RealVector v_append_4 = v1.append(v2_t); + Assert.assertEquals("testData len", 6, v_append_4.getDimension()); + Assert.assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3), 0); + + final RealVector v_append_5 = v1.append(v2); + Assert.assertEquals("testData len", 6, v_append_5.getDimension()); + Assert.assertEquals("testData is 4.0 ", 4.0, v_append_5.getEntry(3), 0); + + final RealVector vout5 = v4.getSubVector(3, 3); + Assert.assertEquals("testData len", 3, vout5.getDimension()); + Assert.assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1), 0); + 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); + try { + v_set1.setEntry(3, 11.0); + Assert.fail("OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected behavior + } + + final RealVector v_set2 = v4.copy(); + v_set2.setSubVector(3, v1); + Assert.assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3), 0); + Assert.assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6), 0); + try { + v_set2.setSubVector(7, v1); + Assert.fail("OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected behavior + } + + final RealVector v_set3 = v1.copy(); + v_set3.set(13.0); + Assert.assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2), 0); + + try { + v_set3.getEntry(23); + Assert.fail("OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected behavior + } + + final RealVector v_set4 = v4.copy(); + v_set4.setSubVector(3, v2_t); + Assert.assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3), 0); + Assert.assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6), 0); + try { + v_set4.setSubVector(7, v2_t); + Assert.fail("OutOfRangeException expected"); + } catch (OutOfRangeException ex) { + // expected behavior + } + + final RealVector vout10 = v1.copy(); + final RealVector vout10_2 = v1.copy(); + Assert.assertEquals(vout10, vout10_2); + vout10_2.setEntry(0, 1.1); + Assert.assertNotSame(vout10, vout10_2); + } + + /* + * TESTS OF THE VISITOR PATTERN + */ + /** The whole vector is visited. */ @Test public void testWalkInDefaultOrderPreservingVisitor1() { diff --git a/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java b/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java index cb494232c..acdb65b0f 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java @@ -335,4 +335,9 @@ public class RealVectorTest extends RealVectorAbstractTest{ public RealVector create(final double[] data) { return new OpenMapRealVector(data); } + + @Override + public RealVector createAlien(double[] data) { + return new TestVectorImpl(data); + } } diff --git a/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java b/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java index d4965bd7e..cb7e9ad35 100644 --- a/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/SparseRealVectorTest.java @@ -26,7 +26,6 @@ import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MathArithmeticException; -import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.analysis.function.Abs; import org.apache.commons.math3.analysis.function.Acos; import org.apache.commons.math3.analysis.function.Asin; @@ -59,17 +58,6 @@ import org.apache.commons.math3.analysis.function.Power; */ public class SparseRealVectorTest extends RealVectorAbstractTest { - // - protected double[][] ma1 = {{1d, 2d, 3d}, {4d, 5d, 6d}, {7d, 8d, 9d}}; - protected double[] vec1 = {1d, 2d, 3d}; - protected double[] vec2 = {4d, 5d, 6d}; - protected double[] vec3 = {7d, 8d, 9d}; - protected double[] vec4 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; - protected double[] vec5 = { -4d, 0d, 3d, 1d, -6d, 3d}; - protected double[] vec_null = {0d, 0d, 0d}; - protected Double[] dvec1 = {1d, 2d, 3d, 4d, 5d, 6d, 7d, 8d, 9d}; - protected double[][] mat1 = {{1d, 2d, 3d}, {4d, 5d, 6d},{ 7d, 8d, 9d}}; - // tolerances protected double entryTolerance = 10E-16; protected double normTolerance = 10E-14; @@ -339,82 +327,6 @@ public class SparseRealVectorTest extends RealVectorAbstractTest { } - @Test - public void testDataInOut() { - - OpenMapRealVector v1 = new OpenMapRealVector(vec1); - OpenMapRealVector v2 = new OpenMapRealVector(vec2); - OpenMapRealVector v4 = new OpenMapRealVector(vec4); - SparseRealVectorTestImpl v2_t = new SparseRealVectorTestImpl(vec2); - - RealVector v_append_1 = v1.append(v2); - Assert.assertEquals("testData len", 6, v_append_1.getDimension()); - Assert.assertEquals("testData is 4.0 ", 4.0, v_append_1.getEntry(3), 0); - - RealVector v_append_2 = v1.append(2.0); - Assert.assertEquals("testData len", 4, v_append_2.getDimension()); - Assert.assertEquals("testData is 2.0 ", 2.0, v_append_2.getEntry(3), 0); - - RealVector v_append_4 = v1.append(v2_t); - Assert.assertEquals("testData len", 6, v_append_4.getDimension()); - Assert.assertEquals("testData is 4.0 ", 4.0, v_append_4.getEntry(3), 0); - - RealVector vout5 = v4.getSubVector(3, 3); - Assert.assertEquals("testData len", 3, vout5.getDimension()); - Assert.assertEquals("testData is 4.0 ", 5.0, vout5.getEntry(1), 0); - try { - v4.getSubVector(3, 7); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - OpenMapRealVector v_set1 = v1.copy(); - v_set1.setEntry(1, 11.0); - Assert.assertEquals("testData is 11.0 ", 11.0, v_set1.getEntry(1), 0); - try { - v_set1.setEntry(3, 11.0); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - OpenMapRealVector v_set2 = v4.copy(); - v_set2.setSubVector(3, v1); - Assert.assertEquals("testData is 1.0 ", 1.0, v_set2.getEntry(3), 0); - Assert.assertEquals("testData is 7.0 ", 7.0, v_set2.getEntry(6), 0); - try { - v_set2.setSubVector(7, v1); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - OpenMapRealVector v_set3 = v1.copy(); - v_set3.set(13.0); - Assert.assertEquals("testData is 13.0 ", 13.0, v_set3.getEntry(2), 0); - - try { - v_set3.getEntry(23); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - OpenMapRealVector v_set4 = v4.copy(); - v_set4.setSubVector(3, v2_t); - Assert.assertEquals("testData is 1.0 ", 4.0, v_set4.getEntry(3), 0); - Assert.assertEquals("testData is 7.0 ", 7.0, v_set4.getEntry(6), 0); - try { - v_set4.setSubVector(7, v2_t); - Assert.fail("OutOfRangeException expected"); - } catch (OutOfRangeException ex) { - // expected behavior - } - - - } - @Test public void testMapFunctions() { OpenMapRealVector v1 = new OpenMapRealVector(vec1); @@ -960,4 +872,9 @@ public class SparseRealVectorTest extends RealVectorAbstractTest { public RealVector create(double[] data) { return new OpenMapRealVector(data); } + + @Override + public RealVector createAlien(double[] data) { + return new SparseRealVectorTestImpl(data); + } }