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