LEFT | RIGHT |
(no file at all) | |
1 // © 2016 and later: Unicode, Inc. and others. | 1 // © 2016 and later: Unicode, Inc. and others. |
2 // License & terms of use: http://www.unicode.org/copyright.html#License | 2 // License & terms of use: http://www.unicode.org/copyright.html#License |
3 /* | 3 /* |
4 ******************************************************************************* | 4 ******************************************************************************* |
5 * Copyright (C) 2001-2012, International Business Machines Corporation and * | 5 * Copyright (C) 2001-2012, International Business Machines Corporation and * |
6 * others. All Rights Reserved. * | 6 * others. All Rights Reserved. * |
7 ******************************************************************************* | 7 ******************************************************************************* |
8 */ | 8 */ |
9 | 9 |
10 /** | 10 /** |
11 * Port From: ICU4C v1.8.1 : format : NumberFormatRegressionTest | 11 * Port From: ICU4C v1.8.1 : format : NumberFormatRegressionTest |
12 * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp | 12 * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp |
13 **/ | 13 **/ |
14 | 14 |
15 package com.ibm.icu.dev.test.format; | 15 package com.ibm.icu.dev.test.format; |
16 | 16 |
17 import java.io.ByteArrayInputStream; | 17 import java.io.ByteArrayInputStream; |
18 import java.io.IOException; | 18 import java.io.IOException; |
19 import java.io.ObjectInputStream; | 19 import java.io.ObjectInputStream; |
20 import java.text.ParseException; | 20 import java.text.ParseException; |
21 import java.text.ParsePosition; | 21 import java.text.ParsePosition; |
22 import java.util.Date; | 22 import java.util.Date; |
23 import java.util.Locale; | 23 import java.util.Locale; |
24 | 24 |
25 import org.junit.Test; | 25 import org.junit.Test; |
26 | 26 |
27 import com.ibm.icu.text.DateFormat; | 27 import com.ibm.icu.text.DateFormat; |
28 import com.ibm.icu.text.DecimalFormat; | 28 import com.ibm.icu.text.DecimalFormat; |
29 import com.ibm.icu.text.DecimalFormatSymbols; | 29 import com.ibm.icu.text.DecimalFormatSymbols; |
30 import com.ibm.icu.text.NumberFormat; | 30 import com.ibm.icu.text.NumberFormat; |
31 import com.ibm.icu.util.Calendar; | 31 import com.ibm.icu.util.Calendar; |
32 import com.ibm.icu.util.ULocale; | 32 import com.ibm.icu.util.ULocale; |
33 | 33 |
34 /** | 34 /** |
35 * Performs regression test for MessageFormat | 35 * Performs regression test for MessageFormat |
36 **/ | 36 **/ |
37 public class NumberFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk { | 37 public class NumberFormatRegressionTest extends com.ibm.icu.dev.test.TestFmwk { |
38 /** | 38 /** |
39 * alphaWorks upgrade | 39 * alphaWorks upgrade |
40 */ | 40 */ |
41 @Test | 41 @Test |
42 public void Test4161100() { | 42 public void Test4161100() { |
43 NumberFormat nf = NumberFormat.getInstance(Locale.US); | 43 NumberFormat nf = NumberFormat.getInstance(Locale.US); |
44 nf.setMinimumFractionDigits(1); | 44 nf.setMinimumFractionDigits(1); |
45 nf.setMaximumFractionDigits(1); | 45 nf.setMaximumFractionDigits(1); |
46 double a = -0.09; | 46 double a = -0.09; |
47 String s = nf.format(a); | 47 String s = nf.format(a); |
48 logln(a + " x " + | 48 logln(a + " x " + |
49 ((DecimalFormat) nf).toPattern() + " = " + s); | 49 ((DecimalFormat) nf).toPattern() + " = " + s); |
50 if (!s.equals("-0.1")) { | 50 if (!s.equals("-0.1")) { |
51 errln("FAIL"); | 51 errln("FAIL"); |
52 } | 52 } |
53 } | 53 } |
54 | 54 |
55 /** | 55 /** |
56 * DateFormat should call setIntegerParseOnly(TRUE) on adopted | 56 * DateFormat should call setIntegerParseOnly(TRUE) on adopted |
57 * NumberFormat objects. | 57 * NumberFormat objects. |
58 */ | 58 */ |
59 @Test | 59 @Test |
60 public void TestJ691() { | 60 public void TestJ691() { |
61 | 61 |
62 Locale loc = new Locale("fr", "CH"); | 62 Locale loc = new Locale("fr", "CH"); |
63 | 63 |
64 // set up the input date string & expected output | 64 // set up the input date string & expected output |
65 String udt = "11.10.2000"; | 65 String udt = "11.10.2000"; |
66 String exp = "11.10.00"; | 66 String exp = "11.10.00"; |
67 | 67 |
68 // create a Calendar for this locale | 68 // create a Calendar for this locale |
69 Calendar cal = Calendar.getInstance(loc); | 69 Calendar cal = Calendar.getInstance(loc); |
70 | 70 |
71 // create a NumberFormat for this locale | 71 // create a NumberFormat for this locale |
72 NumberFormat nf = NumberFormat.getInstance(loc); | 72 NumberFormat nf = NumberFormat.getInstance(loc); |
73 | 73 |
74 // *** Here's the key: We don't want to have to do THIS: | 74 // *** Here's the key: We don't want to have to do THIS: |
75 //nf.setParseIntegerOnly(true); | 75 //nf.setParseIntegerOnly(true); |
76 | 76 |
77 // create the DateFormat | 77 // create the DateFormat |
78 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc); | 78 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc); |
79 | 79 |
80 df.setCalendar(cal); | 80 df.setCalendar(cal); |
81 df.setNumberFormat(nf); | 81 df.setNumberFormat(nf); |
82 | 82 |
83 // set parsing to lenient & parse | 83 // set parsing to lenient & parse |
84 Date ulocdat = new Date(); | 84 Date ulocdat = new Date(); |
85 df.setLenient(true); | 85 df.setLenient(true); |
86 try { | 86 try { |
87 ulocdat = df.parse(udt); | 87 ulocdat = df.parse(udt); |
88 } catch (java.text.ParseException pe) { | 88 } catch (java.text.ParseException pe) { |
89 errln(pe.getMessage()); | 89 errln(pe.getMessage()); |
90 } | 90 } |
91 // format back to a string | 91 // format back to a string |
92 String outString = df.format(ulocdat); | 92 String outString = df.format(ulocdat); |
93 | 93 |
94 if (!outString.equals(exp)) { | 94 if (!outString.equals(exp)) { |
95 errln("FAIL: " + udt + " => " + outString); | 95 errln("FAIL: " + udt + " => " + outString); |
96 } | 96 } |
97 } | 97 } |
98 | 98 |
99 /** | 99 /** |
100 * Test getIntegerInstance(); | 100 * Test getIntegerInstance(); |
101 */ | 101 */ |
102 @Test | 102 @Test |
103 public void Test4408066() { | 103 public void Test4408066() { |
104 | 104 |
105 NumberFormat nf1 = NumberFormat.getIntegerInstance(); | 105 NumberFormat nf1 = NumberFormat.getIntegerInstance(); |
106 NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA); | 106 NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA); |
107 | 107 |
108 //test isParseIntegerOnly | 108 //test isParseIntegerOnly |
109 if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) { | 109 if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) { |
110 errln("Failed : Integer Number Format Instance should set setParseIn
tegerOnly(true)"); | 110 errln("Failed : Integer Number Format Instance should set setParseIn
tegerOnly(true)"); |
111 } | 111 } |
112 | 112 |
113 //Test format | 113 //Test format |
114 { | 114 { |
115 double[] data = { | 115 double[] data = { |
116 -3.75, -2.5, -1.5, | 116 -3.75, -2.5, -1.5, |
117 -1.25, 0, 1.0, | 117 -1.25, 0, 1.0, |
118 1.25, 1.5, 2.5, | 118 1.25, 1.5, 2.5, |
119 3.75, 10.0, 255.5 | 119 3.75, 10.0, 255.5 |
120 }; | 120 }; |
121 String[] expected = { | 121 String[] expected = { |
122 "-4", "-2", "-2", | 122 "-4", "-2", "-2", |
123 "-1", "0", "1", | 123 "-1", "0", "1", |
124 "1", "2", "2", | 124 "1", "2", "2", |
125 "4", "10", "256" | 125 "4", "10", "256" |
126 }; | 126 }; |
127 | 127 |
128 for (int i = 0; i < data.length; ++i) { | 128 for (int i = 0; i < data.length; ++i) { |
129 String result = nf1.format(data[i]); | 129 String result = nf1.format(data[i]); |
130 if (!result.equals(expected[i])) { | 130 if (!result.equals(expected[i])) { |
131 errln("Failed => Source: " + Double.toString(data[i]) | 131 errln("Failed => Source: " + Double.toString(data[i]) |
132 + ";Formatted : " + result | 132 + ";Formatted : " + result |
133 + ";but expectted: " + expected[i]); | 133 + ";but expectted: " + expected[i]); |
134 } | 134 } |
135 } | 135 } |
136 } | 136 } |
137 //Test parse, Parsing should stop at "." | 137 //Test parse, Parsing should stop at "." |
138 { | 138 { |
139 String data[] = { | 139 String data[] = { |
140 "-3.75", "-2.5", "-1.5", | 140 "-3.75", "-2.5", "-1.5", |
141 "-1.25", "0", "1.0", | 141 "-1.25", "0", "1.0", |
142 "1.25", "1.5", "2.5", | 142 "1.25", "1.5", "2.5", |
143 "3.75", "10.0", "255.5" | 143 "3.75", "10.0", "255.5" |
144 }; | 144 }; |
145 long[] expected = { | 145 long[] expected = { |
146 -3, -2, -1, | 146 -3, -2, -1, |
147 -1, 0, 1, | 147 -1, 0, 1, |
148 1, 1, 2, | 148 1, 1, 2, |
149 3, 10, 255 | 149 3, 10, 255 |
150 }; | 150 }; |
151 | 151 |
152 for (int i = 0; i < data.length; ++i) { | 152 for (int i = 0; i < data.length; ++i) { |
153 Number n = null; | 153 Number n = null; |
154 try { | 154 try { |
155 n = nf1.parse(data[i]); | 155 n = nf1.parse(data[i]); |
156 } catch (ParseException e) { | 156 } catch (ParseException e) { |
157 errln("Failed: " + e.getMessage()); | 157 errln("Failed: " + e.getMessage()); |
158 } | 158 } |
159 if (!(n instanceof Long) || (n instanceof Integer)) { | 159 if (!(n instanceof Long) || (n instanceof Integer)) { |
160 errln("Failed: Integer Number Format should parse string to
Long/Integer"); | 160 errln("Failed: Integer Number Format should parse string to
Long/Integer"); |
161 } | 161 } |
162 if (n.longValue() != expected[i]) { | 162 if (n.longValue() != expected[i]) { |
163 errln("Failed=> Source: " + data[i] | 163 errln("Failed=> Source: " + data[i] |
164 + ";result : " + n.toString() | 164 + ";result : " + n.toString() |
165 + ";expected :" + Long.toString(expected[i])); | 165 + ";expected :" + Long.toString(expected[i])); |
166 } | 166 } |
167 } | 167 } |
168 } | 168 } |
169 } | 169 } |
170 | 170 |
171 //Test New serialized DecimalFormat(2.0) read old serialized forms of Decima
lFormat(1.3.1.1) | 171 //Test New serialized DecimalFormat(2.0) read old serialized forms of Decima
lFormat(1.3.1.1) |
172 @Test | 172 @Test |
173 public void TestSerialization() throws IOException{ | 173 public void TestSerialization() throws IOException{ |
174 byte[][] contents = NumberFormatSerialTestData.getContent(); | 174 byte[][] contents = NumberFormatSerialTestData.getContent(); |
175 double data = 1234.56; | 175 double data = 1234.56; |
176 String[] expected = { | 176 String[] expected = { |
177 "1,234.56", "$1,234.56", "123,456%", "1.23456E3"}; | 177 "1,234.56", "$1,234.56", "1.23456E3", "1,234.56"}; |
178 for (int i = 0; i < 4; ++i) { | 178 for (int i = 0; i < 4; ++i) { |
179 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStre
am(contents[i])); | 179 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStre
am(contents[i])); |
180 try { | 180 try { |
181 NumberFormat format = (NumberFormat) ois.readObject(); | 181 NumberFormat format = (NumberFormat) ois.readObject(); |
182 String result = format.format(data); | 182 String result = format.format(data); |
183 if (result.equals(expected[i])) { | 183 assertEquals("Deserialization new version should read old versio
n", expected[i], result); |
184 logln("OK: Deserialized bogus NumberFormat(new version read
old version)"); | |
185 } else { | |
186 errln("FAIL: the test data formats are not euqal"); | |
187 } | |
188 } catch (Exception e) { | 184 } catch (Exception e) { |
189 warnln("FAIL: " + e.getMessage()); | 185 warnln("FAIL: " + e.getMessage()); |
190 } | 186 } |
191 } | 187 } |
192 } | 188 } |
193 | 189 |
194 /* | 190 /* |
195 * Test case for JB#5509, strict parsing issue | 191 * Test case for JB#5509, strict parsing issue |
196 */ | 192 */ |
197 @Test | 193 @Test |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 0.0, | 271 0.0, |
276 -0.0, | 272 -0.0, |
277 }; | 273 }; |
278 | 274 |
279 NumberFormat nfmt = NumberFormat.getInstance(); | 275 NumberFormat nfmt = NumberFormat.getInstance(); |
280 | 276 |
281 for (int i = 0; i < data.length; i++) { | 277 for (int i = 0; i < data.length; i++) { |
282 try { | 278 try { |
283 Number n = nfmt.parse(data[i]); | 279 Number n = nfmt.parse(data[i]); |
284 if (expected[i] != n.doubleValue()) { | 280 if (expected[i] != n.doubleValue()) { |
285 errln("Failed: Parsed result for " + data[i] + ": " | 281 errln("Failed: Parsed result for " + data[i] + ": " |
286 + n.doubleValue() + " / expected: " + expected[i]); | 282 + n.doubleValue() + " / expected: " + expected[i]); |
287 } | 283 } |
288 } catch (ParseException pe) { | 284 } catch (ParseException pe) { |
289 errln("Failed: ParseException is thrown for " + data[i]); | 285 errln("Failed: ParseException is thrown for " + data[i]); |
290 } | 286 } |
291 } | 287 } |
292 } | 288 } |
293 @Test | 289 @Test |
294 public void TestSurrogatesParsing() { // Test parsing of numbers that use di
gits from the supplemental planes. | 290 public void TestSurrogatesParsing() { // Test parsing of numbers that use di
gits from the supplemental planes. |
295 final String[] data = { | 291 final String[] data = { |
296 "1\ud801\udca2,3\ud801\udca45.67", // | 292 "1\ud801\udca2,3\ud801\udca45.67", // |
297 "\ud801\udca1\ud801\udca2,\ud801\udca3\ud801\udca4\ud801\udca5.\
ud801\udca6\ud801\udca7\ud801\udca8", // | 293 "\ud801\udca1\ud801\udca2,\ud801\udca3\ud801\udca4\ud801\udca5.\
ud801\udca6\ud801\udca7\ud801\udca8", // |
298 "\ud835\udfd2.\ud835\udfd7E-\ud835\udfd1", | 294 "\ud835\udfd2.\ud835\udfd7E-\ud835\udfd1", |
299 "\ud835\udfd3.8E-0\ud835\udfd0" | 295 "\ud835\udfd3.8E-0\ud835\udfd0" |
300 }; | 296 }; |
301 final double[] expected = { | 297 final double[] expected = { |
302 12345.67, | 298 12345.67, |
303 12345.678, | 299 12345.678, |
304 0.0049, | 300 0.0049, |
305 0.058 | 301 0.058 |
306 }; | 302 }; |
307 | 303 |
308 NumberFormat nfmt = NumberFormat.getInstance(); | 304 NumberFormat nfmt = NumberFormat.getInstance(); |
309 | 305 |
310 for (int i = 0; i < data.length; i++) { | 306 for (int i = 0; i < data.length; i++) { |
311 try { | 307 try { |
312 Number n = nfmt.parse(data[i]); | 308 Number n = nfmt.parse(data[i]); |
313 if (expected[i] != n.doubleValue()) { | 309 if (expected[i] != n.doubleValue()) { |
314 errln("Failed: Parsed result for " + data[i] + ": " | 310 errln("Failed: Parsed result for " + data[i] + ": " |
315 + n.doubleValue() + " / expected: " + expected[i]); | 311 + n.doubleValue() + " / expected: " + expected[i]); |
316 } | 312 } |
317 } catch (ParseException pe) { | 313 } catch (ParseException pe) { |
318 errln("Failed: ParseException is thrown for " + data[i]); | 314 errln("Failed: ParseException is thrown for " + data[i]); |
319 } | 315 } |
320 } | 316 } |
321 } | 317 } |
322 | 318 |
323 void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber
) { | 319 void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber
) { |
324 String myString = nf.format(myNumber); | 320 String myString = nf.format(myNumber); |
325 | 321 |
326 double aNumber; | 322 double aNumber; |
327 try { | 323 try { |
328 aNumber = nf.parse(myString).doubleValue(); | 324 aNumber = nf.parse(myString).doubleValue(); |
329 } catch (ParseException e) { | 325 } catch (ParseException e) { |
330 // TODO Auto-generated catch block | 326 // TODO Auto-generated catch block |
331 errln("FAIL: " + testcase +" - failed to parse. " + e.toString()
); | 327 errln("FAIL: " + testcase +" - failed to parse. " + e.toString()
); |
332 return; | 328 return; |
333 } | 329 } |
334 if(Math.abs(aNumber-myNumber)>.001) { | 330 if(Math.abs(aNumber-myNumber)>.001) { |
335 errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNu
mber+"\n"); | 331 errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNu
mber+"\n"); |
336 } else { | 332 } else { |
337 logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNu
mber+"\n"); | 333 logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNu
mber+"\n"); |
338 } | 334 } |
339 } | 335 } |
340 | 336 |
341 void checkNBSPPatternRT(String testcase, NumberFormat nf) { | 337 void checkNBSPPatternRT(String testcase, NumberFormat nf) { |
342 checkNBSPPatternRtNum(testcase, nf, 12345.); | 338 checkNBSPPatternRtNum(testcase, nf, 12345.); |
343 checkNBSPPatternRtNum(testcase, nf, -12345.); | 339 checkNBSPPatternRtNum(testcase, nf, -12345.); |
344 } | 340 } |
345 | 341 |
346 @Test | 342 @Test |
347 public void TestNBSPInPattern() { | 343 public void TestNBSPInPattern() { |
348 NumberFormat nf = null; | 344 NumberFormat nf = null; |
349 String testcase; | 345 String testcase; |
350 | 346 |
351 | 347 |
352 testcase="ar_AE UNUM_CURRENCY"; | 348 testcase="ar_AE UNUM_CURRENCY"; |
353 nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE")); | 349 nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE")); |
354 checkNBSPPatternRT(testcase, nf); | 350 checkNBSPPatternRT(testcase, nf); |
355 // if we don't have CLDR 1.6 data, bring out the problem anyways | 351 // if we don't have CLDR 1.6 data, bring out the problem anyways |
356 | 352 |
357 String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00"; | 353 String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00"; |
358 testcase = "ar_AE special pattern: " + SPECIAL_PATTERN; | 354 testcase = "ar_AE special pattern: " + SPECIAL_PATTERN; |
359 nf = new DecimalFormat(); | 355 nf = new DecimalFormat(); |
360 ((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN); | 356 ((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN); |
361 checkNBSPPatternRT(testcase, nf); | 357 checkNBSPPatternRT(testcase, nf); |
362 | 358 |
363 } | 359 } |
364 | 360 |
365 /* | 361 /* |
366 * Test case for #9293 | 362 * Test case for #9293 |
367 * Parsing currency in strict mode | 363 * Parsing currency in strict mode |
368 */ | 364 */ |
369 @Test | 365 @Test |
370 public void TestT9293() { | 366 public void TestT9293() { |
371 NumberFormat fmt = NumberFormat.getCurrencyInstance(); | 367 NumberFormat fmt = NumberFormat.getCurrencyInstance(); |
372 fmt.setParseStrict(true); | 368 fmt.setParseStrict(true); |
373 | 369 |
374 final int val = 123456; | 370 final int val = 123456; |
375 String txt = fmt.format(123456); | 371 String txt = fmt.format(123456); |
376 | 372 |
377 ParsePosition pos = new ParsePosition(0); | 373 ParsePosition pos = new ParsePosition(0); |
378 Number num = fmt.parse(txt, pos); | 374 Number num = fmt.parse(txt, pos); |
379 | 375 |
380 if (pos.getErrorIndex() >= 0) { | 376 if (pos.getErrorIndex() >= 0) { |
381 errln("FAIL: Parsing " + txt + " - error index: " + pos.getErrorInde
x()); | 377 errln("FAIL: Parsing " + txt + " - error index: " + pos.getErrorInde
x()); |
382 } else if (val != num.intValue()) { | 378 } else if (val != num.intValue()) { |
383 errln("FAIL: Parsed result: " + num + " - expected: " + val); | 379 errln("FAIL: Parsed result: " + num + " - expected: " + val); |
384 } | 380 } |
385 } | 381 } |
| 382 |
| 383 @Test |
| 384 public void TestAffixesNoCurrency() { |
| 385 ULocale locale = new ULocale("en"); |
| 386 DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(locale, Numb
erFormat.PLURALCURRENCYSTYLE); |
| 387 assertEquals( |
| 388 "Positive suffix should contain the single currency sign when no cur
rency is set", |
| 389 " \u00A4", |
| 390 nf.getPositiveSuffix()); |
| 391 } |
386 } | 392 } |
LEFT | RIGHT |