KickJava   Java API By Example, From Geeks To Geeks.

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


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.xjep.*;
7 import org.lsmp.djep.xjep.rewriteRules.*;
8
9 /* @author rich
10  * Created on 19-Nov-2003
11  *
12  * This code is covered by a Creative Commons
13  * Attribution, Non Commercial, Share Alike license
14  * <a HREF="http://creativecommons.org/licenses/by-nc-sa/1.0">License</a>
15  */

16
17 /**
18  * @author Rich Morris
19  * Created on 19-Nov-2003
20  */

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

32         TestSuite suite= new TestSuite(DJepTest.class);
33 // DJepTest jt = new DJepTest("DJepTest");
34
// jt.setUp();
35
suite.run(new TestResult());
36     }
37
38     protected void setUp() {
39         j = new XJep();
40         j.addStandardConstants();
41         j.addStandardFunctions();
42         j.addComplex();
43         //j.setTraverse(true);
44
j.setAllowAssignment(true);
45         j.setAllowUndeclared(true);
46         j.setImplicitMul(true);
47     }
48
49     public static Test suite() {
50         return new TestSuite(XJepTest.class);
51     }
52
53     public void myAssertEquals(String JavaDoc msg,String JavaDoc actual,String JavaDoc expected)
54     {
55         if(!actual.equals(expected))
56             System.out.println("Error \""+msg+"\" is \""+actual+" should be "+expected+"\"");
57         assertEquals("<"+msg+">",expected,actual);
58         System.out.println("Success: Value of \""+msg+"\" is \""+actual+"\"");
59     }
60     /** just test JUnit working OK */
61     public void testGood()
62     {
63         assertEquals(1,1);
64     }
65
66     public void valueTest(String JavaDoc expr,double dub) throws Exception JavaDoc
67     {
68         valueTest(expr,new Double JavaDoc(dub));
69     }
70     public void valueTest(String JavaDoc expr,Object JavaDoc expected) throws Exception JavaDoc
71     {
72         Node node = j.parse(expr);
73         Object JavaDoc res = j.evaluate(node);
74         assertEquals("<"+expr+">",expected,res);
75         System.out.println("Sucess value of <"+expr+"> is "+res);
76     }
77     public void complexValueTest(String JavaDoc expr,Complex expected,double tol) throws Exception JavaDoc
78     {
79         Node node = j.parse(expr);
80         Object JavaDoc res = j.evaluate(node);
81         assertTrue("<"+expr+"> expected: <"+expected+"> but was <"+res+">",
82             expected.equals((Complex) res,tol));
83         System.out.println("Sucess value of <"+expr+"> is "+res);
84     }
85
86     public Object JavaDoc calcValue(String JavaDoc expr)
87     {
88         j.parseExpression(expr);
89         if(j.hasError())
90             fail("Parse Failure: "+expr);
91         return j.getValueAsObject();
92     }
93     
94     public void simplifyTest(String JavaDoc expr,String JavaDoc expected) throws ParseException
95     {
96         Node node = j.parse(expr);
97         Node processed = j.preprocess(node);
98         Node simp = j.simplify(processed);
99         String JavaDoc res = j.toString(simp);
100         
101         Node node2 = j.parse(expected);
102         Node processed2 = j.preprocess(node2);
103         Node simp2 = j.simplify(processed2);
104         String JavaDoc res2 = j.toString(simp2);
105
106         if(!res2.equals(res))
107             System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+res2+"\"");
108         assertEquals("<"+expr+">",res2,res);
109         System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\"");
110             
111 // System.out.print("Full Brackets:\t");
112
// j.pv.setFullBrackets(true);
113
// j.pv.println(simp);
114
// j.pv.setFullBrackets(false);
115

116     }
117
118     public void simplifyTestString(String JavaDoc expr,String JavaDoc expected) throws ParseException
119     {
120         Node node = j.parse(expr);
121         Node processed = j.preprocess(node);
122         Node simp = j.simplify(processed);
123         String JavaDoc res = j.toString(simp);
124         
125         if(!expected.equals(res))
126             System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+expected+"\"");
127         assertEquals("<"+expr+">",expected,res);
128         System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\"");
129             
130 // System.out.print("Full Brackets:\t");
131
// j.pv.setFullBrackets(true);
132
// j.pv.println(simp);
133
// j.pv.setFullBrackets(false);
134

135     }
136     
137     public Node parseProcSimpEval(String JavaDoc expr,Object JavaDoc expected) throws ParseException,Exception JavaDoc
138     {
139         Node node = j.parse(expr);
140         Node processed = j.preprocess(node);
141         Node simp = j.simplify(processed);
142         Object JavaDoc res = j.evaluate(simp);
143         
144         if(!expected.equals(res))
145             System.out.println("Error: Value of \""+expr+"\" is \""+res+"\" should be \""+expected+"\"");
146         assertEquals("<"+expr+">",expected,res);
147         System.out.println("Sucess: Value of \""+expr+"\" is \""+res+"\"");
148         return simp;
149     }
150
151
152     public void testSimpleSum() throws Exception JavaDoc
153     {
154         valueTest("1+2",3);
155         valueTest("2*6+3",15);
156         valueTest("2*(6+3)",18);
157     }
158     
159     public void testOperators() throws Exception JavaDoc
160     {
161         OperatorSet opSet = j.getOperatorSet();
162         if(!((XOperator) opSet.getMultiply()).isDistributiveOver(opSet.getAdd()))
163             fail("* should be distrib over +");
164         if(((XOperator) opSet.getMultiply()).isDistributiveOver(opSet.getDivide()))
165             fail("* should not be distrib over /");
166         if(((XOperator) opSet.getMultiply()).getPrecedence() > ((XOperator) opSet.getAdd()).getPrecedence())
167             fail("* should have a lower precedence than +");
168
169         valueTest("T=1",1);
170         valueTest("F=0",0);
171         calcValue("a=F"); 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=F"); 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=F"); 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=F"); 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         calcValue("a=T"); calcValue("b=F"); calcValue("c=F");
185         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
186         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
187         calcValue("a=T"); calcValue("b=F"); calcValue("c=T");
188         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
189         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
190         calcValue("a=T"); calcValue("b=T"); calcValue("c=F");
191         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
192         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
193         calcValue("a=T"); calcValue("b=T"); calcValue("c=T");
194         valueTest("(a&&(b||c)) == ((a&&b)||(a&&c))",1);
195         valueTest("(a||(b&&c)) == ((a||b)&&(a||c))",1);
196     }
197     
198     public void testPrint() throws ParseException
199     {
200         simplifyTestString("(a+b)+c","a+b+c");
201         simplifyTestString("(a-b)+c","a-b+c");
202         simplifyTestString("(a+b)-c","a+b-c");
203         simplifyTestString("(a-b)-c","a-b-c");
204
205         simplifyTestString("a+(b+c)","a+b+c");
206         simplifyTestString("a-(b+c)","a-(b+c)");
207         simplifyTestString("a+(b-c)","a+b-c");
208         simplifyTestString("a-(b-c)","a-(b-c)");
209
210         simplifyTestString("(a*b)*c","a*b*c");
211         simplifyTestString("(a/b)*c","(a/b)*c");
212         simplifyTestString("(a*b)/c","a*b/c");
213         simplifyTestString("(a/b)/c","(a/b)/c");
214
215         simplifyTestString("a*(b*c)","a*b*c");
216         simplifyTestString("a/(b*c)","a/(b*c)");
217         simplifyTestString("a*(b/c)","a*b/c");
218         simplifyTestString("a/(b/c)","a/(b/c)");
219
220         simplifyTestString("a=(b=c)","a=b=c");
221         //simplifyTestString("(a=b)=c","a/(b/c)");
222

223         simplifyTestString("(a*b)+c","a*b+c");
224         simplifyTestString("(a+b)*c","(a+b)*c");
225         simplifyTestString("a*(b+c)","a*(b+c)");
226         simplifyTestString("a+(b*c)","a+b*c");
227
228         simplifyTestString("(a||b)||c","a||b||c");
229         simplifyTestString("(a&&b)||c","a&&b||c");
230         simplifyTestString("(a||b)&&c","(a||b)&&c");
231         simplifyTestString("(a&&b)&&c","a&&b&&c");
232
233         simplifyTestString("a||(b||c)","a||b||c");
234         simplifyTestString("a&&(b||c)","a&&(b||c)");
235         simplifyTestString("a||(b&&c)","a||b&&c");
236         simplifyTestString("a&&(b&&c)","a&&b&&c");
237     }
238     
239     public void testSimp() throws ParseException
240     {
241         simplifyTest("2+3","5");
242         simplifyTest("2*3","6");
243         simplifyTest("2^3","8");
244         simplifyTest("3/2","1.5");
245         simplifyTest("2*3+4","10");
246         simplifyTest("2*(3+4)","14");
247
248         simplifyTest("0+x","x");
249         simplifyTest("x+0","x");
250         simplifyTest("0-x","0-x");
251         simplifyTest("x-0","x");
252         simplifyTest("0*x","0");
253         simplifyTest("x*0","0");
254         simplifyTest("1*x","x");
255         simplifyTest("x*1","x");
256         simplifyTest("-1*x","-x");
257         simplifyTest("x*-1","-x");
258         simplifyTest("-(-x)","x");
259         simplifyTest("-(-(-x))","-x");
260         simplifyTest("(-1)*(-1)*x","x");
261         simplifyTest("(-1)*(-1)*(-1)*x","-x");
262         
263         simplifyTest("0/x","0");
264         simplifyTest("x/0","1/0");
265         
266         simplifyTest("x^0","1");
267         simplifyTest("x^1","x");
268         simplifyTest("0^x","0");
269         simplifyTest("1^x","1");
270
271         // (a+b)+c
272
simplifyTest("(2+3)+x","5+x");
273         simplifyTest("(2+x)+3","5+x");
274         simplifyTest("(x+2)+3","5+x");
275         // a+(b+c)
276
simplifyTest("x+(2+3)","5+x");
277         simplifyTest("2+(x+3)","5+x");
278         simplifyTest("2+(3+x)","5+x");
279         // (a+b)-c
280
simplifyTest("(2+3)-x","5-x");
281         simplifyTest("(2+x)-3","x-1");
282         simplifyTest("(x+2)-3","x-1");
283         // (a-b)+c
284
simplifyTest("(2-3)+x","-1+x");
285         simplifyTest("(2-x)+3","5-x");
286         simplifyTest("(x-2)+3","1+x");
287         // a-(b+c)
288
simplifyTest("x-(2+3)","x-5");
289         simplifyTest("2-(x+3)","-1-x");
290         simplifyTest("2-(3+x)","-1-x");
291         // a+(b-c)
292
simplifyTest("x+(2-3)","x-1");
293         simplifyTest("2+(x-3)","-1+x");
294         simplifyTest("2+(3-x)","5-x");
295         // a-(b-c)
296
simplifyTest("x-(2-3)","1+x");
297         simplifyTest("2-(x-3)","5-x");
298         simplifyTest("2-(3-x)","-1+x");
299         // (a-b)-c
300
simplifyTest("(2-3)-x","-1-x");
301         simplifyTest("(2-x)-3","-1-x");
302         simplifyTest("(x-2)-3","x-5");
303
304         // (a*b)*c
305
simplifyTest("(2*3)*x","6*x");
306         simplifyTest("(2*x)*3","6*x");
307         simplifyTest("(x*2)*3","6*x");
308         // a+(b+c)
309
simplifyTest("x*(2*3)","6*x");
310         simplifyTest("2*(x*3)","6*x");
311         simplifyTest("2*(3*x)","6*x");
312         // (a+b)-c
313
simplifyTest("(2*3)/x","6/x");
314         simplifyTest("(3*x)/2","1.5*x");
315         simplifyTest("(x*3)/2","1.5*x");
316         // (a-b)+c
317
simplifyTest("(3/2)*x","1.5*x");
318         simplifyTest("(3/x)*2","6/x");
319         simplifyTest("(x/2)*3","1.5*x");
320         // a-(b+c)
321
simplifyTest("x/(2*3)","x/6");
322         simplifyTest("3/(x*2)","1.5/x");
323         simplifyTest("3/(2*x)","1.5/x");
324         // a+(b-c)
325
simplifyTest("x*(3/2)","1.5*x");
326         simplifyTest("3*(x/2)","1.5*x");
327         simplifyTest("3*(2/x)","6/x");
328         // a-(b-c)
329
simplifyTest("x/(3/2)","x/1.5");
330         simplifyTest("2/(x/3)","6/x");
331         simplifyTest("3/(2/x)","1.5*x");
332         // (a-b)-c
333
simplifyTest("(3/2)/x","1.5/x");
334         simplifyTest("(3/x)/2","1.5/x");
335         simplifyTest("(x/3)/2","x/6");
336
337
338         simplifyTest("x*(3+2)","5*x");
339         simplifyTest("3*(x+2)","6+3*x");
340         simplifyTest("3*(2+x)","6+3*x");
341         simplifyTest("(3+2)*x","5*x");
342         simplifyTest("(3+x)*2","6+2*x");
343         simplifyTest("(x+3)*2","6+x*2");
344
345         simplifyTest("x*(3-2)","x");
346         simplifyTest("3*(x-2)","-6+3*x");
347         simplifyTest("3*(2-x)","6-3*x");
348         simplifyTest("(3-2)*x","x");
349         simplifyTest("(3-x)*2","6-2*x");
350         simplifyTest("(x-3)*2","-6+2*x");
351
352         simplifyTest("3+(x/4)","3+x/4");
353         simplifyTest("2*(x/4)","0.5*x");
354         simplifyTest("(2*(3+(x/4)))","6+0.5*x");
355         simplifyTest("1+(2*(3+(x/4)))","7+0.5*x");
356         simplifyTest("((3+(x/4))*2)+1","7+0.5*x");
357         
358     }
359
360     public void testComplex() throws Exception JavaDoc
361     {
362         double tol = 0.00000001;
363
364         complexValueTest("z=complex(3,2)",new Complex(3,2),tol);
365         complexValueTest("z*z-z",new Complex(2,10),tol);
366         complexValueTest("z^3",new Complex(-9,46),tol);
367         complexValueTest("(z*z-z)/z",new Complex(2,2),tol);
368         complexValueTest("w=polar(2,pi/2)",new Complex(0,2),tol);
369         
370     }
371
372     public void testIf() throws Exception JavaDoc
373     {
374         valueTest("if(1,2,3)",2);
375         valueTest("if(-1,2,3)",3);
376         valueTest("if(0,2,3)",3);
377         valueTest("if(1,2,3,4)",2);
378         valueTest("if(-1,2,3,4)",3);
379         valueTest("if(0,2,3,4)",4);
380         valueTest("if(0>=0,2,3,4)",2);
381         valueTest("x=3",3);
382         valueTest("if(x==3,1,-1)",1);
383         valueTest("if(x!=3,1,-1)",-1);
384         valueTest("if(x>=3,1,-1)",1);
385         valueTest("if(x>3,1,-1)",-1);
386         valueTest("if(x<=3,1,-1)",1);
387         valueTest("if(x<3,1,-1)",-1);
388     }
389
390     public void testAssign() throws Exception JavaDoc
391     {
392         valueTest("x=3",3);
393         valueTest("y=3+4",7);
394         valueTest("z=x+y",10);
395         valueTest("a=b=c=z",10);
396         valueTest("b",10);
397         valueTest("d=f=a-b",0);
398     }
399
400     public void testMacroFun() throws Exception JavaDoc
401     {
402         j.addFunction("zap",new MacroFunction("zap",1,"x*(x-1)/2",j));
403         valueTest("zap(10)",45);
404     }
405     public void testVariableReuse() throws Exception JavaDoc
406     {
407         System.out.println("\nTesting variable reuse");
408         parseProcSimpEval("x=3",new Double JavaDoc(3));
409         Node node13 = parseProcSimpEval("y=x^2",new Double JavaDoc(9));
410         Node node15 = parseProcSimpEval("z=y+x",new Double JavaDoc(12));
411             
412         j.setVarValue("x",new Double JavaDoc(4));
413         System.out.println("j.setVarValue(\"x\",new Double(4));");
414         System.out.println("j.getVarValue(y): "+j.getVarValue("y"));
415         myAssertEquals("eval y eqn",j.evaluate(node13).toString(),"16.0");
416         System.out.println("j.getVarValue(y): "+j.getVarValue("y"));
417         myAssertEquals("eval z eqn",j.evaluate(node15).toString(),"20.0");
418
419 // j.getSymbolTable().clearValues();
420
j.setVarValue("x",new Double JavaDoc(5));
421         System.out.println("j.setVarValue(\"x\",new Double(5));");
422         myAssertEquals("j.findVarValue(y)",j.calcVarValue("y").toString(),"25.0");
423         myAssertEquals("j.findVarValue(z)",j.calcVarValue("z").toString(),"30.0");
424
425         j.getSymbolTable().clearValues();
426         j.setVarValue("x",new Double JavaDoc(6));
427         System.out.println("j.setVarValue(\"x\",new Double(6));");
428         myAssertEquals("j.findVarValue(z)",j.calcVarValue("z").toString(),"42.0");
429         myAssertEquals("j.findVarValue(y)",j.calcVarValue("y").toString(),"36.0");
430
431         parseProcSimpEval("x=7",new Double JavaDoc(7));
432         myAssertEquals("eval y eqn",j.evaluate(node13).toString(),"49.0");
433         myAssertEquals("eval z eqn",j.evaluate(node15).toString(),"56.0");
434     }
435     
436     public void testDotInName() throws ParseException,Exception JavaDoc
437     {
438         valueTest("x.x=3",3);
439         valueTest("x.x+1",4);
440     }
441
442     public void testReentrant() throws ParseException,Exception JavaDoc
443     {
444         j.restartParser("x=1; // semi-colon; in comment\n y=2; z=x+y;");
445         Node node = j.continueParsing();
446         myAssertEquals("x=1; ...",j.evaluate(node).toString(),"1.0");
447         node = j.continueParsing();
448         myAssertEquals("..., y=2; ...",j.evaluate(node).toString(),"2.0");
449         node = j.continueParsing();
450         myAssertEquals("..., z=x+y;",j.evaluate(node).toString(),"3.0");
451         node = j.continueParsing();
452         assertNull("empty string ",node);
453     }
454     
455     public void testRewrite() throws ParseException,Exception JavaDoc
456     {
457         RewriteVisitor rwv = new RewriteVisitor();
458         ExpandBrackets eb = new ExpandBrackets(j);
459         ExpandPower ep = new ExpandPower(j);
460
461         Node n1 = j.parse("(a+b)*(c+d)");
462         Node n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{eb},false);
463         myAssertEquals("expand((a+b)*(c+d))",j.toString(n2),"a*c+a*d+b*c+b*d");
464
465         n1 = j.parse("(a+b)*(a+b)");
466         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{eb},false);
467         myAssertEquals("expand((a+b)*(a+b))",j.toString(n2),"a*a+a*b+b*a+b*b");
468
469         n1 = j.parse("(a-b)*(a-b)");
470         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{eb},true);
471         myAssertEquals("expand((a+b)*(a+b))",j.toString(n2),"a*a-a*b-(b*a-b*b)");
472
473         n1 = j.parse("(x+7.6)*(x+5.8832)*(x-55.12)");
474         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{eb},false);
475         Node n3 = rwv.rewrite(n1,j,new RewriteRuleI[]{eb},true);
476         j.println(n2);
477         j.println(n3);
478         
479         n1 = j.parse("(a+b)^0");
480         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
481         myAssertEquals("(a+b)^0",j.toString(n2),"1.0");
482
483         n1 = j.parse("(a-b)^0");
484         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
485         myAssertEquals("(a-b)^0",j.toString(n2),"1.0");
486
487         n1 = j.parse("(a+b)^1");
488         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
489         myAssertEquals("(a+b)^1",j.toString(n2),"a+b");
490
491         n1 = j.parse("(a-b)^1");
492         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
493         myAssertEquals("(a-b)^1",j.toString(n2),"a-b");
494
495         n1 = j.parse("(a+b)^2");
496         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
497         myAssertEquals("(a+b)^2",j.toString(n2),"a^2.0+2.0*a*b+b^2.0");
498
499         n1 = j.parse("(a-b)^2");
500         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
501         myAssertEquals("(a-b)^2",j.toString(n2),"a^2.0-(2.0*a*b-b^2.0)");
502
503         n1 = j.parse("(a+b)^3");
504         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
505         myAssertEquals("(a+b)^3",j.toString(n2),"a^3.0+3.0*a^2.0*b+3.0*a*b^2.0+b^3.0");
506
507         n1 = j.parse("(a-b)^3");
508         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
509         myAssertEquals("(a-b)^3",j.toString(n2),"a^3.0-(3.0*a^2.0*b-(3.0*a*b^2.0-b^3.0))");
510
511         n1 = j.parse("(a+b)^4");
512         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
513         myAssertEquals("(a+b)^4",j.toString(n2),"a^4.0+4.0*a^3.0*b+6.0*a^2.0*b^2.0+4.0*a*b^3.0+b^4.0");
514
515         n1 = j.parse("(a-b)^4");
516         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
517         myAssertEquals("(a-b)^4",j.toString(n2),"a^4.0-(4.0*a^3.0*b-(6.0*a^2.0*b^2.0-(4.0*a*b^3.0-b^4.0)))");
518         
519         n1 = j.parse("(a+b)^5");
520         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
521         myAssertEquals("(a+b)^5",j.toString(n2),"a^5.0+5.0*a^4.0*b+10.0*a^3.0*b^2.0+10.0*a^2.0*b^3.0+5.0*a*b^4.0+b^5.0");
522
523         n1 = j.parse("(a-b)^5");
524         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},false);
525         myAssertEquals("(a-b)^5",j.toString(n2),"a^5.0-(5.0*a^4.0*b-(10.0*a^3.0*b^2.0-(10.0*a^2.0*b^3.0-(5.0*a*b^4.0-b^5.0))))");
526
527         n1 = j.parse("(a+1)^5");
528         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},true);
529         myAssertEquals("(a+1)^5",j.toString(n2),"a^5.0+5.0*a^4.0+10.0*a^3.0+10.0*a^2.0+1.0+5.0*a");
530
531         n1 = j.parse("(a-1)^5");
532         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},true);
533         myAssertEquals("(a-1)^5",j.toString(n2),"a^5.0-(5.0*a^4.0-(10.0*a^3.0-(10.0*a^2.0-(5.0*a-1.0))))");
534
535         n1 = j.parse("(a+2)^5");
536         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep},true);
537         myAssertEquals("(a+1)^5",j.toString(n2),"a^5.0+10.0*a^4.0+40.0*a^3.0+80.0*a^2.0+32.0+80.0*a");
538
539         j.getPrintVisitor().setMaxLen(80);
540         n1=j.parse("(xx^2+yy^2+zz^2+ww^2)^8");
541         n2 = rwv.rewrite(n1,j,new RewriteRuleI[]{ep,eb},true);
542
543         j.getPrintVisitor().setMaxLen(80);
544         j.println(n2);
545     }
546
547     public void testBad() throws ParseException
548     {
549         if(SHOW_BAD)
550         {
551             simplifyTest("1&&(1||x)","1");
552             simplifyTest("diff(sgn(x),x)","0"); // sgn not implemented
553
simplifyTest("diff(re(x+i y),x)","1"); // not smart enought to work out re(i) = 1
554
simplifyTest("diff(re(x+i y),y)","0");
555             simplifyTest("diff(im(x+i y),x)","0");
556             simplifyTest("diff(im(x+i y),y)","1");
557             simplifyTest("(x/2)*3","x*1.5");
558         }
559     }
560 }
561
Popular Tags