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