1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.java.math; 18 19 import java.math.BigDecimal; 20 import java.math.MathContext; 21 import java.math.RoundingMode; 22 import junit.framework.TestCase; 23 24 public final class BigDecimalTest extends TestCase { 25 26 public void testGetPrecision() { 27 assertPrecision(1, "0"); 28 assertPrecision(1, "0.9"); 29 assertPrecision(16, "0.9999999999999999"); 30 assertPrecision(16, "9999999999999999"); 31 assertPrecision(19, "1000000000000000000"); 32 assertPrecision(19, "1000000000000000001"); 33 assertPrecision(19, "-1000000000000000001"); 34 assertPrecision(19, "-1000000000000000000"); 35 36 String tenNines = "9999999999"; 37 String fiftyNines = tenNines + tenNines + tenNines + tenNines + tenNines; 38 assertPrecision(10, "0." + tenNines); 39 assertPrecision(50, "0." + fiftyNines); 40 assertPrecision(250, "0." + fiftyNines + fiftyNines + fiftyNines + fiftyNines + fiftyNines); 41 assertPrecision(10, tenNines); 42 assertPrecision(50, fiftyNines); 43 assertPrecision(250, fiftyNines + fiftyNines + fiftyNines + fiftyNines + fiftyNines); 44 45 // test these special cases because we know precision() uses longs internally 46 String maxLong = Long.toString(Long.MAX_VALUE); 47 assertPrecision(maxLong.length(), maxLong); 48 String minLong = Long.toString(Long.MIN_VALUE); 49 assertPrecision(minLong.length() - 1, minLong); 50 } 51 52 private void assertPrecision(int expectedPrecision, String value) { 53 BigDecimal parsed = new BigDecimal(value); 54 assertEquals("Unexpected precision for parsed value " + value, 55 expectedPrecision, parsed.precision()); 56 57 BigDecimal computed = parsed.divide(BigDecimal.ONE); 58 assertEquals("Unexpected precision for computed value " + value, 59 expectedPrecision, computed.precision()); 60 } 61 62 public void testRound() { 63 BigDecimal bigDecimal = new BigDecimal("0.999999999999999"); 64 BigDecimal rounded = bigDecimal.round(new MathContext(2, RoundingMode.FLOOR)); 65 assertEquals("0.99", rounded.toString()); 66 } 67 68 // https://code.google.com/p/android/issues/detail?id=43480 69 public void testPrecisionFromString() { 70 BigDecimal a = new BigDecimal("-0.011111111111111111111"); 71 BigDecimal b = a.multiply(BigDecimal.ONE); 72 73 assertEquals("-0.011111111111111111111", a.toString()); 74 assertEquals("-0.011111111111111111111", b.toString()); 75 76 assertEquals(20, a.precision()); 77 assertEquals(20, b.precision()); 78 79 assertEquals(21, a.scale()); 80 assertEquals(21, b.scale()); 81 82 assertEquals("-11111111111111111111", a.unscaledValue().toString()); 83 assertEquals("-11111111111111111111", b.unscaledValue().toString()); 84 85 assertEquals(a, b); 86 assertEquals(b, a); 87 88 assertEquals(0, a.subtract(b).signum()); 89 assertEquals(0, a.compareTo(b)); 90 } 91 92 // https://code.google.com/p/android/issues/detail?id=54580 93 public void test54580() { 94 BigDecimal a = new BigDecimal("1.200002"); 95 assertEquals("1.200002", a.toPlainString()); 96 assertEquals("1.20", a.abs(new MathContext(3,RoundingMode.HALF_UP)).toPlainString()); 97 assertEquals("1.200002", a.toPlainString()); 98 } 99 } 100