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 junit.framework.TestCase;
26 
27 public class OldBigDecimalScaleOperationsTest extends TestCase {
28 
testMovePointRightEx()29     public void testMovePointRightEx() {
30         BigDecimal a = new BigDecimal("12345.6789012345678901234567890123456789");
31         BigDecimal res = a.movePointRight(10);
32         assertEquals("incorrect scale", 24, res.scale());
33         assertEquals("incorrect value", "123456789012345.678901234567890123456789", res.toString());
34         res = a.movePointRight(-50);
35         assertEquals("incorrect scale", 84, res.scale());
36         assertEquals("incorrect value", "1.23456789012345678901234567890123456789E-46", res.toString());
37         try {
38             a.movePointRight(Integer.MIN_VALUE + 2);
39             fail("ArithmeticException is not thrown");
40         } catch (ArithmeticException expected) {
41         }
42     }
43 
44     // Throws OutOfMemoryError instead of ArithmeticException!
testMovePointRightEx2()45     public void testMovePointRightEx2() {
46         BigDecimal a = new BigDecimal("123456789012345678901234567890123456789E25");
47         try {
48             a.movePointRight(Integer.MAX_VALUE - 2);
49             fail("ArithmeticException is not thrown");
50         } catch (ArithmeticException expected) {
51         }
52     }
53 
testScaleByPowerOfTenEx()54     public void testScaleByPowerOfTenEx() {
55         BigDecimal a = new BigDecimal("12345.6789012345678901234567890123456789");
56         BigDecimal res = a.movePointRight(10);
57         assertEquals("incorrect scale", 24, res.scale());
58         assertEquals("incorrect value", "123456789012345.678901234567890123456789", res.toString());
59         res = a.scaleByPowerOfTen(-50);
60         assertEquals("incorrect scale", 84, res.scale());
61         assertEquals("incorrect value", "1.23456789012345678901234567890123456789E-46", res.toString());
62         res = a.scaleByPowerOfTen(50);
63         assertEquals("incorrect scale", -16, res.scale());
64         assertEquals("incorrect value", "1.23456789012345678901234567890123456789E+54", res.toString());
65         try {
66             a.scaleByPowerOfTen(Integer.MIN_VALUE + 2);
67             fail("ArithmeticException is not thrown");
68         } catch (ArithmeticException expected) {
69         }
70         a = new BigDecimal("123456789012345678901234567890123456789E25");
71         try {
72             a.scaleByPowerOfTen(Integer.MAX_VALUE - 2);
73             fail("ArithmeticException is not thrown");
74         } catch (ArithmeticException expected) {
75         }
76     }
77 
78     /**
79      * check that setScale with a scale greater to the existing scale does not
80      * change the value.
81      */
testSetScale()82     public void testSetScale() {
83         BigDecimal x1 = new BigDecimal(1.23400);
84         BigDecimal x2 = x1.setScale(75);
85 
86         assertEquals(0, x1.compareTo(x2));
87         assertEquals(0, x2.compareTo(x1));
88 
89         x1.precision();
90 
91         assertEquals(0, x1.compareTo(x2));
92         assertEquals(0, x2.compareTo(x1));
93 
94         x2.precision();
95 
96         assertEquals(0, x1.compareTo(x2));
97         assertEquals(0, x2.compareTo(x1));
98     }
99 }
100