From 53fb7e87040a65dd93b80d68a424179befd61a73 Mon Sep 17 00:00:00 2001 From: Sebastien Brisard Date: Fri, 6 Jul 2012 18:24:50 +0000 Subject: [PATCH] MATH-795: moved minimal implementation of RealVector, ArrayRealVectorTest.RealVectorTestImpl to RealVectorAbstractTest.RealVectorTestImpl. This minimal implementation is now used by RealVectorTest and ArrayRealVectorTest. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1358334 13f79535-47bb-0310-9956-ffa450edef68 --- .../math3/linear/ArrayRealVectorTest.java | 99 ----------- .../math3/linear/RealVectorAbstractTest.java | 98 ++++++++++ .../commons/math3/linear/RealVectorTest.java | 168 ++---------------- 3 files changed, 113 insertions(+), 252 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 46f457a32..2c21ffa6c 100644 --- a/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/ArrayRealVectorTest.java @@ -30,105 +30,6 @@ import org.junit.Test; */ public class ArrayRealVectorTest extends RealVectorAbstractTest { - /** - * Minimal implementation of the {@link RealVector} abstract class, for - * mixed types unit tests. - */ - public static class RealVectorTestImpl extends RealVector - implements Serializable { - - /** Serializable version identifier. */ - private static final long serialVersionUID = 20120706L; - - /** Entries of the vector. */ - protected double data[]; - - public RealVectorTestImpl(double[] d) { - data = d.clone(); - } - - private UnsupportedOperationException unsupported() { - return new UnsupportedOperationException("Not supported, unneeded for test purposes"); - } - - @Override - public RealVector mapToSelf(UnivariateFunction function) { - for (int i = 0; i < data.length; i++) { - data[i] = function.value(data[i]); - } - return this; - } - - @Override - public RealVector copy() { - return new RealVectorTestImpl(data); - } - - @Override - public RealVector ebeMultiply(RealVector v) { - throw unsupported(); - } - - @Override - public RealVector ebeDivide(RealVector v) { - throw unsupported(); - } - - @Override - public double getEntry(int index) { - return data[index]; - } - - @Override - public int getDimension() { - return data.length; - } - - @Override - public RealVector append(RealVector v) { - throw unsupported(); - } - - @Override - public RealVector append(double d) { - throw unsupported(); - } - - @Override - public RealVector getSubVector(int index, int n) { - throw unsupported(); - } - - @Override - public void setEntry(int index, double value) { - throw unsupported(); - } - - @Override - public void setSubVector(int index, RealVector v) { - throw unsupported(); - } - - @Override - public void set(double value) { - throw unsupported(); - } - - @Override - public double[] toArray() { - return data.clone(); - } - - @Override - public boolean isNaN() { - throw unsupported(); - } - - @Override - public boolean isInfinite() { - throw unsupported(); - } - } @Override public RealVector create(final double[] 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 695f53f10..e76f9ec3e 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorAbstractTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math3.linear; +import java.io.Serializable; import java.util.Arrays; import java.util.Iterator; import java.util.NoSuchElementException; @@ -1903,4 +1904,101 @@ public abstract class RealVectorAbstractTest { Assert.assertEquals("entry " + i, i + data[i], v.getEntry(i), 0.0); } } + + /** + * Minimal implementation of the {@link RealVector} abstract class, for + * mixed types unit tests. + */ + public static class RealVectorTestImpl extends RealVector + implements Serializable { + + /** Serializable version identifier. */ + private static final long serialVersionUID = 20120706L; + + /** Entries of the vector. */ + protected double data[]; + + public RealVectorTestImpl(double[] d) { + data = d.clone(); + } + + private UnsupportedOperationException unsupported() { + return new UnsupportedOperationException("Not supported, unneeded for test purposes"); + } + + @Override + public RealVector mapToSelf(UnivariateFunction function) { + for (int i = 0; i < data.length; i++) { + data[i] = function.value(data[i]); + } + return this; + } + + @Override + public RealVector copy() { + return new RealVectorTestImpl(data); + } + + @Override + public RealVector ebeMultiply(RealVector v) { + throw unsupported(); + } + + @Override + public RealVector ebeDivide(RealVector v) { + throw unsupported(); + } + + @Override + public double getEntry(int index) { + checkIndex(index); + return data[index]; + } + + @Override + public int getDimension() { + return data.length; + } + + @Override + public RealVector append(RealVector v) { + throw unsupported(); + } + + @Override + public RealVector append(double d) { + throw unsupported(); + } + + @Override + public RealVector getSubVector(int index, int n) { + throw unsupported(); + } + + @Override + public void setEntry(int index, double value) { + checkIndex(index); + data[index] = value; + } + + @Override + public void setSubVector(int index, RealVector v) { + throw unsupported(); + } + + @Override + public double[] toArray() { + return data.clone(); + } + + @Override + public boolean isNaN() { + throw unsupported(); + } + + @Override + public boolean isInfinite() { + throw unsupported(); + } + } } 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 9d0d1a92b..84beb0fb1 100644 --- a/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java +++ b/src/test/java/org/apache/commons/math3/linear/RealVectorTest.java @@ -17,19 +17,15 @@ package org.apache.commons.math3.linear; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.Assert; -import org.apache.commons.math3.analysis.UnivariateFunction; -import org.apache.commons.math3.exception.DimensionMismatchException; -import org.apache.commons.math3.exception.OutOfRangeException; -import org.apache.commons.math3.exception.util.LocalizedFormats; -import org.apache.commons.math3.linear.RealVector.Entry; -import org.apache.commons.math3.util.MathArrays; - import java.util.Iterator; import java.util.Random; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.linear.RealVector.Entry; +import org.junit.Assert; +import org.junit.Ignore; +import org.junit.Test; + /** * Tests for {@link RealVector}. */ @@ -37,148 +33,14 @@ public class RealVectorTest extends RealVectorAbstractTest{ private double[] vec1 = { 1d, 2d, 3d, 4d, 5d }; private double[] vec2 = { -3d, 0d, 0d, 2d, 1d }; - private static class TestVectorImpl extends RealVector { - private double[] values; - - TestVectorImpl(double[] values) { - this.values = MathArrays.copyOf(values); - } - - @Override - public double[] toArray() { return values; } - - @Override - public RealVector copy() { - return new TestVectorImpl(values.clone()); - } - - UnsupportedOperationException unsupported() { - return new UnsupportedOperationException("Test implementation only supports methods necessary for testing"); - } - - @Override - public RealVector mapAddToSelf(double d) { - for(int i=0; i it = v.sparseIterator(); it.hasNext() && (e = it.next()) != null; ) { Assert.assertEquals(onlyOne[1], e.getValue(), 0); } @@ -370,9 +232,9 @@ public class RealVectorTest extends RealVectorAbstractTest{ final double a = 1d; final double b = 2d; double[] aux = new double[] { 3d, 4d, 5d }; - final TestVectorImpl x = new TestVectorImpl(aux); + final RealVector x = new RealVectorTestImpl(aux); aux = new double[] { 6d, 7d }; - final TestVectorImpl y = new TestVectorImpl(aux); + final RealVector y = new RealVectorTestImpl(aux); x.combineToSelf(a, b, y); } @@ -382,8 +244,8 @@ public class RealVectorTest extends RealVectorAbstractTest{ final int dim = 10; final double a = (2 * random.nextDouble() - 1); final double b = (2 * random.nextDouble() - 1); - final RealVector x = new TestVectorImpl(new double[dim]); - final RealVector y = new TestVectorImpl(new double[dim]); + final RealVector x = new RealVectorTestImpl(new double[dim]); + final RealVector y = new RealVectorTestImpl(new double[dim]); final double[] expected = new double[dim]; for (int i = 0; i < dim; i++) { final double xi = 2 * random.nextDouble() - 1; @@ -410,7 +272,7 @@ public class RealVectorTest extends RealVectorAbstractTest{ @Test public void testAddToEntry() { final double[] v = new double[] { 1, 2, 3 }; - final RealVector x = new TestVectorImpl(v.clone()); + final RealVector x = new RealVectorTestImpl(v.clone()); final double inc = 7; for (int i = 0; i < x.getDimension(); i++) { x.addToEntry(i, inc);