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 package tests.api.java.math; 19 20 import dalvik.annotation.TestTargets; 21 import dalvik.annotation.TestLevel; 22 import dalvik.annotation.TestTargetNew; 23 import dalvik.annotation.TestTargetClass; 24 import dalvik.annotation.AndroidOnly; 25 26 import java.io.ByteArrayInputStream; 27 import java.io.ByteArrayOutputStream; 28 import java.io.ObjectInputStream; 29 import java.io.ObjectOutputStream; 30 import java.math.BigDecimal; 31 import java.math.BigInteger; 32 import java.math.RoundingMode; 33 import java.math.MathContext; 34 35 @TestTargetClass(BigDecimal.class) 36 public class BigDecimalTest extends junit.framework.TestCase { 37 BigInteger value = new BigInteger("12345908"); 38 39 BigInteger value2 = new BigInteger("12334560000"); 40 41 /** 42 * @tests java.math.BigDecimal#BigDecimal(java.math.BigInteger) 43 */ 44 @TestTargetNew( 45 level = TestLevel.COMPLETE, 46 notes = "", 47 method = "BigDecimal", 48 args = {java.math.BigInteger.class} 49 ) 50 public void test_ConstructorLjava_math_BigInteger() { 51 BigDecimal big = new BigDecimal(value); 52 assertTrue("the BigDecimal value is not initialized properly", big 53 .unscaledValue().equals(value) 54 && big.scale() == 0); 55 } 56 57 /** 58 * @tests java.math.BigDecimal#BigDecimal(java.math.BigInteger, int) 59 */ 60 @TestTargetNew( 61 level = TestLevel.COMPLETE, 62 notes = "", 63 method = "BigDecimal", 64 args = {java.math.BigInteger.class, int.class} 65 ) 66 public void test_ConstructorLjava_math_BigIntegerI() { 67 BigDecimal big = new BigDecimal(value2, 5); 68 assertTrue("the BigDecimal value is not initialized properly", big 69 .unscaledValue().equals(value2) 70 && big.scale() == 5); 71 assertTrue("the BigDecimal value is not represented properly", big 72 .toString().equals("123345.60000")); 73 } 74 75 /** 76 * @tests java.math.BigDecimal#BigDecimal(double) 77 */ 78 @TestTargetNew( 79 level = TestLevel.COMPLETE, 80 notes = ".", 81 method = "BigDecimal", 82 args = {double.class} 83 ) 84 public void test_ConstructorD() { 85 // 86 // These numbers have an exact representation as doubles: 87 // 88 BigDecimal big = new BigDecimal(123E04); 89 assertTrue( 90 "the BigDecimal value taking a double argument is not initialized properly", 91 big.toString().equals("1230000")); 92 big = new BigDecimal(123.375); 93 assertTrue("init(D) failed for 123.375; became " + big, 94 big.toString().equals("123.375") ); 95 big = new BigDecimal(Math.pow(2, -33)); 96 assertTrue("init(D) failed for 2^(-33) = 1.16415321826934814453125E-10; became " + big, 97 big.toString().equals("1.16415321826934814453125E-10") ); 98 big = new BigDecimal(123456 * Math.pow(2, -33)); 99 assertTrue("init(D) failed for 123456 * 2^(-33) = 0.000014372169971466064453125; became " + big, 100 big.toString().equals("0.000014372169971466064453125") ); 101 big = new BigDecimal(-123456 * Math.pow(2, -33)); 102 assertTrue("init(D) failed for 123456 * 2^(-33) = -0.000014372169971466064453125; became " + big, 103 big.toString().equals("-0.000014372169971466064453125") ); 104 105 // Following numbers can't: 106 // 107 big = new BigDecimal(1.2345E-12); 108 assertTrue("the double representation is not correct", big 109 .doubleValue() == 1.2345E-12); 110 big = new BigDecimal(-12345E-3); 111 assertTrue("the double representation is not correct", big 112 .doubleValue() == -12.345); 113 big = new BigDecimal(5.1234567897654321e138); 114 assertTrue("the double representation is not correct", big 115 .doubleValue() == 5.1234567897654321E138 116 && big.scale() == 0); 117 big = new BigDecimal(0.1); 118 assertTrue( 119 "the double representation of 0.1 bigDecimal is not correct", 120 big.doubleValue() == 0.1); 121 big = new BigDecimal(0.00345); 122 assertTrue( 123 "the double representation of 0.00345 bigDecimal is not correct", 124 big.doubleValue() == 0.00345); 125 // regression test for HARMONY-2429 126 big = new BigDecimal(-0.0); 127 assertTrue( 128 "the double representation of -0.0 bigDecimal is not correct", 129 big.scale() == 0); 130 } 131 132 /** 133 * @tests java.math.BigDecimal#BigDecimal(java.lang.String) 134 */ 135 @TestTargetNew( 136 level = TestLevel.COMPLETE, 137 notes = "", 138 method = "BigDecimal", 139 args = {java.lang.String.class} 140 ) 141 public void test_ConstructorLjava_lang_String() throws NumberFormatException { 142 BigDecimal big = new BigDecimal("345.23499600293850"); 143 assertTrue("the BigDecimal value is not initialized properly", big 144 .toString().equals("345.23499600293850") 145 && big.scale() == 14); 146 big = new BigDecimal("-12345"); 147 assertTrue("the BigDecimal value is not initialized properly", big 148 .toString().equals("-12345") 149 && big.scale() == 0); 150 big = new BigDecimal("123."); 151 assertTrue("the BigDecimal value is not initialized properly", big 152 .toString().equals("123") 153 && big.scale() == 0); 154 155 new BigDecimal("1.234E02"); 156 } 157 158 /** 159 * @tests java.math.BigDecimal#BigDecimal(java.lang.String) 160 */ 161 @TestTargetNew( 162 level = TestLevel.PARTIAL, 163 notes = "", 164 method = "BigDecimal", 165 args = {double.class} 166 ) 167 public void test_constructor_String_plus_exp() { 168 /* 169 * BigDecimal does not support a + sign in the exponent when converting 170 * from a String. 171 * 172 * mc 081106: who says so?!? 173 */ 174 BigDecimal bd; 175 bd = new BigDecimal("+23e-0"); 176 assertEquals("incorrect value", "23", bd.toString()); 177 bd = new BigDecimal("-23e+0"); 178 assertEquals("incorrect value", "-23", bd.toString()); 179 } 180 181 /** 182 * @tests java.math.BigDecimal#BigDecimal(java.lang.String) 183 */ 184 @TestTargetNew( 185 level = TestLevel.PARTIAL, 186 notes = "Exception checked.", 187 method = "BigDecimal", 188 args = {java.lang.String.class} 189 ) 190 public void test_constructor_String_empty() { 191 try { 192 new BigDecimal(""); 193 fail("NumberFormatException expected"); 194 } catch (NumberFormatException e) { 195 } 196 } 197 198 /** 199 * @tests java.math.BigDecimal#BigDecimal(java.lang.String) 200 */ 201 @TestTargetNew( 202 level = TestLevel.PARTIAL, 203 notes = "Exception checked.", 204 method = "BigDecimal", 205 args = {java.lang.String.class} 206 ) 207 public void test_constructor_String_plus_minus_exp() { 208 try { 209 new BigDecimal("+35e+-2"); 210 fail("NumberFormatException expected"); 211 } catch (NumberFormatException e) { 212 } 213 214 try { 215 new BigDecimal("-35e-+2"); 216 fail("NumberFormatException expected"); 217 } catch (NumberFormatException e) { 218 } 219 } 220 221 /** 222 * @tests java.math.BigDecimal#BigDecimal(char[]) 223 */ 224 @TestTargetNew( 225 level = TestLevel.PARTIAL, 226 notes = "Exception checked.", 227 method = "BigDecimal", 228 args = {char[].class} 229 ) 230 public void test_constructor_CC_plus_minus_exp() { 231 try { 232 new BigDecimal("+35e+-2".toCharArray()); 233 fail("NumberFormatException expected"); 234 } catch (NumberFormatException e) { 235 } 236 237 try { 238 new BigDecimal("-35e-+2".toCharArray()); 239 fail("NumberFormatException expected"); 240 } catch (NumberFormatException e) { 241 } 242 } 243 244 /** 245 * @tests java.math.BigDecimal#abs() 246 */ 247 @TestTargetNew( 248 level = TestLevel.COMPLETE, 249 notes = "", 250 method = "abs", 251 args = {} 252 ) 253 public void test_abs() { 254 BigDecimal big = new BigDecimal("-1234"); 255 BigDecimal bigabs = big.abs(); 256 assertTrue("the absolute value of -1234 is not 1234", bigabs.toString() 257 .equals("1234")); 258 big = new BigDecimal(new BigInteger("2345"), 2); 259 bigabs = big.abs(); 260 assertTrue("the absolute value of 23.45 is not 23.45", bigabs 261 .toString().equals("23.45")); 262 } 263 264 /** 265 * @tests java.math.BigDecimal#add(java.math.BigDecimal) 266 */ 267 @TestTargetNew( 268 level = TestLevel.COMPLETE, 269 notes = "", 270 method = "add", 271 args = {java.math.BigDecimal.class} 272 ) 273 public void test_addLjava_math_BigDecimal() { 274 BigDecimal add1 = new BigDecimal("23.456"); 275 BigDecimal add2 = new BigDecimal("3849.235"); 276 BigDecimal sum = add1.add(add2); 277 assertTrue("the sum of 23.456 + 3849.235 is wrong", sum.unscaledValue() 278 .toString().equals("3872691") 279 && sum.scale() == 3); 280 assertTrue("the sum of 23.456 + 3849.235 is not printed correctly", sum 281 .toString().equals("3872.691")); 282 BigDecimal add3 = new BigDecimal(12.34E02D); 283 assertTrue("the sum of 23.456 + 12.34E02 is not printed correctly", 284 (add1.add(add3)).toString().equals("1257.456")); 285 } 286 287 /** 288 * @tests java.math.BigDecimal#compareTo(java.math.BigDecimal) 289 */ 290 @TestTargetNew( 291 level = TestLevel.COMPLETE, 292 notes = "", 293 method = "compareTo", 294 args = {java.math.BigDecimal.class} 295 ) 296 public void test_compareToLjava_math_BigDecimal() { 297 BigDecimal comp1 = new BigDecimal("1.00"); 298 BigDecimal comp2 = new BigDecimal(1.000000D); 299 assertTrue("1.00 and 1.000000 should be equal", 300 comp1.compareTo(comp2) == 0); 301 BigDecimal comp3 = new BigDecimal("1.02"); 302 assertTrue("1.02 should be bigger than 1.00", 303 comp3.compareTo(comp1) == 1); 304 BigDecimal comp4 = new BigDecimal(0.98D); 305 assertTrue("0.98 should be less than 1.00", 306 comp4.compareTo(comp1) == -1); 307 } 308 309 /** 310 * @tests java.math.BigDecimal#divide(java.math.BigDecimal, int) 311 */ 312 @TestTargetNew( 313 level = TestLevel.PARTIAL, 314 notes = "IllegalArgumentException checking missed. Used only ROUND_UP & ROUND_DOWN round modes.", 315 method = "divide", 316 args = {java.math.BigDecimal.class, int.class} 317 ) 318 public void test_divideLjava_math_BigDecimalI() { 319 BigDecimal divd1 = new BigDecimal(value, 2); 320 BigDecimal divd2 = new BigDecimal("2.335"); 321 BigDecimal divd3 = divd1.divide(divd2, BigDecimal.ROUND_UP); 322 assertTrue("123459.08/2.335 is not correct", divd3.toString().equals( 323 "52873.27") 324 && divd3.scale() == divd1.scale()); 325 assertTrue( 326 "the unscaledValue representation of 123459.08/2.335 is not correct", 327 divd3.unscaledValue().toString().equals("5287327")); 328 divd2 = new BigDecimal(123.4D); 329 divd3 = divd1.divide(divd2, BigDecimal.ROUND_DOWN); 330 assertTrue("123459.08/123.4 is not correct", divd3.toString().equals( 331 "1000.47") 332 && divd3.scale() == 2); 333 divd2 = new BigDecimal(000D); 334 335 try { 336 divd1.divide(divd2, BigDecimal.ROUND_DOWN); 337 fail("divide by zero is not caught"); 338 } catch (ArithmeticException e) { 339 } 340 } 341 342 /** 343 * @tests java.math.BigDecimal#divide(java.math.BigDecimal, int, int) 344 */ 345 @TestTargetNew( 346 level = TestLevel.PARTIAL, 347 notes = "IllegalArgumentException checking missed. Used only ROUND_UP & ROUND_DOWN round modes.", 348 method = "divide", 349 args = {java.math.BigDecimal.class, int.class, int.class} 350 ) 351 public void test_divideLjava_math_BigDecimalII() { 352 BigDecimal divd1 = new BigDecimal(value2, 4); 353 BigDecimal divd2 = new BigDecimal("0.0023"); 354 BigDecimal divd3 = divd1.divide(divd2, 3, BigDecimal.ROUND_HALF_UP); 355 assertTrue("1233456/0.0023 is not correct", divd3.toString().equals( 356 "536285217.391") 357 && divd3.scale() == 3); 358 divd2 = new BigDecimal(1345.5E-02D); 359 divd3 = divd1.divide(divd2, 0, BigDecimal.ROUND_DOWN); 360 assertTrue( 361 "1233456/13.455 is not correct or does not have the correct scale", 362 divd3.toString().equals("91672") && divd3.scale() == 0); 363 divd2 = new BigDecimal(0000D); 364 365 try { 366 divd1.divide(divd2, 4, BigDecimal.ROUND_DOWN); 367 fail("divide by zero is not caught"); 368 } catch (ArithmeticException e) { 369 } 370 } 371 372 /** 373 * @tests java.math.BigDecimal#doubleValue() 374 */ 375 @TestTargetNew( 376 level = TestLevel.PARTIAL, 377 notes = "Narrowing limitations of double representation are not checked.", 378 method = "doubleValue", 379 args = {} 380 ) 381 public void test_doubleValue() { 382 BigDecimal bigDB = new BigDecimal(-1.234E-112); 383 // Commenting out this part because it causes an endless loop (see HARMONY-319 and HARMONY-329) 384 // assertTrue( 385 // "the double representation of this BigDecimal is not correct", 386 // bigDB.doubleValue() == -1.234E-112); 387 bigDB = new BigDecimal(5.00E-324); 388 assertTrue("the double representation of bigDecimal is not correct", 389 bigDB.doubleValue() == 5.00E-324); 390 bigDB = new BigDecimal(1.79E308); 391 assertTrue("the double representation of bigDecimal is not correct", 392 bigDB.doubleValue() == 1.79E308 && bigDB.scale() == 0); 393 bigDB = new BigDecimal(-2.33E102); 394 assertTrue( 395 "the double representation of bigDecimal -2.33E102 is not correct", 396 bigDB.doubleValue() == -2.33E102 && bigDB.scale() == 0); 397 bigDB = new BigDecimal(Double.MAX_VALUE); 398 bigDB = bigDB.add(bigDB); 399 assertTrue( 400 "a + number out of the double range should return infinity", 401 bigDB.doubleValue() == Double.POSITIVE_INFINITY); 402 bigDB = new BigDecimal(-Double.MAX_VALUE); 403 bigDB = bigDB.add(bigDB); 404 assertTrue( 405 "a - number out of the double range should return neg infinity", 406 bigDB.doubleValue() == Double.NEGATIVE_INFINITY); 407 } 408 409 /** 410 * @tests java.math.BigDecimal#equals(java.lang.Object) 411 */ 412 @TestTargetNew( 413 level = TestLevel.COMPLETE, 414 notes = "", 415 method = "equals", 416 args = {java.lang.Object.class} 417 ) 418 public void test_equalsLjava_lang_Object() { 419 BigDecimal equal1 = new BigDecimal(1.00D); 420 BigDecimal equal2 = new BigDecimal("1.0"); 421 assertFalse("1.00 and 1.0 should not be equal", 422 equal1.equals(equal2)); 423 equal2 = new BigDecimal(1.01D); 424 assertFalse("1.00 and 1.01 should not be equal", 425 equal1.equals(equal2)); 426 equal2 = new BigDecimal("1.00"); 427 assertFalse("1.00D and 1.00 should not be equal", 428 equal1.equals(equal2)); 429 BigInteger val = new BigInteger("100"); 430 equal1 = new BigDecimal("1.00"); 431 equal2 = new BigDecimal(val, 2); 432 assertTrue("1.00(string) and 1.00(bigInteger) should be equal", equal1 433 .equals(equal2)); 434 equal1 = new BigDecimal(100D); 435 equal2 = new BigDecimal("2.34576"); 436 assertFalse("100D and 2.34576 should not be equal", equal1 437 .equals(equal2)); 438 assertFalse("bigDecimal 100D does not equal string 23415", equal1 439 .equals("23415")); 440 } 441 442 /** 443 * @tests java.math.BigDecimal#floatValue() 444 */ 445 @TestTargetNew( 446 level = TestLevel.PARTIAL, 447 notes = "Narrowing limitations of float representation are not checked.", 448 method = "floatValue", 449 args = {} 450 ) 451 public void test_floatValue() { 452 BigDecimal fl1 = new BigDecimal("234563782344567"); 453 assertTrue("the float representation of bigDecimal 234563782344567", 454 fl1.floatValue() == 234563782344567f); 455 BigDecimal fl2 = new BigDecimal(2.345E37); 456 assertTrue("the float representation of bigDecimal 2.345E37", fl2 457 .floatValue() == 2.345E37F); 458 fl2 = new BigDecimal(-1.00E-44); 459 assertTrue("the float representation of bigDecimal -1.00E-44", fl2 460 .floatValue() == -1.00E-44F); 461 fl2 = new BigDecimal(-3E12); 462 assertTrue("the float representation of bigDecimal -3E12", fl2 463 .floatValue() == -3E12F); 464 fl2 = new BigDecimal(Double.MAX_VALUE); 465 assertTrue( 466 "A number can't be represented by float should return infinity", 467 fl2.floatValue() == Float.POSITIVE_INFINITY); 468 fl2 = new BigDecimal(-Double.MAX_VALUE); 469 assertTrue( 470 "A number can't be represented by float should return infinity", 471 fl2.floatValue() == Float.NEGATIVE_INFINITY); 472 473 } 474 475 /** 476 * @tests java.math.BigDecimal#hashCode() 477 */ 478 @TestTargetNew( 479 level = TestLevel.COMPLETE, 480 notes = "", 481 method = "hashCode", 482 args = {} 483 ) 484 public void test_hashCode() { 485 // anything that is equal must have the same hashCode 486 BigDecimal hash = new BigDecimal("1.00"); 487 BigDecimal hash2 = new BigDecimal(1.00D); 488 assertTrue("the hashCode of 1.00 and 1.00D is equal", 489 hash.hashCode() != hash2.hashCode() && !hash.equals(hash2)); 490 hash2 = new BigDecimal("1.0"); 491 assertTrue("the hashCode of 1.0 and 1.00 is equal", 492 hash.hashCode() != hash2.hashCode() && !hash.equals(hash2)); 493 BigInteger val = new BigInteger("100"); 494 hash2 = new BigDecimal(val, 2); 495 assertTrue("hashCode of 1.00 and 1.00(bigInteger) is not equal", hash 496 .hashCode() == hash2.hashCode() 497 && hash.equals(hash2)); 498 hash = new BigDecimal(value, 2); 499 hash2 = new BigDecimal("-1233456.0000"); 500 assertTrue("hashCode of 123459.08 and -1233456.0000 is not equal", hash 501 .hashCode() != hash2.hashCode() 502 && !hash.equals(hash2)); 503 hash2 = new BigDecimal(value.negate(), 2); 504 assertTrue("hashCode of 123459.08 and -123459.08 is not equal", hash 505 .hashCode() != hash2.hashCode() 506 && !hash.equals(hash2)); 507 } 508 509 /** 510 * @tests java.math.BigDecimal#intValue() 511 */ 512 @TestTargetNew( 513 level = TestLevel.COMPLETE, 514 notes = "", 515 method = "intValue", 516 args = {} 517 ) 518 public void test_intValue() { 519 BigDecimal int1 = new BigDecimal(value, 3); 520 assertTrue("the int value of 12345.908 is not 12345", 521 int1.intValue() == 12345); 522 int1 = new BigDecimal("1.99"); 523 assertTrue("the int value of 1.99 is not 1", int1.intValue() == 1); 524 int1 = new BigDecimal("23423419083091823091283933"); 525 // ran JDK and found representation for the above was -249268259 526 assertTrue("the int value of 23423419083091823091283933 is wrong", int1 527 .intValue() == -249268259); 528 int1 = new BigDecimal(-1235D); 529 assertTrue("the int value of -1235 is not -1235", 530 int1.intValue() == -1235); 531 } 532 533 /** 534 * @tests java.math.BigDecimal#longValue() 535 */ 536 @TestTargetNew( 537 level = TestLevel.COMPLETE, 538 notes = "", 539 method = "longValue", 540 args = {} 541 ) 542 public void test_longValue() { 543 BigDecimal long1 = new BigDecimal(value2.negate(), 0); 544 assertTrue("the long value of 12334560000 is not 12334560000", long1 545 .longValue() == -12334560000L); 546 long1 = new BigDecimal(-1345.348E-123D); 547 assertTrue("the long value of -1345.348E-123D is not zero", long1 548 .longValue() == 0); 549 long1 = new BigDecimal("31323423423419083091823091283933"); 550 // ran JDK and found representation for the above was 551 // -5251313250005125155 552 assertTrue( 553 "the long value of 31323423423419083091823091283933 is wrong", 554 long1.longValue() == -5251313250005125155L); 555 } 556 557 /** 558 * @tests java.math.BigDecimal#max(java.math.BigDecimal) 559 */ 560 @TestTargetNew( 561 level = TestLevel.COMPLETE, 562 notes = "", 563 method = "max", 564 args = {java.math.BigDecimal.class} 565 ) 566 public void test_maxLjava_math_BigDecimal() { 567 BigDecimal max1 = new BigDecimal(value2, 1); 568 BigDecimal max2 = new BigDecimal(value2, 4); 569 assertTrue("1233456000.0 is not greater than 1233456", max1.max(max2) 570 .equals(max1)); 571 max1 = new BigDecimal(-1.224D); 572 max2 = new BigDecimal(-1.2245D); 573 assertTrue("-1.224 is not greater than -1.2245", max1.max(max2).equals( 574 max1)); 575 max1 = new BigDecimal(123E18); 576 max2 = new BigDecimal(123E19); 577 assertTrue("123E19 is the not the max", max1.max(max2).equals(max2)); 578 } 579 580 /** 581 * @tests java.math.BigDecimal#min(java.math.BigDecimal) 582 */ 583 @TestTargetNew( 584 level = TestLevel.COMPLETE, 585 notes = "", 586 method = "min", 587 args = {java.math.BigDecimal.class} 588 ) 589 public void test_minLjava_math_BigDecimal() { 590 BigDecimal min1 = new BigDecimal(-12345.4D); 591 BigDecimal min2 = new BigDecimal(-12345.39D); 592 assertTrue("-12345.39 should have been returned", min1.min(min2) 593 .equals(min1)); 594 min1 = new BigDecimal(value2, 5); 595 min2 = new BigDecimal(value2, 0); 596 assertTrue("123345.6 should have been returned", min1.min(min2).equals( 597 min1)); 598 } 599 600 /** 601 * @tests java.math.BigDecimal#movePointLeft(int) 602 */ 603 @TestTargetNew( 604 level = TestLevel.PARTIAL, 605 notes = "ArithmeticException checking missed.", 606 method = "movePointLeft", 607 args = {int.class} 608 ) 609 public void test_movePointLeftI() { 610 BigDecimal movePtLeft = new BigDecimal("123456265.34"); 611 BigDecimal alreadyMoved = movePtLeft.movePointLeft(5); 612 assertTrue("move point left 5 failed", alreadyMoved.scale() == 7 613 && alreadyMoved.toString().equals("1234.5626534")); 614 movePtLeft = new BigDecimal(value2.negate(), 0); 615 alreadyMoved = movePtLeft.movePointLeft(12); 616 assertTrue("move point left 12 failed", alreadyMoved.scale() == 12 617 && alreadyMoved.toString().equals("-0.012334560000")); 618 movePtLeft = new BigDecimal(123E18); 619 alreadyMoved = movePtLeft.movePointLeft(2); 620 assertTrue("move point left 2 failed", 621 alreadyMoved.scale() == movePtLeft.scale() + 2 622 && alreadyMoved.doubleValue() == 1.23E18); 623 movePtLeft = new BigDecimal(1.123E-12); 624 alreadyMoved = movePtLeft.movePointLeft(3); 625 assertTrue("move point left 3 failed", 626 alreadyMoved.scale() == movePtLeft.scale() + 3 627 && alreadyMoved.doubleValue() == 1.123E-15); 628 movePtLeft = new BigDecimal(value, 2); 629 alreadyMoved = movePtLeft.movePointLeft(-2); 630 assertTrue("move point left -2 failed", 631 alreadyMoved.scale() == movePtLeft.scale() - 2 632 && alreadyMoved.toString().equals("12345908")); 633 } 634 635 /** 636 * @tests java.math.BigDecimal#movePointRight(int) 637 */ 638 @TestTargetNew( 639 level = TestLevel.PARTIAL, 640 notes = "ArithmeticException checking missed.", 641 method = "movePointRight", 642 args = {int.class} 643 ) 644 public void test_movePointRightI() { 645 BigDecimal movePtRight = new BigDecimal("-1.58796521458"); 646 BigDecimal alreadyMoved = movePtRight.movePointRight(8); 647 assertTrue("move point right 8 failed", alreadyMoved.scale() == 3 648 && alreadyMoved.toString().equals("-158796521.458")); 649 movePtRight = new BigDecimal(value, 2); 650 alreadyMoved = movePtRight.movePointRight(4); 651 assertTrue("move point right 4 failed", alreadyMoved.scale() == 0 652 && alreadyMoved.toString().equals("1234590800")); 653 movePtRight = new BigDecimal(134E12); 654 alreadyMoved = movePtRight.movePointRight(2); 655 assertTrue("move point right 2 failed", alreadyMoved.scale() == 0 656 && alreadyMoved.toString().equals("13400000000000000")); 657 movePtRight = new BigDecimal(-3.4E-10); 658 alreadyMoved = movePtRight.movePointRight(5); 659 assertTrue("move point right 5 failed", 660 alreadyMoved.scale() == movePtRight.scale() - 5 661 && alreadyMoved.doubleValue() == -0.000034); 662 alreadyMoved = alreadyMoved.movePointRight(-5); 663 assertTrue("move point right -5 failed", alreadyMoved 664 .equals(movePtRight)); 665 } 666 667 /** 668 * @tests java.math.BigDecimal#multiply(java.math.BigDecimal) 669 */ 670 @TestTargetNew( 671 level = TestLevel.COMPLETE, 672 notes = "", 673 method = "multiply", 674 args = {java.math.BigDecimal.class} 675 ) 676 public void test_multiplyLjava_math_BigDecimal() { 677 BigDecimal multi1 = new BigDecimal(value, 5); 678 BigDecimal multi2 = new BigDecimal(2.345D); 679 BigDecimal result = multi1.multiply(multi2); 680 assertTrue("123.45908 * 2.345 is not correct: " + result, result 681 .toString().startsWith("289.51154260") 682 && result.scale() == multi1.scale() + multi2.scale()); 683 multi1 = new BigDecimal("34656"); 684 multi2 = new BigDecimal("-2"); 685 result = multi1.multiply(multi2); 686 assertTrue("34656 * 2 is not correct", result.toString().equals( 687 "-69312") 688 && result.scale() == 0); 689 multi1 = new BigDecimal(-2.345E-02); 690 multi2 = new BigDecimal(-134E130); 691 result = multi1.multiply(multi2); 692 assertTrue("-2.345E-02 * -134E130 is not correct " + result.doubleValue(), 693 result.doubleValue() == 3.1422999999999997E130 694 && result.scale() == multi1.scale() + multi2.scale()); 695 multi1 = new BigDecimal("11235"); 696 multi2 = new BigDecimal("0"); 697 result = multi1.multiply(multi2); 698 assertTrue("11235 * 0 is not correct", result.doubleValue() == 0 699 && result.scale() == 0); 700 multi1 = new BigDecimal("-0.00234"); 701 multi2 = new BigDecimal(13.4E10); 702 result = multi1.multiply(multi2); 703 assertTrue("-0.00234 * 13.4E10 is not correct", 704 result.doubleValue() == -313560000 705 && result.scale() == multi1.scale() + multi2.scale()); 706 } 707 708 /** 709 * @tests java.math.BigDecimal#negate() 710 */ 711 @TestTargetNew( 712 level = TestLevel.COMPLETE, 713 notes = "", 714 method = "negate", 715 args = {} 716 ) 717 public void test_negate() { 718 BigDecimal negate1 = new BigDecimal(value2, 7); 719 assertTrue("the negate of 1233.4560000 is not -1233.4560000", negate1 720 .negate().toString().equals("-1233.4560000")); 721 negate1 = new BigDecimal("-23465839"); 722 assertTrue("the negate of -23465839 is not 23465839", negate1.negate() 723 .toString().equals("23465839")); 724 negate1 = new BigDecimal(-3.456E6); 725 assertTrue("the negate of -3.456E6 is not 3.456E6", negate1.negate() 726 .negate().equals(negate1)); 727 } 728 729 /** 730 * @tests java.math.BigDecimal#scale() 731 */ 732 @TestTargetNew( 733 level = TestLevel.COMPLETE, 734 notes = "", 735 method = "scale", 736 args = {} 737 ) 738 public void test_scale() { 739 BigDecimal scale1 = new BigDecimal(value2, 8); 740 assertTrue("the scale of the number 123.34560000 is wrong", scale1 741 .scale() == 8); 742 BigDecimal scale2 = new BigDecimal("29389."); 743 assertTrue("the scale of the number 29389. is wrong", 744 scale2.scale() == 0); 745 BigDecimal scale3 = new BigDecimal(3.374E13); 746 assertTrue("the scale of the number 3.374E13 is wrong", 747 scale3.scale() == 0); 748 BigDecimal scale4 = new BigDecimal("-3.45E-203"); 749 // note the scale is calculated as 15 digits of 345000.... + exponent - 750 // 1. -1 for the 3 751 assertTrue("the scale of the number -3.45E-203 is wrong: " 752 + scale4.scale(), scale4.scale() == 205); 753 scale4 = new BigDecimal("-345.4E-200"); 754 assertTrue("the scale of the number -345.4E-200 is wrong", scale4 755 .scale() == 201); 756 } 757 758 /** 759 * @tests java.math.BigDecimal#setScale(int) 760 */ 761 @TestTargetNew( 762 level = TestLevel.COMPLETE, 763 notes = "", 764 method = "setScale", 765 args = {int.class} 766 ) 767 public void test_setScaleI() { 768 // rounding mode defaults to zero 769 BigDecimal setScale1 = new BigDecimal(value, 3); 770 BigDecimal setScale2 = setScale1.setScale(5); 771 BigInteger setresult = new BigInteger("1234590800"); 772 assertTrue("the number 12345.908 after setting scale is wrong", 773 setScale2.unscaledValue().equals(setresult) 774 && setScale2.scale() == 5); 775 776 try { 777 setScale2 = setScale1.setScale(2, BigDecimal.ROUND_UNNECESSARY); 778 fail("arithmetic Exception not caught as a result of loosing precision"); 779 } catch (ArithmeticException e) { 780 } 781 } 782 783 /** 784 * @tests java.math.BigDecimal#setScale(int, int) 785 */ 786 @TestTargetNew( 787 level = TestLevel.COMPLETE, 788 notes = "", 789 method = "setScale", 790 args = {int.class, int.class} 791 ) 792 public void test_setScaleII() { 793 BigDecimal setScale1 = new BigDecimal(2.323E102); 794 BigDecimal setScale2 = setScale1.setScale(4); 795 assertTrue("the number 2.323E102 after setting scale is wrong", 796 setScale2.scale() == 4); 797 assertTrue("the representation of the number 2.323E102 is wrong", 798 setScale2.doubleValue() == 2.323E102); 799 800 setScale1 = new BigDecimal("-1.253E-12"); 801 setScale2 = setScale1.setScale(17, BigDecimal.ROUND_CEILING); 802 assertTrue("the scale of the number -1.253E-12 after setting scale is wrong", 803 setScale2.scale() == 17); 804 assertTrue( 805 "the representation of the number -1.253E-12 after setting scale is wrong, " + setScale2.toString(), 806 setScale2.toString().equals("-1.25300E-12")); 807 808 // testing rounding Mode ROUND_CEILING 809 setScale1 = new BigDecimal(value, 4); 810 setScale2 = setScale1.setScale(1, BigDecimal.ROUND_CEILING); 811 assertTrue( 812 "the number 1234.5908 after setting scale to 1/ROUND_CEILING is wrong", 813 setScale2.toString().equals("1234.6") && setScale2.scale() == 1); 814 BigDecimal setNeg = new BigDecimal(value.negate(), 4); 815 setScale2 = setNeg.setScale(1, BigDecimal.ROUND_CEILING); 816 assertTrue( 817 "the number -1234.5908 after setting scale to 1/ROUND_CEILING is wrong", 818 setScale2.toString().equals("-1234.5") 819 && setScale2.scale() == 1); 820 821 // testing rounding Mode ROUND_DOWN 822 setScale2 = setNeg.setScale(1, BigDecimal.ROUND_DOWN); 823 assertTrue( 824 "the number -1234.5908 after setting scale to 1/ROUND_DOWN is wrong", 825 setScale2.toString().equals("-1234.5") 826 && setScale2.scale() == 1); 827 setScale1 = new BigDecimal(value, 4); 828 setScale2 = setScale1.setScale(1, BigDecimal.ROUND_DOWN); 829 assertTrue( 830 "the number 1234.5908 after setting scale to 1/ROUND_DOWN is wrong", 831 setScale2.toString().equals("1234.5") && setScale2.scale() == 1); 832 833 // testing rounding Mode ROUND_FLOOR 834 setScale2 = setScale1.setScale(1, BigDecimal.ROUND_FLOOR); 835 assertTrue( 836 "the number 1234.5908 after setting scale to 1/ROUND_FLOOR is wrong", 837 setScale2.toString().equals("1234.5") && setScale2.scale() == 1); 838 setScale2 = setNeg.setScale(1, BigDecimal.ROUND_FLOOR); 839 assertTrue( 840 "the number -1234.5908 after setting scale to 1/ROUND_FLOOR is wrong", 841 setScale2.toString().equals("-1234.6") 842 && setScale2.scale() == 1); 843 844 // testing rounding Mode ROUND_HALF_DOWN 845 setScale2 = setScale1.setScale(3, BigDecimal.ROUND_HALF_DOWN); 846 assertTrue( 847 "the number 1234.5908 after setting scale to 3/ROUND_HALF_DOWN is wrong", 848 setScale2.toString().equals("1234.591") 849 && setScale2.scale() == 3); 850 setScale1 = new BigDecimal(new BigInteger("12345000"), 5); 851 setScale2 = setScale1.setScale(1, BigDecimal.ROUND_HALF_DOWN); 852 assertTrue( 853 "the number 123.45908 after setting scale to 1/ROUND_HALF_DOWN is wrong", 854 setScale2.toString().equals("123.4") && setScale2.scale() == 1); 855 setScale2 = new BigDecimal("-1234.5000").setScale(0, 856 BigDecimal.ROUND_HALF_DOWN); 857 assertTrue( 858 "the number -1234.5908 after setting scale to 0/ROUND_HALF_DOWN is wrong", 859 setScale2.toString().equals("-1234") && setScale2.scale() == 0); 860 861 // testing rounding Mode ROUND_HALF_EVEN 862 setScale1 = new BigDecimal(1.2345789D); 863 setScale2 = setScale1.setScale(4, BigDecimal.ROUND_HALF_EVEN); 864 assertTrue( 865 "the number 1.2345789 after setting scale to 4/ROUND_HALF_EVEN is wrong", 866 setScale2.doubleValue() == 1.2346D && setScale2.scale() == 4); 867 setNeg = new BigDecimal(-1.2335789D); 868 setScale2 = setNeg.setScale(2, BigDecimal.ROUND_HALF_EVEN); 869 assertTrue( 870 "the number -1.2335789 after setting scale to 2/ROUND_HALF_EVEN is wrong", 871 setScale2.doubleValue() == -1.23D && setScale2.scale() == 2); 872 setScale2 = new BigDecimal("1.2345000").setScale(3, 873 BigDecimal.ROUND_HALF_EVEN); 874 assertTrue( 875 "the number 1.2345789 after setting scale to 3/ROUND_HALF_EVEN is wrong", 876 setScale2.doubleValue() == 1.234D && setScale2.scale() == 3); 877 setScale2 = new BigDecimal("-1.2345000").setScale(3, 878 BigDecimal.ROUND_HALF_EVEN); 879 assertTrue( 880 "the number -1.2335789 after setting scale to 3/ROUND_HALF_EVEN is wrong", 881 setScale2.doubleValue() == -1.234D && setScale2.scale() == 3); 882 883 // testing rounding Mode ROUND_HALF_UP 884 setScale1 = new BigDecimal("134567.34650"); 885 setScale2 = setScale1.setScale(3, BigDecimal.ROUND_HALF_UP); 886 assertTrue( 887 "the number 134567.34658 after setting scale to 3/ROUND_HALF_UP is wrong", 888 setScale2.toString().equals("134567.347") 889 && setScale2.scale() == 3); 890 setNeg = new BigDecimal("-1234.4567"); 891 setScale2 = setNeg.setScale(0, BigDecimal.ROUND_HALF_UP); 892 assertTrue( 893 "the number -1234.4567 after setting scale to 0/ROUND_HALF_UP is wrong", 894 setScale2.toString().equals("-1234") && setScale2.scale() == 0); 895 896 // testing rounding Mode ROUND_UNNECESSARY 897 try { 898 setScale1.setScale(3, BigDecimal.ROUND_UNNECESSARY); 899 fail("arithmetic Exception not caught for round unnecessary"); 900 } catch (ArithmeticException e) { 901 } 902 903 // testing rounding Mode ROUND_UP 904 setScale1 = new BigDecimal("100000.374"); 905 setScale2 = setScale1.setScale(2, BigDecimal.ROUND_UP); 906 assertTrue( 907 "the number 100000.374 after setting scale to 2/ROUND_UP is wrong", 908 setScale2.toString().equals("100000.38") 909 && setScale2.scale() == 2); 910 setNeg = new BigDecimal(-134.34589D); 911 setScale2 = setNeg.setScale(2, BigDecimal.ROUND_UP); 912 assertTrue( 913 "the number -134.34589 after setting scale to 2/ROUND_UP is wrong", 914 setScale2.doubleValue() == -134.35D && setScale2.scale() == 2); 915 916 // testing invalid rounding modes 917 try { 918 setScale2 = setScale1.setScale(0, -123); 919 fail("IllegalArgumentException is not caught for wrong rounding mode"); 920 } catch (IllegalArgumentException e) { 921 } 922 } 923 924 /** 925 * @tests java.math.BigDecimal#setScale(int, java.math.RoundingMode) 926 */ 927 @TestTargetNew( 928 level = TestLevel.COMPLETE, 929 notes = "", 930 method = "setScale", 931 args = {int.class, java.math.RoundingMode.class} 932 ) 933 public void test_setScaleILjava_math_RoundingMode() { 934 BigDecimal setScale1 = new BigDecimal(2.323E102); 935 BigDecimal setScale2 = setScale1.setScale(4); 936 assertTrue("the number 2.323E102 after setting scale is wrong", 937 setScale2.scale() == 4); 938 assertTrue("the representation of the number 2.323E102 is wrong", 939 setScale2.doubleValue() == 2.323E102); 940 941 setScale1 = new BigDecimal("-1.253E-12"); 942 setScale2 = setScale1.setScale(17, RoundingMode.CEILING); 943 assertTrue("the scale of the number -1.253E-12 after setting scale is wrong", 944 setScale2.scale() == 17); 945 assertTrue( 946 "the representation of the number -1.253E-12 after setting scale is wrong, " + setScale2.toString(), 947 setScale2.toString().equals("-1.25300E-12")); 948 949 // testing rounding Mode RoundingMode.CEILING 950 setScale1 = new BigDecimal(value, 4); 951 setScale2 = setScale1.setScale(1, RoundingMode.CEILING); 952 assertTrue( 953 "the number 1234.5908 after setting scale to 1/RoundingMode.CEILING is wrong", 954 setScale2.toString().equals("1234.6") && setScale2.scale() == 1); 955 BigDecimal setNeg = new BigDecimal(value.negate(), 4); 956 setScale2 = setNeg.setScale(1, RoundingMode.CEILING); 957 assertTrue( 958 "the number -1234.5908 after setting scale to 1/RoundingMode.CEILING is wrong", 959 setScale2.toString().equals("-1234.5") 960 && setScale2.scale() == 1); 961 962 // testing rounding Mode RoundingMode.DOWN 963 setScale2 = setNeg.setScale(1, RoundingMode.DOWN); 964 assertTrue( 965 "the number -1234.5908 after setting scale to 1/RoundingMode.DOWN is wrong", 966 setScale2.toString().equals("-1234.5") 967 && setScale2.scale() == 1); 968 setScale1 = new BigDecimal(value, 4); 969 setScale2 = setScale1.setScale(1, RoundingMode.DOWN); 970 assertTrue( 971 "the number 1234.5908 after setting scale to 1/RoundingMode.DOWN is wrong", 972 setScale2.toString().equals("1234.5") && setScale2.scale() == 1); 973 974 // testing rounding Mode RoundingMode.FLOOR 975 setScale2 = setScale1.setScale(1, RoundingMode.FLOOR); 976 assertTrue( 977 "the number 1234.5908 after setting scale to 1/RoundingMode.FLOOR is wrong", 978 setScale2.toString().equals("1234.5") && setScale2.scale() == 1); 979 setScale2 = setNeg.setScale(1, RoundingMode.FLOOR); 980 assertTrue( 981 "the number -1234.5908 after setting scale to 1/RoundingMode.FLOOR is wrong", 982 setScale2.toString().equals("-1234.6") 983 && setScale2.scale() == 1); 984 985 // testing rounding Mode RoundingMode.HALF_DOWN 986 setScale2 = setScale1.setScale(3, RoundingMode.HALF_DOWN); 987 assertTrue( 988 "the number 1234.5908 after setting scale to 3/RoundingMode.HALF_DOWN is wrong", 989 setScale2.toString().equals("1234.591") 990 && setScale2.scale() == 3); 991 setScale1 = new BigDecimal(new BigInteger("12345000"), 5); 992 setScale2 = setScale1.setScale(1, RoundingMode.HALF_DOWN); 993 assertTrue( 994 "the number 123.45908 after setting scale to 1/RoundingMode.HALF_DOWN is wrong", 995 setScale2.toString().equals("123.4") && setScale2.scale() == 1); 996 setScale2 = new BigDecimal("-1234.5000").setScale(0, 997 RoundingMode.HALF_DOWN); 998 assertTrue( 999 "the number -1234.5908 after setting scale to 0/RoundingMode.HALF_DOWN is wrong", 1000 setScale2.toString().equals("-1234") && setScale2.scale() == 0); 1001 1002 // testing rounding Mode RoundingMode.HALF_EVEN 1003 setScale1 = new BigDecimal(1.2345789D); 1004 setScale2 = setScale1.setScale(4, RoundingMode.HALF_EVEN); 1005 assertTrue( 1006 "the number 1.2345789 after setting scale to 4/RoundingMode.HALF_EVEN is wrong", 1007 setScale2.doubleValue() == 1.2346D && setScale2.scale() == 4); 1008 setNeg = new BigDecimal(-1.2335789D); 1009 setScale2 = setNeg.setScale(2, RoundingMode.HALF_EVEN); 1010 assertTrue( 1011 "the number -1.2335789 after setting scale to 2/RoundingMode.HALF_EVEN is wrong", 1012 setScale2.doubleValue() == -1.23D && setScale2.scale() == 2); 1013 setScale2 = new BigDecimal("1.2345000").setScale(3, 1014 RoundingMode.HALF_EVEN); 1015 assertTrue( 1016 "the number 1.2345789 after setting scale to 3/RoundingMode.HALF_EVEN is wrong", 1017 setScale2.doubleValue() == 1.234D && setScale2.scale() == 3); 1018 setScale2 = new BigDecimal("-1.2345000").setScale(3, 1019 RoundingMode.HALF_EVEN); 1020 assertTrue( 1021 "the number -1.2335789 after setting scale to 3/RoundingMode.HALF_EVEN is wrong", 1022 setScale2.doubleValue() == -1.234D && setScale2.scale() == 3); 1023 1024 // testing rounding Mode RoundingMode.HALF_UP 1025 setScale1 = new BigDecimal("134567.34650"); 1026 setScale2 = setScale1.setScale(3, RoundingMode.HALF_UP); 1027 assertTrue( 1028 "the number 134567.34658 after setting scale to 3/RoundingMode.HALF_UP is wrong", 1029 setScale2.toString().equals("134567.347") 1030 && setScale2.scale() == 3); 1031 setNeg = new BigDecimal("-1234.4567"); 1032 setScale2 = setNeg.setScale(0, RoundingMode.HALF_UP); 1033 assertTrue( 1034 "the number -1234.4567 after setting scale to 0/RoundingMode.HALF_UP is wrong", 1035 setScale2.toString().equals("-1234") && setScale2.scale() == 0); 1036 1037 // testing rounding Mode RoundingMode.UNNECESSARY 1038 try { 1039 setScale1.setScale(3, RoundingMode.UNNECESSARY); 1040 fail("arithmetic Exception not caught for round unnecessary"); 1041 } catch (ArithmeticException e) { 1042 } 1043 1044 // testing rounding Mode RoundingMode.UP 1045 setScale1 = new BigDecimal("100000.374"); 1046 setScale2 = setScale1.setScale(2, RoundingMode.UP); 1047 assertTrue( 1048 "the number 100000.374 after setting scale to 2/RoundingMode.UP is wrong", 1049 setScale2.toString().equals("100000.38") 1050 && setScale2.scale() == 2); 1051 setNeg = new BigDecimal(-134.34589D); 1052 setScale2 = setNeg.setScale(2, RoundingMode.UP); 1053 assertTrue( 1054 "the number -134.34589 after setting scale to 2/RoundingMode.UP is wrong", 1055 setScale2.doubleValue() == -134.35D && setScale2.scale() == 2); 1056 1057 // testing invalid rounding modes 1058 try { 1059 setScale2 = setScale1.setScale(0, -123); 1060 fail("IllegalArgumentException is not caught for wrong rounding mode"); 1061 } catch (IllegalArgumentException e) { 1062 } 1063 } 1064 1065 /** 1066 * @tests java.math.BigDecimal#signum() 1067 */ 1068 @TestTargetNew( 1069 level = TestLevel.COMPLETE, 1070 notes = "", 1071 method = "signum", 1072 args = {} 1073 ) 1074 public void test_signum() { 1075 BigDecimal sign = new BigDecimal(123E-104); 1076 assertTrue("123E-104 is not positive in signum()", sign.signum() == 1); 1077 sign = new BigDecimal("-1234.3959"); 1078 assertTrue("-1234.3959 is not negative in signum()", 1079 sign.signum() == -1); 1080 sign = new BigDecimal(000D); 1081 assertTrue("000D is not zero in signum()", sign.signum() == 0); 1082 } 1083 1084 /** 1085 * @tests java.math.BigDecimal#subtract(java.math.BigDecimal) 1086 */ 1087 @TestTargetNew( 1088 level = TestLevel.COMPLETE, 1089 notes = "", 1090 method = "subtract", 1091 args = {java.math.BigDecimal.class} 1092 ) 1093 public void test_subtractLjava_math_BigDecimal() { 1094 BigDecimal sub1 = new BigDecimal("13948"); 1095 BigDecimal sub2 = new BigDecimal("2839.489"); 1096 BigDecimal result = sub1.subtract(sub2); 1097 assertTrue("13948 - 2839.489 is wrong: " + result, result.toString() 1098 .equals("11108.511") 1099 && result.scale() == 3); 1100 BigDecimal result2 = sub2.subtract(sub1); 1101 assertTrue("2839.489 - 13948 is wrong", result2.toString().equals( 1102 "-11108.511") 1103 && result2.scale() == 3); 1104 assertTrue("13948 - 2839.489 is not the negative of 2839.489 - 13948", 1105 result.equals(result2.negate())); 1106 sub1 = new BigDecimal(value, 1); 1107 sub2 = new BigDecimal("0"); 1108 result = sub1.subtract(sub2); 1109 assertTrue("1234590.8 - 0 is wrong", result.equals(sub1)); 1110 sub1 = new BigDecimal(1.234E-03); 1111 sub2 = new BigDecimal(3.423E-10); 1112 result = sub1.subtract(sub2); 1113 assertTrue("1.234E-03 - 3.423E-10 is wrong, " + result.doubleValue(), 1114 result.doubleValue() == 0.0012339996577); 1115 sub1 = new BigDecimal(1234.0123); 1116 sub2 = new BigDecimal(1234.0123000); 1117 result = sub1.subtract(sub2); 1118 assertTrue("1234.0123 - 1234.0123000 is wrong, " + result.doubleValue(), 1119 result.doubleValue() == 0.0); 1120 } 1121 1122 /** 1123 * @tests java.math.BigDecimal#toBigInteger() 1124 */ 1125 @TestTargetNew( 1126 level = TestLevel.COMPLETE, 1127 notes = "", 1128 method = "toBigInteger", 1129 args = {} 1130 ) 1131 public void test_toBigInteger() { 1132 BigDecimal sub1 = new BigDecimal("-29830.989"); 1133 BigInteger result = sub1.toBigInteger(); 1134 1135 assertTrue("the bigInteger equivalent of -29830.989 is wrong", result 1136 .toString().equals("-29830")); 1137 sub1 = new BigDecimal(-2837E10); 1138 result = sub1.toBigInteger(); 1139 assertTrue("the bigInteger equivalent of -2837E10 is wrong", result 1140 .doubleValue() == -2837E10); 1141 sub1 = new BigDecimal(2.349E-10); 1142 result = sub1.toBigInteger(); 1143 assertTrue("the bigInteger equivalent of 2.349E-10 is wrong", result 1144 .equals(BigInteger.ZERO)); 1145 sub1 = new BigDecimal(value2, 6); 1146 result = sub1.toBigInteger(); 1147 assertTrue("the bigInteger equivalent of 12334.560000 is wrong", result 1148 .toString().equals("12334")); 1149 } 1150 1151 /** 1152 * @tests java.math.BigDecimal#toString() 1153 */ 1154 @TestTargetNew( 1155 level = TestLevel.COMPLETE, 1156 notes = "", 1157 method = "toString", 1158 args = {} 1159 ) 1160 public void test_toString() { 1161 BigDecimal toString1 = new BigDecimal("1234.000"); 1162 assertTrue("the toString representation of 1234.000 is wrong", 1163 toString1.toString().equals("1234.000")); 1164 toString1 = new BigDecimal("-123.4E-5"); 1165 assertTrue("the toString representation of -123.4E-5 is wrong: " 1166 + toString1, toString1.toString().equals("-0.001234")); 1167 toString1 = new BigDecimal("-1.455E-20"); 1168 assertTrue("the toString representation of -1.455E-20 is wrong", 1169 toString1.toString().equals("-1.455E-20")); 1170 toString1 = new BigDecimal(value2, 4); 1171 assertTrue("the toString representation of 1233456.0000 is wrong", 1172 toString1.toString().equals("1233456.0000")); 1173 } 1174 1175 /** 1176 * @tests java.math.BigDecimal#unscaledValue() 1177 */ 1178 @TestTargetNew( 1179 level = TestLevel.COMPLETE, 1180 notes = "", 1181 method = "unscaledValue", 1182 args = {} 1183 ) 1184 public void test_unscaledValue() { 1185 BigDecimal unsVal = new BigDecimal("-2839485.000"); 1186 assertTrue("the unscaledValue of -2839485.000 is wrong", unsVal 1187 .unscaledValue().toString().equals("-2839485000")); 1188 unsVal = new BigDecimal(123E10); 1189 assertTrue("the unscaledValue of 123E10 is wrong", unsVal 1190 .unscaledValue().toString().equals("1230000000000")); 1191 unsVal = new BigDecimal("-4.56E-13"); 1192 assertTrue("the unscaledValue of -4.56E-13 is wrong: " 1193 + unsVal.unscaledValue(), unsVal.unscaledValue().toString() 1194 .equals("-456")); 1195 unsVal = new BigDecimal(value, 3); 1196 assertTrue("the unscaledValue of 12345.908 is wrong", unsVal 1197 .unscaledValue().toString().equals("12345908")); 1198 1199 } 1200 1201 /** 1202 * @tests java.math.BigDecimal#valueOf(long) 1203 */ 1204 @TestTargetNew( 1205 level = TestLevel.COMPLETE, 1206 notes = "", 1207 method = "valueOf", 1208 args = {long.class} 1209 ) 1210 public void test_valueOfJ() { 1211 BigDecimal valueOfL = BigDecimal.valueOf(9223372036854775806L); 1212 assertTrue("the bigDecimal equivalent of 9223372036854775806 is wrong", 1213 valueOfL.unscaledValue().toString().equals( 1214 "9223372036854775806") 1215 && valueOfL.scale() == 0); 1216 assertTrue( 1217 "the toString representation of 9223372036854775806 is wrong", 1218 valueOfL.toString().equals("9223372036854775806")); 1219 valueOfL = BigDecimal.valueOf(0L); 1220 assertTrue("the bigDecimal equivalent of 0 is wrong", valueOfL 1221 .unscaledValue().toString().equals("0") 1222 && valueOfL.scale() == 0); 1223 } 1224 1225 /** 1226 * @tests java.math.BigDecimal#valueOf(long, int) 1227 */ 1228 @TestTargetNew( 1229 level = TestLevel.COMPLETE, 1230 notes = "", 1231 method = "valueOf", 1232 args = {long.class, int.class} 1233 ) 1234 public void test_valueOfJI() { 1235 BigDecimal valueOfJI = BigDecimal.valueOf(9223372036854775806L, 5); 1236 assertTrue( 1237 "the bigDecimal equivalent of 92233720368547.75806 is wrong", 1238 valueOfJI.unscaledValue().toString().equals( 1239 "9223372036854775806") 1240 && valueOfJI.scale() == 5); 1241 assertTrue( 1242 "the toString representation of 9223372036854775806 is wrong", 1243 valueOfJI.toString().equals("92233720368547.75806")); 1244 valueOfJI = BigDecimal.valueOf(1234L, 8); 1245 assertTrue( 1246 "the bigDecimal equivalent of 92233720368547.75806 is wrong", 1247 valueOfJI.unscaledValue().toString().equals("1234") 1248 && valueOfJI.scale() == 8); 1249 assertTrue( 1250 "the toString representation of 9223372036854775806 is wrong", 1251 valueOfJI.toString().equals("0.00001234")); 1252 valueOfJI = BigDecimal.valueOf(0, 3); 1253 assertTrue( 1254 "the bigDecimal equivalent of 92233720368547.75806 is wrong", 1255 valueOfJI.unscaledValue().toString().equals("0") 1256 && valueOfJI.scale() == 3); 1257 assertTrue( 1258 "the toString representation of 9223372036854775806 is wrong", 1259 valueOfJI.toString().equals("0.000")); 1260 1261 } 1262 1263 @TestTargetNew( 1264 level = TestLevel.COMPLETE, 1265 notes = "Checks serialization", 1266 method = "!SerializationSelf", 1267 args = {} 1268 ) 1269 public void test_BigDecimal_serialization() throws Exception { 1270 // Regression for HARMONY-1896 1271 char[] in = { '1', '5', '6', '7', '8', '7', '.', '0', '0' }; 1272 BigDecimal bd = new BigDecimal(in, 0, 9); 1273 1274 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 1275 ObjectOutputStream oos = new ObjectOutputStream(bos); 1276 oos.writeObject(bd); 1277 1278 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); 1279 ObjectInputStream ois = new ObjectInputStream(bis); 1280 BigDecimal nbd = (BigDecimal) ois.readObject(); 1281 1282 assertEquals(bd.intValue(), nbd.intValue()); 1283 assertEquals(bd.doubleValue(), nbd.doubleValue(), 0.0); 1284 assertEquals(bd.toString(), nbd.toString()); 1285 } 1286 1287 /** 1288 * @tests java.math.BigDecimal#stripTrailingZero(long) 1289 */ 1290 @TestTargetNew( 1291 level = TestLevel.PARTIAL, 1292 notes = "The RI fails the Zero Test: has scale 4 for BigDecimal('0.0000')", 1293 method = "stripTrailingZeros", 1294 args = {} 1295 ) 1296 public void test_stripTrailingZero() { 1297 BigDecimal sixhundredtest = new BigDecimal("600.0"); 1298 assertTrue("stripTrailingZero failed for 600.0", 1299 ((sixhundredtest.stripTrailingZeros()).scale() == -2) 1300 ); 1301 1302 /* Single digit, no trailing zero, odd number */ 1303 BigDecimal notrailingzerotest = new BigDecimal("1"); 1304 assertTrue("stripTrailingZero failed for 1", 1305 ((notrailingzerotest.stripTrailingZeros()).scale() == 0) 1306 ); 1307 1308 // BEGIN android-changed: preserve RI compatibility, so BigDecimal.equals (which checks 1309 // value *and* scale) continues to work. https://issues.apache.org/jira/browse/HARMONY-4623 1310 /* Zero */ 1311 BigDecimal zerotest = new BigDecimal("0.0000"); 1312 assertEquals("stripTrailingZero failed for 0.0000", 1313 4, (zerotest.stripTrailingZeros()).scale() ); 1314 // END android-changed 1315 } 1316 1317 @TestTargetNew( 1318 level = TestLevel.PARTIAL_COMPLETE, 1319 notes = "", 1320 method = "abs", 1321 args = {MathContext.class} 1322 ) 1323 public void testMathContextConstruction() { 1324 String a = "-12380945E+61"; 1325 BigDecimal aNumber = new BigDecimal(a); 1326 int precision = 6; 1327 RoundingMode rm = RoundingMode.HALF_DOWN; 1328 MathContext mcIntRm = new MathContext(precision, rm); 1329 MathContext mcStr = new MathContext("precision=6 roundingMode=HALF_DOWN"); 1330 MathContext mcInt = new MathContext(precision); 1331 BigDecimal res = aNumber.abs(mcInt); 1332 assertEquals("MathContext Constructor with int precision failed", 1333 res, 1334 new BigDecimal("1.23809E+68")); 1335 1336 assertEquals("Equal MathContexts are not Equal ", 1337 mcIntRm, 1338 mcStr); 1339 1340 assertEquals("Different MathContext are reported as Equal ", 1341 mcInt.equals(mcStr), 1342 false); 1343 1344 assertEquals("Equal MathContexts have different hashcodes ", 1345 mcIntRm.hashCode(), 1346 mcStr.hashCode()); 1347 1348 assertEquals("MathContext.toString() returning incorrect value", 1349 mcIntRm.toString(), 1350 "precision=6 roundingMode=HALF_DOWN"); 1351 } 1352 } 1353