001
002 /*
003 * $Id: LocalTest.java 3367 2010-10-24 13:05:02Z kredel $
004 */
005
006 package edu.jas.poly;
007
008
009 import junit.framework.Test;
010 import junit.framework.TestCase;
011 import junit.framework.TestSuite;
012
013 import org.apache.log4j.BasicConfigurator;
014 //import org.apache.log4j.Logger;
015
016 import edu.jas.arith.BigRational;
017 import edu.jas.arith.BigInteger;
018
019
020
021 /**
022 * Local tests with JUnit.
023 * @author Heinz Kredel.
024 */
025
026 public class LocalTest extends TestCase {
027
028 /**
029 * main.
030 */
031 public static void main (String[] args) {
032 BasicConfigurator.configure();
033 junit.textui.TestRunner.run( suite() );
034 }
035
036 /**
037 * Constructs a <CODE>LocalTest</CODE> object.
038 * @param name String.
039 */
040 public LocalTest(String name) {
041 super(name);
042 }
043
044 /**
045 * suite.
046 */
047 public static Test suite() {
048 TestSuite suite= new TestSuite(LocalTest.class);
049 return suite;
050 }
051
052 LocalRing<BigInteger> fac;
053 GenPolynomialRing<BigRational> pfac;
054 LocalRing< GenPolynomial<BigRational> > mfac;
055
056 Local< BigInteger > a;
057 Local< BigInteger > b;
058 Local< BigInteger > c;
059 Local< BigInteger > d;
060 Local< BigInteger > e;
061
062 Local< GenPolynomial<BigRational> > ap;
063 Local< GenPolynomial<BigRational> > bp;
064 Local< GenPolynomial<BigRational> > cp;
065 Local< GenPolynomial<BigRational> > dp;
066 Local< GenPolynomial<BigRational> > ep;
067
068 int rl = 1;
069 int kl = 13;
070 int ll = 5;
071 int el = 2;
072 float q = 0.3f;
073 int il = 2;
074 long p = 1152921504606846883L; // 2^60-93;
075
076 protected void setUp() {
077 a = b = c = d = e = null;
078 ap = bp = cp = dp = ep = null;
079 BigInteger cfac = new BigInteger(1);
080 fac = new LocalRing<BigInteger>( cfac, new BigInteger( p ) );
081
082 pfac = new GenPolynomialRing<BigRational>( new BigRational(1), 1 );
083 GenPolynomial<BigRational> mo = pfac.random(kl,ll,el,q);
084 while ( mo.isConstant() ) {
085 mo = pfac.random(kl,ll,el,q);
086 }
087 mfac = new LocalRing<GenPolynomial<BigRational>>( pfac, mo );
088 }
089
090 protected void tearDown() {
091 a = b = c = d = e = null;
092 ap = bp = cp = dp = ep = null;
093 fac = null;
094 pfac = null;
095 mfac = null;
096 }
097
098
099 /**
100 * Test constructor for integer.
101 *
102 */
103 public void testIntConstruction() {
104 c = fac.getONE();
105 //System.out.println("c = " + c);
106 assertTrue("isZERO( c )", !c.isZERO() );
107 assertTrue("isONE( c )", c.isONE() );
108
109 d = fac.getZERO();
110 //System.out.println("d = " + d);
111 assertTrue("isZERO( d )", d.isZERO() );
112 assertTrue("isONE( d )", !d.isONE() );
113 }
114
115
116 /**
117 * Test constructor for polynomial.
118 *
119 */
120 public void testPolyConstruction() {
121 cp = mfac.getONE();
122 assertTrue("isZERO( cp )", !cp.isZERO() );
123 assertTrue("isONE( cp )", cp.isONE() );
124
125 dp = mfac.getZERO();
126 assertTrue("isZERO( dp )", dp.isZERO() );
127 assertTrue("isONE( dp )", !dp.isONE() );
128 }
129
130
131 /**
132 * Test random integer.
133 *
134 */
135 public void testRandom() {
136 for (int i = 0; i < 4; i++) {
137 a = fac.random(kl*(i+1));
138 //a = fac.random(kl*(i+1), ll+i, el, q );
139 //System.out.println("a = " + a);
140 assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
141 assertTrue(" not isONE( a"+i+" )", !a.isONE() );
142 }
143 }
144
145
146 /**
147 * Test random polynomial.
148 *
149 */
150 public void testPolyRandom() {
151 for (int i = 0; i < 7; i++) {
152 ap = mfac.random(kl+i);
153 assertTrue(" not isZERO( ap"+i+" )", !ap.isZERO() );
154 assertTrue(" not isONE( ap"+i+" )", !ap.isONE() );
155 }
156 }
157
158
159 /**
160 * Test integer addition.
161 */
162 public void testIntAddition() {
163
164 a = fac.random(kl);
165 b = fac.random(kl);
166 //System.out.println("a = " + a);
167 //System.out.println("b = " + b);
168
169 c = a.sum(b);
170 d = c.subtract(b);
171 assertEquals("a+b-b = a",a,d);
172
173 c = a.sum(b);
174 d = b.sum(a);
175 assertEquals("a+b = b+a",c,d);
176
177 c = fac.random(kl);
178 d = c.sum( a.sum(b) );
179 e = c.sum( a ).sum(b);
180 assertEquals("c+(a+b) = (c+a)+b",d,e);
181
182 c = a.sum( fac.getZERO() );
183 d = a.subtract( fac.getZERO() );
184 assertEquals("a+0 = a-0",c,d);
185
186 c = fac.getZERO().sum( a );
187 d = fac.getZERO().subtract( a.negate() );
188 assertEquals("0+a = 0+(-a)",c,d);
189 }
190
191
192 /**
193 * Test polynomial addition.
194 *
195 */
196 public void testPolyAddition() {
197
198 ap = mfac.random(kl);
199 bp = mfac.random(kl);
200 //System.out.println("a = " + a);
201 //System.out.println("b = " + b);
202
203 cp = ap.sum(bp);
204 dp = cp.subtract(bp);
205 assertEquals("a+b-b = a",ap,dp);
206
207 cp = ap.sum(bp);
208 dp = bp.sum(ap);
209 //System.out.println("c = " + c);
210 //System.out.println("d = " + d);
211
212 assertEquals("a+b = b+a",cp,dp);
213
214 cp = mfac.random(kl);
215 dp = cp.sum( ap.sum(bp) );
216 ep = cp.sum( ap ).sum(bp);
217 assertEquals("c+(a+b) = (c+a)+b",dp,ep);
218
219
220 cp = ap.sum( mfac.getZERO() );
221 dp = ap.subtract( mfac.getZERO() );
222 assertEquals("a+0 = a-0",cp,dp);
223
224 cp = mfac.getZERO().sum( ap );
225 dp = mfac.getZERO().subtract( ap.negate() );
226 assertEquals("0+a = 0+(-a)",cp,dp);
227 }
228
229
230 /**
231 * Test integer multiplication.
232 */
233 public void testIntMultiplication() {
234
235 a = fac.random(kl);
236 b = fac.random(kl);
237 if ( a.isZERO() || b.isZERO() ) {
238 return;
239 }
240 assertTrue("not isZERO( a )", !a.isZERO() );
241 assertTrue("not isZERO( b )", !b.isZERO() );
242
243 c = b.multiply(a);
244 d = a.multiply(b);
245 assertTrue("not isZERO( c )", !c.isZERO() );
246 assertTrue("not isZERO( d )", !d.isZERO() );
247
248 //System.out.println("a = " + a);
249 //System.out.println("b = " + b);
250 e = d.subtract(c);
251 assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
252
253 assertTrue("a*b = b*a", c.equals(d) );
254 assertEquals("a*b = b*a",c,d);
255
256 c = fac.random(kl);
257 d = a.multiply( b.multiply(c) );
258 e = (a.multiply(b)).multiply(c);
259
260 assertEquals("a(bc) = (ab)c",d,e);
261 assertTrue("a(bc) = (ab)c", d.equals(e) );
262
263 c = a.multiply( fac.getONE() );
264 d = fac.getONE().multiply( a );
265 assertEquals("a*1 = 1*a",c,d);
266
267 if ( a.isUnit() ) {
268 c = a.inverse();
269 d = c.multiply(a);
270 //System.out.println("a = " + a);
271 //System.out.println("c = " + c);
272 //System.out.println("d = " + d);
273 assertTrue("a*1/a = 1",d.isONE());
274 }
275 }
276
277
278 /**
279 * Test polynomial multiplication.
280 *
281 */
282 public void testPolyMultiplication() {
283
284 ap = mfac.random(kl);
285 bp = mfac.random(kl);
286 if ( ap.isZERO() || bp.isZERO() ) {
287 return;
288 }
289
290 assertTrue("not isZERO( a )", !ap.isZERO() );
291 assertTrue("not isZERO( b )", !bp.isZERO() );
292
293 cp = bp.multiply(ap);
294 dp = ap.multiply(bp);
295 assertTrue("not isZERO( c )", !cp.isZERO() );
296 assertTrue("not isZERO( d )", !dp.isZERO() );
297
298 //System.out.println("a = " + a);
299 //System.out.println("b = " + b);
300 ep = dp.subtract(cp);
301 assertTrue("isZERO( a*b-b*a ) " + ep, ep.isZERO() );
302
303 assertTrue("a*b = b*a", cp.equals(dp) );
304 assertEquals("a*b = b*a",cp,dp);
305
306 cp = mfac.random(kl);
307 //System.out.println("c = " + c);
308 dp = ap.multiply( bp.multiply(cp) );
309 ep = (ap.multiply(bp)).multiply(cp);
310
311 //System.out.println("d = " + d);
312 //System.out.println("e = " + e);
313
314 //System.out.println("d-e = " + d.subtract(c) );
315
316 assertEquals("a(bc) = (ab)c",dp,ep);
317 assertTrue("a(bc) = (ab)c", dp.equals(ep) );
318
319 cp = ap.multiply( mfac.getONE() );
320 dp = mfac.getONE().multiply( ap );
321 assertEquals("a*1 = 1*a",cp,dp);
322
323 if ( ap.isUnit() ) {
324 cp = ap.inverse();
325 dp = cp.multiply(ap);
326 //System.out.println("a = " + a);
327 //System.out.println("c = " + c);
328 //System.out.println("d = " + d);
329 assertTrue("a*1/a = 1",dp.isONE());
330 }
331 }
332
333 }