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 */
20
21package org.apache.harmony.tests.java.math;
22
23import java.math.BigDecimal;
24import java.math.BigInteger;
25import java.math.MathContext;
26import java.math.RoundingMode;
27
28import junit.framework.TestCase;
29
30/**
31 * Class:  java.math.BigDecimal
32 * Methods: abs, compareTo, equals, hashCode,
33 * max, min, negate, signum
34 */
35public class BigDecimalCompareTest extends TestCase {
36    /**
37     * Abs() of a negative BigDecimal
38     */
39    public void testAbsNeg() {
40        String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
41        BigDecimal aNumber = new BigDecimal(a);
42        String result = "123809648392384754573567356745735635678902957849027687.87678287";
43        assertEquals("incorrect value", result, aNumber.abs().toString());
44    }
45
46    /**
47     * Abs() of a positive BigDecimal
48     */
49    public void testAbsPos() {
50        String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
51        BigDecimal aNumber = new BigDecimal(a);
52        String result = "123809648392384754573567356745735635678902957849027687.87678287";
53        assertEquals("incorrect value", result, aNumber.abs().toString());
54    }
55
56    /**
57     * Abs(MathContext) of a negative BigDecimal
58     */
59    public void testAbsMathContextNeg() {
60        String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21";
61        BigDecimal aNumber = new BigDecimal(a);
62        int precision = 15;
63        RoundingMode rm = RoundingMode.HALF_DOWN;
64        MathContext mc = new MathContext(precision, rm);
65        String result = "1.23809648392385E+53";
66        int resScale = -39;
67        BigDecimal res = aNumber.abs(mc);
68        assertEquals("incorrect value", result, res.toString());
69        assertEquals("incorrect scale", resScale, res.scale());
70    }
71
72    /**
73     * Abs(MathContext) of a positive BigDecimal
74     */
75    public void testAbsMathContextPos() {
76        String a = "123809648392384754573567356745735.63567890295784902768787678287E+21";
77        BigDecimal aNumber = new BigDecimal(a);
78        int precision = 41;
79        RoundingMode rm = RoundingMode.HALF_EVEN;
80        MathContext mc = new MathContext(precision, rm);
81        String result = "1.2380964839238475457356735674573563567890E+53";
82        int resScale = -13;
83        BigDecimal res = aNumber.abs(mc);
84        assertEquals("incorrect value", result, res.toString());
85        assertEquals("incorrect scale", resScale, res.scale());
86    }
87
88    /**
89     * Compare to a number of an equal scale
90     */
91    public void testCompareEqualScale1() {
92        String a = "12380964839238475457356735674573563567890295784902768787678287";
93        int aScale = 18;
94        String b = "4573563567890295784902768787678287";
95        int bScale = 18;
96        BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
97        BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
98        int result = 1;
99        assertEquals("incorrect result", result, aNumber.compareTo(bNumber));
100    }
101
102    /**
103     * Compare to a number of an equal scale
104     */
105    public void testCompareEqualScale2() {
106        String a = "12380964839238475457356735674573563567890295784902768787678287";
107        int aScale = 18;
108        String b = "4573563923487289357829759278282992758247567890295784902768787678287";
109        int bScale = 18;
110        BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
111        BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
112        int result = -1;
113        assertEquals("incorrect result", result, aNumber.compareTo(bNumber));
114    }
115
116    /**
117     * Compare to a number of an greater scale
118     */
119    public void testCompareGreaterScale1() {
120        String a = "12380964839238475457356735674573563567890295784902768787678287";
121        int aScale = 28;
122        String b = "4573563567890295784902768787678287";
123        int bScale = 18;
124        BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
125        BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
126        int result = 1;
127        assertEquals("incorrect result", result, aNumber.compareTo(bNumber));
128    }
129
130    /**
131     * Compare to a number of an greater scale
132     */
133    public void testCompareGreaterScale2() {
134        String a = "12380964839238475457356735674573563567890295784902768787678287";
135        int aScale = 48;
136        String b = "4573563567890295784902768787678287";
137        int bScale = 2;
138        BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
139        BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
140        int result = -1;
141        assertEquals("incorrect result", result, aNumber.compareTo(bNumber));
142    }
143
144    /**
145     * Compare to a number of an less scale
146     */
147    public void testCompareLessScale1() {
148        String a = "12380964839238475457356735674573563567890295784902768787678287";
149        int aScale = 18;
150        String b = "4573563567890295784902768787678287";
151        int bScale = 28;
152        BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
153        BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
154        int result = 1;
155        assertEquals("incorrect result", result, aNumber.compareTo(bNumber));
156    }
157
158    /**
159     * Compare to a number of an less scale
160     */
161    public void testCompareLessScale2() {
162        String a = "12380964839238475457356735674573";
163        int aScale = 36;
164        String b = "45735635948573894578349572001798379183767890295784902768787678287";
165        int bScale = 48;
166        BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
167        BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
168        int result = -1;
169        assertEquals("incorrect result", result, aNumber.compareTo(bNumber));
170    }
171
172    /**
173     * Equals() for unequal BigDecimals
174     */
175    public void testEqualsUnequal1() {
176       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
177       int aScale = -24;
178       String b = "7472334223847623782375469293018787918347987234564568";
179       int bScale = 13;
180       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
181       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
182       assertFalse(aNumber.equals(bNumber));
183    }
184
185    /**
186     * Equals() for unequal BigDecimals
187     */
188    public void testEqualsUnequal2() {
189       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
190       int aScale = -24;
191       String b = "92948782094488478231212478987482988429808779810457634781384756794987";
192       int bScale = 13;
193       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
194       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
195       assertFalse(aNumber.equals(bNumber));
196    }
197
198    /**
199     * Equals() for unequal BigDecimals
200     */
201    public void testEqualsUnequal3() {
202       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
203       int aScale = -24;
204       String b = "92948782094488478231212478987482988429808779810457634781384756794987";
205       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
206       assertFalse(aNumber.equals(b));
207    }
208
209    /**
210     * equals() for equal BigDecimals
211     */
212    public void testEqualsEqual() {
213       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
214       int aScale = -24;
215       String b = "92948782094488478231212478987482988429808779810457634781384756794987";
216       int bScale = -24;
217       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
218       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
219       assertEquals(aNumber, bNumber);
220    }
221
222    /**
223     * equals() for equal BigDecimals
224     */
225    public void testEqualsNull() {
226       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
227       int aScale = -24;
228       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
229       assertFalse(aNumber.equals(null));
230    }
231
232    /**
233     * hashCode() for equal BigDecimals
234     */
235    public void testHashCodeEqual() {
236       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
237       int aScale = -24;
238       String b = "92948782094488478231212478987482988429808779810457634781384756794987";
239       int bScale = -24;
240       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
241       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
242       assertEquals("incorrect value", aNumber.hashCode(), bNumber.hashCode());
243    }
244
245    /**
246     * hashCode() for unequal BigDecimals
247     */
248    public void testHashCodeUnequal() {
249       String a = "8478231212478987482988429808779810457634781384756794987";
250       int aScale = 41;
251       String b = "92948782094488478231212478987482988429808779810457634781384756794987";
252       int bScale = -24;
253       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
254       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
255       assertTrue("incorrect value", aNumber.hashCode() != bNumber.hashCode());
256    }
257
258    /**
259     * max() for equal BigDecimals
260     */
261    public void testMaxEqual() {
262       String a = "8478231212478987482988429808779810457634781384756794987";
263       int aScale = 41;
264       String b = "8478231212478987482988429808779810457634781384756794987";
265       int bScale = 41;
266       String c = "8478231212478987482988429808779810457634781384756794987";
267       int cScale = 41;
268       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
269       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
270       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
271       assertEquals("incorrect value", cNumber, aNumber.max(bNumber));
272    }
273
274    /**
275     * max() for unequal BigDecimals
276     */
277    public void testMaxUnequal1() {
278       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
279       int aScale = 24;
280       String b = "92948782094488478231212478987482988429808779810457634781384756794987";
281       int bScale = 41;
282       String c = "92948782094488478231212478987482988429808779810457634781384756794987";
283       int cScale = 24;
284       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
285       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
286       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
287       assertEquals("incorrect value", cNumber, aNumber.max(bNumber));
288    }
289
290    /**
291     * max() for unequal BigDecimals
292     */
293    public void testMaxUnequal2() {
294       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
295       int aScale = 41;
296       String b = "94488478231212478987482988429808779810457634781384756794987";
297       int bScale = 41;
298       String c = "92948782094488478231212478987482988429808779810457634781384756794987";
299       int cScale = 41;
300       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
301       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
302       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
303       assertEquals("incorrect value", cNumber, aNumber.max(bNumber));
304    }
305
306    /**
307     * min() for equal BigDecimals
308     */
309    public void testMinEqual() {
310       String a = "8478231212478987482988429808779810457634781384756794987";
311       int aScale = 41;
312       String b = "8478231212478987482988429808779810457634781384756794987";
313       int bScale = 41;
314       String c = "8478231212478987482988429808779810457634781384756794987";
315       int cScale = 41;
316       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
317       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
318       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
319       assertEquals("incorrect value", cNumber, aNumber.min(bNumber));
320    }
321
322    /**
323     * min() for unequal BigDecimals
324     */
325    public void testMinUnequal1() {
326       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
327       int aScale = 24;
328       String b = "92948782094488478231212478987482988429808779810457634781384756794987";
329       int bScale = 41;
330       String c = "92948782094488478231212478987482988429808779810457634781384756794987";
331       int cScale = 41;
332       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
333       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
334       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
335       assertEquals("incorrect value", cNumber, aNumber.min(bNumber));
336    }
337
338    /**
339     * min() for unequal BigDecimals
340     */
341    public void testMinUnequal2() {
342       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
343       int aScale = 41;
344       String b = "94488478231212478987482988429808779810457634781384756794987";
345       int bScale = 41;
346       String c = "94488478231212478987482988429808779810457634781384756794987";
347       int cScale = 41;
348       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
349       BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale);
350       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
351       assertEquals("incorrect value", cNumber, aNumber.min(bNumber));
352    }
353
354    /**
355     * plus() for a positive BigDecimal
356     */
357    public void testPlusPositive() {
358       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
359       int aScale = 41;
360       String c = "92948782094488478231212478987482988429808779810457634781384756794987";
361       int cScale = 41;
362       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
363       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
364       assertEquals("incorrect value", cNumber, aNumber.plus());
365    }
366
367    /**
368     * plus(MathContext) for a positive BigDecimal
369     */
370    public void testPlusMathContextPositive() {
371       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
372       int aScale = 41;
373       int precision = 37;
374       RoundingMode rm = RoundingMode.FLOOR;
375       MathContext mc = new MathContext(precision, rm);
376       String c = "929487820944884782312124789.8748298842";
377       int cScale = 10;
378       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
379       BigDecimal res = aNumber.plus(mc);
380       assertEquals("incorrect value", c, res.toString());
381       assertEquals("incorrect scale", cScale, res.scale());
382    }
383
384    /**
385     * plus() for a negative BigDecimal
386     */
387    public void testPlusNegative() {
388       String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
389       int aScale = 41;
390       String c = "-92948782094488478231212478987482988429808779810457634781384756794987";
391       int cScale = 41;
392       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
393       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
394       assertEquals("incorrect value", cNumber, aNumber.plus());
395    }
396
397    /**
398     * plus(MathContext) for a negative BigDecimal
399     */
400    public void testPlusMathContextNegative() {
401       String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
402       int aScale = 49;
403       int precision = 46;
404       RoundingMode rm = RoundingMode.CEILING;
405       MathContext mc = new MathContext(precision, rm);
406       String c = "-9294878209448847823.121247898748298842980877981";
407       int cScale = 27;
408       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
409       BigDecimal res = aNumber.plus(mc);
410       assertEquals("incorrect value", c, res.toString());
411       assertEquals("incorrect scale", cScale, res.scale());
412    }
413
414    /**
415     * negate() for a positive BigDecimal
416     */
417    public void testNegatePositive() {
418       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
419       int aScale = 41;
420       String c = "-92948782094488478231212478987482988429808779810457634781384756794987";
421       int cScale = 41;
422       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
423       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
424       assertEquals("incorrect value", cNumber, aNumber.negate());
425    }
426
427    /**
428     * negate(MathContext) for a positive BigDecimal
429     */
430    public void testNegateMathContextPositive() {
431       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
432       int aScale = 41;
433       int precision = 37;
434       RoundingMode rm = RoundingMode.FLOOR;
435       MathContext mc = new MathContext(precision, rm);
436       String c = "-929487820944884782312124789.8748298843";
437       int cScale = 10;
438       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
439       BigDecimal res = aNumber.negate(mc);
440       assertEquals("incorrect value", c, res.toString());
441       assertEquals("incorrect scale", cScale, res.scale());
442    }
443
444    /**
445     * negate() for a negative BigDecimal
446     */
447    public void testNegateNegative() {
448       String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
449       int aScale = 41;
450       String c = "92948782094488478231212478987482988429808779810457634781384756794987";
451       int cScale = 41;
452       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
453       BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale);
454       assertEquals("incorrect value", cNumber, aNumber.negate());
455    }
456
457    /**
458     * negate(MathContext) for a negative BigDecimal
459     */
460    public void testNegateMathContextNegative() {
461       String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
462       int aScale = 49;
463       int precision = 46;
464       RoundingMode rm = RoundingMode.CEILING;
465       MathContext mc = new MathContext(precision, rm);
466       String c = "9294878209448847823.121247898748298842980877982";
467       int cScale = 27;
468       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
469       BigDecimal res = aNumber.negate(mc);
470       assertEquals("incorrect value", c, res.toString());
471       assertEquals("incorrect scale", cScale, res.scale());
472    }
473
474    /**
475     * signum() for a positive BigDecimal
476     */
477    public void testSignumPositive() {
478       String a = "92948782094488478231212478987482988429808779810457634781384756794987";
479       int aScale = 41;
480       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
481       assertEquals("incorrect value", 1, aNumber.signum());
482    }
483
484    /**
485     * signum() for a negative BigDecimal
486     */
487    public void testSignumNegative() {
488       String a = "-92948782094488478231212478987482988429808779810457634781384756794987";
489       int aScale = 41;
490       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
491       assertEquals("incorrect value", -1, aNumber.signum());
492    }
493
494    /**
495     * signum() for zero
496     */
497    public void testSignumZero() {
498       String a = "0";
499       int aScale = 41;
500       BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale);
501       assertEquals("incorrect value", 0, aNumber.signum());
502    }
503
504    /*
505     * Regression test for HARMONY-6406
506     */
507    public void testApproxPrecision() {
508        BigDecimal testInstance = BigDecimal.TEN.multiply(new BigDecimal("0.1"));
509        int result = testInstance.compareTo(new BigDecimal("1.00"));
510        assertEquals(0, result);
511    }
512}
513