Path: blob/master/test/jdk/java/math/BigDecimal/NegateTests.java
41149 views
/*1* Copyright (c) 2005, 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 632553526* @summary Test for the rounding behavior of negate(MathContext)27* @author Joseph D. Darcy28*/2930import java.math.*;3132public class NegateTests {3334static BigDecimal negateThenRound(BigDecimal bd, MathContext mc) {35return bd.negate().plus(mc);36}373839static BigDecimal absThenRound(BigDecimal bd, MathContext mc) {40return bd.abs().plus(mc);41}424344static int negateTest(BigDecimal[][] testCases, MathContext mc) {45int failures = 0;4647for (BigDecimal [] testCase : testCases) {4849BigDecimal bd = testCase[0];50BigDecimal neg1 = bd.negate(mc);51BigDecimal neg2 = negateThenRound(bd, mc);52BigDecimal expected = testCase[1];5354if (! neg1.equals(expected) ) {55failures++;56System.err.println("(" + bd + ").negate(" + mc + ") => " +57neg1 + " != expected " + expected);58}5960if (! neg1.equals(neg2) ) {61failures++;62System.err.println("(" + bd + ").negate(" + mc + ") => " +63neg1 + " != ntr " + neg2);64}6566// Test abs consistency67BigDecimal abs = bd.abs(mc);68BigDecimal expectedAbs = absThenRound(bd,mc);69if (! abs.equals(expectedAbs) ) {70failures++;71System.err.println("(" + bd + ").abs(" + mc + ") => " +72abs + " != atr " + expectedAbs);73}7475}7677return failures;78}7980static int negateTests() {81int failures = 0;82BigDecimal [][] testCasesCeiling = {83{new BigDecimal("1.3"), new BigDecimal("-1")},84{new BigDecimal("-1.3"), new BigDecimal("2")},85};8687failures += negateTest(testCasesCeiling,88new MathContext(1, RoundingMode.CEILING));8990BigDecimal [][] testCasesFloor = {91{new BigDecimal("1.3"), new BigDecimal("-2")},92{new BigDecimal("-1.3"), new BigDecimal("1")},93};9495failures += negateTest(testCasesFloor,96new MathContext(1, RoundingMode.FLOOR));9798return failures;99}100101public static void main(String argv[]) {102int failures = 0;103104failures += negateTests();105106if (failures > 0 )107throw new RuntimeException("Incurred " + failures + " failures" +108" testing the negate and/or abs.");109}110}111112113