package JSci.tests;

import java.lang.reflect.Constructor;
import junit.framework.*;
import JSci.maths.*;

/**
* Testcase for vectors.
* @author Mark Hale
*/
public class VectorTest extends junitx.extensions.EqualsHashCodeTestCase {
        private static final Class[] classes = new Class[] {
                DoubleVector.class, DoubleSparseVector.class
        };
        private static int classesIndex;
        private final Class vectorClass;
        private final int N = 5;
        private Constructor constructor;
        private double array[];
        private double array2[];

        public static void main(String arg[]) {
                junit.textui.TestRunner.run(suite());
        }
        public static Test suite() {
                TestSuite suite = new TestSuite(VectorTest.class.toString());
                for(classesIndex = 0; classesIndex < classes.length; classesIndex++)
                        suite.addTest(new TestSuite(VectorTest.class, classes[classesIndex].toString()));
                return suite;
        }
        public VectorTest(String name) {
                super(name);
                vectorClass = classes[classesIndex];
        }
        protected void setUp() throws Exception {
                JSci.GlobalSettings.ZERO_TOL=1.0e-6;
                array=new double[N];
                array2=new double[N];
                constructor = vectorClass.getConstructor(new Class[] {double[].class});
                for(int i=0; i<N; i++) {
                        array[i]=Math.random();
                        array2[i]=Math.random();
                }
                super.setUp();
        }
        protected Object createInstance() {
                try {
                        return constructor.newInstance(new Object[] {array});
                } catch(Exception e) {
                        throw new RuntimeException(e);
                }
        }
        protected Object createNotEqualInstance() {
                try {
                        return constructor.newInstance(new Object[] {array2});
                } catch(Exception e) {
                        throw new RuntimeException(e.getMessage());
                }
        }
        public void testConstructor() {
                AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
                assertEquals(N, vec.dimension());
                for(int i=0;i<vec.dimension();i++)
                        assertEquals(array[i], vec.getComponent(i), JSci.GlobalSettings.ZERO_TOL);
        }
        public void testSetGet() {
                AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
                for(int i=0;i<vec.dimension();i++) {
                        vec.setComponent(i,-1.0);
                        assertEquals(-1.0, vec.getComponent(i), JSci.GlobalSettings.ZERO_TOL);
                        vec.setComponent(i,array[i]);
                }
        }
        public void testAdd() {
                AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
                AbstractDoubleVector ans=vec.add(vec);
                for(int i=0;i<ans.dimension();i++)
                        assertEquals(array[i]+array[i], ans.getComponent(i), JSci.GlobalSettings.ZERO_TOL);
        }
        public void testSubtract() {
                AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
                AbstractDoubleVector ans=vec.subtract(vec);
                for(int i=0;i<ans.dimension();i++)
                        assertEquals(array[i]-array[i], ans.getComponent(i), JSci.GlobalSettings.ZERO_TOL);
        }
        public void testScalarProduct() {
                AbstractDoubleVector vec = (AbstractDoubleVector) createInstance();
                double ans=vec.scalarProduct(vec);
                double sp=ArrayMath.scalarProduct(array, array);
                assertEquals(sp, ans, JSci.GlobalSettings.ZERO_TOL);
        }
}

