001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Elena Semukhina
019: * @version $Revision$
020: */package org.apache.harmony.tests.java.math;
021:
022: import junit.framework.TestCase;
023: import java.math.*;
024:
025: /**
026: * Class: java.math.BigDecimal
027: * Methods: movePointLeft, movePointRight, scale, setScale, unscaledValue *
028: */
029: public class BigDecimalScaleOperationsTest extends TestCase {
030: /**
031: * Check the default scale
032: */
033: public void testScaleDefault() {
034: String a = "1231212478987482988429808779810457634781384756794987";
035: int cScale = 0;
036: BigDecimal aNumber = new BigDecimal(new BigInteger(a));
037: assertTrue("incorrect scale", aNumber.scale() == cScale);
038: }
039:
040: /**
041: * Check a negative scale
042: */
043: public void testScaleNeg() {
044: String a = "1231212478987482988429808779810457634781384756794987";
045: int aScale = -10;
046: int cScale = -10;
047: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
048: assertTrue("incorrect scale", aNumber.scale() == cScale);
049: }
050:
051: /**
052: * Check a positive scale
053: */
054: public void testScalePos() {
055: String a = "1231212478987482988429808779810457634781384756794987";
056: int aScale = 10;
057: int cScale = 10;
058: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
059: assertTrue("incorrect scale", aNumber.scale() == cScale);
060: }
061:
062: /**
063: * Check the zero scale
064: */
065: public void testScaleZero() {
066: String a = "1231212478987482988429808779810457634781384756794987";
067: int aScale = 0;
068: int cScale = 0;
069: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
070: assertTrue("incorrect scale", aNumber.scale() == cScale);
071: }
072:
073: /**
074: * Check the unscaled value
075: */
076: public void testUnscaledValue() {
077: String a = "1231212478987482988429808779810457634781384756794987";
078: int aScale = 100;
079: BigInteger bNumber = new BigInteger(a);
080: BigDecimal aNumber = new BigDecimal(bNumber, aScale);
081: assertTrue("incorrect unscaled value", aNumber.unscaledValue()
082: .equals(bNumber));
083: }
084:
085: /**
086: * Set a greater new scale
087: */
088: public void testSetScaleGreater() {
089: String a = "1231212478987482988429808779810457634781384756794987";
090: int aScale = 18;
091: int newScale = 28;
092: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
093: BigDecimal bNumber = aNumber.setScale(newScale);
094: assertTrue("incorrect scale", bNumber.scale() == newScale);
095: assertEquals("incorrect value", 0, bNumber.compareTo(aNumber));
096: }
097:
098: /**
099: * Set a less new scale; this.scale == 8; newScale == 5.
100: */
101: public void testSetScaleLess() {
102: String a = "2.345726458768760000E+10";
103: int newScale = 5;
104: BigDecimal aNumber = new BigDecimal(a);
105: BigDecimal bNumber = aNumber.setScale(newScale);
106: assertTrue("incorrect scale", bNumber.scale() == newScale);
107: assertEquals("incorrect value", 0, bNumber.compareTo(aNumber));
108: }
109:
110: /**
111: * Verify an exception when setting a new scale
112: */
113: public void testSetScaleException() {
114: String a = "1231212478987482988429808779810457634781384756794987";
115: int aScale = 28;
116: int newScale = 18;
117: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
118: try {
119: aNumber.setScale(newScale);
120: fail("ArithmeticException has not been caught");
121: } catch (ArithmeticException e) {
122: assertEquals("Improper exception message",
123: "Rounding necessary", e.getMessage());
124: }
125: }
126:
127: /**
128: * Set the same new scale
129: */
130: public void testSetScaleSame() {
131: String a = "1231212478987482988429808779810457634781384756794987";
132: int aScale = 18;
133: int newScale = 18;
134: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
135: BigDecimal bNumber = aNumber.setScale(newScale);
136: assertTrue("incorrect scale", bNumber.scale() == newScale);
137: assertTrue("incorrect value", bNumber.equals(aNumber));
138: }
139:
140: /**
141: * Set a new scale
142: */
143: public void testSetScaleRoundUp() {
144: String a = "1231212478987482988429808779810457634781384756794987";
145: String b = "123121247898748298842980877981045763478139";
146: int aScale = 28;
147: int newScale = 18;
148: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
149: BigDecimal bNumber = aNumber.setScale(newScale,
150: BigDecimal.ROUND_UP);
151: assertTrue("incorrect scale", bNumber.scale() == newScale);
152: assertTrue("incorrect value", bNumber.unscaledValue()
153: .toString().equals(b));
154: }
155:
156: /**
157: * Set a new scale
158: */
159: public void testSetScaleRoundDown() {
160: String a = "1231212478987482988429808779810457634781384756794987";
161: String b = "123121247898748298842980877981045763478138";
162: int aScale = 28;
163: int newScale = 18;
164: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
165: BigDecimal bNumber = aNumber.setScale(newScale,
166: BigDecimal.ROUND_DOWN);
167: assertTrue("incorrect scale", bNumber.scale() == newScale);
168: assertTrue("incorrect value", bNumber.unscaledValue()
169: .toString().equals(b));
170: }
171:
172: /**
173: * Set a new scale
174: */
175: public void testSetScaleRoundCeiling() {
176: String a = "1231212478987482988429808779810457634781384756794987";
177: String b = "123121247898748298842980877981045763478139";
178: int aScale = 28;
179: int newScale = 18;
180: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
181: BigDecimal bNumber = aNumber.setScale(newScale,
182: BigDecimal.ROUND_CEILING);
183: assertTrue("incorrect scale", bNumber.scale() == newScale);
184: assertTrue("incorrect value", bNumber.unscaledValue()
185: .toString().equals(b));
186: }
187:
188: /**
189: * Set a new scale
190: */
191: public void testSetScaleRoundFloor() {
192: String a = "1231212478987482988429808779810457634781384756794987";
193: String b = "123121247898748298842980877981045763478138";
194: int aScale = 28;
195: int newScale = 18;
196: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
197: BigDecimal bNumber = aNumber.setScale(newScale,
198: BigDecimal.ROUND_FLOOR);
199: assertTrue("incorrect scale", bNumber.scale() == newScale);
200: assertTrue("incorrect value", bNumber.unscaledValue()
201: .toString().equals(b));
202: }
203:
204: /**
205: * Set a new scale
206: */
207: public void testSetScaleRoundHalfUp() {
208: String a = "1231212478987482988429808779810457634781384756794987";
209: String b = "123121247898748298842980877981045763478138";
210: int aScale = 28;
211: int newScale = 18;
212: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
213: BigDecimal bNumber = aNumber.setScale(newScale,
214: BigDecimal.ROUND_HALF_UP);
215: assertTrue("incorrect scale", bNumber.scale() == newScale);
216: assertTrue("incorrect value", bNumber.unscaledValue()
217: .toString().equals(b));
218: }
219:
220: /**
221: * Set a new scale
222: */
223: public void testSetScaleRoundHalfDown() {
224: String a = "1231212478987482988429808779810457634781384756794987";
225: String b = "123121247898748298842980877981045763478138";
226: int aScale = 28;
227: int newScale = 18;
228: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
229: BigDecimal bNumber = aNumber.setScale(newScale,
230: BigDecimal.ROUND_HALF_DOWN);
231: assertTrue("incorrect scale", bNumber.scale() == newScale);
232: assertTrue("incorrect value", bNumber.unscaledValue()
233: .toString().equals(b));
234: }
235:
236: /**
237: * Set a new scale
238: */
239: public void testSetScaleRoundHalfEven() {
240: String a = "1231212478987482988429808779810457634781384756794987";
241: String b = "123121247898748298842980877981045763478138";
242: int aScale = 28;
243: int newScale = 18;
244: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
245: BigDecimal bNumber = aNumber.setScale(newScale,
246: BigDecimal.ROUND_HALF_EVEN);
247: assertTrue("incorrect scale", bNumber.scale() == newScale);
248: assertTrue("incorrect value", bNumber.unscaledValue()
249: .toString().equals(b));
250: }
251:
252: /**
253: * SetScale(int, RoundingMode)
254: */
255: public void testSetScaleIntRoundingMode() {
256: String a = "1231212478987482988429808779810457634781384756794987";
257: int aScale = 28;
258: int newScale = 18;
259: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
260: BigDecimal result = aNumber.setScale(newScale,
261: RoundingMode.HALF_EVEN);
262: String res = "123121247898748298842980.877981045763478138";
263: int resScale = 18;
264: assertEquals("incorrect value", res, result.toString());
265: assertEquals("incorrect scale", resScale, result.scale());
266: }
267:
268: /**
269: * Move the decimal point to the left; the shift value is positive
270: */
271: public void testMovePointLeftPos() {
272: String a = "1231212478987482988429808779810457634781384756794987";
273: int aScale = 28;
274: int shift = 18;
275: int resScale = 46;
276: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
277: BigDecimal bNumber = aNumber.movePointLeft(shift);
278: assertTrue("incorrect scale", bNumber.scale() == resScale);
279: assertTrue("incorrect value", bNumber.unscaledValue()
280: .toString().equals(a));
281: }
282:
283: /**
284: * Move the decimal point to the left; the shift value is positive
285: */
286: public void testMovePointLeftNeg() {
287: String a = "1231212478987482988429808779810457634781384756794987";
288: int aScale = 28;
289: int shift = -18;
290: int resScale = 10;
291: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
292: BigDecimal bNumber = aNumber.movePointLeft(shift);
293: assertTrue("incorrect scale", bNumber.scale() == resScale);
294: assertTrue("incorrect value", bNumber.unscaledValue()
295: .toString().equals(a));
296: }
297:
298: /**
299: * Move the decimal point to the right; the shift value is positive
300: */
301: public void testMovePointRightPosGreater() {
302: String a = "1231212478987482988429808779810457634781384756794987";
303: int aScale = 28;
304: int shift = 18;
305: int resScale = 10;
306: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
307: BigDecimal bNumber = aNumber.movePointRight(shift);
308: assertTrue("incorrect scale", bNumber.scale() == resScale);
309: assertTrue("incorrect value", bNumber.unscaledValue()
310: .toString().equals(a));
311: }
312:
313: /**
314: * Move the decimal point to the right; the shift value is positive
315: */
316: public void testMovePointRightPosLess() {
317: String a = "1231212478987482988429808779810457634781384756794987";
318: String b = "123121247898748298842980877981045763478138475679498700";
319: int aScale = 28;
320: int shift = 30;
321: int resScale = 0;
322: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
323: BigDecimal bNumber = aNumber.movePointRight(shift);
324: assertTrue("incorrect scale", bNumber.scale() == resScale);
325: assertTrue("incorrect value", bNumber.unscaledValue()
326: .toString().equals(b));
327: }
328:
329: /**
330: * Move the decimal point to the right; the shift value is positive
331: */
332: public void testMovePointRightNeg() {
333: String a = "1231212478987482988429808779810457634781384756794987";
334: int aScale = 28;
335: int shift = -18;
336: int resScale = 46;
337: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
338: BigDecimal bNumber = aNumber.movePointRight(shift);
339: assertTrue("incorrect scale", bNumber.scale() == resScale);
340: assertTrue("incorrect value", bNumber.unscaledValue()
341: .toString().equals(a));
342: }
343:
344: /**
345: * Move the decimal point to the right when the scale overflows
346: */
347: public void testMovePointRightException() {
348: String a = "12312124789874829887348723648726347429808779810457634781384756794987";
349: int aScale = Integer.MAX_VALUE; //2147483647
350: int shift = -18;
351: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
352: try {
353: aNumber.movePointRight(shift);
354: fail("ArithmeticException has not been caught");
355: } catch (ArithmeticException e) {
356: assertEquals("Improper exception message", "Underflow", e
357: .getMessage());
358: }
359: }
360:
361: /**
362: * precision()
363: */
364: public void testPrecision() {
365: String a = "12312124789874829887348723648726347429808779810457634781384756794987";
366: int aScale = 14;
367: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
368: int prec = aNumber.precision();
369: assertEquals(68, prec);
370: }
371: }
|