1 package JSci.tests; 2 3 import java.lang.reflect.Constructor ; 4 import java.util.Map ; 5 import java.util.HashMap ; 6 import junit.framework.*; 7 import JSci.maths.*; 8 import JSci.maths.vectors.*; 9 import JSci.maths.polynomials.*; 10 11 15 public class VectorTest extends junitx.extensions.EqualsHashCodeTestCase { 16 private final int N = 10; 17 private Constructor constructor; 18 private double array[]; 19 private double array2[]; 20 21 public static void main(String arg[]) { 22 junit.textui.TestRunner.run(suite()); 23 } 24 public static Test suite() { 25 Class [] classes = new Class [] { 26 DoubleVector.class, DoubleSparseVector.class 27 }; 28 TestSuite suite = new TestSuite(VectorTest.class.toString()); 29 for(int i=0; i<classes.length; i++) { 30 Map properties = new HashMap (); 31 properties.put("test.vector.class", classes[i]); 32 suite.addTest(new TestProperties(new TestSuite(VectorTest.class, classes[i].toString()), properties)); 33 } 34 return suite; 35 } 36 public VectorTest(String name) { 37 super(name); 38 } 39 protected void setUp() throws Exception { 40 JSci.GlobalSettings.ZERO_TOL=1.0e-6; 41 array=new double[N]; 42 array2=new double[N]; 43 Class vectorClass = (Class ) TestProperties.getProperties().get("test.vector.class"); 44 constructor = vectorClass.getConstructor(new Class [] {double[].class}); 45 for(int i=0; i<N; i++) { 46 array[i]=Math.random(); 47 array2[i]=Math.random(); 48 } 49 super.setUp(); 50 } 51 protected Object createInstance() { 52 try { 53 return constructor.newInstance(new Object [] {array}); 54 } catch(Exception e) { 55 throw new RuntimeException (e); 56 } 57 } 58 protected Object createNotEqualInstance() { 59 try { 60 return constructor.newInstance(new Object [] {array2}); 61 } catch(Exception e) { 62 throw new RuntimeException (e.getMessage()); 63 } 64 } 65 public void testConstructor() { 66 AbstractDoubleVector vec = (AbstractDoubleVector) createInstance(); 67 assertEquals(N, vec.dimension()); 68 for(int i=0;i<vec.dimension();i++) 69 assertEquals(array[i], vec.getComponent(i), JSci.GlobalSettings.ZERO_TOL); 70 } 71 public void testSetGet() { 72 AbstractDoubleVector vec = (AbstractDoubleVector) createInstance(); 73 for(int i=0;i<vec.dimension();i++) { 74 vec.setComponent(i,-1.0); 75 assertEquals(-1.0, vec.getComponent(i), JSci.GlobalSettings.ZERO_TOL); 76 vec.setComponent(i,array[i]); 77 } 78 } 79 public void testAdd() { 80 AbstractDoubleVector vec = (AbstractDoubleVector) createInstance(); 81 AbstractDoubleVector ans=vec.add(vec); 82 for(int i=0;i<ans.dimension();i++) { 83 String msg = vec.getClass().toString()+": ("+i+")"; 84 assertEquals(msg, array[i]+array[i], ans.getComponent(i), JSci.GlobalSettings.ZERO_TOL); 85 } 86 } 87 public void testSubtract() { 88 AbstractDoubleVector vec = (AbstractDoubleVector) createInstance(); 89 AbstractDoubleVector ans=vec.subtract(vec); 90 for(int i=0;i<ans.dimension();i++) { 91 String msg = vec.getClass().toString()+": ("+i+")"; 92 assertEquals(msg, array[i]-array[i], ans.getComponent(i), JSci.GlobalSettings.ZERO_TOL); 93 } 94 } 95 public void testScalarProduct() { 96 AbstractDoubleVector vec = (AbstractDoubleVector) createInstance(); 97 double ans=vec.scalarProduct(vec); 98 double sp=ArrayMath.scalarProduct(array, array); 99 String msg = vec.getClass().toString(); 100 assertEquals(msg, sp, ans, JSci.GlobalSettings.ZERO_TOL); 101 } 102 public void testMapComponents() { 103 AbstractDoubleVector vec = (AbstractDoubleVector) createInstance(); 104 RealPolynomial map = new RealPolynomial(new double[] {2.0, 1.0}); 105 AbstractDoubleVector ans = vec.mapComponents(map); 106 for(int i=0;i<ans.dimension();i++) { 107 String msg = vec.getClass().toString()+": ("+i+")"; 108 assertEquals(msg, map.map(array[i]), ans.getComponent(i), JSci.GlobalSettings.ZERO_TOL); 109 } 110 } 111 } 112
| Popular Tags
|