001    
002    /*
003     * $Id: QuotientTest.java 3365 2010-10-24 12:59:34Z 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    //import edu.jas.structure.RingElem;
020    
021    
022    
023    /**
024     * Quotient tests with JUnit. 
025     * @author Heinz Kredel.
026     */
027    
028    public class QuotientTest extends TestCase {
029    
030    /**
031     * main.
032     */
033       public static void main (String[] args) {
034           BasicConfigurator.configure();
035           junit.textui.TestRunner.run( suite() );
036       }
037    
038    /**
039     * Constructs a <CODE>QuotientTest</CODE> object.
040     * @param name String.
041     */
042       public QuotientTest(String name) {
043              super(name);
044       }
045    
046    /**
047     * suite.
048     */ 
049     public static Test suite() {
050         TestSuite suite= new TestSuite(QuotientTest.class);
051         return suite;
052       }
053    
054    
055       QuotientRing<BigInteger> fac;
056       GenPolynomialRing<BigInteger> pfac;
057       QuotientRing< GenPolynomial<BigInteger> > mfac;
058    
059       Quotient< BigInteger > a;
060       Quotient< BigInteger > b;
061       Quotient< BigInteger > c;
062       Quotient< BigInteger > d;
063       Quotient< BigInteger > e;
064    
065       Quotient< GenPolynomial<BigInteger> > ap;
066       Quotient< GenPolynomial<BigInteger> > bp;
067       Quotient< GenPolynomial<BigInteger> > cp;
068       Quotient< GenPolynomial<BigInteger> > dp;
069       Quotient< GenPolynomial<BigInteger> > ep;
070    
071       int rl = 2; 
072       int kl = 17;
073       int ll = 3; //6;
074       int el = 2;
075       float q = 0.4f;
076    
077       protected void setUp() {
078           a = b = c = d = e = null;
079           ap = bp = cp = dp = ep = null;
080           fac = new QuotientRing<BigInteger>( new BigInteger(1) );
081           pfac = new GenPolynomialRing<BigInteger>( new BigInteger(1), 1 );
082           mfac = new QuotientRing<GenPolynomial<BigInteger>>( pfac );
083       }
084    
085       protected void tearDown() {
086           a = b = c = d = e = null;
087           ap = bp = cp = dp = ep = null;
088           fac = null;
089           pfac = null;
090           mfac = null;
091       }
092    
093    
094    /**
095     * Test constructor for integer.
096     * 
097     */
098     public void testIntConstruction() {
099         c = fac.getONE();
100         //System.out.println("c = " + c);
101         assertTrue("isZERO( c )", !c.isZERO() );
102         assertTrue("isONE( c )", c.isONE() );
103    
104         d = fac.getZERO();
105         //System.out.println("d = " + d);
106         assertTrue("isZERO( d )", d.isZERO() );
107         assertTrue("isONE( d )", !d.isONE() );
108     }
109    
110    
111    /**
112     * Test constructor for polynomial.
113     * 
114     */
115     public void testPolyConstruction() {
116         cp = mfac.getONE();
117         assertTrue("isZERO( cp )", !cp.isZERO() );
118         assertTrue("isONE( cp )", cp.isONE() );
119    
120         dp = mfac.getZERO();
121         assertTrue("isZERO( dp )", dp.isZERO() );
122         assertTrue("isONE( dp )", !dp.isONE() );
123     }
124    
125    
126    /**
127     * Test random integer.
128     * 
129     */
130     public void testIntRandom() {
131         for (int i = 0; i < 7; i++) {
132             a = fac.random(kl*(i+1));
133             //a = fac.random(kl*(i+1), ll+2+2*i, el, q );
134             //System.out.println("a = " + a);
135             assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
136             assertTrue(" not isONE( a"+i+" )", !a.isONE() );
137         }
138     }
139    
140    
141    /**
142     * Test random polynomial.
143     * 
144     */
145     public void testPolyRandom() {
146         for (int i = 0; i < 7; i++) {
147             ap = mfac.random(kl+i);
148             assertTrue(" not isZERO( ap"+i+" )", !ap.isZERO() );
149             assertTrue(" not isONE( ap"+i+" )", !ap.isONE() );
150         }
151     }
152    
153    
154    /**
155     * Test integer addition.
156     * 
157     */
158     public void testIntAddition() {
159    
160         a = fac.random(kl);
161         b = fac.random(kl);
162         //System.out.println("a = " + a);
163         //System.out.println("b = " + b);
164    
165         c = a.sum(b);
166         d = c.subtract(b);
167         assertEquals("a+b-b = a",a,d);
168    
169         c = a.sum(b);
170         d = b.sum(a);
171         //System.out.println("c = " + c);
172         //System.out.println("d = " + d);
173    
174         assertEquals("a+b = b+a",c,d);
175    
176         c = fac.random(kl);
177         d = c.sum( a.sum(b) );
178         e = c.sum( a ).sum(b);
179         assertEquals("c+(a+b) = (c+a)+b",d,e);
180    
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     */
234     public void testIntMultiplication() {
235    
236         a = fac.random(kl);
237         assertTrue("not isZERO( a )", !a.isZERO() );
238    
239         b = fac.random(kl);
240         assertTrue("not isZERO( b )", !b.isZERO() );
241    
242         c = b.multiply(a);
243         d = a.multiply(b);
244         assertTrue("not isZERO( c )", !c.isZERO() );
245         assertTrue("not isZERO( d )", !d.isZERO() );
246    
247         //System.out.println("a = " + a);
248         //System.out.println("b = " + b);
249         e = d.subtract(c);
250         assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
251    
252         assertTrue("a*b = b*a", c.equals(d) );
253         assertEquals("a*b = b*a",c,d);
254    
255         c = fac.random(kl);
256         //System.out.println("c = " + c);
257         d = a.multiply( b.multiply(c) );
258         e = (a.multiply(b)).multiply(c);
259    
260         //System.out.println("d = " + d);
261         //System.out.println("e = " + e);
262    
263         //System.out.println("d-e = " + d.subtract(c) );
264    
265         assertEquals("a(bc) = (ab)c",d,e);
266         assertTrue("a(bc) = (ab)c", d.equals(e) );
267    
268         c = a.multiply( fac.getONE() );
269         d = fac.getONE().multiply( a );
270         assertEquals("a*1 = 1*a",c,d);
271    
272         if ( a.isUnit() ) {
273            c = a.inverse();
274            d = c.multiply(a);
275            //System.out.println("a = " + a);
276            //System.out.println("c = " + c);
277            //System.out.println("d = " + d);
278            assertTrue("a*1/a = 1",d.isONE()); 
279         }
280     }
281    
282    
283    /**
284     * Test polynomial multiplication.
285     * 
286     */
287     public void testPolyMultiplication() {
288    
289         ap = mfac.random(kl);
290         assertTrue("not isZERO( a )", !ap.isZERO() );
291    
292         bp = mfac.random(kl);
293         assertTrue("not isZERO( b )", !bp.isZERO() );
294    
295         cp = bp.multiply(ap);
296         dp = ap.multiply(bp);
297         assertTrue("not isZERO( c )", !cp.isZERO() );
298         assertTrue("not isZERO( d )", !dp.isZERO() );
299    
300         //System.out.println("a = " + a);
301         //System.out.println("b = " + b);
302         ep = dp.subtract(cp);
303         assertTrue("isZERO( a*b-b*a ) " + ep, ep.isZERO() );
304    
305         assertTrue("a*b = b*a", cp.equals(dp) );
306         assertEquals("a*b = b*a",cp,dp);
307    
308         cp = mfac.random(kl);
309         //System.out.println("c = " + c);
310         dp = ap.multiply( bp.multiply(cp) );
311         ep = (ap.multiply(bp)).multiply(cp);
312    
313         //System.out.println("d = " + d);
314         //System.out.println("e = " + e);
315    
316         //System.out.println("d-e = " + d.subtract(c) );
317    
318         assertEquals("a(bc) = (ab)c",dp,ep);
319         assertTrue("a(bc) = (ab)c", dp.equals(ep) );
320    
321         cp = ap.multiply( mfac.getONE() );
322         dp = mfac.getONE().multiply( ap );
323         assertEquals("a*1 = 1*a",cp,dp);
324    
325         if ( ap.isUnit() ) {
326            cp = ap.inverse();
327            dp = cp.multiply(ap);
328            //System.out.println("a = " + a);
329            //System.out.println("c = " + c);
330            //System.out.println("d = " + d);
331            assertTrue("a*1/a = 1",dp.isONE()); 
332         }
333     }
334    
335    }