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
47 * decimal digits you desire in the four batches of test numbers.
48 *
49 * The tests are performed on arrays of random numbers which are
50 * generated by a Random class as well as special cases which
51 * throw in boundary numbers such as 0, 1, maximum sized, etc.
52 *
53 */
54 public class BigIntegerTest {
55 //
56 // Bit large number thresholds based on the int thresholds
57 // defined in BigInteger itself:
58 //
59 // KARATSUBA_THRESHOLD = 50 ints = 1600 bits
60 // TOOM_COOK_THRESHOLD = 75 ints = 2400 bits
61 // KARATSUBA_SQUARE_THRESHOLD = 90 ints = 2880 bits
62 // TOOM_COOK_SQUARE_THRESHOLD = 140 ints = 4480 bits
63 //
64 // SCHOENHAGE_BASE_CONVERSION_THRESHOLD = 8 ints = 256 bits
65 //
66 // BURNIKEL_ZIEGLER_THRESHOLD = 50 ints = 1600 bits
67 //
68 static final int BITS_KARATSUBA = 1600;
69 static final int BITS_TOOM_COOK = 2400;
70 static final int BITS_KARATSUBA_SQUARE = 2880;
71 static final int BITS_TOOM_COOK_SQUARE = 4480;
72 static final int BITS_SCHOENHAGE_BASE = 256;
73 static final int BITS_BURNIKEL_ZIEGLER = 1600;
74
75 static final int ORDER_SMALL = 60;
76 static final int ORDER_MEDIUM = 100;
77 // #bits for testing Karatsuba
78 static final int ORDER_KARATSUBA = 1800;
79 // #bits for testing Toom-Cook and Burnikel-Ziegler
80 static final int ORDER_TOOM_COOK = 4000;
81 // #bits for testing Karatsuba squaring
82 static final int ORDER_KARATSUBA_SQUARE = 3200;
83 // #bits for testing Toom-Cook squaring
84 static final int ORDER_TOOM_COOK_SQUARE = 4600;
85
86 static final int SIZE = 1000; // numbers per batch
87
88 static Random rnd = new Random();
89 static boolean failure = false;
90
91 public static void pow(int order) {
92 int failCount1 = 0;
93
94 for (int i=0; i<SIZE; i++) {
95 // Test identity x^power == x*x*x ... *x
96 int power = rnd.nextInt(6) + 2;
97 BigInteger x = fetchNumber(order);
98 BigInteger y = x.pow(power);
99 BigInteger z = x;
100
101 for (int j=1; j<power; j++)
102 z = z.multiply(x);
103
104 if (!y.equals(z))
|
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
47 * decimal digits you desire in the four batches of test numbers.
48 *
49 * The tests are performed on arrays of random numbers which are
50 * generated by a Random class as well as special cases which
51 * throw in boundary numbers such as 0, 1, maximum sized, etc.
52 *
53 */
54 public class BigIntegerTest {
55 //
56 // Bit large number thresholds based on the int thresholds
57 // defined in BigInteger itself:
58 //
59 // KARATSUBA_THRESHOLD = 80 ints = 2560 bits
60 // TOOM_COOK_THRESHOLD = 240 ints = 7680 bits
61 // KARATSUBA_SQUARE_THRESHOLD = 128 ints = 4096 bits
62 // TOOM_COOK_SQUARE_THRESHOLD = 216 ints = 6912 bits
63 //
64 // SCHOENHAGE_BASE_CONVERSION_THRESHOLD = 20 ints = 640 bits
65 //
66 // BURNIKEL_ZIEGLER_THRESHOLD = 80 ints = 2560 bits
67 //
68 static final int BITS_KARATSUBA = 2560;
69 static final int BITS_TOOM_COOK = 7680;
70 static final int BITS_KARATSUBA_SQUARE = 4096;
71 static final int BITS_TOOM_COOK_SQUARE = 6912;
72 static final int BITS_SCHOENHAGE_BASE = 640;
73 static final int BITS_BURNIKEL_ZIEGLER = 2560;
74
75 static final int ORDER_SMALL = 60;
76 static final int ORDER_MEDIUM = 100;
77 // #bits for testing Karatsuba
78 static final int ORDER_KARATSUBA = 2760;
79 // #bits for testing Toom-Cook and Burnikel-Ziegler
80 static final int ORDER_TOOM_COOK = 8000;
81 // #bits for testing Karatsuba squaring
82 static final int ORDER_KARATSUBA_SQUARE = 4200;
83 // #bits for testing Toom-Cook squaring
84 static final int ORDER_TOOM_COOK_SQUARE = 7000;
85
86 static final int SIZE = 1000; // numbers per batch
87
88 static Random rnd = new Random();
89 static boolean failure = false;
90
91 public static void pow(int order) {
92 int failCount1 = 0;
93
94 for (int i=0; i<SIZE; i++) {
95 // Test identity x^power == x*x*x ... *x
96 int power = rnd.nextInt(6) + 2;
97 BigInteger x = fetchNumber(order);
98 BigInteger y = x.pow(power);
99 BigInteger z = x;
100
101 for (int j=1; j<power; j++)
102 z = z.multiply(x);
103
104 if (!y.equals(z))
|