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 * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946
27 * @summary tests methods in BigInteger
28 * @run main/timeout=400 BigIntegerTest
29 * @author madbot
30 */
31
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.ObjectInputStream;
36 import java.io.ObjectOutputStream;
37 import java.math.BigInteger;
38 import java.util.Random;
39
40 /**
41 * This is a simple test class created to ensure that the results
42 * generated by BigInteger adhere to certain identities. Passing
43 * this test is a strong assurance that the BigInteger operations
44 * are working correctly.
45 *
46 * Four arguments may be specified which give the number of
72 static final int BITS_SCHOENHAGE_BASE = 640;
73 static final int BITS_BURNIKEL_ZIEGLER = 2560;
74 static final int BITS_BURNIKEL_ZIEGLER_OFFSET = 1280;
75
76 static final int ORDER_SMALL = 60;
77 static final int ORDER_MEDIUM = 100;
78 // #bits for testing Karatsuba
79 static final int ORDER_KARATSUBA = 2760;
80 // #bits for testing Toom-Cook and Burnikel-Ziegler
81 static final int ORDER_TOOM_COOK = 8000;
82 // #bits for testing Karatsuba squaring
83 static final int ORDER_KARATSUBA_SQUARE = 4200;
84 // #bits for testing Toom-Cook squaring
85 static final int ORDER_TOOM_COOK_SQUARE = 7000;
86
87 static final int SIZE = 1000; // numbers per batch
88
89 static Random rnd = new Random();
90 static boolean failure = false;
91
92 public static void pow(int order) {
93 int failCount1 = 0;
94
95 for (int i=0; i<SIZE; i++) {
96 // Test identity x^power == x*x*x ... *x
97 int power = rnd.nextInt(6) + 2;
98 BigInteger x = fetchNumber(order);
99 BigInteger y = x.pow(power);
100 BigInteger z = x;
101
102 for (int j=1; j<power; j++)
103 z = z.multiply(x);
104
105 if (!y.equals(z))
106 failCount1++;
107 }
108 report("pow for " + order + " bits", failCount1);
109 }
110
111 public static void square(int order) {
944 * the maximum number of decimal digits that the parameters will have.
945 *
946 */
947 public static void main(String[] args) throws Exception {
948
949 // Some variables for sizing test numbers in bits
950 int order1 = ORDER_MEDIUM;
951 int order2 = ORDER_SMALL;
952 int order3 = ORDER_KARATSUBA;
953 int order4 = ORDER_TOOM_COOK;
954
955 if (args.length >0)
956 order1 = (int)((Integer.parseInt(args[0]))* 3.333);
957 if (args.length >1)
958 order2 = (int)((Integer.parseInt(args[1]))* 3.333);
959 if (args.length >2)
960 order3 = (int)((Integer.parseInt(args[2]))* 3.333);
961 if (args.length >3)
962 order4 = (int)((Integer.parseInt(args[3]))* 3.333);
963
964 prime();
965 nextProbablePrime();
966
967 arithmetic(order1); // small numbers
968 arithmetic(order3); // Karatsuba range
969 arithmetic(order4); // Toom-Cook / Burnikel-Ziegler range
970
971 divideAndRemainder(order1); // small numbers
972 divideAndRemainder(order3); // Karatsuba range
973 divideAndRemainder(order4); // Toom-Cook / Burnikel-Ziegler range
974
975 pow(order1);
976 pow(order3);
977 pow(order4);
978
979 square(ORDER_MEDIUM);
980 square(ORDER_KARATSUBA_SQUARE);
981 square(ORDER_TOOM_COOK_SQUARE);
982
983 bitCount();
|
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 * @bug 4181191 4161971 4227146 4194389 4823171 4624738 4812225 4837946 4026465
27 * @summary tests methods in BigInteger
28 * @run main/timeout=400 BigIntegerTest
29 * @author madbot
30 */
31
32 import java.io.File;
33 import java.io.FileInputStream;
34 import java.io.FileOutputStream;
35 import java.io.ObjectInputStream;
36 import java.io.ObjectOutputStream;
37 import java.math.BigInteger;
38 import java.util.Random;
39
40 /**
41 * This is a simple test class created to ensure that the results
42 * generated by BigInteger adhere to certain identities. Passing
43 * this test is a strong assurance that the BigInteger operations
44 * are working correctly.
45 *
46 * Four arguments may be specified which give the number of
72 static final int BITS_SCHOENHAGE_BASE = 640;
73 static final int BITS_BURNIKEL_ZIEGLER = 2560;
74 static final int BITS_BURNIKEL_ZIEGLER_OFFSET = 1280;
75
76 static final int ORDER_SMALL = 60;
77 static final int ORDER_MEDIUM = 100;
78 // #bits for testing Karatsuba
79 static final int ORDER_KARATSUBA = 2760;
80 // #bits for testing Toom-Cook and Burnikel-Ziegler
81 static final int ORDER_TOOM_COOK = 8000;
82 // #bits for testing Karatsuba squaring
83 static final int ORDER_KARATSUBA_SQUARE = 4200;
84 // #bits for testing Toom-Cook squaring
85 static final int ORDER_TOOM_COOK_SQUARE = 7000;
86
87 static final int SIZE = 1000; // numbers per batch
88
89 static Random rnd = new Random();
90 static boolean failure = false;
91
92 public static void constructor() {
93 int failCount = 0;
94
95 byte[] magZeroLength = new byte[0];
96 for (int signum = -1; signum <= 1; signum++) {
97 BigInteger bi = new BigInteger(signum, magZeroLength);
98 if (bi.compareTo(BigInteger.ZERO) != 0) {
99 System.err.println("A: Zero length BigInteger != 0 for signum " + signum);
100 failCount++;
101 }
102 }
103
104 for (int signum = -1; signum <= 1; signum++) {
105 BigInteger bi = new BigInteger(signum, magZeroLength, 0, 0);
106 if (bi.compareTo(BigInteger.ZERO) != 0) {
107 System.err.println("B: Zero length BigInteger != 0 for signum " + signum);
108 failCount++;
109 }
110 }
111
112 byte[] magNonZeroLength = new byte[42];
113 rnd.nextBytes(magNonZeroLength);
114 for (int signum = -1; signum <= 1; signum++) {
115 BigInteger bi = new BigInteger(signum, magNonZeroLength, 0, 0);
116 if (bi.compareTo(BigInteger.ZERO) != 0) {
117 System.err.println("C: Zero length BigInteger != 0 for signum " + signum);
118 failCount++;
119 }
120 }
121
122 for (int i = 0; i < SIZE; i++) {
123 BigInteger reference = new BigInteger(rnd.nextInt(5000), rnd);
124
125 byte[] refArray = reference.toByteArray();
126 int refLen = refArray.length;
127 int factor = rnd.nextInt(5);
128 int objLen = refArray.length + factor*rnd.nextInt(refArray.length) + 1;
129 int offset = rnd.nextInt(objLen - refLen);
130 byte[] objArray = new byte[objLen];
131 System.arraycopy(refArray, 0, objArray, offset, refLen);
132
133 BigInteger twosComp = new BigInteger(objArray, offset, refLen);
134 if (twosComp.compareTo(reference) != 0) {
135 System.err.println("Two's-complement BigInteger not equal for offset " +
136 offset + " and length " + refLen);
137 failCount++;
138 }
139
140 boolean isNegative = rnd.nextBoolean();
141 BigInteger signMag = new BigInteger(isNegative ? -1 : 1, objArray, offset, refLen);
142 if (signMag.compareTo(isNegative ? reference.negate() : reference) != 0) {
143 System.err.println("Sign-magnitude BigInteger not equal for offset " +
144 offset + " and length " + refLen);
145 failCount++;
146 }
147 }
148
149 report("Constructor", failCount);
150 }
151
152 public static void pow(int order) {
153 int failCount1 = 0;
154
155 for (int i=0; i<SIZE; i++) {
156 // Test identity x^power == x*x*x ... *x
157 int power = rnd.nextInt(6) + 2;
158 BigInteger x = fetchNumber(order);
159 BigInteger y = x.pow(power);
160 BigInteger z = x;
161
162 for (int j=1; j<power; j++)
163 z = z.multiply(x);
164
165 if (!y.equals(z))
166 failCount1++;
167 }
168 report("pow for " + order + " bits", failCount1);
169 }
170
171 public static void square(int order) {
1004 * the maximum number of decimal digits that the parameters will have.
1005 *
1006 */
1007 public static void main(String[] args) throws Exception {
1008
1009 // Some variables for sizing test numbers in bits
1010 int order1 = ORDER_MEDIUM;
1011 int order2 = ORDER_SMALL;
1012 int order3 = ORDER_KARATSUBA;
1013 int order4 = ORDER_TOOM_COOK;
1014
1015 if (args.length >0)
1016 order1 = (int)((Integer.parseInt(args[0]))* 3.333);
1017 if (args.length >1)
1018 order2 = (int)((Integer.parseInt(args[1]))* 3.333);
1019 if (args.length >2)
1020 order3 = (int)((Integer.parseInt(args[2]))* 3.333);
1021 if (args.length >3)
1022 order4 = (int)((Integer.parseInt(args[3]))* 3.333);
1023
1024 constructor();
1025
1026 prime();
1027 nextProbablePrime();
1028
1029 arithmetic(order1); // small numbers
1030 arithmetic(order3); // Karatsuba range
1031 arithmetic(order4); // Toom-Cook / Burnikel-Ziegler range
1032
1033 divideAndRemainder(order1); // small numbers
1034 divideAndRemainder(order3); // Karatsuba range
1035 divideAndRemainder(order4); // Toom-Cook / Burnikel-Ziegler range
1036
1037 pow(order1);
1038 pow(order3);
1039 pow(order4);
1040
1041 square(ORDER_MEDIUM);
1042 square(ORDER_KARATSUBA_SQUARE);
1043 square(ORDER_TOOM_COOK_SQUARE);
1044
1045 bitCount();
|