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 java.math.BigDecimal;
023: import java.math.BigInteger;
024: import java.math.MathContext;
025: import java.math.RoundingMode;
026:
027: import junit.framework.TestCase;
028:
029: /**
030: * Class: java.math.BigDecimal
031: * Methods: abs, compareTo, equals, hashCode,
032: * max, min, negate, signum
033: */
034: public class BigDecimalCompareTest extends TestCase {
035: /**
036: * Abs() of a negative BigDecimal
037: */
038: public void testAbsNeg() {
039: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
040: BigDecimal aNumber = new BigDecimal(a);
041: String result = "123809648392384754573567356745735635678902957849027687.87678287";
042: assertEquals("incorrect value", result, aNumber.abs()
043: .toString());
044: }
045:
046: /**
047: * Abs() of a positive BigDecimal
048: */
049: public void testAbsPos() {
050: String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
051: BigDecimal aNumber = new BigDecimal(a);
052: String result = "123809648392384754573567356745735635678902957849027687.87678287";
053: assertEquals("incorrect value", result, aNumber.abs()
054: .toString());
055: }
056:
057: /**
058: * Abs(MathContext) of a negative BigDecimal
059: */
060: public void testAbsMathContextNeg() {
061: String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
062: BigDecimal aNumber = new BigDecimal(a);
063: int precision = 15;
064: RoundingMode rm = RoundingMode.HALF_DOWN;
065: MathContext mc = new MathContext(precision, rm);
066: String result = "1.23809648392385E+53";
067: int resScale = -39;
068: BigDecimal res = aNumber.abs(mc);
069: assertEquals("incorrect value", result, res.toString());
070: assertEquals("incorrect scale", resScale, res.scale());
071: }
072:
073: /**
074: * Abs(MathContext) of a positive BigDecimal
075: */
076: public void testAbsMathContextPos() {
077: String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
078: BigDecimal aNumber = new BigDecimal(a);
079: int precision = 41;
080: RoundingMode rm = RoundingMode.HALF_EVEN;
081: MathContext mc = new MathContext(precision, rm);
082: String result = "1.2380964839238475457356735674573563567890E+53";
083: int resScale = -13;
084: BigDecimal res = aNumber.abs(mc);
085: assertEquals("incorrect value", result, res.toString());
086: assertEquals("incorrect scale", resScale, res.scale());
087: }
088:
089: /**
090: * Compare to a number of an equal scale
091: */
092: public void testCompareEqualScale1() {
093: String a = "12380964839238475457356735674573563567890295784902768787678287";
094: int aScale = 18;
095: String b = "4573563567890295784902768787678287";
096: int bScale = 18;
097: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
098: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
099: int result = 1;
100: assertEquals("incorrect result", result, aNumber
101: .compareTo(bNumber));
102: }
103:
104: /**
105: * Compare to a number of an equal scale
106: */
107: public void testCompareEqualScale2() {
108: String a = "12380964839238475457356735674573563567890295784902768787678287";
109: int aScale = 18;
110: String b = "4573563923487289357829759278282992758247567890295784902768787678287";
111: int bScale = 18;
112: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
113: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
114: int result = -1;
115: assertEquals("incorrect result", result, aNumber
116: .compareTo(bNumber));
117: }
118:
119: /**
120: * Compare to a number of an greater scale
121: */
122: public void testCompareGreaterScale1() {
123: String a = "12380964839238475457356735674573563567890295784902768787678287";
124: int aScale = 28;
125: String b = "4573563567890295784902768787678287";
126: int bScale = 18;
127: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
128: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
129: int result = 1;
130: assertEquals("incorrect result", result, aNumber
131: .compareTo(bNumber));
132: }
133:
134: /**
135: * Compare to a number of an greater scale
136: */
137: public void testCompareGreaterScale2() {
138: String a = "12380964839238475457356735674573563567890295784902768787678287";
139: int aScale = 48;
140: String b = "4573563567890295784902768787678287";
141: int bScale = 2;
142: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
143: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
144: int result = -1;
145: assertEquals("incorrect result", result, aNumber
146: .compareTo(bNumber));
147: }
148:
149: /**
150: * Compare to a number of an less scale
151: */
152: public void testCompareLessScale1() {
153: String a = "12380964839238475457356735674573563567890295784902768787678287";
154: int aScale = 18;
155: String b = "4573563567890295784902768787678287";
156: int bScale = 28;
157: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
158: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
159: int result = 1;
160: assertEquals("incorrect result", result, aNumber
161: .compareTo(bNumber));
162: }
163:
164: /**
165: * Compare to a number of an less scale
166: */
167: public void testCompareLessScale2() {
168: String a = "12380964839238475457356735674573";
169: int aScale = 36;
170: String b = "45735635948573894578349572001798379183767890295784902768787678287";
171: int bScale = 48;
172: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
173: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
174: int result = -1;
175: assertEquals("incorrect result", result, aNumber
176: .compareTo(bNumber));
177: }
178:
179: /**
180: * Equals() for unequal BigDecimals
181: */
182: public void testEqualsUnequal1() {
183: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
184: int aScale = -24;
185: String b = "7472334223847623782375469293018787918347987234564568";
186: int bScale = 13;
187: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
188: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
189: assertFalse(aNumber.equals(bNumber));
190: }
191:
192: /**
193: * Equals() for unequal BigDecimals
194: */
195: public void testEqualsUnequal2() {
196: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
197: int aScale = -24;
198: String b = "92948782094488478231212478987482988429808779810457634781384756794987";
199: int bScale = 13;
200: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
201: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
202: assertFalse(aNumber.equals(bNumber));
203: }
204:
205: /**
206: * Equals() for unequal BigDecimals
207: */
208: public void testEqualsUnequal3() {
209: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
210: int aScale = -24;
211: String b = "92948782094488478231212478987482988429808779810457634781384756794987";
212: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
213: assertFalse(aNumber.equals(b));
214: }
215:
216: /**
217: * equals() for equal BigDecimals
218: */
219: public void testEqualsEqual() {
220: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
221: int aScale = -24;
222: String b = "92948782094488478231212478987482988429808779810457634781384756794987";
223: int bScale = -24;
224: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
225: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
226: assertEquals(aNumber, bNumber);
227: }
228:
229: /**
230: * equals() for equal BigDecimals
231: */
232: public void testEqualsNull() {
233: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
234: int aScale = -24;
235: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
236: assertFalse(aNumber.equals(null));
237: }
238:
239: /**
240: * hashCode() for equal BigDecimals
241: */
242: public void testHashCodeEqual() {
243: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
244: int aScale = -24;
245: String b = "92948782094488478231212478987482988429808779810457634781384756794987";
246: int bScale = -24;
247: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
248: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
249: assertEquals("incorrect value", aNumber.hashCode(), bNumber
250: .hashCode());
251: }
252:
253: /**
254: * hashCode() for unequal BigDecimals
255: */
256: public void testHashCodeUnequal() {
257: String a = "8478231212478987482988429808779810457634781384756794987";
258: int aScale = 41;
259: String b = "92948782094488478231212478987482988429808779810457634781384756794987";
260: int bScale = -24;
261: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
262: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
263: assertTrue("incorrect value", aNumber.hashCode() != bNumber
264: .hashCode());
265: }
266:
267: /**
268: * max() for equal BigDecimals
269: */
270: public void testMaxEqual() {
271: String a = "8478231212478987482988429808779810457634781384756794987";
272: int aScale = 41;
273: String b = "8478231212478987482988429808779810457634781384756794987";
274: int bScale = 41;
275: String c = "8478231212478987482988429808779810457634781384756794987";
276: int cScale = 41;
277: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
278: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
279: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
280: assertEquals("incorrect value", cNumber, aNumber.max(bNumber));
281: }
282:
283: /**
284: * max() for unequal BigDecimals
285: */
286: public void testMaxUnequal1() {
287: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
288: int aScale = 24;
289: String b = "92948782094488478231212478987482988429808779810457634781384756794987";
290: int bScale = 41;
291: String c = "92948782094488478231212478987482988429808779810457634781384756794987";
292: int cScale = 24;
293: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
294: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
295: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
296: assertEquals("incorrect value", cNumber, aNumber.max(bNumber));
297: }
298:
299: /**
300: * max() for unequal BigDecimals
301: */
302: public void testMaxUnequal2() {
303: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
304: int aScale = 41;
305: String b = "94488478231212478987482988429808779810457634781384756794987";
306: int bScale = 41;
307: String c = "92948782094488478231212478987482988429808779810457634781384756794987";
308: int cScale = 41;
309: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
310: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
311: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
312: assertEquals("incorrect value", cNumber, aNumber.max(bNumber));
313: }
314:
315: /**
316: * min() for equal BigDecimals
317: */
318: public void testMinEqual() {
319: String a = "8478231212478987482988429808779810457634781384756794987";
320: int aScale = 41;
321: String b = "8478231212478987482988429808779810457634781384756794987";
322: int bScale = 41;
323: String c = "8478231212478987482988429808779810457634781384756794987";
324: int cScale = 41;
325: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
326: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
327: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
328: assertEquals("incorrect value", cNumber, aNumber.min(bNumber));
329: }
330:
331: /**
332: * min() for unequal BigDecimals
333: */
334: public void testMinUnequal1() {
335: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
336: int aScale = 24;
337: String b = "92948782094488478231212478987482988429808779810457634781384756794987";
338: int bScale = 41;
339: String c = "92948782094488478231212478987482988429808779810457634781384756794987";
340: int cScale = 41;
341: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
342: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
343: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
344: assertEquals("incorrect value", cNumber, aNumber.min(bNumber));
345: }
346:
347: /**
348: * min() for unequal BigDecimals
349: */
350: public void testMinUnequal2() {
351: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
352: int aScale = 41;
353: String b = "94488478231212478987482988429808779810457634781384756794987";
354: int bScale = 41;
355: String c = "94488478231212478987482988429808779810457634781384756794987";
356: int cScale = 41;
357: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
358: BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
359: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
360: assertEquals("incorrect value", cNumber, aNumber.min(bNumber));
361: }
362:
363: /**
364: * plus() for a positive BigDecimal
365: */
366: public void testPlusPositive() {
367: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
368: int aScale = 41;
369: String c = "92948782094488478231212478987482988429808779810457634781384756794987";
370: int cScale = 41;
371: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
372: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
373: assertEquals("incorrect value", cNumber, aNumber.plus());
374: }
375:
376: /**
377: * plus(MathContext) for a positive BigDecimal
378: */
379: public void testPlusMathContextPositive() {
380: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
381: int aScale = 41;
382: int precision = 37;
383: RoundingMode rm = RoundingMode.FLOOR;
384: MathContext mc = new MathContext(precision, rm);
385: String c = "929487820944884782312124789.8748298842";
386: int cScale = 10;
387: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
388: BigDecimal res = aNumber.plus(mc);
389: assertEquals("incorrect value", c, res.toString());
390: assertEquals("incorrect scale", cScale, res.scale());
391: }
392:
393: /**
394: * plus() for a negative BigDecimal
395: */
396: public void testPlusNegative() {
397: String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
398: int aScale = 41;
399: String c = "-92948782094488478231212478987482988429808779810457634781384756794987";
400: int cScale = 41;
401: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
402: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
403: assertEquals("incorrect value", cNumber, aNumber.plus());
404: }
405:
406: /**
407: * plus(MathContext) for a negative BigDecimal
408: */
409: public void testPlusMathContextNegative() {
410: String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
411: int aScale = 49;
412: int precision = 46;
413: RoundingMode rm = RoundingMode.CEILING;
414: MathContext mc = new MathContext(precision, rm);
415: String c = "-9294878209448847823.121247898748298842980877981";
416: int cScale = 27;
417: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
418: BigDecimal res = aNumber.plus(mc);
419: assertEquals("incorrect value", c, res.toString());
420: assertEquals("incorrect scale", cScale, res.scale());
421: }
422:
423: /**
424: * negate() for a positive BigDecimal
425: */
426: public void testNegatePositive() {
427: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
428: int aScale = 41;
429: String c = "-92948782094488478231212478987482988429808779810457634781384756794987";
430: int cScale = 41;
431: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
432: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
433: assertEquals("incorrect value", cNumber, aNumber.negate());
434: }
435:
436: /**
437: * negate(MathContext) for a positive BigDecimal
438: */
439: public void testNegateMathContextPositive() {
440: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
441: int aScale = 41;
442: int precision = 37;
443: RoundingMode rm = RoundingMode.FLOOR;
444: MathContext mc = new MathContext(precision, rm);
445: String c = "-929487820944884782312124789.8748298842";
446: int cScale = 10;
447: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
448: BigDecimal res = aNumber.negate(mc);
449: assertEquals("incorrect value", c, res.toString());
450: assertEquals("incorrect scale", cScale, res.scale());
451: }
452:
453: /**
454: * negate() for a negative BigDecimal
455: */
456: public void testNegateNegative() {
457: String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
458: int aScale = 41;
459: String c = "92948782094488478231212478987482988429808779810457634781384756794987";
460: int cScale = 41;
461: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
462: BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
463: assertEquals("incorrect value", cNumber, aNumber.negate());
464: }
465:
466: /**
467: * negate(MathContext) for a negative BigDecimal
468: */
469: public void testNegateMathContextNegative() {
470: String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
471: int aScale = 49;
472: int precision = 46;
473: RoundingMode rm = RoundingMode.CEILING;
474: MathContext mc = new MathContext(precision, rm);
475: String c = "9294878209448847823.121247898748298842980877981";
476: int cScale = 27;
477: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
478: BigDecimal res = aNumber.negate(mc);
479: assertEquals("incorrect value", c, res.toString());
480: assertEquals("incorrect scale", cScale, res.scale());
481: }
482:
483: /**
484: * signum() for a positive BigDecimal
485: */
486: public void testSignumPositive() {
487: String a = "92948782094488478231212478987482988429808779810457634781384756794987";
488: int aScale = 41;
489: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
490: assertEquals("incorrect value", 1, aNumber.signum());
491: }
492:
493: /**
494: * signum() for a negative BigDecimal
495: */
496: public void testSignumNegative() {
497: String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
498: int aScale = 41;
499: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
500: assertEquals("incorrect value", -1, aNumber.signum());
501: }
502:
503: /**
504: * signum() for zero
505: */
506: public void testSignumZero() {
507: String a = "0";
508: int aScale = 41;
509: BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
510: assertEquals("incorrect value", 0, aNumber.signum());
511: }
512: }
|