Path: blob/master/test/jdk/java/math/BigDecimal/LongValueExactTests.java
41149 views
/*1* Copyright (c) 2005, 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 6806261 821193626* @summary Tests of BigDecimal.longValueExact27*/28import java.math.*;29import java.util.List;30import java.util.Map;31import static java.util.Map.entry;3233public class LongValueExactTests {34public static void main(String... args) {35int failures = 0;3637failures += longValueExactSuccessful();38failures += longValueExactExceptional();3940if (failures > 0) {41throw new RuntimeException("Incurred " + failures +42" failures while testing longValueExact.");43}44}4546private static long simpleLongValueExact(BigDecimal bd) {47return bd.toBigIntegerExact().longValue();48}4950private static int longValueExactSuccessful() {51int failures = 0;5253// Strings used to create BigDecimal instances on which invoking54// longValueExact() will succeed.55Map<BigDecimal, Long> successCases =56Map.ofEntries(entry(new BigDecimal("9223372036854775807"), Long.MAX_VALUE), // 2^63 -157entry(new BigDecimal("9223372036854775807.0"), Long.MAX_VALUE),58entry(new BigDecimal("9223372036854775807.00"), Long.MAX_VALUE),5960entry(new BigDecimal("-9223372036854775808"), Long.MIN_VALUE), // -2^6361entry(new BigDecimal("-9223372036854775808.0"), Long.MIN_VALUE),62entry(new BigDecimal("-9223372036854775808.00"),Long.MIN_VALUE),6364entry(new BigDecimal("1e0"), 1L),65entry(new BigDecimal(BigInteger.ONE, -18), 1_000_000_000_000_000_000L),6667entry(new BigDecimal("0e13"), 0L), // Fast path zero68entry(new BigDecimal("0e64"), 0L),69entry(new BigDecimal("0e1024"), 0L),7071entry(new BigDecimal("10.000000000000000000000000000000000"), 10L));7273for (var testCase : successCases.entrySet()) {74BigDecimal bd = testCase.getKey();75long expected = testCase.getValue();76try {77long longValueExact = bd.longValueExact();78if (expected != longValueExact ||79longValueExact != simpleLongValueExact(bd)) {80failures++;81System.err.println("Unexpected longValueExact result " + longValueExact +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 longValueExactExceptional() {93int failures = 0;94List<BigDecimal> exceptionalCases =95List.of(new BigDecimal("9223372036854775808"), // Long.MAX_VALUE + 196new BigDecimal("9223372036854775808.0"),97new BigDecimal("9223372036854775808.00"),98new BigDecimal("-9223372036854775809"), // Long.MIN_VALUE - 199new BigDecimal("-9223372036854775808.1"),100new BigDecimal("-9223372036854775808.01"),101102new BigDecimal("9999999999999999999"),103new BigDecimal("10000000000000000000"),104105new BigDecimal("0.99"),106new BigDecimal("0.999999999999999999999"));107108for (BigDecimal bd : exceptionalCases) {109try {110long longValueExact = bd.longValueExact();111failures++;112System.err.println("Unexpected non-exceptional longValueExact on " + bd);113} catch (ArithmeticException e) {114// Success;115}116}117return failures;118}119}120121122