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 21package org.apache.harmony.tests.java.math; 22 23import junit.framework.TestCase; 24import java.math.BigDecimal; 25import java.math.BigInteger; 26 27/** 28 * Class: java.math.BigDecimal 29 * Methods: doubleValue, floatValue, intValue, longValue, 30 * valueOf, toString, toBigInteger 31 */ 32public class BigDecimalConvertTest extends TestCase { 33 /** 34 * Double value of a negative BigDecimal 35 */ 36 public void testDoubleValueNeg() { 37 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; 38 BigDecimal aNumber = new BigDecimal(a); 39 double result = -1.2380964839238476E53; 40 assertEquals("incorrect value", result, aNumber.doubleValue(), 0); 41 } 42 43 /** 44 * Double value of a positive BigDecimal 45 */ 46 public void testDoubleValuePos() { 47 String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; 48 BigDecimal aNumber = new BigDecimal(a); 49 double result = 1.2380964839238476E53; 50 assertEquals("incorrect value", result, aNumber.doubleValue(), 0); 51 } 52 53 /** 54 * Double value of a large positive BigDecimal 55 */ 56 public void testDoubleValuePosInfinity() { 57 String a = "123809648392384754573567356745735.63567890295784902768787678287E+400"; 58 BigDecimal aNumber = new BigDecimal(a); 59 double result = Double.POSITIVE_INFINITY; 60 assertEquals("incorrect value", result, aNumber.doubleValue(), 0); 61 } 62 63 /** 64 * Double value of a large negative BigDecimal 65 */ 66 public void testDoubleValueNegInfinity() { 67 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+400"; 68 BigDecimal aNumber = new BigDecimal(a); 69 double result = Double.NEGATIVE_INFINITY; 70 assertEquals("incorrect value", result, aNumber.doubleValue(), 0); 71 } 72 73 /** 74 * Double value of a small negative BigDecimal 75 */ 76 public void testDoubleValueMinusZero() { 77 String a = "-123809648392384754573567356745735.63567890295784902768787678287E-400"; 78 BigDecimal aNumber = new BigDecimal(a); 79 long minusZero = -9223372036854775808L; 80 double result = aNumber.doubleValue(); 81 assertTrue("incorrect value", Double.doubleToLongBits(result) == minusZero); 82 } 83 84 /** 85 * Double value of a small positive BigDecimal 86 */ 87 public void testDoubleValuePlusZero() { 88 String a = "123809648392384754573567356745735.63567890295784902768787678287E-400"; 89 BigDecimal aNumber = new BigDecimal(a); 90 long zero = 0; 91 double result = aNumber.doubleValue(); 92 assertTrue("incorrect value", Double.doubleToLongBits(result) == zero); 93 } 94 95 /** 96 * Float value of a negative BigDecimal 97 */ 98 public void testFloatValueNeg() { 99 String a = "-1238096483923847.6356789029578E+21"; 100 BigDecimal aNumber = new BigDecimal(a); 101 float result = -1.2380965E36F; 102 assertTrue("incorrect value", aNumber.floatValue() == result); 103 } 104 105 /** 106 * Float value of a positive BigDecimal 107 */ 108 public void testFloatValuePos() { 109 String a = "1238096483923847.6356789029578E+21"; 110 BigDecimal aNumber = new BigDecimal(a); 111 float result = 1.2380965E36F; 112 assertTrue("incorrect value", aNumber.floatValue() == result); 113 } 114 115 /** 116 * Float value of a large positive BigDecimal 117 */ 118 public void testFloatValuePosInfinity() { 119 String a = "123809648373567356745735.6356789787678287E+200"; 120 BigDecimal aNumber = new BigDecimal(a); 121 float result = Float.POSITIVE_INFINITY; 122 assertTrue("incorrect value", aNumber.floatValue() == result); 123 } 124 125 /** 126 * Float value of a large negative BigDecimal 127 */ 128 public void testFloatValueNegInfinity() { 129 String a = "-123809648392384755735.63567887678287E+200"; 130 BigDecimal aNumber = new BigDecimal(a); 131 float result = Float.NEGATIVE_INFINITY; 132 assertTrue("incorrect value", aNumber.floatValue() == result); 133 } 134 135 /** 136 * Float value of a small negative BigDecimal 137 */ 138 public void testFloatValueMinusZero() { 139 String a = "-123809648392384754573567356745735.63567890295784902768787678287E-400"; 140 BigDecimal aNumber = new BigDecimal(a); 141 int minusZero = -2147483648; 142 float result = aNumber.floatValue(); 143 assertTrue("incorrect value", Float.floatToIntBits(result) == minusZero); 144 } 145 146 /** 147 * Float value of a small positive BigDecimal 148 */ 149 public void testFloatValuePlusZero() { 150 String a = "123809648392384754573567356745735.63567890295784902768787678287E-400"; 151 BigDecimal aNumber = new BigDecimal(a); 152 int zero = 0; 153 float result = aNumber.floatValue(); 154 assertTrue("incorrect value", Float.floatToIntBits(result) == zero); 155 } 156 157 /** 158 * Integer value of a negative BigDecimal 159 */ 160 public void testIntValueNeg() { 161 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; 162 BigDecimal aNumber = new BigDecimal(a); 163 int result = 218520473; 164 assertTrue("incorrect value", aNumber.intValue() == result); 165 } 166 167 /** 168 * Integer value of a positive BigDecimal 169 */ 170 public void testIntValuePos() { 171 String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; 172 BigDecimal aNumber = new BigDecimal(a); 173 int result = -218520473; 174 assertTrue("incorrect value", aNumber.intValue() == result); 175 } 176 177 /** 178 * Long value of a negative BigDecimal 179 */ 180 public void testLongValueNeg() { 181 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; 182 BigDecimal aNumber = new BigDecimal(a); 183 long result = -1246043477766677607L; 184 assertTrue("incorrect value", aNumber.longValue() == result); 185 } 186 187 /** 188 * Long value of a positive BigDecimal 189 */ 190 public void testLongValuePos() { 191 String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; 192 BigDecimal aNumber = new BigDecimal(a); 193 long result = 1246043477766677607L; 194 assertTrue("incorrect value", aNumber.longValue() == result); 195 } 196 197 /** 198 * scaleByPowerOfTen(int n) 199 */ 200 public void testScaleByPowerOfTen1() { 201 String a = "1231212478987482988429808779810457634781384756794987"; 202 int aScale = 13; 203 BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); 204 BigDecimal result = aNumber.scaleByPowerOfTen(10); 205 String res = "1231212478987482988429808779810457634781384756794.987"; 206 int resScale = 3; 207 assertEquals("incorrect value", res, result.toString()); 208 assertEquals("incorrect scale", resScale, result.scale()); 209 } 210 211 /** 212 * scaleByPowerOfTen(int n) 213 */ 214 public void testScaleByPowerOfTen2() { 215 String a = "1231212478987482988429808779810457634781384756794987"; 216 int aScale = -13; 217 BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); 218 BigDecimal result = aNumber.scaleByPowerOfTen(10); 219 String res = "1.231212478987482988429808779810457634781384756794987E+74"; 220 int resScale = -23; 221 assertEquals("incorrect value", res, result.toString()); 222 assertEquals("incorrect scale", resScale, result.scale()); 223 } 224 225 /** 226 * Convert a positive BigDecimal to BigInteger 227 */ 228 public void testToBigIntegerPos1() { 229 String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; 230 BigInteger bNumber = new BigInteger("123809648392384754573567356745735635678902957849027687"); 231 BigDecimal aNumber = new BigDecimal(a); 232 BigInteger result = aNumber.toBigInteger(); 233 assertTrue("incorrect value", result.equals(bNumber)); 234 } 235 236 /** 237 * Convert a positive BigDecimal to BigInteger 238 */ 239 public void testToBigIntegerPos2() { 240 String a = "123809648392384754573567356745735.63567890295784902768787678287E+15"; 241 BigInteger bNumber = new BigInteger("123809648392384754573567356745735635678902957849"); 242 BigDecimal aNumber = new BigDecimal(a); 243 BigInteger result = aNumber.toBigInteger(); 244 assertTrue("incorrect value", result.equals(bNumber)); 245 } 246 247 /** 248 * Convert a positive BigDecimal to BigInteger 249 */ 250 public void testToBigIntegerPos3() { 251 String a = "123809648392384754573567356745735.63567890295784902768787678287E+45"; 252 BigInteger bNumber = new BigInteger("123809648392384754573567356745735635678902957849027687876782870000000000000000"); 253 BigDecimal aNumber = new BigDecimal(a); 254 BigInteger result = aNumber.toBigInteger(); 255 assertTrue("incorrect value", result.equals(bNumber)); 256 } 257 258 /** 259 * Convert a negative BigDecimal to BigInteger 260 */ 261 public void testToBigIntegerNeg1() { 262 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; 263 BigInteger bNumber = new BigInteger("-123809648392384754573567356745735635678902957849027687"); 264 BigDecimal aNumber = new BigDecimal(a); 265 BigInteger result = aNumber.toBigInteger(); 266 assertTrue("incorrect value", result.equals(bNumber)); 267 } 268 269 /** 270 * Convert a negative BigDecimal to BigInteger 271 */ 272 public void testToBigIntegerNeg2() { 273 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+15"; 274 BigInteger bNumber = new BigInteger("-123809648392384754573567356745735635678902957849"); 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 testToBigIntegerNeg3() { 284 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+45"; 285 BigInteger bNumber = new BigInteger("-123809648392384754573567356745735635678902957849027687876782870000000000000000"); 286 BigDecimal aNumber = new BigDecimal(a); 287 BigInteger result = aNumber.toBigInteger(); 288 assertTrue("incorrect value", result.equals(bNumber)); 289 } 290 291 /** 292 * Convert a small BigDecimal to BigInteger 293 */ 294 public void testToBigIntegerZero() { 295 String a = "-123809648392384754573567356745735.63567890295784902768787678287E-500"; 296 BigInteger bNumber = new BigInteger("0"); 297 BigDecimal aNumber = new BigDecimal(a); 298 BigInteger result = aNumber.toBigInteger(); 299 assertTrue("incorrect value", result.equals(bNumber)); 300 } 301 302 /** 303 * toBigIntegerExact() 304 */ 305 public void testToBigIntegerExact1() { 306 String a = "-123809648392384754573567356745735.63567890295784902768787678287E+45"; 307 BigDecimal aNumber = new BigDecimal(a); 308 String res = "-123809648392384754573567356745735635678902957849027687876782870000000000000000"; 309 BigInteger result = aNumber.toBigIntegerExact(); 310 assertEquals("incorrect value", res, result.toString()); 311 } 312 313 /** 314 * toBigIntegerExact() 315 */ 316 public void testToBigIntegerExactException() { 317 String a = "-123809648392384754573567356745735.63567890295784902768787678287E-10"; 318 BigDecimal aNumber = new BigDecimal(a); 319 try { 320 aNumber.toBigIntegerExact(); 321 fail("java.lang.ArithmeticException has not been thrown"); 322 } catch (java.lang.ArithmeticException e) { 323 return; 324 } 325 } 326 327 /** 328 * Convert a positive BigDecimal to an engineering string representation 329 */ 330 public void testToEngineeringStringPos() { 331 String a = "123809648392384754573567356745735.63567890295784902768787678287E-501"; 332 BigDecimal aNumber = new BigDecimal(a); 333 String result = "123.80964839238475457356735674573563567890295784902768787678287E-471"; 334 assertEquals("incorrect value", result, aNumber.toEngineeringString()); 335 } 336 337 /** 338 * Convert a negative BigDecimal to an engineering string representation 339 */ 340 public void testToEngineeringStringNeg() { 341 String a = "-123809648392384754573567356745735.63567890295784902768787678287E-501"; 342 BigDecimal aNumber = new BigDecimal(a); 343 String result = "-123.80964839238475457356735674573563567890295784902768787678287E-471"; 344 assertEquals("incorrect value", result, aNumber.toEngineeringString()); 345 } 346 347 /** 348 * Convert a negative BigDecimal to an engineering string representation 349 */ 350 public void testToEngineeringStringZeroPosExponent() { 351 String a = "0.0E+16"; 352 BigDecimal aNumber = new BigDecimal(a); 353 String result = "0E+15"; 354 assertEquals("incorrect value", result, aNumber.toEngineeringString()); 355 } 356 357 /** 358 * Convert a negative BigDecimal to an engineering string representation 359 */ 360 public void testToEngineeringStringZeroNegExponent() { 361 String a = "0.0E-16"; 362 BigDecimal aNumber = new BigDecimal(a); 363 String result = "0.00E-15"; 364 assertEquals("incorrect value", result, aNumber.toEngineeringString()); 365 } 366 367 /** 368 * Convert a negative BigDecimal with a negative exponent to a plain string 369 * representation; scale == 0. 370 */ 371 public void testToPlainStringNegNegExp() { 372 String a = "-123809648392384754573567356745735.63567890295784902768787678287E-100"; 373 BigDecimal aNumber = new BigDecimal(a); 374 String result = "-0.000000000000000000000000000000000000000000000000000000000000000000012380964839238475457356735674573563567890295784902768787678287"; 375 assertTrue("incorrect value", aNumber.toPlainString().equals(result)); 376 } 377 378 /** 379 * Convert a negative BigDecimal with a positive exponent 380 * to a plain string representation; 381 * scale == 0. 382 */ 383 public void testToPlainStringNegPosExp() { 384 String a = "-123809648392384754573567356745735.63567890295784902768787678287E100"; 385 BigDecimal aNumber = new BigDecimal(a); 386 String result = "-1238096483923847545735673567457356356789029578490276878767828700000000000000000000000000000000000000000000000000000000000000000000000"; 387 assertTrue("incorrect value", aNumber.toPlainString().equals(result)); 388 } 389 390 /** 391 * Convert a positive BigDecimal with a negative exponent 392 * to a plain string representation; 393 * scale == 0. 394 */ 395 public void testToPlainStringPosNegExp() { 396 String a = "123809648392384754573567356745735.63567890295784902768787678287E-100"; 397 BigDecimal aNumber = new BigDecimal(a); 398 String result = "0.000000000000000000000000000000000000000000000000000000000000000000012380964839238475457356735674573563567890295784902768787678287"; 399 assertTrue("incorrect value", aNumber.toPlainString().equals(result)); 400 } 401 402 /** 403 * Convert a negative BigDecimal with a negative exponent 404 * to a plain string representation; 405 * scale == 0. 406 */ 407 public void testToPlainStringPosPosExp() { 408 String a = "123809648392384754573567356745735.63567890295784902768787678287E+100"; 409 BigDecimal aNumber = new BigDecimal(a); 410 String result = "1238096483923847545735673567457356356789029578490276878767828700000000000000000000000000000000000000000000000000000000000000000000000"; 411 assertTrue("incorrect value", aNumber.toPlainString().equals(result)); 412 } 413 414 /** 415 * Convert a BigDecimal to a string representation; 416 * scale == 0. 417 */ 418 public void testToStringZeroScale() { 419 String a = "-123809648392384754573567356745735635678902957849027687876782870"; 420 BigDecimal aNumber = new BigDecimal(new BigInteger(a)); 421 String result = "-123809648392384754573567356745735635678902957849027687876782870"; 422 assertTrue("incorrect value", aNumber.toString().equals(result)); 423 } 424 425 /** 426 * Convert a positive BigDecimal to a string representation 427 */ 428 public void testToStringPos() { 429 String a = "123809648392384754573567356745735.63567890295784902768787678287E-500"; 430 BigDecimal aNumber = new BigDecimal(a); 431 String result = "1.2380964839238475457356735674573563567890295784902768787678287E-468"; 432 assertTrue("incorrect value", aNumber.toString().equals(result)); 433 } 434 435 /** 436 * Convert a negative BigDecimal to a string representation 437 */ 438 public void testToStringNeg() { 439 String a = "-123.4564563673567380964839238475457356735674573563567890295784902768787678287E-5"; 440 BigDecimal aNumber = new BigDecimal(a); 441 String result = "-0.001234564563673567380964839238475457356735674573563567890295784902768787678287"; 442 assertTrue("incorrect value", aNumber.toString().equals(result)); 443 } 444 445 /** 446 * Create a BigDecimal from a positive long value; scale == 0 447 */ 448 public void testValueOfPosZeroScale() { 449 long a = 98374823947823578L; 450 BigDecimal aNumber = BigDecimal.valueOf(a); 451 String result = "98374823947823578"; 452 assertTrue("incorrect value", aNumber.toString().equals(result)); 453 } 454 455 /** 456 * Create a BigDecimal from a negative long value; scale is 0 457 */ 458 public void testValueOfNegZeroScale() { 459 long a = -98374823947823578L; 460 BigDecimal aNumber = BigDecimal.valueOf(a); 461 String result = "-98374823947823578"; 462 assertTrue("incorrect value", aNumber.toString().equals(result)); 463 } 464 465 /** 466 * Create a BigDecimal from a negative long value; scale is positive 467 */ 468 public void testValueOfNegScalePos() { 469 long a = -98374823947823578L; 470 int scale = 12; 471 BigDecimal aNumber = BigDecimal.valueOf(a, scale); 472 String result = "-98374.823947823578"; 473 assertTrue("incorrect value", aNumber.toString().equals(result)); 474 } 475 476 /** 477 * Create a BigDecimal from a negative long value; scale is negative 478 */ 479 public void testValueOfNegScaleNeg() { 480 long a = -98374823947823578L; 481 int scale = -12; 482 BigDecimal aNumber = BigDecimal.valueOf(a, scale); 483 String result = "-9.8374823947823578E+28"; 484 assertTrue("incorrect value", aNumber.toString().equals(result)); 485 } 486 487 /** 488 * Create a BigDecimal from a negative long value; scale is positive 489 */ 490 public void testValueOfPosScalePos() { 491 long a = 98374823947823578L; 492 int scale = 12; 493 BigDecimal aNumber = BigDecimal.valueOf(a, scale); 494 String result = "98374.823947823578"; 495 assertTrue("incorrect value", aNumber.toString().equals(result)); 496 } 497 498 /** 499 * Create a BigDecimal from a negative long value; scale is negative 500 */ 501 public void testValueOfPosScaleNeg() { 502 long a = 98374823947823578L; 503 int scale = -12; 504 BigDecimal aNumber = BigDecimal.valueOf(a, scale); 505 String result = "9.8374823947823578E+28"; 506 assertTrue("incorrect value", aNumber.toString().equals(result)); 507 } 508 509 /** 510 * Create a BigDecimal from a negative double value 511 */ 512 public void testValueOfDoubleNeg() { 513 double a = -65678765876567576.98788767; 514 BigDecimal result = BigDecimal.valueOf(a); 515 String res = "-65678765876567576"; 516 int resScale = 0; 517 assertEquals("incorrect value", res, result.toString()); 518 assertEquals("incorrect scale", resScale, result.scale()); 519 } 520 521 /** 522 * Create a BigDecimal from a positive double value 523 */ 524 public void testValueOfDoublePos1() { 525 double a = 65678765876567576.98788767; 526 BigDecimal result = BigDecimal.valueOf(a); 527 String res = "65678765876567576"; 528 int resScale = 0; 529 assertEquals("incorrect value", res, result.toString()); 530 assertEquals("incorrect scale", resScale, result.scale()); 531 } 532 533 /** 534 * Create a BigDecimal from a positive double value 535 */ 536 public void testValueOfDoublePos2() { 537 double a = 12321237576.98788767; 538 BigDecimal result = BigDecimal.valueOf(a); 539 String res = "12321237576.987888"; 540 int resScale = 6; 541 assertEquals("incorrect value", res, result.toString()); 542 assertEquals("incorrect scale", resScale, result.scale()); 543 } 544 545 /** 546 * Create a BigDecimal from a positive double value 547 */ 548 public void testValueOfDoublePos3() { 549 double a = 12321237576.9878838; 550 BigDecimal result = BigDecimal.valueOf(a); 551 String res = "12321237576.987885"; 552 int resScale = 6; 553 assertEquals("incorrect value", res, result.toString()); 554 assertEquals("incorrect scale", resScale, result.scale()); 555 } 556 557 /** 558 * valueOf(Double.NaN) 559 */ 560 public void testValueOfDoubleNaN() { 561 double a = Double.NaN; 562 try { 563 BigDecimal.valueOf(a); 564 fail("NumberFormatException has not been thrown for Double.NaN"); 565 } catch (NumberFormatException e) { 566 return; 567 } 568 } 569} 570