1 package org.lsmp.djepJUnit; 2 3 import junit.framework.*; 4 5 import org.nfunk.jep.*; 6 import org.lsmp.djep.djep.*; 7 8 15 16 20 public class DJepTest extends TestCase { 21 DJep j; 22 public static final boolean SHOW_BAD=false; 23 24 public DJepTest(String name) { 25 super(name); 26 } 27 28 public static void main(String args[]) { 29 31 TestSuite suite= new TestSuite(DJepTest.class); 32 suite.run(new TestResult()); 35 } 36 37 protected void setUp() { 38 j = new DJep(); 39 j.addStandardConstants(); 40 j.addStandardFunctions(); 41 j.addComplex(); 42 j.setAllowAssignment(true); 44 j.setAllowUndeclared(true); 45 j.setImplicitMul(true); 46 j.addStandardDiffRules(); 47 } 48 49 public static Test suite() { 50 return new TestSuite(DJepTest.class); 51 } 52 53 public void testGood() 54 { 55 assertEquals(1,1); 56 } 57 58 public void valueTest(String expr,double dub) 59 { 60 valueTest(expr,new Double (dub)); 61 } 62 public void valueTest(String expr,Object expected) 63 { 64 j.parseExpression(expr); 65 if(j.hasError()) 66 fail("Parse Failure: "+expr); 67 Object res = j.getValueAsObject(); 68 if(j.hasError()) 69 fail("Evaluation Failure: "+expr+j.getErrorInfo()); 70 assertEquals("<"+expr+">",expected,res); 71 System.out.println("Sucess value of <"+expr+"> is "+res); 72 } 73 74 public Object calcValue(String expr) 75 { 76 j.parseExpression(expr); 77 if(j.hasError()) 78 fail("Parse Failure: "+expr); 79 return j.getValueAsObject(); 80 } 81 82 public void simplifyTest(String expr,String expected) throws ParseException 83 { 84 Node node = j.parse(expr); 85 Node processed = j.preprocess(node); 86 Node simp = j.simplify(processed); 87 String res = j.toString(simp); 88 89 Node node2 = j.parse(expected); 90 Node processed2 = j.preprocess(node2); 91 Node simp2 = j.simplify(processed2); 92 String res2 = j.toString(simp2); 93 94 if(!res2.equals(res)) 95 System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+res2+"\""); 96 assertEquals("<"+expr+">",res2,res); 97 System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\""); 98 99 104 } 105 106 public void simplifyTestString(String expr,String expected) throws ParseException 107 { 108 Node node = j.parse(expr); 109 Node processed = j.preprocess(node); 110 Node simp = j.simplify(processed); 111 String res = j.toString(simp); 112 113 if(!expected.equals(res)) 114 System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+expected+"\""); 115 assertEquals("<"+expr+">",expected,res); 116 System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\""); 117 118 123 } 124 125 public Node parseProcSimpEval(String expr,Object expected) throws ParseException,Exception 126 { 127 Node node = j.parse(expr); 128 Node processed = j.preprocess(node); 129 Node simp = j.simplify(processed); 130 Object res = j.evaluate(simp); 131 132 if(!expected.equals(res)) 133 System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+expected+"\""); 134 assertEquals("<"+expr+">",expected,res); 135 System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\""); 136 return simp; 137 } 138 139 140 public void testSimpleSum() 141 { 142 valueTest("1+2",3); 143 valueTest("2*6+3",15); 144 valueTest("2*(6+3)",18); 145 } 146 147 public void testOperators() 148 { 149 156 valueTest("T=1",1); 157 valueTest("F=0",0); 158 calcValue("a=F"); calcValue("b=F"); calcValue("c=F"); 159 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 160 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 161 calcValue("a=F"); calcValue("b=F"); calcValue("c=T"); 162 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 163 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 164 calcValue("a=F"); calcValue("b=T"); calcValue("c=F"); 165 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 166 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 167 calcValue("a=F"); calcValue("b=T"); calcValue("c=T"); 168 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 169 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 170 171 calcValue("a=T"); calcValue("b=F"); calcValue("c=F"); 172 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 173 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 174 calcValue("a=T"); calcValue("b=F"); calcValue("c=T"); 175 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 176 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 177 calcValue("a=T"); calcValue("b=T"); calcValue("c=F"); 178 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 179 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 180 calcValue("a=T"); calcValue("b=T"); calcValue("c=T"); 181 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 182 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 183 } 184 185 public void testEval() throws ParseException 186 { 187 } 190 191 public void testSimp() throws ParseException 192 { 193 simplifyTest("2+3","5"); 194 simplifyTest("2*3","6"); 195 simplifyTest("2^3","8"); 196 simplifyTest("3/2","1.5"); 197 simplifyTest("2*3+4","10"); 198 simplifyTest("2*(3+4)","14"); 199 200 simplifyTest("0+x","x"); 201 simplifyTest("x+0","x"); 202 simplifyTest("0-x","0-x"); 203 simplifyTest("x-0","x"); 204 simplifyTest("0*x","0"); 205 simplifyTest("x*0","0"); 206 simplifyTest("1*x","x"); 207 simplifyTest("x*1","x"); 208 simplifyTest("-1*x","-x"); 209 simplifyTest("x*-1","-x"); 210 simplifyTest("-(-x)","x"); 211 simplifyTest("-(-(-x))","-x"); 212 simplifyTest("(-1)*(-1)*x","x"); 213 simplifyTest("(-1)*(-1)*(-1)*x","-x"); 214 215 simplifyTest("0/x","0"); 216 simplifyTest("x/0","1/0"); 217 218 simplifyTest("x^0","1"); 219 simplifyTest("x^1","x"); 220 simplifyTest("0^x","0"); 221 simplifyTest("1^x","1"); 222 223 simplifyTest("(2+3)+x","5+x"); 225 simplifyTest("(2+x)+3","5+x"); 226 simplifyTest("(x+2)+3","5+x"); 227 simplifyTest("x+(2+3)","5+x"); 229 simplifyTest("2+(x+3)","5+x"); 230 simplifyTest("2+(3+x)","5+x"); 231 simplifyTest("(2+3)-x","5-x"); 233 simplifyTest("(2+x)-3","x-1"); 234 simplifyTest("(x+2)-3","x-1"); 235 simplifyTest("(2-3)+x","-1+x"); 237 simplifyTest("(2-x)+3","5-x"); 238 simplifyTest("(x-2)+3","1+x"); 239 simplifyTest("x-(2+3)","x-5"); 241 simplifyTest("2-(x+3)","-1-x"); 242 simplifyTest("2-(3+x)","-1-x"); 243 simplifyTest("x+(2-3)","x-1"); 245 simplifyTest("2+(x-3)","-1+x"); 246 simplifyTest("2+(3-x)","5-x"); 247 simplifyTest("x-(2-3)","1+x"); 249 simplifyTest("2-(x-3)","5-x"); 250 simplifyTest("2-(3-x)","-1+x"); 251 simplifyTest("(2-3)-x","-1-x"); 253 simplifyTest("(2-x)-3","-1-x"); 254 simplifyTest("(x-2)-3","x-5"); 255 256 simplifyTest("(2*3)*x","6*x"); 258 simplifyTest("(2*x)*3","6*x"); 259 simplifyTest("(x*2)*3","6*x"); 260 simplifyTest("x*(2*3)","6*x"); 262 simplifyTest("2*(x*3)","6*x"); 263 simplifyTest("2*(3*x)","6*x"); 264 simplifyTest("(2*3)/x","6/x"); 266 simplifyTest("(3*x)/2","1.5*x"); 267 simplifyTest("(x*3)/2","1.5*x"); 268 simplifyTest("(3/2)*x","1.5*x"); 270 simplifyTest("(3/x)*2","6/x"); 271 simplifyTest("(x/2)*3","1.5*x"); 272 simplifyTest("x/(2*3)","x/6"); 274 simplifyTest("3/(x*2)","1.5/x"); 275 simplifyTest("3/(2*x)","1.5/x"); 276 simplifyTest("x*(3/2)","1.5*x"); 278 simplifyTest("3*(x/2)","1.5*x"); 279 simplifyTest("3*(2/x)","6/x"); 280 simplifyTest("x/(3/2)","x/1.5"); 282 simplifyTest("2/(x/3)","6/x"); 283 simplifyTest("3/(2/x)","1.5*x"); 284 simplifyTest("(3/2)/x","1.5/x"); 286 simplifyTest("(3/x)/2","1.5/x"); 287 simplifyTest("(x/3)/2","x/6"); 288 289 290 simplifyTest("x*(3+2)","5*x"); 291 simplifyTest("3*(x+2)","6+3*x"); 292 simplifyTest("3*(2+x)","6+3*x"); 293 simplifyTest("(3+2)*x","5*x"); 294 simplifyTest("(3+x)*2","6+2*x"); 295 simplifyTest("(x+3)*2","6+x*2"); 296 297 simplifyTest("x*(3-2)","x"); 298 simplifyTest("3*(x-2)","-6+3*x"); 299 simplifyTest("3*(2-x)","6-3*x"); 300 simplifyTest("(3-2)*x","x"); 301 simplifyTest("(3-x)*2","6-2*x"); 302 simplifyTest("(x-3)*2","-6+2*x"); 303 304 simplifyTest("3+(x/4)","3+x/4"); 305 simplifyTest("2*(x/4)","0.5*x"); 306 simplifyTest("(2*(3+(x/4)))","6+0.5*x"); 307 simplifyTest("1+(2*(3+(x/4)))","7+0.5*x"); 308 simplifyTest("((3+(x/4))*2)+1","7+0.5*x"); 309 310 } 311 312 public void testIf() 313 { 314 valueTest("if(1,2,3)",2); 315 valueTest("if(-1,2,3)",3); 316 valueTest("if(0,2,3)",3); 317 valueTest("if(1,2,3,4)",2); 318 valueTest("if(-1,2,3,4)",3); 319 valueTest("if(0,2,3,4)",4); 320 valueTest("if(0>=0,2,3,4)",2); 321 valueTest("x=3",3); 322 valueTest("if(x==3,1,-1)",1); 323 valueTest("if(x!=3,1,-1)",-1); 324 valueTest("if(x>=3,1,-1)",1); 325 valueTest("if(x>3,1,-1)",-1); 326 valueTest("if(x<=3,1,-1)",1); 327 valueTest("if(x<3,1,-1)",-1); 328 } 329 330 public void testAssign() 331 { 332 valueTest("x=3",3); 333 valueTest("y=3+4",7); 334 valueTest("z=x+y",10); 335 valueTest("a=b=c=z",10); 336 valueTest("b",10); 337 valueTest("d=f=a-b",0); 338 valueTest("x=2",2); 339 valueTest("(x*x)*x*(x*x)",32.0); JEP j2 = new org.lsmp.djep.vectorJep.VectorJep(); 341 valueTest("(x*x)*x*(x*x)",32.0); 342 } 346 347 348 public void testDiff() throws ParseException 349 { 350 simplifyTest("diff(x^2,x)","2 x"); 351 simplifyTest("diff(x^3,x)","3 x^2"); 352 simplifyTest("diff(x,x)","1"); 353 simplifyTest("diff(1,x)","0"); 354 simplifyTest("diff(x^2+x+1,x)","2 x+1"); 355 simplifyTest("diff((x+x^2)*(x+x^3),x)","(1+2*x)*(x+x^3)+(x+x^2)*(1+3*x^2)"); 356 simplifyTest("diff((x+x^2)/(x+x^3),x)","((1+2*x)*(x+x^3)-(x+x^2)*(1+3*x^2))/((x+x^3)*(x+x^3))"); 357 358 simplifyTest("diff(y^x,x)","y^x*ln(y)"); 359 simplifyTest("diff(e^x,x)","e^x*ln(e)"); 360 361 simplifyTest("diff(sin(x),x)","cos(x)"); 362 363 simplifyTest("diff((x+1)^2,x)","2+2*x"); 364 simplifyTest("diff((x+y)^2,x)","2*(x+y)"); 365 simplifyTest("diff((x+x^2)^3,x)","3*(x+x^2)^2*(1+2*x)"); 366 367 simplifyTest("diff(sin(x+1),x)","cos(x+1)"); 368 simplifyTest("diff(sin(x+x^2),x)","cos(x+x^2)*(1+2*x)"); 369 370 simplifyTest("diff(cos(x),x)","-sin(x)"); 371 simplifyTest("diff(tan(x),x)","1/((cos(x))^2)"); 372 373 simplifyTest("diff(sec(x),x)","sec(x)*tan(x)"); 374 simplifyTest("diff(cosec(x),x)","-cosec(x) * cot(x)"); 375 simplifyTest("diff(cot(x),x)","-(cosec(x))^2"); 376 377 simplifyTest("diff(sec(x),x)","sec(x) * tan(x)"); 378 simplifyTest("diff(cosec(x),x)","-cosec(x) * cot(x)"); 379 simplifyTest("diff(cot(x),x)","-(cosec(x))^2"); 380 381 simplifyTest("diff(asin(x),x)","1/(sqrt(1-x^2))"); 382 simplifyTest("diff(acos(x),x)","-1/(sqrt(1-x^2))"); 383 simplifyTest("diff(atan(x),x)","1/(1+x^2)"); 384 385 simplifyTest("diff(sinh(x),x)","cosh(x)"); 386 simplifyTest("diff(cosh(x),x)","sinh(x)"); 387 simplifyTest("diff(tanh(x),x)","1-(tanh(x))^2"); 388 389 simplifyTest("diff(asinh(x),x)","1/(sqrt(1+x^2))"); 390 simplifyTest("diff(acosh(x),x)","1/(sqrt(x^2-1))"); 391 simplifyTest("diff(atanh(x),x)","1/(1-x^2)"); 392 393 simplifyTest("diff(sqrt(x),x)","1/(2 (sqrt(x)))"); 394 395 simplifyTest("diff(exp(x),x)","exp(x)"); 396 simplifyTest("diff(ln(x),x)","1/x"); 397 simplifyTest("diff(log(x),x)","(1/ln(10)) /x"); 398 simplifyTest("diff(abs(x),x)","abs(x)/x"); 399 simplifyTest("diff(angle(x,y),x)","y/(x^2+y^2)"); 400 simplifyTest("diff(angle(x,y),y)","-x/(x^2+y^2)"); 401 simplifyTest("diff(mod(x,y),x)","1"); 402 simplifyTest("diff(mod(x,y),y)","0"); 403 simplifyTest("diff(sum(x,x^2,x^3),x)","sum(1,2 x,3 x^2)"); 404 405 } 421 422 public void myAssertEquals(String msg,String actual,String expected) 423 { 424 if(!actual.equals(expected)) 425 System.out.println("Error \""+msg+"\" is \""+actual+" should be "+expected+"\""); 426 assertEquals("<"+msg+">",expected,actual); 427 System.out.println("Success: Value of \""+msg+"\" is \""+actual+"\""); 428 } 429 430 public void testAssignDiff() throws ParseException 431 { 432 simplifyTestString("y=x^5","y=x^5.0"); 433 simplifyTestString("z=diff(y,x)","z=5.0*x^4.0"); 434 Node n1 = ((DSymbolTable) j.getSymbolTable()).getPartialDeriv("y",new String []{"x"}).getEquation(); 435 myAssertEquals("dy/dx","5.0*x^4.0",j.toString(n1)); 436 simplifyTestString("w=diff(z,x)","w=20.0*x^3.0"); 437 Node n2 = ((DSymbolTable) j.getSymbolTable()).getPartialDeriv("y",new String []{"x","x"}).getEquation(); 438 myAssertEquals("d^2y/dxdx","20.0*x^3.0",j.toString(n2)); 439 valueTest("x=2",2); 440 valueTest("y",32); valueTest("z",80); valueTest("w",160); simplifyTestString("diff(ln(y),x)","(1.0/y)*5.0*x^4.0"); 444 } 445 446 public void testVariableReuse() throws ParseException,Exception 447 { 448 System.out.println("\nTesting variable reuse"); 449 parseProcSimpEval("x=3",new Double (3)); 450 Node node13 = parseProcSimpEval("y=x^2",new Double (9)); 451 Node node15 = parseProcSimpEval("z=diff(y,x)",new Double (6)); 452 453 j.setVarValue("x",new Double (4)); 454 System.out.println("j.setVarValue(\"x\",new Double(4));"); 455 System.out.println("j.getVarValue(y): "+j.getVarValue("y")); 456 myAssertEquals("eval y eqn",j.evaluate(node13).toString(),"16.0"); 457 System.out.println("j.getVarValue(y): "+j.getVarValue("y")); 458 myAssertEquals("eval z eqn",j.evaluate(node15).toString(),"8.0"); 459 460 j.setVarValue("x",new Double (5)); 462 System.out.println("j.setVarValue(\"x\",new Double(5));"); 463 myAssertEquals("j.findVarValue(y)",j.calcVarValue("y").toString(),"25.0"); 464 myAssertEquals("j.findVarValue(z)",j.calcVarValue("z").toString(),"10.0"); 465 466 j.getSymbolTable().clearValues(); 467 j.setVarValue("x",new Double (6)); 468 System.out.println("j.setVarValue(\"x\",new Double(5));"); 469 myAssertEquals("j.findVarValue(z)",j.calcVarValue("z").toString(),"12.0"); 470 myAssertEquals("j.findVarValue(y)",j.calcVarValue("y").toString(),"36.0"); 471 } 472 473 public void testNaN() throws ParseException, Exception 474 { 475 System.out.println("Set x to Double(Double.NaN)"); 476 j.setVarValue("x",new Double (Double.NaN)); 477 j.addVariable("y",new Double (Double.NaN)); 478 Node n = j.parse("x+5"); 479 System.out.println(j.evaluate(n)); 480 Node n2 = j.parse("y"); 481 System.out.println(j.evaluate(n2)); 482 valueTest("x == x+5",1); 483 valueTest("x == 0/0",1); 484 valueTest("x == x",1); 485 valueTest("x == 0 * x",1); 486 valueTest("x == 5",0); 487 valueTest("x == y",1); 488 valueTest("y == y",1); 489 System.out.println("Set x to Double(5)"); 490 j.setVarValue("x",new Double (5)); 491 valueTest("x == x+5",0); 492 } 493 494 public void testAssign2() 495 { 496 JEP parser = new JEP(); 497 498 parser.addVariable("AB",12); 499 parser.setAllowAssignment(true); 500 parser.parseExpression("AB=3"); System.out.println("AB=3"+parser.getValue()); 502 parser.parseExpression("AB+2"); 503 double result= parser.getValue(); assertEquals("<AB+2>",5.0,result,0.0); 505 } 506 507 public void testSqrt() 508 { 509 j.parseExpression("sqrt(-1)"); 510 double val = j.getValue(); 511 assertEquals("sqrt(-1) NaN",Double.isNaN(val),true); 512 j.parseExpression("sqrt(-1)^2"); 513 val = j.getValue(); 514 assertEquals("sqrt(-1)^2",-1.0,val,0.0); 515 } 516 517 public void testSum() 518 { 519 valueTest("Sum(x,x,1,10)",55); 520 valueTest("Sum(x^2,x,1,5)",55); 521 valueTest("Product(x,x,1,5)",120); 522 valueTest("Min(x^2,x,1,5)",1); 523 valueTest("Max(x^2,x,1,5)",25); 524 valueTest("MinArg(x^2,x,1,5)",1); 525 valueTest("MaxArg(x^2,x,1,5)",5); 526 } 527 public void testBad() throws ParseException 528 { 529 if(SHOW_BAD) 530 { 531 simplifyTest("1&&(1||x)","1"); 532 simplifyTest("diff(sgn(x),x)","0"); simplifyTest("diff(re(x+i y),x)","1"); simplifyTest("diff(re(x+i y),y)","0"); 535 simplifyTest("diff(im(x+i y),x)","0"); 536 simplifyTest("diff(im(x+i y),y)","1"); 537 simplifyTest("(x/2)*3","x*1.5"); 538 simplifyTest("diff(pow(x,y),x)","y*(pow(x,y-1))"); 539 simplifyTest("diff(pow(x,y),y)","(ln(x)) (pow(x,y))"); 540 } 541 } 542 } 543 | Popular Tags |