001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.math;
032:
033: import java.math.*;
034: import net.sf.retrotranslator.tests.TestCaseBase;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class _BigDecimalTestCase extends TestCaseBase {
040:
041: public void testConstants() throws Exception {
042: assertEquals(0, BigDecimal.ZERO.intValue());
043: assertEquals(1, BigDecimal.ONE.intValue());
044: assertEquals(10, BigDecimal.TEN.intValue());
045: }
046:
047: public void testConvertConstructorArguments() throws Exception {
048: assertEquals(10L, new BigDecimal(10L).longValue());
049: assertEquals(20, new BigDecimal(20).intValue());
050: assertEquals(30, new BigDecimal("a30b".toCharArray(), 1, 2)
051: .intValue());
052: assertEquals(40, new BigDecimal("40".toCharArray()).intValue());
053: class MyDecimal extends BigDecimal {
054: public MyDecimal(int val) {
055: super (val);
056: }
057:
058: public MyDecimal(long val) {
059: super (val);
060: }
061:
062: public MyDecimal(char[] in, int offset, int len) {
063: super (in, offset, len);
064: }
065:
066: public MyDecimal(char[] in) {
067: super (in);
068: }
069: }
070: assertEquals(10L, new MyDecimal(10L).longValue());
071: assertEquals(20, new MyDecimal(20).intValue());
072: assertEquals(30, new MyDecimal("a30b".toCharArray(), 1, 2)
073: .intValue());
074: assertEquals(40, new MyDecimal("40".toCharArray()).intValue());
075: }
076:
077: public void testDivideAndRemainder() throws Exception {
078: BigDecimal dividend = BigDecimal.valueOf(123.4567);
079: assertEquals(4, dividend.scale());
080: BigDecimal divisor = BigDecimal.valueOf(8.9);
081: assertEquals(1, divisor.scale());
082: BigDecimal[] result = dividend.divideAndRemainder(divisor);
083: BigDecimal quotient = result[0];
084: BigDecimal remainder = result[1];
085: assertEquals(13.0, quotient.doubleValue());
086: assertEquals(3, quotient.scale());
087: assertEquals(7.7567, remainder.doubleValue());
088: assertEquals(4, remainder.scale());
089:
090: }
091:
092: public void testDivideToIntegralValue_scale1() throws Exception {
093: BigDecimal dividend = BigDecimal.valueOf(100, 0);
094: BigDecimal divisor = BigDecimal.valueOf(5, 1);
095: BigDecimal quotient = dividend.divideToIntegralValue(divisor);
096: assertEquals(0, BigDecimal.valueOf(200).compareTo(quotient));
097: }
098:
099: public void testDivideToIntegralValue_scale2() throws Exception {
100: BigDecimal dividend = BigDecimal.valueOf(123.456);
101: assertEquals(3, dividend.scale());
102: BigDecimal divisor = BigDecimal.valueOf(7.8);
103: assertEquals(1, divisor.scale());
104: BigDecimal quotient = dividend.divideToIntegralValue(divisor);
105: assertEquals(15.0, quotient.doubleValue());
106: assertEquals(2, quotient.scale());
107: }
108:
109: public void testDivideToIntegralValue_scale0() throws Exception {
110: BigDecimal dividend = BigDecimal.valueOf(123.4);
111: assertEquals(1, dividend.scale());
112: BigDecimal divisor = BigDecimal.valueOf(5.678);
113: assertEquals(3, divisor.scale());
114: BigDecimal quotient = dividend.divideToIntegralValue(divisor);
115: assertEquals(21.0, quotient.doubleValue());
116: assertEquals(0, quotient.scale());
117: }
118:
119: public void testDivideToIntegralValue_zeroDividend()
120: throws Exception {
121: BigDecimal dividend = BigDecimal.valueOf(0, 10);
122: assertEquals(10, dividend.scale());
123: BigDecimal divisor = BigDecimal.valueOf(1, 5);
124: assertEquals(5, divisor.scale());
125: BigDecimal quotient = dividend.divideToIntegralValue(divisor);
126: assertEquals(0.0, quotient.doubleValue());
127: assertEquals(5, quotient.scale());
128: }
129:
130: public void testDivideToIntegralValue_zeroDivider()
131: throws Exception {
132: BigDecimal dividend = BigDecimal.valueOf(1);
133: BigDecimal divisor = BigDecimal.valueOf(0);
134: try {
135: dividend.divideToIntegralValue(divisor);
136: fail();
137: } catch (ArithmeticException e) {
138: //ok
139: }
140: }
141:
142: public void testPow() throws Exception {
143: assertEquals(1, new BigDecimal(10).pow(0).intValue());
144: assertEquals(10, new BigDecimal(10).pow(1).intValue());
145: assertEquals(100, new BigDecimal(10).pow(2).intValue());
146: assertEquals(100000, new BigDecimal(10).pow(5).intValue());
147: assertEquals(8, BigDecimal.valueOf(20, 1).pow(3).intValue());
148: assertEquals(0, BigDecimal.valueOf(0, Integer.MAX_VALUE / 2)
149: .pow(3).unscaledValue().intValue());
150: try {
151: int n = -10;
152: new BigDecimal(10).pow(n);
153: fail();
154: } catch (ArithmeticException e) {
155: //ok
156: }
157: try {
158: BigDecimal.valueOf(1, Integer.MAX_VALUE / 2).pow(3);
159: fail();
160: } catch (ArithmeticException e) {
161: //ok
162: }
163: }
164:
165: public void testSetScale() throws Exception {
166: assertEquals("123.5", new BigDecimal(123.45).setScale(1,
167: BigDecimal.ROUND_HALF_EVEN).toPlainString());
168: assertEquals("120", new BigDecimal(123).setScale(-1,
169: BigDecimal.ROUND_HALF_EVEN).toPlainString());
170: }
171:
172: public void testRemainder() throws Exception {
173: BigDecimal dividend = BigDecimal.valueOf(12.3);
174: assertEquals(1, dividend.scale());
175: BigDecimal divisor = BigDecimal.valueOf(4.5678);
176: assertEquals(4, divisor.scale());
177: BigDecimal remainder = dividend.remainder(divisor);
178: assertEquals(3.1644, remainder.doubleValue());
179: assertEquals(4, remainder.scale());
180: }
181:
182: public void testToPlainString() throws Exception {
183: assertEquals("1230", new BigDecimal(1230).toPlainString());
184: assertEquals("1.23", new BigDecimal(BigInteger.valueOf(123), 2)
185: .toPlainString());
186: }
187:
188: public void testValueOf() throws Exception {
189: assertEquals(1.23, BigDecimal.valueOf(1.23).doubleValue());
190: assertEquals(123, BigDecimal.valueOf(123).longValue());
191: }
192:
193: public void testDivide() {
194: assertEquals(BigDecimal.valueOf(2, 1), BigDecimal.valueOf(1, 0)
195: .divide(BigDecimal.valueOf(5, 0)));
196: assertEquals(BigDecimal.valueOf(5, 1), BigDecimal.valueOf(1, 0)
197: .divide(BigDecimal.valueOf(2, 0)));
198: assertEquals(BigDecimal.valueOf(3, 1), BigDecimal.valueOf(9, 0)
199: .divide(BigDecimal.valueOf(30, 0)));
200: assertEquals(BigDecimal.valueOf(-3, 1), BigDecimal
201: .valueOf(9, 0).divide(BigDecimal.valueOf(-30, 0)));
202: assertEquals(BigDecimal.valueOf(5, 3), BigDecimal.valueOf(1, 5)
203: .divide(BigDecimal.valueOf(2, 3)));
204: assertEquals(BigDecimal.valueOf(3, 2), BigDecimal.valueOf(9, 3)
205: .divide(BigDecimal.valueOf(30, 2)));
206: assertEquals(BigDecimal.valueOf(0, 3), BigDecimal.valueOf(0, 5)
207: .divide(BigDecimal.valueOf(1, 2)));
208: assertEquals(BigDecimal.valueOf(0, 0), BigDecimal.valueOf(0, 0)
209: .divide(BigDecimal.valueOf(1, 0)));
210: assertEquals(0, BigDecimal.valueOf(50, 0).compareTo(
211: BigDecimal.valueOf(1, 3).divide(
212: BigDecimal.valueOf(2, 5))));
213: }
214:
215: public void testDivide_Java5() {
216: if (isJava5AtLeast()) {
217: assertEquals(BigDecimal.valueOf(0, Integer.MAX_VALUE),
218: BigDecimal.valueOf(0, Integer.MAX_VALUE).divide(
219: BigDecimal.valueOf(2, -10)));
220: assertEquals(BigDecimal.valueOf(0, Integer.MIN_VALUE),
221: BigDecimal.valueOf(0, Integer.MIN_VALUE).divide(
222: BigDecimal.valueOf(2, 10)));
223: }
224: }
225:
226: public void testDivide_Large2() {
227: BigDecimal p = BigDecimal.valueOf(3, 0);
228: BigDecimal q = new BigDecimal(BigInteger.valueOf(2).pow(600)
229: .multiply(BigInteger.valueOf(125)), 0);
230: assertEquals(0, p.divide(q).multiply(q).compareTo(p));
231: }
232:
233: public void testDivide_Large5() {
234: BigDecimal p = BigDecimal.valueOf(3, 0);
235: BigDecimal q = new BigDecimal(BigInteger.valueOf(5).pow(600)
236: .multiply(BigInteger.valueOf(8)), 0);
237: assertEquals(0, p.divide(q).multiply(q).compareTo(p));
238: }
239:
240: public void testDivide_Exceptions() {
241: try {
242: BigDecimal.valueOf(1, Integer.MAX_VALUE).divide(
243: BigDecimal.valueOf(2, 0));
244: fail();
245: } catch (ArithmeticException e) {
246: //ok
247: }
248: try {
249: BigDecimal.valueOf(1, 0).divide(BigDecimal.valueOf(0));
250: fail();
251: } catch (ArithmeticException e) {
252: //ok
253: }
254: try {
255: BigDecimal.valueOf(0).divide(BigDecimal.valueOf(0));
256: fail();
257: } catch (ArithmeticException e) {
258: //ok
259: }
260: }
261:
262: }
|