Home | History | Annotate | Download | only in math
      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 
     21 package libcore.java.math;
     22 
     23 import java.math.BigDecimal;
     24 import java.math.BigInteger;
     25 import java.math.MathContext;
     26 import java.math.RoundingMode;
     27 import junit.framework.TestCase;
     28 
     29 public class OldBigDecimalConstructorsTest extends TestCase {
     30 
     31     /**
     32      * new BigDecimal(BigInteger value, MathContext)
     33      */
     34     public void testConstrBigIntegerMathContext() {
     35         String a = "1231212478987482988429808779810457634781384756794987";
     36         BigInteger bA = new BigInteger(a);
     37         int precision = 46;
     38         RoundingMode rm = RoundingMode.CEILING;
     39         MathContext mc = new MathContext(precision, rm);
     40         String res = "1231212478987482988429808779810457634781384757";
     41         int resScale = -6;
     42         BigDecimal result = new BigDecimal(bA, mc);
     43         assertEquals("incorrect value", res, result.unscaledValue().toString());
     44         assertEquals("incorrect scale", resScale, result.scale());
     45 
     46         // Now test more than just RoundingMode.CEILING:
     47         //
     48         BigDecimal bd;
     49         BigInteger bi =  new BigInteger( "12345678901234567890123456789012345");
     50         BigInteger nbi = new BigInteger("-12345678901234567890123456789012345");
     51 
     52         mc = new MathContext(31, RoundingMode.UP);
     53         bd = new BigDecimal(bi, mc);
     54         assertEquals("incorrect value",  "1.234567890123456789012345678902E+34", bd.toString());
     55         bd = new BigDecimal(nbi, mc);
     56         assertEquals("incorrect value", "-1.234567890123456789012345678902E+34", bd.toString());
     57 
     58         mc = new MathContext(28, RoundingMode.DOWN);
     59         bd = new BigDecimal(bi, mc);
     60         assertEquals("incorrect value",  "1.234567890123456789012345678E+34", bd.toString());
     61         bd = new BigDecimal(nbi, mc);
     62         assertEquals("incorrect value", "-1.234567890123456789012345678E+34", bd.toString());
     63 
     64         mc = new MathContext(33, RoundingMode.CEILING);
     65         bd = new BigDecimal(bi, mc);
     66         assertEquals("incorrect value",  "1.23456789012345678901234567890124E+34", bd.toString());
     67         bd = new BigDecimal(nbi, mc);
     68         assertEquals("incorrect value", "-1.23456789012345678901234567890123E+34", bd.toString());
     69 
     70         mc = new MathContext(34, RoundingMode.FLOOR);
     71         bd = new BigDecimal(bi, mc);
     72         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+34", bd.toString());
     73         bd = new BigDecimal(nbi, mc);
     74         assertEquals("incorrect value", "-1.234567890123456789012345678901235E+34", bd.toString());
     75 
     76         mc = new MathContext(34, RoundingMode.HALF_EVEN);
     77         bd = new BigDecimal(bi, mc);
     78         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+34", bd.toString());
     79         bd = new BigDecimal(nbi, mc);
     80         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+34", bd.toString());
     81         bd = new BigDecimal(new BigInteger("-12345678901234567890123456789012335"), mc);
     82         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+34", bd.toString());
     83 
     84         mc = new MathContext(34, RoundingMode.HALF_UP);
     85         bd = new BigDecimal(bi, mc);
     86         assertEquals("incorrect value",  "1.234567890123456789012345678901235E+34", bd.toString());
     87         bd = new BigDecimal(nbi, mc);
     88         assertEquals("incorrect value", "-1.234567890123456789012345678901235E+34", bd.toString());
     89 
     90         mc = new MathContext(34, RoundingMode.HALF_DOWN);
     91         bd = new BigDecimal(bi, mc);
     92         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+34", bd.toString());
     93         bd = new BigDecimal(nbi, mc);
     94         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+34", bd.toString());
     95 
     96         mc = new MathContext(34, RoundingMode.UNNECESSARY);
     97         try {
     98             bd = new BigDecimal(bi, mc);
     99             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    100         } catch (ArithmeticException e) {
    101             // expected
    102         }
    103         try {
    104             bd = new BigDecimal(nbi, mc);
    105             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    106         } catch (ArithmeticException e) {
    107             // expected
    108         }
    109     }
    110 
    111     /**
    112      * new BigDecimal(BigInteger value, int scale, MathContext)
    113      */
    114     public void testConstrBigIntegerScaleMathContext() {
    115         String a = "1231212478987482988429808779810457634781384756794987";
    116         BigInteger bA = new BigInteger(a);
    117         int aScale = 10;
    118         int precision = 46;
    119         RoundingMode rm = RoundingMode.CEILING;
    120         MathContext mc = new MathContext(precision, rm);
    121         String res = "1231212478987482988429808779810457634781384757";
    122         int resScale = 4;
    123         BigDecimal result = new BigDecimal(bA, aScale, mc);
    124         assertEquals("incorrect value", res, result.unscaledValue().toString());
    125         assertEquals("incorrect scale", resScale, result.scale());
    126 
    127         // Now test more than just RoundingMode.CEILING:
    128         //
    129         // ATTENTION:
    130         //   The remaining section is TEXTUALLY COPIED
    131         //   from testConstrBigIntegerMathContext
    132         //   with minor repetitive modifications.
    133         //
    134         BigDecimal bd;
    135 
    136         BigInteger bi =  new BigInteger( "12345678901234567890123456789012345");
    137         BigInteger nbi = new BigInteger("-12345678901234567890123456789012345");
    138 
    139         mc = new MathContext(31, RoundingMode.UP);
    140         bd = new BigDecimal(bi, -10, mc);
    141         assertEquals("incorrect value",  "1.234567890123456789012345678902E+44", bd.toString());
    142         bd = new BigDecimal(nbi, -10, mc);
    143         assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
    144 
    145         mc = new MathContext(28, RoundingMode.DOWN);
    146         bd = new BigDecimal(bi, -10, mc);
    147         assertEquals("incorrect value",  "1.234567890123456789012345678E+44", bd.toString());
    148         bd = new BigDecimal(nbi, -10, mc);
    149         assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
    150 
    151         mc = new MathContext(33, RoundingMode.CEILING);
    152         bd = new BigDecimal(bi, -10, mc);
    153         assertEquals("incorrect value",  "1.23456789012345678901234567890124E+44", bd.toString());
    154         bd = new BigDecimal(nbi, -10, mc);
    155         assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
    156 
    157         mc = new MathContext(34, RoundingMode.FLOOR);
    158         bd = new BigDecimal(bi, -10, mc);
    159         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+44", bd.toString());
    160         bd = new BigDecimal(nbi, -10, mc);
    161         assertEquals("incorrect value", "-1.234567890123456789012345678901235E+44", bd.toString());
    162 
    163         mc = new MathContext(34, RoundingMode.HALF_EVEN);
    164         bd = new BigDecimal(bi, -10, mc);
    165         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+44", bd.toString());
    166         bd = new BigDecimal(nbi, -10, mc);
    167         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+44", bd.toString());
    168         bd = new BigDecimal(new BigInteger("-12345678901234567890123456789012335"), -10, mc);
    169         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+44", bd.toString());
    170 
    171         mc = new MathContext(34, RoundingMode.HALF_UP);
    172         bd = new BigDecimal(bi, -10, mc);
    173         assertEquals("incorrect value",  "1.234567890123456789012345678901235E+44", bd.toString());
    174         bd = new BigDecimal(nbi, -10, mc);
    175         assertEquals("incorrect value", "-1.234567890123456789012345678901235E+44", bd.toString());
    176 
    177         mc = new MathContext(34, RoundingMode.HALF_DOWN);
    178         bd = new BigDecimal(bi, -10, mc);
    179         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+44", bd.toString());
    180         bd = new BigDecimal(nbi, -10, mc);
    181         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+44", bd.toString());
    182 
    183         mc = new MathContext(34, RoundingMode.UNNECESSARY);
    184         try {
    185             bd = new BigDecimal(bi, -10, mc);
    186             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    187         } catch (ArithmeticException e) {
    188             // expected
    189         }
    190         try {
    191             bd = new BigDecimal(nbi, -10, mc);
    192             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    193         } catch (ArithmeticException e) {
    194             // expected
    195         }
    196 
    197         // And just TEXTUALLY COPIED again:
    198         //
    199         mc = new MathContext(31, RoundingMode.UP);
    200         bd = new BigDecimal(bi, 10, mc);
    201         assertEquals("incorrect value",  "1234567890123456789012345.678902", bd.toString());
    202         bd = new BigDecimal(nbi, 10, mc);
    203         assertEquals("incorrect value", "-1234567890123456789012345.678902", bd.toString());
    204 
    205         mc = new MathContext(28, RoundingMode.DOWN);
    206         bd = new BigDecimal(bi, 10, mc);
    207         assertEquals("incorrect value",  "1234567890123456789012345.678", bd.toString());
    208         bd = new BigDecimal(nbi, 10, mc);
    209         assertEquals("incorrect value", "-1234567890123456789012345.678", bd.toString());
    210 
    211         mc = new MathContext(33, RoundingMode.CEILING);
    212         bd = new BigDecimal(bi, 10, mc);
    213         assertEquals("incorrect value",  "1234567890123456789012345.67890124", bd.toString());
    214         bd = new BigDecimal(nbi, 10, mc);
    215         assertEquals("incorrect value", "-1234567890123456789012345.67890123", bd.toString());
    216 
    217         mc = new MathContext(34, RoundingMode.FLOOR);
    218         bd = new BigDecimal(bi, 10, mc);
    219         assertEquals("incorrect value",  "1234567890123456789012345.678901234", bd.toString());
    220         bd = new BigDecimal(nbi, 10, mc);
    221         assertEquals("incorrect value", "-1234567890123456789012345.678901235", bd.toString());
    222 
    223         mc = new MathContext(34, RoundingMode.HALF_EVEN);
    224         bd = new BigDecimal(bi, 10, mc);
    225         assertEquals("incorrect value",  "1234567890123456789012345.678901234", bd.toString());
    226         bd = new BigDecimal(nbi, 10, mc);
    227         assertEquals("incorrect value", "-1234567890123456789012345.678901234", bd.toString());
    228         bd = new BigDecimal(new BigInteger("-12345678901234567890123456789012335"), 10, mc);
    229         assertEquals("incorrect value", "-1234567890123456789012345.678901234", bd.toString());
    230 
    231         mc = new MathContext(34, RoundingMode.HALF_UP);
    232         bd = new BigDecimal(bi, 10, mc);
    233         assertEquals("incorrect value",  "1234567890123456789012345.678901235", bd.toString());
    234         bd = new BigDecimal(nbi, 10, mc);
    235         assertEquals("incorrect value", "-1234567890123456789012345.678901235", bd.toString());
    236 
    237         mc = new MathContext(34, RoundingMode.HALF_DOWN);
    238         bd = new BigDecimal(bi, 10, mc);
    239         assertEquals("incorrect value",  "1234567890123456789012345.678901234", bd.toString());
    240         bd = new BigDecimal(nbi, 10, mc);
    241         assertEquals("incorrect value", "-1234567890123456789012345.678901234", bd.toString());
    242 
    243         mc = new MathContext(34, RoundingMode.UNNECESSARY);
    244         try {
    245             bd = new BigDecimal(bi, 10, mc);
    246             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    247         } catch (ArithmeticException e) {
    248             // expected
    249         }
    250         try {
    251             bd = new BigDecimal(nbi, 10, mc);
    252             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    253         } catch (ArithmeticException e) {
    254             // expected
    255         }
    256 
    257         mc = new MathContext(28, RoundingMode.FLOOR);
    258         bd = new BigDecimal(bi, 10, mc);
    259         assertEquals("incorrect value",  "1234567890123456789012345.678", bd.toString());
    260         bd = new BigDecimal(nbi, 10, mc);
    261         assertEquals("incorrect value", "-1234567890123456789012345.679", bd.toString());
    262     }
    263 
    264     public void testConstrBigIntegerScaleMathContext_AndroidFailure() {
    265         MathContext mc;
    266         BigDecimal bd;
    267 
    268         mc = new MathContext(17, RoundingMode.FLOOR);
    269         bd = new BigDecimal(new BigInteger("123456789012345678"), 3, mc);
    270         assertEquals("incorrect value", "123456789012345.67", bd.toString());
    271     }
    272 
    273 
    274     /**
    275      * new BigDecimal(char[] value, int offset, int len, MathContext mc);
    276      */
    277     public void testConstrCharIntIntMathContext() {
    278         char value[] = {'-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3'};
    279         int offset = 3;
    280         int len = 12;
    281         int precision = 4;
    282         RoundingMode rm = RoundingMode.CEILING;
    283         MathContext mc = new MathContext(precision, rm);
    284         BigDecimal result = new BigDecimal(value, offset, len, mc);
    285         String res = "3.805E-40";
    286         int resScale = 43;
    287         assertEquals("incorrect value", res, result.toString());
    288         assertEquals("incorrect scale", resScale, result.scale());
    289 
    290         try {
    291             // Regression for HARMONY-783
    292             new BigDecimal(new char[] {}, 0, 0, MathContext.DECIMAL32);
    293             fail("NumberFormatException has not been thrown");
    294         } catch (NumberFormatException e) {
    295         }
    296 
    297         // Now test more than just RoundingMode.CEILING:
    298         //
    299         // ATTENTION:
    300         //   The remaining section is TEXTUALLY COPIED
    301         //   from testConstrBigIntegerScaleMathContext
    302         //   with minor repetitive modifications.
    303         //
    304         char[] biCA =  "bla: 12345678901234567890123456789012345.0E+10, and more bla".toCharArray();
    305         char[] nbiCA = "bla: -12345678901234567890123456789012345.E+10, and more bla".toCharArray();
    306         BigDecimal bd;
    307 
    308         mc = new MathContext(31, RoundingMode.UP);
    309         bd = new BigDecimal(biCA, 5, 41, mc);
    310         assertEquals("incorrect value",  "1.234567890123456789012345678902E+44", bd.toString());
    311         bd = new BigDecimal(nbiCA, 5, 41, mc);
    312         assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
    313 
    314         mc = new MathContext(28, RoundingMode.DOWN);
    315         bd = new BigDecimal(biCA, 5, 41, mc);
    316         assertEquals("incorrect value",  "1.234567890123456789012345678E+44", bd.toString());
    317         bd = new BigDecimal(nbiCA, 5, 41, mc);
    318         assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
    319 
    320         mc = new MathContext(33, RoundingMode.CEILING);
    321         bd = new BigDecimal(biCA, 5, 41, mc);
    322         assertEquals("incorrect value",  "1.23456789012345678901234567890124E+44", bd.toString());
    323         bd = new BigDecimal(nbiCA, 5, 41, mc);
    324         assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
    325 
    326         mc = new MathContext(34, RoundingMode.FLOOR);
    327         bd = new BigDecimal(biCA, 5, 41, mc);
    328         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+44", bd.toString());
    329         bd = new BigDecimal(nbiCA, 5, 41, mc);
    330         assertEquals("incorrect value", "-1.234567890123456789012345678901235E+44", bd.toString());
    331 
    332         mc = new MathContext(34, RoundingMode.HALF_EVEN);
    333         bd = new BigDecimal(biCA, 5, 41, mc);
    334         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+44", bd.toString());
    335         bd = new BigDecimal(nbiCA, 5, 41, mc);
    336         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+44", bd.toString());
    337         bd = new BigDecimal("-123456789012345678901234567890123350000000000".toCharArray(), 0, 46, mc);
    338         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+44", bd.toString());
    339 
    340         mc = new MathContext(34, RoundingMode.HALF_UP);
    341         bd = new BigDecimal(biCA, 5, 41, mc);
    342         assertEquals("incorrect value",  "1.234567890123456789012345678901235E+44", bd.toString());
    343         bd = new BigDecimal(nbiCA, 5, 41, mc);
    344         assertEquals("incorrect value", "-1.234567890123456789012345678901235E+44", bd.toString());
    345 
    346         mc = new MathContext(34, RoundingMode.HALF_DOWN);
    347         bd = new BigDecimal(biCA, 5, 41, mc);
    348         assertEquals("incorrect value",  "1.234567890123456789012345678901234E+44", bd.toString());
    349         bd = new BigDecimal(nbiCA, 5, 41, mc);
    350         assertEquals("incorrect value", "-1.234567890123456789012345678901234E+44", bd.toString());
    351 
    352         mc = new MathContext(34, RoundingMode.UNNECESSARY);
    353         try {
    354             bd = new BigDecimal(biCA, 5, 41, mc);
    355             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    356         } catch (ArithmeticException e) {
    357             // expected
    358         }
    359         try {
    360             bd = new BigDecimal(nbiCA, 5, 41, mc);
    361             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    362         } catch (ArithmeticException e) {
    363             // expected
    364         }
    365     }
    366 
    367     /**
    368      * new BigDecimal(char[] value, int offset, int len, MathContext mc);
    369      */
    370     public void testConstrCharIntIntMathContextException1() {
    371         char value[] = {'-', '1', '2', '3', '8', '0', '.', '4', '7', '3', '8', 'E', '-', '4', '2', '3'};
    372         int offset = 3;
    373         int len = 120;
    374         int precision = 4;
    375         RoundingMode rm = RoundingMode.CEILING;
    376         MathContext mc = new MathContext(precision, rm);
    377         try {
    378             new BigDecimal(value, offset, len, mc);
    379             fail("NumberFormatException has not been thrown");
    380         } catch (NumberFormatException e) {
    381         }
    382      }
    383 
    384     /**
    385      * new BigDecimal(char[] value, MathContext mc);
    386      */
    387     public void testConstrCharMathContext() {
    388         // Now test more than just regression
    389         // (even if for quite sure the implementation will use the offset/len variant internally):
    390         //
    391         char[] biCA =  "12345678901234567890123456789012345.0E+10".toCharArray();
    392         char[] nbiCA = "-12345678901234567890123456789012345.E+10".toCharArray();
    393         BigDecimal bd;
    394         MathContext mc;
    395 
    396         mc = new MathContext(31, RoundingMode.UP);
    397         bd = new BigDecimal(biCA, mc);
    398         assertEquals("incorrect value",  "1.234567890123456789012345678902E+44", bd.toString());
    399         bd = new BigDecimal(nbiCA, mc);
    400         assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
    401 
    402         mc = new MathContext(28, RoundingMode.DOWN);
    403         bd = new BigDecimal(biCA, mc);
    404         assertEquals("incorrect value",  "1.234567890123456789012345678E+44", bd.toString());
    405         bd = new BigDecimal(nbiCA, mc);
    406         assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
    407 
    408         mc = new MathContext(33, RoundingMode.CEILING);
    409         bd = new BigDecimal(biCA, mc);
    410         assertEquals("incorrect value",  "1.23456789012345678901234567890124E+44", bd.toString());
    411         bd = new BigDecimal(nbiCA, mc);
    412         assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
    413 
    414         mc = new MathContext(34, RoundingMode.UNNECESSARY);
    415         try {
    416             bd = new BigDecimal(biCA, mc);
    417             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    418         } catch (ArithmeticException e) {
    419             // expected
    420         }
    421         try {
    422             bd = new BigDecimal(nbiCA, mc);
    423             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    424         } catch (ArithmeticException e) {
    425             // expected
    426         }
    427     }
    428 
    429     /**
    430      * new BigDecimal(double, MathContext)
    431      */
    432     public void testConstrDoubleMathContext() {
    433         double a = 732546982374982347892379283571094797.287346782359284756;
    434         int precision = 21;
    435         RoundingMode rm = RoundingMode.CEILING;
    436         MathContext mc = new MathContext(precision, rm);
    437         String res = "732546982374982285074";
    438         int resScale = -15;
    439         BigDecimal result = new BigDecimal(a, mc);
    440         assertEquals("incorrect value", res, result.unscaledValue().toString());
    441         assertEquals("incorrect scale", resScale, result.scale());
    442 
    443         // Now test more than just RoundingMode.CEILING
    444         //
    445         BigDecimal bd;
    446 
    447         mc = new MathContext(9, RoundingMode.UP);
    448         bd = new BigDecimal(123456789.125, mc);
    449         assertEquals("incorrect value",  "123456790", bd.toString());
    450         bd = new BigDecimal(-123456789.125, mc);
    451         assertEquals("incorrect value", "-123456790", bd.toString());
    452 
    453         mc = new MathContext(8, RoundingMode.DOWN);
    454         bd = new BigDecimal(123456789.125, mc);
    455         assertEquals("incorrect value",  "1.2345678E+8", bd.toString());
    456         bd = new BigDecimal(-123456789.125, mc);
    457         assertEquals("incorrect value", "-1.2345678E+8", bd.toString());
    458 
    459         mc = new MathContext(10, RoundingMode.CEILING);
    460         bd = new BigDecimal(123456789.125, mc);
    461         assertEquals("incorrect value",  "123456789.2", bd.toString());
    462         bd = new BigDecimal(-123456789.125, mc);
    463         assertEquals("incorrect value", "-123456789.1", bd.toString());
    464 
    465         mc = new MathContext(8, RoundingMode.FLOOR);
    466         bd = new BigDecimal(123456789.125, mc);
    467         assertEquals("incorrect value",  "1.2345678E+8", bd.toString());
    468         bd = new BigDecimal(-123456789.125, mc);
    469         assertEquals("incorrect value", "-1.2345679E+8", bd.toString());
    470 
    471         mc = new MathContext(11, RoundingMode.HALF_EVEN);
    472         //
    473         // VERY FUNNY:
    474         // This works:
    475         bd = new BigDecimal("123456789.125", mc);
    476         assertEquals("incorrect value",  "123456789.12", bd.toString());
    477         // But this doesn't:
    478 //        bd = new BigDecimal(123456789.125, mc);
    479 //        assertEquals("incorrect value",  "123456789.12", bd.toString());
    480 
    481 //        bd = new BigDecimal(-123456789.125, mc);
    482 //        assertEquals("incorrect value", "-123456789.12", bd.toString());
    483         bd = new BigDecimal(-123456789.135, mc);
    484         assertEquals("incorrect value", "-123456789.14", bd.toString());
    485 
    486         mc = new MathContext(11, RoundingMode.HALF_UP);
    487         bd = new BigDecimal("123456789.125", mc);
    488         assertEquals("incorrect value",  "123456789.13", bd.toString());
    489 
    490         // AND HERE, TOO:
    491 //        mc = new MathContext(11, RoundingMode.HALF_UP);
    492 //        bd = new BigDecimal(123456789.125, mc);
    493 //        assertEquals("incorrect value",  "123456789.13", bd.toString());
    494 //        bd = new BigDecimal(-123456789.125, mc);
    495 //        assertEquals("incorrect value", "-123456789.13", bd.toString());
    496 
    497         mc = new MathContext(11, RoundingMode.HALF_DOWN);
    498         //
    499         // SAME HERE:
    500         // This works:
    501         bd = new BigDecimal("123456789.125", mc);
    502         assertEquals("incorrect value",  "123456789.12", bd.toString());
    503         // But this doesn't:
    504 //        bd = new BigDecimal(123456789.125, mc);
    505 //        assertEquals("incorrect value",  "123456789.12", bd.toString());
    506 
    507 //        bd = new BigDecimal(123456789.125, mc);
    508 //        assertEquals("incorrect value",  "123456789.12", bd.toString());
    509 //        bd = new BigDecimal(-123456789.125, mc);
    510 //        assertEquals("incorrect value", "-123456789.12", bd.toString());
    511 
    512         mc = new MathContext(8, RoundingMode.UNNECESSARY);
    513         try {
    514             bd = new BigDecimal(123456789.125, mc);
    515             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    516         } catch (ArithmeticException e) {
    517             // expected
    518         }
    519         try {
    520             bd = new BigDecimal(-123456789.125, mc);
    521             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    522         } catch (ArithmeticException e) {
    523             // expected
    524         }
    525     }
    526 
    527     public void testConstrDoubleMathContext_AndroidFailure() {
    528         BigDecimal bd;
    529         MathContext mc;
    530 
    531         mc = new MathContext(11, RoundingMode.HALF_EVEN);
    532         //
    533         // VERY FUNNY:
    534         // This works:
    535         bd = new BigDecimal("123456789.125", mc);
    536         assertEquals("incorrect value",  "123456789.12", bd.toString());
    537         // But this doesn't:
    538         bd = new BigDecimal(123456789.125, mc);
    539         assertEquals("incorrect value",  "123456789.12", bd.toString());
    540 
    541         bd = new BigDecimal(-123456789.125, mc);
    542         assertEquals("incorrect value", "-123456789.12", bd.toString());
    543 
    544         // AND HERE, TOO:
    545         mc = new MathContext(11, RoundingMode.HALF_UP);
    546         bd = new BigDecimal(123456789.125, mc);
    547         assertEquals("incorrect value",  "123456789.13", bd.toString());
    548         bd = new BigDecimal(-123456789.125, mc);
    549         assertEquals("incorrect value", "-123456789.13", bd.toString());
    550 
    551         mc = new MathContext(11, RoundingMode.HALF_DOWN);
    552         //
    553         // SAME HERE:
    554         // This works:
    555         bd = new BigDecimal("123456789.125", mc);
    556         assertEquals("incorrect value",  "123456789.12", bd.toString());
    557         // But this doesn't:
    558         bd = new BigDecimal(123456789.125, mc);
    559         assertEquals("incorrect value",  "123456789.12", bd.toString());
    560 
    561         bd = new BigDecimal(123456789.125, mc);
    562         assertEquals("incorrect value",  "123456789.12", bd.toString());
    563         bd = new BigDecimal(-123456789.125, mc);
    564         assertEquals("incorrect value", "-123456789.12", bd.toString());
    565     }
    566 
    567     /**
    568      * new BigDecimal(long, MathContext)
    569      */
    570     public void testConstrLongMathContext() {
    571         long a = 4576578677732546982L;
    572         int precision = 5;
    573         RoundingMode rm = RoundingMode.CEILING;
    574         MathContext mc = new MathContext(precision, rm);
    575         String res = "45766";
    576         int resScale = -14;
    577         BigDecimal result = new BigDecimal(a, mc);
    578         assertEquals("incorrect value", res, result.unscaledValue().toString());
    579         assertEquals("incorrect scale", resScale, result.scale());
    580 
    581         // Now test more than just RoundingMode.CEILING
    582         //
    583         BigDecimal bd;
    584 
    585         mc = new MathContext(15, RoundingMode.UP);
    586         bd = new BigDecimal(78901234567890125L, mc);
    587         assertEquals("incorrect value",  "7.89012345678902E+16", bd.toString());
    588         bd = new BigDecimal(-78901234567890125L, mc);
    589         assertEquals("incorrect value", "-7.89012345678902E+16", bd.toString());
    590 
    591         mc = new MathContext(12, RoundingMode.DOWN);
    592         bd = new BigDecimal(78901234567890125L, mc);
    593         assertEquals("incorrect value",  "7.89012345678E+16", bd.toString());
    594         bd = new BigDecimal(-78901234567890125L, mc);
    595         assertEquals("incorrect value", "-7.89012345678E+16", bd.toString());
    596 
    597         mc = new MathContext(15, RoundingMode.CEILING);
    598         bd = new BigDecimal(78901234567890125L, mc);
    599         assertEquals("incorrect value",  "7.89012345678902E+16", bd.toString());
    600         bd = new BigDecimal(-78901234567890125L, mc);
    601         assertEquals("incorrect value", "-7.89012345678901E+16", bd.toString());
    602 
    603         mc = new MathContext(12, RoundingMode.FLOOR);
    604         bd = new BigDecimal(78901234567890125L, mc);
    605         assertEquals("incorrect value",  "7.89012345678E+16", bd.toString());
    606         bd = new BigDecimal(-78901234567890125L, mc);
    607         assertEquals("incorrect value", "-7.89012345679E+16", bd.toString());
    608 
    609         mc = new MathContext(16, RoundingMode.HALF_EVEN);
    610         bd = new BigDecimal(78901234567890125L, mc);
    611         assertEquals("incorrect value",  "7.890123456789012E+16", bd.toString());
    612         bd = new BigDecimal(-78901234567890125L, mc);
    613         assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
    614         bd = new BigDecimal(-78901234567890135L, mc);
    615         assertEquals("incorrect value", "-7.890123456789014E+16", bd.toString());
    616 
    617         mc = new MathContext(16, RoundingMode.HALF_UP);
    618         bd = new BigDecimal(78901234567890125L, mc);
    619         assertEquals("incorrect value",  "7.890123456789013E+16", bd.toString());
    620         bd = new BigDecimal(-78901234567890125L, mc);
    621         assertEquals("incorrect value", "-7.890123456789013E+16", bd.toString());
    622 
    623         mc = new MathContext(16, RoundingMode.HALF_DOWN);
    624         bd = new BigDecimal(78901234567890125L, mc);
    625         assertEquals("incorrect value",  "7.890123456789012E+16", bd.toString());
    626         bd = new BigDecimal(-78901234567890125L, mc);
    627         assertEquals("incorrect value", "-7.890123456789012E+16", bd.toString());
    628 
    629         mc = new MathContext(8, RoundingMode.UNNECESSARY);
    630         try {
    631             bd = new BigDecimal(78901234567890125L, mc);
    632             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    633         } catch (ArithmeticException e) {
    634             // expected
    635         }
    636         try {
    637             bd = new BigDecimal(-78901234567890125L, mc);
    638             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    639         } catch (ArithmeticException e) {
    640             // expected
    641         }
    642     }
    643 
    644     /**
    645      * new BigDecimal(String value)
    646      * when value is not a valid representation of BigDecimal.
    647      */
    648     public void testConstrStringException() {
    649         String a = "-238768.787678287a+10";
    650         try {
    651             BigDecimal bd = new BigDecimal(a);
    652             fail("NumberFormatException has not been caught: " + bd.toString());
    653         } catch (NumberFormatException e) {}
    654     }
    655 
    656     /**
    657      * new BigDecimal(String value, MathContext)
    658      */
    659     public void testConstrStringMathContext() {
    660         String a = "-238768787678287e214";
    661         int precision = 5;
    662         RoundingMode rm = RoundingMode.CEILING;
    663         MathContext mc = new MathContext(precision, rm);
    664         String res = "-23876";
    665         int resScale = -224;
    666         BigDecimal result = new BigDecimal(a, mc);
    667         assertEquals("incorrect value", res, result.unscaledValue().toString());
    668         assertEquals("incorrect scale", resScale, result.scale());
    669 
    670         // Now test more than just RoundingMode.CEILING:
    671         //
    672         String biStr = new String( "12345678901234567890123456789012345.0E+10");
    673         String nbiStr = new String("-12345678901234567890123456789012345.E+10");
    674         BigDecimal bd;
    675 
    676         mc = new MathContext(31, RoundingMode.UP);
    677         bd = new BigDecimal(biStr, mc);
    678         assertEquals("incorrect value",  "1.234567890123456789012345678902E+44", bd.toString());
    679         bd = new BigDecimal(nbiStr, mc);
    680         assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString());
    681 
    682         mc = new MathContext(28, RoundingMode.DOWN);
    683         bd = new BigDecimal(biStr, mc);
    684         assertEquals("incorrect value",  "1.234567890123456789012345678E+44", bd.toString());
    685         bd = new BigDecimal(nbiStr, mc);
    686         assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString());
    687 
    688         mc = new MathContext(33, RoundingMode.CEILING);
    689         bd = new BigDecimal(biStr, mc);
    690         assertEquals("incorrect value",  "1.23456789012345678901234567890124E+44", bd.toString());
    691         bd = new BigDecimal(nbiStr, mc);
    692         assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString());
    693 
    694         mc = new MathContext(34, RoundingMode.UNNECESSARY);
    695         try {
    696             bd = new BigDecimal(biStr, mc);
    697             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    698         } catch (ArithmeticException e) {
    699             // expected
    700         }
    701         try {
    702             bd = new BigDecimal(nbiStr, mc);
    703             fail("No ArithmeticException for RoundingMode.UNNECESSARY");
    704         } catch (ArithmeticException e) {
    705             // expected
    706         }
    707 
    708         mc = new MathContext(7, RoundingMode.FLOOR);
    709         bd = new BigDecimal("1000000.9", mc);
    710         assertEquals("incorrect value", "1000000", bd.toString());
    711     }
    712 
    713 // ANDROID ADDED
    714 
    715     /**
    716      * java.math.BigDecimal#BigDecimal(java.math.BigInteger, int)
    717      */
    718     public void test_Constructor_java_math_BigInteger_int() {
    719         BigInteger value = new BigInteger("12345908");
    720         BigDecimal big = new BigDecimal(value);
    721         assertTrue("the BigDecimal value is not initialized properly",
    722                 big.unscaledValue().equals(value)
    723                 && big.scale() == 0);
    724 
    725         BigInteger value2 = new BigInteger("12334560000");
    726         BigDecimal big2 = new BigDecimal(value2, 5);
    727         assertTrue("the BigDecimal value is not initialized properly",
    728                 big2.unscaledValue().equals(value2)
    729                 && big2.scale() == 5);
    730         assertTrue("the BigDecimal value is not represented properly", big2.toString().equals(
    731                 "123345.60000"));
    732     }
    733 
    734     /**
    735      * java.math.BigDecimal#BigDecimal(double)
    736      */
    737     public void test_Constructor_Double() {
    738         BigDecimal big = new BigDecimal(123E04);
    739         assertTrue("the BigDecimal value taking a double argument is not initialized properly", big
    740                 .toString().equals("1230000"));
    741         big = new BigDecimal(1.2345E-12);
    742         assertTrue("the double representation is not correct for 1.2345E-12",
    743                 big.doubleValue() == 1.2345E-12);
    744         big = new BigDecimal(-12345E-3);
    745         assertTrue("the double representation is not correct for -12345E-3",
    746                 big.doubleValue() == -12.345);
    747         big = new BigDecimal(5.1234567897654321e138);
    748         assertTrue("the double representation is not correct for 5.1234567897654321e138", big
    749                 .doubleValue() == 5.1234567897654321E138
    750                 && big.scale() == 0);
    751         big = new BigDecimal(0.1);
    752         assertTrue("the double representation of 0.1 bigDecimal is not correct",
    753                 big.doubleValue() == 0.1);
    754         big = new BigDecimal(0.00345);
    755         assertTrue("the double representation of 0.00345 bigDecimal is not correct", big
    756                 .doubleValue() == 0.00345);
    757         // regression test for HARMONY-2429
    758         big = new BigDecimal(-0.0);
    759         assertTrue("the double representation of -0.0 bigDecimal is not correct", big.scale() == 0);
    760     }
    761 
    762     /**
    763      * java.math.BigDecimal#BigDecimal(java.lang.String)
    764      */
    765     public void test_Constructor_java_lang_String() throws NumberFormatException {
    766         BigDecimal big = new BigDecimal("345.23499600293850");
    767         assertTrue("the BigDecimal value is not initialized properly", big.toString().equals(
    768                 "345.23499600293850")
    769                 && big.scale() == 14);
    770         big = new BigDecimal("-12345");
    771         assertTrue("the BigDecimal value is not initialized properly", big.toString().equals(
    772                 "-12345")
    773                 && big.scale() == 0);
    774         big = new BigDecimal("123.");
    775         assertTrue("the BigDecimal value is not initialized properly", big.toString().equals("123")
    776                 && big.scale() == 0);
    777 
    778     }
    779 }
    780