001/*
002 * $Id$
003 */
004
005package edu.jas.arith;
006
007
008import java.util.LinkedList;
009import java.util.List;
010
011import edu.jas.structure.MonoidElem;
012import edu.jas.structure.Power;
013
014import junit.framework.Test;
015import junit.framework.TestCase;
016import junit.framework.TestSuite;
017
018
019/**
020 * Basic power and structure tests with JUnit.
021 * @author Heinz Kredel
022 */
023
024public class PowerTest extends TestCase {
025
026
027    /**
028     * main.
029     */
030    public static void main(String[] args) {
031        junit.textui.TestRunner.run(suite());
032    }
033
034
035    /**
036     * Constructs a <CODE>PowerTest</CODE> object.
037     * @param name String.
038     */
039    public PowerTest(String name) {
040        super(name);
041    }
042
043
044    /**
045     * suite.
046     */
047    public static Test suite() {
048        TestSuite suite = new TestSuite(PowerTest.class);
049        return suite;
050    }
051
052
053    @Override
054    protected void setUp() {
055        //a = b = c = d = e = null;
056    }
057
058
059    @Override
060    protected void tearDown() {
061        //a = b = c = d = e = null;
062    }
063
064
065    /**
066     * Test addition for Integer.
067     */
068    public void testAddition() {
069        BigInteger a, b, c;
070        List<BigInteger> L = new LinkedList<BigInteger>();
071        int n = 471;
072        a = BigInteger.ZERO;
073        for (int i = 1; i < n; i++) {
074            b = BigInteger.ZERO.random(50);
075            L.add(b);
076            a = a.sum(b);
077        }
078        c = Power.<BigInteger> sum(BigInteger.ONE, L);
079        assertEquals("a = c", c, a);
080    }
081
082
083    /**
084     * Test multiplication for Integer.
085     */
086    public void testMultiplication() {
087        BigInteger a, b, c;
088        List<BigInteger> L = new LinkedList<BigInteger>();
089        int n = 471;
090        a = BigInteger.ONE;
091        for (int i = 1; i < n; i++) {
092            b = BigInteger.ZERO.random(50);
093            L.add(b);
094            a = a.multiply(b);
095        }
096        c = Power.<BigInteger> multiply(BigInteger.ONE, L);
097        assertEquals("a = c", c, a);
098    }
099
100
101    /**
102     * Test logarithm for Integer.
103     */
104    public void testLog() {
105        long x = Power.logarithm(2, 1024);
106        //System.out.println("x = " + x);
107        assertEquals("x = 10", x, 10);
108
109        BigInteger a, b;
110        a = new BigInteger("2");
111        b = new BigInteger("1024");
112        x = Power.<BigInteger> logarithm(a, b);
113        //System.out.println("x = " + x);
114        assertEquals("x = 10", x, 10L);
115    }
116
117
118    /**
119     * Test power for Rational.
120     */
121    public void testRationalPower() {
122        BigRational a, b, c, d;
123        a = BigRational.ZERO.random(100);
124
125        // power operations
126        b = Power.<BigRational> positivePower(a, 1);
127        assertEquals("a^1 = a", b, a);
128
129        Power<BigRational> pow = new Power<BigRational>(BigRational.ONE);
130        b = pow.power(a, 1);
131        assertEquals("a^1 = a", b, a);
132
133        b = pow.power(a, 2);
134        c = a.multiply(a);
135        assertEquals("a^2 = a*a", b, c);
136
137        d = pow.power(a, -2);
138        c = b.multiply(d);
139        assertTrue("a^2 * a^-2 = 1", c.isONE());
140
141        b = pow.power(a, 3);
142        c = a.multiply(a).multiply(a);
143        assertEquals("a^3 = a*a*a", b, c);
144
145        d = pow.power(a, -3);
146        c = b.multiply(d);
147        assertTrue("a^3 * a^-3 = 1", c.isONE());
148
149        //Java 8:
150        d = a.power(-3);
151        c = b.multiply(d);
152        assertTrue("a^3 * a^-3 = 1", c.isONE());
153
154        d = a.power(0);
155        c = BigRational.ONE;
156        assertEquals("a^0 == 1", c, d);
157
158        d = a.power(3);
159        c = a.multiply(a).multiply(a);
160        assertEquals("a^3 == a*a*a", c, d);
161    }
162
163
164    /**
165     * Test power for Integer.
166     */
167    public void testIntegerPower() {
168        BigInteger a, b, c, d, e;
169        a = BigInteger.ZERO.random(500);
170
171        // power operations
172        b = Power.<BigInteger> positivePower(a, 1);
173        assertEquals("a^1 = a", b, a);
174
175        Power<BigInteger> pow = new Power<BigInteger>(BigInteger.ONE);
176        b = pow.power(a, 1);
177        assertEquals("a^1 = a", b, a);
178
179        b = pow.power(a, 2);
180        c = a.multiply(a);
181        assertEquals("a^2 = a*a", b, c);
182
183        b = pow.power(a, 3);
184        c = a.multiply(a).multiply(a);
185        assertEquals("a^3 = a*a*a", b, c);
186
187        // mod power operations
188        a = new BigInteger(3);
189        b = Power.<BigInteger> positivePower(a, 1);
190        assertEquals("a^1 = a", b, a);
191        java.math.BigInteger x = java.math.BigInteger.ONE;
192        b = Power.<BigInteger> positivePower(a, x);
193        assertEquals("a^1 = a", b, a);
194
195        a = new BigInteger(11);
196        e = new BigInteger(2);
197        c = Power.<BigInteger> modPositivePower(a, 10, e);
198        assertTrue("3^n mod 2 = 1", c.isONE());
199
200        // little fermat
201        a = BigInteger.ZERO.random(500);
202        b = new BigInteger(32003);
203        c = Power.<BigInteger> modPositivePower(a, 32003, b);
204        d = a.remainder(b);
205        assertEquals("a^p = a mod p", c, d);
206
207        c = pow.modPower(a, 32003, b);
208        assertEquals("a^p = a mod p", c, d);
209
210        //Java 8:
211        a = BigInteger.ZERO.random(100);
212        d = a.power(1);
213        c = a;
214        assertEquals("a^1 == a", c, d);
215
216        d = a.power(0);
217        c = BigInteger.ONE;
218        assertEquals("a^0 == 1", c, d);
219
220        d = a.power(3);
221        c = a.multiply(a).multiply(a);
222        assertEquals("a^3 == a*a*a", c, d);
223    }
224
225
226    /**
227     * Test left/right division for Integer.
228     */
229    public void testIntegerLeftRight() {
230        BigInteger a, b, c, d, e, f, g;
231        a = BigInteger.ZERO.random(500);
232        b = BigInteger.ZERO.random(50);
233        //System.out.println("a = " + a);
234        //System.out.println("b = " + b);
235
236        c = a.divide(b);
237        d = a.leftDivide(b);
238        e = a.rightDivide(b);
239        assertEquals("a /_l b == a/b", c, d);
240        assertEquals("a /_r b == a/b", c, e);
241        f = c;
242
243        c = a.remainder(b);
244        d = a.leftRemainder(b);
245        e = a.rightRemainder(b);
246        assertEquals("a rem_l b == a rem b", c, d);
247        assertEquals("a rem_r b == a rem b", c, e);
248        e = c;
249
250        // no test since overridden in BigInteger:
251        BigInteger[] qr = a.quotientRemainder(b);
252        assertEquals("qr[0] == a/b", qr[0], f);
253        assertEquals("qr[1] == a rem b", qr[1], e);
254
255        MonoidElem[] lr = a.twosidedDivide(b);
256        assertEquals("lr[0] == a/b", lr[0], f);
257        assertTrue("lr[1] == 1", lr[1].isONE());
258
259        // wrong: todo
260        MonoidElem r = a.twosidedRemainder(b);
261        assertEquals("r == e", r, e);
262
263        c = a.gcd(b);
264        d = a.leftGcd(b);
265        e = a.rightGcd(b);
266        assertEquals("a ggt_l b == a ggt b", c, d);
267        assertEquals("a ggt_r b == a ggt b", c, e);
268    }
269
270}