001/*
002 * $Id: ModGenPolynomialTest.java 3789 2011-10-01 18:54:43Z kredel $
003 */
004
005package edu.jas.poly;
006
007import junit.framework.Test;
008import junit.framework.TestCase;
009import junit.framework.TestSuite;
010
011import edu.jas.poly.GenPolynomial;
012import edu.jas.arith.ModInteger;
013import edu.jas.arith.ModIntegerRing;
014//import edu.jas.structure.RingElem;
015
016
017/**
018 * ModInteger coefficients GenPolynomial tests with JUnit.
019 * @author Heinz Kredel.
020 */
021
022public class ModGenPolynomialTest extends TestCase {
023
024/**
025 * main.
026 */
027   public static void main (String[] args) {
028          junit.textui.TestRunner.run( suite() );
029   }
030
031/**
032 * Constructs a <CODE>ModGenPolynomialTest</CODE> object.
033 * @param name String.
034 */
035   public ModGenPolynomialTest(String name) {
036          super(name);
037   }
038
039/**
040 * suite.
041 */ 
042 public static Test suite() {
043     TestSuite suite= new TestSuite(ModGenPolynomialTest.class);
044     return suite;
045   }
046
047   //private final static int bitlen = 100;
048
049   GenPolynomialRing<ModInteger> fac;
050
051   GenPolynomial<ModInteger> a;
052   GenPolynomial<ModInteger> b;
053   GenPolynomial<ModInteger> c;
054   GenPolynomial<ModInteger> d;
055   GenPolynomial<ModInteger> e;
056
057   int ml = 19; // modul 
058   int rl = 7; 
059   int kl = 10;
060   int ll = 10;
061   int el = 5;
062   float q = 0.5f;
063
064   protected void setUp() {
065       a = b = c = d = e = null;
066       fac = new GenPolynomialRing<ModInteger>(new ModIntegerRing(ml),rl);
067   }
068
069   protected void tearDown() {
070       a = b = c = d = e = null;
071       fac = null;
072   }
073
074
075/**
076 * Test constructor and toString.
077 * 
078 */
079 public void testConstruction() {
080     c = fac.getONE();
081     assertTrue("length( c ) = 1", c.length() == 1);
082     assertTrue("isZERO( c )", !c.isZERO() );
083     assertTrue("isONE( c )", c.isONE() );
084
085     d = fac.getZERO();
086     assertTrue("length( d ) = 0", d.length() == 0);
087     assertTrue("isZERO( d )", d.isZERO() );
088     assertTrue("isONE( d )", !d.isONE() );
089 }
090
091
092/**
093 * Test random polynomial.
094 * 
095 */
096 public void testRandom() {
097     for (int i = 0; i < 7; i++) {
098         a = fac.random(ll);
099             // fac.random(rl+i, kl*(i+1), ll+2*i, el+i, q );
100         assertTrue("length( a"+i+" ) <> 0", a.length() >= 0);
101         assertTrue(" not isZERO( a"+i+" )", !a.isZERO() );
102         assertTrue(" not isONE( a"+i+" )", !a.isONE() );
103     }
104 }
105
106
107/**
108 * Test addition.
109 * 
110 */
111 public void testAddition() {
112
113     a = fac.random(ll);
114     b = fac.random(ll);
115
116     c = a.sum(b);
117     d = c.subtract(b);
118     assertEquals("a+b-b = a",a,d);
119
120     c = fac.random(ll);
121
122     ExpVector u = ExpVector.EVRAND(rl,el,q);
123     ModInteger x = c.leadingBaseCoefficient().ring.random(kl);
124
125     b = new GenPolynomial<ModInteger>(fac,x, u);
126     c = a.sum(b);
127     d = a.sum(x,u);
128     assertEquals("a+p(x,u) = a+(x,u)",c,d);
129
130     c = a.subtract(b);
131     d = a.subtract(x,u);
132     assertEquals("a-p(x,u) = a-(x,u)",c,d);
133
134     a = new GenPolynomial<ModInteger>(fac);
135     b = new GenPolynomial<ModInteger>(fac,x, u);
136     c = b.sum(a);
137     d = a.sum(x,u);
138     assertEquals("a+p(x,u) = a+(x,u)",c,d);
139
140     c = a.subtract(b);
141     d = a.subtract(x,u);
142     assertEquals("a-p(x,u) = a-(x,u)",c,d);
143 }
144
145
146/**
147 * Test object multiplication.
148 * 
149 */
150
151 public void testMultiplication() {
152
153     a = fac.random(ll);
154     assertTrue("not isZERO( a )", !a.isZERO() );
155
156     b = fac.random(ll);
157     assertTrue("not isZERO( b )", !b.isZERO() );
158
159     c = b.multiply(a);
160     d = a.multiply(b);
161     assertTrue("not isZERO( c )", !c.isZERO() );
162     assertTrue("not isZERO( d )", !d.isZERO() );
163
164     //System.out.println("a = " + a);
165     //System.out.println("b = " + b);
166     e = d.subtract(c);
167     assertTrue("isZERO( a*b-b*a ) " + e, e.isZERO() );
168
169     assertTrue("a*b = b*a", c.equals(d) );
170     assertEquals("a*b = b*a",c,d);
171
172     c = fac.random(ll);
173     //System.out.println("c = " + c);
174     d = a.multiply( b.multiply(c) );
175     e = (a.multiply(b)).multiply(c);
176
177     //System.out.println("d = " + d);
178     //System.out.println("e = " + e);
179
180     //System.out.println("d-e = " + d.subtract(c) );
181
182     assertEquals("a(bc) = (ab)c",d,e);
183     assertTrue("a(bc) = (ab)c", d.equals(e) );
184
185     ModInteger x = a.leadingBaseCoefficient().inverse();
186     c = a.monic();
187     d = a.multiply(x);
188     assertEquals("a.monic() = a(1/ldcf(a))",c,d);
189
190     ModInteger y = b.leadingBaseCoefficient().inverse();
191     c = b.monic();
192     d = b.multiply(y);
193     assertEquals("b.monic() = b(1/ldcf(b))",c,d);
194
195     e = new GenPolynomial<ModInteger>(fac,y);
196     d = b.multiply(e);
197     assertEquals("b.monic() = b(1/ldcf(b))",c,d);
198
199     d = e.multiply(b);
200     assertEquals("b.monic() = (1/ldcf(b) (0))*b",c,d);
201 }
202
203
204/**
205 * Test distributive law.
206 * 
207 */
208 public void testDistributive() {
209     a = fac.random(kl,ll,el,q);
210     b = fac.random(kl,ll,el,q);
211     c = fac.random(kl,ll,el,q);
212
213     d = a.multiply( b.sum(c) );
214     e = a.multiply( b ).sum( a.multiply(c) );
215
216     assertEquals("a(b+c) = ab+ac",d,e);
217 }
218
219}