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     public void test_ByteValueNeg() {
    255         String sNumber = "-127.56789";
    256         int iNumber = -128;
    257         int iPresition = 3;
    258         MathContext mc = new MathContext(iPresition, RoundingMode.UP);
    259         BigDecimal bdNumber = new BigDecimal(sNumber, mc);
    260         byte bNumber = bdNumber.byteValue();
    261         assertTrue("incorrect byteValueExact", iNumber == bNumber);
    262     }
    263 
    264     public void test_ByteValueCharZero() {
    265         char[] cNumber = {
    266                 '-', '0', '.', '0'
    267         };
    268         int iNumber = 0;
    269         int iPresition = 0;
    270         MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP);
    271         BigDecimal bdNumber = new BigDecimal(cNumber, mc);
    272         byte bNumber = bdNumber.byteValue();
    273         assertTrue("incorrect byteValue", iNumber == bNumber);
    274     }
    275 
    276     public void test_ByteValueStringZero() {
    277         String sNumber = "00000";
    278         int iNumber = 0;
    279         int iPresition = 0;
    280         MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP);
    281         BigDecimal bdNumber = new BigDecimal(sNumber, mc);
    282         byte bNumber = bdNumber.byteValue();
    283         assertTrue("incorrect byteValue", iNumber == bNumber);
    284     }
    285 
    286     public void test_ByteValueDoubleMax() {
    287         double dNumber = Double.MAX_VALUE;
    288         BigDecimal bdNumber = new BigDecimal(dNumber);
    289         int result = 0;
    290         byte bNumber = bdNumber.byteValue();
    291         assertTrue("incorrect byteValue", bNumber == result);
    292     }
    293 
    294     public void test_ByteValueDoubleMin() {
    295         double dNumber = Double.MIN_VALUE;
    296         BigDecimal bdNumber = new BigDecimal(dNumber);
    297         int result = 0;
    298         byte bNumber = bdNumber.byteValue();
    299         assertTrue("incorrect byteValue", bNumber == result);
    300     }
    301 
    302     public void test_ByteValueFloatNeg() {
    303         float fNumber = -12.987654321F;
    304         byte bValue = -12;
    305         BigDecimal bdNumber = new BigDecimal(fNumber);
    306         byte bNumber = bdNumber.byteValue();
    307         assertTrue("incorrect byteValue", bNumber == bValue);
    308     }
    309 
    310     public void test_ByteValueDouble() {
    311         double dNumber = 123.0000D;
    312         BigDecimal bdNumber = new BigDecimal(dNumber);
    313         byte bNumber = bdNumber.byteValue();
    314         assertTrue("incorrect byteValue", dNumber == bNumber);
    315     }
    316 
    317     public void test_ByteValueLongMin() {
    318         long lNumber = Long.MIN_VALUE;
    319         int result = 0;
    320         BigDecimal bdNumber = new BigDecimal(lNumber);
    321         byte bNumber = bdNumber.byteValue();
    322         assertTrue("incorrect byteValue", bNumber == result);
    323     }
    324 
    325     public void test_ByteValueIntMin() {
    326         int iNumber = Integer.MIN_VALUE;
    327         int result = 0;
    328         BigDecimal bdNumber = new BigDecimal(iNumber);
    329         byte bNumber = bdNumber.byteValue();
    330         assertTrue("incorrect byteValue", bNumber == result);
    331     }
    332 
    333     public void test_ByteValueIntMax() {
    334         int iNumber = Integer.MAX_VALUE;
    335         int result = -1;
    336         BigDecimal bdNumber = new BigDecimal(iNumber);
    337         byte bNumber = bdNumber.byteValue();
    338         assertTrue("incorrect byteValue", bNumber == result);
    339     }
    340 
    341     public void test_ShortValueNeg() {
    342         String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
    343         BigDecimal aNumber = new BigDecimal(a);
    344         int result = 23449;
    345         assertTrue("incorrect value", aNumber.shortValue() == result);
    346     }
    347 
    348     public void test_ShortValuePos() {
    349         String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
    350         BigDecimal aNumber = new BigDecimal(a);
    351         int result = -23449;
    352         assertTrue("incorrect value", aNumber.shortValue() == result);
    353     }
    354 
    355     public void test_ShortValueExactNeg() {
    356         String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
    357         BigDecimal aNumber = new BigDecimal(a);
    358         try {
    359             aNumber.shortValueExact();
    360             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    361         } catch (java.lang.ArithmeticException ae) {
    362             // expected;
    363         }
    364     }
    365 
    366     public void test_ShortValueExactPos() {
    367         String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
    368         BigDecimal aNumber = new BigDecimal(a);
    369         try {
    370             aNumber.shortValueExact();
    371             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    372         } catch (java.lang.ArithmeticException ae) {
    373             // expected;
    374         }
    375     }
    376 
    377     public void test_ShortValueExactFloatNeg() {
    378         BigDecimal aNumber = new BigDecimal("-32766.99999");
    379         try {
    380             aNumber.shortValueExact();
    381             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    382         } catch (java.lang.ArithmeticException ae) {
    383             // expected;
    384         }
    385     }
    386 
    387     public void test_ShortValueExactFloatPos() {
    388         float a = 32767.99999F;
    389         BigDecimal aNumber = new BigDecimal(a);
    390         try {
    391             aNumber.shortValueExact();
    392             fail("java.lang.ArithmeticException isn't thrown after calling intValueExact");
    393         } catch (java.lang.ArithmeticException ae) {
    394             // expected;
    395         }
    396     }
    397 
    398     public void test_ShortValueExactLongPos() {
    399         long a = 12345L;
    400         BigDecimal aNumber = new BigDecimal(a);
    401         short shNumber = aNumber.shortValueExact();
    402         assertTrue("incorrect value", shNumber == a);
    403     }
    404 
    405     public void test_ShortValueExactLongNeg() {
    406         long a = -12345L;
    407         BigDecimal aNumber = new BigDecimal(a);
    408         int iNumber = aNumber.shortValueExact();
    409         assertTrue("incorrect value", iNumber == a);
    410     }
    411 
    412     public void test_stripTrailingZerosZeros() {
    413 
    414         BigDecimal bdNumber = new BigDecimal("0000000");
    415         BigDecimal result = bdNumber.stripTrailingZeros();
    416         assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue());
    417         assertTrue("incorrect value", result.scale() == 0);
    418 
    419         bdNumber = new BigDecimal(0);
    420         result = bdNumber.stripTrailingZeros();
    421         assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue());
    422         assertTrue("incorrect value", result.scale() == 0);
    423 
    424         bdNumber = new BigDecimal(0.000000);
    425         result = bdNumber.stripTrailingZeros();
    426         assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue());
    427         assertTrue("incorrect value", result.scale() == 0);
    428     }
    429 
    430     public void test_stripTrailingZeros() {
    431         String s = "00000000100000000100000000.000000000100000000";
    432         int iScale = 10;
    433         BigDecimal bdValue = new BigDecimal("1000000001000000000000000001");
    434         BigDecimal bdNumber = new BigDecimal(s);
    435         BigDecimal bdResult = bdNumber.stripTrailingZeros();
    436         assertEquals("incorrect value", bdResult.unscaledValue(), bdValue.unscaledValue());
    437         assertTrue("incorrect value", bdResult.scale() == iScale);
    438 
    439         s = "1000.0";
    440         iScale = -3;
    441         BigDecimal bd = new BigDecimal("1");
    442         bdNumber = new BigDecimal(s);
    443         bdResult = bdNumber.stripTrailingZeros();
    444         assertEquals("incorrect value", bdResult.unscaledValue(), bd.unscaledValue());
    445         assertTrue("incorrect value", bdResult.scale() == iScale);
    446     }
    447 }
    448