001: package JSci.tests;
002:
003: import java.lang.reflect.Constructor;
004: import java.util.Map;
005: import java.util.HashMap;
006: import junit.framework.*;
007: import JSci.maths.*;
008: import JSci.maths.vectors.*;
009: import JSci.maths.polynomials.*;
010:
011: /**
012: * Testcase for vectors.
013: * @author Mark Hale
014: */
015: public class VectorTest extends
016: junitx.extensions.EqualsHashCodeTestCase {
017: private final int N = 10;
018: private Constructor constructor;
019: private double array[];
020: private double array2[];
021:
022: public static void main(String arg[]) {
023: junit.textui.TestRunner.run(suite());
024: }
025:
026: public static Test suite() {
027: Class[] classes = new Class[] { DoubleVector.class,
028: DoubleSparseVector.class };
029: TestSuite suite = new TestSuite(VectorTest.class.toString());
030: for (int i = 0; i < classes.length; i++) {
031: Map properties = new HashMap();
032: properties.put("test.vector.class", classes[i]);
033: suite.addTest(new TestProperties(new TestSuite(
034: VectorTest.class, classes[i].toString()),
035: properties));
036: }
037: return suite;
038: }
039:
040: public VectorTest(String name) {
041: super (name);
042: }
043:
044: protected void setUp() throws Exception {
045: JSci.GlobalSettings.ZERO_TOL = 1.0e-6;
046: array = new double[N];
047: array2 = new double[N];
048: Class vectorClass = (Class) TestProperties.getProperties().get(
049: "test.vector.class");
050: constructor = vectorClass
051: .getConstructor(new Class[] { double[].class });
052: for (int i = 0; i < N; i++) {
053: array[i] = Math.random();
054: array2[i] = Math.random();
055: }
056: super .setUp();
057: }
058:
059: protected Object createInstance() {
060: try {
061: return constructor.newInstance(new Object[] { array });
062: } catch (Exception e) {
063: throw new RuntimeException(e);
064: }
065: }
066:
067: protected Object createNotEqualInstance() {
068: try {
069: return constructor.newInstance(new Object[] { array2 });
070: } catch (Exception e) {
071: throw new RuntimeException(e.getMessage());
072: }
073: }
074:
075: public void testConstructor() {
076: AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
077: assertEquals(N, vec.dimension());
078: for (int i = 0; i < vec.dimension(); i++)
079: assertEquals(array[i], vec.getComponent(i),
080: JSci.GlobalSettings.ZERO_TOL);
081: }
082:
083: public void testSetGet() {
084: AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
085: for (int i = 0; i < vec.dimension(); i++) {
086: vec.setComponent(i, -1.0);
087: assertEquals(-1.0, vec.getComponent(i),
088: JSci.GlobalSettings.ZERO_TOL);
089: vec.setComponent(i, array[i]);
090: }
091: }
092:
093: public void testAdd() {
094: AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
095: AbstractDoubleVector ans = vec.add(vec);
096: for (int i = 0; i < ans.dimension(); i++) {
097: String msg = vec.getClass().toString() + ": (" + i + ")";
098: assertEquals(msg, array[i] + array[i], ans.getComponent(i),
099: JSci.GlobalSettings.ZERO_TOL);
100: }
101: }
102:
103: public void testSubtract() {
104: AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
105: AbstractDoubleVector ans = vec.subtract(vec);
106: for (int i = 0; i < ans.dimension(); i++) {
107: String msg = vec.getClass().toString() + ": (" + i + ")";
108: assertEquals(msg, array[i] - array[i], ans.getComponent(i),
109: JSci.GlobalSettings.ZERO_TOL);
110: }
111: }
112:
113: public void testScalarProduct() {
114: AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
115: double ans = vec.scalarProduct(vec);
116: double sp = ArrayMath.scalarProduct(array, array);
117: String msg = vec.getClass().toString();
118: assertEquals(msg, sp, ans, JSci.GlobalSettings.ZERO_TOL);
119: }
120:
121: public void testMapComponents() {
122: AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
123: RealPolynomial map = new RealPolynomial(
124: new double[] { 2.0, 1.0 });
125: AbstractDoubleVector ans = vec.mapComponents(map);
126: for (int i = 0; i < ans.dimension(); i++) {
127: String msg = vec.getClass().toString() + ": (" + i + ")";
128: assertEquals(msg, map.map(array[i]), ans.getComponent(i),
129: JSci.GlobalSettings.ZERO_TOL);
130: }
131: }
132: }
|