1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package org.apache.harmony.tests.java.math;
19 
20 import java.math.BigInteger;
21 import java.util.Random;
22 
23 public class OldBigIntegerTest extends junit.framework.TestCase {
24 
25     BigInteger minusOne = new BigInteger("-1", 10);
26 
27     BigInteger two = new BigInteger("2", 10);
28 
29     BigInteger aZillion = new BigInteger("100000000000000000000000000000000000000000000000000", 10);
30 
31     Random rand = new Random();
32 
33     BigInteger bi;
34 
35     BigInteger bi2;
36 
37     BigInteger bi3;
38 
39     /**
40      * java.math.BigInteger#BigInteger(int, java.util.Random)
41      */
test_ConstructorILjava_util_Random()42     public void test_ConstructorILjava_util_Random() {
43         // regression test for HARMONY-1047
44         try {
45             new BigInteger(128, (Random) null);
46             fail();
47         } catch (NullPointerException expected) {
48         }
49 
50         bi = new BigInteger(70, rand);
51         bi2 = new BigInteger(70, rand);
52         assertTrue("Random number is negative", bi.compareTo(BigInteger.ZERO) >= 0);
53         assertTrue("Random number is too big", bi.compareTo(two.pow(70)) < 0);
54         assertTrue(
55                 "Two random numbers in a row are the same (might not be a bug but it very likely is)",
56                 !bi.equals(bi2));
57         assertTrue("Not zero", new BigInteger(0, rand).equals(BigInteger.ZERO));
58 
59         try {
60             new BigInteger(-1, (Random)null);
61             fail("IllegalArgumentException expected");
62         } catch (IllegalArgumentException e) {
63             // PASSED
64         }
65     }
66 
67     /**
68      * java.math.BigInteger#BigInteger(int, int, java.util.Random)
69      */
70     // BIGNUM returns no Primes smaller than 16 bits.
test_ConstructorIILjava_util_Random()71     public void test_ConstructorIILjava_util_Random() {
72         BigInteger bi1 = new BigInteger(10, 5, rand);
73         BigInteger bi2 = new BigInteger(10, 5, rand);
74         assertTrue(bi1 + " is negative", bi1.compareTo(BigInteger.ZERO) >= 0);
75         assertTrue(bi1 + " is too big", bi1.compareTo(new BigInteger("1024", 10)) < 0);
76         assertTrue(bi2 + " is negative", bi2.compareTo(BigInteger.ZERO) >= 0);
77         assertTrue(bi2 + " is too big", bi2.compareTo(new BigInteger("1024", 10)) < 0);
78 
79         Random rand = new Random();
80         BigInteger bi;
81         int certainty[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
82                 Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -2, -1 };
83         for (int i = 2; i <= 20; i++) {
84             for (int c = 0; c < certainty.length; c++) {
85                 bi = new BigInteger(i, c, rand); // Create BigInteger
86                 assertEquals(i, bi.bitLength());
87             }
88         }
89 
90         try {
91             new BigInteger(1, 80, (Random)null);
92             fail("ArithmeticException expected");
93         } catch (ArithmeticException expected) {
94         }
95 
96         try {
97             new BigInteger(-1, (Random)null);
98             fail("IllegalArgumentException expected");
99         } catch (IllegalArgumentException expected) {
100         }
101     }
102 
103 //    public void test_SpecialPrimes() {
104 //        System.out.println("test_SpecialPrimes");
105 //        final BigInteger TWO = BigInteger.valueOf(2);
106 //        BigInteger p, q;
107 //        for (;;) {
108 //            p = new BigInteger(1024, 23, new Random());
109 //            q = p.subtract(BigInteger.ONE).divide(TWO);
110 //            if (q.isProbablePrime(20)) {
111 //                System.out.println(q);
112 //                System.out.println(p);
113 //                break;
114 //            }
115 //            System.out.print(".");
116 //        }
117 //        fail("isProbablePrime failed for: " + bi);
118 //    }
119 
120     /**
121      * java.math.BigInteger#isProbablePrime(int)
122      */
test_isProbablePrimeI()123     public void test_isProbablePrimeI() {
124         int fails = 0;
125         bi = new BigInteger(20, 20, rand);
126         if (!bi.isProbablePrime(17)) {
127             fails++;
128         }
129         bi = new BigInteger("4", 10);
130         if (bi.isProbablePrime(17)) {
131             fail("isProbablePrime failed for: " + bi);
132         }
133         bi = BigInteger.valueOf(17L * 13L);
134         if (bi.isProbablePrime(17)) {
135             fail("isProbablePrime failed for: " + bi);
136         }
137         for (long a = 2; a < 1000; a++) {
138             if (isPrime(a)) {
139                 assertTrue("false negative on prime number <1000", BigInteger
140                         .valueOf(a).isProbablePrime(5));
141             } else if (BigInteger.valueOf(a).isProbablePrime(17)) {
142                 System.out.println("isProbablePrime failed for: " + a);
143                 fails++;
144             }
145         }
146         for (int a = 0; a < 1000; a++) {
147             bi = BigInteger.valueOf(rand.nextInt(1000000)).multiply(
148                     BigInteger.valueOf(rand.nextInt(1000000)));
149             if (bi.isProbablePrime(17)) {
150                 System.out.println("isProbablePrime failed for: " + bi);
151                 fails++;
152             }
153         }
154         for (int a = 0; a < 200; a++) {
155             bi = new BigInteger(70, rand).multiply(new BigInteger(70, rand));
156             if (bi.isProbablePrime(17)) {
157                 System.out.println("isProbablePrime failed for: " + bi);
158                 fails++;
159             }
160         }
161         assertTrue("Too many false positives - may indicate a problem",
162                 fails <= 1);
163 
164         //
165         // And now some tests on real big integers:
166         //
167         bi = new BigInteger("153890972191202256150310830154922163807316525358455215516067727076235016932726922093888770552128767458882963869421440585369743", 10);
168         if (!bi.isProbablePrime(80)) {
169             fail("isProbablePrime failed for: " + bi);
170         }
171         bi = new BigInteger("2090575416269141767246491983797422123741252476560371649798066134123893524014911825188890458270426076468664046568752890122415061377308817346303546688282957897504000216241497550243010257911214329646877810655164658470278901030511157372440751259674247310396158238588463284702737181653", 10);
172         if (!bi.isProbablePrime(80)) {
173             fail("isProbablePrime failed for: " + bi);
174         }
175         //
176         for (int bitLength = 100; bitLength <= 600; bitLength += 100) {
177             BigInteger a = BigInteger.probablePrime(bitLength, rand);
178             BigInteger b = BigInteger.probablePrime(bitLength, rand);
179             BigInteger c = a.multiply(b);
180             assertFalse("isProbablePrime failed for product of two large primes" +
181                             a + " * " + b + " = " + c +
182                             " (bitLength = " + bitLength + ")",
183                     c.isProbablePrime(80) );
184         }
185     }
186 
187     /**
188      * java.math.BigInteger#nextProbablePrime()
189      */
test_nextProbablePrime()190     public void test_nextProbablePrime() {
191         largePrimesProduct(
192                 new BigInteger("2537895984043447429238717358455377929009126353874925049325287329295635198252046158619999217453233889378619619008359011789"),
193                 new BigInteger("1711501451602688337873833423534849678524059393231999670806585630179374689152366029939952735718718709436427337762082614710093"),
194                 "4343612660706993434504106787562106084038357258130862545477481433639575850237346784798851102536616749334772541987502120552264920040629526028540204698334741815536099373917351194423681128374184971846099257056996626343051832131340568120612204287123"
195         );
196 
197         largePrimesProduct(
198                 new BigInteger("4617974730611208463200675282934641082129817404749925308887287017217158545765190433369842932770197341032031682222405074564586462802072184047198214312142847809259437477387527466762251087500170588962277514858557309036550499896961735701485020851"),
199                 new BigInteger("4313158964405728158057980867015758419530142215799386331265837224051830838583266274443105715022196238165196727467066901495701708590167750818040112544031694506528759169669442493029999154074962566165293254671176670719518898684698255068313216294333"),
200                 "19918059106734861363335842730108905466210762564765297409619920041621379008685530738918145604092111306972524565803236031571858280032420140331838737621152630780261815015157696362550138161774466814661069892975003440654998880587960037013294137372709096788892473385003457361736563927256562678181177287998121131179907762285048659075843995525830945659905573174849006768920618442371027575308854641789533211132313916836205357976988977849024687805212304038260207820679964201211309384057458137851"
201         );
202     }
203 
largePrimesProduct(BigInteger a, BigInteger b, String c)204     static void largePrimesProduct(BigInteger a, BigInteger b, String c) {
205         BigInteger wp = a.multiply(b);
206         assertFalse("isProbablePrime failed for product of two large primes" +
207                         a + " * " + b + " = " + c,
208                 wp.isProbablePrime(80) );
209         BigInteger wpMinusOne = wp.subtract(BigInteger.ONE);
210         BigInteger next = wpMinusOne.nextProbablePrime();
211 //        System.out.println(c);
212 //        System.out.println(next);
213         assertTrue("nextProbablePrime returns wrong number: " + next +
214                         "instead of expected: " + c,
215                 next.toString().equals(c) );
216     }
217 
218     /**
219      * java.math.BigInteger#probablePrime(int, java.util.Random)
220      */
test_probablePrime()221     public void test_probablePrime() {
222         for (int bitLength = 50; bitLength <= 1050; bitLength += 100) {
223             BigInteger a = BigInteger.probablePrime(bitLength, rand);
224             assertTrue("isProbablePrime(probablePrime()) failed for: " + bi,
225                     a.isProbablePrime(80));
226 //            System.out.println(a);
227 //            BigInteger prime = a.nextProbablePrime();
228 //            System.out.print("Next Probable Prime is ");
229 //            System.out.println(prime);
230         }
231     }
232 
233 // BEGIN Android-added
234 //    public void testModPowPerformance() {
235 //        Random rnd = new Random();
236 //        for (int i = 0; i < 10; i++) {
237 //            BigInteger a = new BigInteger(512, rnd);
238 //            BigInteger m = new BigInteger(1024, rnd);
239 //            BigInteger p = new BigInteger(256, rnd);
240 //            BigInteger mp = a.modPow(p, m);
241 //            System.out.println(mp);
242 //        }
243 //    }
244 
245 // shows factor 20 speed up (BIGNUM to Harmony Java):
246 //    public void testNextProbablePrime() {
247 //        Random rnd = new Random();
248 //        rnd.setSeed(0);
249 //        for (int i = 1; i <= 32; i += 1) {
250 //            BigInteger a = new BigInteger(i, rnd);
251 //            System.out.println(a);
252 //            BigInteger prime = a.nextProbablePrime();
253 //            System.out.print("Next Probable Prime is ");
254 //            System.out.println(prime);
255 //        }
256 //        for (int i = 1; i <= 32; i += 4) {
257 //            BigInteger a = new BigInteger(32 * i, rnd);
258 //            System.out.println(a);
259 //            BigInteger prime = a.nextProbablePrime();
260 //            System.out.print("Next Probable Prime is ");
261 //            System.out.println(prime);
262 //        }
263 //    }
264 
265 // shows factor 20 speed up (BIGNUM to Harmony Java):
266 // shows that certainty 80 is "practically aquivalent" to certainty 100
267 //    public void testPrimeGenPerformance() {
268 //        Random rnd = new Random();
269 //        rnd.setSeed(0);
270 //        for (int i = 1; i <= 32; i +=8 ) {
271 //            BigInteger a = new BigInteger(32 * i, 80, rnd);
272 //            System.out.println(a);
273 //            System.out.println("Now testing it again:");
274 //            if (a.isProbablePrime(100)) {
275 //                System.out.println("************************ PASSED! **************************");
276 //            } else {
277 //                System.out.println("************************ FAILED!!! **************************");
278 //                System.out.println("************************ FAILED!!! **************************");
279 //                System.out.println("************************ FAILED!!! **************************");
280 //                System.out.println("************************ FAILED!!! **************************");
281 //                System.out.println("************************ FAILED!!! **************************");
282 //                System.out.println("************************ FAILED!!! **************************");
283 //            }
284 //        }
285 //    }
286 // END Android-added
287 
288 
289 
290     /**
291      * java.math.BigInteger#add(java.math.BigInteger)
292      */
test_addLjava_math_BigInteger()293     public void test_addLjava_math_BigInteger() {
294         assertTrue("Incorrect sum--wanted a zillion", aZillion.add(aZillion)
295                 .add(aZillion.negate()).equals(aZillion));
296         assertTrue("0+0", BigInteger.ZERO.add(BigInteger.ZERO).equals(BigInteger.ZERO));
297         assertTrue("0+1", BigInteger.ZERO.add(BigInteger.ONE).equals(BigInteger.ONE));
298         assertTrue("1+0", BigInteger.ONE.add(BigInteger.ZERO).equals(BigInteger.ONE));
299         assertTrue("1+1", BigInteger.ONE.add(BigInteger.ONE).equals(two));
300         assertTrue("0+(-1)", BigInteger.ZERO.add(minusOne).equals(minusOne));
301         assertTrue("(-1)+0", minusOne.add(BigInteger.ZERO).equals(minusOne));
302         assertTrue("(-1)+(-1)", minusOne.add(minusOne).equals(new BigInteger("-2", 10)));
303         assertTrue("1+(-1)", BigInteger.ONE.add(minusOne).equals(BigInteger.ZERO));
304         assertTrue("(-1)+1", minusOne.add(BigInteger.ONE).equals(BigInteger.ZERO));
305 
306         for (int i = 0; i < 200; i++) {
307             BigInteger midbit = BigInteger.ZERO.setBit(i);
308             assertTrue("add fails to carry on bit " + i, midbit.add(midbit)
309                 .equals(BigInteger.ZERO.setBit(i + 1)));
310         }
311         BigInteger bi2p3 = bi2.add(bi3);
312         BigInteger bi3p2 = bi3.add(bi2);
313         assertTrue("bi2p3=bi3p2", bi2p3.equals(bi3p2));
314 
315 
316         // BESSER UEBERGREIFENDE TESTS MACHEN IN FORM VON STRESS TEST.
317         // add large positive + small positive
318         BigInteger sum = aZillion;
319         BigInteger increment = BigInteger.ONE;
320         for (int i = 0; i < 20; i++) {
321 
322         }
323 
324         // add large positive + small negative
325 
326         // add large negative + small positive
327 
328         // add large negative + small negative
329     }
330 
testClone()331     public void testClone() {
332         // Regression test for HARMONY-1770
333         MyBigInteger myBigInteger = new MyBigInteger("12345");
334         myBigInteger = (MyBigInteger) myBigInteger.clone();
335     }
336 
337     static class MyBigInteger extends BigInteger implements Cloneable {
MyBigInteger(String val)338         public MyBigInteger(String val) {
339             super(val);
340         }
clone()341         public Object clone() {
342             try {
343                 return super.clone();
344             } catch (CloneNotSupportedException e) {
345                 throw new AssertionError(e); // Android-changed
346             }
347         }
348     }
349 
350     @Override
setUp()351     protected void setUp() {
352         bi2 = new BigInteger("4576829475724387584378543764555", 16);
353         bi3 = new BigInteger("43987298363278574365732645872643587624387563245", 16);
354     }
355 
isPrime(long b)356     private boolean isPrime(long b) {
357         if (b == 2) {
358             return true;
359         }
360         // check for div by 2
361         if ((b & 1L) == 0) {
362             return false;
363         }
364         long maxlen = ((long) Math.sqrt(b)) + 2;
365         for (long x = 3; x < maxlen; x += 2) {
366             if (b % x == 0) {
367                 return false;
368             }
369         }
370         return true;
371     }
372 }
373