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:
025: /**
026: * Class: java.math.BigInteger
027: * Methods: intValue, longValue, toByteArray(), valueOf(long val),
028: * floatValue(), doubleValue()
029: */
030: public class BigIntegerConvertTest extends TestCase {
031: /**
032: * Return the double value of ZERO.
033: */
034: public void testDoubleValueZero() {
035: String a = "0";
036: double result = 0.0;
037: double aNumber = new BigInteger(a).doubleValue();
038: assertTrue(aNumber == result);
039: }
040:
041: /**
042: * Convert a positive number to a double value.
043: * The number's length is less than 64 bits.
044: */
045: public void testDoubleValuePositive1() {
046: String a = "27467238945";
047: double result = 2.7467238945E10;
048: double aNumber = new BigInteger(a).doubleValue();
049: assertTrue(aNumber == result);
050: }
051:
052: /**
053: * Convert a positive number to a double value.
054: * The number's bit length is inside [63, 1024].
055: */
056: public void testDoubleValuePositive2() {
057: String a = "2746723894572364578265426346273456972";
058: double result = 2.7467238945723645E36;
059: double aNumber = new BigInteger(a).doubleValue();
060: assertTrue(aNumber == result);
061: }
062:
063: /**
064: * Convert a negative number to a double value.
065: * The number's bit length is less than 64 bits.
066: */
067: public void testDoubleValueNegative1() {
068: String a = "-27467238945";
069: double result = -2.7467238945E10;
070: double aNumber = new BigInteger(a).doubleValue();
071: assertTrue(aNumber == result);
072: }
073:
074: /**
075: * Convert a negative number to a double value.
076: * The number's bit length is inside [63, 1024].
077: */
078: public void testDoubleValueNegative2() {
079: String a = "-2746723894572364578265426346273456972";
080: double result = -2.7467238945723645E36;
081: double aNumber = new BigInteger(a).doubleValue();
082: assertTrue(aNumber == result);
083: }
084:
085: /**
086: * Convert a positive number to a double value.
087: * Rounding is needed.
088: * The rounding bit is 1 and the next bit to the left is 1.
089: */
090: public void testDoubleValuePosRounded1() {
091: byte[] a = { -128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5 };
092: int aSign = 1;
093: double result = 1.54747264387948E26;
094: double aNumber = new BigInteger(aSign, a).doubleValue();
095: assertTrue(aNumber == result);
096: }
097:
098: /**
099: * Convert a positive number to a double value.
100: * Rounding is needed.
101: * The rounding bit is 1 and the next bit to the left is 0
102: * but some of dropped bits are 1s.
103: */
104: public void testDoubleValuePosRounded2() {
105: byte[] a = { -128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5 };
106: int aSign = 1;
107: double result = 1.547472643879479E26;
108: double aNumber = new BigInteger(aSign, a).doubleValue();
109: assertTrue(aNumber == result);
110: }
111:
112: /**
113: * Convert a positive number to a double value.
114: * Rounding is NOT needed.
115: */
116: public void testDoubleValuePosNotRounded() {
117: byte[] a = { -128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5 };
118: int aSign = 1;
119: double result = 1.5474726438794828E26;
120: double aNumber = new BigInteger(aSign, a).doubleValue();
121: assertTrue(aNumber == result);
122: }
123:
124: /**
125: * Convert a positive number to a double value.
126: * Rounding is needed.
127: */
128: public void testDoubleValueNegRounded1() {
129: byte[] a = { -128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5 };
130: int aSign = -1;
131: double result = -1.54747264387948E26;
132: double aNumber = new BigInteger(aSign, a).doubleValue();
133: assertTrue(aNumber == result);
134: }
135:
136: /**
137: * Convert a positive number to a double value.
138: * Rounding is needed.
139: * The rounding bit is 1 and the next bit to the left is 0
140: * but some of dropped bits are 1s.
141: */
142: public void testDoubleValueNegRounded2() {
143: byte[] a = { -128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5 };
144: int aSign = -1;
145: double result = -1.547472643879479E26;
146: double aNumber = new BigInteger(aSign, a).doubleValue();
147: assertTrue(aNumber == result);
148: }
149:
150: /**
151: * Convert a positive number to a double value.
152: * Rounding is NOT needed.
153: */
154: public void testDoubleValueNegNotRounded() {
155: byte[] a = { -128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5 };
156: int aSign = -1;
157: double result = -1.5474726438794828E26;
158: double aNumber = new BigInteger(aSign, a).doubleValue();
159: assertTrue(aNumber == result);
160: }
161:
162: /**
163: * Convert a positive number to a double value.
164: * The exponent is 1023 and the mantissa is all 1s.
165: * The rounding bit is 0.
166: * The result is Double.MAX_VALUE.
167: */
168: public void testDoubleValuePosMaxValue() {
169: byte[] a = { 0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1,
170: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
171: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
172: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
173: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
174: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
175: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
176: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
177: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
178: -1, -1, -1, -1 };
179: int aSign = 1;
180: double aNumber = new BigInteger(aSign, a).doubleValue();
181: assertTrue(aNumber == Double.MAX_VALUE);
182: }
183:
184: /**
185: * Convert a negative number to a double value.
186: * The exponent is 1023 and the mantissa is all 1s.
187: * The result is -Double.MAX_VALUE.
188: */
189: public void testDoubleValueNegMaxValue() {
190: byte[] a = { 0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1,
191: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
192: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
193: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
194: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
195: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
196: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
197: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
198: -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
199: -1, -1, -1, -1 };
200: int aSign = -1;
201: double aNumber = new BigInteger(aSign, a).doubleValue();
202: assertTrue(aNumber == -Double.MAX_VALUE);
203: }
204:
205: /**
206: * Convert a positive number to a double value.
207: * The exponent is 1023 and the mantissa is all 1s.
208: * The rounding bit is 1.
209: * The result is Double.POSITIVE_INFINITY.
210: */
211: public void testDoubleValuePositiveInfinity1() {
212: byte[] a = { -1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0,
213: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
214: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
215: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
216: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
217: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
218: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
219: 0, 0, 0, 0, 0, 0 };
220: int aSign = 1;
221: double aNumber = new BigInteger(aSign, a).doubleValue();
222: assertTrue(aNumber == Double.POSITIVE_INFINITY);
223: }
224:
225: /**
226: * Convert a positive number to a double value.
227: * The number's bit length is greater than 1024.
228: */
229: public void testDoubleValuePositiveInfinity2() {
230: String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
231: double aNumber = new BigInteger(a).doubleValue();
232: assertTrue(aNumber == Double.POSITIVE_INFINITY);
233: }
234:
235: /**
236: * Convert a negative number to a double value.
237: * The number's bit length is greater than 1024.
238: */
239: public void testDoubleValueNegativeInfinity1() {
240: String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
241: double aNumber = new BigInteger(a).doubleValue();
242: assertTrue(aNumber == Double.NEGATIVE_INFINITY);
243: }
244:
245: /**
246: * Convert a negative number to a double value.
247: * The exponent is 1023 and the mantissa is all 0s.
248: * The rounding bit is 0.
249: * The result is Double.NEGATIVE_INFINITY.
250: */
251: public void testDoubleValueNegativeInfinity2() {
252: byte[] a = { -1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0,
253: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
254: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
255: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
256: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
257: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
258: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
259: 0, 0, 0, 0, 0, 0 };
260: int aSign = -1;
261: double aNumber = new BigInteger(aSign, a).doubleValue();
262: assertTrue(aNumber == Double.NEGATIVE_INFINITY);
263: }
264:
265: /**
266: * Convert a positive number to a double value.
267: * The exponent is 1023 and the mantissa is all 0s
268: * but the 54th bit (implicit) is 1.
269: */
270: public void testDoubleValuePosMantissaIsZero() {
271: byte[] a = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
272: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
273: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
274: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
275: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
276: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
277: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
278: 0, 0, 0, 0 };
279: int aSign = 1;
280: double result = 8.98846567431158E307;
281: double aNumber = new BigInteger(aSign, a).doubleValue();
282: assertTrue(aNumber == result);
283: }
284:
285: /**
286: * Convert a positive number to a double value.
287: * The exponent is 1023 and the mantissa is all 0s
288: * but the 54th bit (implicit) is 1.
289: */
290: public void testDoubleValueNegMantissaIsZero() {
291: byte[] a = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
292: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
293: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
294: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
295: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
296: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
297: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
298: 0, 0, 0, 0 };
299: int aSign = -1;
300: double aNumber = new BigInteger(aSign, a).doubleValue();
301: assertTrue(aNumber == -8.98846567431158E307);
302: }
303:
304: /**
305: * Return the float value of ZERO.
306: */
307: public void testFloatValueZero() {
308: String a = "0";
309: float result = 0.0f;
310: float aNumber = new BigInteger(a).floatValue();
311: assertTrue(aNumber == result);
312: }
313:
314: /**
315: * Convert a positive number to a float value.
316: * The number's length is less than 32 bits.
317: */
318: public void testFloatValuePositive1() {
319: String a = "27467238";
320: float result = 2.7467238E7f;
321: float aNumber = new BigInteger(a).floatValue();
322: assertTrue(aNumber == result);
323: }
324:
325: /**
326: * Convert a positive number to a float value.
327: * The number's bit length is inside [32, 127].
328: */
329: public void testFloatValuePositive2() {
330: String a = "27467238945723645782";
331: float result = 2.7467239E19f;
332: float aNumber = new BigInteger(a).floatValue();
333: assertTrue(aNumber == result);
334: }
335:
336: /**
337: * Convert a negative number to a float value.
338: * The number's bit length is less than 32 bits.
339: */
340: public void testFloatValueNegative1() {
341: String a = "-27467238";
342: float result = -2.7467238E7f;
343: float aNumber = new BigInteger(a).floatValue();
344: assertTrue(aNumber == result);
345: }
346:
347: /**
348: * Convert a negative number to a doufloatble value.
349: * The number's bit length is inside [63, 1024].
350: */
351: public void testFloatValueNegative2() {
352: String a = "-27467238945723645782";
353: float result = -2.7467239E19f;
354: float aNumber = new BigInteger(a).floatValue();
355: assertTrue(aNumber == result);
356: }
357:
358: /**
359: * Convert a positive number to a float value.
360: * Rounding is needed.
361: * The rounding bit is 1 and the next bit to the left is 1.
362: */
363: public void testFloatValuePosRounded1() {
364: byte[] a = { -128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5 };
365: int aSign = 1;
366: float result = 1.5475195E26f;
367: float aNumber = new BigInteger(aSign, a).floatValue();
368: assertTrue(aNumber == result);
369: }
370:
371: /**
372: * Convert a positive number to a float value.
373: * Rounding is needed.
374: * The rounding bit is 1 and the next bit to the left is 0
375: * but some of dropped bits are 1s.
376: */
377: public void testFloatValuePosRounded2() {
378: byte[] a = { -128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5 };
379: int aSign = 1;
380: float result = 1.5474728E26f;
381: float aNumber = new BigInteger(aSign, a).floatValue();
382: assertTrue(aNumber == result);
383: }
384:
385: /**
386: * Convert a positive number to a float value.
387: * Rounding is NOT needed.
388: */
389: public void testFloatValuePosNotRounded() {
390: byte[] a = { -128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5 };
391: int aSign = 1;
392: float result = 1.5474726E26f;
393: float aNumber = new BigInteger(aSign, a).floatValue();
394: assertTrue(aNumber == result);
395: }
396:
397: /**
398: * Convert a positive number to a float value.
399: * Rounding is needed.
400: */
401: public void testFloatValueNegRounded1() {
402: byte[] a = { -128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5 };
403: int aSign = -1;
404: float result = -1.5475195E26f;
405: float aNumber = new BigInteger(aSign, a).floatValue();
406: assertTrue(aNumber == result);
407: }
408:
409: /**
410: * Convert a positive number to a float value.
411: * Rounding is needed.
412: * The rounding bit is 1 and the next bit to the left is 0
413: * but some of dropped bits are 1s.
414: */
415: public void testFloatValueNegRounded2() {
416: byte[] a = { -128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5 };
417: int aSign = -1;
418: float result = -1.5474728E26f;
419: float aNumber = new BigInteger(aSign, a).floatValue();
420: assertTrue(aNumber == result);
421: }
422:
423: /**
424: * Convert a positive number to a float value.
425: * Rounding is NOT needed.
426: */
427: public void testFloatValueNegNotRounded() {
428: byte[] a = { -128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5 };
429: int aSign = -1;
430: float result = -1.5474726E26f;
431: float aNumber = new BigInteger(aSign, a).floatValue();
432: assertTrue(aNumber == result);
433: }
434:
435: /**
436: * Convert a positive number to a float value.
437: * The exponent is 1023 and the mantissa is all 1s.
438: * The rounding bit is 0.
439: * The result is Float.MAX_VALUE.
440: */
441: public void testFloatValuePosMaxValue() {
442: byte[] a = { 0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1,
443: -1, -1, -1, -1 };
444: int aSign = 1;
445: float aNumber = new BigInteger(aSign, a).floatValue();
446: assertTrue(aNumber == Float.MAX_VALUE);
447: }
448:
449: /**
450: * Convert a negative number to a float value.
451: * The exponent is 1023 and the mantissa is all 1s.
452: * The rounding bit is 0.
453: * The result is -Float.MAX_VALUE.
454: */
455: public void testFloatValueNegMaxValue() {
456: byte[] a = { 0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1,
457: -1, -1, -1, -1 };
458: int aSign = -1;
459: float aNumber = new BigInteger(aSign, a).floatValue();
460: assertTrue(aNumber == -Float.MAX_VALUE);
461: }
462:
463: /**
464: * Convert a positive number to a float value.
465: * The exponent is 1023 and the mantissa is all 1s.
466: * The rounding bit is 1.
467: * The result is Float.POSITIVE_INFINITY.
468: */
469: public void testFloatValuePositiveInfinity1() {
470: byte[] a = { 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
471: -1, -1, -1, -1 };
472: int aSign = 1;
473: float aNumber = new BigInteger(aSign, a).floatValue();
474: assertTrue(aNumber == Float.POSITIVE_INFINITY);
475: }
476:
477: /**
478: * Convert a positive number to a float value.
479: * The number's bit length is greater than 127.
480: */
481: public void testFloatValuePositiveInfinity2() {
482: String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
483: float aNumber = new BigInteger(a).floatValue();
484: assertTrue(aNumber == Float.POSITIVE_INFINITY);
485: }
486:
487: /**
488: * Convert a negative number to a float value.
489: * The number's bit length is greater than 127.
490: */
491: public void testFloatValueNegativeInfinity1() {
492: String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
493: float aNumber = new BigInteger(a).floatValue();
494: assertTrue(aNumber == Float.NEGATIVE_INFINITY);
495: }
496:
497: /**
498: * Convert a negative number to a float value.
499: * The exponent is 1023 and the mantissa is all 0s.
500: * The rounding bit is 0.
501: * The result is Float.NEGATIVE_INFINITY.
502: */
503: public void testFloatValueNegativeInfinity2() {
504: byte[] a = { 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
505: -1, -1, -1, -1 };
506: int aSign = -1;
507: float aNumber = new BigInteger(aSign, a).floatValue();
508: assertTrue(aNumber == Float.NEGATIVE_INFINITY);
509: }
510:
511: /**
512: * Convert a positive number to a float value.
513: * The exponent is 1023 and the mantissa is all 0s
514: * but the 54th bit (implicit) is 1.
515: */
516: public void testFloatValuePosMantissaIsZero() {
517: byte[] a = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
518: int aSign = 1;
519: float result = 1.7014118E38f;
520: float aNumber = new BigInteger(aSign, a).floatValue();
521: assertTrue(aNumber == result);
522: }
523:
524: /**
525: * Convert a positive number to a double value.
526: * The exponent is 1023 and the mantissa is all 0s
527: * but the 54th bit (implicit) is 1.
528: */
529: public void testFloatValueNegMantissaIsZero() {
530: byte[] a = { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
531: int aSign = -1;
532: float aNumber = new BigInteger(aSign, a).floatValue();
533: assertTrue(aNumber == Float.NEGATIVE_INFINITY);
534: }
535:
536: /**
537: * Convert a negative number to a float value.
538: * The number's bit length is less than 32 bits.
539: */
540: public void testFloatValueBug2482() {
541: String a = "2147483649";
542: float result = 2.14748365E9f;
543: float aNumber = new BigInteger(a).floatValue();
544: assertTrue(aNumber == result);
545: }
546:
547: /**
548: * Convert a positive BigInteger to an integer value.
549: * The low digit is positive
550: */
551: public void testIntValuePositive1() {
552: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3 };
553: int resInt = 1496144643;
554: int aNumber = new BigInteger(aBytes).intValue();
555: assertTrue(aNumber == resInt);
556: }
557:
558: /**
559: * Convert a positive BigInteger to an integer value.
560: * The low digit is positive
561: */
562: public void testIntValuePositive2() {
563: byte aBytes[] = { 12, 56, 100 };
564: int resInt = 800868;
565: int aNumber = new BigInteger(aBytes).intValue();
566: assertTrue(aNumber == resInt);
567: }
568:
569: /**
570: * Convert a positive BigInteger to an integer value.
571: * The low digit is negative.
572: */
573: public void testIntValuePositive3() {
574: byte aBytes[] = { 56, 13, 78, -12, -5, 56, 100 };
575: int sign = 1;
576: int resInt = -184862620;
577: int aNumber = new BigInteger(sign, aBytes).intValue();
578: assertTrue(aNumber == resInt);
579: }
580:
581: /**
582: * Convert a negative BigInteger to an integer value.
583: * The low digit is negative.
584: */
585: public void testIntValueNegative1() {
586: byte aBytes[] = { 12, 56, 100, -2, -76, -128, 45, 91, 3 };
587: int sign = -1;
588: int resInt = 2144511229;
589: int aNumber = new BigInteger(sign, aBytes).intValue();
590: assertTrue(aNumber == resInt);
591: }
592:
593: /**
594: * Convert a negative BigInteger to an integer value.
595: * The low digit is negative.
596: */
597: public void testIntValueNegative2() {
598: byte aBytes[] = { -12, 56, 100 };
599: int result = -771996;
600: int aNumber = new BigInteger(aBytes).intValue();
601: assertTrue(aNumber == result);
602: }
603:
604: /**
605: * Convert a negative BigInteger to an integer value.
606: * The low digit is positive.
607: */
608: public void testIntValueNegative3() {
609: byte aBytes[] = { 12, 56, 100, -2, -76, 127, 45, 91, 3 };
610: int sign = -1;
611: int resInt = -2133678851;
612: int aNumber = new BigInteger(sign, aBytes).intValue();
613: assertTrue(aNumber == resInt);
614: }
615:
616: /**
617: * Convert a BigInteger to a positive long value
618: * The BigInteger is longer than int.
619: */
620: public void testLongValuePositive1() {
621: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, 120,
622: -34, -12, 45, 98 };
623: long result = 3268209772258930018L;
624: long aNumber = new BigInteger(aBytes).longValue();
625: assertTrue(aNumber == result);
626: }
627:
628: /**
629: * Convert a number to a positive long value
630: * The number fits in a long.
631: */
632: public void testLongValuePositive2() {
633: byte aBytes[] = { 12, 56, 100, 18, -105, 34, -18, 45 };
634: long result = 880563758158769709L;
635: long aNumber = new BigInteger(aBytes).longValue();
636: assertTrue(aNumber == result);
637: }
638:
639: /**
640: * Convert a number to a negative long value
641: * The BigInteger is longer than int.
642: */
643: public void testLongValueNegative1() {
644: byte aBytes[] = { 12, -1, 100, -2, -76, -128, 45, 91, 3 };
645: long result = -43630045168837885L;
646: long aNumber = new BigInteger(aBytes).longValue();
647: assertTrue(aNumber == result);
648: }
649:
650: /**
651: * Convert a number to a negative long value
652: * The number fits in a long.
653: */
654: public void testLongValueNegative2() {
655: byte aBytes[] = { -12, 56, 100, 45, -101, 45, 98 };
656: long result = -3315696807498398L;
657: long aNumber = new BigInteger(aBytes).longValue();
658: assertTrue(aNumber == result);
659: }
660:
661: /**
662: * valueOf (long val): convert Integer.MAX_VALUE to a BigInteger.
663: */
664: public void testValueOfIntegerMax() {
665: long longVal = Integer.MAX_VALUE;
666: BigInteger aNumber = BigInteger.valueOf(longVal);
667: byte rBytes[] = { 127, -1, -1, -1 };
668: byte resBytes[] = new byte[rBytes.length];
669: resBytes = aNumber.toByteArray();
670: for (int i = 0; i < resBytes.length; i++) {
671: assertTrue(resBytes[i] == rBytes[i]);
672: }
673: assertEquals("incorrect sign", 1, aNumber.signum());
674: }
675:
676: /**
677: * valueOf (long val): convert Integer.MIN_VALUE to a BigInteger.
678: */
679: public void testValueOfIntegerMin() {
680: long longVal = Integer.MIN_VALUE;
681: BigInteger aNumber = BigInteger.valueOf(longVal);
682: byte rBytes[] = { -128, 0, 0, 0 };
683: byte resBytes[] = new byte[rBytes.length];
684: resBytes = aNumber.toByteArray();
685: for (int i = 0; i < resBytes.length; i++) {
686: assertTrue(resBytes[i] == rBytes[i]);
687: }
688: assertEquals("incorrect sign", -1, aNumber.signum());
689: }
690:
691: /**
692: * valueOf (long val): convert Long.MAX_VALUE to a BigInteger.
693: */
694: public void testValueOfLongMax() {
695: long longVal = Long.MAX_VALUE;
696: BigInteger aNumber = BigInteger.valueOf(longVal);
697: byte rBytes[] = { 127, -1, -1, -1, -1, -1, -1, -1 };
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: * valueOf (long val): convert Long.MIN_VALUE to a BigInteger.
708: */
709: public void testValueOfLongMin() {
710: long longVal = Long.MIN_VALUE;
711: BigInteger aNumber = BigInteger.valueOf(longVal);
712: byte rBytes[] = { -128, 0, 0, 0, 0, 0, 0, 0 };
713: byte resBytes[] = new byte[rBytes.length];
714: resBytes = aNumber.toByteArray();
715: for (int i = 0; i < resBytes.length; i++) {
716: assertTrue(resBytes[i] == rBytes[i]);
717: }
718: assertEquals("incorrect sign", -1, aNumber.signum());
719: }
720:
721: /**
722: * valueOf (long val): convert a positive long value to a BigInteger.
723: */
724: public void testValueOfLongPositive1() {
725: long longVal = 268209772258930018L;
726: BigInteger aNumber = BigInteger.valueOf(longVal);
727: byte rBytes[] = { 3, -72, -33, 93, -24, -56, 45, 98 };
728: byte resBytes[] = new byte[rBytes.length];
729: resBytes = aNumber.toByteArray();
730: for (int i = 0; i < resBytes.length; i++) {
731: assertTrue(resBytes[i] == rBytes[i]);
732: }
733: assertEquals("incorrect sign", 1, aNumber.signum());
734: }
735:
736: /**
737: * valueOf (long val): convert a positive long value to a BigInteger.
738: * The long value fits in integer.
739: */
740: public void testValueOfLongPositive2() {
741: long longVal = 58930018L;
742: BigInteger aNumber = BigInteger.valueOf(longVal);
743: byte rBytes[] = { 3, -125, 51, 98 };
744: byte resBytes[] = new byte[rBytes.length];
745: resBytes = aNumber.toByteArray();
746: for (int i = 0; i < resBytes.length; i++) {
747: assertTrue(resBytes[i] == rBytes[i]);
748: }
749: assertEquals("incorrect sign", 1, aNumber.signum());
750: }
751:
752: /**
753: * valueOf (long val): convert a negative long value to a BigInteger.
754: */
755: public void testValueOfLongNegative1() {
756: long longVal = -268209772258930018L;
757: BigInteger aNumber = BigInteger.valueOf(longVal);
758: byte rBytes[] = { -4, 71, 32, -94, 23, 55, -46, -98 };
759: byte resBytes[] = new byte[rBytes.length];
760: resBytes = aNumber.toByteArray();
761: for (int i = 0; i < resBytes.length; i++) {
762: assertTrue(resBytes[i] == rBytes[i]);
763: }
764: assertEquals("incorrect sign", -1, aNumber.signum());
765: }
766:
767: /**
768: * valueOf (long val): convert a negative long value to a BigInteger.
769: * The long value fits in integer.
770: */
771: public void testValueOfLongNegative2() {
772: long longVal = -58930018L;
773: BigInteger aNumber = BigInteger.valueOf(longVal);
774: byte rBytes[] = { -4, 124, -52, -98 };
775: byte resBytes[] = new byte[rBytes.length];
776: resBytes = aNumber.toByteArray();
777: for (int i = 0; i < resBytes.length; i++) {
778: assertTrue(resBytes[i] == rBytes[i]);
779: }
780: assertEquals("incorrect sign", -1, aNumber.signum());
781: }
782:
783: /**
784: * valueOf (long val): convert a zero long value to a BigInteger.
785: */
786: public void testValueOfLongZero() {
787: long longVal = 0L;
788: BigInteger aNumber = BigInteger.valueOf(longVal);
789: byte rBytes[] = { 0 };
790: byte resBytes[] = new byte[rBytes.length];
791: resBytes = aNumber.toByteArray();
792: for (int i = 0; i < resBytes.length; i++) {
793: assertTrue(resBytes[i] == rBytes[i]);
794: }
795: assertEquals("incorrect sign", 0, aNumber.signum());
796: }
797: }
|