提交 ca1f906e 编写于 作者: A alanb

7036582: Improve test coverage of java.math.BigDecimal

Reviewed-by: darcy
Contributed-by: sergey.kuksenko@oracle.com
上级 0ea812f8
...@@ -288,12 +288,6 @@ javax/management/monitor/AttributeArbitraryDataTypeTest.java generic-all ...@@ -288,12 +288,6 @@ javax/management/monitor/AttributeArbitraryDataTypeTest.java generic-all
# jdk_math # jdk_math
# Problems with rounding add failures on solaris-sparcv9 and -server
java/math/BigDecimal/AddTests.java solaris-sparcv9
# Should be samevm? But seems problematic with samevm on windows
java/math/BigInteger/ModPow65537.java generic-all
############################################################################ ############################################################################
# jdk_misc # jdk_misc
......
此差异已折叠。
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
* @test * @test
* @bug 6274390 * @bug 6274390
* @summary Verify {float, double}Value methods work with condensed representation * @summary Verify {float, double}Value methods work with condensed representation
* @run main FloatDoubleValueTests
* @run main/othervm -XX:+AggressiveOpts FloatDoubleValueTests
*/ */
import java.math.*; import java.math.*;
...@@ -64,6 +66,7 @@ public class FloatDoubleValueTests { ...@@ -64,6 +66,7 @@ public class FloatDoubleValueTests {
static void checkDouble(BigDecimal bd, double d) { static void checkDouble(BigDecimal bd, double d) {
double dbd = bd.doubleValue(); double dbd = bd.doubleValue();
if (d != dbd ) { if (d != dbd ) {
String message = String.format("Bad conversion:"+ String message = String.format("Bad conversion:"+
"got %g (%a)\texpected %g (%a)", "got %g (%a)\texpected %g (%a)",
...@@ -156,9 +159,29 @@ public class FloatDoubleValueTests { ...@@ -156,9 +159,29 @@ public class FloatDoubleValueTests {
} }
} }
static void testFloatValue1() {
checkFloat(new BigDecimal("85070591730234615847396907784232501249"), 8.507059e+37f);
checkFloat(new BigDecimal("7784232501249e12"), 7.7842326e24f);
checkFloat(new BigDecimal("907784232501249e-12"),907.78424f);
checkFloat(new BigDecimal("7784e8"),7.7839997e11f);
checkFloat(new BigDecimal("9077e-8"),9.077e-5f);
}
static void testDoubleValue1() {
checkDouble(new BigDecimal("85070591730234615847396907784232501249"), 8.507059173023462e37);
checkDouble(new BigDecimal("7784232501249e12"), 7.784232501249e24);
checkDouble(new BigDecimal("907784232501249e-12"), 907.784232501249);
checkDouble(new BigDecimal("7784e8"), 7.784e11);
checkDouble(new BigDecimal("9077e-8"), 9.077e-5);
}
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
testFloatDoubleValue(); testFloatDoubleValue();
testDoubleValue(); testDoubleValue();
testFloatValue(); testFloatValue();
testFloatValue1();
testDoubleValue1();
} }
} }
/*
* Copyright (c) 2011, 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 7036582
* @summary Some new tests for the add method and constructor with MathContext.
* @run main RangeTests
* @run main/othervm -XX:+AggressiveOpts RangeTests
* @author Sergey V. Kuksenko
*/
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
public class RangeTests {
private static int addTest(BigDecimal arg1, BigDecimal arg2, BigDecimal expectedResult) {
int failures = 0;
BigDecimal result = arg1.add(arg2);
if (!result.equals(expectedResult)) {
System.out.println("Sum:" +
arg1 + " + " +
arg2 + " == " +
result + "; expected " +
expectedResult
);
failures++;
}
result = arg2.add(arg1);
if (!result.equals(expectedResult)) {
System.out.println("Sum:" +
arg2 + " + " +
arg1 + " == " +
result + "; expected " +
expectedResult
);
failures++;
}
return failures;
}
/*
* Test BigDecimal.add(BigDecimal) when values are withing different ranges:
* 1. within 32 bits
* 2. within 64 bits
* 3. outside 64 bits.
*/
private static int addBoundaryTest() {
int failures = 0;
failures += addTest(
new BigDecimal("85070591730234615847396907784232501249"),
BigDecimal.valueOf(0),
new BigDecimal("85070591730234615847396907784232501249") );
failures += addTest(
new BigDecimal("-85070591730234615847396907784232501249"),
BigDecimal.valueOf(0),
new BigDecimal("-85070591730234615847396907784232501249") );
failures += addTest(
new BigDecimal("85070591730234615847396907784232501249"),
BigDecimal.valueOf(1),
new BigDecimal("85070591730234615847396907784232501250") );
failures += addTest(
new BigDecimal("85070591730234615847396907784232501249"),
BigDecimal.valueOf(-1),
new BigDecimal("85070591730234615847396907784232501248") );
failures += addTest(
new BigDecimal("-85070591730234615847396907784232501250"),
BigDecimal.valueOf(-1),
new BigDecimal("-85070591730234615847396907784232501251") );
failures += addTest(
new BigDecimal("-85070591730234615847396907784232501249"),
BigDecimal.valueOf(1),
new BigDecimal("-85070591730234615847396907784232501248") );
failures += addTest(
new BigDecimal("147573952589676412927"),
BigDecimal.valueOf(Integer.MAX_VALUE),
new BigDecimal("147573952591823896574") );
failures += addTest(
new BigDecimal("-147573952589676412927"),
BigDecimal.valueOf(Integer.MAX_VALUE),
new BigDecimal("-147573952587528929280") );
failures += addTest(
new BigDecimal("79228162514264337593543950335"),
BigDecimal.valueOf(999),
new BigDecimal("79228162514264337593543951334") );
failures += addTest(
new BigDecimal("79228162514264337593543950335"),
BigDecimal.valueOf(Integer.MAX_VALUE/2),
new BigDecimal("79228162514264337594617692158") );
failures += addTest(
new BigDecimal("79228162514264337593543950335"),
BigDecimal.valueOf(Integer.MIN_VALUE/2),
new BigDecimal("79228162514264337592470208511") );
failures += addTest(
new BigDecimal("-79228162514264337593543950335"),
BigDecimal.valueOf(Integer.MAX_VALUE/2),
new BigDecimal("-79228162514264337592470208512") );
failures += addTest(
new BigDecimal("79228162514264337593543950335"),
BigDecimal.valueOf(-(Integer.MIN_VALUE/2)),
new BigDecimal("79228162514264337594617692159") );
failures += addTest(
new BigDecimal("79228162514264337593543950335"),
BigDecimal.valueOf(Long.MAX_VALUE/2),
new BigDecimal("79228162518876023611971338238") );
failures += addTest(
new BigDecimal("79228162514264337593543950335"),
BigDecimal.valueOf(Long.MIN_VALUE/2),
new BigDecimal("79228162509652651575116562431") );
failures += addTest(
new BigDecimal("-79228162514264337593543950335"),
BigDecimal.valueOf(Long.MAX_VALUE/2),
new BigDecimal("-79228162509652651575116562432") );
failures += addTest(
new BigDecimal("79228162514264337593543950335"),
BigDecimal.valueOf(-(Long.MIN_VALUE/2)),
new BigDecimal("79228162518876023611971338239") );
failures += addTest(
new BigDecimal("-9223372036854775808"),
BigDecimal.valueOf(1),
new BigDecimal("-9223372036854775807") );
failures += addTest(
new BigDecimal("-9223372036854775808"),
BigDecimal.valueOf(Long.MAX_VALUE/2),
new BigDecimal("-4611686018427387905") );
failures += addTest(
new BigDecimal("9223372036854775808"),
BigDecimal.valueOf(-1),
new BigDecimal("9223372036854775807") );
failures += addTest(
new BigDecimal("9223372036854775808"),
BigDecimal.valueOf(-Long.MAX_VALUE/2),
new BigDecimal("4611686018427387905") );
return failures;
}
private static int testRoundingFromBigInteger(BigInteger bi, int scale, MathContext mc) {
int failures = 0;
BigDecimal bd1 = new BigDecimal(bi,scale, mc);
BigDecimal bd2 = (new BigDecimal(bi,scale)).round(mc);
if (!bd1.equals(bd2)) {
System.out.println("new BigDecimal(BigInteger,int,MathContext):" +
"BigInteger == " +
bi + "; scale == " + scale + "; result == " +
bd1 + "; expected == " +
bd2
);
failures++;
}
return failures;
}
private static int roundingConstructorTest() {
int failures = 0;
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
7, MathContext.DECIMAL64);
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
0, MathContext.DECIMAL64);
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
-7, MathContext.DECIMAL64);
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
7, MathContext.DECIMAL128);
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
177, MathContext.DECIMAL128);
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
177, MathContext.DECIMAL32);
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
177, MathContext.UNLIMITED);
failures += testRoundingFromBigInteger(
new BigInteger("85070591730234615847396907784232501249"),
0, MathContext.UNLIMITED);
return failures;
}
private static int minLongConstructorTest(MathContext mc) {
int failures = 0;
BigDecimal bd1 = new BigDecimal(Long.MIN_VALUE,mc);
BigDecimal bd2 = new BigDecimal(Long.MIN_VALUE).round(mc);
if (!bd1.equals(bd2)) {
System.out.println("new BigDecimal(long,MathContext):" +
"long == " +
Long.MIN_VALUE + "; result == " +
bd1 + "; expected == " +
bd2
);
failures++;
}
return failures;
}
private static int minLongConstructorTest() {
int failures = 0;
failures+=minLongConstructorTest(MathContext.UNLIMITED);
failures+=minLongConstructorTest(MathContext.DECIMAL32);
failures+=minLongConstructorTest(MathContext.DECIMAL64);
failures+=minLongConstructorTest(MathContext.DECIMAL128);
return failures;
}
public static void main(String argv[]) {
int failures = 0;
failures += addBoundaryTest();
failures += roundingConstructorTest();
failures += minLongConstructorTest();
if (failures > 0) {
throw new RuntimeException("Incurred " + failures +
" failures while testing.");
}
}
}
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
* @test * @test
* @bug 4108852 * @bug 4108852
* @summary A few tests of stripTrailingZeros * @summary A few tests of stripTrailingZeros
* @run main StrippingZerosTest
* @run main/othervm -XX:+AggressiveOpts StrippingZerosTest
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
...@@ -53,6 +55,11 @@ public class StrippingZerosTest { ...@@ -53,6 +55,11 @@ public class StrippingZerosTest {
{new BigDecimal("10000000e2"), new BigDecimal("1e9")}, {new BigDecimal("10000000e2"), new BigDecimal("1e9")},
{new BigDecimal("1000000e3"), new BigDecimal("1e9")}, {new BigDecimal("1000000e3"), new BigDecimal("1e9")},
{new BigDecimal("100000e4"), new BigDecimal("1e9")}, {new BigDecimal("100000e4"), new BigDecimal("1e9")},
// BD value which larger than Long.MaxValue
{new BigDecimal("1.0000000000000000000000000000"), new BigDecimal("1")},
{new BigDecimal("-1.0000000000000000000000000000"), new BigDecimal("-1")},
{new BigDecimal("1.00000000000000000000000000001"), new BigDecimal("1.00000000000000000000000000001")},
{new BigDecimal("1000000000000000000000000000000e4"), new BigDecimal("1e34")},
}; };
for(int i = 0; i < testCases.length; i++) { for(int i = 0; i < testCases.length; i++) {
......
...@@ -25,6 +25,8 @@ ...@@ -25,6 +25,8 @@
* @test * @test
* @bug 4984872 * @bug 4984872
* @summary Basic tests of toPlainString method * @summary Basic tests of toPlainString method
* @run main ToPlainStringTests
* @run main/othervm -XX:+AggressiveOpts ToPlainStringTests
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
...@@ -60,6 +62,11 @@ public class ToPlainStringTests { ...@@ -60,6 +62,11 @@ public class ToPlainStringTests {
{"8e-8", "0.00000008"}, {"8e-8", "0.00000008"},
{"9e-9", "0.000000009"}, {"9e-9", "0.000000009"},
{"9000e-12", "0.000000009000"}, {"9000e-12", "0.000000009000"},
{"9000e-22", "0.0000000000000000009000"},
{"12345678901234567890", "12345678901234567890"},
{"12345678901234567890e22", "123456789012345678900000000000000000000000"},
{"12345678901234567890e-22", "0.0012345678901234567890"},
}; };
int errors = 0; int errors = 0;
...@@ -73,8 +80,8 @@ public class ToPlainStringTests { ...@@ -73,8 +80,8 @@ public class ToPlainStringTests {
s + "'' from BigDecimal " + s + "'' from BigDecimal " +
bd); bd);
} }
bd = new BigDecimal("-"+testCase[0]);
if (!(s=("-"+bd.toPlainString())).equals("-"+testCase[1])) { if (bd.signum()!=0 && !(s=(bd.toPlainString())).equals("-"+testCase[1])) {
errors++; errors++;
System.err.println("Unexpected plain result ``" + System.err.println("Unexpected plain result ``" +
s + "'' from BigDecimal " + s + "'' from BigDecimal " +
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册