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.BigDecimal;
024: import java.math.BigInteger;
025:
026: /**
027: * Class: java.math.BigDecimal
028: * Methods: doubleValue, floatValue, intValue, longValue,
029: * valueOf, toString, toBigInteger
030: */
031: public class BigDecimalConvertTest extends TestCase {
032: /**
033: * Double value of a negative BigDecimal
034: */
035: public void testDoubleValueNeg() {
036: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
037: BigDecimal aNumber = new BigDecimal(a);
038: double result = -1.2380964839238476E53;
039: assertEquals("incorrect value", result, aNumber.doubleValue(),
040: 0);
041: }
042:
043: /**
044: * Double value of a positive BigDecimal
045: */
046: public void testDoubleValuePos() {
047: String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
048: BigDecimal aNumber = new BigDecimal(a);
049: double result = 1.2380964839238476E53;
050: assertEquals("incorrect value", result, aNumber.doubleValue(),
051: 0);
052: }
053:
054: /**
055: * Double value of a large positive BigDecimal
056: */
057: public void testDoubleValuePosInfinity() {
058: String a = "123809648392384754573567356745735.63567890295784902768787678287E+400";
059: BigDecimal aNumber = new BigDecimal(a);
060: double result = Double.POSITIVE_INFINITY;
061: assertEquals("incorrect value", result, aNumber.doubleValue(),
062: 0);
063: }
064:
065: /**
066: * Double value of a large negative BigDecimal
067: */
068: public void testDoubleValueNegInfinity() {
069: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+400";
070: BigDecimal aNumber = new BigDecimal(a);
071: double result = Double.NEGATIVE_INFINITY;
072: assertEquals("incorrect value", result, aNumber.doubleValue(),
073: 0);
074: }
075:
076: /**
077: * Double value of a small negative BigDecimal
078: */
079: public void testDoubleValueMinusZero() {
080: String a = "-123809648392384754573567356745735.63567890295784902768787678287E-400";
081: BigDecimal aNumber = new BigDecimal(a);
082: long minusZero = -9223372036854775808L;
083: double result = aNumber.doubleValue();
084: assertTrue("incorrect value",
085: Double.doubleToLongBits(result) == minusZero);
086: }
087:
088: /**
089: * Double value of a small positive BigDecimal
090: */
091: public void testDoubleValuePlusZero() {
092: String a = "123809648392384754573567356745735.63567890295784902768787678287E-400";
093: BigDecimal aNumber = new BigDecimal(a);
094: long zero = 0;
095: double result = aNumber.doubleValue();
096: assertTrue("incorrect value",
097: Double.doubleToLongBits(result) == zero);
098: }
099:
100: /**
101: * Float value of a negative BigDecimal
102: */
103: public void testFloatValueNeg() {
104: String a = "-1238096483923847.6356789029578E+21";
105: BigDecimal aNumber = new BigDecimal(a);
106: float result = -1.2380965E36F;
107: assertTrue("incorrect value", aNumber.floatValue() == result);
108: }
109:
110: /**
111: * Float value of a positive BigDecimal
112: */
113: public void testFloatValuePos() {
114: String a = "1238096483923847.6356789029578E+21";
115: BigDecimal aNumber = new BigDecimal(a);
116: float result = 1.2380965E36F;
117: assertTrue("incorrect value", aNumber.floatValue() == result);
118: }
119:
120: /**
121: * Float value of a large positive BigDecimal
122: */
123: public void testFloatValuePosInfinity() {
124: String a = "123809648373567356745735.6356789787678287E+200";
125: BigDecimal aNumber = new BigDecimal(a);
126: float result = Float.POSITIVE_INFINITY;
127: assertTrue("incorrect value", aNumber.floatValue() == result);
128: }
129:
130: /**
131: * Float value of a large negative BigDecimal
132: */
133: public void testFloatValueNegInfinity() {
134: String a = "-123809648392384755735.63567887678287E+200";
135: BigDecimal aNumber = new BigDecimal(a);
136: float result = Float.NEGATIVE_INFINITY;
137: assertTrue("incorrect value", aNumber.floatValue() == result);
138: }
139:
140: /**
141: * Float value of a small negative BigDecimal
142: */
143: public void testFloatValueMinusZero() {
144: String a = "-123809648392384754573567356745735.63567890295784902768787678287E-400";
145: BigDecimal aNumber = new BigDecimal(a);
146: int minusZero = -2147483648;
147: float result = aNumber.floatValue();
148: assertTrue("incorrect value",
149: Float.floatToIntBits(result) == minusZero);
150: }
151:
152: /**
153: * Float value of a small positive BigDecimal
154: */
155: public void testFloatValuePlusZero() {
156: String a = "123809648392384754573567356745735.63567890295784902768787678287E-400";
157: BigDecimal aNumber = new BigDecimal(a);
158: int zero = 0;
159: float result = aNumber.floatValue();
160: assertTrue("incorrect value",
161: Float.floatToIntBits(result) == zero);
162: }
163:
164: /**
165: * Integer value of a negative BigDecimal
166: */
167: public void testIntValueNeg() {
168: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
169: BigDecimal aNumber = new BigDecimal(a);
170: int result = 218520473;
171: assertTrue("incorrect value", aNumber.intValue() == result);
172: }
173:
174: /**
175: * Integer value of a positive BigDecimal
176: */
177: public void testIntValuePos() {
178: String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
179: BigDecimal aNumber = new BigDecimal(a);
180: int result = -218520473;
181: assertTrue("incorrect value", aNumber.intValue() == result);
182: }
183:
184: /**
185: * Long value of a negative BigDecimal
186: */
187: public void testLongValueNeg() {
188: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
189: BigDecimal aNumber = new BigDecimal(a);
190: long result = -1246043477766677607L;
191: assertTrue("incorrect value", aNumber.longValue() == result);
192: }
193:
194: /**
195: * Long value of a positive BigDecimal
196: */
197: public void testLongValuePos() {
198: String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
199: BigDecimal aNumber = new BigDecimal(a);
200: long result = 1246043477766677607L;
201: assertTrue("incorrect value", aNumber.longValue() == result);
202: }
203:
204: /**
205: * scaleByPowerOfTen(int n)
206: */
207: public void testScaleByPowerOfTen1() {
208: String a = "1231212478987482988429808779810457634781384756794987";
209: int aScale = 13;
210: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
211: BigDecimal result = aNumber.scaleByPowerOfTen(10);
212: String res = "1231212478987482988429808779810457634781384756794.987";
213: int resScale = 3;
214: assertEquals("incorrect value", res, result.toString());
215: assertEquals("incorrect scale", resScale, result.scale());
216: }
217:
218: /**
219: * scaleByPowerOfTen(int n)
220: */
221: public void testScaleByPowerOfTen2() {
222: String a = "1231212478987482988429808779810457634781384756794987";
223: int aScale = -13;
224: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
225: BigDecimal result = aNumber.scaleByPowerOfTen(10);
226: String res = "1.231212478987482988429808779810457634781384756794987E+74";
227: int resScale = -23;
228: assertEquals("incorrect value", res, result.toString());
229: assertEquals("incorrect scale", resScale, result.scale());
230: }
231:
232: /**
233: * Convert a positive BigDecimal to BigInteger
234: */
235: public void testToBigIntegerPos1() {
236: String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
237: BigInteger bNumber = new BigInteger(
238: "123809648392384754573567356745735635678902957849027687");
239: BigDecimal aNumber = new BigDecimal(a);
240: BigInteger result = aNumber.toBigInteger();
241: assertTrue("incorrect value", result.equals(bNumber));
242: }
243:
244: /**
245: * Convert a positive BigDecimal to BigInteger
246: */
247: public void testToBigIntegerPos2() {
248: String a = "123809648392384754573567356745735.63567890295784902768787678287E+15";
249: BigInteger bNumber = new BigInteger(
250: "123809648392384754573567356745735635678902957849");
251: BigDecimal aNumber = new BigDecimal(a);
252: BigInteger result = aNumber.toBigInteger();
253: assertTrue("incorrect value", result.equals(bNumber));
254: }
255:
256: /**
257: * Convert a positive BigDecimal to BigInteger
258: */
259: public void testToBigIntegerPos3() {
260: String a = "123809648392384754573567356745735.63567890295784902768787678287E+45";
261: BigInteger bNumber = new BigInteger(
262: "123809648392384754573567356745735635678902957849027687876782870000000000000000");
263: BigDecimal aNumber = new BigDecimal(a);
264: BigInteger result = aNumber.toBigInteger();
265: assertTrue("incorrect value", result.equals(bNumber));
266: }
267:
268: /**
269: * Convert a negative BigDecimal to BigInteger
270: */
271: public void testToBigIntegerNeg1() {
272: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
273: BigInteger bNumber = new BigInteger(
274: "-123809648392384754573567356745735635678902957849027687");
275: BigDecimal aNumber = new BigDecimal(a);
276: BigInteger result = aNumber.toBigInteger();
277: assertTrue("incorrect value", result.equals(bNumber));
278: }
279:
280: /**
281: * Convert a negative BigDecimal to BigInteger
282: */
283: public void testToBigIntegerNeg2() {
284: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+15";
285: BigInteger bNumber = new BigInteger(
286: "-123809648392384754573567356745735635678902957849");
287: BigDecimal aNumber = new BigDecimal(a);
288: BigInteger result = aNumber.toBigInteger();
289: assertTrue("incorrect value", result.equals(bNumber));
290: }
291:
292: /**
293: * Convert a negative BigDecimal to BigInteger
294: */
295: public void testToBigIntegerNeg3() {
296: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+45";
297: BigInteger bNumber = new BigInteger(
298: "-123809648392384754573567356745735635678902957849027687876782870000000000000000");
299: BigDecimal aNumber = new BigDecimal(a);
300: BigInteger result = aNumber.toBigInteger();
301: assertTrue("incorrect value", result.equals(bNumber));
302: }
303:
304: /**
305: * Convert a small BigDecimal to BigInteger
306: */
307: public void testToBigIntegerZero() {
308: String a = "-123809648392384754573567356745735.63567890295784902768787678287E-500";
309: BigInteger bNumber = new BigInteger("0");
310: BigDecimal aNumber = new BigDecimal(a);
311: BigInteger result = aNumber.toBigInteger();
312: assertTrue("incorrect value", result.equals(bNumber));
313: }
314:
315: /**
316: * toBigIntegerExact()
317: */
318: public void testToBigIntegerExact1() {
319: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+45";
320: BigDecimal aNumber = new BigDecimal(a);
321: String res = "-123809648392384754573567356745735635678902957849027687876782870000000000000000";
322: BigInteger result = aNumber.toBigIntegerExact();
323: assertEquals("incorrect value", res, result.toString());
324: }
325:
326: /**
327: * toBigIntegerExact()
328: */
329: public void testToBigIntegerExactException() {
330: String a = "-123809648392384754573567356745735.63567890295784902768787678287E-10";
331: BigDecimal aNumber = new BigDecimal(a);
332: try {
333: aNumber.toBigIntegerExact();
334: fail("java.lang.ArithmeticException has not been thrown");
335: } catch (java.lang.ArithmeticException e) {
336: return;
337: }
338: }
339:
340: /**
341: * Convert a positive BigDecimal to an engineering string representation
342: */
343: public void testToEngineeringStringPos() {
344: String a = "123809648392384754573567356745735.63567890295784902768787678287E-501";
345: BigDecimal aNumber = new BigDecimal(a);
346: String result = "123.80964839238475457356735674573563567890295784902768787678287E-471";
347: assertEquals("incorrect value", result, aNumber
348: .toEngineeringString());
349: }
350:
351: /**
352: * Convert a negative BigDecimal to an engineering string representation
353: */
354: public void testToEngineeringStringNeg() {
355: String a = "-123809648392384754573567356745735.63567890295784902768787678287E-501";
356: BigDecimal aNumber = new BigDecimal(a);
357: String result = "-123.80964839238475457356735674573563567890295784902768787678287E-471";
358: assertEquals("incorrect value", result, aNumber
359: .toEngineeringString());
360: }
361:
362: /**
363: * Convert a negative BigDecimal to an engineering string representation
364: */
365: public void testToEngineeringStringZeroPosExponent() {
366: String a = "0.0E+16";
367: BigDecimal aNumber = new BigDecimal(a);
368: String result = "0E+15";
369: assertEquals("incorrect value", result, aNumber
370: .toEngineeringString());
371: }
372:
373: /**
374: * Convert a negative BigDecimal to an engineering string representation
375: */
376: public void testToEngineeringStringZeroNegExponent() {
377: String a = "0.0E-16";
378: BigDecimal aNumber = new BigDecimal(a);
379: String result = "0.00E-15";
380: assertEquals("incorrect value", result, aNumber
381: .toEngineeringString());
382: }
383:
384: /**
385: * Convert a negative BigDecimal with a negative exponent to a plain string
386: * representation; scale == 0.
387: */
388: public void testToPlainStringNegNegExp() {
389: String a = "-123809648392384754573567356745735.63567890295784902768787678287E-100";
390: BigDecimal aNumber = new BigDecimal(a);
391: String result = "-0.000000000000000000000000000000000000000000000000000000000000000000012380964839238475457356735674573563567890295784902768787678287";
392: assertTrue("incorrect value", aNumber.toPlainString().equals(
393: result));
394: }
395:
396: /**
397: * Convert a negative BigDecimal with a positive exponent
398: * to a plain string representation;
399: * scale == 0.
400: */
401: public void testToPlainStringNegPosExp() {
402: String a = "-123809648392384754573567356745735.63567890295784902768787678287E100";
403: BigDecimal aNumber = new BigDecimal(a);
404: String result = "-1238096483923847545735673567457356356789029578490276878767828700000000000000000000000000000000000000000000000000000000000000000000000";
405: assertTrue("incorrect value", aNumber.toPlainString().equals(
406: result));
407: }
408:
409: /**
410: * Convert a positive BigDecimal with a negative exponent
411: * to a plain string representation;
412: * scale == 0.
413: */
414: public void testToPlainStringPosNegExp() {
415: String a = "123809648392384754573567356745735.63567890295784902768787678287E-100";
416: BigDecimal aNumber = new BigDecimal(a);
417: String result = "0.000000000000000000000000000000000000000000000000000000000000000000012380964839238475457356735674573563567890295784902768787678287";
418: assertTrue("incorrect value", aNumber.toPlainString().equals(
419: result));
420: }
421:
422: /**
423: * Convert a negative BigDecimal with a negative exponent
424: * to a plain string representation;
425: * scale == 0.
426: */
427: public void testToPlainStringPosPosExp() {
428: String a = "123809648392384754573567356745735.63567890295784902768787678287E+100";
429: BigDecimal aNumber = new BigDecimal(a);
430: String result = "1238096483923847545735673567457356356789029578490276878767828700000000000000000000000000000000000000000000000000000000000000000000000";
431: assertTrue("incorrect value", aNumber.toPlainString().equals(
432: result));
433: }
434:
435: /**
436: * Convert a BigDecimal to a string representation;
437: * scale == 0.
438: */
439: public void testToStringZeroScale() {
440: String a = "-123809648392384754573567356745735635678902957849027687876782870";
441: BigDecimal aNumber = new BigDecimal(new BigInteger(a));
442: String result = "-123809648392384754573567356745735635678902957849027687876782870";
443: assertTrue("incorrect value", aNumber.toString().equals(result));
444: }
445:
446: /**
447: * Convert a positive BigDecimal to a string representation
448: */
449: public void testToStringPos() {
450: String a = "123809648392384754573567356745735.63567890295784902768787678287E-500";
451: BigDecimal aNumber = new BigDecimal(a);
452: String result = "1.2380964839238475457356735674573563567890295784902768787678287E-468";
453: assertTrue("incorrect value", aNumber.toString().equals(result));
454: }
455:
456: /**
457: * Convert a negative BigDecimal to a string representation
458: */
459: public void testToStringNeg() {
460: String a = "-123.4564563673567380964839238475457356735674573563567890295784902768787678287E-5";
461: BigDecimal aNumber = new BigDecimal(a);
462: String result = "-0.001234564563673567380964839238475457356735674573563567890295784902768787678287";
463: assertTrue("incorrect value", aNumber.toString().equals(result));
464: }
465:
466: /**
467: * Create a BigDecimal from a positive long value; scale == 0
468: */
469: public void testValueOfPosZeroScale() {
470: long a = 98374823947823578L;
471: BigDecimal aNumber = BigDecimal.valueOf(a);
472: String result = "98374823947823578";
473: assertTrue("incorrect value", aNumber.toString().equals(result));
474: }
475:
476: /**
477: * Create a BigDecimal from a negative long value; scale is 0
478: */
479: public void testValueOfNegZeroScale() {
480: long a = -98374823947823578L;
481: BigDecimal aNumber = BigDecimal.valueOf(a);
482: String result = "-98374823947823578";
483: assertTrue("incorrect value", aNumber.toString().equals(result));
484: }
485:
486: /**
487: * Create a BigDecimal from a negative long value; scale is positive
488: */
489: public void testValueOfNegScalePos() {
490: long a = -98374823947823578L;
491: int scale = 12;
492: BigDecimal aNumber = BigDecimal.valueOf(a, scale);
493: String result = "-98374.823947823578";
494: assertTrue("incorrect value", aNumber.toString().equals(result));
495: }
496:
497: /**
498: * Create a BigDecimal from a negative long value; scale is negative
499: */
500: public void testValueOfNegScaleNeg() {
501: long a = -98374823947823578L;
502: int scale = -12;
503: BigDecimal aNumber = BigDecimal.valueOf(a, scale);
504: String result = "-9.8374823947823578E+28";
505: assertTrue("incorrect value", aNumber.toString().equals(result));
506: }
507:
508: /**
509: * Create a BigDecimal from a negative long value; scale is positive
510: */
511: public void testValueOfPosScalePos() {
512: long a = 98374823947823578L;
513: int scale = 12;
514: BigDecimal aNumber = BigDecimal.valueOf(a, scale);
515: String result = "98374.823947823578";
516: assertTrue("incorrect value", aNumber.toString().equals(result));
517: }
518:
519: /**
520: * Create a BigDecimal from a negative long value; scale is negative
521: */
522: public void testValueOfPosScaleNeg() {
523: long a = 98374823947823578L;
524: int scale = -12;
525: BigDecimal aNumber = BigDecimal.valueOf(a, scale);
526: String result = "9.8374823947823578E+28";
527: assertTrue("incorrect value", aNumber.toString().equals(result));
528: }
529:
530: /**
531: * Create a BigDecimal from a negative double value
532: */
533: public void testValueOfDoubleNeg() {
534: double a = -65678765876567576.98788767;
535: BigDecimal result = BigDecimal.valueOf(a);
536: String res = "-65678765876567576";
537: int resScale = 0;
538: assertEquals("incorrect value", res, result.toString());
539: assertEquals("incorrect scale", resScale, result.scale());
540: }
541:
542: /**
543: * Create a BigDecimal from a positive double value
544: */
545: public void testValueOfDoublePos1() {
546: double a = 65678765876567576.98788767;
547: BigDecimal result = BigDecimal.valueOf(a);
548: String res = "65678765876567576";
549: int resScale = 0;
550: assertEquals("incorrect value", res, result.toString());
551: assertEquals("incorrect scale", resScale, result.scale());
552: }
553:
554: /**
555: * Create a BigDecimal from a positive double value
556: */
557: public void testValueOfDoublePos2() {
558: double a = 12321237576.98788767;
559: BigDecimal result = BigDecimal.valueOf(a);
560: String res = "12321237576.987888";
561: int resScale = 6;
562: assertEquals("incorrect value", res, result.toString());
563: assertEquals("incorrect scale", resScale, result.scale());
564: }
565:
566: /**
567: * Create a BigDecimal from a positive double value
568: */
569: public void testValueOfDoublePos3() {
570: double a = 12321237576.9878838;
571: BigDecimal result = BigDecimal.valueOf(a);
572: String res = "12321237576.987885";
573: int resScale = 6;
574: assertEquals("incorrect value", res, result.toString());
575: assertEquals("incorrect scale", resScale, result.scale());
576: }
577:
578: /**
579: * valueOf(Double.NaN)
580: */
581: public void testValueOfDoubleNaN() {
582: double a = Double.NaN;
583: try {
584: BigDecimal.valueOf(a);
585: fail("NumberFormatException has not been thrown for Double.NaN");
586: } catch (NumberFormatException e) {
587: return;
588: }
589: }
590: }
|