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