< prev index next >

test/jdk/java/math/BigInteger/BigIntegerTest.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @library /test/lib
  27  * @build jdk.test.lib.RandomFactory
  28  * @run main BigIntegerTest
  29  * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946 4026465 8074460 8078672 8032027
  30  * @summary tests methods in BigInteger (use -Dseed=X to set PRNG seed)
  31  * @run main/timeout=400 BigIntegerTest
  32  * @author madbot
  33  * @key randomness
  34  */
  35 
  36 import java.io.File;
  37 import java.io.FileInputStream;
  38 import java.io.FileOutputStream;
  39 import java.io.ObjectInputStream;
  40 import java.io.ObjectOutputStream;
  41 import java.math.BigDecimal;
  42 import java.math.BigInteger;
  43 import java.util.Random;
  44 import java.util.function.ToIntFunction;
  45 import java.util.stream.Collectors;
  46 import java.util.stream.DoubleStream;
  47 import java.util.stream.IntStream;
  48 import java.util.stream.LongStream;
  49 import java.util.stream.Stream;


 778                 failCount1++;
 779                 System.err.println("fail2 x :"+x);
 780                 System.err.println("      y :"+y);
 781             }
 782 
 783             y = x.divideAndRemainder(z);
 784             if (!y[0].equals(BigInteger.valueOf(2))) {
 785                 failCount1++;
 786                 System.err.println("fail3 x :"+x);
 787                 System.err.println("      y :"+y);
 788             }
 789         }
 790         report("divideAndRemainder for " + order + " bits", failCount1);
 791     }
 792 
 793     public static void stringConv() {
 794         int failCount = 0;
 795 
 796         // Generic string conversion.
 797         for (int i=0; i<100; i++) {
 798             byte xBytes[] = new byte[Math.abs(random.nextInt())%100+1];
 799             random.nextBytes(xBytes);
 800             BigInteger x = new BigInteger(xBytes);
 801 
 802             for (int radix=Character.MIN_RADIX; radix < Character.MAX_RADIX; radix++) {
 803                 String result = x.toString(radix);
 804                 BigInteger test = new BigInteger(result, radix);
 805                 if (!test.equals(x)) {
 806                     failCount++;
 807                     System.err.println("BigInteger toString: "+x);
 808                     System.err.println("Test: "+test);
 809                     System.err.println(radix);
 810                 }
 811             }
 812         }
 813 
 814         // String conversion straddling the Schoenhage algorithm crossover
 815         // threshold, and at twice and four times the threshold.
 816         for (int k = 0; k <= 2; k++) {
 817             int factor = 1 << k;
 818             int upper = factor * BITS_SCHOENHAGE_BASE + 33;
 819             int lower = upper - 35;
 820 
 821             for (int bits = upper; bits >= lower; bits--) {
 822                 for (int i = 0; i < 50; i++) {
 823                     BigInteger x = BigInteger.ONE.shiftLeft(bits - 1).or(new BigInteger(bits - 2, random));
 824 
 825                     for (int radix = Character.MIN_RADIX; radix < Character.MAX_RADIX; radix++) {
 826                         String result = x.toString(radix);
 827                         BigInteger test = new BigInteger(result, radix);
 828                         if (!test.equals(x)) {
 829                             failCount++;
 830                             System.err.println("BigInteger toString: " + x);
 831                             System.err.println("Test: " + test);
 832                             System.err.println(radix);
 833                         }
 834                     }
 835                 }
 836             }
 837         }
 838 










 839         report("String Conversion", failCount);
 840     }
 841 
 842     public static void byteArrayConv(int order) {
 843         int failCount = 0;
 844 
 845         for (int i=0; i<SIZE; i++) {
 846             BigInteger x = fetchNumber(order);
 847             while (x.equals(BigInteger.ZERO))
 848                 x = fetchNumber(order);
 849             BigInteger y = new BigInteger(x.toByteArray());
 850             if (!x.equals(y)) {
 851                 failCount++;
 852                 System.err.println("orig is "+x);
 853                 System.err.println("new is "+y);
 854             }
 855         }
 856         report("Array Conversion for " + order + " bits", failCount);
 857     }
 858 


   1 /*
   2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @library /test/lib
  27  * @build jdk.test.lib.RandomFactory
  28  * @run main BigIntegerTest
  29  * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946 4026465 8074460 8078672 8032027 8229845
  30  * @summary tests methods in BigInteger (use -Dseed=X to set PRNG seed)
  31  * @run main/timeout=400 BigIntegerTest
  32  * @author madbot
  33  * @key randomness
  34  */
  35 
  36 import java.io.File;
  37 import java.io.FileInputStream;
  38 import java.io.FileOutputStream;
  39 import java.io.ObjectInputStream;
  40 import java.io.ObjectOutputStream;
  41 import java.math.BigDecimal;
  42 import java.math.BigInteger;
  43 import java.util.Random;
  44 import java.util.function.ToIntFunction;
  45 import java.util.stream.Collectors;
  46 import java.util.stream.DoubleStream;
  47 import java.util.stream.IntStream;
  48 import java.util.stream.LongStream;
  49 import java.util.stream.Stream;


 778                 failCount1++;
 779                 System.err.println("fail2 x :"+x);
 780                 System.err.println("      y :"+y);
 781             }
 782 
 783             y = x.divideAndRemainder(z);
 784             if (!y[0].equals(BigInteger.valueOf(2))) {
 785                 failCount1++;
 786                 System.err.println("fail3 x :"+x);
 787                 System.err.println("      y :"+y);
 788             }
 789         }
 790         report("divideAndRemainder for " + order + " bits", failCount1);
 791     }
 792 
 793     public static void stringConv() {
 794         int failCount = 0;
 795 
 796         // Generic string conversion.
 797         for (int i=0; i<100; i++) {
 798             byte xBytes[] = new byte[Math.abs(random.nextInt())%200+1];
 799             random.nextBytes(xBytes);
 800             BigInteger x = new BigInteger(xBytes);
 801 
 802             for (int radix=Character.MIN_RADIX; radix < Character.MAX_RADIX; radix++) {
 803                 String result = x.toString(radix);
 804                 BigInteger test = new BigInteger(result, radix);
 805                 if (!test.equals(x)) {
 806                     failCount++;
 807                     System.err.println("BigInteger toString: "+x);
 808                     System.err.println("Test: "+test);
 809                     System.err.println(radix);
 810                 }
 811             }
 812         }
 813 
 814         // String conversion straddling the Schoenhage algorithm crossover
 815         // threshold, and at twice and four times the threshold.
 816         for (int k = 0; k <= 2; k++) {
 817             int factor = 1 << k;
 818             int upper = factor * BITS_SCHOENHAGE_BASE + 33;
 819             int lower = upper - 35;
 820 
 821             for (int bits = upper; bits >= lower; bits--) {
 822                 for (int i = 0; i < 50; i++) {
 823                     BigInteger x = BigInteger.ONE.shiftLeft(bits - 1).or(new BigInteger(bits - 2, random));
 824 
 825                     for (int radix = Character.MIN_RADIX; radix < Character.MAX_RADIX; radix++) {
 826                         String result = x.toString(radix);
 827                         BigInteger test = new BigInteger(result, radix);
 828                         if (!test.equals(x)) {
 829                             failCount++;
 830                             System.err.println("BigInteger toString: " + x);
 831                             System.err.println("Test: " + test);
 832                             System.err.println(radix);
 833                         }
 834                     }
 835                 }
 836             }
 837         }
 838 
 839         // Check value with many trailing zeros.
 840         String val = "123456789" + "0".repeat(200);
 841         BigInteger b = new BigInteger(val);
 842         String s = b.toString();
 843         if (!val.equals(s)) {
 844             System.err.format("Expected length %d but got %d%n",
 845                 val.length(), s.length());
 846             failCount++;
 847         }
 848 
 849         report("String Conversion", failCount);
 850     }
 851 
 852     public static void byteArrayConv(int order) {
 853         int failCount = 0;
 854 
 855         for (int i=0; i<SIZE; i++) {
 856             BigInteger x = fetchNumber(order);
 857             while (x.equals(BigInteger.ZERO))
 858                 x = fetchNumber(order);
 859             BigInteger y = new BigInteger(x.toByteArray());
 860             if (!x.equals(y)) {
 861                 failCount++;
 862                 System.err.println("orig is "+x);
 863                 System.err.println("new is "+y);
 864             }
 865         }
 866         report("Array Conversion for " + order + " bits", failCount);
 867     }
 868 


< prev index next >