001 /*
002 * $Id: BigIntegerTest.java 3255 2010-08-08 20:46:37Z kredel $
003 */
004
005 package edu.jas.arith;
006
007
008 import java.util.Iterator;
009
010 //import edu.jas.arith.BigInteger;
011
012 import junit.framework.Test;
013 import junit.framework.TestCase;
014 import junit.framework.TestSuite;
015
016 /**
017 * BigInteger tests with JUnit.
018 * @author Heinz Kredel.
019 */
020
021 public class BigIntegerTest 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>BigIntegerTest</CODE> object.
032 * @param name String.
033 */
034 public BigIntegerTest(String name) {
035 super(name);
036 }
037
038 /**
039 * suite.
040 */
041 public static Test suite() {
042 TestSuite suite= new TestSuite(BigIntegerTest.class);
043 return suite;
044 }
045
046 private final static int bitlen = 100;
047
048 BigInteger a;
049 BigInteger b;
050 BigInteger c;
051 BigInteger d;
052 BigInteger e;
053
054 protected void setUp() {
055 a = b = c = d = e = null;
056 }
057
058 protected void tearDown() {
059 a = b = c = d = e = null;
060 }
061
062
063 /**
064 * Test static initialization and constants.
065 */
066 public void testConstants() {
067 a = BigInteger.ZERO;
068 b = BigInteger.ONE;
069 c = BigInteger.IDIF(b,b);
070
071 assertEquals("1-1 = 0",c,a);
072 assertTrue("1-1 = 0",c.isZERO());
073 assertTrue("1 = 1", b.isONE() );
074 }
075
076
077 /**
078 * Test constructor and toString.
079 */
080 public void testConstructor() {
081 a = new BigInteger( "34" );
082 b = new BigInteger( "34" );
083
084 assertEquals("34 = 34",a,b);
085
086 a = new BigInteger( "-4" );
087 b = new BigInteger( "-4" );
088
089 assertEquals("-4 = -4",a,b);
090
091 String s = "1111111111111111111111111111111111111111111";
092 a = new BigInteger( s );
093 String t = a.toString();
094
095 assertEquals("stringConstr = toString",s,t);
096
097 a = new BigInteger( 1 );
098 b = new BigInteger( -1 );
099 c = BigInteger.ISUM(b,a);
100
101 assertTrue("1 = 1", a.isONE() );
102 assertEquals("1+(-1) = 0",c,BigInteger.ZERO);
103 }
104
105
106 /**
107 * Test random integer.
108 */
109 public void testRandom() {
110 a = BigInteger.IRAND( 500 );
111 b = new BigInteger( "" + a );
112 c = BigInteger.IDIF(b,a);
113
114 assertEquals("a-b = 0",c,BigInteger.ZERO);
115
116 d = new BigInteger( b.getVal() );
117 assertEquals("sign(a-a) = 0", 0, b.compareTo(d) );
118 }
119
120
121 /**
122 * Test addition.
123 */
124 public void testAddition() {
125 a = BigInteger.IRAND( bitlen );
126 b = BigInteger.ISUM(a,a);
127 c = BigInteger.IDIF(b,a);
128
129 assertEquals("a+a-a = a",c,a);
130 assertEquals("a+a-a = a", 0, c.compareTo(a) );
131
132 d = BigInteger.ISUM( a, BigInteger.ZERO );
133 assertEquals("a+0 = a",d,a);
134 d = BigInteger.IDIF( a, BigInteger.ZERO );
135 assertEquals("a-0 = a",d,a);
136 d = BigInteger.IDIF( a, a );
137 assertEquals("a-a = 0", d, BigInteger.ZERO );
138
139 }
140
141
142 /**
143 * Test multiplication.
144 */
145 public void testMultiplication() {
146 a = BigInteger.IRAND( bitlen );
147 b = BigInteger.IPROD( a, a );
148 c = BigInteger.IQ( b, a );
149
150 assertEquals("a*a/a = a",c,a);
151 assertEquals("a*a/a = a",0,BigInteger.ICOMP(c,a));
152
153 d = BigInteger.IPROD( a, BigInteger.ONE );
154 assertEquals("a*1 = a",d,a);
155 d = BigInteger.IQ( a, BigInteger.ONE );
156 assertEquals("a/1 = a",d,a);
157
158 a = BigInteger.IRAND( bitlen*2 );
159 b = BigInteger.IRAND( bitlen );
160 BigInteger[] qr = BigInteger.IQR( a, b );
161 c = BigInteger.IPROD( qr[0], b );
162 c = BigInteger.ISUM( c, qr[1] );
163 assertEquals("a = q*b+r)",a,c);
164 }
165
166
167 /**
168 * Test distributive law.
169 */
170 public void testDistributive() {
171 BigInteger fac = new BigInteger();
172
173 a = fac.random( bitlen );
174 b = fac.random( bitlen );
175 c = fac.random( bitlen );
176
177 d = a.multiply( b.sum(c) );
178 e = a.multiply( b ).sum( a.multiply(c) );
179
180 assertEquals("a(b+c) = ab+ac",d,e);
181 }
182
183
184 /**
185 * Test gcd.
186 */
187 public void testGcd() {
188 a = BigInteger.IRAND( bitlen );
189 b = BigInteger.IRAND( bitlen );
190 c = BigInteger.IGCD( a, b ); // ~1
191
192 BigInteger[] qr = BigInteger.IQR( a, c );
193 d = BigInteger.IPROD( qr[0], c );
194 assertEquals("a = gcd(a,b)*q1",a,d);
195 assertEquals("a/gcd(a,b) = q*x + 0", qr[1], BigInteger.ZERO );
196
197 qr = BigInteger.IQR( b, c );
198 d = BigInteger.IPROD( qr[0], c );
199 assertEquals("b = gcd(a,b)*q1",b,d);
200 assertEquals("b/gcd(a,b) = q*x + 0", qr[1], BigInteger.ZERO );
201
202
203 c = BigInteger.IRAND( bitlen*4 );
204 a = BigInteger.IPROD( a, c );
205 b = BigInteger.IPROD( b, c );
206 c = BigInteger.IGCD( a, b ); // = c
207
208 qr = BigInteger.IQR( a, c );
209 d = BigInteger.IPROD( qr[0], c );
210 assertEquals("a = gcd(a,b)*q1",a,d);
211 assertEquals("a/gcd(a,b) = q*x + 0", qr[1], BigInteger.ZERO );
212
213 qr = BigInteger.IQR( b, c );
214 d = BigInteger.IPROD( qr[0], c );
215 assertEquals("b = gcd(a,b)*q1",b,d);
216 assertEquals("b/gcd(a,b) = q*x + 0", qr[1], BigInteger.ZERO );
217
218 }
219
220
221 /**
222 * Test iterator.
223 */
224 public void testIterator() {
225 int t = 0;
226 BigInteger bi = new BigInteger();
227 bi.setAllIterator();
228 BigInteger j = null, ten = null;
229 for ( BigInteger i : bi ) {
230 t++;
231 //System.out.println("i = " + i);
232 if ( t >= 20 ) {
233 j = i;
234 break;
235 }
236 }
237 ten = new BigInteger(10);
238 assertTrue("j == 10 ", j.equals(ten) );
239 }
240
241
242 /**
243 * Test non-negative iterator.
244 */
245 public void testNNIterator() {
246 int t = 0;
247 BigInteger bi = new BigInteger();
248 bi.setNonNegativeIterator();
249 BigInteger j = null, ten = null;
250 Iterator<BigInteger> iter = bi.iterator();
251 while ( iter.hasNext() ) {
252 BigInteger i = iter.next();
253 t++;
254 //System.out.println("i = " + i);
255 if ( t > 20 ) {
256 j = i;
257 break;
258 }
259 }
260 ten = new BigInteger(20);
261 assertTrue("j == 10 ", j.equals(ten) );
262 }
263
264 }