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