1 package org.lsmp.djepJUnit; 2 3 import junit.framework.*; 4 import org.nfunk.jep.*; 5 import org.nfunk.jep.type.*; 6 import org.lsmp.djep.matrixJep.*; 7 import org.lsmp.djep.vectorJep.values.*; 8 import org.lsmp.djep.rpe.*; 9 10 17 18 24 public class RpTest extends TestCase { 25 JEP j; 26 public static final boolean SHOW_BAD=false; 27 28 public RpTest(String name) { 29 super(name); 30 } 31 32 public static void main(String args[]) { 33 35 TestSuite suite= new TestSuite(RpTest.class); 36 suite.run(new TestResult()); 39 } 40 41 String matStrs[][] = new String [10][10]; 42 String vecStrs[] = new String [10]; 43 44 protected void setUp() { 45 j = new JEP(); 46 j.addStandardConstants(); 47 j.addStandardFunctions(); 48 j.addComplex(); 49 j.setAllowAssignment(true); 51 j.setAllowUndeclared(true); 52 j.setImplicitMul(true); 53 54 } 55 56 public static Test suite() { 57 return new TestSuite(RpTest.class); 58 } 59 60 public void testGood() 61 { 62 assertEquals(1,1); 63 } 64 65 public void myAssertEquals(String msg,String actual,String expected) 66 { 67 if(!actual.equals(expected)) 68 System.out.println("Error \""+msg+"\" is \n<"+actual+"> should be \n<"+expected+">"); 69 assertEquals("<"+msg+">",expected,actual); 70 System.out.println("Success: Value of <"+msg+"> is <"+actual+">"); 71 } 72 73 public void valueTest(String expr,double dub) throws ParseException,Exception 74 { 75 valueTest(expr,new Double (dub)); 76 } 77 public void valueTest(String expr,Object expected) throws ParseException,Exception 78 { 79 Node node = j.parse(expr); 80 Object res = j.evaluate(node); 81 if(j.hasError()) 82 fail("Evaluation Failure: "+expr+j.getErrorInfo()); 83 assertEquals("<"+expr+">",expected,res); 84 System.out.println("Sucess value of <"+expr+"> is "+res); 85 } 86 87 public void valueTest(String expr,String expected) throws ParseException,Exception 88 { 89 Node node = j.parse(expr); 90 Object res = j.evaluate(node); 91 if(j.hasError()) 92 fail("Evaluation Failure: "+expr+j.getErrorInfo()); 93 assertEquals("<"+expr+">",expected,res.toString()); 94 System.out.println("Sucess value of <"+expr+"> is "+res.toString()); 95 } 96 97 public void complexValueTest(String expr,Complex expected,double tol) throws Exception 98 { 99 Node node = j.parse(expr); 100 Object res = j.evaluate(node); 101 assertTrue("<"+expr+"> expected: <"+expected+"> but was <"+res+">", 102 expected.equals((Complex) res,tol)); 103 System.out.println("Sucess value of <"+expr+"> is "+res); 104 } 105 106 public Object calcValue(String expr) throws ParseException,Exception 107 { 108 Node node = j.parse(expr); 109 Object res = j.evaluate(node); 110 return res; 111 } 112 136 137 156 void rpTest(String eqns[], String eqn2) throws ParseException,Exception 157 { 158 for(int i=0;i<eqns.length;++i) { 159 System.out.println("eqns "+eqns[i]); 160 Node node = j.parse(eqns[i]); 161 j.evaluate(node); 162 } 163 Node node3 = j.parse(eqn2); 164 RpEval rpe = new RpEval(j); 165 RpCommandList list = rpe.compile(node3); 166 double rpRes = rpe.evaluate(list); 168 169 Object matRes = j.evaluate(node3); 170 if(j.hasError()) 172 fail("Evaluation Failure: "+eqn2+j.getErrorInfo()); 173 myAssertEquals("<"+eqn2+">",""+rpRes,matRes.toString()); 174 175 if(!matRes.equals(new Double (rpRes))) 176 fail("Expected <"+matRes+"> found <"+rpRes+">"); 177 } 178 179 180 void rpTest2(String eqns[]) throws ParseException,Exception 181 { 182 Node nodes[] = new Node[eqns.length]; 183 double rpRes[] = new double[eqns.length]; 184 RpEval rpe = new RpEval(j); 185 for(int i=0;i<eqns.length;++i) { 186 System.out.println("eqns "+eqns[i]); 187 nodes[i] = j.parse(eqns[i]); 188 RpCommandList list = rpe.compile(nodes[i]); 189 rpRes[i] = rpe.evaluate(list); 190 System.out.println("<"+eqns[i]+"> "+rpRes[i]); 191 } 192 for(int i=0;i<eqns.length;++i) { 193 Object matRes = j.evaluate(nodes[i]); 194 if(!matRes.equals(new Double (rpRes[i]))) 195 fail("Expected <"+matRes+"> found <"+rpRes[i]+">"); 196 } 197 rpe.cleanUp(); 198 } 199 200 public void testRp() throws ParseException,Exception 201 { 202 rpTest(new String [0],"1*2*3+4*5*6+7*8*9"); 203 204 rpTest(new String []{"x1=1","x2=2","x3=3","x4=4","x5=5","x6=6","x7=7","x8=8","x9=9"}, 205 "x1*x2*x3+x4*x5*x6+x7*x8*x9"); 206 207 } 208 209 public void testAssign() throws ParseException,Exception 210 { 211 rpTest2(new String []{"x=5","x+x"}); 212 j.setVarValue("x",new Double (6.0)); 213 rpTest2(new String []{"x+x"}); 214 } 215 216 public void testLogical() throws ParseException,Exception 217 { 218 rpTest2(new String []{"1&&1","1&&0","0&&0","0&&1","3.14&&1"}); 219 rpTest2(new String []{"1||1","1||0","0||0","0||1","3.14||0"}); 220 rpTest2(new String []{"!0","!1","!3.14","!-3.14"}); 221 222 rpTest2(new String []{"1>1","1>0","0>0","0>1","3.14>1"}); 223 rpTest2(new String []{"1<1","1<0","0<0","0<1","3.14<1"}); 224 rpTest2(new String []{"1>=1","1>=0","0>=0","0>=1","3.14>=1"}); 225 rpTest2(new String []{"1<=1","1<=0","0<=0","0<=1","3.14<=1"}); 226 rpTest2(new String []{"1==1","1==0","0==0","0==1","3.14==1"}); 227 rpTest2(new String []{"1!=1","1!=0","0!=0","0!=1","3.14!=1"}); 228 229 } 230 boolean TESTALL = false; 231 public void testFun() throws ParseException,Exception 232 { 233 rpTest2(new String []{"x=5","y=4","x/y","x%y","x^y"}); 234 rpTest2(new String []{"x=0.5","cos(x)","sin(x)","tan(x)","asin(x)","acos(x)","atan(x)"}); 235 rpTest2(new String []{"x=0.5","cosh(x)","sinh(x)","tanh(x)","asinh(x)","acosh(x+1)","atanh(x)"}); 236 rpTest2(new String []{"x=0.5","sqrt(x)","ln(x)","log(x)","exp(x)","abs(x)"}); 237 238 rpTest2(new String []{"x=0.5","cos(x)^2+sin(x)^2"}); 239 } 241 600 } 601 | Popular Tags |