File: Bug4838107.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (282 lines) | stat: -rw-r--r-- 13,826 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 4838107 8008577
 * @summary Confirm that DecimalFormat can format a number with a negative
 *          exponent number correctly. Tests also involve using a DecimalFormat
 *          with a custom pattern or a custom minus sign.
 * @run junit/othervm -Djava.locale.providers=COMPAT,SPI Bug4838107
 */

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.util.stream.Stream;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

/*
 * This bug is about exponential formatting. But I added test cases for:
 *   - Double and BigDecimal numbers which don't have exponent parts.
 *   - Long and BigInteger numbers which don't support exponential
 *     notation.
 * because there are few test cases for suffix and prefix.
 * And also, I added test cases to guarantee further formatting and
 * parsing using the same DecimalFormat instance will not change the
 * Number's value anymore.
 */
public class Bug4838107 {

    // Save JVM default Locale
    private static final Locale savedLocale = Locale.getDefault();

    // Set JVM default Locale to US
    @BeforeAll
    static void init() {
        Locale.setDefault(Locale.US);
    }

    // Restore the original JVM default locale
    @AfterAll
    static void tearDown() {
        Locale.setDefault(savedLocale);
    }

    // Check that negative exponent number recognized for doubles
    @ParameterizedTest
    @MethodSource("doubles")
    public void doubleTest(Number num, String str, DecimalFormat df) {
        test(num, str, df);
    }

    // Provides a double to be formatted, which is compared to the expected String.
    // Additionally, provides a DecimalFormat to do the formatting (can have a custom
    // pattern and minus sign). Given in the form (double, String, DecimalFormat).
    private static Stream<Arguments> doubles() {
        DecimalFormat defaultDf = new DecimalFormat();
        DecimalFormat customDf1 = getDecimalFormat("<P>#.###E00<S>", 'm');
        DecimalFormat customDf2 = getDecimalFormat("<P>#.###E00<S>;#.###E00", 'm');
        DecimalFormat customDf3 = getDecimalFormat("#.###E00;<P>#.###E00<S>", 'm');
        DecimalFormat customDf4 = getDecimalFormat("<P>#.###E00<S>;<p>-#.###E00<s>", 'm');
        return Stream.of(
                // Test with default pattern
                Arguments.of(1234D,    "1,234", defaultDf),
                Arguments.of(0.1234,  "0.123", defaultDf),    // rounded
                Arguments.of(-1234D,   "-1,234", defaultDf),
                Arguments.of(-0.1234, "-0.123", defaultDf),    // rounded
                Arguments.of(Double.POSITIVE_INFINITY, "\u221e", defaultDf),
                Arguments.of(Double.NEGATIVE_INFINITY, "-\u221e", defaultDf),
                Arguments.of(Double.NaN, "\ufffd", defaultDf), // without prefix and suffix
                Arguments.of(0.0,  "0", defaultDf),
                Arguments.of(-0.0, "-0", defaultDf),   // with the minus sign
                // Test with a pattern and the minus sign
                Arguments.of(1234D,    "<P>1.234E03<S>", customDf1),
                Arguments.of(0.1234,  "<P>1.234Em01<S>", customDf1),
                Arguments.of(-1234D,   "m<P>1.234E03<S>", customDf1),
                Arguments.of(-0.1234, "m<P>1.234Em01<S>", customDf1),
                Arguments.of(1234D,    "<P>1.234E03<S>", customDf2),
                Arguments.of(0.1234,  "<P>1.234Em01<S>", customDf2),
                Arguments.of(-1234D,   "1.234E03", customDf2),
                Arguments.of(-0.1234, "1.234Em01", customDf2),
                Arguments.of(1234D,    "1.234E03", customDf3),
                Arguments.of(0.1234,  "1.234Em01", customDf3),
                Arguments.of(-1234D,   "<P>1.234E03<S>", customDf3),
                Arguments.of(-0.1234, "<P>1.234Em01<S>", customDf3),
                Arguments.of(1234D,    "<P>1.234E03<S>", customDf4),
                Arguments.of(0.1234,  "<P>1.234Em01<S>", customDf4),
                Arguments.of(-1234D,   "<p>m1.234E03<s>", customDf4),
                Arguments.of(-0.1234, "<p>m1.234Em01<s>", customDf4),
                Arguments.of(Double.POSITIVE_INFINITY, "<P>\u221e<S>", customDf4),
                Arguments.of(Double.NEGATIVE_INFINITY, "<p>m\u221e<s>", customDf4),
                Arguments.of(Double.NaN, "\ufffd", customDf4), // without prefix and suffix
                Arguments.of(0.0,  "<P>0E00<S>", customDf4),
                Arguments.of(-0.0, "<p>m0E00<s>", customDf4) // with the minus sign
        );
    }

    // Check that negative exponent number recognized for longs
    @ParameterizedTest
    @MethodSource("longs")
    public void longTest(Number num, String str, DecimalFormat df) {
        test(num, str, df);
    }

    // Same as doubles() data provider, but with long values
    // Given in the form (long, String, DecimalFormat).
    private static Stream<Arguments> longs() {
        DecimalFormat defaultDf = new DecimalFormat();
        DecimalFormat customDf = getDecimalFormat(
                "<P>#,###<S>;<p>-#,###<s>", 'm');
        return Stream.of(
                // Test with default pattern
                Arguments.of(123456789L,  "123,456,789", defaultDf),
                Arguments.of(-123456789L, "-123,456,789", defaultDf),
                Arguments.of(0L, "0", defaultDf),
                Arguments.of(-0L, "0", defaultDf),
                // Test with a pattern and the minus sign
                Arguments.of(123456789L,  "<P>123,456,789<S>", customDf),
                Arguments.of(-123456789L, "<p>m123,456,789<s>", customDf),
                Arguments.of(0L, "<P>0<S>", customDf),
                Arguments.of(-0L, "<P>0<S>", customDf)
        );
    }

    // Check that negative exponent number recognized for bigDecimals
    @ParameterizedTest
    @MethodSource("bigDecimals")
    public void bigDecimalTest(Number num, String str, DecimalFormat df) {
        test(num, str, df);
    }

    // Same as doubles() data provider, but with BigDecimal values
    // Given in the form (BigDecimal, String, DecimalFormat).
    private static Stream<Arguments> bigDecimals() {
        DecimalFormat defaultDf = new DecimalFormat();
        DecimalFormat customDf = getDecimalFormat(
                "<P>#.####################E00<S>;<p>-#.####################E00<s>", 'm');
        return Stream.of(
                // Test with default pattern
                Arguments.of(new BigDecimal("123456789012345678901234567890"),
                        "123,456,789,012,345,678,901,234,567,890", defaultDf),
                Arguments.of(new BigDecimal("0.000000000123456789012345678901234567890"),
                        "0", defaultDf),
                Arguments.of(new BigDecimal("-123456789012345678901234567890"),
                        "-123,456,789,012,345,678,901,234,567,890", defaultDf),
                Arguments.of(new BigDecimal("-0.000000000123456789012345678901234567890"),
                        "-0", defaultDf),
                Arguments.of(new BigDecimal("0"), "0", defaultDf),
                Arguments.of(new BigDecimal("-0"), "0", defaultDf),
                // Test with a pattern and the minus sign
                Arguments.of(new BigDecimal("123456789012345678901234567890"),
                        "<P>1.23456789012345678901E29<S>", customDf),
                Arguments.of(new BigDecimal("0.000000000123456789012345678901234567890"),
                        "<P>1.23456789012345678901Em10<S>", customDf),
                Arguments.of(new BigDecimal("-123456789012345678901234567890"),
                        "<p>m1.23456789012345678901E29<s>", customDf),
                Arguments.of(new BigDecimal("-0.000000000123456789012345678901234567890"),
                        "<p>m1.23456789012345678901Em10<s>", customDf),
                Arguments.of(new BigDecimal("0"), "<P>0E00<S>", customDf),
                Arguments.of(new BigDecimal("-0"), "<P>0E00<S>", customDf)
        );
    }

    // Check that negative exponent number recognized for bigIntegers
    @ParameterizedTest
    @MethodSource("bigIntegers")
    public void bigIntegerTest(Number num, String str, DecimalFormat df) {
        test(num, str, df);
    }

    // Same as doubles() data provider, but with BigInteger values
    // Given in the form (BigInteger, String, DecimalFormat).
    private static Stream<Arguments> bigIntegers() {
        DecimalFormat defaultDf = new DecimalFormat();
        DecimalFormat customDf = getDecimalFormat(
                "<P>#,###<S>;<p>-#,###<s>", 'm');
        return Stream.of(
                // Test with default pattern
                Arguments.of(new BigInteger("123456789012345678901234567890"),
                        "123,456,789,012,345,678,901,234,567,890", defaultDf),
                Arguments.of(new BigInteger("-123456789012345678901234567890"),
                        "-123,456,789,012,345,678,901,234,567,890", defaultDf),
                Arguments.of(new BigInteger("0"), "0", defaultDf),
                Arguments.of(new BigInteger("-0"), "0", defaultDf),
                // Test with a pattern and the minus sign
                Arguments.of(new BigInteger("123456789012345678901234567890"),
                        "<P>123,456,789,012,345,678,901,234,567,890<S>", customDf),
                Arguments.of(new BigInteger("-123456789012345678901234567890"),
                        "<p>m123,456,789,012,345,678,901,234,567,890<s>", customDf),
                Arguments.of(new BigInteger("0"), "<P>0<S>", customDf),
                Arguments.of(new BigInteger("-0"), "<P>0<S>", customDf)
        );
    }

    // Check that the formatted value is correct and also check that
    // it can be round-tripped via parse() and format()
    private static void test(Number num, String str, DecimalFormat df) {
        String formatted = df.format(num);
        assertEquals(str, formatted, String.format("DecimalFormat format(%s) " +
                "Error: number: %s, minus sign: %s", num.getClass().getName(), num, df.getDecimalFormatSymbols().getMinusSign()));

        if (num instanceof BigDecimal || num instanceof BigInteger) {
            df.setParseBigDecimal(true);
        }
        testRoundTrip(formatted, str, num, df);
    }

    // Test that a parsed value can be round-tripped via format() and parse()
    private static void testRoundTrip(String formatted, String str,
                                      Number num, DecimalFormat df) {
        Number parsed1 = null, parsed2 = null;
        try {
            parsed1 = df.parse(formatted);
            formatted = df.format(parsed1);
            parsed2 = df.parse(formatted);
            assertEquals(parsed2, parsed1, """
                            DecimalFormat round trip parse(%s) error:
                                original number: %s
                                parsed number: %s
                                (%s)
                                formatted number: %s
                                re-parsed number: %s
                                (%s)
                                minus sign: %s
                            """.formatted(num.getClass().getName(), str, parsed1, parsed1.getClass().getName(),
                                    formatted, parsed2, parsed2.getClass().getName(), df.getDecimalFormatSymbols().getMinusSign()));
        }
        catch (Exception e) {
            fail("""
                    DecimalFormat parse(%s) threw an Exception: %s
                        original number: %s
                        parsed number: %s
                        (%s)
                        formatted number: %s
                        re-parsed number: %s
                        (%s)
                        minus sign: %s
                    """.formatted(num.getClass().getName(), e.getMessage(), str, parsed1, parsed1.getClass().getName(),
                            formatted, parsed2, parsed2.getClass().getName(), df.getDecimalFormatSymbols().getMinusSign()));
        }
    }

    // Set up custom DecimalFormat with DecimalFormatSymbols
    private static DecimalFormat getDecimalFormat(String pattern, char minusSign) {
        DecimalFormat df = new DecimalFormat();
        DecimalFormatSymbols dfs = df.getDecimalFormatSymbols();
        df.applyPattern(pattern);
        dfs.setMinusSign(minusSign);
        df.setDecimalFormatSymbols(dfs);
        return df;
    }
}