Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestCompactPatternsValidity.java
41153 views
1
/*
2
* Copyright (c) 2018, 2020, 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 8177552 8217254 8251499
26
* @summary Checks the validity of compact number patterns specified through
27
* CompactNumberFormat constructor
28
* @run testng/othervm TestCompactPatternsValidity
29
*/
30
31
import java.math.BigDecimal;
32
import java.math.BigInteger;
33
import java.text.CompactNumberFormat;
34
import java.text.DecimalFormatSymbols;
35
import java.text.ParseException;
36
import java.util.List;
37
import java.util.Locale;
38
import org.testng.annotations.DataProvider;
39
import org.testng.annotations.Test;
40
41
public class TestCompactPatternsValidity {
42
43
// Max range 10^4
44
private static final String[] COMPACT_PATTERN1 = new String[]{"0", "0", "0", "0K", "00K"};
45
// Quoted special character '.' as prefix
46
private static final String[] COMPACT_PATTERN2 = new String[]{"0", "'.'K0"};
47
// Quoted special character '.' as suffix
48
private static final String[] COMPACT_PATTERN3 = new String[]{"0", "0", "0", "0K", "00K'.'"};
49
// Containing both prefix and suffix
50
private static final String[] COMPACT_PATTERN4 = new String[]{"", "", "H0H", "0K", "00K", "H0G"};
51
// Differing while specifying prefix and suffix
52
private static final String[] COMPACT_PATTERN5 = new String[]{"", "", "", "0K", "K0"};
53
// Containing both prefix ('.') and suffix (K)
54
private static final String[] COMPACT_PATTERN6 = new String[]{"0", "", "", "'.'0K"};
55
// Quoted special character ',' as suffix
56
private static final String[] COMPACT_PATTERN7 = new String[]{"", "0", "0", "0K','"};
57
// Most commonly used type of compact patterns with 15 elements
58
private static final String[] COMPACT_PATTERN8 = new String[]{"", "", "", "0K", "00K", "000K", "0M",
59
"00M", "000M", "0B", "00B", "000B", "0T", "00T", "000T"};
60
// All empty or special patterns; checking the default formatting behaviour
61
private static final String[] COMPACT_PATTERN9 = new String[]{"", "", "", "0", "0", "", "", "", "", "", "", "", "", "", ""};
62
// Patterns beyond 10^19; divisors beyond long range
63
private static final String[] COMPACT_PATTERN10 = new String[]{"", "", "", "0K", "00K", "000K", "0M", "00M",
64
"000M", "0B", "00B", "000B", "0T", "00T", "000T", "0L", "00L", "000L", "0XL", "00XL"};
65
// Containing positive;negative subpatterns
66
private static final String[] COMPACT_PATTERN11 = new String[]{"", "", "", "elfu 0;elfu -0", "elfu 00;elfu -00",
67
"elfu 000;elfu -000", "milioni 0;milioni -0", "milioni 00;milioni -00", "milioni 000;milioni -000"};
68
// Containing both prefix and suffix and positive;negative subpatern
69
private static final String[] COMPACT_PATTERN12 = new String[]{"", "", "H0H;H-0H", "0K;0K-", "00K;-00K", "H0G;-H0G"};
70
// A non empty pattern containing no 0s (min integer digits)
71
private static final String[] COMPACT_PATTERN13 =
72
new String[]{"", "", "", "Thousand", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "BeyondLong"};
73
// A non empty pattern containing no 0s (min integer digits), with plural rules
74
private static final String[] COMPACT_PATTERN14 =
75
new String[]{"", "", "", "{one:Kun other:0' 'Kun}"}; // from Somali in CLDR 38
76
77
@DataProvider(name = "invalidPatterns")
78
Object[][] invalidCompactPatterns() {
79
return new Object[][] {
80
// compact patterns
81
// Pattern containing unquoted special character '.'
82
{new String[]{"", "", "", "0K", "00K."}},
83
// Pattern containing invalid single quote
84
{new String[]{"", "", "", "0 'do", "00K"}},
85
{new String[]{"", "", "", "0K", "00 don't"}},
86
// 0s (min integer digits) exceeding for the range at index 3
87
{new String[]{"", "", "0K", "00000K"}},
88
// null as a compact pattern
89
{new String[]{"", "", null, "00K"}},
90
};
91
}
92
93
@DataProvider(name = "validPatternsFormat")
94
Object[][] validPatternsFormat() {
95
return new Object[][] {
96
// compact patterns, numbers, expected output
97
{COMPACT_PATTERN1, List.of(200, 1000, 3000, 500000), List.of("200", "1K", "3K", "500K")},
98
{COMPACT_PATTERN2, List.of(1, 20, 3000), List.of("1", ".K2", ".K300")},
99
{COMPACT_PATTERN3, List.of(100.99, 1000, 30000), List.of("101", "1K", "30K.")},
100
{COMPACT_PATTERN4, List.of(0.0, 500, -500, 30000, 5000000), List.of("0", "H5H", "-H5H", "30K", "H50G")},
101
{COMPACT_PATTERN5, List.of(100, 1000, 30000), List.of("100", "1K", "K3")},
102
{COMPACT_PATTERN6, List.of(20.99, 1000, 30000), List.of("21", ".1K", ".30K")},
103
{COMPACT_PATTERN7, List.of(100, 1000, new BigInteger("12345678987654321")), List.of("100", "1K,", "12345678987654K,")},
104
{COMPACT_PATTERN8, List.of(new BigInteger("223565686837667632"), new BigDecimal("12322456774334.89766"), 30000, 3456.78),
105
List.of("223566T", "12T", "30K", "3K")},
106
{COMPACT_PATTERN9, List.of(new BigInteger("223566000000000000"), new BigDecimal("12345678987654567"), 30000, 3000),
107
List.of("223,566,000,000,000,000", "12,345,678,987,654,567", "30,000", "3,000")},
108
{COMPACT_PATTERN10, List.of(new BigInteger("100000000000000000"), new BigInteger("10000000000000000000"), new BigDecimal("555555555555555555555.89766"), 30000),
109
List.of("100L", "10XL", "556XL", "30K")},
110
{COMPACT_PATTERN11, List.of(20.99, -20.99, 1000, -1000, 30000, -30000, new BigInteger("12345678987654321"), new BigInteger("-12345678987654321")),
111
List.of("21", "-21", "elfu 1", "elfu -1", "elfu 30", "elfu -30", "milioni 12345678988", "milioni -12345678988")},
112
{COMPACT_PATTERN12, List.of(0, 500, -500, 30000, -3000, 5000000), List.of("0", "H5H", "H-5H", "30K", "3K-", "H50G")},
113
{COMPACT_PATTERN13, List.of(1000, new BigInteger("10000000000000000000")), List.of("Thousand", "BeyondLong")},
114
};
115
}
116
117
@DataProvider(name = "validPatternsParse")
118
Object[][] validPatternsParse() {
119
return new Object[][] {
120
// compact patterns, parse string, expected output
121
{COMPACT_PATTERN1, List.of(".56", "200", ".1K", "3K", "500K"), List.of(0.56, 200L, 100L, 3000L, 500000L)},
122
{COMPACT_PATTERN2, List.of("1", ".K2", ".K300"), List.of(1L, 20L, 3000L)},
123
{COMPACT_PATTERN3, List.of("101", "1K", "30K."), List.of(101L, 1000L, 30000L)},
124
{COMPACT_PATTERN4, List.of("0", "H5H", "-H5H", "30K", "H50G"), List.of(0L, 500L, -500L, 30000L, 5000000L)},
125
{COMPACT_PATTERN5, List.of("100", "1K", "K3"), List.of(100L, 1000L, 30000L)},
126
{COMPACT_PATTERN6, List.of("21", ".1K", ".30K"), List.of(21L, 1000L, 30000L)},
127
{COMPACT_PATTERN7, List.of("100", "1K,", "12345678987654K,"), List.of(100L, 1000L, 12345678987654000L)},
128
{COMPACT_PATTERN8, List.of("223566T", "12T", "30K", "3K"), List.of(223566000000000000L, 12000000000000L, 30000L, 3000L)},
129
{COMPACT_PATTERN10, List.of("1L", "100L", "10XL", "556XL", "30K"), List.of(1000000000000000L, 100000000000000000L, 1.0E19, 5.56E20, 30000L)},
130
{COMPACT_PATTERN11, List.of("21", "-21", "100.90", "-100.90", "elfu 1", "elfu -1", "elfu 30", "elfu -30", "milioni 12345678988", "milioni -12345678988"),
131
List.of(21L, -21L, 100.90, -100.90, 1000L, -1000L, 30000L, -30000L, 12345678988000000L, -12345678988000000L)},
132
{COMPACT_PATTERN12, List.of("0", "H5H", "H-5H", "30K", "30K-", "H50G"), List.of(0L, 500L, -500L, 30000L, -30000L, 5000000L)},
133
{COMPACT_PATTERN13, List.of("Thousand", "BeyondLong"), List.of(1000L, new BigInteger("10000000000000000000"))},
134
};
135
}
136
137
@DataProvider(name = "validPatternsFormatWithPluralRules")
138
Object[][] validPatternsFormatWithPluralRules() {
139
return new Object[][] {
140
// compact patterns, plural rules, numbers, expected output
141
{COMPACT_PATTERN14, "one:n = 1", List.of(1000, 2345), List.of("Kun", "2 Kun")},
142
};
143
}
144
145
@DataProvider(name = "validPatternsParseWithPluralRules")
146
Object[][] validPatternsParseWithPluralRules() {
147
return new Object[][] {
148
// compact patterns, plural rules, parse string, expected output
149
{COMPACT_PATTERN14, "one:n = 1", List.of("Kun", "2 Kun"), List.of(1000L, 2000L)},
150
};
151
}
152
153
@Test(dataProvider = "invalidPatterns",
154
expectedExceptions = IllegalArgumentException.class)
155
public void testInvalidCompactPatterns(String[] compactPatterns) {
156
new CompactNumberFormat("#,##0.0#", DecimalFormatSymbols
157
.getInstance(Locale.US), compactPatterns);
158
}
159
160
@Test(dataProvider = "validPatternsFormat")
161
public void testValidPatternsFormat(String[] compactPatterns,
162
List<Object> numbers, List<String> expected) {
163
CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",
164
DecimalFormatSymbols.getInstance(Locale.US), compactPatterns);
165
for (int index = 0; index < numbers.size(); index++) {
166
CompactFormatAndParseHelper.testFormat(fmt, numbers.get(index),
167
expected.get(index));
168
}
169
}
170
171
@Test(dataProvider = "validPatternsParse")
172
public void testValidPatternsParse(String[] compactPatterns,
173
List<String> parseString, List<Number> numbers) throws ParseException {
174
CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",
175
DecimalFormatSymbols.getInstance(Locale.US), compactPatterns);
176
for (int index = 0; index < parseString.size(); index++) {
177
CompactFormatAndParseHelper.testParse(fmt, parseString.get(index),
178
numbers.get(index), null, null);
179
}
180
}
181
182
@Test(dataProvider = "validPatternsFormatWithPluralRules")
183
public void testValidPatternsFormatWithPluralRules(String[] compactPatterns, String pluralRules,
184
List<Object> numbers, List<String> expected) {
185
CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",
186
DecimalFormatSymbols.getInstance(Locale.US), compactPatterns, pluralRules);
187
for (int index = 0; index < numbers.size(); index++) {
188
CompactFormatAndParseHelper.testFormat(fmt, numbers.get(index),
189
expected.get(index));
190
}
191
}
192
193
@Test(dataProvider = "validPatternsParseWithPluralRules")
194
public void testValidPatternsParsewithPluralRules(String[] compactPatterns, String pluralRules,
195
List<String> parseString, List<Number> numbers) throws ParseException {
196
CompactNumberFormat fmt = new CompactNumberFormat("#,##0.0#",
197
DecimalFormatSymbols.getInstance(Locale.US), compactPatterns, pluralRules);
198
for (int index = 0; index < parseString.size(); index++) {
199
CompactFormatAndParseHelper.testParse(fmt, parseString.get(index),
200
numbers.get(index), null, null);
201
}
202
}
203
}
204
205