/* * Copyright (c) 2006, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/* * @test * @bug 6362557 8200698 * @summary Some tests of add(BigDecimal, mc) * @author Joseph D. Darcy
*/
/** * Test for some simple additions, particularly, it will test * the overflow case.
*/ privatestaticint simpleTests() { int failures = 0;
BigDecimal[] bd1 = { new BigDecimal(new BigInteger("7812404666936930160"), 11), new BigDecimal(new BigInteger("7812404666936930160"), 12), new BigDecimal(new BigInteger("7812404666936930160"), 13),
};
BigDecimal bd2 = new BigDecimal(new BigInteger("2790000"), 1);
BigDecimal[] expectedResult = { new BigDecimal("78403046.66936930160"), new BigDecimal("8091404.666936930160"), new BigDecimal("1060240.4666936930160"),
}; for (int i = 0; i < bd1.length; i++) { if (!bd1[i].add(bd2).equals(expectedResult[i]))
failures++;
} return failures;
}
/** * Test for extreme value of scale and rounding precision that * could cause integer overflow in right-shift-into-sticky-bit * computations.
*/ privatestaticint extremaTests() { int failures = 0;
/** * Print sum of b1 and b2; correct result will not throw an * exception.
*/ privatestaticint addWithoutException(BigDecimal b1, BigDecimal b2, MathContext mc) { if (mc == null)
mc = new MathContext(2, RoundingMode.DOWN);
/** * Test combinations of operands that may meet the condensation * criteria when rounded to different precisions.
*/ privatestaticint roundingGradationTests() { int failures = 0;
failures += roundAway(new BigDecimal("1234e100"), new BigDecimal( "1234e97"));
failures += roundAway(new BigDecimal("1234e100"), new BigDecimal( "1234e96"));
failures += roundAway(new BigDecimal("1234e100"), new BigDecimal( "1234e95"));
failures += roundAway(new BigDecimal("1234e100"), new BigDecimal( "1234e94"));
failures += roundAway(new BigDecimal("1234e100"), new BigDecimal( "1234e93"));
failures += roundAway(new BigDecimal("1234e100"), new BigDecimal( "1234e92"));
failures += roundAway(new BigDecimal("1234e100"), new BigDecimal("1234e50"));
failures += roundAway(new BigDecimal("1000e100"), new BigDecimal( "1234e97"));
failures += roundAway(new BigDecimal("1000e100"), new BigDecimal( "1234e96"));
failures += roundAway(new BigDecimal("1000e100"), new BigDecimal( "1234e95"));
failures += roundAway(new BigDecimal("1000e100"), new BigDecimal( "1234e94"));
failures += roundAway(new BigDecimal("1000e100"), new BigDecimal( "1234e93"));
failures += roundAway(new BigDecimal("1000e100"), new BigDecimal( "1234e92"));
failures += roundAway(new BigDecimal("1000e100"), new BigDecimal("1234e50"));
failures += roundAway(new BigDecimal("1999e100"), new BigDecimal( "1234e97"));
failures += roundAway(new BigDecimal("1999e100"), new BigDecimal( "1234e96"));
failures += roundAway(new BigDecimal("1999e100"), new BigDecimal( "1234e95"));
failures += roundAway(new BigDecimal("1999e100"), new BigDecimal( "1234e94"));
failures += roundAway(new BigDecimal("1999e100"), new BigDecimal( "1234e93"));
failures += roundAway(new BigDecimal("1999e100"), new BigDecimal( "1234e92"));
failures += roundAway(new BigDecimal("1999e100"), new BigDecimal("1234e50"));
failures += roundAway(new BigDecimal("9999e100"), new BigDecimal( "1234e97"));
failures += roundAway(new BigDecimal("9999e100"), new BigDecimal( "1234e96"));
failures += roundAway(new BigDecimal("9999e100"), new BigDecimal( "1234e95"));
failures += roundAway(new BigDecimal("9999e100"), new BigDecimal( "1234e94"));
failures += roundAway(new BigDecimal("9999e100"), new BigDecimal( "1234e93"));
failures += roundAway(new BigDecimal("9999e100"), new BigDecimal( "1234e92"));
failures += roundAway(new BigDecimal("9999e100"), new BigDecimal("1234e50"));
/** * Verify calling the precision method should not change the * computed result.
*/ privatestaticint precisionConsistencyTest() { int failures = 0;
MathContext mc = new MathContext(1,RoundingMode.DOWN);
BigDecimal a = BigDecimal.valueOf(1999, -1); //value is equivalent to 19990
privatestaticint arithmeticExceptionTest() { int failures = 0;
BigDecimal x; try { // // The string representation "1e2147483647", which is equivalent // to 10^Integer.MAX_VALUE, is used to create an augend with an // unscaled value of 1 and a scale of -Integer.MAX_VALUE. The // addend "1" has an unscaled value of 1 with a scale of 0. The // addition is performed exactly and is specified to have a // preferred scale of max(-Integer.MAX_VALUE, 0). As the scale // of the result is 0, a value with Integer.MAX_VALUE + 1 digits // would need to be created. Therefore the next statement is // expected to overflow with an ArithmeticException. //
x = new BigDecimal("1e2147483647").add(new BigDecimal(1));
failures++;
} catch (ArithmeticException ae) {
} return failures;
}
publicstaticvoid main(String argv[]) { int failures = 0;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.