1 /* GENERATED SOURCE. DO NOT MODIFY. */ 2 // © 2016 and later: Unicode, Inc. and others. 3 // License & terms of use: http://www.unicode.org/copyright.html#License 4 /* 5 ******************************************************************************* 6 * Copyright (C) 2001-2012, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 11 /** 12 * Port From: ICU4C v1.8.1 : format : NumberFormatRegressionTest 13 * Source File: $ICU4CRoot/source/test/intltest/numrgts.cpp 14 **/ 15 16 package ohos.global.icu.dev.test.format; 17 18 import java.io.ByteArrayInputStream; 19 import java.io.IOException; 20 import java.io.ObjectInputStream; 21 import java.text.ParseException; 22 import java.text.ParsePosition; 23 import java.util.Date; 24 import java.util.Locale; 25 26 import org.junit.Test; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 import ohos.global.icu.dev.test.TestFmwk; 31 import ohos.global.icu.text.DateFormat; 32 import ohos.global.icu.text.DecimalFormat; 33 import ohos.global.icu.text.DecimalFormatSymbols; 34 import ohos.global.icu.text.NumberFormat; 35 import ohos.global.icu.util.Calendar; 36 import ohos.global.icu.util.ULocale; 37 38 39 /** 40 * Performs regression test for MessageFormat 41 **/ 42 43 @RunWith(JUnit4.class) 44 public class NumberFormatRegressionTest extends TestFmwk { 45 /** 46 * alphaWorks upgrade 47 */ 48 @Test Test4161100()49 public void Test4161100() { 50 NumberFormat nf = NumberFormat.getInstance(Locale.US); 51 nf.setMinimumFractionDigits(1); 52 nf.setMaximumFractionDigits(1); 53 double a = -0.09; 54 String s = nf.format(a); 55 logln(a + " x " + 56 ((DecimalFormat) nf).toPattern() + " = " + s); 57 if (!s.equals("-0.1")) { 58 errln("FAIL"); 59 } 60 } 61 62 /** 63 * DateFormat should call setIntegerParseOnly(TRUE) on adopted 64 * NumberFormat objects. 65 */ 66 @Test TestJ691()67 public void TestJ691() { 68 69 Locale loc = new Locale("fr", "CH"); 70 71 // set up the input date string & expected output 72 String udt = "11.10.2000"; 73 String exp = "11.10.00"; 74 75 // create a Calendar for this locale 76 Calendar cal = Calendar.getInstance(loc); 77 78 // create a NumberFormat for this locale 79 NumberFormat nf = NumberFormat.getInstance(loc); 80 81 // *** Here's the key: We don't want to have to do THIS: 82 //nf.setParseIntegerOnly(true); 83 // or this (with changes to fr_CH per cldrbug:9370): 84 //nf.setGroupingUsed(false); 85 // so they are done in DateFormat.setNumberFormat 86 87 // create the DateFormat 88 DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, loc); 89 90 df.setCalendar(cal); 91 df.setNumberFormat(nf); 92 93 // set parsing to lenient & parse 94 Date ulocdat = new Date(); 95 df.setLenient(true); 96 try { 97 ulocdat = df.parse(udt); 98 } catch (java.text.ParseException pe) { 99 errln(pe.getMessage()); 100 } 101 // format back to a string 102 String outString = df.format(ulocdat); 103 104 if (!outString.equals(exp)) { 105 errln("FAIL: " + udt + " => " + outString); 106 } 107 } 108 109 /** 110 * Test getIntegerInstance(); 111 */ 112 @Test Test4408066()113 public void Test4408066() { 114 115 NumberFormat nf1 = NumberFormat.getIntegerInstance(); 116 NumberFormat nf2 = NumberFormat.getIntegerInstance(Locale.CHINA); 117 118 //test isParseIntegerOnly 119 if (!nf1.isParseIntegerOnly() || !nf2.isParseIntegerOnly()) { 120 errln("Failed : Integer Number Format Instance should set setParseIntegerOnly(true)"); 121 } 122 123 //Test format 124 { 125 double[] data = { 126 -3.75, -2.5, -1.5, 127 -1.25, 0, 1.0, 128 1.25, 1.5, 2.5, 129 3.75, 10.0, 255.5 130 }; 131 String[] expected = { 132 "-4", "-2", "-2", 133 "-1", "0", "1", 134 "1", "2", "2", 135 "4", "10", "256" 136 }; 137 138 for (int i = 0; i < data.length; ++i) { 139 String result = nf1.format(data[i]); 140 if (!result.equals(expected[i])) { 141 errln("Failed => Source: " + Double.toString(data[i]) 142 + ";Formatted : " + result 143 + ";but expectted: " + expected[i]); 144 } 145 } 146 } 147 //Test parse, Parsing should stop at "." 148 { 149 String data[] = { 150 "-3.75", "-2.5", "-1.5", 151 "-1.25", "0", "1.0", 152 "1.25", "1.5", "2.5", 153 "3.75", "10.0", "255.5" 154 }; 155 long[] expected = { 156 -3, -2, -1, 157 -1, 0, 1, 158 1, 1, 2, 159 3, 10, 255 160 }; 161 162 for (int i = 0; i < data.length; ++i) { 163 Number n = null; 164 try { 165 n = nf1.parse(data[i]); 166 } catch (ParseException e) { 167 errln("Failed: " + e.getMessage()); 168 } 169 if (!(n instanceof Long) || (n instanceof Integer)) { 170 errln("Failed: Integer Number Format should parse string to Long/Integer"); 171 } 172 if (n.longValue() != expected[i]) { 173 errln("Failed=> Source: " + data[i] 174 + ";result : " + n.toString() 175 + ";expected :" + Long.toString(expected[i])); 176 } 177 } 178 } 179 } 180 181 //Test New serialized DecimalFormat(2.0) read old serialized forms of DecimalFormat(1.3.1.1) 182 @Test TestSerialization()183 public void TestSerialization() throws IOException{ 184 byte[][] contents = NumberFormatSerialTestData.getContent(); 185 double data = 1234.56; 186 String[] expected = { 187 "1,234.56", "$1,234.56", "1.23456E3", "1,234.56"}; 188 for (int i = 0; i < 4; ++i) { 189 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(contents[i])); 190 try { 191 NumberFormat format = (NumberFormat) ois.readObject(); 192 String result = format.format(data); 193 assertEquals("Deserialization new version should read old version", expected[i], result); 194 } catch (Exception e) { 195 e.printStackTrace(); 196 warnln("FAIL: " + e); 197 } 198 } 199 } 200 201 /* 202 * Test case for JB#5509, strict parsing issue 203 */ 204 @Test TestJB5509()205 public void TestJB5509() { 206 String[] data = { 207 "1,2", 208 "1.2", 209 "1,2.5", 210 "1,23.5", 211 "1,234.5", 212 "1,234", 213 "1,234,567", 214 "1,234,567.8", 215 "1,234,5", 216 "1,234,5.6", 217 "1,234,56.7" 218 }; 219 boolean[] expected = { // false for expected parse failure 220 false, 221 true, 222 false, 223 false, 224 true, 225 true, 226 true, 227 true, 228 false, 229 false, 230 false, 231 false 232 }; 233 234 DecimalFormat df = new DecimalFormat("#,##0.###", new DecimalFormatSymbols(new ULocale("en_US"))); 235 df.setParseStrict(true); 236 for (int i = 0; i < data.length; i++) { 237 try { 238 df.parse(data[i]); 239 if (!expected[i]) { 240 errln("Failed: ParseException must be thrown for string " + data[i]); 241 } 242 } catch (ParseException pe) { 243 if (expected[i]) { 244 errln("Failed: ParseException must not be thrown for string " + data[i]); 245 } 246 } 247 } 248 } 249 250 /* 251 * Test case for ticket#5698 - parsing extremely large/small values 252 */ 253 @Test TestT5698()254 public void TestT5698() { 255 final String[] data = { 256 "12345679E66666666666666666", 257 "-12345679E66666666666666666", 258 ".1E2147483648", // exponent > max int 259 ".1E2147483647", // exponent == max int 260 ".1E-2147483648", // exponent == min int 261 ".1E-2147483649", // exponent < min int 262 "1.23E350", // value > max double 263 "1.23E300", // value < max double 264 "-1.23E350", // value < min double 265 "-1.23E300", // value > min double 266 "4.9E-324", // value = smallest non-zero double 267 "1.0E-325", // 0 < value < smallest non-zero positive double0 268 "-1.0E-325", // 0 > value > largest non-zero negative double 269 }; 270 final double[] expected = { 271 Double.POSITIVE_INFINITY, 272 Double.NEGATIVE_INFINITY, 273 Double.POSITIVE_INFINITY, 274 Double.POSITIVE_INFINITY, 275 0.0, 276 0.0, 277 Double.POSITIVE_INFINITY, 278 1.23e300d, 279 Double.NEGATIVE_INFINITY, 280 -1.23e300d, 281 4.9e-324d, 282 0.0, 283 -0.0, 284 }; 285 286 NumberFormat nfmt = NumberFormat.getInstance(); 287 288 for (int i = 0; i < data.length; i++) { 289 try { 290 Number n = nfmt.parse(data[i]); 291 if (expected[i] != n.doubleValue()) { 292 errln("Failed: Parsed result for " + data[i] + ": " 293 + n.doubleValue() + " / expected: " + expected[i]); 294 } 295 } catch (ParseException pe) { 296 errln("Failed: ParseException is thrown for " + data[i]); 297 } 298 } 299 } 300 @Test TestSurrogatesParsing()301 public void TestSurrogatesParsing() { // Test parsing of numbers that use digits from the supplemental planes. 302 final String[] data = { 303 "1\ud801\udca2,3\ud801\udca45.67", // 304 "\ud801\udca1\ud801\udca2,\ud801\udca3\ud801\udca4\ud801\udca5.\ud801\udca6\ud801\udca7\ud801\udca8", // 305 "\ud835\udfd2.\ud835\udfd7E-\ud835\udfd1", 306 "\ud835\udfd3.8E-0\ud835\udfd0" 307 }; 308 final double[] expected = { 309 12345.67, 310 12345.678, 311 0.0049, 312 0.058 313 }; 314 315 NumberFormat nfmt = NumberFormat.getInstance(); 316 317 for (int i = 0; i < data.length; i++) { 318 try { 319 Number n = nfmt.parse(data[i]); 320 if (expected[i] != n.doubleValue()) { 321 errln("Failed: Parsed result for " + data[i] + ": " 322 + n.doubleValue() + " / expected: " + expected[i]); 323 } 324 } catch (ParseException pe) { 325 errln("Failed: ParseException is thrown for " + data[i]); 326 } 327 } 328 } 329 checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber)330 void checkNBSPPatternRtNum(String testcase, NumberFormat nf, double myNumber) { 331 String myString = nf.format(myNumber); 332 333 double aNumber; 334 try { 335 aNumber = nf.parse(myString).doubleValue(); 336 } catch (ParseException e) { 337 // TODO Auto-generated catch block 338 errln("FAIL: " + testcase +" - failed to parse. " + e.toString()); 339 return; 340 } 341 if(Math.abs(aNumber-myNumber)>.001) { 342 errln("FAIL: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n"); 343 } else { 344 logln("PASS: "+testcase+": formatted "+myNumber+", parsed into "+aNumber+"\n"); 345 } 346 } 347 checkNBSPPatternRT(String testcase, NumberFormat nf)348 void checkNBSPPatternRT(String testcase, NumberFormat nf) { 349 checkNBSPPatternRtNum(testcase, nf, 12345.); 350 checkNBSPPatternRtNum(testcase, nf, -12345.); 351 } 352 353 @Test TestNBSPInPattern()354 public void TestNBSPInPattern() { 355 NumberFormat nf = null; 356 String testcase; 357 358 359 testcase="ar_AE UNUM_CURRENCY"; 360 nf = NumberFormat.getCurrencyInstance(new ULocale("ar_AE")); 361 checkNBSPPatternRT(testcase, nf); 362 // if we don't have CLDR 1.6 data, bring out the problem anyways 363 364 String SPECIAL_PATTERN = "\u00A4\u00A4'\u062f.\u0625.\u200f\u00a0'###0.00"; 365 testcase = "ar_AE special pattern: " + SPECIAL_PATTERN; 366 nf = new DecimalFormat(); 367 ((DecimalFormat)nf).applyPattern(SPECIAL_PATTERN); 368 checkNBSPPatternRT(testcase, nf); 369 370 } 371 372 /* 373 * Test case for #9293 374 * Parsing currency in strict mode 375 */ 376 @Test TestT9293()377 public void TestT9293() { 378 NumberFormat fmt = NumberFormat.getCurrencyInstance(); 379 fmt.setParseStrict(true); 380 381 final int val = 123456; 382 String txt = fmt.format(123456); 383 384 ParsePosition pos = new ParsePosition(0); 385 Number num = fmt.parse(txt, pos); 386 387 if (pos.getErrorIndex() >= 0) { 388 errln("FAIL: Parsing " + txt + " - error index: " + pos.getErrorIndex()); 389 } else if (val != num.intValue()) { 390 errln("FAIL: Parsed result: " + num + " - expected: " + val); 391 } 392 } 393 394 @Test TestAffixesNoCurrency()395 public void TestAffixesNoCurrency() { 396 ULocale locale = new ULocale("en"); 397 DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(locale, NumberFormat.PLURALCURRENCYSTYLE); 398 assertEquals( 399 "Positive suffix should contain the localized display name for currency XXX", 400 " (unknown currency)", 401 nf.getPositiveSuffix()); 402 } 403 404 @Test TestGroupingEnabledDisabledGetters()405 public void TestGroupingEnabledDisabledGetters() { 406 // See ticket #13442 comment 14 407 DecimalFormatSymbols EN = DecimalFormatSymbols.getInstance(ULocale.ENGLISH); 408 { 409 DecimalFormat df = new DecimalFormat("0", EN); 410 assertEquals("pat 0: ", 0, df.getGroupingSize()); 411 assertEquals("pat 0: ", false, df.isGroupingUsed()); 412 df.setGroupingUsed(false); 413 assertEquals("pat 0 then disabled: ", 0, df.getGroupingSize()); 414 assertEquals("pat 0 then disabled: ", "1111", df.format(1111)); 415 df.setGroupingUsed(true); 416 assertEquals("pat 0 then enabled: ", 0, df.getGroupingSize()); 417 assertEquals("pat 0 then enabled: ", "1111", df.format(1111)); 418 } 419 { 420 DecimalFormat df = new DecimalFormat("#,##0", EN); 421 assertEquals("pat #,##0: ", 3, df.getGroupingSize()); 422 assertEquals("pat #,##0: ", true, df.isGroupingUsed()); 423 df.setGroupingUsed(false); 424 assertEquals("pat #,##0 then disabled: ", 3, df.getGroupingSize()); 425 assertEquals("pat #,##0 then disabled: ", "1111", df.format(1111)); 426 df.setGroupingUsed(true); 427 assertEquals("pat #,##0 then enabled: ", 3, df.getGroupingSize()); 428 assertEquals("pat #,##0 then enabled: ", "1,111", df.format(1111)); 429 } 430 } 431 } 432