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