001
002/*
003 * $Id$
004 */
005
006package edu.jas.ufd;
007
008
009import java.util.SortedMap;
010
011import edu.jas.arith.BigInteger;
012import edu.jas.kern.ComputerThreads;
013import edu.jas.kern.PrettyPrint;
014import edu.jas.poly.GenPolynomialRing;
015import edu.jas.poly.TermOrder;
016
017import junit.framework.Test;
018import junit.framework.TestCase;
019import junit.framework.TestSuite;
020
021
022/**
023 * Quotient over BigInteger GenPolynomial tests with JUnit.
024 * @author Heinz Kredel
025 */
026
027public class QuotientIntTest extends TestCase {
028
029
030    /**
031     * main.
032     */
033    public static void main(String[] args) {
034        junit.textui.TestRunner.run(suite());
035    }
036
037
038    /**
039     * Constructs a <CODE>QuotientIntTest</CODE> object.
040     * @param name String.
041     */
042    public QuotientIntTest(String name) {
043        super(name);
044    }
045
046
047    /**
048     * suite.
049     */
050    public static Test suite() {
051        TestSuite suite = new TestSuite(QuotientIntTest.class);
052        return suite;
053    }
054
055
056    QuotientRing<BigInteger> efac;
057
058
059    GenPolynomialRing<BigInteger> mfac;
060
061
062    Quotient<BigInteger> a, b, c, d, e;
063
064
065    int rl = 3;
066
067
068    int kl = 5;
069
070
071    int ll = 4; //6;
072
073
074    int el = 2;
075
076
077    float q = 0.4f;
078
079
080    @Override
081    protected void setUp() {
082        a = b = c = d = e = null;
083        BigInteger cfac = new BigInteger(1);
084        TermOrder to = new TermOrder(TermOrder.INVLEX);
085        mfac = new GenPolynomialRing<BigInteger>(cfac, rl, to);
086        efac = new QuotientRing<BigInteger>(mfac);
087    }
088
089
090    @Override
091    protected void tearDown() {
092        a = b = c = d = e = null;
093        //efac.terminate();
094        efac = null;
095        ComputerThreads.terminate();
096    }
097
098
099    /**
100     * Test constructor and toString.
101     */
102    public void testConstruction() {
103        c = efac.getONE();
104        //System.out.println("c = " + c);
105        //System.out.println("c.val = " + c.val);
106        assertTrue("length( c ) = 1", c.num.length() == 1);
107        assertTrue("isZERO( c )", !c.isZERO());
108        assertTrue("isONE( c )", c.isONE());
109
110        d = efac.getZERO();
111        //System.out.println("d = " + d);
112        //System.out.println("d.val = " + d.val);
113        assertTrue("length( d ) = 0", d.num.length() == 0);
114        assertTrue("isZERO( d )", d.isZERO());
115        assertTrue("isONE( d )", !d.isONE());
116    }
117
118
119    /**
120     * Test random polynomial.
121     */
122    public void testRandom() {
123        for (int i = 0; i < 7; i++) {
124            //a = efac.random(ll+i);
125            a = efac.random(kl * (i + 1), ll + 2 + 2 * i, el, q);
126            //System.out.println("a = " + a);
127            if (a.isZERO() || a.isONE()) {
128                continue;
129            }
130            assertTrue("length( a" + i + " ) <> 0", a.num.length() >= 0);
131            assertTrue(" not isZERO( a" + i + " )", !a.isZERO());
132            assertTrue(" not isONE( a" + i + " )", !a.isONE());
133        }
134    }
135
136
137    /**
138     * Test addition.
139     */
140    public void testAddition() {
141        a = efac.random(kl, ll, el, q);
142        b = efac.random(kl, ll, el, q);
143        //System.out.println("a = " + a);
144        //System.out.println("b = " + b);
145
146        c = a.sum(b);
147        d = c.subtract(b);
148        assertEquals("a+b-b = a", a, d);
149
150        c = a.sum(b);
151        d = b.sum(a);
152        //System.out.println("c = " + c);
153        //System.out.println("d = " + d);
154
155        assertEquals("a+b = b+a", c, d);
156
157        c = efac.random(kl, ll, el, q);
158        d = c.sum(a.sum(b));
159        e = c.sum(a).sum(b);
160        assertEquals("c+(a+b) = (c+a)+b", d, e);
161
162
163        c = a.sum(efac.getZERO());
164        d = a.subtract(efac.getZERO());
165        assertEquals("a+0 = a-0", c, d);
166
167        c = efac.getZERO().sum(a);
168        d = efac.getZERO().subtract(a.negate());
169        assertEquals("0+a = 0+(-a)", c, d);
170    }
171
172
173    /**
174     * Test object multiplication.
175     */
176    public void testMultiplication() {
177        a = efac.random(kl, ll, el, q);
178        //assertTrue("not isZERO( a )", !a.isZERO() );
179
180        b = efac.random(kl, ll, el, q);
181        //assertTrue("not isZERO( b )", !b.isZERO() );
182
183        c = b.multiply(a);
184        d = a.multiply(b);
185        if (!a.isZERO() && !b.isZERO()) {
186            assertTrue("not isZERO( c )", !c.isZERO());
187            assertTrue("not isZERO( d )", !d.isZERO());
188        }
189
190        //System.out.println("a = " + a);
191        //System.out.println("b = " + b);
192        e = d.subtract(c);
193        assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO());
194
195        assertTrue("a*b = b*a", c.equals(d));
196        assertEquals("a*b = b*a", c, d);
197
198        c = efac.random(kl, ll, el, q);
199        //System.out.println("c = " + c);
200        d = a.multiply(b.multiply(c));
201        e = (a.multiply(b)).multiply(c);
202
203        //System.out.println("d = " + d);
204        //System.out.println("e = " + e);
205
206        //System.out.println("d-e = " + d.subtract(c) );
207
208        assertEquals("a(bc) = (ab)c", d, e);
209        assertTrue("a(bc) = (ab)c", d.equals(e));
210
211        c = a.multiply(efac.getONE());
212        d = efac.getONE().multiply(a);
213        assertEquals("a*1 = 1*a", c, d);
214
215        if (a.isUnit()) {
216            c = a.inverse();
217            d = c.multiply(a);
218            //System.out.println("a = " + a);
219            //System.out.println("c = " + c);
220            //System.out.println("d = " + d);
221            assertTrue("a*1/a = 1", d.isONE());
222        }
223    }
224
225
226    /**
227     * Test parse().
228     */
229    public void testParse() {
230        a = efac.random(kl * 2, ll * 2, el * 2, q * 2);
231        //assertTrue("not isZERO( a )", !a.isZERO() );
232
233        //PrettyPrint.setInternal();
234        //System.out.println("a = " + a);
235        PrettyPrint.setPretty();
236        //System.out.println("a = " + a);
237        String p = a.toString();
238        //System.out.println("p = " + p);
239        b = efac.parse(p);
240        //System.out.println("b = " + b);
241
242        //c = a.subtract(b);
243        //System.out.println("c = " + c);
244        assertEquals("parse(a.toSting()) = a", a, b);
245    }
246
247
248    /**
249     * Test factorization.
250     */
251    public void testFactors() {
252        a = efac.random(kl, ll, el, q);
253        b = efac.random(kl, ll, el, q);
254        b = b.multiply(b);
255        //System.out.println("a = " + a);
256        //System.out.println("b = " + b);
257
258        c = a.multiply(b);
259        //System.out.println("c = " + c);
260
261        SortedMap<Quotient<BigInteger>, Long> factors = PolyUfdUtil.<BigInteger> factors(c);
262        //System.out.println("factors = " + factors);
263
264        boolean t = PolyUfdUtil.<BigInteger> isFactorization(c, factors);
265        //System.out.println("t = " + t);
266        assertTrue("c == prod(factors): " + c + ", " + factors, t);
267    }
268
269}