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  * @version $Revision$
     20  */
     21 
     22 package libcore.java.math;
     23 
     24 import java.math.BigDecimal;
     25 import java.math.MathContext;
     26 import java.math.RoundingMode;
     27 import junit.framework.TestCase;
     28 
     29 public class OldBigDecimalConvertTest extends TestCase {
     30 
     31     public void test_IntValueExactNeg() {
     32         String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
     33         BigDecimal aNumber = new BigDecimal(a);
     34         try {
     35             aNumber.intValueExact();
     36             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
     37         } catch (java.lang.ArithmeticException ae) {
     38             // expected;
     39         }
     40     }
     41 
     42     public void test_IntValueExactPos() {
     43         String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
     44         BigDecimal aNumber = new BigDecimal(a);
     45         try {
     46             aNumber.intValueExact();
     47             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
     48         } catch (java.lang.ArithmeticException ae) {
     49             // expected;
     50         }
     51     }
     52 
     53     public void test_IntValueExactFloatNeg() {
     54         BigDecimal aNumber = new BigDecimal("-2147483647.999");
     55         try {
     56             aNumber.intValueExact();
     57             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
     58         } catch (java.lang.ArithmeticException ae) {
     59             // expected;
     60         }
     61     }
     62 
     63     public void test_IntValueExactFloatPos() {
     64         float a = 2147483646.99999F;
     65         BigDecimal aNumber = new BigDecimal(a);
     66         try {
     67             aNumber.intValueExact();
     68             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
     69         } catch (java.lang.ArithmeticException ae) {
     70             // expected;
     71         }
     72     }
     73 
     74     public void test_IntValueExactLongPos() {
     75         long a = 2147483647L;
     76         BigDecimal aNumber = new BigDecimal(a);
     77         int iNumber = aNumber.intValueExact();
     78         assertTrue("incorrect value", iNumber == a);
     79     }
     80 
     81     public void test_IntValueExactLongNeg() {
     82         long a = -2147483648L;
     83         BigDecimal aNumber = new BigDecimal(a);
     84         int iNumber = aNumber.intValueExact();
     85         assertTrue("incorrect value", iNumber == a);
     86     }
     87 
     88     public void test_LongValueExactNeg() {
     89         String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
     90         BigDecimal aNumber = new BigDecimal(a);
     91         try {
     92             aNumber.longValueExact();
     93             fail("java.lang.ArithmeticException isn't thrown after calling longValueExact");
     94         } catch (java.lang.ArithmeticException ae) {
     95             // expected;
     96         }
     97     }
     98 
     99     public void test_LongValueExactPos() {
    100         String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
    101         BigDecimal aNumber = new BigDecimal(a);
    102         try {
    103             aNumber.longValueExact();
    104             fail("java.lang.ArithmeticException isn't thrown after calling longValueExact");
    105         } catch (java.lang.ArithmeticException ae) {
    106             // expected;
    107         }
    108     }
    109 
    110     public void test_LongValueExactFloatNeg() {
    111         BigDecimal aNumber = new BigDecimal("-9223372036854775807.99999");
    112         try {
    113             aNumber.longValueExact();
    114             fail("java.lang.ArithmeticException isn't thrown after calling longValueExact");
    115         } catch (java.lang.ArithmeticException ae) {
    116             // expected;
    117         }
    118     }
    119 
    120     /**
    121      * java.math.BigDecimal#longValueExact() Long value of a positive
    122      *        BigDecimal
    123      */
    124     public void test_LongValueExactFloatPos() {
    125         float a = 9223372036854775806.99999F;
    126         BigDecimal aNumber = new BigDecimal(a);
    127         try {
    128             aNumber.longValueExact();
    129             fail("java.lang.ArithmeticException isn't thrown after calling longValueExact");
    130         } catch (java.lang.ArithmeticException ae) {
    131             // expected;
    132         }
    133     }
    134 
    135     public void test_ByteValueExactPos() {
    136         int i = 127;
    137         BigDecimal bdNumber = new BigDecimal(i);
    138         byte bNumber = bdNumber.byteValueExact();
    139         assertTrue("incorrect byteValueExact", i == bNumber);
    140     }
    141 
    142     public void test_ByteValueExactNeg() {
    143         String sNumber = "-127.56789";
    144         int iNumber = -128;
    145         int iPresition = 3;
    146         MathContext mc = new MathContext(iPresition, RoundingMode.UP);
    147         BigDecimal bdNumber = new BigDecimal(sNumber, mc);
    148         byte bNumber = bdNumber.byteValueExact();
    149         assertTrue("incorrect byteValueExact", iNumber == bNumber);
    150     }
    151 
    152     public void test_ByteValueExactCharZero() {
    153         char[] cNumber = {
    154                 '-', '0', '.', '0'
    155         };
    156         int iNumber = 0;
    157         int iPresition = 5;
    158         MathContext mc = new MathContext(iPresition, RoundingMode.HALF_DOWN);
    159         BigDecimal bdNumber = new BigDecimal(cNumber, mc);
    160         byte bNumber = bdNumber.byteValueExact();
    161         assertTrue("incorrect byteValueExact", iNumber == bNumber);
    162     }
    163 
    164     public void test_ByteValueExactStringZero() {
    165         String sNumber = "00000000000000";
    166         int iNumber = 0;
    167         int iPresition = 0;
    168         MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP);
    169         BigDecimal bdNumber = new BigDecimal(sNumber, mc);
    170         byte bNumber = bdNumber.byteValueExact();
    171         assertTrue("incorrect byteValueExact", iNumber == bNumber);
    172     }
    173 
    174     public void test_ByteValueExactDoubleMax() {
    175         double dNumber = Double.MAX_VALUE;
    176         BigDecimal bdNumber = new BigDecimal(dNumber);
    177         try {
    178             bdNumber.byteValueExact();
    179             fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact");
    180         } catch (java.lang.ArithmeticException ae) {
    181             // expected
    182         }
    183     }
    184 
    185     public void test_ByteValueExactDoubleMin() {
    186         double dNumber = Double.MIN_VALUE;
    187         BigDecimal bdNumber = new BigDecimal(dNumber);
    188         try {
    189             bdNumber.byteValueExact();
    190             fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact");
    191         } catch (java.lang.ArithmeticException ae) {
    192             // expected
    193         }
    194     }
    195 
    196     public void test_ByteValueExactFloatPos() {
    197         float fNumber = 123.5445F;
    198         BigDecimal bdNumber = new BigDecimal(fNumber);
    199         try {
    200             bdNumber.byteValueExact();
    201             fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact");
    202         } catch (java.lang.ArithmeticException ae) {
    203             // expected
    204         }
    205     }
    206 
    207     public void test_ByteValueExactFloatNeg() {
    208         float fNumber = -12.987654321F;
    209         BigDecimal bdNumber = new BigDecimal(fNumber);
    210         try {
    211             bdNumber.byteValueExact();
    212             fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact");
    213         } catch (java.lang.ArithmeticException ae) {
    214             // expected
    215         }
    216     }
    217 
    218     public void test_ByteValueExactDouble() {
    219         double dNumber = 123.0000D;
    220         BigDecimal bdNumber = new BigDecimal(dNumber);
    221         byte bNumber = bdNumber.byteValueExact();
    222         assertTrue("incorrect byteValueExact", dNumber == bNumber);
    223     }
    224 
    225     public void test_ByteValueExactLongMin() {
    226         long lNumber = Long.MIN_VALUE;
    227         BigDecimal bdNumber = new BigDecimal(lNumber);
    228         try {
    229             bdNumber.byteValueExact();
    230             fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact");
    231         } catch (java.lang.ArithmeticException ae) {
    232             // expected
    233         }
    234     }
    235 
    236     public void test_ByteValueExactIntMax() {
    237         int iNumber = Integer.MAX_VALUE;
    238         BigDecimal bdNumber = new BigDecimal(iNumber);
    239         try {
    240             bdNumber.byteValueExact();
    241             fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact");
    242         } catch (java.lang.ArithmeticException ae) {
    243             // expected
    244         }
    245     }
    246 
    247     public void test_ByteValuePos() {
    248         int i = 127;
    249         BigDecimal bdNumber = new BigDecimal(i);
    250         byte bNumber = bdNumber.byteValue();
    251         assertTrue("incorrect byteValue", i == bNumber);
    252     }
    253 
    254     /**
    255      * @test java.math.BigDecimal#byteValue() Convert negative BigDesimal to
    256      *       byte type
    257      */
    258     public void test_ByteValueNeg() {
    259         String sNumber = "-127.56789";
    260         int iNumber = -128;
    261         int iPresition = 3;
    262         MathContext mc = new MathContext(iPresition, RoundingMode.UP);
    263         BigDecimal bdNumber = new BigDecimal(sNumber, mc);
    264         byte bNumber = bdNumber.byteValue();
    265         assertTrue("incorrect byteValueExact", iNumber == bNumber);
    266     }
    267 
    268     public void test_ByteValueCharZero() {
    269         char[] cNumber = {
    270                 '-', '0', '.', '0'
    271         };
    272         int iNumber = 0;
    273         int iPresition = 0;
    274         MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP);
    275         BigDecimal bdNumber = new BigDecimal(cNumber, mc);
    276         byte bNumber = bdNumber.byteValue();
    277         assertTrue("incorrect byteValue", iNumber == bNumber);
    278     }
    279 
    280     public void test_ByteValueStringZero() {
    281         String sNumber = "00000";
    282         int iNumber = 0;
    283         int iPresition = 0;
    284         MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP);
    285         BigDecimal bdNumber = new BigDecimal(sNumber, mc);
    286         byte bNumber = bdNumber.byteValue();
    287         assertTrue("incorrect byteValue", iNumber == bNumber);
    288     }
    289 
    290     public void test_ByteValueDoubleMax() {
    291         double dNumber = Double.MAX_VALUE;
    292         BigDecimal bdNumber = new BigDecimal(dNumber);
    293         int result = 0;
    294         byte bNumber = bdNumber.byteValue();
    295         assertTrue("incorrect byteValue", bNumber == result);
    296     }
    297 
    298     public void test_ByteValueDoubleMin() {
    299         double dNumber = Double.MIN_VALUE;
    300         BigDecimal bdNumber = new BigDecimal(dNumber);
    301         int result = 0;
    302         byte bNumber = bdNumber.byteValue();
    303         assertTrue("incorrect byteValue", bNumber == result);
    304     }
    305 
    306     public void test_ByteValueFloatNeg() {
    307         float fNumber = -12.987654321F;
    308         byte bValue = -12;
    309         BigDecimal bdNumber = new BigDecimal(fNumber);
    310         byte bNumber = bdNumber.byteValue();
    311         assertTrue("incorrect byteValue", bNumber == bValue);
    312     }
    313 
    314     public void test_ByteValueDouble() {
    315         double dNumber = 123.0000D;
    316         BigDecimal bdNumber = new BigDecimal(dNumber);
    317         byte bNumber = bdNumber.byteValue();
    318         assertTrue("incorrect byteValue", dNumber == bNumber);
    319     }
    320 
    321     public void test_ByteValueLongMin() {
    322         long lNumber = Long.MIN_VALUE;
    323         int result = 0;
    324         BigDecimal bdNumber = new BigDecimal(lNumber);
    325         byte bNumber = bdNumber.byteValue();
    326         assertTrue("incorrect byteValue", bNumber == result);
    327     }
    328 
    329     public void test_ByteValueIntMin() {
    330         int iNumber = Integer.MIN_VALUE;
    331         int result = 0;
    332         BigDecimal bdNumber = new BigDecimal(iNumber);
    333         byte bNumber = bdNumber.byteValue();
    334         assertTrue("incorrect byteValue", bNumber == result);
    335     }
    336 
    337     public void test_ByteValueIntMax() {
    338         int iNumber = Integer.MAX_VALUE;
    339         int result = -1;
    340         BigDecimal bdNumber = new BigDecimal(iNumber);
    341         byte bNumber = bdNumber.byteValue();
    342         assertTrue("incorrect byteValue", bNumber == result);
    343     }
    344 
    345     public void test_ShortValueNeg() {
    346         String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
    347         BigDecimal aNumber = new BigDecimal(a);
    348         int result = 23449;
    349         assertTrue("incorrect value", aNumber.shortValue() == result);
    350     }
    351 
    352     public void test_ShortValuePos() {
    353         String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
    354         BigDecimal aNumber = new BigDecimal(a);
    355         int result = -23449;
    356         assertTrue("incorrect value", aNumber.shortValue() == result);
    357     }
    358 
    359     public void test_ShortValueExactNeg() {
    360         String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
    361         BigDecimal aNumber = new BigDecimal(a);
    362         try {
    363             aNumber.shortValueExact();
    364             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    365         } catch (java.lang.ArithmeticException ae) {
    366             // expected;
    367         }
    368     }
    369 
    370     public void test_ShortValueExactPos() {
    371         String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
    372         BigDecimal aNumber = new BigDecimal(a);
    373         try {
    374             aNumber.shortValueExact();
    375             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    376         } catch (java.lang.ArithmeticException ae) {
    377             // expected;
    378         }
    379     }
    380 
    381     public void test_ShortValueExactFloatNeg() {
    382         BigDecimal aNumber = new BigDecimal("-32766.99999");
    383         try {
    384             aNumber.shortValueExact();
    385             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    386         } catch (java.lang.ArithmeticException ae) {
    387             // expected;
    388         }
    389     }
    390 
    391     public void test_ShortValueExactFloatPos() {
    392         float a = 32767.99999F;
    393         BigDecimal aNumber = new BigDecimal(a);
    394         try {
    395             aNumber.shortValueExact();
    396             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    397         } catch (java.lang.ArithmeticException ae) {
    398             // expected;
    399         }
    400     }
    401 
    402     public void test_ShortValueExactLongPos() {
    403         long a = 12345L;
    404         BigDecimal aNumber = new BigDecimal(a);
    405         short shNumber = aNumber.shortValueExact();
    406         assertTrue("incorrect value", shNumber == a);
    407     }
    408 
    409     public void test_ShortValueExactLongNeg() {
    410         long a = -12345L;
    411         BigDecimal aNumber = new BigDecimal(a);
    412         int iNumber = aNumber.shortValueExact();
    413         assertTrue("incorrect value", iNumber == a);
    414     }
    415 
    416     public void test_stripTrailingZerosZeros() {
    417 
    418         BigDecimal bdNumber = new BigDecimal("0000000");
    419         BigDecimal result = bdNumber.stripTrailingZeros();
    420         assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue());
    421         assertTrue("incorrect value", result.scale() == 0);
    422 
    423         bdNumber = new BigDecimal(0);
    424         result = bdNumber.stripTrailingZeros();
    425         assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue());
    426         assertTrue("incorrect value", result.scale() == 0);
    427 
    428         bdNumber = new BigDecimal(0.000000);
    429         result = bdNumber.stripTrailingZeros();
    430         assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue());
    431         assertTrue("incorrect value", result.scale() == 0);
    432     }
    433 
    434     public void test_stripTrailingZeros() {
    435         String s = "00000000100000000100000000.000000000100000000";
    436         int iScale = 10;
    437         BigDecimal bdValue = new BigDecimal("1000000001000000000000000001");
    438         BigDecimal bdNumber = new BigDecimal(s);
    439         BigDecimal bdResult = bdNumber.stripTrailingZeros();
    440         assertEquals("incorrect value", bdResult.unscaledValue(), bdValue.unscaledValue());
    441         assertTrue("incorrect value", bdResult.scale() == iScale);
    442 
    443         s = "1000.0";
    444         iScale = -3;
    445         BigDecimal bd = new BigDecimal("1");
    446         bdNumber = new BigDecimal(s);
    447         bdResult = bdNumber.stripTrailingZeros();
    448         assertEquals("incorrect value", bdResult.unscaledValue(), bd.unscaledValue());
    449         assertTrue("incorrect value", bdResult.scale() == iScale);
    450     }
    451 }
    452