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
18package tests.api.java.math;
19
20import dalvik.annotation.KnownFailure;
21import dalvik.annotation.TestLevel;
22import dalvik.annotation.TestTargetClass;
23import dalvik.annotation.TestTargetNew;
24import dalvik.annotation.TestTargets;
25
26import java.math.BigInteger;
27import java.util.Random;
28
29@TestTargetClass(BigInteger.class)
30public class BigIntegerTest extends junit.framework.TestCase {
31
32    BigInteger minusTwo = new BigInteger("-2", 10);
33
34    BigInteger minusOne = new BigInteger("-1", 10);
35
36    BigInteger zero = new BigInteger("0", 10);
37
38    BigInteger one = new BigInteger("1", 10);
39
40    BigInteger two = new BigInteger("2", 10);
41
42    BigInteger ten = new BigInteger("10", 10);
43
44    BigInteger sixteen = new BigInteger("16", 10);
45
46    BigInteger oneThousand = new BigInteger("1000", 10);
47
48    BigInteger aZillion = new BigInteger(
49            "100000000000000000000000000000000000000000000000000", 10);
50
51    BigInteger twoToTheTen = new BigInteger("1024", 10);
52
53    BigInteger twoToTheSeventy = two.pow(70);
54
55    Random rand = new Random();
56
57    BigInteger bi;
58
59    BigInteger bi1;
60
61    BigInteger bi2;
62
63    BigInteger bi3;
64
65    BigInteger bi11;
66
67    BigInteger bi22;
68
69    BigInteger bi33;
70
71    BigInteger bi12;
72
73    BigInteger bi23;
74
75    BigInteger bi13;
76
77    BigInteger largePos;
78
79    BigInteger smallPos;
80
81    BigInteger largeNeg;
82
83    BigInteger smallNeg;
84
85    BigInteger[][] booleanPairs;
86
87    /**
88     * @tests java.math.BigInteger#BigInteger(int, java.util.Random)
89     */
90    @TestTargetNew(
91        level = TestLevel.COMPLETE,
92        notes = "",
93        method = "BigInteger",
94        args = {int.class, java.util.Random.class}
95    )
96    public void test_ConstructorILjava_util_Random() {
97        // regression test for HARMONY-1047
98        try {
99            new BigInteger(Integer.MAX_VALUE, (Random)null);
100            fail("NegativeArraySizeException expected");
101        } catch (NegativeArraySizeException e) {
102            // PASSED
103        }
104
105        bi = new BigInteger(70, rand);
106        bi2 = new BigInteger(70, rand);
107        assertTrue("Random number is negative", bi.compareTo(zero) >= 0);
108        assertTrue("Random number is too big",
109                bi.compareTo(twoToTheSeventy) < 0);
110        assertTrue(
111                "Two random numbers in a row are the same (might not be a bug but it very likely is)",
112                !bi.equals(bi2));
113        assertTrue("Not zero", new BigInteger(0, rand).equals(BigInteger.ZERO));
114
115        try {
116            new BigInteger(-1, (Random)null);
117            fail("IllegalArgumentException expected");
118        } catch (IllegalArgumentException e) {
119            // PASSED
120        }
121    }
122
123    /**
124     * @tests java.math.BigInteger#BigInteger(int, int, java.util.Random)
125     */
126    @TestTargetNew(
127        level = TestLevel.COMPLETE,
128        method = "BigInteger",
129        args = {int.class, int.class, java.util.Random.class}
130    )
131    @KnownFailure("BIGNUM returns no Primes smaller than 16 bits.")
132    public void test_ConstructorIILjava_util_Random() {
133        bi = new BigInteger(10, 5, rand);
134        bi2 = new BigInteger(10, 5, rand);
135        assertTrue("Random number one is negative", bi.compareTo(zero) >= 0);
136        assertTrue("Random number one is too big",
137                bi.compareTo(twoToTheTen) < 0);
138        assertTrue("Random number two is negative", bi2.compareTo(zero) >= 0);
139        assertTrue("Random number two is too big",
140                bi2.compareTo(twoToTheTen) < 0);
141
142        Random rand = new Random();
143        BigInteger bi;
144        int certainty[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
145                Integer.MIN_VALUE, Integer.MIN_VALUE + 1, -2, -1 };
146        for (int i = 2; i <= 20; i++) {
147            for (int c = 0; c < certainty.length; c++) {
148                bi = new BigInteger(i, c, rand); // Create BigInteger
149                assertTrue("Bit length incorrect", bi.bitLength() == i);
150            }
151        }
152
153        try {
154            new BigInteger(1, 80, (Random)null);
155            fail("ArithmeticException expected");
156        } catch (ArithmeticException e) {
157            // PASSED
158        }
159
160        try {
161            new BigInteger(-1, (Random)null);
162            fail("IllegalArgumentException expected");
163        } catch (IllegalArgumentException e) {
164            // PASSED
165        }
166    }
167
168    /**
169     * @tests java.math.BigInteger#BigInteger(byte[])
170     */
171    @TestTargetNew(
172        level = TestLevel.PARTIAL,
173        notes = "NumberFormatException checking missed",
174        method = "BigInteger",
175        args = {byte[].class}
176    )
177    public void test_Constructor$B() {
178        byte[] myByteArray;
179        myByteArray = new byte[] { (byte) 0x00, (byte) 0xFF, (byte) 0xFE };
180        bi = new BigInteger(myByteArray);
181        assertTrue("Incorrect value for pos number", bi.equals(BigInteger.ZERO
182                .setBit(16).subtract(two)));
183        myByteArray = new byte[] { (byte) 0xFF, (byte) 0xFE };
184        bi = new BigInteger(myByteArray);
185        assertTrue("Incorrect value for neg number", bi.equals(minusTwo));
186    }
187
188    /**
189     * @tests java.math.BigInteger#BigInteger(int, byte[])
190     */
191    @TestTargetNew(
192        level = TestLevel.COMPLETE,
193        notes = "",
194        method = "BigInteger",
195        args = {int.class, byte[].class}
196    )
197    public void test_ConstructorI$B() {
198        byte[] myByteArray;
199        myByteArray = new byte[] { (byte) 0xFF, (byte) 0xFE };
200        bi = new BigInteger(1, myByteArray);
201        assertTrue("Incorrect value for pos number", bi.equals(BigInteger.ZERO
202                .setBit(16).subtract(two)));
203        bi = new BigInteger(-1, myByteArray);
204        assertTrue("Incorrect value for neg number", bi.equals(BigInteger.ZERO
205                .setBit(16).subtract(two).negate()));
206        myByteArray = new byte[] { (byte) 0, (byte) 0 };
207        bi = new BigInteger(0, myByteArray);
208        assertTrue("Incorrect value for zero", bi.equals(zero));
209        myByteArray = new byte[] { (byte) 1 };
210        try {
211            new BigInteger(0, myByteArray);
212            fail("Failed to throw NumberFormatException");
213        } catch (NumberFormatException e) {
214            // correct
215        }
216    }
217
218    /**
219     * @tests java.math.BigInteger#BigInteger(java.lang.String)
220     */
221    @TestTargetNew(
222        level = TestLevel.PARTIAL,
223        notes = "Checks NumberFormatException",
224        method = "BigInteger",
225        args = {java.lang.String.class}
226    )
227    public void test_constructor_String_empty() {
228        try {
229            new BigInteger("");
230            fail("Expected NumberFormatException for new BigInteger(\"\")");
231        } catch (NumberFormatException e) {
232        }
233    }
234
235    /**
236     * @tests java.math.BigInteger#toByteArray()
237     */
238    @TestTargetNew(
239        level = TestLevel.COMPLETE,
240        notes = "",
241        method = "toByteArray",
242        args = {}
243    )
244    public void test_toByteArray() {
245        byte[] myByteArray, anotherByteArray;
246        myByteArray = new byte[] { 97, 33, 120, 124, 50, 2, 0, 0, 0, 12, 124,
247                42 };
248        anotherByteArray = new BigInteger(myByteArray).toByteArray();
249        assertTrue("Incorrect byte array returned",
250                myByteArray.length == anotherByteArray.length);
251        for (int counter = myByteArray.length - 1; counter >= 0; counter--) {
252            assertTrue("Incorrect values in returned byte array",
253                    myByteArray[counter] == anotherByteArray[counter]);
254        }
255    }
256
257//    public void test_SpecialPrimes() {
258//        System.out.println("test_SpecialPrimes");
259//        final BigInteger TWO = BigInteger.valueOf(2);
260//        BigInteger p, q;
261//        for (;;) {
262//            p = new BigInteger(1024, 23, new Random());
263//            q = p.subtract(BigInteger.ONE).divide(TWO);
264//            if (q.isProbablePrime(20)) {
265//                System.out.println(q);
266//                System.out.println(p);
267//                break;
268//            }
269//            System.out.print(".");
270//        }
271//        fail("isProbablePrime failed for: " + bi);
272//    }
273
274    /**
275     * @tests java.math.BigInteger#isProbablePrime(int)
276     */
277    @TestTargets({
278        @TestTargetNew(
279            level = TestLevel.COMPLETE,
280            notes = "",
281            method = "isProbablePrime",
282            args = {int.class}
283        ),
284        @TestTargetNew(
285            level = TestLevel.COMPLETE,
286            notes = "",
287            method = "probablePrime",
288            args = {int.class, java.util.Random.class}
289        )
290    })
291    public void test_isProbablePrimeI() {
292        int fails = 0;
293        bi = new BigInteger(20, 20, rand);
294        if (!bi.isProbablePrime(17)) {
295            fails++;
296        }
297        bi = new BigInteger("4", 10);
298        if (bi.isProbablePrime(17)) {
299            fail("isProbablePrime failed for: " + bi);
300        }
301        bi = BigInteger.valueOf(17L * 13L);
302        if (bi.isProbablePrime(17)) {
303            fail("isProbablePrime failed for: " + bi);
304        }
305        for (long a = 2; a < 1000; a++) {
306            if (isPrime(a)) {
307                assertTrue("false negative on prime number <1000", BigInteger
308                        .valueOf(a).isProbablePrime(5));
309            } else if (BigInteger.valueOf(a).isProbablePrime(17)) {
310                System.out.println("isProbablePrime failed for: " + a);
311                fails++;
312            }
313        }
314        for (int a = 0; a < 1000; a++) {
315            bi = BigInteger.valueOf(rand.nextInt(1000000)).multiply(
316                    BigInteger.valueOf(rand.nextInt(1000000)));
317            if (bi.isProbablePrime(17)) {
318                System.out.println("isProbablePrime failed for: " + bi);
319                fails++;
320            }
321        }
322        for (int a = 0; a < 200; a++) {
323            bi = new BigInteger(70, rand).multiply(new BigInteger(70, rand));
324            if (bi.isProbablePrime(17)) {
325                System.out.println("isProbablePrime failed for: " + bi);
326                fails++;
327            }
328        }
329        assertTrue("Too many false positives - may indicate a problem",
330                fails <= 1);
331
332        //
333        // And now some tests on real big integers:
334        //
335        bi = new BigInteger("153890972191202256150310830154922163807316525358455215516067727076235016932726922093888770552128767458882963869421440585369743", 10);
336        if (!bi.isProbablePrime(80)) {
337            fail("isProbablePrime failed for: " + bi);
338        }
339        bi = new BigInteger("2090575416269141767246491983797422123741252476560371649798066134123893524014911825188890458270426076468664046568752890122415061377308817346303546688282957897504000216241497550243010257911214329646877810655164658470278901030511157372440751259674247310396158238588463284702737181653", 10);
340        if (!bi.isProbablePrime(80)) {
341            fail("isProbablePrime failed for: " + bi);
342        }
343        //
344        for (int bitLength = 100; bitLength <= 600; bitLength += 100) {
345            BigInteger a = BigInteger.probablePrime(bitLength, rand);
346            BigInteger b = BigInteger.probablePrime(bitLength, rand);
347            BigInteger c = a.multiply(b);
348            assertFalse("isProbablePrime failed for product of two large primes" +
349                            a + " * " + b + " = " + c +
350                            " (bitLength = " + bitLength + ")",
351                    c.isProbablePrime(80) );
352        }
353    }
354
355    /**
356     * @tests java.math.BigInteger#nextProbablePrime()
357     */
358    @TestTargets({
359        @TestTargetNew(
360            level = TestLevel.COMPLETE,
361            notes = "",
362            method = "nextProbablePrime",
363            args = {}
364        ),
365        @TestTargetNew(
366            level = TestLevel.COMPLETE,
367            notes = "",
368            method = "isProbablePrime",
369            args = {int.class}
370        )
371    })
372    public void test_nextProbablePrime() {
373        largePrimesProduct(
374                new BigInteger("2537895984043447429238717358455377929009126353874925049325287329295635198252046158619999217453233889378619619008359011789"),
375                new BigInteger("1711501451602688337873833423534849678524059393231999670806585630179374689152366029939952735718718709436427337762082614710093"),
376                "4343612660706993434504106787562106084038357258130862545477481433639575850237346784798851102536616749334772541987502120552264920040629526028540204698334741815536099373917351194423681128374184971846099257056996626343051832131340568120612204287123"
377        );
378
379        largePrimesProduct(
380                new BigInteger("4617974730611208463200675282934641082129817404749925308887287017217158545765190433369842932770197341032031682222405074564586462802072184047198214312142847809259437477387527466762251087500170588962277514858557309036550499896961735701485020851"),
381                new BigInteger("4313158964405728158057980867015758419530142215799386331265837224051830838583266274443105715022196238165196727467066901495701708590167750818040112544031694506528759169669442493029999154074962566165293254671176670719518898684698255068313216294333"),
382                "19918059106734861363335842730108905466210762564765297409619920041621379008685530738918145604092111306972524565803236031571858280032420140331838737621152630780261815015157696362550138161774466814661069892975003440654998880587960037013294137372709096788892473385003457361736563927256562678181177287998121131179907762285048659075843995525830945659905573174849006768920618442371027575308854641789533211132313916836205357976988977849024687805212304038260207820679964201211309384057458137851"
383        );
384    }
385
386    static void largePrimesProduct(BigInteger a, BigInteger b, String c) {
387        BigInteger wp = a.multiply(b);
388        assertFalse("isProbablePrime failed for product of two large primes" +
389                        a + " * " + b + " = " + c,
390                wp.isProbablePrime(80) );
391        BigInteger wpMinusOne = wp.subtract(BigInteger.ONE);
392        BigInteger next = wpMinusOne.nextProbablePrime();
393//        System.out.println(c);
394//        System.out.println(next);
395        assertTrue("nextProbablePrime returns wrong number: " + next +
396                        "instead of expected: " + c,
397                next.toString().equals(c) );
398    }
399
400    /**
401     * @tests java.math.BigInteger#probablePrime(int, java.util.Random)
402     */
403    @TestTargetNew(
404        level = TestLevel.COMPLETE,
405        notes = "",
406        method = "probablePrime",
407        args = {int.class, java.util.Random.class}
408    )
409    public void test_probablePrime() {
410        for (int bitLength = 50; bitLength <= 1050; bitLength += 100) {
411            BigInteger a = BigInteger.probablePrime(bitLength, rand);
412            assertTrue("isProbablePrime(probablePrime()) failed for: " + bi,
413                    a.isProbablePrime(80));
414//            System.out.println(a);
415//            BigInteger prime = a.nextProbablePrime();
416//            System.out.print("Next Probable Prime is ");
417//            System.out.println(prime);
418        }
419    }
420
421// BEGIN android-added
422//    public void testModPowPerformance() {
423//        Random rnd = new Random();
424//        for (int i = 0; i < 10; i++) {
425//            BigInteger a = new BigInteger(512, rnd);
426//            BigInteger m = new BigInteger(1024, rnd);
427//            BigInteger p = new BigInteger(256, rnd);
428//            BigInteger mp = a.modPow(p, m);
429//            System.out.println(mp);
430//        }
431//    }
432
433// shows factor 20 speed up (BIGNUM to Harmony Java):
434//    public void testNextProbablePrime() {
435//        Random rnd = new Random();
436//        rnd.setSeed(0);
437//        for (int i = 1; i <= 32; i += 1) {
438//            BigInteger a = new BigInteger(i, rnd);
439//            System.out.println(a);
440//            BigInteger prime = a.nextProbablePrime();
441//            System.out.print("Next Probable Prime is ");
442//            System.out.println(prime);
443//        }
444//        for (int i = 1; i <= 32; i += 4) {
445//            BigInteger a = new BigInteger(32 * i, rnd);
446//            System.out.println(a);
447//            BigInteger prime = a.nextProbablePrime();
448//            System.out.print("Next Probable Prime is ");
449//            System.out.println(prime);
450//        }
451//    }
452
453// shows factor 20 speed up (BIGNUM to Harmony Java):
454// shows that certainty 80 is "practically aquivalent" to certainty 100
455//    public void testPrimeGenPerformance() {
456//        Random rnd = new Random();
457//        rnd.setSeed(0);
458//        for (int i = 1; i <= 32; i +=8 ) {
459//            BigInteger a = new BigInteger(32 * i, 80, rnd);
460//            System.out.println(a);
461//            System.out.println("Now testing it again:");
462//            if (a.isProbablePrime(100)) {
463//                System.out.println("************************ PASSED! **************************");
464//            } else {
465//                System.out.println("************************ FAILED!!! **************************");
466//                System.out.println("************************ FAILED!!! **************************");
467//                System.out.println("************************ FAILED!!! **************************");
468//                System.out.println("************************ FAILED!!! **************************");
469//                System.out.println("************************ FAILED!!! **************************");
470//                System.out.println("************************ FAILED!!! **************************");
471//            }
472//        }
473//    }
474// END android-added
475
476
477
478    /**
479     * @tests java.math.BigInteger#equals(java.lang.Object)
480     */
481    @TestTargetNew(
482        level = TestLevel.COMPLETE,
483        notes = "",
484        method = "equals",
485        args = {java.lang.Object.class}
486    )
487    public void test_equalsLjava_lang_Object() {
488        assertTrue("0=0", zero.equals(BigInteger.valueOf(0)));
489        assertTrue("-123=-123", BigInteger.valueOf(-123).equals(
490                BigInteger.valueOf(-123)));
491        assertTrue("0=1", !zero.equals(one));
492        assertTrue("0=-1", !zero.equals(minusOne));
493        assertTrue("1=-1", !one.equals(minusOne));
494        assertTrue("bi3=bi3", bi3.equals(bi3));
495        assertTrue("bi3=copy of bi3", bi3.equals(bi3.negate().negate()));
496        assertTrue("bi3=bi2", !bi3.equals(bi2));
497    }
498
499    /**
500     * @tests java.math.BigInteger#compareTo(java.math.BigInteger)
501     */
502    @TestTargetNew(
503        level = TestLevel.COMPLETE,
504        notes = "",
505        method = "compareTo",
506        args = {java.math.BigInteger.class}
507    )
508    public void test_compareToLjava_math_BigInteger() {
509        assertTrue("Smaller number returned >= 0", one.compareTo(two) < 0);
510        assertTrue("Larger number returned >= 0", two.compareTo(one) > 0);
511        assertTrue("Equal numbers did not return 0", one.compareTo(one) == 0);
512        assertTrue("Neg number messed things up",
513                two.negate().compareTo(one) < 0);
514    }
515
516    /**
517     * @tests java.math.BigInteger#intValue()
518     */
519    @TestTargetNew(
520        level = TestLevel.COMPLETE,
521        notes = "",
522        method = "intValue",
523        args = {}
524    )
525    public void test_intValue() {
526        assertTrue("Incorrect intValue for 2**70",
527                twoToTheSeventy.intValue() == 0);
528        assertTrue("Incorrect intValue for 2", two.intValue() == 2);
529    }
530
531    /**
532     * @tests java.math.BigInteger#longValue()
533     */
534    @TestTargetNew(
535        level = TestLevel.COMPLETE,
536        notes = "",
537        method = "longValue",
538        args = {}
539    )
540    public void test_longValue() {
541        assertTrue("Incorrect longValue for 2**70",
542                twoToTheSeventy.longValue() == 0);
543        assertTrue("Incorrect longValue for 2", two.longValue() == 2);
544    }
545
546    /**
547     * @tests java.math.BigInteger#valueOf(long)
548     */
549    @TestTargetNew(
550        level = TestLevel.COMPLETE,
551        notes = "",
552        method = "valueOf",
553        args = {long.class}
554    )
555    public void test_valueOfJ() {
556        assertTrue("Incurred number returned for 2", BigInteger.valueOf(2L)
557                .equals(two));
558        assertTrue("Incurred number returned for 200", BigInteger.valueOf(200L)
559                .equals(BigInteger.valueOf(139).add(BigInteger.valueOf(61))));
560    }
561
562    /**
563     * @tests java.math.BigInteger#add(java.math.BigInteger)
564     */
565    @TestTargetNew(
566        level = TestLevel.PARTIAL,
567        notes = "Test is OK, but some cases listed below can be reasonable.",
568        method = "add",
569        args = {java.math.BigInteger.class}
570    )
571    public void test_addLjava_math_BigInteger() {
572        assertTrue("Incorrect sum--wanted a zillion", aZillion.add(aZillion)
573                .add(aZillion.negate()).equals(aZillion));
574        assertTrue("0+0", zero.add(zero).equals(zero));
575        assertTrue("0+1", zero.add(one).equals(one));
576        assertTrue("1+0", one.add(zero).equals(one));
577        assertTrue("1+1", one.add(one).equals(two));
578        assertTrue("0+(-1)", zero.add(minusOne).equals(minusOne));
579        assertTrue("(-1)+0", minusOne.add(zero).equals(minusOne));
580        assertTrue("(-1)+(-1)", minusOne.add(minusOne).equals(minusTwo));
581        assertTrue("1+(-1)", one.add(minusOne).equals(zero));
582        assertTrue("(-1)+1", minusOne.add(one).equals(zero));
583
584        for (int i = 0; i < 200; i++) {
585            BigInteger midbit = zero.setBit(i);
586            assertTrue("add fails to carry on bit " + i, midbit.add(midbit)
587                    .equals(zero.setBit(i + 1)));
588        }
589        BigInteger bi2p3 = bi2.add(bi3);
590        BigInteger bi3p2 = bi3.add(bi2);
591        assertTrue("bi2p3=bi3p2", bi2p3.equals(bi3p2));
592
593
594        // BESSER UEBERGREIFENDE TESTS MACHEN IN FORM VON STRESS TEST.
595        // add large positive + small positive
596        BigInteger sum = aZillion;
597        BigInteger increment = one;
598        for (int i = 0; i < 20; i++) {
599
600        }
601
602        // add large positive + small negative
603
604        // add large negative + small positive
605
606        // add large negative + small negative
607    }
608
609    /**
610     * @tests java.math.BigInteger#negate()
611     */
612    @TestTargetNew(
613        level = TestLevel.COMPLETE,
614        notes = "",
615        method = "negate",
616        args = {}
617    )
618    public void test_negate() {
619        assertTrue("Single negation of zero did not result in zero", zero
620                .negate().equals(zero));
621        assertTrue("Single negation resulted in original nonzero number",
622                !aZillion.negate().equals(aZillion));
623        assertTrue("Double negation did not result in original number",
624                aZillion.negate().negate().equals(aZillion));
625
626        assertTrue("0.neg", zero.negate().equals(zero));
627        assertTrue("1.neg", one.negate().equals(minusOne));
628        assertTrue("2.neg", two.negate().equals(minusTwo));
629        assertTrue("-1.neg", minusOne.negate().equals(one));
630        assertTrue("-2.neg", minusTwo.negate().equals(two));
631        assertTrue("0x62EB40FEF85AA9EBL*2.neg", BigInteger.valueOf(
632                0x62EB40FEF85AA9EBL * 2).negate().equals(
633                BigInteger.valueOf(-0x62EB40FEF85AA9EBL * 2)));
634        for (int i = 0; i < 200; i++) {
635            BigInteger midbit = zero.setBit(i);
636            BigInteger negate = midbit.negate();
637            assertTrue("negate negate", negate.negate().equals(midbit));
638            assertTrue("neg fails on bit " + i, midbit.negate().add(midbit)
639                    .equals(zero));
640        }
641    }
642
643    /**
644     * @tests java.math.BigInteger#signum()
645     */
646    @TestTargetNew(
647        level = TestLevel.COMPLETE,
648        notes = "",
649        method = "signum",
650        args = {}
651    )
652    public void test_signum() {
653        assertTrue("Wrong positive signum", two.signum() == 1);
654        assertTrue("Wrong zero signum", zero.signum() == 0);
655        assertTrue("Wrong neg zero signum", zero.negate().signum() == 0);
656        assertTrue("Wrong neg signum", two.negate().signum() == -1);
657    }
658
659    /**
660     * @tests java.math.BigInteger#abs()
661     */
662    @TestTargetNew(
663        level = TestLevel.COMPLETE,
664        notes = "",
665        method = "abs",
666        args = {}
667    )
668    public void test_abs() {
669        assertTrue("Invalid number returned for zillion", aZillion.negate()
670                .abs().equals(aZillion.abs()));
671        assertTrue("Invalid number returned for zero neg", zero.negate().abs()
672                .equals(zero));
673        assertTrue("Invalid number returned for zero", zero.abs().equals(zero));
674        assertTrue("Invalid number returned for two", two.negate().abs()
675                .equals(two));
676    }
677
678    /**
679     * @tests java.math.BigInteger#pow(int)
680     */
681    @TestTargetNew(
682        level = TestLevel.PARTIAL,
683        notes = "ArithmeticException checking missed",
684        method = "pow",
685        args = {int.class}
686    )
687    public void test_powI() {
688        assertTrue("Incorrect exponent returned for 2**10", two.pow(10).equals(
689                twoToTheTen));
690        assertTrue("Incorrect exponent returned for 2**70", two.pow(30)
691                .multiply(two.pow(40)).equals(twoToTheSeventy));
692        assertTrue("Incorrect exponent returned for 10**50", ten.pow(50)
693                .equals(aZillion));
694    }
695
696    /**
697     * @tests java.math.BigInteger#modInverse(java.math.BigInteger)
698     */
699    @TestTargetNew(
700        level = TestLevel.COMPLETE,
701        notes = "",
702        method = "modInverse",
703        args = {java.math.BigInteger.class}
704    )
705    public void test_modInverseLjava_math_BigInteger() {
706        BigInteger a = zero, mod, inv;
707        for (int j = 3; j < 50; j++) {
708            mod = BigInteger.valueOf(j);
709            for (int i = -j + 1; i < j; i++) {
710                try {
711                    a = BigInteger.valueOf(i);
712                    inv = a.modInverse(mod);
713                    assertTrue("bad inverse: " + a + " inv mod " + mod
714                            + " equals " + inv, one.equals(a.multiply(inv).mod(
715                            mod)));
716                    assertTrue("inverse greater than modulo: " + a
717                            + " inv mod " + mod + " equals " + inv, inv
718                            .compareTo(mod) < 0);
719                    assertTrue("inverse less than zero: " + a + " inv mod "
720                            + mod + " equals " + inv, inv
721                            .compareTo(BigInteger.ZERO) >= 0);
722                } catch (ArithmeticException e) {
723                    assertTrue("should have found inverse for " + a + " mod "
724                            + mod, !one.equals(a.gcd(mod)));
725                }
726            }
727        }
728        for (int j = 1; j < 10; j++) {
729            mod = bi2.add(BigInteger.valueOf(j));
730            for (int i = 0; i < 20; i++) {
731                try {
732                    a = bi3.add(BigInteger.valueOf(i));
733                    inv = a.modInverse(mod);
734                    assertTrue("bad inverse: " + a + " inv mod " + mod
735                            + " equals " + inv, one.equals(a.multiply(inv).mod(
736                            mod)));
737                    assertTrue("inverse greater than modulo: " + a
738                            + " inv mod " + mod + " equals " + inv, inv
739                            .compareTo(mod) < 0);
740                    assertTrue("inverse less than zero: " + a + " inv mod "
741                            + mod + " equals " + inv, inv
742                            .compareTo(BigInteger.ZERO) >= 0);
743                } catch (ArithmeticException e) {
744                    assertTrue("should have found inverse for " + a + " mod "
745                            + mod, !one.equals(a.gcd(mod)));
746                }
747            }
748        }
749    }
750
751    /**
752     * @tests java.math.BigInteger#shiftRight(int)
753     */
754    @TestTargetNew(
755        level = TestLevel.COMPLETE,
756        notes = "",
757        method = "shiftRight",
758        args = {int.class}
759    )
760    public void test_shiftRightI() {
761        assertTrue("1 >> 0", BigInteger.valueOf(1).shiftRight(0).equals(
762                BigInteger.ONE));
763        assertTrue("1 >> 1", BigInteger.valueOf(1).shiftRight(1).equals(
764                BigInteger.ZERO));
765        assertTrue("1 >> 63", BigInteger.valueOf(1).shiftRight(63).equals(
766                BigInteger.ZERO));
767        assertTrue("1 >> 64", BigInteger.valueOf(1).shiftRight(64).equals(
768                BigInteger.ZERO));
769        assertTrue("1 >> 65", BigInteger.valueOf(1).shiftRight(65).equals(
770                BigInteger.ZERO));
771        assertTrue("1 >> 1000", BigInteger.valueOf(1).shiftRight(1000).equals(
772                BigInteger.ZERO));
773        assertTrue("-1 >> 0", BigInteger.valueOf(-1).shiftRight(0).equals(
774                minusOne));
775        assertTrue("-1 >> 1", BigInteger.valueOf(-1).shiftRight(1).equals(
776                minusOne));
777        assertTrue("-1 >> 63", BigInteger.valueOf(-1).shiftRight(63).equals(
778                minusOne));
779        assertTrue("-1 >> 64", BigInteger.valueOf(-1).shiftRight(64).equals(
780                minusOne));
781        assertTrue("-1 >> 65", BigInteger.valueOf(-1).shiftRight(65).equals(
782                minusOne));
783        assertTrue("-1 >> 1000", BigInteger.valueOf(-1).shiftRight(1000)
784                .equals(minusOne));
785
786        BigInteger a = BigInteger.ONE;
787        BigInteger c = bi3;
788        BigInteger E = bi3.negate();
789        BigInteger e = E;
790        for (int i = 0; i < 200; i++) {
791            BigInteger b = BigInteger.ZERO.setBit(i);
792            assertTrue("a==b", a.equals(b));
793            a = a.shiftLeft(1);
794            assertTrue("a non-neg", a.signum() >= 0);
795
796            BigInteger d = bi3.shiftRight(i);
797            assertTrue("c==d", c.equals(d));
798            c = c.shiftRight(1);
799            assertTrue(">>1 == /2", d.divide(two).equals(c));
800            assertTrue("c non-neg", c.signum() >= 0);
801
802            BigInteger f = E.shiftRight(i);
803            assertTrue("e==f", e.equals(f));
804            e = e.shiftRight(1);
805            assertTrue(">>1 == /2", f.subtract(one).divide(two).equals(e));
806            assertTrue("e negative", e.signum() == -1);
807
808            assertTrue("b >> i", b.shiftRight(i).equals(one));
809            assertTrue("b >> i+1", b.shiftRight(i + 1).equals(zero));
810            assertTrue("b >> i-1", b.shiftRight(i - 1).equals(two));
811        }
812    }
813
814    /**
815     * @tests java.math.BigInteger#shiftLeft(int)
816     */
817    @TestTargetNew(
818        level = TestLevel.COMPLETE,
819        notes = "",
820        method = "shiftLeft",
821        args = {int.class}
822    )
823    public void test_shiftLeftI() {
824        assertTrue("1 << 0", one.shiftLeft(0).equals(one));
825        assertTrue("1 << 1", one.shiftLeft(1).equals(two));
826        assertTrue("1 << 63", one.shiftLeft(63).equals(
827                new BigInteger("8000000000000000", 16)));
828        assertTrue("1 << 64", one.shiftLeft(64).equals(
829                new BigInteger("10000000000000000", 16)));
830        assertTrue("1 << 65", one.shiftLeft(65).equals(
831                new BigInteger("20000000000000000", 16)));
832        assertTrue("-1 << 0", minusOne.shiftLeft(0).equals(minusOne));
833        assertTrue("-1 << 1", minusOne.shiftLeft(1).equals(minusTwo));
834        assertTrue("-1 << 63", minusOne.shiftLeft(63).equals(
835                new BigInteger("-9223372036854775808")));
836        assertTrue("-1 << 64", minusOne.shiftLeft(64).equals(
837                new BigInteger("-18446744073709551616")));
838        assertTrue("-1 << 65", minusOne.shiftLeft(65).equals(
839                new BigInteger("-36893488147419103232")));
840
841        BigInteger a = bi3;
842        BigInteger c = minusOne;
843        for (int i = 0; i < 200; i++) {
844            BigInteger b = bi3.shiftLeft(i);
845            assertTrue("a==b", a.equals(b));
846            assertTrue("a >> i == bi3", a.shiftRight(i).equals(bi3));
847            a = a.shiftLeft(1);
848            assertTrue("<<1 == *2", b.multiply(two).equals(a));
849            assertTrue("a non-neg", a.signum() >= 0);
850            assertTrue("a.bitCount==b.bitCount", a.bitCount() == b.bitCount());
851
852            BigInteger d = minusOne.shiftLeft(i);
853            assertTrue("c==d", c.equals(d));
854            c = c.shiftLeft(1);
855            assertTrue("<<1 == *2 negative", d.multiply(two).equals(c));
856            assertTrue("c negative", c.signum() == -1);
857            assertTrue("d >> i == minusOne", d.shiftRight(i).equals(minusOne));
858        }
859    }
860
861    /**
862     * @tests java.math.BigInteger#multiply(java.math.BigInteger)
863     */
864    @TestTargetNew(
865        level = TestLevel.COMPLETE,
866        notes = "",
867        method = "multiply",
868        args = {java.math.BigInteger.class}
869    )
870    public void test_multiplyLjava_math_BigInteger() {
871        assertTrue("Incorrect sum--wanted three zillion", aZillion
872                .add(aZillion).add(aZillion).equals(
873                        aZillion.multiply(new BigInteger("3", 10))));
874
875        assertTrue("0*0", zero.multiply(zero).equals(zero));
876        assertTrue("0*1", zero.multiply(one).equals(zero));
877        assertTrue("1*0", one.multiply(zero).equals(zero));
878        assertTrue("1*1", one.multiply(one).equals(one));
879        assertTrue("0*(-1)", zero.multiply(minusOne).equals(zero));
880        assertTrue("(-1)*0", minusOne.multiply(zero).equals(zero));
881        assertTrue("(-1)*(-1)", minusOne.multiply(minusOne).equals(one));
882        assertTrue("1*(-1)", one.multiply(minusOne).equals(minusOne));
883        assertTrue("(-1)*1", minusOne.multiply(one).equals(minusOne));
884
885        testAllMults(bi1, bi1, bi11);
886        testAllMults(bi2, bi2, bi22);
887        testAllMults(bi3, bi3, bi33);
888        testAllMults(bi1, bi2, bi12);
889        testAllMults(bi1, bi3, bi13);
890        testAllMults(bi2, bi3, bi23);
891    }
892
893    /**
894     * @tests java.math.BigInteger#divide(java.math.BigInteger)
895     */
896    @TestTargetNew(
897        level = TestLevel.COMPLETE,
898        notes = "",
899        method = "divide",
900        args = {java.math.BigInteger.class}
901    )
902    public void test_divideLjava_math_BigInteger() {
903        testAllDivs(bi33, bi3);
904        testAllDivs(bi22, bi2);
905        testAllDivs(bi11, bi1);
906        testAllDivs(bi13, bi1);
907        testAllDivs(bi13, bi3);
908        testAllDivs(bi12, bi1);
909        testAllDivs(bi12, bi2);
910        testAllDivs(bi23, bi2);
911        testAllDivs(bi23, bi3);
912        testAllDivs(largePos, bi1);
913        testAllDivs(largePos, bi2);
914        testAllDivs(largePos, bi3);
915        testAllDivs(largeNeg, bi1);
916        testAllDivs(largeNeg, bi2);
917        testAllDivs(largeNeg, bi3);
918        testAllDivs(largeNeg, largePos);
919        testAllDivs(largePos, largeNeg);
920        testAllDivs(bi3, bi3);
921        testAllDivs(bi2, bi2);
922        testAllDivs(bi1, bi1);
923        testDivRanges(bi1);
924        testDivRanges(bi2);
925        testDivRanges(bi3);
926        testDivRanges(smallPos);
927        testDivRanges(largePos);
928        testDivRanges(new BigInteger("62EB40FEF85AA9EB", 16));
929        testAllDivs(BigInteger.valueOf(0xCC0225953CL), BigInteger
930                .valueOf(0x1B937B765L));
931
932        try {
933            largePos.divide(zero);
934            fail("ArithmeticException expected");
935        } catch (ArithmeticException e) {
936        }
937
938        try {
939            bi1.divide(zero);
940            fail("ArithmeticException expected");
941        } catch (ArithmeticException e) {
942        }
943
944        try {
945            bi3.negate().divide(zero);
946            fail("ArithmeticException expected");
947        } catch (ArithmeticException e) {
948        }
949
950        try {
951            zero.divide(zero);
952            fail("ArithmeticException expected");
953        } catch (ArithmeticException e) {
954        }
955    }
956
957    /**
958     * @tests java.math.BigInteger#remainder(java.math.BigInteger)
959     */
960    @TestTargetNew(
961        level = TestLevel.PARTIAL,
962        notes = "ArithmeticException checked",
963        method = "remainder",
964        args = {java.math.BigInteger.class}
965    )
966    public void test_remainderLjava_math_BigInteger() {
967        try {
968            largePos.remainder(zero);
969            fail("ArithmeticException expected");
970        } catch (ArithmeticException e) {
971        }
972
973        try {
974            bi1.remainder(zero);
975            fail("ArithmeticException expected");
976        } catch (ArithmeticException e) {
977        }
978
979        try {
980            bi3.negate().remainder(zero);
981            fail("ArithmeticException expected");
982        } catch (ArithmeticException e) {
983        }
984
985        try {
986            zero.remainder(zero);
987            fail("ArithmeticException expected");
988        } catch (ArithmeticException e) {
989        }
990    }
991
992    /**
993     * @tests java.math.BigInteger#mod(java.math.BigInteger)
994     */
995    @TestTargetNew(
996        level = TestLevel.PARTIAL,
997        notes = "ArithmeticException checked",
998        method = "mod",
999        args = {java.math.BigInteger.class}
1000    )
1001    public void test_modLjava_math_BigInteger() {
1002        try {
1003            largePos.mod(zero);
1004            fail("ArithmeticException expected");
1005        } catch (ArithmeticException e) {
1006        }
1007
1008        try {
1009            bi1.mod(zero);
1010            fail("ArithmeticException expected");
1011        } catch (ArithmeticException e) {
1012        }
1013
1014        try {
1015            bi3.negate().mod(zero);
1016            fail("ArithmeticException expected");
1017        } catch (ArithmeticException e) {
1018        }
1019
1020        try {
1021            zero.mod(zero);
1022            fail("ArithmeticException expected");
1023        } catch (ArithmeticException e) {
1024        }
1025    }
1026
1027    /**
1028     * @tests java.math.BigInteger#divideAndRemainder(java.math.BigInteger)
1029     */
1030    @TestTargetNew(
1031        level = TestLevel.PARTIAL,
1032        notes = "ArithmeticException checked",
1033        method = "divideAndRemainder",
1034        args = {java.math.BigInteger.class}
1035    )
1036    public void test_divideAndRemainderLjava_math_BigInteger() {
1037        try {
1038            largePos.divideAndRemainder(zero);
1039            fail("ArithmeticException expected");
1040        } catch (ArithmeticException e) {
1041        }
1042
1043        try {
1044            bi1.divideAndRemainder(zero);
1045            fail("ArithmeticException expected");
1046        } catch (ArithmeticException e) {
1047        }
1048
1049        try {
1050            bi3.negate().divideAndRemainder(zero);
1051            fail("ArithmeticException expected");
1052        } catch (ArithmeticException e) {
1053        }
1054
1055        try {
1056            zero.divideAndRemainder(zero);
1057            fail("ArithmeticException expected");
1058        } catch (ArithmeticException e) {
1059        }
1060    }
1061
1062    /**
1063     * @tests java.math.BigInteger#BigInteger(java.lang.String)
1064     */
1065    @TestTargetNew(
1066        level = TestLevel.PARTIAL,
1067        notes = "NumberFormatException checking missed.",
1068        method = "BigInteger",
1069        args = {java.lang.String.class}
1070    )
1071    public void test_ConstructorLjava_lang_String() {
1072        assertTrue("new(0)", new BigInteger("0").equals(BigInteger.valueOf(0)));
1073        assertTrue("new(1)", new BigInteger("1").equals(BigInteger.valueOf(1)));
1074        assertTrue("new(12345678901234)", new BigInteger("12345678901234")
1075                .equals(BigInteger.valueOf(12345678901234L)));
1076        assertTrue("new(-1)", new BigInteger("-1").equals(BigInteger
1077                .valueOf(-1)));
1078        assertTrue("new(-12345678901234)", new BigInteger("-12345678901234")
1079                .equals(BigInteger.valueOf(-12345678901234L)));
1080    }
1081
1082    /**
1083     * @tests java.math.BigInteger#BigInteger(java.lang.String, int)
1084     */
1085    @TestTargetNew(
1086        level = TestLevel.PARTIAL,
1087        notes = "NumberFormatException checking missed.",
1088        method = "BigInteger",
1089        args = {java.lang.String.class, int.class}
1090    )
1091    public void test_ConstructorLjava_lang_StringI() {
1092        assertTrue("new(0,16)", new BigInteger("0", 16).equals(BigInteger
1093                .valueOf(0)));
1094        assertTrue("new(1,16)", new BigInteger("1", 16).equals(BigInteger
1095                .valueOf(1)));
1096        assertTrue("new(ABF345678901234,16)", new BigInteger("ABF345678901234",
1097                16).equals(BigInteger.valueOf(0xABF345678901234L)));
1098        assertTrue("new(abf345678901234,16)", new BigInteger("abf345678901234",
1099                16).equals(BigInteger.valueOf(0xABF345678901234L)));
1100        assertTrue("new(-1,16)", new BigInteger("-1", 16).equals(BigInteger
1101                .valueOf(-1)));
1102        assertTrue("new(-ABF345678901234,16)", new BigInteger(
1103                "-ABF345678901234", 16).equals(BigInteger
1104                .valueOf(-0xABF345678901234L)));
1105        assertTrue("new(-abf345678901234,16)", new BigInteger(
1106                "-abf345678901234", 16).equals(BigInteger
1107                .valueOf(-0xABF345678901234L)));
1108        assertTrue("new(-101010101,2)", new BigInteger("-101010101", 2)
1109                .equals(BigInteger.valueOf(-341)));
1110    }
1111
1112    /**
1113     * @tests java.math.BigInteger#toString()
1114     */
1115    @TestTargetNew(
1116        level = TestLevel.COMPLETE,
1117        notes = "",
1118        method = "toString",
1119        args = {}
1120    )
1121    public void test_toString() {
1122        assertTrue("0.toString", "0".equals(BigInteger.valueOf(0).toString()));
1123        assertTrue("1.toString", "1".equals(BigInteger.valueOf(1).toString()));
1124        assertTrue("12345678901234.toString", "12345678901234"
1125                .equals(BigInteger.valueOf(12345678901234L).toString()));
1126        assertTrue("-1.toString", "-1"
1127                .equals(BigInteger.valueOf(-1).toString()));
1128        assertTrue("-12345678901234.toString", "-12345678901234"
1129                .equals(BigInteger.valueOf(-12345678901234L).toString()));
1130    }
1131
1132    /**
1133     * @tests java.math.BigInteger#toString(int)
1134     */
1135    @TestTargetNew(
1136        level = TestLevel.COMPLETE,
1137        notes = "",
1138        method = "toString",
1139        args = {int.class}
1140    )
1141    public void test_toStringI() {
1142        assertTrue("0.toString(16)", "0".equals(BigInteger.valueOf(0).toString(
1143                16)));
1144        assertTrue("1.toString(16)", "1".equals(BigInteger.valueOf(1).toString(
1145                16)));
1146        assertTrue("ABF345678901234.toString(16)", "abf345678901234"
1147                .equals(BigInteger.valueOf(0xABF345678901234L).toString(16)));
1148        assertTrue("-1.toString(16)", "-1".equals(BigInteger.valueOf(-1)
1149                .toString(16)));
1150        assertTrue("-ABF345678901234.toString(16)", "-abf345678901234"
1151                .equals(BigInteger.valueOf(-0xABF345678901234L).toString(16)));
1152        assertTrue("-101010101.toString(2)", "-101010101".equals(BigInteger
1153                .valueOf(-341).toString(2)));
1154    }
1155
1156    /**
1157     * @tests java.math.BigInteger#and(java.math.BigInteger)
1158     */
1159    @TestTargetNew(
1160        level = TestLevel.COMPLETE,
1161        notes = "",
1162        method = "and",
1163        args = {java.math.BigInteger.class}
1164    )
1165    public void test_andLjava_math_BigInteger() {
1166        for (BigInteger[] element : booleanPairs) {
1167            BigInteger i1 = element[0], i2 = element[1];
1168            BigInteger res = i1.and(i2);
1169            assertTrue("symmetry of and", res.equals(i2.and(i1)));
1170            int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
1171            for (int i = 0; i < len; i++) {
1172                assertTrue("and", (i1.testBit(i) && i2.testBit(i)) == res
1173                        .testBit(i));
1174            }
1175        }
1176    }
1177
1178    /**
1179     * @tests java.math.BigInteger#or(java.math.BigInteger)
1180     */
1181    @TestTargetNew(
1182        level = TestLevel.COMPLETE,
1183        notes = "",
1184        method = "or",
1185        args = {java.math.BigInteger.class}
1186    )
1187    public void test_orLjava_math_BigInteger() {
1188        for (BigInteger[] element : booleanPairs) {
1189            BigInteger i1 = element[0], i2 = element[1];
1190            BigInteger res = i1.or(i2);
1191            assertTrue("symmetry of or", res.equals(i2.or(i1)));
1192            int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
1193            for (int i = 0; i < len; i++) {
1194                assertTrue("or", (i1.testBit(i) || i2.testBit(i)) == res
1195                        .testBit(i));
1196            }
1197        }
1198    }
1199
1200    /**
1201     * @tests java.math.BigInteger#xor(java.math.BigInteger)
1202     */
1203    @TestTargetNew(
1204        level = TestLevel.COMPLETE,
1205        notes = "",
1206        method = "xor",
1207        args = {java.math.BigInteger.class}
1208    )
1209    public void test_xorLjava_math_BigInteger() {
1210        for (BigInteger[] element : booleanPairs) {
1211            BigInteger i1 = element[0], i2 = element[1];
1212            BigInteger res = i1.xor(i2);
1213            assertTrue("symmetry of xor", res.equals(i2.xor(i1)));
1214            int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
1215            for (int i = 0; i < len; i++) {
1216                assertTrue("xor", (i1.testBit(i) ^ i2.testBit(i)) == res
1217                        .testBit(i));
1218            }
1219        }
1220    }
1221
1222    /**
1223     * @tests java.math.BigInteger#not()
1224     */
1225    @TestTargetNew(
1226        level = TestLevel.COMPLETE,
1227        notes = "",
1228        method = "not",
1229        args = {}
1230    )
1231    public void test_not() {
1232        for (BigInteger[] element : booleanPairs) {
1233            BigInteger i1 = element[0];
1234            BigInteger res = i1.not();
1235            int len = i1.bitLength() + 66;
1236            for (int i = 0; i < len; i++) {
1237                assertTrue("not", !i1.testBit(i) == res.testBit(i));
1238            }
1239        }
1240    }
1241
1242    /**
1243     * @tests java.math.BigInteger#andNot(java.math.BigInteger)
1244     */
1245    @TestTargetNew(
1246        level = TestLevel.COMPLETE,
1247        notes = "",
1248        method = "andNot",
1249        args = {java.math.BigInteger.class}
1250    )
1251    public void test_andNotLjava_math_BigInteger() {
1252        for (BigInteger[] element : booleanPairs) {
1253            BigInteger i1 = element[0], i2 = element[1];
1254            BigInteger res = i1.andNot(i2);
1255            int len = Math.max(i1.bitLength(), i2.bitLength()) + 66;
1256            for (int i = 0; i < len; i++) {
1257                assertTrue("andNot", (i1.testBit(i) && !i2.testBit(i)) == res
1258                        .testBit(i));
1259            }
1260            // asymmetrical
1261            i1 = element[1];
1262            i2 = element[0];
1263            res = i1.andNot(i2);
1264            for (int i = 0; i < len; i++) {
1265                assertTrue("andNot reversed",
1266                        (i1.testBit(i) && !i2.testBit(i)) == res.testBit(i));
1267            }
1268        }
1269
1270        //regression for HARMONY-4653
1271        try{
1272            BigInteger.ZERO.andNot(null);
1273            fail("should throw NPE");
1274        }catch(Exception e){
1275            //expected
1276        }
1277        BigInteger bi = new BigInteger(0, new byte[]{});
1278        assertEquals(BigInteger.ZERO, bi.andNot(BigInteger.ZERO));
1279    }
1280
1281
1282    @TestTargetNew(
1283        level = TestLevel.PARTIAL,
1284        notes = "Regression test",
1285        method = "clone",
1286        args = {}
1287    )
1288    public void testClone() {
1289        // Regression test for HARMONY-1770
1290        MyBigInteger myBigInteger = new MyBigInteger("12345");
1291        myBigInteger = (MyBigInteger) myBigInteger.clone();
1292    }
1293
1294    static class MyBigInteger extends BigInteger implements Cloneable {
1295        public MyBigInteger(String val) {
1296            super(val);
1297        }
1298        public Object clone() {
1299            try {
1300                return super.clone();
1301            } catch (CloneNotSupportedException e) {
1302                throw new AssertionError(e); // android-changed
1303            }
1304        }
1305    }
1306
1307    @Override
1308    protected void setUp() {
1309        bi1 = new BigInteger("2436798324768978", 16);
1310        bi2 = new BigInteger("4576829475724387584378543764555", 16);
1311        bi3 = new BigInteger("43987298363278574365732645872643587624387563245",
1312                16);
1313
1314        bi33 = new BigInteger(
1315                "10730846694701319120609898625733976090865327544790136667944805934175543888691400559249041094474885347922769807001",
1316                10);
1317        bi22 = new BigInteger(
1318                "33301606932171509517158059487795669025817912852219962782230629632224456249",
1319                10);
1320        bi11 = new BigInteger("6809003003832961306048761258711296064", 10);
1321        bi23 = new BigInteger(
1322                "597791300268191573513888045771594235932809890963138840086083595706565695943160293610527214057",
1323                10);
1324        bi13 = new BigInteger(
1325                "270307912162948508387666703213038600031041043966215279482940731158968434008",
1326                10);
1327        bi12 = new BigInteger(
1328                "15058244971895641717453176477697767050482947161656458456", 10);
1329
1330        largePos = new BigInteger(
1331                "834759814379857314986743298675687569845986736578576375675678998612743867438632986243982098437620983476924376",
1332                16);
1333        smallPos = new BigInteger("48753269875973284765874598630960986276", 16);
1334        largeNeg = new BigInteger(
1335                "-878824397432651481891353247987891423768534321387864361143548364457698487264387568743568743265873246576467643756437657436587436",
1336                16);
1337        smallNeg = new BigInteger("-567863254343798609857456273458769843", 16);
1338        booleanPairs = new BigInteger[][] { { largePos, smallPos },
1339                { largePos, smallNeg }, { largeNeg, smallPos },
1340                { largeNeg, smallNeg } };
1341    }
1342
1343    private void testDiv(BigInteger i1, BigInteger i2) {
1344        BigInteger q = i1.divide(i2);
1345        BigInteger r = i1.remainder(i2);
1346        BigInteger[] temp = i1.divideAndRemainder(i2);
1347
1348        assertTrue("divide and divideAndRemainder do not agree", q
1349                .equals(temp[0]));
1350        assertTrue("remainder and divideAndRemainder do not agree", r
1351                .equals(temp[1]));
1352        assertTrue("signum and equals(zero) do not agree on quotient", q
1353                .signum() != 0
1354                || q.equals(zero));
1355        assertTrue("signum and equals(zero) do not agree on remainder", r
1356                .signum() != 0
1357                || r.equals(zero));
1358        assertTrue("wrong sign on quotient", q.signum() == 0
1359                || q.signum() == i1.signum() * i2.signum());
1360        assertTrue("wrong sign on remainder", r.signum() == 0
1361                || r.signum() == i1.signum());
1362        assertTrue("remainder out of range", r.abs().compareTo(i2.abs()) < 0);
1363        assertTrue("quotient too small", q.abs().add(one).multiply(i2.abs())
1364                .compareTo(i1.abs()) > 0);
1365        assertTrue("quotient too large", q.abs().multiply(i2.abs()).compareTo(
1366                i1.abs()) <= 0);
1367        BigInteger p = q.multiply(i2);
1368        BigInteger a = p.add(r);
1369        assertTrue("(a/b)*b+(a%b) != a", a.equals(i1));
1370        try {
1371            BigInteger mod = i1.mod(i2);
1372            assertTrue("mod is negative", mod.signum() >= 0);
1373            assertTrue("mod out of range", mod.abs().compareTo(i2.abs()) < 0);
1374            assertTrue("positive remainder == mod", r.signum() < 0
1375                    || r.equals(mod));
1376            assertTrue("negative remainder == mod - divisor", r.signum() >= 0
1377                    || r.equals(mod.subtract(i2)));
1378        } catch (ArithmeticException e) {
1379            assertTrue("mod fails on negative divisor only", i2.signum() <= 0);
1380        }
1381    }
1382
1383    private void testDivRanges(BigInteger i) {
1384        BigInteger bound = i.multiply(two);
1385        for (BigInteger j = bound.negate(); j.compareTo(bound) <= 0; j = j
1386                .add(i)) {
1387            BigInteger innerbound = j.add(two);
1388            BigInteger k = j.subtract(two);
1389            for (; k.compareTo(innerbound) <= 0; k = k.add(one)) {
1390                testDiv(k, i);
1391            }
1392        }
1393    }
1394
1395    private boolean isPrime(long b) {
1396        if (b == 2) {
1397            return true;
1398        }
1399        // check for div by 2
1400        if ((b & 1L) == 0) {
1401            return false;
1402        }
1403        long maxlen = ((long) Math.sqrt(b)) + 2;
1404        for (long x = 3; x < maxlen; x += 2) {
1405            if (b % x == 0) {
1406                return false;
1407            }
1408        }
1409        return true;
1410    }
1411
1412    private void testAllMults(BigInteger i1, BigInteger i2, BigInteger ans) {
1413        assertTrue("i1*i2=ans", i1.multiply(i2).equals(ans));
1414        assertTrue("i2*i1=ans", i2.multiply(i1).equals(ans));
1415        assertTrue("-i1*i2=-ans", i1.negate().multiply(i2).equals(ans.negate()));
1416        assertTrue("-i2*i1=-ans", i2.negate().multiply(i1).equals(ans.negate()));
1417        assertTrue("i1*-i2=-ans", i1.multiply(i2.negate()).equals(ans.negate()));
1418        assertTrue("i2*-i1=-ans", i2.multiply(i1.negate()).equals(ans.negate()));
1419        assertTrue("-i1*-i2=ans", i1.negate().multiply(i2.negate()).equals(ans));
1420        assertTrue("-i2*-i1=ans", i2.negate().multiply(i1.negate()).equals(ans));
1421    }
1422
1423    private void testAllDivs(BigInteger i1, BigInteger i2) {
1424        testDiv(i1, i2);
1425        testDiv(i1.negate(), i2);
1426        testDiv(i1, i2.negate());
1427        testDiv(i1.negate(), i2.negate());
1428    }
1429}
1430