1 package org.lsmp.djepJUnit; 2 3 import junit.framework.*; 4 import org.nfunk.jep.*; 5 import org.lsmp.djep.vectorJep.*; 6 13 14 20 public class VectorJepTest extends TestCase { 21 VectorJep j; 22 public static final boolean SHOW_BAD=false; 23 24 public VectorJepTest(String name) { 25 super(name); 26 } 27 28 public static void main(String args[]) { 29 31 TestSuite suite= new TestSuite(MatrixJepTest.class); 32 suite.run(new TestResult()); 35 } 36 37 protected void setUp() { 38 j = new VectorJep(); 39 j.addStandardConstants(); 40 j.addStandardFunctions(); 41 j.addComplex(); 42 j.setAllowAssignment(true); 44 j.setAllowUndeclared(true); 45 j.setImplicitMul(true); 46 } 47 48 public static Test suite() { 49 return new TestSuite(VectorJepTest.class); 50 } 51 52 public void testGood() 53 { 54 assertEquals(1,1); 55 } 56 57 public void valueTest(String expr,double dub) throws Exception 58 { 59 valueTest(expr,new Double (dub)); 60 } 61 public void valueTest(String expr,Object expected) throws Exception 62 { 63 Node node = j.parse(expr); 64 Object res = j.evaluate(node); 65 assertEquals("<"+expr+">",expected,res); 66 System.out.println("Sucess value of <"+expr+"> is "+res); 67 } 68 69 public void valueTest(String expr,String expected) throws Exception 70 { 71 Node node = j.parse(expr); 72 Object res = j.evaluate(node); 73 assertEquals("<"+expr+">",expected,res.toString()); 74 System.out.println("Sucess value of <"+expr+"> is "+res.toString()); 75 } 76 77 public Object calcValue(String expr) throws Exception 78 { 79 Node node = j.parse(expr); 80 Object res = j.evaluate(node); 81 return res; 82 } 83 84 85 public void testSimpleSum() throws Exception 86 { 87 valueTest("1+2",3); 88 valueTest("2*6+3",15); 89 valueTest("2*(6+3)",18); 90 } 91 92 public void testOperators() throws Exception 93 { 94 101 valueTest("T=1",1); 102 valueTest("F=0",0); 103 calcValue("a=F"); calcValue("b=F"); calcValue("c=F"); 104 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 105 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 106 calcValue("a=F"); calcValue("b=F"); calcValue("c=T"); 107 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 108 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 109 calcValue("a=F"); calcValue("b=T"); calcValue("c=F"); 110 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 111 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 112 calcValue("a=F"); calcValue("b=T"); calcValue("c=T"); 113 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 114 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 115 116 calcValue("a=T"); calcValue("b=F"); calcValue("c=F"); 117 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 118 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 119 calcValue("a=T"); calcValue("b=F"); calcValue("c=T"); 120 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 121 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 122 calcValue("a=T"); calcValue("b=T"); calcValue("c=F"); 123 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 124 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 125 calcValue("a=T"); calcValue("b=T"); calcValue("c=T"); 126 valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1); 127 valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1); 128 } 129 130 public void testIf() throws Exception 131 { 132 valueTest("if(1,2,3)",2); 133 valueTest("if(-1,2,3)",3); 134 valueTest("if(0,2,3)",3); 135 valueTest("if(1,2,3,4)",2); 136 valueTest("if(-1,2,3,4)",3); 137 valueTest("if(0,2,3,4)",4); 138 valueTest("if(0>=0,2,3,4)",2); 139 valueTest("x=3",3); 140 valueTest("if(x==3,1,-1)",1); 141 valueTest("if(x!=3,1,-1)",-1); 142 valueTest("if(x>=3,1,-1)",1); 143 valueTest("if(x>3,1,-1)",-1); 144 valueTest("if(x<=3,1,-1)",1); 145 valueTest("if(x<3,1,-1)",-1); 146 } 147 148 public void testAssign() throws Exception 149 { 150 valueTest("x=3",3); 151 valueTest("y=3+4",7); 152 valueTest("z=x+y",10); 153 valueTest("a=b=c=z",10); 154 valueTest("b",10); 155 valueTest("d=f=a-b",0); 156 } 157 158 159 160 public void myAssertEquals(String msg,String actual,String expected) 161 { 162 if(!actual.equals(expected)) 163 System.out.println("Error \""+msg+"\" is \""+actual+" should be "+expected+"\""); 164 assertEquals("<"+msg+">",expected,actual); 165 System.out.println("Success: Value of \""+msg+"\" is \""+actual+"\""); 166 } 167 168 public void testMatrix() throws Exception 169 { 170 j.getSymbolTable().clearValues(); 171 valueTest("x=2",2); 172 valueTest("(x*x)*x*(x*x)",32.0); 173 valueTest("y=[x^3,x^2,x]","[8.0,4.0,2.0]"); 174 valueTest("z=[3*x^2,2*x,1]","[12.0,4.0,1.0]"); 175 valueTest("w=y^^z","[-4.0,16.0,-16.0]"); 176 valueTest("w.y","0.0"); 177 valueTest("w.z","0.0"); 178 valueTest("sqrt(w . z)","0.0"); valueTest("sqrt([3,4] . [3,4])","5.0"); valueTest("y+z","[20.0,8.0,3.0]"); 181 valueTest("y-z","[-4.0,0.0,1.0]"); 182 valueTest("3*y","[24.0,12.0,6.0]"); 183 valueTest("y*4","[32.0,16.0,8.0]"); 184 valueTest("y*z","[[96.0,32.0,8.0],[48.0,16.0,4.0],[24.0,8.0,2.0]]"); 185 valueTest("z*y","[[96.0,48.0,24.0],[32.0,16.0,8.0],[8.0,4.0,2.0]]"); 186 j.getSymbolTable().clearValues(); 187 j.evaluate(j.parse("y=[cos(x),sin(x)]")); 188 j.evaluate(j.parse("z=[-sin(x),cos(x)]")); 189 valueTest("y . y","1.0"); 190 valueTest("y . z","0.0"); 191 valueTest("z . z","1.0"); 192 j.getSymbolTable().clearValues(); 193 valueTest("x=[[1,2],[3,4]]","[[1.0,2.0],[3.0,4.0]]"); 194 valueTest("y=[1,-1]","[1.0,-1.0]"); 195 valueTest("x*y","[-1.0,-1.0]"); 196 valueTest("y*x","[-2.0,-2.0]"); 197 valueTest("x+[y,y]","[[2.0,1.0],[4.0,3.0]]"); 198 valueTest("ele(y,1)","1.0"); valueTest("ele(y,2)","-1.0"); valueTest("ele(x,[1,1])","1.0"); valueTest("ele(x,[1,2])","2.0"); valueTest("ele(x,[2,1])","3.0"); valueTest("ele(x,[2,2])","4.0"); } 205 public void testBad() throws Exception 206 { 207 if(SHOW_BAD) 208 { 209 } 210 } 211 } 212 | Popular Tags |