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