Home | History | Annotate | Download | only in math
      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