|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +/* |
| 27 | + * @test |
| 28 | + * @bug 8159023 |
| 29 | + * @summary Confirm behavior of mantissa for scientific notation in Decimal Format |
| 30 | + * @run junit MantissaDigits |
| 31 | + */ |
| 32 | + |
| 33 | +import java.text.DecimalFormat; |
| 34 | +import java.text.DecimalFormatSymbols; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.stream.Stream; |
| 37 | + |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.junit.jupiter.params.ParameterizedTest; |
| 40 | +import org.junit.jupiter.params.provider.Arguments; |
| 41 | +import org.junit.jupiter.params.provider.MethodSource; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 43 | + |
| 44 | +public class MantissaDigits { |
| 45 | + private static final double[] NUMBERS = { |
| 46 | + 1.1, 12.1, 123.1, 1234.1, 12345.1, 123456.1, |
| 47 | + -1.1, -12.1, -123.1, -1234.1, -12345.1, -123456.1, |
| 48 | + 1, 12, 123, 1234, 12345, 123456, 1234567, |
| 49 | + -1, -12, -123, -1234, -12345, -123456, -1234567, |
| 50 | + 1.1234, 1.1111, 1.412, 222.333, -771.2222 |
| 51 | + }; |
| 52 | + private static final DecimalFormatSymbols DFS = new DecimalFormatSymbols(Locale.US); |
| 53 | + private static final String ERRMSG = "%s formatted with %s gives %s, and " + |
| 54 | + "significant digit count was %s, but the formula provided %s%n"; |
| 55 | + // Hard coded as 1, since all test patterns only have 1 exponent digit |
| 56 | + private static final int EXPONENTDIGITS = 1; |
| 57 | + |
| 58 | + @ParameterizedTest |
| 59 | + @MethodSource("patterns") |
| 60 | + public void testMantissaDefinition(String pattern, int minDigits, int maxDigits) { |
| 61 | + DecimalFormat df = new DecimalFormat(pattern, DFS); |
| 62 | + for (double number : NUMBERS) { |
| 63 | + // Count the significant digits in the pre-formatted number |
| 64 | + int originalNumDigits = (int) String.valueOf(number).chars() |
| 65 | + .filter(Character::isDigit).count(); |
| 66 | + |
| 67 | + if (wholeNumber(number)) { |
| 68 | + // Trailing 0 should not be counted |
| 69 | + originalNumDigits--; |
| 70 | + } |
| 71 | + |
| 72 | + // Format the number, then grab the significant |
| 73 | + // digits inside the mantissa |
| 74 | + String formattedNum = df.format(number); |
| 75 | + int mantissaDigits = (int) formattedNum.chars() |
| 76 | + .filter(Character::isDigit).count() - EXPONENTDIGITS; |
| 77 | + |
| 78 | + // Test the new definition of the Mantissa |
| 79 | + Integer calculatedDigits = Math |
| 80 | + .min(Math.max(minDigits, originalNumDigits), maxDigits); |
| 81 | + assertEquals(mantissaDigits, calculatedDigits, String.format(ERRMSG, |
| 82 | + number, pattern, formattedNum, mantissaDigits, calculatedDigits)); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static Boolean wholeNumber(double number) { |
| 87 | + return (int) number == number; |
| 88 | + } |
| 89 | + |
| 90 | + private static Stream<Arguments> patterns() { |
| 91 | + return Stream.of( |
| 92 | + Arguments.of("#0.0##E0", 2, 5), |
| 93 | + Arguments.of("#00.00##E0", 4, 7), |
| 94 | + Arguments.of("#0.000##E0", 4, 7), |
| 95 | + Arguments.of("#00.000##E0", 5, 8), |
| 96 | + Arguments.of("#000.0##E0", 4, 7), |
| 97 | + Arguments.of("#000.00##E0", 5, 8), |
| 98 | + Arguments.of("#000.000##E0", 6, 9), |
| 99 | + Arguments.of("000.000E0", 6, 6), |
| 100 | + Arguments.of("#.##E0", 0, 3), |
| 101 | + Arguments.of("######.######E0", 0, 12), |
| 102 | + Arguments.of("####00.00######E0", 4, 14) |
| 103 | + ); |
| 104 | + } |
| 105 | +} |
0 commit comments