Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/text/Format/DecimalFormat/Bug6609740.java
41152 views
1
/*
2
* Copyright (c) 2017, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
/*
24
* @test
25
* @bug 6609740
26
* @summary Checks the formatting and parsing of a number based
27
* on the positive and negative sub-patterns, also
28
* checks few invalid number patterns
29
*/
30
import java.text.DecimalFormat;
31
import java.text.NumberFormat;
32
import java.text.ParseException;
33
import java.util.Locale;
34
35
public class Bug6609740 {
36
37
public static void main(String[] args) {
38
39
double dNumber = -3456.349347;
40
String fOutput = "(3,456.35)";
41
String[] validCases = {"#,##0.0#;(#,##0.0#)", "#,##0.0#;(#)",
42
"#,##0.0#;(#,##0)"};
43
44
// formatting with the valid cases
45
NumberFormat nf = NumberFormat.getInstance(Locale.US);
46
for (String pattern : validCases) {
47
formatOnPattern(nf, pattern, dNumber, fOutput);
48
}
49
50
// parsing with the valid cases
51
String parseString = "(3,456.35)";
52
Number pOutput = -3456.35;
53
for (String pattern : validCases) {
54
parseOnPattern(nf, pattern, parseString, pOutput);
55
}
56
57
// should throw parse exception
58
String[] invalidParseCases = {"#,##0.0#;0", "#,##0.0#;()"};
59
for (String pattern : invalidParseCases) {
60
if (nf instanceof DecimalFormat) {
61
((DecimalFormat) nf).applyPattern(pattern);
62
}
63
64
try {
65
nf.parse(parseString);
66
} catch (ParseException ex) {
67
continue;
68
}
69
throw new RuntimeException("[FAILED: Should throw"
70
+ " ParseException for pattern: "
71
+ pattern + " and input: " + parseString + "]");
72
}
73
74
// should throw exception on invalid patterns
75
// invalid patterns: no positive subpattern, zero after non-zero in
76
// the decimal part i.e. 0#0, multiple decimal separators,
77
// multiple percent, malformed pattern
78
String[] invalidPatterns = {";(#,##0.0#)", "#,##0.0#0;(#)",
79
"#,##0.0.#", "#,##0%%", ".#,##0"};
80
for (String pattern : invalidPatterns) {
81
if (nf instanceof DecimalFormat) {
82
try {
83
((DecimalFormat) nf).applyPattern(pattern);
84
} catch (IllegalArgumentException ex) {
85
continue;
86
}
87
throw new RuntimeException("[FAILED: Should throw"
88
+ " IllegalArgumentException for invalid pattern: "
89
+ pattern + "]");
90
}
91
}
92
}
93
94
private static void formatOnPattern(NumberFormat nf, String pattern,
95
double number, String expected) {
96
97
if (nf instanceof DecimalFormat) {
98
((DecimalFormat) nf).applyPattern(pattern);
99
}
100
101
String formatted = nf.format(number);
102
if (!formatted.equals(expected)) {
103
throw new RuntimeException("[FAILED: Unable to format the number"
104
+ " based on the pattern: '" + pattern + "', Expected : '"
105
+ expected + "', Found: '" + formatted + "']");
106
}
107
}
108
109
private static void parseOnPattern(NumberFormat nf, String pattern,
110
String parseString, Number expected) {
111
112
if (nf instanceof DecimalFormat) {
113
((DecimalFormat) nf).applyPattern(pattern);
114
}
115
116
try {
117
Number output = nf.parse(parseString);
118
if (expected.doubleValue() != output.doubleValue()) {
119
throw new RuntimeException("[FAILED: Unable to parse the number"
120
+ " based on the pattern: '" + pattern + "', Expected : '"
121
+ expected + "', Found: '" + output + "']");
122
}
123
} catch (ParseException ex) {
124
throw new RuntimeException("[FAILED: Unable to parse the pattern:"
125
+ " '" + pattern + "']", ex);
126
}
127
}
128
129
}
130
131