• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @author Elena Semukhina
19  */
20 
21 package org.apache.harmony.tests.java.math;
22 
23 import junit.framework.TestCase;
24 import java.math.BigInteger;
25 import java.util.Random;
26 
27 /**
28  * Class:   java.math.BigInteger
29  * Constructors: BigInteger(byte[] a), BigInteger(int sign, byte[] a),
30  *               BigInteger(String val, int radix)
31  */
32 public class BigIntegerConstructorsTest extends TestCase {
33     /**
34      * Create a number from an array of bytes.
35      * Verify an exception thrown if an array is zero bytes long
36      */
testConstructorBytesException()37     public void testConstructorBytesException() {
38         byte aBytes[] = {};
39         try {
40             new BigInteger(aBytes);
41             fail("NumberFormatException has not been caught");
42         } catch (NumberFormatException e) {
43         }
44     }
45 
46     /**
47      * Create a positive number from an array of bytes.
48      * The number fits in an array of integers.
49      */
testConstructorBytesPositive1()50     public void testConstructorBytesPositive1() {
51         byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
52         byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
53         BigInteger aNumber = new BigInteger(aBytes);
54         byte resBytes[] = new byte[rBytes.length];
55         resBytes = aNumber.toByteArray();
56         for(int i = 0; i < resBytes.length; i++) {
57             assertTrue(resBytes[i] == rBytes[i]);
58         }
59         assertEquals("incorrect sign", 1, aNumber.signum());
60     }
61 
62     /**
63      * Create a positive number from an array of bytes.
64      * The number fits in an integer.
65      */
testConstructorBytesPositive2()66     public void testConstructorBytesPositive2() {
67         byte aBytes[] = {12, 56, 100};
68         byte rBytes[] = {12, 56, 100};
69         BigInteger aNumber = new BigInteger(aBytes);
70         byte resBytes[] = new byte[rBytes.length];
71         resBytes = aNumber.toByteArray();
72         for(int i = 0; i < resBytes.length; i++) {
73             assertTrue(resBytes[i] == rBytes[i]);
74         }
75         assertEquals("incorrect sign", 1, aNumber.signum());
76     }
77 
78     /**
79      * Create a positive number from an array of bytes.
80      * The number of bytes is 4.
81      */
testConstructorBytesPositive3()82     public void testConstructorBytesPositive3() {
83         byte aBytes[] = {127, 56, 100, -1};
84         byte rBytes[] = {127, 56, 100, -1};
85         BigInteger aNumber = new BigInteger(aBytes);
86         byte resBytes[] = new byte[rBytes.length];
87         resBytes = aNumber.toByteArray();
88         for(int i = 0; i < resBytes.length; i++) {
89             assertTrue(resBytes[i] == rBytes[i]);
90         }
91         assertEquals("incorrect sign", 1, aNumber.signum());
92     }
93 
94     /**
95      * Create a positive number from an array of bytes.
96      * The number of bytes is multiple of 4.
97      */
testConstructorBytesPositive()98     public void testConstructorBytesPositive() {
99         byte aBytes[] = {127, 56, 100, -1, 14, 75, -24, -100};
100         byte rBytes[] = {127, 56, 100, -1, 14, 75, -24, -100};
101         BigInteger aNumber = new BigInteger(aBytes);
102         byte resBytes[] = new byte[rBytes.length];
103         resBytes = aNumber.toByteArray();
104         for(int i = 0; i < resBytes.length; i++) {
105             assertTrue(resBytes[i] == rBytes[i]);
106         }
107         assertEquals("incorrect sign", 1, aNumber.signum());
108     }
109 
110     /**
111      * Create a negative number from an array of bytes.
112      * The number fits in an array of integers.
113      */
testConstructorBytesNegative1()114     public void testConstructorBytesNegative1() {
115         byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
116         byte rBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
117         BigInteger aNumber = new BigInteger(aBytes);
118         byte resBytes[] = new byte[rBytes.length];
119         resBytes = aNumber.toByteArray();
120         for(int i = 0; i < resBytes.length; i++) {
121             assertTrue(resBytes[i] == rBytes[i]);
122         }
123         assertEquals("incorrect sign", -1, aNumber.signum());
124     }
125 
126     /**
127      * Create a negative number from an array of bytes.
128      * The number fits in an integer.
129      */
testConstructorBytesNegative2()130     public void testConstructorBytesNegative2() {
131         byte aBytes[] = {-12, 56, 100};
132         byte rBytes[] = {-12, 56, 100};
133         BigInteger aNumber = new BigInteger(aBytes);
134         byte resBytes[] = new byte[rBytes.length];
135         resBytes = aNumber.toByteArray();
136         for(int i = 0; i < resBytes.length; i++) {
137             assertTrue(resBytes[i] == rBytes[i]);
138         }
139         assertEquals("incorrect sign", -1, aNumber.signum());
140     }
141 
142     /**
143      * Create a negative number from an array of bytes.
144      * The number of bytes is 4.
145      */
testConstructorBytesNegative3()146     public void testConstructorBytesNegative3() {
147         byte aBytes[] = {-128, -12, 56, 100};
148         byte rBytes[] = {-128, -12, 56, 100};
149         BigInteger aNumber = new BigInteger(aBytes);
150         byte resBytes[] = new byte[rBytes.length];
151         resBytes = aNumber.toByteArray();
152         for(int i = 0; i < resBytes.length; i++) {
153             assertTrue(resBytes[i] == rBytes[i]);
154         }
155         assertEquals("incorrect sign", -1, aNumber.signum());
156     }
157 
158     /**
159      * Create a negative number from an array of bytes.
160      * The number of bytes is multiple of 4.
161      */
testConstructorBytesNegative4()162     public void testConstructorBytesNegative4() {
163         byte aBytes[] = {-128, -12, 56, 100, -13, 56, 93, -78};
164         byte rBytes[] = {-128, -12, 56, 100, -13, 56, 93, -78};
165         BigInteger aNumber = new BigInteger(aBytes);
166         byte resBytes[] = new byte[rBytes.length];
167         resBytes = aNumber.toByteArray();
168         for(int i = 0; i < resBytes.length; i++) {
169             assertTrue(resBytes[i] == rBytes[i]);
170         }
171         assertEquals("incorrect sign", -1, aNumber.signum());
172     }
173 
174     /**
175      * Create a zero number from an array of zero bytes.
176      */
testConstructorBytesZero()177     public void testConstructorBytesZero() {
178         byte aBytes[] = {0, 0, 0, -0, +0, 0, -0};
179         byte rBytes[] = {0};
180         BigInteger aNumber = new BigInteger(aBytes);
181         byte resBytes[] = new byte[rBytes.length];
182         resBytes = aNumber.toByteArray();
183         for(int i = 0; i < resBytes.length; i++) {
184             assertTrue(resBytes[i] == rBytes[i]);
185         }
186         assertEquals("incorrect sign", 0, aNumber.signum());
187     }
188 
189     /**
190      * Create a number from a sign and an array of bytes.
191      * Verify an exception thrown if a sign has improper value.
192      */
testConstructorSignBytesException1()193     public void testConstructorSignBytesException1() {
194         byte aBytes[] = {123, 45, -3, -76};
195         int aSign = 3;
196         try {
197             new BigInteger(aSign, aBytes);
198             fail("NumberFormatException has not been caught");
199         } catch (NumberFormatException e) {
200         }
201     }
202 
203     /**
204      * Create a number from a sign and an array of bytes.
205      * Verify an exception thrown if the array contains non-zero bytes while the sign is 0.
206      */
testConstructorSignBytesException2()207     public void testConstructorSignBytesException2() {
208         byte aBytes[] = {123, 45, -3, -76};
209         int aSign = 0;
210         try {
211             new BigInteger(aSign, aBytes);
212             fail("NumberFormatException has not been caught");
213         } catch (NumberFormatException e) {
214             assertEquals("Improper exception message", "signum-magnitude mismatch", e.getMessage());
215         }
216     }
217 
218     /**
219      * Create a positive number from a sign and an array of bytes.
220      * The number fits in an array of integers.
221      * The most significant byte is positive.
222      */
testConstructorSignBytesPositive1()223     public void testConstructorSignBytesPositive1() {
224         byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
225         int aSign = 1;
226         byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
227         BigInteger aNumber = new BigInteger(aSign, aBytes);
228         byte resBytes[] = new byte[rBytes.length];
229         resBytes = aNumber.toByteArray();
230         for(int i = 0; i < resBytes.length; i++) {
231             assertTrue(resBytes[i] == rBytes[i]);
232         }
233         assertEquals("incorrect sign", 1, aNumber.signum());
234     }
235 
236     /**
237      * Create a positive number from a sign and an array of bytes.
238      * The number fits in an array of integers.
239      * The most significant byte is negative.
240      */
testConstructorSignBytesPositive2()241     public void testConstructorSignBytesPositive2() {
242         byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
243         int aSign = 1;
244         byte rBytes[] = {0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
245         BigInteger aNumber = new BigInteger(aSign, aBytes);
246         byte resBytes[] = new byte[rBytes.length];
247         resBytes = aNumber.toByteArray();
248         for(int i = 0; i < resBytes.length; i++) {
249             assertTrue(resBytes[i] == rBytes[i]);
250         }
251         assertEquals("incorrect sign", 1, aNumber.signum());
252     }
253 
254     /**
255      * Create a positive number from a sign and an array of bytes.
256      * The number fits in an integer.
257      */
testConstructorSignBytesPositive3()258     public void testConstructorSignBytesPositive3() {
259         byte aBytes[] = {-12, 56, 100};
260         int aSign = 1;
261         byte rBytes[] = {0, -12, 56, 100};
262         BigInteger aNumber = new BigInteger(aSign, aBytes);
263         byte resBytes[] = new byte[rBytes.length];
264         resBytes = aNumber.toByteArray();
265         for(int i = 0; i < resBytes.length; i++) {
266             assertTrue(resBytes[i] == rBytes[i]);
267         }
268         assertEquals("incorrect sign", 1, aNumber.signum());
269     }
270 
271     /**
272      * Create a positive number from a sign and an array of bytes.
273      * The number of bytes is 4.
274      * The most significant byte is positive.
275      */
testConstructorSignBytesPositive4()276     public void testConstructorSignBytesPositive4() {
277         byte aBytes[] = {127, 56, 100, -2};
278         int aSign = 1;
279         byte rBytes[] = {127, 56, 100, -2};
280         BigInteger aNumber = new BigInteger(aSign, aBytes);
281         byte resBytes[] = new byte[rBytes.length];
282         resBytes = aNumber.toByteArray();
283         for(int i = 0; i < resBytes.length; i++) {
284             assertTrue(resBytes[i] == rBytes[i]);
285         }
286         assertEquals("incorrect sign", 1, aNumber.signum());
287     }
288 
289     /**
290      * Create a positive number from a sign and an array of bytes.
291      * The number of bytes is 4.
292      * The most significant byte is negative.
293      */
testConstructorSignBytesPositive5()294     public void testConstructorSignBytesPositive5() {
295         byte aBytes[] = {-127, 56, 100, -2};
296         int aSign = 1;
297         byte rBytes[] = {0, -127, 56, 100, -2};
298         BigInteger aNumber = new BigInteger(aSign, aBytes);
299         byte resBytes[] = new byte[rBytes.length];
300         resBytes = aNumber.toByteArray();
301         for(int i = 0; i < resBytes.length; i++) {
302             assertTrue(resBytes[i] == rBytes[i]);
303         }
304         assertEquals("incorrect sign", 1, aNumber.signum());
305     }
306 
307     /**
308      * Create a positive number from a sign and an array of bytes.
309      * The number of bytes is multiple of 4.
310      * The most significant byte is positive.
311      */
testConstructorSignBytesPositive6()312     public void testConstructorSignBytesPositive6() {
313         byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
314         int aSign = 1;
315         byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
316         BigInteger aNumber = new BigInteger(aSign, aBytes);
317         byte resBytes[] = new byte[rBytes.length];
318         resBytes = aNumber.toByteArray();
319         for(int i = 0; i < resBytes.length; i++) {
320             assertTrue(resBytes[i] == rBytes[i]);
321         }
322         assertEquals("incorrect sign", 1, aNumber.signum());
323     }
324 
325     /**
326      * Create a positive number from a sign and an array of bytes.
327      * The number of bytes is multiple of 4.
328      * The most significant byte is negative.
329      */
testConstructorSignBytesPositive7()330     public void testConstructorSignBytesPositive7() {
331         byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
332         int aSign = 1;
333         byte rBytes[] = {0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
334         BigInteger aNumber = new BigInteger(aSign, aBytes);
335         byte resBytes[] = new byte[rBytes.length];
336         resBytes = aNumber.toByteArray();
337         for(int i = 0; i < resBytes.length; i++) {
338             assertTrue(resBytes[i] == rBytes[i]);
339         }
340         assertEquals("incorrect sign", 1, aNumber.signum());
341     }
342 
343     /**
344      * Create a negative number from a sign and an array of bytes.
345      * The number fits in an array of integers.
346      * The most significant byte is positive.
347      */
testConstructorSignBytesNegative1()348     public void testConstructorSignBytesNegative1() {
349         byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
350         int aSign = -1;
351         byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 15};
352         BigInteger aNumber = new BigInteger(aSign, aBytes);
353         byte resBytes[] = new byte[rBytes.length];
354         resBytes = aNumber.toByteArray();
355         for(int i = 0; i < resBytes.length; i++) {
356             assertTrue(resBytes[i] == rBytes[i]);
357         }
358         assertEquals("incorrect sign", -1, aNumber.signum());
359     }
360 
361     /**
362      * Create a negative number from a sign and an array of bytes.
363      * The number fits in an array of integers.
364      * The most significant byte is negative.
365      */
testConstructorSignBytesNegative2()366     public void testConstructorSignBytesNegative2() {
367         byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
368         int aSign = -1;
369         byte rBytes[] = {-1, 11, -57, -101, 1, 75, -90, -46, -92, -4, 15};
370         BigInteger aNumber = new BigInteger(aSign, aBytes);
371         byte resBytes[] = new byte[rBytes.length];
372         resBytes = aNumber.toByteArray();
373         for(int i = 0; i < resBytes.length; i++) {
374             assertTrue(resBytes[i] == rBytes[i]);
375         }
376         assertEquals("incorrect sign", -1, aNumber.signum());
377     }
378 
379     /**
380      * Create a negative number from a sign and an array of bytes.
381      * The number fits in an integer.
382      */
testConstructorSignBytesNegative3()383     public void testConstructorSignBytesNegative3() {
384         byte aBytes[] = {-12, 56, 100};
385         int aSign = -1;
386         byte rBytes[] = {-1, 11, -57, -100};
387         BigInteger aNumber = new BigInteger(aSign, aBytes);
388         byte resBytes[] = new byte[rBytes.length];
389         resBytes = aNumber.toByteArray();
390         for(int i = 0; i < resBytes.length; i++) {
391             assertTrue(resBytes[i] == rBytes[i]);
392         }
393         assertEquals("incorrect sign", -1, aNumber.signum());
394     }
395 
396     /**
397      * Create a negative number from a sign and an array of bytes.
398      * The number of bytes is 4.
399      * The most significant byte is positive.
400      */
testConstructorSignBytesNegative4()401     public void testConstructorSignBytesNegative4() {
402         byte aBytes[] = {127, 56, 100, -2};
403         int aSign = -1;
404         byte rBytes[] = {-128, -57, -101, 2};
405         BigInteger aNumber = new BigInteger(aSign, aBytes);
406         byte resBytes[] = new byte[rBytes.length];
407         resBytes = aNumber.toByteArray();
408         for(int i = 0; i < resBytes.length; i++) {
409             assertTrue(resBytes[i] == rBytes[i]);
410         }
411         assertEquals("incorrect sign", -1, aNumber.signum());
412     }
413 
414     /**
415      * Create a negative number from a sign and an array of bytes.
416      * The number of bytes is 4.
417      * The most significant byte is negative.
418      */
testConstructorSignBytesNegative5()419     public void testConstructorSignBytesNegative5() {
420         byte aBytes[] = {-127, 56, 100, -2};
421         int aSign = -1;
422         byte rBytes[] = {-1, 126, -57, -101, 2};
423         BigInteger aNumber = new BigInteger(aSign, aBytes);
424         byte resBytes[] = new byte[rBytes.length];
425         resBytes = aNumber.toByteArray();
426         for(int i = 0; i < resBytes.length; i++) {
427             assertTrue(resBytes[i] == rBytes[i]);
428         }
429         assertEquals("incorrect sign", -1, aNumber.signum());
430     }
431 
432     /**
433      * Create a negative number from a sign and an array of bytes.
434      * The number of bytes is multiple of 4.
435      * The most significant byte is positive.
436      */
testConstructorSignBytesNegative6()437     public void testConstructorSignBytesNegative6() {
438         byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
439         int aSign = -1;
440         byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 14, -24, 101};
441         BigInteger aNumber = new BigInteger(aSign, aBytes);
442         byte resBytes[] = new byte[rBytes.length];
443         resBytes = aNumber.toByteArray();
444         for(int i = 0; i < resBytes.length; i++) {
445             assertTrue(resBytes[i] == rBytes[i]);
446         }
447         assertEquals("incorrect sign", -1, aNumber.signum());
448     }
449 
450     /**
451      * Create a negative number from a sign and an array of bytes.
452      * The number of bytes is multiple of 4.
453      * The most significant byte is negative.
454      */
testConstructorSignBytesNegative7()455     public void testConstructorSignBytesNegative7() {
456         byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
457         int aSign = -1;
458         byte rBytes[] = {-1, 11, -57, -101, 1, 75, -90, -46, -92, -4, 14, -24, 101};
459         BigInteger aNumber = new BigInteger(aSign, aBytes);
460         byte resBytes[] = new byte[rBytes.length];
461         resBytes = aNumber.toByteArray();
462         for(int i = 0; i < resBytes.length; i++) {
463             assertTrue(resBytes[i] == rBytes[i]);
464         }
465         assertEquals("incorrect sign", -1, aNumber.signum());
466     }
467 
468     /**
469      * Create a zero number from a sign and an array of zero bytes.
470      * The sign is -1.
471      */
testConstructorSignBytesZero1()472     public void testConstructorSignBytesZero1() {
473         byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000};
474         int aSign = -1;
475         byte rBytes[] = {0};
476         BigInteger aNumber = new BigInteger(aSign, aBytes);
477         byte resBytes[] = new byte[rBytes.length];
478         resBytes = aNumber.toByteArray();
479         for(int i = 0; i < resBytes.length; i++) {
480             assertTrue(resBytes[i] == rBytes[i]);
481         }
482         assertEquals("incorrect sign", 0, aNumber.signum());
483     }
484 
485     /**
486      * Create a zero number from a sign and an array of zero bytes.
487      * The sign is 0.
488      */
testConstructorSignBytesZero2()489     public void testConstructorSignBytesZero2() {
490         byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000};
491         int aSign = 0;
492         byte rBytes[] = {0};
493         BigInteger aNumber = new BigInteger(aSign, aBytes);
494         byte resBytes[] = new byte[rBytes.length];
495         resBytes = aNumber.toByteArray();
496         for(int i = 0; i < resBytes.length; i++) {
497             assertTrue(resBytes[i] == rBytes[i]);
498         }
499         assertEquals("incorrect sign", 0, aNumber.signum());
500     }
501 
502     /**
503      * Create a zero number from a sign and an array of zero bytes.
504      * The sign is 1.
505      */
testConstructorSignBytesZero3()506     public void testConstructorSignBytesZero3() {
507         byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000};
508         int aSign = 1;
509         byte rBytes[] = {0};
510         BigInteger aNumber = new BigInteger(aSign, aBytes);
511         byte resBytes[] = new byte[rBytes.length];
512         resBytes = aNumber.toByteArray();
513         for(int i = 0; i < resBytes.length; i++) {
514             assertTrue(resBytes[i] == rBytes[i]);
515         }
516         assertEquals("incorrect sign", 0, aNumber.signum());
517     }
518 
519     /**
520      * Create a zero number from a sign and an array of zero length.
521      * The sign is -1.
522      */
testConstructorSignBytesZeroNull1()523     public void testConstructorSignBytesZeroNull1() {
524         byte aBytes[] = {};
525         int aSign = -1;
526         byte rBytes[] = {0};
527         BigInteger aNumber = new BigInteger(aSign, aBytes);
528         byte resBytes[] = new byte[rBytes.length];
529         resBytes = aNumber.toByteArray();
530         for(int i = 0; i < resBytes.length; i++) {
531             assertTrue(resBytes[i] == rBytes[i]);
532         }
533         assertEquals("incorrect sign", 0, aNumber.signum());
534     }
535 
536     /**
537      * Create a zero number from a sign and an array of zero length.
538      * The sign is 0.
539      */
testConstructorSignBytesZeroNull2()540     public void testConstructorSignBytesZeroNull2() {
541         byte aBytes[] = {};
542         int aSign = 0;
543         byte rBytes[] = {0};
544         BigInteger aNumber = new BigInteger(aSign, aBytes);
545         byte resBytes[] = new byte[rBytes.length];
546         resBytes = aNumber.toByteArray();
547         for(int i = 0; i < resBytes.length; i++) {
548             assertTrue(resBytes[i] == rBytes[i]);
549         }
550         assertEquals("incorrect sign", 0, aNumber.signum());
551     }
552 
553     /**
554      * Create a zero number from a sign and an array of zero length.
555      * The sign is 1.
556      */
testConstructorSignBytesZeroNull3()557     public void testConstructorSignBytesZeroNull3() {
558         byte aBytes[] = {};
559         int aSign = 1;
560         byte rBytes[] = {0};
561         BigInteger aNumber = new BigInteger(aSign, aBytes);
562         byte resBytes[] = new byte[rBytes.length];
563         resBytes = aNumber.toByteArray();
564         for(int i = 0; i < resBytes.length; i++) {
565             assertTrue(resBytes[i] == rBytes[i]);
566         }
567         assertEquals("incorrect sign", 0, aNumber.signum());
568     }
569 
570     /**
571      * Create a number from a string value and radix.
572      * Verify an exception thrown if a radix is out of range
573      */
testConstructorStringException1()574     public void testConstructorStringException1() {
575         String value = "9234853876401";
576         int radix = 45;
577         try {
578             new BigInteger(value, radix);
579             fail("NumberFormatException has not been caught");
580         } catch (NumberFormatException e) {
581         }
582     }
583 
584     /**
585      * Create a number from a string value and radix.
586      * Verify an exception thrown if the string starts with a space.
587      */
testConstructorStringException2()588     public void testConstructorStringException2() {
589         String value = "   9234853876401";
590         int radix = 10;
591         try {
592             new BigInteger(value, radix);
593             fail("NumberFormatException has not been caught");
594         } catch (NumberFormatException e) {
595         }
596     }
597 
598     /**
599      * Create a number from a string value and radix.
600      * Verify an exception thrown if the string contains improper characters.
601      */
testConstructorStringException3()602     public void testConstructorStringException3() {
603         String value = "92348$*#78987";
604         int radix = 34;
605         try {
606             new BigInteger(value, radix);
607             fail("NumberFormatException has not been caught");
608         } catch (NumberFormatException e) {
609         }
610     }
611 
612     /**
613      * Create a number from a string value and radix.
614      * Verify an exception thrown if some digits are greater than radix.
615      */
testConstructorStringException4()616     public void testConstructorStringException4() {
617         String value = "98zv765hdsaiy";
618         int radix = 20;
619         try {
620             new BigInteger(value, radix);
621             fail("NumberFormatException has not been caught");
622         } catch (NumberFormatException e) {
623         }
624     }
625 
626     /**
627      * Create a positive number from a string value and radix 2.
628      */
testConstructorStringRadix2()629     public void testConstructorStringRadix2() {
630         String value = "10101010101010101";
631         int radix = 2;
632         byte rBytes[] = {1, 85, 85};
633         BigInteger aNumber = new BigInteger(value, radix);
634         byte resBytes[] = new byte[rBytes.length];
635         resBytes = aNumber.toByteArray();
636         for(int i = 0; i < resBytes.length; i++) {
637             assertTrue(resBytes[i] == rBytes[i]);
638         }
639         assertEquals("incorrect sign", 1, aNumber.signum());
640     }
641 
642     /**
643      * Create a positive number from a string value and radix 8.
644      */
testConstructorStringRadix8()645     public void testConstructorStringRadix8() {
646         String value = "76356237071623450";
647         int radix = 8;
648         byte rBytes[] = {7, -50, -28, -8, -25, 39, 40};
649         BigInteger aNumber = new BigInteger(value, radix);
650         byte resBytes[] = new byte[rBytes.length];
651         resBytes = aNumber.toByteArray();
652         for(int i = 0; i < resBytes.length; i++) {
653             assertTrue(resBytes[i] == rBytes[i]);
654         }
655         assertEquals("incorrect sign", 1, aNumber.signum());
656     }
657 
658     /**
659      * Create a positive number from a string value and radix 10.
660      */
testConstructorStringRadix10()661     public void testConstructorStringRadix10() {
662         String value = "987328901348934898";
663         int radix = 10;
664         byte rBytes[] = {13, -77, -78, 103, -103, 97, 68, -14};
665         BigInteger aNumber = new BigInteger(value, radix);
666         byte resBytes[] = new byte[rBytes.length];
667         resBytes = aNumber.toByteArray();
668         for(int i = 0; i < resBytes.length; i++) {
669             assertTrue(resBytes[i] == rBytes[i]);
670         }
671         assertEquals("incorrect sign", 1, aNumber.signum());
672     }
673 
674     /**
675      * Create a positive number from a string value and radix 16.
676      */
testConstructorStringRadix16()677     public void testConstructorStringRadix16() {
678         String value = "fe2340a8b5ce790";
679         int radix = 16;
680         byte rBytes[] = {15, -30, 52, 10, -117, 92, -25, -112};
681         BigInteger aNumber = new BigInteger(value, radix);
682         byte resBytes[] = new byte[rBytes.length];
683         resBytes = aNumber.toByteArray();
684         for(int i = 0; i < resBytes.length; i++) {
685             assertTrue(resBytes[i] == rBytes[i]);
686         }
687         assertEquals("incorrect sign", 1, aNumber.signum());
688     }
689 
690     /**
691      * Create a positive number from a string value and radix 36.
692      */
testConstructorStringRadix36()693     public void testConstructorStringRadix36() {
694         String value = "skdjgocvhdjfkl20jndjkf347ejg457";
695         int radix = 36;
696         byte rBytes[] = {0, -12, -116, 112, -105, 12, -36, 66, 108, 66, -20, -37, -15, 108, -7, 52, -99, -109, -8, -45, -5};
697         BigInteger aNumber = new BigInteger(value, radix);
698         byte resBytes[] = new byte[rBytes.length];
699         resBytes = aNumber.toByteArray();
700         for(int i = 0; i < resBytes.length; i++) {
701             assertTrue(resBytes[i] == rBytes[i]);
702         }
703         assertEquals("incorrect sign", 1, aNumber.signum());
704     }
705 
706     /**
707      * Create a negative number from a string value and radix 10.
708      */
testConstructorStringRadix10Negative()709     public void testConstructorStringRadix10Negative() {
710         String value = "-234871376037";
711         int radix = 36;
712         byte rBytes[] = {-4, 48, 71, 62, -76, 93, -105, 13};
713         BigInteger aNumber = new BigInteger(value, radix);
714         byte resBytes[] = new byte[rBytes.length];
715         resBytes = aNumber.toByteArray();
716         for(int i = 0; i < resBytes.length; i++) {
717             assertTrue(resBytes[i] == rBytes[i]);
718         }
719         assertEquals("incorrect sign", -1, aNumber.signum());
720     }
721 
722     /**
723      * Create a zero number from a string value and radix 36.
724      */
testConstructorStringRadix10Zero()725     public void testConstructorStringRadix10Zero() {
726         String value = "-00000000000000";
727         int radix = 10;
728         byte rBytes[] = {0};
729         BigInteger aNumber = new BigInteger(value, radix);
730         byte resBytes[] = new byte[rBytes.length];
731         resBytes = aNumber.toByteArray();
732         for(int i = 0; i < resBytes.length; i++) {
733             assertTrue(resBytes[i] == rBytes[i]);
734         }
735         assertEquals("incorrect sign", 0, aNumber.signum());
736     }
737 
738     /**
739      * Create a random number of 75 bits length.
740      */
testConstructorRandom()741     public void testConstructorRandom() {
742         int bitLen = 75;
743         Random rnd = new Random();
744         BigInteger aNumber = new BigInteger(bitLen, rnd);
745         assertTrue("incorrect bitLength", aNumber.bitLength() <= bitLen);
746     }
747 
testConstructorPrime()748   public void testConstructorPrime() {
749     for (int rep = 0; rep < 2048; ++rep) {
750       Random rnd = new Random();
751       BigInteger b;
752       int bits;
753 
754       // Create a 128-bit prime number.
755       bits = 128;
756       b = new BigInteger(bits, 10, rnd);
757       assertEquals(b.toString(), bits, b.bitLength());
758 
759       // Create a prime number of 25 bits length.
760       bits = 25;
761       b = new BigInteger(bits, 10, rnd);
762       assertEquals(b.toString(), bits, b.bitLength());
763 
764       // Create a prime number of 18 bits length.
765       bits = 18;
766       b = new BigInteger(bits, 10, rnd);
767       assertEquals(b.toString(), bits, b.bitLength());
768 
769       // On Android, anything less than 16 bits used to be at least 16 bits
770       // because that's how OpenSSL behaves, but we recently fixed this...
771       bits = 2;
772       b = new BigInteger(bits, 10, rnd);
773       assertEquals(b.toString(), bits, b.bitLength());
774 
775       // The 2-arg constructor has never used OpenSSL.
776       bits = 2;
777       b = new BigInteger(bits, rnd);
778       assertTrue(b.toString(), b.bitLength() <= bits);
779       assertTrue(b.toString(), b.intValue() <= 3);
780 
781       bits = 16;
782       b = new BigInteger(bits, rnd);
783       assertTrue(b.toString(), b.bitLength() <= bits);
784     }
785   }
786 }
787