1 /*
2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 package test.java.math.BigDecimal;
24 
25 /*
26  * @test
27  * @bug 1234567
28  * @summary Test BigDecimal.equals() method.
29  * @author xlu
30  */
31 import java.math.*;
32 import static java.math.BigDecimal.*;
33 
34 import org.testng.Assert;
35 import org.testng.annotations.Test;
36 
37 // Android-changed: Replace error counting with asserts.
38 public class EqualsTests {
39 
40     @Test
testEquals()41     public void testEquals() {
42         BigDecimal[][] testValues = {
43             // The even index is supposed to return true for equals call and
44             // the odd index is supposed to return false, i.e. not equal.
45             {ZERO, ZERO},
46             {ONE, TEN},
47 
48             {valueOf(Integer.MAX_VALUE), valueOf(Integer.MAX_VALUE)},
49             {valueOf(Long.MAX_VALUE), valueOf(-Long.MAX_VALUE)},
50 
51             {valueOf(12345678), valueOf(12345678)},
52             {valueOf(123456789), valueOf(123456788)},
53 
54             {new BigDecimal("123456789123456789123"),
55              new BigDecimal(new BigInteger("123456789123456789123"))},
56             {new BigDecimal("123456789123456789123"),
57              new BigDecimal(new BigInteger("123456789123456789124"))},
58 
59             {valueOf(Long.MIN_VALUE), new BigDecimal("-9223372036854775808")},
60             {new BigDecimal("9223372036854775808"), valueOf(Long.MAX_VALUE)},
61 
62             {valueOf(Math.round(Math.pow(2, 10))), new BigDecimal("1024")},
63             {new BigDecimal("1020"), valueOf(Math.pow(2, 11))},
64 
65             {new BigDecimal(BigInteger.valueOf(2).pow(65)),
66              new BigDecimal("36893488147419103232")},
67             {new BigDecimal("36893488147419103231.81"),
68              new BigDecimal("36893488147419103231.811"),
69             }
70         };
71 
72         boolean expected = Boolean.TRUE;
73         for (BigDecimal[] testValuePair : testValues) {
74             equalsTest(testValuePair[0], testValuePair[1], expected);
75             expected = !expected;
76         }
77     }
78 
equalsTest(BigDecimal l, BigDecimal r, boolean expected)79     private static void equalsTest(BigDecimal l, BigDecimal r, boolean expected) {
80         boolean result = l.equals(r);
81         Assert.assertEquals(result, expected, l + " .equals(" + r + ") => " + result +
82                                "\n\tExpected " + expected);
83     }
84 }
85