Path: blob/master/test/jdk/java/math/BigDecimal/IntValueExactTests.java
41149 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.intValueExact27*/28import java.math.*;29import java.util.List;30import java.util.Map;31import static java.util.Map.entry;3233public class IntValueExactTests {34public static void main(String... args) {35int failures = 0;3637failures += intValueExactSuccessful();38failures += intValueExactExceptional();3940if (failures > 0) {41throw new RuntimeException("Incurred " + failures +42" failures while testing intValueExact.");43}44}4546private static int simpleIntValueExact(BigDecimal bd) {47return bd.toBigIntegerExact().intValue();48}4950private static int intValueExactSuccessful() {51int failures = 0;5253// Strings used to create BigDecimal instances on which invoking54// intValueExact() will succeed.55Map<BigDecimal, Integer> successCases =56Map.ofEntries(entry(new BigDecimal("2147483647"), Integer.MAX_VALUE), // 2^31 -157entry(new BigDecimal("2147483647.0"), Integer.MAX_VALUE),58entry(new BigDecimal("2147483647.00"), Integer.MAX_VALUE),5960entry(new BigDecimal("-2147483648"), Integer.MIN_VALUE), // -2^3161entry(new BigDecimal("-2147483648.0"), Integer.MIN_VALUE),62entry(new BigDecimal("-2147483648.00"),Integer.MIN_VALUE),6364entry(new BigDecimal("1e0"), 1),65entry(new BigDecimal(BigInteger.ONE, -9), 1_000_000_000),6667entry(new BigDecimal("0e13"), 0), // Fast path zero68entry(new BigDecimal("0e32"), 0),69entry(new BigDecimal("0e512"), 0),7071entry(new BigDecimal("10.000000000000000000000000000000000"), 10));7273for (var testCase : successCases.entrySet()) {74BigDecimal bd = testCase.getKey();75int expected = testCase.getValue();76try {77int intValueExact = bd.intValueExact();78if (expected != intValueExact ||79intValueExact != simpleIntValueExact(bd)) {80failures++;81System.err.println("Unexpected intValueExact result " + intValueExact +82" on " + bd);83}84} catch (Exception e) {85failures++;86System.err.println("Error on " + bd + "\tException message:" + e.getMessage());87}88}89return failures;90}9192private static int intValueExactExceptional() {93int failures = 0;94List<BigDecimal> exceptionalCases =95List.of(new BigDecimal("2147483648"), // Integer.MAX_VALUE + 196new BigDecimal("2147483648.0"),97new BigDecimal("2147483648.00"),98new BigDecimal("-2147483649"), // Integer.MIN_VALUE - 199new BigDecimal("-2147483649.1"),100new BigDecimal("-2147483649.01"),101102new BigDecimal("9999999999999999999999999999999"),103new BigDecimal("10000000000000000000000000000000"),104105new BigDecimal("0.99"),106new BigDecimal("0.999999999999999999999"));107108for (BigDecimal bd : exceptionalCases) {109try {110int intValueExact = bd.intValueExact();111failures++;112System.err.println("Unexpected non-exceptional intValueExact on " + bd);113} catch (ArithmeticException e) {114// Success;115}116}117return failures;118}119}120121122