Path: blob/master/test/jdk/java/math/BigDecimal/EqualsTests.java
41149 views
/*1* Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 123456726* @summary Test BigDecimal.equals() method.27* @author xlu28*/2930import java.math.*;31import static java.math.BigDecimal.*;3233public class EqualsTests {3435public static void main(String argv[]) {36int failures = 0;3738BigDecimal[][] testValues = {39// The even index is supposed to return true for equals call and40// the odd index is supposed to return false, i.e. not equal.41{ZERO, ZERO},42{ONE, TEN},4344{valueOf(Integer.MAX_VALUE), valueOf(Integer.MAX_VALUE)},45{valueOf(Long.MAX_VALUE), valueOf(-Long.MAX_VALUE)},4647{valueOf(12345678), valueOf(12345678)},48{valueOf(123456789), valueOf(123456788)},4950{new BigDecimal("123456789123456789123"),51new BigDecimal(new BigInteger("123456789123456789123"))},52{new BigDecimal("123456789123456789123"),53new BigDecimal(new BigInteger("123456789123456789124"))},5455{valueOf(Long.MIN_VALUE), new BigDecimal("-9223372036854775808")},56{new BigDecimal("9223372036854775808"), valueOf(Long.MAX_VALUE)},5758{valueOf(Math.round(Math.pow(2, 10))), new BigDecimal("1024")},59{new BigDecimal("1020"), valueOf(Math.pow(2, 11))},6061{new BigDecimal(BigInteger.valueOf(2).pow(65)),62new BigDecimal("36893488147419103232")},63{new BigDecimal("36893488147419103231.81"),64new BigDecimal("36893488147419103231.811"),65}66};6768boolean expected = Boolean.TRUE;69for (BigDecimal[] testValuePair : testValues) {70failures += equalsTest(testValuePair[0], testValuePair[1], expected);71expected = !expected;72}7374if (failures > 0) {75throw new RuntimeException("Inccured " + failures +76" failures while testing equals.");77}78}7980private static int equalsTest(BigDecimal l, BigDecimal r, boolean expected) {81boolean result = l.equals(r);82int failed = (result == expected) ? 0 : 1;8384if (failed == 1) {85System.err.println(l + " .equals(" + r + ") => " + result +86"\n\tExpected " + expected);87}88return failed;89}90}919293