Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/text/Format/CompactNumberFormat/TestCompactNumber.java
41153 views
1
/*
2
* Copyright (c) 2018, 2019, 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 8217721 8222756
26
* @summary Checks the functioning of compact number format
27
* @modules jdk.localedata
28
* @run testng/othervm TestCompactNumber
29
*/
30
import java.math.BigDecimal;
31
import java.math.BigInteger;
32
import java.text.FieldPosition;
33
import java.text.Format;
34
import java.text.NumberFormat;
35
import java.text.ParseException;
36
import java.text.ParsePosition;
37
import java.util.Locale;
38
import java.util.stream.Stream;
39
import static org.testng.Assert.*;
40
import org.testng.annotations.DataProvider;
41
import org.testng.annotations.Test;
42
43
public class TestCompactNumber {
44
45
private static final NumberFormat FORMAT_DZ_LONG = NumberFormat
46
.getCompactNumberInstance(new Locale("dz"), NumberFormat.Style.LONG);
47
48
private static final NumberFormat FORMAT_EN_US_SHORT = NumberFormat
49
.getCompactNumberInstance(Locale.US, NumberFormat.Style.SHORT);
50
51
private static final NumberFormat FORMAT_EN_LONG = NumberFormat
52
.getCompactNumberInstance(new Locale("en"), NumberFormat.Style.LONG);
53
54
private static final NumberFormat FORMAT_HI_IN_LONG = NumberFormat
55
.getCompactNumberInstance(new Locale("hi", "IN"), NumberFormat.Style.LONG);
56
57
private static final NumberFormat FORMAT_JA_JP_SHORT = NumberFormat
58
.getCompactNumberInstance(Locale.JAPAN, NumberFormat.Style.SHORT);
59
60
private static final NumberFormat FORMAT_IT_SHORT = NumberFormat
61
.getCompactNumberInstance(new Locale("it"), NumberFormat.Style.SHORT);
62
63
private static final NumberFormat FORMAT_CA_LONG = NumberFormat
64
.getCompactNumberInstance(new Locale("ca"), NumberFormat.Style.LONG);
65
66
private static final NumberFormat FORMAT_AS_LONG = NumberFormat
67
.getCompactNumberInstance(new Locale("as"), NumberFormat.Style.LONG);
68
69
private static final NumberFormat FORMAT_BRX_SHORT = NumberFormat
70
.getCompactNumberInstance(new Locale("brx"), NumberFormat.Style.SHORT);
71
72
private static final NumberFormat FORMAT_SW_LONG = NumberFormat
73
.getCompactNumberInstance(new Locale("sw"), NumberFormat.Style.LONG);
74
75
private static final NumberFormat FORMAT_SE_SHORT = NumberFormat
76
.getCompactNumberInstance(new Locale("se"), NumberFormat.Style.SHORT);
77
78
private static final NumberFormat FORMAT_DE_LONG = NumberFormat
79
.getCompactNumberInstance(Locale.GERMAN, NumberFormat.Style.LONG);
80
81
private static final NumberFormat FORMAT_SL_LONG = NumberFormat
82
.getCompactNumberInstance(new Locale("sl"), NumberFormat.Style.LONG);
83
84
@DataProvider(name = "format")
85
Object[][] compactFormatData() {
86
return new Object[][]{
87
// compact number format instance, number to format, formatted output
88
{FORMAT_DZ_LONG, 1000.09, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55"
89
+ "\u0FB2\u0F42 \u0F21"},
90
{FORMAT_DZ_LONG, -999.99, "-\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55"
91
+ "\u0FB2\u0F42 \u0F21"},
92
{FORMAT_DZ_LONG, -0.0, "-\u0F20"},
93
{FORMAT_DZ_LONG, 3000L, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55"
94
+ "\u0FB2\u0F42 \u0F23"},
95
{FORMAT_DZ_LONG, new BigInteger("12345678901234567890"), "\u0F51"
96
+ "\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62\u0F0B\u0F66"
97
+ "\u0F0B\u0F61\u0F0B \u0F21\u0F22\u0F23\u0F24\u0F25\u0F27"},
98
// negative
99
{FORMAT_DZ_LONG, new BigInteger("-12345678901234567890"), "-\u0F51"
100
+ "\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62\u0F0B\u0F66"
101
+ "\u0F0B\u0F61\u0F0B \u0F21\u0F22\u0F23\u0F24\u0F25\u0F27"},
102
{FORMAT_DZ_LONG, new BigDecimal("12345678901234567890.89"), "\u0F51"
103
+ "\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62\u0F0B\u0F66"
104
+ "\u0F0B\u0F61\u0F0B \u0F21\u0F22\u0F23\u0F24\u0F25\u0F27"},
105
{FORMAT_DZ_LONG, new BigDecimal("-12345678901234567890.89"), "-\u0F51"
106
+ "\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62\u0F0B\u0F66"
107
+ "\u0F0B\u0F61\u0F0B \u0F21\u0F22\u0F23\u0F24\u0F25\u0F27"},
108
// Zeros
109
{FORMAT_EN_US_SHORT, 0, "0"},
110
{FORMAT_EN_US_SHORT, 0.0, "0"},
111
{FORMAT_EN_US_SHORT, -0.0, "-0"},
112
// Less than 1000 no suffix
113
{FORMAT_EN_US_SHORT, 499, "499"},
114
// Boundary number
115
{FORMAT_EN_US_SHORT, 1000.0, "1K"},
116
// Long
117
{FORMAT_EN_US_SHORT, 3000L, "3K"},
118
{FORMAT_EN_US_SHORT, 30000L, "30K"},
119
{FORMAT_EN_US_SHORT, 300000L, "300K"},
120
{FORMAT_EN_US_SHORT, 3000000L, "3M"},
121
{FORMAT_EN_US_SHORT, 30000000L, "30M"},
122
{FORMAT_EN_US_SHORT, 300000000L, "300M"},
123
{FORMAT_EN_US_SHORT, 3000000000L, "3B"},
124
{FORMAT_EN_US_SHORT, 30000000000L, "30B"},
125
{FORMAT_EN_US_SHORT, 300000000000L, "300B"},
126
{FORMAT_EN_US_SHORT, 3000000000000L, "3T"},
127
{FORMAT_EN_US_SHORT, 30000000000000L, "30T"},
128
{FORMAT_EN_US_SHORT, 300000000000000L, "300T"},
129
{FORMAT_EN_US_SHORT, 3000000000000000L, "3000T"},
130
// Negatives
131
{FORMAT_EN_US_SHORT, -3000L, "-3K"},
132
{FORMAT_EN_US_SHORT, -30000L, "-30K"},
133
{FORMAT_EN_US_SHORT, -300000L, "-300K"},
134
{FORMAT_EN_US_SHORT, -3000000L, "-3M"},
135
{FORMAT_EN_US_SHORT, -30000000L, "-30M"},
136
{FORMAT_EN_US_SHORT, -300000000L, "-300M"},
137
{FORMAT_EN_US_SHORT, -3000000000L, "-3B"},
138
{FORMAT_EN_US_SHORT, -30000000000L, "-30B"},
139
{FORMAT_EN_US_SHORT, -300000000000L, "-300B"},
140
{FORMAT_EN_US_SHORT, -3000000000000L, "-3T"},
141
{FORMAT_EN_US_SHORT, -30000000000000L, "-30T"},
142
{FORMAT_EN_US_SHORT, -300000000000000L, "-300T"},
143
{FORMAT_EN_US_SHORT, -3000000000000000L, "-3000T"},
144
// Double
145
{FORMAT_EN_US_SHORT, 3000.0, "3K"},
146
{FORMAT_EN_US_SHORT, 30000.0, "30K"},
147
{FORMAT_EN_US_SHORT, 300000.0, "300K"},
148
{FORMAT_EN_US_SHORT, 3000000.0, "3M"},
149
{FORMAT_EN_US_SHORT, 30000000.0, "30M"},
150
{FORMAT_EN_US_SHORT, 300000000.0, "300M"},
151
{FORMAT_EN_US_SHORT, 3000000000.0, "3B"},
152
{FORMAT_EN_US_SHORT, 30000000000.0, "30B"},
153
{FORMAT_EN_US_SHORT, 300000000000.0, "300B"},
154
{FORMAT_EN_US_SHORT, 3000000000000.0, "3T"},
155
{FORMAT_EN_US_SHORT, 30000000000000.0, "30T"},
156
{FORMAT_EN_US_SHORT, 300000000000000.0, "300T"},
157
{FORMAT_EN_US_SHORT, 3000000000000000.0, "3000T"},
158
// Negatives
159
{FORMAT_EN_US_SHORT, -3000.0, "-3K"},
160
{FORMAT_EN_US_SHORT, -30000.0, "-30K"},
161
{FORMAT_EN_US_SHORT, -300000.0, "-300K"},
162
{FORMAT_EN_US_SHORT, -3000000.0, "-3M"},
163
{FORMAT_EN_US_SHORT, -30000000.0, "-30M"},
164
{FORMAT_EN_US_SHORT, -300000000.0, "-300M"},
165
{FORMAT_EN_US_SHORT, -3000000000.0, "-3B"},
166
{FORMAT_EN_US_SHORT, -30000000000.0, "-30B"},
167
{FORMAT_EN_US_SHORT, -300000000000.0, "-300B"},
168
{FORMAT_EN_US_SHORT, -3000000000000.0, "-3T"},
169
{FORMAT_EN_US_SHORT, -30000000000000.0, "-30T"},
170
{FORMAT_EN_US_SHORT, -300000000000000.0, "-300T"},
171
{FORMAT_EN_US_SHORT, -3000000000000000.0, "-3000T"},
172
// BigInteger
173
{FORMAT_EN_US_SHORT, new BigInteger("12345678901234567890"),
174
"12345679T"},
175
{FORMAT_EN_US_SHORT, new BigInteger("-12345678901234567890"),
176
"-12345679T"},
177
//BigDecimal
178
{FORMAT_EN_US_SHORT, new BigDecimal("12345678901234567890.89"),
179
"12345679T"},
180
{FORMAT_EN_US_SHORT, new BigDecimal("-12345678901234567890.89"),
181
"-12345679T"},
182
{FORMAT_EN_US_SHORT, new BigDecimal("12345678901234567890123466767.89"),
183
"12345678901234568T"},
184
{FORMAT_EN_US_SHORT, new BigDecimal(
185
"12345678901234567890878732267863209.89"),
186
"12345678901234567890879T"},
187
// number as exponent
188
{FORMAT_EN_US_SHORT, 9.78313E+3, "10K"},
189
// Less than 1000 no suffix
190
{FORMAT_EN_LONG, 999, "999"},
191
// Round the value and then format
192
{FORMAT_EN_LONG, 999.99, "1 thousand"},
193
// 10 thousand
194
{FORMAT_EN_LONG, 99000, "99 thousand"},
195
// Long path
196
{FORMAT_EN_LONG, 330000, "330 thousand"},
197
// Double path
198
{FORMAT_EN_LONG, 3000.90, "3 thousand"},
199
// BigInteger path
200
{FORMAT_EN_LONG, new BigInteger("12345678901234567890"),
201
"12345679 trillion"},
202
//BigDecimal path
203
{FORMAT_EN_LONG, new BigDecimal("12345678901234567890.89"),
204
"12345679 trillion"},
205
// Less than 1000 no suffix
206
{FORMAT_HI_IN_LONG, -999, "-999"},
207
// Round the value with 0 fraction digits and format it
208
{FORMAT_HI_IN_LONG, -999.99, "-1 \u0939\u091C\u093C\u093E\u0930"},
209
// 10 thousand
210
{FORMAT_HI_IN_LONG, 99000, "99 \u0939\u091C\u093C\u093E\u0930"},
211
// Long path
212
{FORMAT_HI_IN_LONG, 330000, "3 \u0932\u093E\u0916"},
213
// Double path
214
{FORMAT_HI_IN_LONG, 3000.90, "3 \u0939\u091C\u093C\u093E\u0930"},
215
// BigInteger path
216
{FORMAT_HI_IN_LONG, new BigInteger("12345678901234567890"),
217
"123456789 \u0916\u0930\u092C"},
218
// BigDecimal path
219
{FORMAT_HI_IN_LONG, new BigDecimal("12345678901234567890.89"),
220
"123456789 \u0916\u0930\u092C"},
221
// 1000 does not have any suffix in "ja" locale
222
{FORMAT_JA_JP_SHORT, -999.99, "-1,000"},
223
// 0-9999 does not have any suffix
224
{FORMAT_JA_JP_SHORT, 9999, "9,999"},
225
// 99000/10000 => 9.9\u4E07 rounded to 10\u4E07
226
{FORMAT_JA_JP_SHORT, 99000, "10\u4E07"},
227
// Negative
228
{FORMAT_JA_JP_SHORT, -99000, "-10\u4E07"},
229
// Long path
230
{FORMAT_JA_JP_SHORT, 330000, "33\u4E07"},
231
// Double path
232
{FORMAT_JA_JP_SHORT, 3000.90, "3,001"},
233
// BigInteger path
234
{FORMAT_JA_JP_SHORT, new BigInteger("12345678901234567890"),
235
"12345679\u5146"},
236
// BigDecimal path
237
{FORMAT_JA_JP_SHORT, new BigDecimal("12345678901234567890.89"),
238
"12345679\u5146"},
239
// less than 1000 no suffix
240
{FORMAT_IT_SHORT, 499, "499"},
241
// Boundary number
242
{FORMAT_IT_SHORT, 1000, "1.000"},
243
// Long path
244
{FORMAT_IT_SHORT, 3000000L, "3\u00a0Mln"},
245
// Double path
246
{FORMAT_IT_SHORT, 3000000.0, "3\u00a0Mln"},
247
// BigInteger path
248
{FORMAT_IT_SHORT, new BigInteger("12345678901234567890"),
249
"12345679\u00a0Bln"},
250
// BigDecimal path
251
{FORMAT_IT_SHORT, new BigDecimal("12345678901234567890.89"),
252
"12345679\u00a0Bln"},
253
{FORMAT_CA_LONG, 999, "999"},
254
{FORMAT_CA_LONG, 999.99, "1 miler"},
255
{FORMAT_CA_LONG, 99000, "99 milers"},
256
{FORMAT_CA_LONG, 330000, "330 milers"},
257
{FORMAT_CA_LONG, 3000.90, "3 milers"},
258
{FORMAT_CA_LONG, 1000000, "1 mili\u00f3"},
259
{FORMAT_CA_LONG, new BigInteger("12345678901234567890"),
260
"12345679 bilions"},
261
{FORMAT_CA_LONG, new BigDecimal("12345678901234567890.89"),
262
"12345679 bilions"},
263
{FORMAT_AS_LONG, 5000.0, "\u09eb \u09b9\u09be\u099c\u09be\u09f0"},
264
{FORMAT_AS_LONG, 50000.0, "\u09eb\u09e6 \u09b9\u09be\u099c\u09be\u09f0"},
265
{FORMAT_AS_LONG, 500000.0, "\u09eb \u09b2\u09be\u0996"},
266
{FORMAT_AS_LONG, 5000000.0, "\u09eb \u09a8\u09bf\u09af\u09c1\u09a4"},
267
{FORMAT_AS_LONG, 50000000.0, "\u09eb\u09e6 \u09a8\u09bf\u09af\u09c1\u09a4"},
268
{FORMAT_AS_LONG, 500000000.0, "\u09eb\u09e6\u09e6 \u09a8\u09bf\u09af\u09c1\u09a4"},
269
{FORMAT_AS_LONG, 5000000000.0, "\u09eb \u09b6\u09a4 \u0995\u09cb\u099f\u09bf"},
270
{FORMAT_AS_LONG, 50000000000.0, "\u09eb\u09e6 \u09b6\u09a4 \u0995\u09cb\u099f\u09bf"},
271
{FORMAT_AS_LONG, 500000000000.0, "\u09eb\u09e6\u09e6 \u09b6\u09a4 \u0995\u09cb\u099f\u09bf"},
272
{FORMAT_AS_LONG, 5000000000000.0, "\u09eb \u09b6\u09a4 \u09aa\u09f0\u09be\u09f0\u09cd\u09a6\u09cd\u09a7"},
273
{FORMAT_AS_LONG, 50000000000000.0, "\u09eb\u09e6 \u09b6\u09a4 \u09aa\u09f0\u09be\u09f0\u09cd\u09a6\u09cd\u09a7"},
274
{FORMAT_AS_LONG, 500000000000000.0, "\u09eb\u09e6\u09e6 \u09b6\u09a4 \u09aa\u09f0\u09be\u09f0\u09cd\u09a6\u09cd\u09a7"},
275
{FORMAT_AS_LONG, 5000000000000000.0, "\u09eb\u09e6\u09e6\u09e6 \u09b6\u09a4 \u09aa\u09f0\u09be\u09f0\u09cd\u09a6\u09cd\u09a7"},
276
{FORMAT_AS_LONG, new BigInteger("12345678901234567890"),
277
"\u09e7\u09e8\u09e9\u09ea\u09eb\u09ec\u09ed\u09ef \u09b6\u09a4 \u09aa\u09f0\u09be\u09f0\u09cd\u09a6\u09cd\u09a7"},
278
{FORMAT_AS_LONG, new BigDecimal("12345678901234567890123466767.89"),
279
"\u09e7\u09e8\u09e9\u09ea\u09eb\u09ec\u09ed\u09ee\u09ef\u09e6\u09e7\u09e8\u09e9\u09ea\u09eb\u09ec\u09ee \u09b6\u09a4 \u09aa\u09f0\u09be\u09f0\u09cd\u09a6\u09cd\u09a7"},
280
{FORMAT_BRX_SHORT, 999, "999"},
281
{FORMAT_BRX_SHORT, 999.99, "1K"},
282
{FORMAT_BRX_SHORT, 99000, "99K"},
283
{FORMAT_BRX_SHORT, 330000, "330K"},
284
{FORMAT_BRX_SHORT, 3000.90, "3K"},
285
{FORMAT_BRX_SHORT, 1000000, "1M"},
286
{FORMAT_BRX_SHORT, new BigInteger("12345678901234567890"),
287
"12345679T"},
288
{FORMAT_BRX_SHORT, new BigDecimal("12345678901234567890.89"),
289
"12345679T"},
290
// Less than 1000 no suffix
291
{FORMAT_SW_LONG, 499, "499"},
292
// Boundary number
293
{FORMAT_SW_LONG, 1000, "elfu 1"},
294
// Long path
295
{FORMAT_SW_LONG, 3000000L, "milioni 3"},
296
// Long path, negative
297
{FORMAT_SW_LONG, -3000000L, "milioni -3"},
298
// Double path
299
{FORMAT_SW_LONG, 3000000.0, "milioni 3"},
300
// Double path, negative
301
{FORMAT_SW_LONG, -3000000.0, "milioni -3"},
302
// BigInteger path
303
{FORMAT_SW_LONG, new BigInteger("12345678901234567890"),
304
"trilioni 12345679"},
305
// BigDecimal path
306
{FORMAT_SW_LONG, new BigDecimal("12345678901234567890.89"),
307
"trilioni 12345679"},
308
// Positives
309
// No compact form
310
{FORMAT_SE_SHORT, 999, "999"},
311
// Long
312
{FORMAT_SE_SHORT, 8000000L, "8\u00a0mn"},
313
// Double
314
{FORMAT_SE_SHORT, 8000.98, "8\u00a0dt"},
315
// Big integer
316
{FORMAT_SE_SHORT, new BigInteger("12345678901234567890"), "12345679\u00a0bn"},
317
// Big decimal
318
{FORMAT_SE_SHORT, new BigDecimal("12345678901234567890.98"), "12345679\u00a0bn"},
319
// Negatives
320
// No compact form
321
{FORMAT_SE_SHORT, -999, "\u2212999"},
322
// Long
323
{FORMAT_SE_SHORT, -8000000L, "\u22128\u00a0mn"},
324
// Double
325
{FORMAT_SE_SHORT, -8000.98, "\u22128\u00a0dt"},
326
// BigInteger
327
{FORMAT_SE_SHORT, new BigInteger("-12345678901234567890"), "\u221212345679\u00a0bn"},
328
// BigDecimal
329
{FORMAT_SE_SHORT, new BigDecimal("-12345678901234567890.98"), "\u221212345679\u00a0bn"},
330
331
// Plurals
332
// DE: one:i = 1 and v = 0
333
{FORMAT_DE_LONG, 1_000_000, "1 Million"},
334
{FORMAT_DE_LONG, 2_000_000, "2 Millionen"},
335
// SL: one:v = 0 and i % 100 = 1
336
// two:v = 0 and i % 100 = 2
337
// few:v = 0 and i % 100 = 3..4 or v != 0
338
{FORMAT_SL_LONG, 1_000_000, "1 milijon"},
339
{FORMAT_SL_LONG, 2_000_000, "2 milijona"},
340
{FORMAT_SL_LONG, 3_000_000, "3 milijone"},
341
{FORMAT_SL_LONG, 5_000_000, "5 milijonov"},
342
};
343
}
344
345
@DataProvider(name = "parse")
346
Object[][] compactParseData() {
347
return new Object[][]{
348
// compact number format instance, string to parse, parsed number, return type
349
{FORMAT_DZ_LONG, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
350
+ "\u0F42 \u0F21", 1000L, Long.class},
351
{FORMAT_DZ_LONG, "-\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
352
+ "\u0F42 \u0F23", -3000L, Long.class},
353
{FORMAT_DZ_LONG, "\u0F51\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62"
354
+ "\u0F0B\u0F66\u0F0B\u0F61\u0F0B \u0F21"
355
+ "\u0F22\u0F23\u0F24\u0F25\u0F27", 1.23457E19, Double.class},
356
{FORMAT_DZ_LONG, "-\u0F51\u0F74\u0F44\u0F0B\u0F55\u0FB1\u0F74\u0F62"
357
+ "\u0F0B\u0F66\u0F0B\u0F61\u0F0B \u0F21"
358
+ "\u0F22\u0F23\u0F24\u0F25\u0F27", -1.23457E19, Double.class},
359
{FORMAT_EN_US_SHORT, "-0.0", -0.0, Double.class},
360
{FORMAT_EN_US_SHORT, "-0", -0.0, Double.class},
361
{FORMAT_EN_US_SHORT, "0", 0L, Long.class},
362
{FORMAT_EN_US_SHORT, "499", 499L, Long.class},
363
{FORMAT_EN_US_SHORT, "-499", -499L, Long.class},
364
{FORMAT_EN_US_SHORT, "499.89", 499.89, Double.class},
365
{FORMAT_EN_US_SHORT, "-499.89", -499.89, Double.class},
366
{FORMAT_EN_US_SHORT, "1K", 1000L, Long.class},
367
{FORMAT_EN_US_SHORT, "-1K", -1000L, Long.class},
368
{FORMAT_EN_US_SHORT, "3K", 3000L, Long.class},
369
{FORMAT_EN_US_SHORT, "17K", 17000L, Long.class},
370
{FORMAT_EN_US_SHORT, "-17K", -17000L, Long.class},
371
{FORMAT_EN_US_SHORT, "-3K", -3000L, Long.class},
372
{FORMAT_EN_US_SHORT, "12345678901234567890", 1.2345678901234567E19, Double.class},
373
{FORMAT_EN_US_SHORT, "12345679T", 1.2345679E19, Double.class},
374
{FORMAT_EN_US_SHORT, "-12345679T", -1.2345679E19, Double.class},
375
{FORMAT_EN_US_SHORT, "599.01K", 599010L, Long.class},
376
{FORMAT_EN_US_SHORT, "-599.01K", -599010L, Long.class},
377
{FORMAT_EN_US_SHORT, "599444444.90T", 5.994444449E20, Double.class},
378
{FORMAT_EN_US_SHORT, "-599444444.90T", -5.994444449E20, Double.class},
379
{FORMAT_EN_US_SHORT, "123456789012345.5678K", 123456789012345568L, Long.class},
380
{FORMAT_EN_US_SHORT, "17.000K", 17000L, Long.class},
381
{FORMAT_EN_US_SHORT, "123.56678K", 123566.78000, Double.class},
382
{FORMAT_EN_US_SHORT, "-123.56678K", -123566.78000, Double.class},
383
{FORMAT_EN_LONG, "999", 999L, Long.class},
384
{FORMAT_EN_LONG, "1 thousand", 1000L, Long.class},
385
{FORMAT_EN_LONG, "3 thousand", 3000L, Long.class},
386
{FORMAT_EN_LONG, "12345679 trillion", 1.2345679E19, Double.class},
387
{FORMAT_HI_IN_LONG, "999", 999L, Long.class},
388
{FORMAT_HI_IN_LONG, "-999", -999L, Long.class},
389
{FORMAT_HI_IN_LONG, "1 \u0939\u091C\u093C\u093E\u0930", 1000L, Long.class},
390
{FORMAT_HI_IN_LONG, "-1 \u0939\u091C\u093C\u093E\u0930", -1000L, Long.class},
391
{FORMAT_HI_IN_LONG, "3 \u0939\u091C\u093C\u093E\u0930", 3000L, Long.class},
392
{FORMAT_HI_IN_LONG, "12345679 \u0916\u0930\u092C", 1234567900000000000L, Long.class},
393
{FORMAT_HI_IN_LONG, "-12345679 \u0916\u0930\u092C", -1234567900000000000L, Long.class},
394
{FORMAT_JA_JP_SHORT, "-99", -99L, Long.class},
395
{FORMAT_JA_JP_SHORT, "1\u4E07", 10000L, Long.class},
396
{FORMAT_JA_JP_SHORT, "30\u4E07", 300000L, Long.class},
397
{FORMAT_JA_JP_SHORT, "-30\u4E07", -300000L, Long.class},
398
{FORMAT_JA_JP_SHORT, "12345679\u5146", 1.2345679E19, Double.class},
399
{FORMAT_JA_JP_SHORT, "-12345679\u5146", -1.2345679E19, Double.class},
400
{FORMAT_IT_SHORT, "-99", -99L, Long.class},
401
{FORMAT_IT_SHORT, "1\u00a0Mln", 1000000L, Long.class},
402
{FORMAT_IT_SHORT, "30\u00a0Mln", 30000000L, Long.class},
403
{FORMAT_IT_SHORT, "-30\u00a0Mln", -30000000L, Long.class},
404
{FORMAT_IT_SHORT, "12345679\u00a0Bln", 1.2345679E19, Double.class},
405
{FORMAT_IT_SHORT, "-12345679\u00a0Bln", -1.2345679E19, Double.class},
406
{FORMAT_SW_LONG, "-0.0", -0.0, Double.class},
407
{FORMAT_SW_LONG, "499", 499L, Long.class},
408
{FORMAT_SW_LONG, "elfu 1", 1000L, Long.class},
409
{FORMAT_SW_LONG, "elfu 3", 3000L, Long.class},
410
{FORMAT_SW_LONG, "elfu 17", 17000L, Long.class},
411
{FORMAT_SW_LONG, "elfu -3", -3000L, Long.class},
412
{FORMAT_SW_LONG, "499", 499L, Long.class},
413
{FORMAT_SW_LONG, "-499", -499L, Long.class},
414
{FORMAT_SW_LONG, "elfu 1", 1000L, Long.class},
415
{FORMAT_SW_LONG, "elfu 3", 3000L, Long.class},
416
{FORMAT_SW_LONG, "elfu -3", -3000L, Long.class},
417
{FORMAT_SW_LONG, "elfu 17", 17000L, Long.class},
418
{FORMAT_SW_LONG, "trilioni 12345679", 1.2345679E19, Double.class},
419
{FORMAT_SW_LONG, "trilioni -12345679", -1.2345679E19, Double.class},
420
{FORMAT_SW_LONG, "elfu 599.01", 599010L, Long.class},
421
{FORMAT_SW_LONG, "elfu -599.01", -599010L, Long.class},
422
{FORMAT_SE_SHORT, "999", 999L, Long.class},
423
{FORMAT_SE_SHORT, "8\u00a0mn", 8000000L, Long.class},
424
{FORMAT_SE_SHORT, "8\u00a0dt", 8000L, Long.class},
425
{FORMAT_SE_SHORT, "12345679\u00a0bn", 1.2345679E19, Double.class},
426
{FORMAT_SE_SHORT, "12345679,89\u00a0bn", 1.2345679890000001E19, Double.class},
427
{FORMAT_SE_SHORT, "\u2212999", -999L, Long.class},
428
{FORMAT_SE_SHORT, "\u22128\u00a0mn", -8000000L, Long.class},
429
{FORMAT_SE_SHORT, "\u22128\u00a0dt", -8000L, Long.class},
430
{FORMAT_SE_SHORT, "\u221212345679\u00a0bn", -1.2345679E19, Double.class},
431
{FORMAT_SE_SHORT, "\u221212345679,89\u00a0bn", -1.2345679890000001E19, Double.class},
432
433
// Plurals
434
// DE: one:i = 1 and v = 0
435
{FORMAT_DE_LONG, "1 Million", 1_000_000L, Long.class},
436
{FORMAT_DE_LONG, "2 Millionen", 2_000_000L, Long.class},
437
// SL: one:v = 0 and i % 100 = 1
438
// two:v = 0 and i % 100 = 2
439
// few:v = 0 and i % 100 = 3..4 or v != 0
440
{FORMAT_SL_LONG, "1 milijon", 1_000_000L, Long.class},
441
{FORMAT_SL_LONG, "2 milijona", 2_000_000L, Long.class},
442
{FORMAT_SL_LONG, "3 milijone", 3_000_000L, Long.class},
443
{FORMAT_SL_LONG, "5 milijonov", 5_000_000L, Long.class},
444
};
445
}
446
447
@DataProvider(name = "exceptionParse")
448
Object[][] exceptionParseData() {
449
return new Object[][]{
450
// compact number instance, string to parse, null (no o/p; must throws exception)
451
// no number
452
{FORMAT_DZ_LONG, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
453
+ "\u0F42", null},
454
// Invalid prefix
455
{FORMAT_DZ_LONG, "-\u0F66\u0F9F\u0F7C\u0F44,\u0F0B\u0F55\u0FB2"
456
+ "\u0F42 \u0F23", null},
457
// Invalid prefix for en_US
458
{FORMAT_EN_US_SHORT, "K12,347", null},
459
// Invalid prefix for ja_JP
460
{FORMAT_JA_JP_SHORT, "\u4E071", null},
461
// Localized minus sign should be used
462
{FORMAT_SE_SHORT, "-8\u00a0mn", null},};
463
}
464
465
@DataProvider(name = "invalidParse")
466
Object[][] invalidParseData() {
467
return new Object[][]{
468
// compact number instance, string to parse, parsed number
469
// Prefix and suffix do not match
470
{FORMAT_DZ_LONG, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
471
+ "\u0F42 \u0F21 KM", 1000L},
472
// Exponents are unparseable
473
{FORMAT_EN_US_SHORT, "-1.05E4K", -1.05},
474
// Default instance does not allow grouping
475
{FORMAT_EN_US_SHORT, "12,347", 12L},
476
// Take partial suffix "K" as 1000 for en_US_SHORT patterns
477
{FORMAT_EN_US_SHORT, "12KM", 12000L},
478
// Invalid suffix
479
{FORMAT_HI_IN_LONG, "-1 \u00a0\u0915.", -1L},
480
481
// invalid plurals
482
{FORMAT_DE_LONG, "2 Million", 2L},
483
{FORMAT_SL_LONG, "2 milijon", 2L},
484
{FORMAT_SL_LONG, "2 milijone", 2L},
485
{FORMAT_SL_LONG, "2 milijonv", 2L},
486
{FORMAT_SL_LONG, "3 milijon", 3L},
487
{FORMAT_SL_LONG, "3 milijona", 3L},
488
{FORMAT_SL_LONG, "3 milijonv", 3L},
489
{FORMAT_SL_LONG, "5 milijon", 5L},
490
{FORMAT_SL_LONG, "5 milijona", 5L},
491
{FORMAT_SL_LONG, "5 milijone", 5L},
492
};
493
}
494
495
@DataProvider(name = "fieldPosition")
496
Object[][] formatFieldPositionData() {
497
return new Object[][]{
498
//compact number instance, number to format, field, start position, end position, formatted string
499
{FORMAT_DZ_LONG, -3500, NumberFormat.Field.SIGN, 0, 1, "-\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2\u0F42 \u0F24"},
500
{FORMAT_DZ_LONG, 3500, NumberFormat.Field.INTEGER, 9, 10, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2\u0F42 \u0F24"},
501
{FORMAT_DZ_LONG, -3500, NumberFormat.Field.INTEGER, 10, 11, "-\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2\u0F42 \u0F24"},
502
{FORMAT_DZ_LONG, 999, NumberFormat.Field.INTEGER, 0, 3, "\u0F29\u0F29\u0F29"},
503
{FORMAT_DZ_LONG, -999, NumberFormat.Field.INTEGER, 1, 4, "-\u0F29\u0F29\u0F29"},
504
{FORMAT_DZ_LONG, 3500, NumberFormat.Field.PREFIX, 0, 9, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2\u0F42 \u0F24"},
505
{FORMAT_DZ_LONG, -3500, NumberFormat.Field.PREFIX, 0, 10, "-\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2\u0F42 \u0F24"},
506
{FORMAT_DZ_LONG, 999, NumberFormat.Field.PREFIX, 0, 0, "\u0F29\u0F29\u0F29"},
507
{FORMAT_EN_US_SHORT, -3500, NumberFormat.Field.SIGN, 0, 1, "-4K"},
508
{FORMAT_EN_US_SHORT, 3500, NumberFormat.Field.INTEGER, 0, 1, "4K"},
509
{FORMAT_EN_US_SHORT, 14900000067L, NumberFormat.Field.INTEGER, 0, 2, "15B"},
510
{FORMAT_EN_US_SHORT, -1000, NumberFormat.Field.PREFIX, 0, 1, "-1K"},
511
{FORMAT_EN_US_SHORT, 3500, NumberFormat.Field.SUFFIX, 1, 2, "4K"},
512
{FORMAT_EN_US_SHORT, 14900000067L, NumberFormat.Field.SUFFIX, 2, 3, "15B"},
513
{FORMAT_EN_LONG, 3500, NumberFormat.Field.INTEGER, 0, 1, "4 thousand"},
514
{FORMAT_EN_LONG, 14900000067L, NumberFormat.Field.INTEGER, 0, 2, "15 billion"},
515
{FORMAT_EN_LONG, 3500, NumberFormat.Field.SUFFIX, 1, 10, "4 thousand"},
516
{FORMAT_EN_LONG, 14900000067L, NumberFormat.Field.SUFFIX, 2, 10, "15 billion"},
517
{FORMAT_JA_JP_SHORT, 14900000067L, NumberFormat.Field.INTEGER, 0, 3, "149\u5104"},
518
{FORMAT_JA_JP_SHORT, -999.99, NumberFormat.Field.INTEGER, 1, 6, "-1,000"},
519
{FORMAT_JA_JP_SHORT, 14900000067L, NumberFormat.Field.SUFFIX, 3, 4, "149\u5104"},
520
{FORMAT_JA_JP_SHORT, -999.99, NumberFormat.Field.SUFFIX, 0, 0, "-1,000"},
521
{FORMAT_JA_JP_SHORT, -999.99, NumberFormat.Field.SIGN, 0, 1, "-1,000"},
522
{FORMAT_HI_IN_LONG, -14900000067L, NumberFormat.Field.SIGN, 0, 1,
523
"-15 \u0905\u0930\u092C"},
524
{FORMAT_HI_IN_LONG, 3500, NumberFormat.Field.INTEGER, 0, 1,
525
"4 \u0939\u091C\u093C\u093E\u0930"},
526
{FORMAT_HI_IN_LONG, 14900000067L, NumberFormat.Field.INTEGER, 0, 2,
527
"15 \u0905\u0930\u092C"},
528
{FORMAT_HI_IN_LONG, 3500, NumberFormat.Field.SUFFIX, 1, 7,
529
"4 \u0939\u091C\u093C\u093E\u0930"},
530
{FORMAT_HI_IN_LONG, 14900000067L, NumberFormat.Field.SUFFIX, 2, 6,
531
"15 \u0905\u0930\u092C"},
532
{FORMAT_SE_SHORT, 8000000L, NumberFormat.Field.SUFFIX, 1, 4, "8\u00a0mn"},
533
{FORMAT_SE_SHORT, 8000.98, NumberFormat.Field.SUFFIX, 1, 4, "8\u00a0dt"},
534
{FORMAT_SE_SHORT, new BigInteger("12345678901234567890"), NumberFormat.Field.SUFFIX, 8, 11, "12345679\u00a0bn"},
535
{FORMAT_SE_SHORT, new BigDecimal("12345678901234567890.98"), NumberFormat.Field.SUFFIX, 8, 11, "12345679\u00a0bn"},
536
{FORMAT_SE_SHORT, -8000000L, NumberFormat.Field.INTEGER, 1, 2, "\u22128\u00a0mn"},
537
{FORMAT_SE_SHORT, -8000.98, NumberFormat.Field.SIGN, 0, 1, "\u22128\u00a0dt"},
538
{FORMAT_SE_SHORT, new BigDecimal("-48982865901234567890.98"), NumberFormat.Field.INTEGER, 1, 9, "\u221248982866\u00a0bn"},};
539
}
540
541
@DataProvider(name = "varParsePosition")
542
Object[][] varParsePosition() {
543
return new Object[][]{
544
// compact number instance, parse string, parsed number,
545
// start position, end position, error index
546
{FORMAT_DZ_LONG, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
547
+ "\u0F42 \u0F21 KM", 1000L, 0, 10, -1},
548
// Invalid prefix returns null
549
{FORMAT_DZ_LONG, "Number is: -\u0F66\u0F9F\u0F7C\u0F44,\u0F0B\u0F55\u0FB2"
550
+ "\u0F42 \u0F23", null, 11, 11, 11},
551
// Returns null
552
{FORMAT_DZ_LONG, "\u0F66\u0F9F\u0F7C\u0F44\u0F0B\u0F55\u0FB2"
553
+ "\u0F42", null, 0, 0, 0},
554
{FORMAT_EN_US_SHORT, "Exponent: -1.05E4K", -1.05, 10, 15, -1},
555
// Default instance does not allow grouping
556
{FORMAT_EN_US_SHORT, "12,347", 12L, 0, 2, -1},
557
// Invalid suffix "KM" for en_US_SHORT patterns
558
{FORMAT_EN_US_SHORT, "12KM", 12000L, 0, 3, -1},
559
// Invalid suffix
560
{FORMAT_HI_IN_LONG, "-1 \u00a0\u0915.", -1L, 0, 2, -1},
561
{FORMAT_EN_LONG, "Number is: 12345679 trillion",
562
1.2345679E19, 11, 28, -1},
563
{FORMAT_EN_LONG, "Number is: -12345679 trillion",
564
-1.2345679E19, 11, 29, -1},
565
{FORMAT_EN_LONG, "parse 12 thousand and four", 12000L, 6, 17, -1},};
566
}
567
568
@Test
569
public void testInstanceCreation() {
570
Stream.of(NumberFormat.getAvailableLocales()).forEach(l -> NumberFormat
571
.getCompactNumberInstance(l, NumberFormat.Style.SHORT).format(10000));
572
Stream.of(NumberFormat.getAvailableLocales()).forEach(l -> NumberFormat
573
.getCompactNumberInstance(l, NumberFormat.Style.LONG).format(10000));
574
}
575
576
@Test(expectedExceptions = IllegalArgumentException.class)
577
public void testFormatWithNullParam() {
578
FORMAT_EN_US_SHORT.format(null);
579
}
580
581
@Test(dataProvider = "format")
582
public void testFormat(NumberFormat cnf, Object number,
583
String expected) {
584
CompactFormatAndParseHelper.testFormat(cnf, number, expected);
585
}
586
587
@Test(dataProvider = "parse")
588
public void testParse(NumberFormat cnf, String parseString,
589
Number expected, Class<? extends Number> returnType) throws ParseException {
590
CompactFormatAndParseHelper.testParse(cnf, parseString, expected, null, returnType);
591
}
592
593
@Test(dataProvider = "parse")
594
public void testParsePosition(NumberFormat cnf, String parseString,
595
Number expected, Class<? extends Number> returnType) throws ParseException {
596
ParsePosition pos = new ParsePosition(0);
597
CompactFormatAndParseHelper.testParse(cnf, parseString, expected, pos, returnType);
598
assertEquals(pos.getIndex(), parseString.length());
599
assertEquals(pos.getErrorIndex(), -1);
600
}
601
602
@Test(dataProvider = "varParsePosition")
603
public void testVarParsePosition(NumberFormat cnf, String parseString,
604
Number expected, int startPosition, int indexPosition,
605
int errPosition) throws ParseException {
606
ParsePosition pos = new ParsePosition(startPosition);
607
CompactFormatAndParseHelper.testParse(cnf, parseString, expected, pos, null);
608
assertEquals(pos.getIndex(), indexPosition);
609
assertEquals(pos.getErrorIndex(), errPosition);
610
}
611
612
@Test(dataProvider = "exceptionParse", expectedExceptions = ParseException.class)
613
public void throwsParseException(NumberFormat cnf, String parseString,
614
Number expected) throws ParseException {
615
CompactFormatAndParseHelper.testParse(cnf, parseString, expected, null, null);
616
}
617
618
@Test(dataProvider = "invalidParse")
619
public void testInvalidParse(NumberFormat cnf, String parseString,
620
Number expected) throws ParseException {
621
CompactFormatAndParseHelper.testParse(cnf, parseString, expected, null, null);
622
}
623
624
@Test(dataProvider = "fieldPosition")
625
public void testFormatWithFieldPosition(NumberFormat nf,
626
Object number, Format.Field field, int posStartExpected,
627
int posEndExpected, String expected) {
628
FieldPosition pos = new FieldPosition(field);
629
StringBuffer buf = new StringBuffer();
630
StringBuffer result = nf.format(number, buf, pos);
631
assertEquals(result.toString(), expected, "Incorrect formatting of the number '"
632
+ number + "'");
633
assertEquals(pos.getBeginIndex(), posStartExpected, "Incorrect start position"
634
+ " while formatting the number '" + number + "', for the field " + field);
635
assertEquals(pos.getEndIndex(), posEndExpected, "Incorrect end position"
636
+ " while formatting the number '" + number + "', for the field " + field);
637
}
638
639
}
640
641