1247N/A/*
2362N/A * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
1247N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1247N/A *
1247N/A * This code is free software; you can redistribute it and/or modify it
1247N/A * under the terms of the GNU General Public License version 2 only, as
1247N/A * published by the Free Software Foundation.
1247N/A *
1247N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1247N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1247N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1247N/A * version 2 for more details (a copy is included in the LICENSE file that
1247N/A * accompanied this code).
1247N/A *
1247N/A * You should have received a copy of the GNU General Public License version
1247N/A * 2 along with this work; if not, write to the Free Software Foundation,
1247N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1247N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1247N/A */
1247N/A
1247N/A/*
1247N/A * @test
1247N/A * @bug 1234567
1247N/A * @summary Test BigDecimal.equals() method.
1247N/A * @author xlu
1247N/A */
1247N/A
1247N/Aimport java.math.*;
1247N/Aimport static java.math.BigDecimal.*;
1247N/A
1247N/Apublic class EqualsTests {
1247N/A
1247N/A public static void main(String argv[]) {
1247N/A int failures = 0;
1247N/A
1247N/A BigDecimal[][] testValues = {
1247N/A // The even index is supposed to return true for equals call and
1247N/A // the odd index is supposed to return false, i.e. not equal.
1247N/A {ZERO, ZERO},
1247N/A {ONE, TEN},
1247N/A
1247N/A {valueOf(Integer.MAX_VALUE), valueOf(Integer.MAX_VALUE)},
1247N/A {valueOf(Long.MAX_VALUE), valueOf(-Long.MAX_VALUE)},
1247N/A
1247N/A {valueOf(12345678), valueOf(12345678)},
1247N/A {valueOf(123456789), valueOf(123456788)},
1247N/A
1247N/A {new BigDecimal("123456789123456789123"),
1247N/A new BigDecimal(new BigInteger("123456789123456789123"))},
1247N/A {new BigDecimal("123456789123456789123"),
1247N/A new BigDecimal(new BigInteger("123456789123456789124"))},
1247N/A
1247N/A {valueOf(Long.MIN_VALUE), new BigDecimal("-9223372036854775808")},
1247N/A {new BigDecimal("9223372036854775808"), valueOf(Long.MAX_VALUE)},
1247N/A
1247N/A {valueOf(Math.round(Math.pow(2, 10))), new BigDecimal("1024")},
1247N/A {new BigDecimal("1020"), valueOf(Math.pow(2, 11))},
1247N/A
1247N/A {new BigDecimal(BigInteger.valueOf(2).pow(65)),
1247N/A new BigDecimal("36893488147419103232")},
1247N/A {new BigDecimal("36893488147419103231.81"),
1247N/A new BigDecimal("36893488147419103231.811"),
1247N/A }
1247N/A };
1247N/A
1247N/A boolean expected = Boolean.TRUE;
1247N/A for (BigDecimal[] testValuePair : testValues) {
1247N/A failures += equalsTest(testValuePair[0], testValuePair[1], expected);
1247N/A expected = !expected;
1247N/A }
1247N/A
1247N/A if (failures > 0) {
1247N/A throw new RuntimeException("Inccured " + failures +
1247N/A " failures while testing equals.");
1247N/A }
1247N/A }
1247N/A
1247N/A private static int equalsTest(BigDecimal l, BigDecimal r, boolean expected) {
1247N/A boolean result = l.equals(r);
1247N/A int failed = (result == expected) ? 0 : 1;
1247N/A
1247N/A if (failed == 1) {
1247N/A System.err.println(l + " .equals(" + r + ") => " + result +
1247N/A "\n\tExpected " + expected);
1247N/A }
1247N/A return failed;
1247N/A }
1247N/A}