KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lsmp > djepJUnit > VectorJepTest


1 package org.lsmp.djepJUnit;
2
3 import junit.framework.*;
4 import org.nfunk.jep.*;
5 import org.lsmp.djep.vectorJep.*;
6 /* @author rich
7  * Created on 19-Nov-2003
8  *
9  * This code is covered by a Creative Commons
10  * Attribution, Non Commercial, Share Alike license
11  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
12  */

13
14 /**
15  * JUnit test for VectorJep
16  *
17  * @author Rich Morris
18  * Created on 19-Nov-2003
19  */

20 public class VectorJepTest extends TestCase {
21     VectorJep j;
22     public static final boolean SHOW_BAD=false;
23     
24     public VectorJepTest(String JavaDoc name) {
25         super(name);
26     }
27
28     public static void main(String JavaDoc args[]) {
29         // Create an instance of this class and analyse the file
30

31         TestSuite suite= new TestSuite(MatrixJepTest.class);
32 // DJepTest jt = new DJepTest("DJepTest");
33
// jt.setUp();
34
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.setTraverse(true);
43
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 JavaDoc expr,double dub) throws Exception JavaDoc
58     {
59         valueTest(expr,new Double JavaDoc(dub));
60     }
61     public void valueTest(String JavaDoc expr,Object JavaDoc expected) throws Exception JavaDoc
62     {
63         Node node = j.parse(expr);
64         Object JavaDoc 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 JavaDoc expr,String JavaDoc expected) throws Exception JavaDoc
70     {
71         Node node = j.parse(expr);
72         Object JavaDoc 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 JavaDoc calcValue(String JavaDoc expr) throws Exception JavaDoc
78     {
79         Node node = j.parse(expr);
80         Object JavaDoc res = j.evaluate(node);
81         return res;
82     }
83     
84
85     public void testSimpleSum() throws Exception JavaDoc
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 JavaDoc
93     {
94 // if(!Operator.OP_MULTIPLY.isDistributiveOver(Operator.OP_ADD))
95
// fail("* should be distrib over +");
96
// if(Operator.OP_MULTIPLY.isDistributiveOver(Operator.OP_DIVIDE))
97
// fail("* should not be distrib over /");
98
// if(Operator.OP_MULTIPLY.getPrecedence() > Operator.OP_ADD.getPrecedence())
99
// fail("* should have a lower precedence than +");
100

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 JavaDoc
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 JavaDoc
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 JavaDoc msg,String JavaDoc actual,String JavaDoc 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 JavaDoc
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"); // tests result is unwrapped from scaler
179
valueTest("sqrt([3,4] . [3,4])","5.0"); // tests result is unwrapped from scaler
180
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"); // Value: 2.0
199
valueTest("ele(y,2)","-1.0"); // Value: 2.0
200
valueTest("ele(x,[1,1])","1.0"); // Value: 2.0
201
valueTest("ele(x,[1,2])","2.0"); // Value: 2.0
202
valueTest("ele(x,[2,1])","3.0"); // Value: 2.0
203
valueTest("ele(x,[2,2])","4.0"); // Value: 2.0
204
}
205     public void testBad() throws Exception JavaDoc
206     {
207         if(SHOW_BAD)
208         {
209         }
210     }
211 }
212
Popular Tags