001    /*
002     * $Id: GCDFactoryTest.java 3031 2010-03-08 23:18:01Z kredel $
003     */
004    
005    package edu.jas.ufd;
006    
007    
008    //import java.util.Map;
009    
010    import junit.framework.Test;
011    import junit.framework.TestCase;
012    import junit.framework.TestSuite;
013    
014    import org.apache.log4j.BasicConfigurator;
015    
016    import edu.jas.arith.BigComplex;
017    import edu.jas.arith.BigInteger;
018    import edu.jas.arith.BigRational;
019    import edu.jas.arith.ModInteger;
020    import edu.jas.arith.ModIntegerRing;
021    import edu.jas.poly.AlgebraicNumber;
022    import edu.jas.poly.AlgebraicNumberRing;
023    import edu.jas.poly.GenPolynomial;
024    import edu.jas.poly.GenPolynomialRing;
025    import edu.jas.poly.TermOrder;
026    
027    
028    /**
029     * GreatestCommonDivisor factory tests with JUnit.
030     * @author Heinz Kredel.
031     */
032    
033    public class GCDFactoryTest extends TestCase {
034    
035    
036        /**
037         * main.
038         */
039        public static void main(String[] args) {
040            BasicConfigurator.configure();
041            junit.textui.TestRunner.run(suite());
042        }
043    
044    
045        /**
046         * Constructs a <CODE>GCDFactoryTest</CODE> object.
047         * @param name String.
048         */
049        public GCDFactoryTest(String name) {
050            super(name);
051        }
052    
053    
054        /**
055         */
056        public static Test suite() {
057            TestSuite suite = new TestSuite(GCDFactoryTest.class);
058            return suite;
059        }
060    
061    
062        //private final static int bitlen = 100;
063    
064        TermOrder to = new TermOrder(TermOrder.INVLEX);
065    
066    
067        GenPolynomialRing<BigInteger> dfac;
068    
069    
070        GenPolynomialRing<BigInteger> cfac;
071    
072    
073        GenPolynomialRing<GenPolynomial<BigInteger>> rfac;
074    
075    
076        BigInteger ai;
077    
078    
079        BigInteger bi;
080    
081    
082        BigInteger ci;
083    
084    
085        BigInteger di;
086    
087    
088        BigInteger ei;
089    
090    
091        GenPolynomial<BigInteger> a;
092    
093    
094        GenPolynomial<BigInteger> b;
095    
096    
097        GenPolynomial<BigInteger> c;
098    
099    
100        GenPolynomial<BigInteger> d;
101    
102    
103        GenPolynomial<BigInteger> e;
104    
105    
106        GenPolynomial<GenPolynomial<BigInteger>> ar;
107    
108    
109        GenPolynomial<GenPolynomial<BigInteger>> br;
110    
111    
112        GenPolynomial<GenPolynomial<BigInteger>> cr;
113    
114    
115        GenPolynomial<GenPolynomial<BigInteger>> dr;
116    
117    
118        GenPolynomial<GenPolynomial<BigInteger>> er;
119    
120    
121        int rl = 5;
122    
123    
124        int kl = 4;
125    
126    
127        int ll = 5;
128    
129    
130        int el = 3;
131    
132    
133        float q = 0.3f;
134    
135    
136        @Override
137        protected void setUp() {
138            a = b = c = d = e = null;
139            ai = bi = ci = di = ei = null;
140            ar = br = cr = dr = er = null;
141            dfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl, to);
142            cfac = new GenPolynomialRing<BigInteger>(new BigInteger(1), rl - 1, to);
143            rfac = new GenPolynomialRing<GenPolynomial<BigInteger>>(cfac, 1, to);
144        }
145    
146    
147        @Override
148        protected void tearDown() {
149            a = b = c = d = e = null;
150            ai = bi = ci = di = ei = null;
151            ar = br = cr = dr = er = null;
152            dfac = null;
153            cfac = null;
154            rfac = null;
155        }
156    
157    
158        /**
159         * Test get BigInteger implementation.
160         * 
161         */
162        public void testBigInteger() {
163            BigInteger bi = new BigInteger();
164            GreatestCommonDivisor<BigInteger> ufd;
165    
166            ufd = GCDFactory.getImplementation(bi);
167            //System.out.println("ufd = " + ufd);
168            assertTrue("ufd = Modular " + ufd, ufd instanceof GreatestCommonDivisorModular);
169        }
170    
171    
172        /**
173         * Test get ModInteger implementation.
174         * 
175         */
176        public void testModInteger() {
177            ModIntegerRing mi = new ModIntegerRing(19, true);
178            GreatestCommonDivisor<ModInteger> ufd;
179    
180            ufd = GCDFactory.getImplementation(mi);
181            //System.out.println("ufd = " + ufd);
182            assertTrue("ufd != ModEval " + ufd, ufd instanceof GreatestCommonDivisorModEval);
183    
184            mi = new ModIntegerRing(30);
185            ufd = GCDFactory.getImplementation(mi);
186            //System.out.println("ufd = " + ufd);
187            assertTrue("ufd != Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres);
188        }
189    
190    
191        /**
192         * Test get BigRational implementation.
193         * 
194         */
195        public void testBigRational() {
196            BigRational b = new BigRational();
197            GreatestCommonDivisor<BigRational> ufd;
198    
199            ufd = GCDFactory.getImplementation(b);
200            //System.out.println("ufd = " + ufd);
201            assertTrue("ufd = Primitive " + ufd, ufd instanceof GreatestCommonDivisorPrimitive);
202        }
203    
204    
205        /**
206         * Test get BigComplex implementation.
207         * 
208         */
209        public void testBigComplex() {
210            BigComplex b = new BigComplex();
211            GreatestCommonDivisor<BigComplex> ufd;
212    
213            ufd = GCDFactory.<BigComplex> getImplementation(b);
214            //System.out.println("ufd = " + ufd);
215            assertTrue("ufd != Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple);
216        }
217    
218    
219        /**
220         * Test get AlgebraicNumber&lt;BigRational&gt; implementation.
221         * 
222         */
223        public void testAlgebraicNumberBigRational() {
224            BigRational b = new BigRational();
225            GenPolynomialRing<BigRational> fac;
226            fac = new GenPolynomialRing<BigRational>(b, 1);
227            GenPolynomial<BigRational> mo = fac.random(kl, ll, el, q);
228            while (mo.isZERO() || mo.isONE() || mo.isConstant()) {
229                mo = fac.random(kl, ll, el, q);
230            }
231    
232            AlgebraicNumberRing<BigRational> afac;
233            afac = new AlgebraicNumberRing<BigRational>(mo);
234    
235            GreatestCommonDivisor<AlgebraicNumber<BigRational>> ufd;
236    
237            ufd = GCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac);
238            //System.out.println("ufd1 = " + ufd);
239            assertTrue("ufd = Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres);
240    
241    
242            mo = fac.univariate(0).subtract(fac.getONE());
243            afac = new AlgebraicNumberRing<BigRational>(mo, true);
244    
245            ufd = GCDFactory.<AlgebraicNumber<BigRational>> getImplementation(afac);
246            //System.out.println("ufd1 = " + ufd);
247            assertTrue("ufd = Simple " + ufd, ufd instanceof GreatestCommonDivisorSimple);
248        }
249    
250    
251        /**
252         * Test get AlgebraicNumber&lt;ModInteger&gt; implementation.
253         * 
254         */
255        public void testAlgebraicNumberModInteger() {
256            ModIntegerRing b = new ModIntegerRing(19, true);
257            GenPolynomialRing<ModInteger> fac;
258            fac = new GenPolynomialRing<ModInteger>(b, 1);
259            GenPolynomial<ModInteger> mo = fac.random(kl, ll, el, q);
260            while (mo.isZERO() || mo.isONE() || mo.isConstant()) {
261                mo = fac.random(kl, ll, el, q);
262            }
263    
264            AlgebraicNumberRing<ModInteger> afac;
265            afac = new AlgebraicNumberRing<ModInteger>(mo);
266    
267            AlgebraicNumber<ModInteger> a = afac.getONE();
268            GreatestCommonDivisor<AlgebraicNumber<ModInteger>> ufd;
269    
270            ufd = GCDFactory.<AlgebraicNumber<ModInteger>> getImplementation(afac);
271            //System.out.println("ufd2 = " + ufd);
272            assertTrue("ufd = Subres " + ufd, ufd instanceof GreatestCommonDivisorSubres);
273        }
274    
275    }