001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Elena Semukhina
019: * @version $Revision$
020: */package org.apache.harmony.tests.java.math;
021:
022: import junit.framework.TestCase;
023: import java.math.BigInteger;
024: import java.util.Random;
025:
026: /**
027: * Class: java.math.BigInteger
028: * Constructors: BigInteger(byte[] a), BigInteger(int sign, byte[] a),
029: * BigInteger(String val, int radix)
030: */
031: public class BigIntegerConstructorsTest extends TestCase {
032: /**
033: * Create a number from an array of bytes.
034: * Verify an exception thrown if an array is zero bytes long
035: */
036: public void testConstructorBytesException() {
037: byte aBytes[] = {};
038: try {
039: new BigInteger(aBytes);
040: fail("NumberFormatException has not been caught");
041: } catch (NumberFormatException e) {
042: assertEquals("Improper exception message",
043: "Zero length BigInteger", e.getMessage());
044: }
045: }
046:
047: /**
048: * Create a positive number from an array of bytes.
049: * The number fits in an array of integers.
050: */
051: public void testConstructorBytesPositive1() {
052: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
053: 26, 3, 91 };
054: byte rBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
055: 26, 3, 91 };
056: BigInteger aNumber = new BigInteger(aBytes);
057: byte resBytes[] = new byte[rBytes.length];
058: resBytes = aNumber.toByteArray();
059: for (int i = 0; i < resBytes.length; i++) {
060: assertTrue(resBytes[i] == rBytes[i]);
061: }
062: assertEquals("incorrect sign", 1, aNumber.signum());
063: }
064:
065: /**
066: * Create a positive number from an array of bytes.
067: * The number fits in an integer.
068: */
069: public void testConstructorBytesPositive2() {
070: byte aBytes[] = { 12, 56, 100 };
071: byte rBytes[] = { 12, 56, 100 };
072: BigInteger aNumber = new BigInteger(aBytes);
073: byte resBytes[] = new byte[rBytes.length];
074: resBytes = aNumber.toByteArray();
075: for (int i = 0; i < resBytes.length; i++) {
076: assertTrue(resBytes[i] == rBytes[i]);
077: }
078: assertEquals("incorrect sign", 1, aNumber.signum());
079: }
080:
081: /**
082: * Create a positive number from an array of bytes.
083: * The number of bytes is 4.
084: */
085: public void testConstructorBytesPositive3() {
086: byte aBytes[] = { 127, 56, 100, -1 };
087: byte rBytes[] = { 127, 56, 100, -1 };
088: BigInteger aNumber = new BigInteger(aBytes);
089: byte resBytes[] = new byte[rBytes.length];
090: resBytes = aNumber.toByteArray();
091: for (int i = 0; i < resBytes.length; i++) {
092: assertTrue(resBytes[i] == rBytes[i]);
093: }
094: assertEquals("incorrect sign", 1, aNumber.signum());
095: }
096:
097: /**
098: * Create a positive number from an array of bytes.
099: * The number of bytes is multiple of 4.
100: */
101: public void testConstructorBytesPositive() {
102: byte aBytes[] = { 127, 56, 100, -1, 14, 75, -24, -100 };
103: byte rBytes[] = { 127, 56, 100, -1, 14, 75, -24, -100 };
104: BigInteger aNumber = new BigInteger(aBytes);
105: byte resBytes[] = new byte[rBytes.length];
106: resBytes = aNumber.toByteArray();
107: for (int i = 0; i < resBytes.length; i++) {
108: assertTrue(resBytes[i] == rBytes[i]);
109: }
110: assertEquals("incorrect sign", 1, aNumber.signum());
111: }
112:
113: /**
114: * Create a negative number from an array of bytes.
115: * The number fits in an array of integers.
116: */
117: public void testConstructorBytesNegative1() {
118: byte aBytes[] = { -12, 56, 100, -2, -76, 89, 45, 91, 3, -15,
119: 35, 26, 3, 91 };
120: byte rBytes[] = { -12, 56, 100, -2, -76, 89, 45, 91, 3, -15,
121: 35, 26, 3, 91 };
122: BigInteger aNumber = new BigInteger(aBytes);
123: byte resBytes[] = new byte[rBytes.length];
124: resBytes = aNumber.toByteArray();
125: for (int i = 0; i < resBytes.length; i++) {
126: assertTrue(resBytes[i] == rBytes[i]);
127: }
128: assertEquals("incorrect sign", -1, aNumber.signum());
129: }
130:
131: /**
132: * Create a negative number from an array of bytes.
133: * The number fits in an integer.
134: */
135: public void testConstructorBytesNegative2() {
136: byte aBytes[] = { -12, 56, 100 };
137: byte rBytes[] = { -12, 56, 100 };
138: BigInteger aNumber = new BigInteger(aBytes);
139: byte resBytes[] = new byte[rBytes.length];
140: resBytes = aNumber.toByteArray();
141: for (int i = 0; i < resBytes.length; i++) {
142: assertTrue(resBytes[i] == rBytes[i]);
143: }
144: assertEquals("incorrect sign", -1, aNumber.signum());
145: }
146:
147: /**
148: * Create a negative number from an array of bytes.
149: * The number of bytes is 4.
150: */
151: public void testConstructorBytesNegative3() {
152: byte aBytes[] = { -128, -12, 56, 100 };
153: byte rBytes[] = { -128, -12, 56, 100 };
154: BigInteger aNumber = new BigInteger(aBytes);
155: byte resBytes[] = new byte[rBytes.length];
156: resBytes = aNumber.toByteArray();
157: for (int i = 0; i < resBytes.length; i++) {
158: assertTrue(resBytes[i] == rBytes[i]);
159: }
160: assertEquals("incorrect sign", -1, aNumber.signum());
161: }
162:
163: /**
164: * Create a negative number from an array of bytes.
165: * The number of bytes is multiple of 4.
166: */
167: public void testConstructorBytesNegative4() {
168: byte aBytes[] = { -128, -12, 56, 100, -13, 56, 93, -78 };
169: byte rBytes[] = { -128, -12, 56, 100, -13, 56, 93, -78 };
170: BigInteger aNumber = new BigInteger(aBytes);
171: byte resBytes[] = new byte[rBytes.length];
172: resBytes = aNumber.toByteArray();
173: for (int i = 0; i < resBytes.length; i++) {
174: assertTrue(resBytes[i] == rBytes[i]);
175: }
176: assertEquals("incorrect sign", -1, aNumber.signum());
177: }
178:
179: /**
180: * Create a zero number from an array of zero bytes.
181: */
182: public void testConstructorBytesZero() {
183: byte aBytes[] = { 0, 0, 0, -0, +0, 0, -0 };
184: byte rBytes[] = { 0 };
185: BigInteger aNumber = new BigInteger(aBytes);
186: byte resBytes[] = new byte[rBytes.length];
187: resBytes = aNumber.toByteArray();
188: for (int i = 0; i < resBytes.length; i++) {
189: assertTrue(resBytes[i] == rBytes[i]);
190: }
191: assertEquals("incorrect sign", 0, aNumber.signum());
192: }
193:
194: /**
195: * Create a number from a sign and an array of bytes.
196: * Verify an exception thrown if a sign has improper value.
197: */
198: public void testConstructorSignBytesException1() {
199: byte aBytes[] = { 123, 45, -3, -76 };
200: int aSign = 3;
201: try {
202: new BigInteger(aSign, aBytes);
203: fail("NumberFormatException has not been caught");
204: } catch (NumberFormatException e) {
205: assertEquals("Improper exception message",
206: "Invalid signum value", e.getMessage());
207: }
208: }
209:
210: /**
211: * Create a number from a sign and an array of bytes.
212: * Verify an exception thrown if the array contains non-zero bytes while the sign is 0.
213: */
214: public void testConstructorSignBytesException2() {
215: byte aBytes[] = { 123, 45, -3, -76 };
216: int aSign = 0;
217: try {
218: new BigInteger(aSign, aBytes);
219: fail("NumberFormatException has not been caught");
220: } catch (NumberFormatException e) {
221: assertEquals("Improper exception message",
222: "signum-magnitude mismatch", e.getMessage());
223: }
224: }
225:
226: /**
227: * Create a positive number from a sign and an array of bytes.
228: * The number fits in an array of integers.
229: * The most significant byte is positive.
230: */
231: public void testConstructorSignBytesPositive1() {
232: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15 };
233: int aSign = 1;
234: byte rBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15 };
235: BigInteger aNumber = new BigInteger(aSign, aBytes);
236: byte resBytes[] = new byte[rBytes.length];
237: resBytes = aNumber.toByteArray();
238: for (int i = 0; i < resBytes.length; i++) {
239: assertTrue(resBytes[i] == rBytes[i]);
240: }
241: assertEquals("incorrect sign", 1, aNumber.signum());
242: }
243:
244: /**
245: * Create a positive number from a sign and an array of bytes.
246: * The number fits in an array of integers.
247: * The most significant byte is negative.
248: */
249: public void testConstructorSignBytesPositive2() {
250: byte aBytes[] = { -12, 56, 100, -2, -76, 89, 45, 91, 3, -15 };
251: int aSign = 1;
252: byte rBytes[] = { 0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15 };
253: BigInteger aNumber = new BigInteger(aSign, aBytes);
254: byte resBytes[] = new byte[rBytes.length];
255: resBytes = aNumber.toByteArray();
256: for (int i = 0; i < resBytes.length; i++) {
257: assertTrue(resBytes[i] == rBytes[i]);
258: }
259: assertEquals("incorrect sign", 1, aNumber.signum());
260: }
261:
262: /**
263: * Create a positive number from a sign and an array of bytes.
264: * The number fits in an integer.
265: */
266: public void testConstructorSignBytesPositive3() {
267: byte aBytes[] = { -12, 56, 100 };
268: int aSign = 1;
269: byte rBytes[] = { 0, -12, 56, 100 };
270: BigInteger aNumber = new BigInteger(aSign, aBytes);
271: byte resBytes[] = new byte[rBytes.length];
272: resBytes = aNumber.toByteArray();
273: for (int i = 0; i < resBytes.length; i++) {
274: assertTrue(resBytes[i] == rBytes[i]);
275: }
276: assertEquals("incorrect sign", 1, aNumber.signum());
277: }
278:
279: /**
280: * Create a positive number from a sign and an array of bytes.
281: * The number of bytes is 4.
282: * The most significant byte is positive.
283: */
284: public void testConstructorSignBytesPositive4() {
285: byte aBytes[] = { 127, 56, 100, -2 };
286: int aSign = 1;
287: byte rBytes[] = { 127, 56, 100, -2 };
288: BigInteger aNumber = new BigInteger(aSign, aBytes);
289: byte resBytes[] = new byte[rBytes.length];
290: resBytes = aNumber.toByteArray();
291: for (int i = 0; i < resBytes.length; i++) {
292: assertTrue(resBytes[i] == rBytes[i]);
293: }
294: assertEquals("incorrect sign", 1, aNumber.signum());
295: }
296:
297: /**
298: * Create a positive number from a sign and an array of bytes.
299: * The number of bytes is 4.
300: * The most significant byte is negative.
301: */
302: public void testConstructorSignBytesPositive5() {
303: byte aBytes[] = { -127, 56, 100, -2 };
304: int aSign = 1;
305: byte rBytes[] = { 0, -127, 56, 100, -2 };
306: BigInteger aNumber = new BigInteger(aSign, aBytes);
307: byte resBytes[] = new byte[rBytes.length];
308: resBytes = aNumber.toByteArray();
309: for (int i = 0; i < resBytes.length; i++) {
310: assertTrue(resBytes[i] == rBytes[i]);
311: }
312: assertEquals("incorrect sign", 1, aNumber.signum());
313: }
314:
315: /**
316: * Create a positive number from a sign and an array of bytes.
317: * The number of bytes is multiple of 4.
318: * The most significant byte is positive.
319: */
320: public void testConstructorSignBytesPositive6() {
321: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23,
322: -101 };
323: int aSign = 1;
324: byte rBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23,
325: -101 };
326: BigInteger aNumber = new BigInteger(aSign, aBytes);
327: byte resBytes[] = new byte[rBytes.length];
328: resBytes = aNumber.toByteArray();
329: for (int i = 0; i < resBytes.length; i++) {
330: assertTrue(resBytes[i] == rBytes[i]);
331: }
332: assertEquals("incorrect sign", 1, aNumber.signum());
333: }
334:
335: /**
336: * Create a positive number from a sign and an array of bytes.
337: * The number of bytes is multiple of 4.
338: * The most significant byte is negative.
339: */
340: public void testConstructorSignBytesPositive7() {
341: byte aBytes[] = { -12, 56, 100, -2, -76, 89, 45, 91, 3, -15,
342: 23, -101 };
343: int aSign = 1;
344: byte rBytes[] = { 0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15,
345: 23, -101 };
346: BigInteger aNumber = new BigInteger(aSign, aBytes);
347: byte resBytes[] = new byte[rBytes.length];
348: resBytes = aNumber.toByteArray();
349: for (int i = 0; i < resBytes.length; i++) {
350: assertTrue(resBytes[i] == rBytes[i]);
351: }
352: assertEquals("incorrect sign", 1, aNumber.signum());
353: }
354:
355: /**
356: * Create a negative number from a sign and an array of bytes.
357: * The number fits in an array of integers.
358: * The most significant byte is positive.
359: */
360: public void testConstructorSignBytesNegative1() {
361: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15 };
362: int aSign = -1;
363: byte rBytes[] = { -13, -57, -101, 1, 75, -90, -46, -92, -4, 15 };
364: BigInteger aNumber = new BigInteger(aSign, aBytes);
365: byte resBytes[] = new byte[rBytes.length];
366: resBytes = aNumber.toByteArray();
367: for (int i = 0; i < resBytes.length; i++) {
368: assertTrue(resBytes[i] == rBytes[i]);
369: }
370: assertEquals("incorrect sign", -1, aNumber.signum());
371: }
372:
373: /**
374: * Create a negative number from a sign and an array of bytes.
375: * The number fits in an array of integers.
376: * The most significant byte is negative.
377: */
378: public void testConstructorSignBytesNegative2() {
379: byte aBytes[] = { -12, 56, 100, -2, -76, 89, 45, 91, 3, -15 };
380: int aSign = -1;
381: byte rBytes[] = { -1, 11, -57, -101, 1, 75, -90, -46, -92, -4,
382: 15 };
383: BigInteger aNumber = new BigInteger(aSign, aBytes);
384: byte resBytes[] = new byte[rBytes.length];
385: resBytes = aNumber.toByteArray();
386: for (int i = 0; i < resBytes.length; i++) {
387: assertTrue(resBytes[i] == rBytes[i]);
388: }
389: assertEquals("incorrect sign", -1, aNumber.signum());
390: }
391:
392: /**
393: * Create a negative number from a sign and an array of bytes.
394: * The number fits in an integer.
395: */
396: public void testConstructorSignBytesNegative3() {
397: byte aBytes[] = { -12, 56, 100 };
398: int aSign = -1;
399: byte rBytes[] = { -1, 11, -57, -100 };
400: BigInteger aNumber = new BigInteger(aSign, aBytes);
401: byte resBytes[] = new byte[rBytes.length];
402: resBytes = aNumber.toByteArray();
403: for (int i = 0; i < resBytes.length; i++) {
404: assertTrue(resBytes[i] == rBytes[i]);
405: }
406: assertEquals("incorrect sign", -1, aNumber.signum());
407: }
408:
409: /**
410: * Create a negative number from a sign and an array of bytes.
411: * The number of bytes is 4.
412: * The most significant byte is positive.
413: */
414: public void testConstructorSignBytesNegative4() {
415: byte aBytes[] = { 127, 56, 100, -2 };
416: int aSign = -1;
417: byte rBytes[] = { -128, -57, -101, 2 };
418: BigInteger aNumber = new BigInteger(aSign, aBytes);
419: byte resBytes[] = new byte[rBytes.length];
420: resBytes = aNumber.toByteArray();
421: for (int i = 0; i < resBytes.length; i++) {
422: assertTrue(resBytes[i] == rBytes[i]);
423: }
424: assertEquals("incorrect sign", -1, aNumber.signum());
425: }
426:
427: /**
428: * Create a negative number from a sign and an array of bytes.
429: * The number of bytes is 4.
430: * The most significant byte is negative.
431: */
432: public void testConstructorSignBytesNegative5() {
433: byte aBytes[] = { -127, 56, 100, -2 };
434: int aSign = -1;
435: byte rBytes[] = { -1, 126, -57, -101, 2 };
436: BigInteger aNumber = new BigInteger(aSign, aBytes);
437: byte resBytes[] = new byte[rBytes.length];
438: resBytes = aNumber.toByteArray();
439: for (int i = 0; i < resBytes.length; i++) {
440: assertTrue(resBytes[i] == rBytes[i]);
441: }
442: assertEquals("incorrect sign", -1, aNumber.signum());
443: }
444:
445: /**
446: * Create a negative number from a sign and an array of bytes.
447: * The number of bytes is multiple of 4.
448: * The most significant byte is positive.
449: */
450: public void testConstructorSignBytesNegative6() {
451: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23,
452: -101 };
453: int aSign = -1;
454: byte rBytes[] = { -13, -57, -101, 1, 75, -90, -46, -92, -4, 14,
455: -24, 101 };
456: BigInteger aNumber = new BigInteger(aSign, aBytes);
457: byte resBytes[] = new byte[rBytes.length];
458: resBytes = aNumber.toByteArray();
459: for (int i = 0; i < resBytes.length; i++) {
460: assertTrue(resBytes[i] == rBytes[i]);
461: }
462: assertEquals("incorrect sign", -1, aNumber.signum());
463: }
464:
465: /**
466: * Create a negative number from a sign and an array of bytes.
467: * The number of bytes is multiple of 4.
468: * The most significant byte is negative.
469: */
470: public void testConstructorSignBytesNegative7() {
471: byte aBytes[] = { -12, 56, 100, -2, -76, 89, 45, 91, 3, -15,
472: 23, -101 };
473: int aSign = -1;
474: byte rBytes[] = { -1, 11, -57, -101, 1, 75, -90, -46, -92, -4,
475: 14, -24, 101 };
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", -1, aNumber.signum());
483: }
484:
485: /**
486: * Create a zero number from a sign and an array of zero bytes.
487: * The sign is -1.
488: */
489: public void testConstructorSignBytesZero1() {
490: byte aBytes[] = { -0, 0, +0, 0, 0, 00, 000 };
491: int aSign = -1;
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 0.
505: */
506: public void testConstructorSignBytesZero2() {
507: byte aBytes[] = { -0, 0, +0, 0, 0, 00, 000 };
508: int aSign = 0;
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 bytes.
521: * The sign is 1.
522: */
523: public void testConstructorSignBytesZero3() {
524: byte aBytes[] = { -0, 0, +0, 0, 0, 00, 000 };
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 -1.
539: */
540: public void testConstructorSignBytesZeroNull1() {
541: byte aBytes[] = {};
542: int aSign = -1;
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 0.
556: */
557: public void testConstructorSignBytesZeroNull2() {
558: byte aBytes[] = {};
559: int aSign = 0;
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 zero number from a sign and an array of zero length.
572: * The sign is 1.
573: */
574: public void testConstructorSignBytesZeroNull3() {
575: byte aBytes[] = {};
576: int aSign = 1;
577: byte rBytes[] = { 0 };
578: BigInteger aNumber = new BigInteger(aSign, aBytes);
579: byte resBytes[] = new byte[rBytes.length];
580: resBytes = aNumber.toByteArray();
581: for (int i = 0; i < resBytes.length; i++) {
582: assertTrue(resBytes[i] == rBytes[i]);
583: }
584: assertEquals("incorrect sign", 0, aNumber.signum());
585: }
586:
587: /**
588: * Create a number from a string value and radix.
589: * Verify an exception thrown if a radix is out of range
590: */
591: public void testConstructorStringException1() {
592: String value = "9234853876401";
593: int radix = 45;
594: try {
595: new BigInteger(value, radix);
596: fail("NumberFormatException has not been caught");
597: } catch (NumberFormatException e) {
598: assertEquals("Improper exception message",
599: "Radix out of range", e.getMessage());
600: }
601: }
602:
603: /**
604: * Create a number from a string value and radix.
605: * Verify an exception thrown if the string starts with a space.
606: */
607: public void testConstructorStringException2() {
608: String value = " 9234853876401";
609: int radix = 10;
610: try {
611: new BigInteger(value, radix);
612: fail("NumberFormatException has not been caught");
613: } catch (NumberFormatException e) {
614: }
615: }
616:
617: /**
618: * Create a number from a string value and radix.
619: * Verify an exception thrown if the string contains improper characters.
620: */
621: public void testConstructorStringException3() {
622: String value = "92348$*#78987";
623: int radix = 34;
624: try {
625: new BigInteger(value, radix);
626: fail("NumberFormatException has not been caught");
627: } catch (NumberFormatException e) {
628: }
629: }
630:
631: /**
632: * Create a number from a string value and radix.
633: * Verify an exception thrown if some digits are greater than radix.
634: */
635: public void testConstructorStringException4() {
636: String value = "98zv765hdsaiy";
637: int radix = 20;
638: try {
639: new BigInteger(value, radix);
640: fail("NumberFormatException has not been caught");
641: } catch (NumberFormatException e) {
642: }
643: }
644:
645: /**
646: * Create a positive number from a string value and radix 2.
647: */
648: public void testConstructorStringRadix2() {
649: String value = "10101010101010101";
650: int radix = 2;
651: byte rBytes[] = { 1, 85, 85 };
652: BigInteger aNumber = new BigInteger(value, radix);
653: byte resBytes[] = new byte[rBytes.length];
654: resBytes = aNumber.toByteArray();
655: for (int i = 0; i < resBytes.length; i++) {
656: assertTrue(resBytes[i] == rBytes[i]);
657: }
658: assertEquals("incorrect sign", 1, aNumber.signum());
659: }
660:
661: /**
662: * Create a positive number from a string value and radix 8.
663: */
664: public void testConstructorStringRadix8() {
665: String value = "76356237071623450";
666: int radix = 8;
667: byte rBytes[] = { 7, -50, -28, -8, -25, 39, 40 };
668: BigInteger aNumber = new BigInteger(value, radix);
669: byte resBytes[] = new byte[rBytes.length];
670: resBytes = aNumber.toByteArray();
671: for (int i = 0; i < resBytes.length; i++) {
672: assertTrue(resBytes[i] == rBytes[i]);
673: }
674: assertEquals("incorrect sign", 1, aNumber.signum());
675: }
676:
677: /**
678: * Create a positive number from a string value and radix 10.
679: */
680: public void testConstructorStringRadix10() {
681: String value = "987328901348934898";
682: int radix = 10;
683: byte rBytes[] = { 13, -77, -78, 103, -103, 97, 68, -14 };
684: BigInteger aNumber = new BigInteger(value, radix);
685: byte resBytes[] = new byte[rBytes.length];
686: resBytes = aNumber.toByteArray();
687: for (int i = 0; i < resBytes.length; i++) {
688: assertTrue(resBytes[i] == rBytes[i]);
689: }
690: assertEquals("incorrect sign", 1, aNumber.signum());
691: }
692:
693: /**
694: * Create a positive number from a string value and radix 16.
695: */
696: public void testConstructorStringRadix16() {
697: String value = "fe2340a8b5ce790";
698: int radix = 16;
699: byte rBytes[] = { 15, -30, 52, 10, -117, 92, -25, -112 };
700: BigInteger aNumber = new BigInteger(value, radix);
701: byte resBytes[] = new byte[rBytes.length];
702: resBytes = aNumber.toByteArray();
703: for (int i = 0; i < resBytes.length; i++) {
704: assertTrue(resBytes[i] == rBytes[i]);
705: }
706: assertEquals("incorrect sign", 1, aNumber.signum());
707: }
708:
709: /**
710: * Create a positive number from a string value and radix 36.
711: */
712: public void testConstructorStringRadix36() {
713: String value = "skdjgocvhdjfkl20jndjkf347ejg457";
714: int radix = 36;
715: byte rBytes[] = { 0, -12, -116, 112, -105, 12, -36, 66, 108,
716: 66, -20, -37, -15, 108, -7, 52, -99, -109, -8, -45, -5 };
717: BigInteger aNumber = new BigInteger(value, radix);
718: byte resBytes[] = new byte[rBytes.length];
719: resBytes = aNumber.toByteArray();
720: for (int i = 0; i < resBytes.length; i++) {
721: assertTrue(resBytes[i] == rBytes[i]);
722: }
723: assertEquals("incorrect sign", 1, aNumber.signum());
724: }
725:
726: /**
727: * Create a negative number from a string value and radix 10.
728: */
729: public void testConstructorStringRadix10Negative() {
730: String value = "-234871376037";
731: int radix = 36;
732: byte rBytes[] = { -4, 48, 71, 62, -76, 93, -105, 13 };
733: BigInteger aNumber = new BigInteger(value, radix);
734: byte resBytes[] = new byte[rBytes.length];
735: resBytes = aNumber.toByteArray();
736: for (int i = 0; i < resBytes.length; i++) {
737: assertTrue(resBytes[i] == rBytes[i]);
738: }
739: assertEquals("incorrect sign", -1, aNumber.signum());
740: }
741:
742: /**
743: * Create a zero number from a string value and radix 36.
744: */
745: public void testConstructorStringRadix10Zero() {
746: String value = "-00000000000000";
747: int radix = 10;
748: byte rBytes[] = { 0 };
749: BigInteger aNumber = new BigInteger(value, radix);
750: byte resBytes[] = new byte[rBytes.length];
751: resBytes = aNumber.toByteArray();
752: for (int i = 0; i < resBytes.length; i++) {
753: assertTrue(resBytes[i] == rBytes[i]);
754: }
755: assertEquals("incorrect sign", 0, aNumber.signum());
756: }
757:
758: /**
759: * Create a random number of 75 bits length.
760: */
761: public void testConstructorRandom() {
762: int bitLen = 75;
763: Random rnd = new Random();
764: BigInteger aNumber = new BigInteger(bitLen, rnd);
765: assertTrue("incorrect bitLength", aNumber.bitLength() <= bitLen);
766: }
767:
768: /**
769: * Create a prime number of 25 bits length.
770: */
771: public void testConstructorPrime() {
772: int bitLen = 25;
773: Random rnd = new Random();
774: BigInteger aNumber = new BigInteger(bitLen, 80, rnd);
775: assertTrue("incorrect bitLength", aNumber.bitLength() == bitLen);
776: }
777:
778: /**
779: * Create a prime number of 2 bits length.
780: */
781: public void testConstructorPrime2() {
782: int bitLen = 2;
783: Random rnd = new Random();
784: BigInteger aNumber = new BigInteger(bitLen, 80, rnd);
785: assertTrue("incorrect bitLength", aNumber.bitLength() == bitLen);
786: int num = aNumber.intValue();
787: assertTrue("incorrect value", num == 2 || num == 3);
788: }
789: }
|