Path: blob/master/test/jdk/java/math/BigDecimal/IntegralValueTests.java
41152 views
/*1* Copyright (c) 2019, 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 821193626* @summary Tests of BigDecimal.intValue() and BigDecimal.longValue()27*/28import java.math.BigDecimal;29import java.util.Map;3031public class IntegralValueTests {32public static void main(String... args) {33int failures =34integralValuesTest(INT_VALUES, true) +35integralValuesTest(LONG_VALUES, false);36if (failures != 0) {37throw new RuntimeException38("Incurred " + failures + " failures for {int,long}Value().");39}40}4142private static final Map<BigDecimal, Number> INT_VALUES =43Map.ofEntries(4445// 2**31 - 146Map.entry(new BigDecimal("2147483647"), Integer.MAX_VALUE),47Map.entry(new BigDecimal("2147483647.0"), Integer.MAX_VALUE),48Map.entry(new BigDecimal("2147483647.00"), Integer.MAX_VALUE),4950Map.entry(new BigDecimal("-2147483647"), -Integer.MAX_VALUE),51Map.entry(new BigDecimal("-2147483647.0"), -Integer.MAX_VALUE),5253// -2**3154Map.entry(new BigDecimal("-2147483648"), Integer.MIN_VALUE),55Map.entry(new BigDecimal("-2147483648.1"), Integer.MIN_VALUE),56Map.entry(new BigDecimal("-2147483648.01"), Integer.MIN_VALUE),5758// -2**31 + 1 truncation to 2**31 - 159Map.entry(new BigDecimal("-2147483649"), Integer.MAX_VALUE),6061// 2**64 - 1 truncation to 162Map.entry(new BigDecimal("4294967295"), -1),6364// 2**64 truncation to 065Map.entry(new BigDecimal("4294967296"), 0),6667// Fast path truncation to 068Map.entry(new BigDecimal("1e32"), 0),6970// Slow path truncation to -2**3171Map.entry(new BigDecimal("1e31"), Integer.MIN_VALUE),7273// Slow path74Map.entry(new BigDecimal("1e0"), 1),7576// Fast path round to 077Map.entry(new BigDecimal("9e-1"), 0),7879// Some random values80Map.entry(new BigDecimal("900e-1"), 90), // Increasing negative exponents81Map.entry(new BigDecimal("900e-2"), 9),82Map.entry(new BigDecimal("900e-3"), 0),8384// Fast path round to 085Map.entry(new BigDecimal("123456789e-9"), 0),8687// Slow path round to 188Map.entry(new BigDecimal("123456789e-8"), 1),8990// Increasing positive exponents91Map.entry(new BigDecimal("10000001e1"), 100000010),92Map.entry(new BigDecimal("10000001e10"), -1315576832),93Map.entry(new BigDecimal("10000001e100"), 0),94Map.entry(new BigDecimal("10000001e1000"), 0),95Map.entry(new BigDecimal("10000001e10000"), 0),96Map.entry(new BigDecimal("10000001e100000"), 0),97Map.entry(new BigDecimal("10000001e1000000"), 0),98Map.entry(new BigDecimal("10000001e10000000"), 0),99Map.entry(new BigDecimal("10000001e100000000"), 0),100Map.entry(new BigDecimal("10000001e1000000000"), 0),101102// Increasing negative exponents103Map.entry(new BigDecimal("10000001e-1"), 1000000),104Map.entry(new BigDecimal("10000001e-10"), 0),105Map.entry(new BigDecimal("10000001e-100"), 0),106Map.entry(new BigDecimal("10000001e-1000"), 0),107Map.entry(new BigDecimal("10000001e-10000"), 0),108Map.entry(new BigDecimal("10000001e-100000"), 0),109Map.entry(new BigDecimal("10000001e-1000000"), 0),110Map.entry(new BigDecimal("10000001e-10000000"), 0),111Map.entry(new BigDecimal("10000001e-100000000"), 0),112Map.entry(new BigDecimal("10000001e-1000000000"), 0),113114// Currency calculation to 4 places115Map.entry(new BigDecimal("12345.0001"), 12345),116Map.entry(new BigDecimal("12345.9999"), 12345),117Map.entry(new BigDecimal("-12345.0001"), -12345),118Map.entry(new BigDecimal("-12345.9999"), -12345));119120private static final Map<BigDecimal, Number> LONG_VALUES =121Map.ofEntries(122// 2**63 - 1123Map.entry(new BigDecimal("9223372036854775807"), Long.MAX_VALUE),124Map.entry(new BigDecimal("9223372036854775807.0"), Long.MAX_VALUE),125Map.entry(new BigDecimal("9223372036854775807.00"), Long.MAX_VALUE),126127// 2**63 truncation to -2**63128Map.entry(new BigDecimal("-9223372036854775808"), Long.MIN_VALUE),129Map.entry(new BigDecimal("-9223372036854775808.1"), Long.MIN_VALUE),130Map.entry(new BigDecimal("-9223372036854775808.01"), Long.MIN_VALUE),131132// -2**63 + 1 truncation to 2**63 - 1133Map.entry(new BigDecimal("-9223372036854775809"), 9223372036854775807L),134135// 2**64 - 1 truncation to -1136Map.entry(new BigDecimal("18446744073709551615"), -1L),137138// 2**64 truncation to 0139Map.entry(new BigDecimal("18446744073709551616"), 0L),140141// Slow path truncation to -2**63142Map.entry(new BigDecimal("1e63"), -9223372036854775808L),143Map.entry(new BigDecimal("-1e63"), -9223372036854775808L),144// Fast path with larger magnitude scale145Map.entry(new BigDecimal("1e64"), 0L),146Map.entry(new BigDecimal("-1e64"), 0L),147Map.entry(new BigDecimal("1e65"), 0L),148Map.entry(new BigDecimal("-1e65"), 0L),149150// Slow path151Map.entry(new BigDecimal("1e0"), 1L),152153// Fast path round to 0154Map.entry(new BigDecimal("9e-1"), 0L),155156// Some random values157Map.entry(new BigDecimal("900e-1"), 90L), // Increasing negative exponents158Map.entry(new BigDecimal("900e-2"), 9L),159Map.entry(new BigDecimal("900e-3"), 0L),160161// Fast path round to 0162Map.entry(new BigDecimal("123456789e-9"), 0L),163164// Slow path round to 1165Map.entry(new BigDecimal("123456789e-8"), 1L),166167// Increasing positive exponents168Map.entry(new BigDecimal("10000001e1"), 100000010L),169Map.entry(new BigDecimal("10000001e10"), 100000010000000000L),170Map.entry(new BigDecimal("10000001e100"), 0L),171Map.entry(new BigDecimal("10000001e1000"), 0L),172Map.entry(new BigDecimal("10000001e10000"), 0L),173Map.entry(new BigDecimal("10000001e100000"), 0L),174Map.entry(new BigDecimal("10000001e1000000"), 0L),175Map.entry(new BigDecimal("10000001e10000000"), 0L),176Map.entry(new BigDecimal("10000001e100000000"), 0L),177Map.entry(new BigDecimal("10000001e1000000000"), 0L),178179// Increasing negative exponents180Map.entry(new BigDecimal("10000001e-1"), 1000000L),181Map.entry(new BigDecimal("10000001e-10"), 0L),182Map.entry(new BigDecimal("10000001e-100"), 0L),183Map.entry(new BigDecimal("10000001e-1000"), 0L),184Map.entry(new BigDecimal("10000001e-10000"), 0L),185Map.entry(new BigDecimal("10000001e-100000"), 0L),186Map.entry(new BigDecimal("10000001e-1000000"), 0L),187Map.entry(new BigDecimal("10000001e-10000000"), 0L),188Map.entry(new BigDecimal("10000001e-100000000"), 0L),189Map.entry(new BigDecimal("10000001e-1000000000"), 0L),190191// Currency calculation to 4 places192Map.entry(new BigDecimal("12345.0001"), 12345L),193Map.entry(new BigDecimal("12345.9999"), 12345L),194Map.entry(new BigDecimal("-12345.0001"), -12345L),195Map.entry(new BigDecimal("-12345.9999"), -12345L));196197private static int integralValuesTest(Map<BigDecimal, Number> v, boolean isInt) {198System.err.format("Testing %s%n", isInt ? "Integer" : "Long");199int failures = 0;200for (var testCase : v.entrySet()) {201BigDecimal bd = testCase.getKey();202Number expected = testCase.getValue();203try {204if (isInt) {205int intValue = bd.intValue();206if (intValue != (int)expected) {207failures += reportError(bd, expected, intValue, isInt);208}209} else {210long longValue = bd.longValue();211if (longValue != (long)expected) {212failures += reportError(bd, expected, longValue, isInt);213}214}215} catch (Exception e) {216failures++;217System.err.format("Unexpected exception %s for %s%n",218e, bd.toString());219}220}221return failures;222}223224private static int reportError(BigDecimal bd, Number expected, long longValue, boolean isInt) {225System.err.format("For %s, scale=%d, expected %d, actual %d, simple %d%n",226bd.toString(), bd.scale(),227(isInt ? (Integer) expected : (Long) expected ),228longValue,229(isInt ? simpleIntValue(bd): simpleLongValue(bd) ));230return 1;231}232233private static long simpleLongValue(BigDecimal bd) {234return bd.toBigInteger().longValue();235}236237private static int simpleIntValue(BigDecimal bd) {238return bd.toBigInteger().intValue();239}240}241242243