KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > math > _BigDecimalTestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package net.sf.retrotranslator.runtime.java.math;
33
34 import java.math.*;
35 import net.sf.retrotranslator.tests.BaseTestCase;
36
37 /**
38  * @author Taras Puchko
39  */

40 public class _BigDecimalTestCase extends BaseTestCase {
41
42     public void testConstants() throws Exception JavaDoc {
43         assertEquals(0, BigDecimal.ZERO.intValue());
44         assertEquals(1, BigDecimal.ONE.intValue());
45         assertEquals(10, BigDecimal.TEN.intValue());
46     }
47
48     public void testConvertConstructorArguments() throws Exception JavaDoc {
49         assertEquals(10L, new BigDecimal(10L).longValue());
50         assertEquals(20, new BigDecimal(20).intValue());
51         class MyDecimal extends BigDecimal {
52             public MyDecimal(int val) {
53                 super(val);
54             }
55
56             public MyDecimal(long val) {
57                 super(val);
58             }
59         }
60         assertEquals(10L, new MyDecimal(10L).longValue());
61         assertEquals(20, new MyDecimal(20).intValue());
62     }
63
64     public void testDivideAndRemainder() throws Exception JavaDoc {
65         BigDecimal dividend = BigDecimal.valueOf(123.4567);
66         assertEquals(4, dividend.scale());
67         BigDecimal divisor = BigDecimal.valueOf(8.9);
68         assertEquals(1, divisor.scale());
69         BigDecimal[] result = dividend.divideAndRemainder(divisor);
70         BigDecimal quotient = result[0];
71         BigDecimal remainder = result[1];
72         assertEquals(13.0, quotient.doubleValue());
73         assertEquals(3, quotient.scale());
74         assertEquals(7.7567, remainder.doubleValue());
75         assertEquals(4, remainder.scale());
76
77     }
78
79     public void testDivideToIntegralValue_scale2() throws Exception JavaDoc {
80         BigDecimal dividend = BigDecimal.valueOf(123.456);
81         assertEquals(3, dividend.scale());
82         BigDecimal divisor = BigDecimal.valueOf(7.8);
83         assertEquals(1, divisor.scale());
84         BigDecimal quotient = dividend.divideToIntegralValue(divisor);
85         assertEquals(15.0, quotient.doubleValue());
86         assertEquals(2, quotient.scale());
87     }
88
89     public void testDivideToIntegralValue_scale0() throws Exception JavaDoc {
90         BigDecimal dividend = BigDecimal.valueOf(123.4);
91         assertEquals(1, dividend.scale());
92         BigDecimal divisor = BigDecimal.valueOf(5.678);
93         assertEquals(3, divisor.scale());
94         BigDecimal quotient = dividend.divideToIntegralValue(divisor);
95         assertEquals(21.0, quotient.doubleValue());
96         assertEquals(0, quotient.scale());
97     }
98
99     public void testDivideToIntegralValue_zeroDividend() throws Exception JavaDoc {
100         BigDecimal dividend = BigDecimal.valueOf(0, 10);
101         assertEquals(10, dividend.scale());
102         BigDecimal divisor = BigDecimal.valueOf(1, 5);
103         assertEquals(5, divisor.scale());
104         BigDecimal quotient = dividend.divideToIntegralValue(divisor);
105         assertEquals(0.0, quotient.doubleValue());
106         assertEquals(5, quotient.scale());
107     }
108
109     public void testDivideToIntegralValue_zeroDivider() throws Exception JavaDoc {
110         BigDecimal dividend = BigDecimal.valueOf(1);
111         BigDecimal divisor = BigDecimal.valueOf(0);
112         try {
113             dividend.divideToIntegralValue(divisor);
114             fail();
115         } catch (ArithmeticException JavaDoc e) {
116             //ok
117
}
118     }
119
120     public void testPow() throws Exception JavaDoc {
121         assertEquals(1, new BigDecimal(10).pow(0).intValue());
122         assertEquals(10, new BigDecimal(10).pow(1).intValue());
123         assertEquals(100, new BigDecimal(10).pow(2).intValue());
124         assertEquals(100000, new BigDecimal(10).pow(5).intValue());
125         try {
126             new BigDecimal(10).pow(-10);
127             fail();
128         } catch (ArithmeticException JavaDoc e) {
129             //ok
130
}
131     }
132
133     public void testSetScale() throws Exception JavaDoc {
134         assertEquals("123.5", new BigDecimal(123.45).setScale(1, BigDecimal.ROUND_HALF_EVEN).toPlainString());
135         assertEquals("120", new BigDecimal(123).setScale(-1, BigDecimal.ROUND_HALF_EVEN).toPlainString());
136     }
137
138     public void testRemainder() throws Exception JavaDoc {
139         BigDecimal dividend = BigDecimal.valueOf(12.3);
140         assertEquals(1, dividend.scale());
141         BigDecimal divisor = BigDecimal.valueOf(4.5678);
142         assertEquals(4, divisor.scale());
143         BigDecimal remainder = dividend.remainder(divisor);
144         assertEquals(3.1644, remainder.doubleValue());
145         assertEquals(4, remainder.scale());
146     }
147
148     public void testToPlainString() throws Exception JavaDoc {
149         assertEquals("1230", new BigDecimal(1230).toPlainString());
150         assertEquals("1.23", new BigDecimal(BigInteger.valueOf(123), 2).toPlainString());
151     }
152
153     public void testValueOf() throws Exception JavaDoc {
154         assertEquals(1.23, BigDecimal.valueOf(1.23).doubleValue());
155         assertEquals(123, BigDecimal.valueOf(123).longValue());
156     }
157
158 }
Popular Tags