KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 /**
20  * Tests some strange properties of special groups.
21  *
22  * @author Rich Morris
23  * Created on 19-Nov-2003
24  */

25 public class GroupJepTest extends TestCase {
26     GroupJep j;
27     public static final boolean SHOW_BAD=false;
28     
29     public GroupJepTest(String JavaDoc name) {
30         super(name);
31     }
32
33     public static void main(String JavaDoc args[]) {
34         // Create an instance of this class and analyse the file
35

36         TestSuite suite= new TestSuite(GroupJepTest.class);
37 // DJepTest jt = new DJepTest("DJepTest");
38
// jt.setUp();
39
suite.run(new TestResult());
40     }
41
42     protected void setUp() {
43     }
44
45     public static Test suite() {
46         return new TestSuite(GroupJepTest.class);
47     }
48
49     public void myAssertEquals(String JavaDoc msg,String JavaDoc actual,String JavaDoc expected)
50     {
51         if(!actual.equals(expected))
52             System.out.println("Error \""+msg+"\" is \""+actual+" should be "+expected+"\"");
53         assertEquals("<"+msg+">",expected,actual);
54         System.out.println("Success: Value of \""+msg+"\" is \""+actual+"\"");
55     }
56
57     public void myAssertEquals(String JavaDoc msg,String JavaDoc actual,String JavaDoc expected,String JavaDoc ending)
58     {
59         if(!actual.equals(expected))
60             System.out.println("Error \""+msg+"\" is \""+actual+" should be "+expected+"\"");
61         assertEquals("<"+msg+">",expected,actual);
62         System.out.println("Success: Value of \""+msg+"\" is \""+actual+"\": "+ending);
63     }
64
65     /** just test JUnit working OK */
66     public void testGood()
67     {
68         assertEquals(1,1);
69     }
70
71     public void valueToStringTest(String JavaDoc expr,String JavaDoc expected) throws Exception JavaDoc
72     {
73         Node node = j.parse(expr);
74         Object JavaDoc val = j.evaluate(node);
75         String JavaDoc res = val.toString();
76         String JavaDoc ending="";
77         if(val instanceof HasComplexValueI)
78         {
79             Complex cval = ((HasComplexValueI) val).getComplexValue();
80             ending = cval.toString();
81         }
82         myAssertEquals(expr,res,expected,ending);
83     }
84
85     /** Tests very large numbers, 20! */
86     public void testZ() throws Exception JavaDoc
87     {
88         j = new GroupJep(new Integers());
89         valueToStringTest("1*2*3*4*5*6*7*8*9*10*11*12*13*14*15*16*17*18*19*20","2432902008176640000");
90     }
91
92     /** Tests rationals */
93     public void testQ() throws Exception JavaDoc
94     {
95         j = new GroupJep(new Rationals());
96         System.out.println(j.getGroup().toString());
97         valueToStringTest("(1/2)-(1/3)","1/6");
98     }
99     
100     /** Tests rationals */
101     public void testQuartonians() throws Exception JavaDoc
102     {
103         j = new GroupJep(new Quartonians());
104         j.addStandardConstants();
105         System.out.println(j.getGroup().toString());
106         valueToStringTest("i*j","0.0+0.0 i +0.0 j +-1.0 k");
107     }
108
109     /** Tests integers mod 5 */
110     public void testZn() throws Exception JavaDoc
111     {
112         j = new GroupJep(new Zn(BigInteger.valueOf(5)));
113         System.out.println(j.getGroup().toString());
114         valueToStringTest("2*3","1");
115         valueToStringTest("2*4","3");
116         valueToStringTest("3*3","4");
117         valueToStringTest("3*4","2");
118         valueToStringTest("4*4","1");
119
120         valueToStringTest("2/3","4");
121         valueToStringTest("2/4","3");
122         valueToStringTest("3/2","4");
123         valueToStringTest("3/4","2");
124         valueToStringTest("4/2","2");
125         valueToStringTest("4/3","3");
126     }
127
128     public void testZroot2() throws Exception JavaDoc
129     {
130         RingI ring = new Integers();
131         Number JavaDoc coeffs[] = new Number JavaDoc[]{
132             BigInteger.valueOf(-2),
133             BigInteger.ZERO,
134             BigInteger.ONE};
135         Polynomial p1 = new Polynomial(ring,"rt2",coeffs);
136
137         j = new GroupJep(new AlgebraicExtension(ring, p1));
138         j.addStandardConstants();
139         System.out.println(j.getGroup().toString());
140         valueToStringTest("rt2*rt2","<2>");
141         valueToStringTest("(rt2+1)*(rt2+1)","<2 rt2+3>");
142     }
143     
144     public void testZ5thRootUnity() throws Exception JavaDoc
145     {
146         RingI ring = new Integers();
147         Number JavaDoc coeffs[] = new Number JavaDoc[]{
148             BigInteger.valueOf(-1),
149             BigInteger.ZERO,
150             BigInteger.ZERO,
151             BigInteger.ZERO,
152             BigInteger.ZERO,
153             BigInteger.ONE};
154         Polynomial p1 = new Polynomial(ring,"t",coeffs);
155
156         j = new GroupJep(new AlgebraicExtension(ring, p1));
157         j.addStandardConstants();
158         System.out.println(j.getGroup().toString());
159         valueToStringTest("t*t","<t^2>");
160         valueToStringTest("t*t*t","<t^3>");
161         valueToStringTest("t*t*t*t","<t^4>");
162         valueToStringTest("t*t*t*t*t","<1>");
163     }
164
165     public void testZRoot2Root5() throws Exception JavaDoc
166     {
167         RingI ring = new Integers();
168         Number JavaDoc coeffs1[] = new Number JavaDoc[]{
169             BigInteger.valueOf(-2),
170             BigInteger.ZERO,
171             BigInteger.ONE};
172         Polynomial p1 = new Polynomial(ring,"t",coeffs1);
173         AlgebraicExtension an1 = new AlgebraicExtension(ring, p1);
174         
175         Number JavaDoc coeffs2[] = new Number JavaDoc[]{
176             an1.valueOf("-5"),
177             an1.valueOf("0"),
178             an1.valueOf("1")};
179         Polynomial p2 = new Polynomial(an1,"s",coeffs2);
180         AlgebraicExtension an2 = new AlgebraicExtension(an1, p2);
181
182         j = new GroupJep(an2);
183         j.addStandardConstants();
184         System.out.println(j.getGroup().toString());
185
186         valueToStringTest("t","<t>");
187         valueToStringTest("s","<s>");
188         valueToStringTest("t*t","<2>");
189         valueToStringTest("s*s","<5>");
190         valueToStringTest("s*t","<t s>");
191         valueToStringTest("s+t","<s+t>");
192         valueToStringTest("(t-1)*(s-1)","<<t-1> s+-t+1>");
193     }
194
195     public void testZtau() throws Exception JavaDoc
196     {
197         RingI ring = new Integers();
198         Number JavaDoc coeffs[] = new Number JavaDoc[]{
199             BigInteger.valueOf(-1),
200             BigInteger.valueOf(-1),
201             BigInteger.ONE};
202         Polynomial p1 = new Polynomial(ring,"t",coeffs);
203
204         AlgebraicExtension an = new AlgebraicExtension(ring, p1);
205         j = new GroupJep(an);
206         j.addStandardConstants();
207         j.setAllowAssignment(true);
208         j.setAllowUndeclared(true);
209         System.out.println(j.getGroup().toString());
210         valueToStringTest("t*t","<t+1>");
211         valueToStringTest("t*t*t","<2 t+1>");
212         valueToStringTest("t*t*t*t","<3 t+2>");
213         valueToStringTest("t*t*t*t*t","<5 t+3>");
214         valueToStringTest("t*t*t*t*t*t","<8 t+5>");
215         valueToStringTest("x=2*t-1","<2 t-1>");
216         valueToStringTest("x*x","<5>");
217         valueToStringTest("-t","<-t>");
218         valueToStringTest("1-t","<-t+1>");
219         valueToStringTest("t*(1-t)","<-1>");
220     }
221
222     public void testBad() throws ParseException
223     {
224         if(SHOW_BAD)
225         {
226 // simplifyTest("1&&(1||x)","1");
227
// simplifyTest("diff(sgn(x),x)","0"); // sgn not implemented
228
// simplifyTest("diff(re(x+i y),x)","1"); // not smart enought to work out re(i) = 1
229
// simplifyTest("diff(re(x+i y),y)","0");
230
// simplifyTest("diff(im(x+i y),x)","0");
231
// simplifyTest("diff(im(x+i y),y)","1");
232
// simplifyTest("(x/2)*3","x*1.5");
233
}
234     }
235 }
236
Popular Tags