Path: blob/master/test/jdk/java/math/BigDecimal/PrecisionTests.java
41149 views
/*1* Copyright (c) 2009, 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 123456726* @summary Test that precision() is computed properly.27* @author Joseph D. Darcy28*/2930import java.math.*;31import static java.math.BigDecimal.*;3233public class PrecisionTests {34private static BigDecimal NINE = valueOf(9);3536public static void main(String argv[]) {37int failures = 0;3839// Smallest and largest values of a given length40BigDecimal[] testValues = {41valueOf(1), valueOf(9),42};4344failures += testPrecision(new BigDecimal(0), 1);4546for(int i = 1; i < 100; i++) {47for(BigDecimal bd : testValues) {48failures += testPrecision(bd, i);49failures += testPrecision(bd.negate(), i);50}5152testValues[0] = testValues[0].multiply(TEN);53testValues[1] = testValues[1].multiply(TEN).add(NINE);54}5556// The following test tries to cover testings for precision of long values57BigDecimal[] randomTestValues = {58valueOf(2147483648L), // 2^31: 10 digits59valueOf(-2147483648L), // -2^31: 10 digits60valueOf(98893745455L), // random: 11 digits61valueOf(3455436789887L), // random: 13 digits62valueOf(140737488355328L), // 2^47: 15 digits63valueOf(-140737488355328L), // -2^47: 15 digits64valueOf(7564232235739573L), // random: 16 digits65valueOf(25335434990002322L), // random: 17 digits66valueOf(9223372036854775807L), // 2^63 - 1: 19 digits67valueOf(-9223372036854775807L) // -2^63 + 1: 19 digits68};69// The array below contains the expected precision of the above numbers70int[] expectedPrecision = {10, 10, 11, 13, 15, 15, 16, 17, 19, 19};71for (int i = 0; i < randomTestValues.length; i++) {72failures += testPrecision(randomTestValues[i], expectedPrecision[i]);73}7475if (failures > 0) {76throw new RuntimeException("Incurred " + failures +77" failures while testing precision.");78}79}8081private static int testPrecision(BigDecimal bd, int expected) {82int precision = bd.precision();8384// System.out.printf("Testing %s, expected %d%n", bd, expected);8586if (precision != expected) {87System.err.printf("For (%s).precision expected %d, got %d%n",88bd, expected, precision);89return 1;90}91return 0;92}93}949596