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) 1996-2009, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 package android.icu.dev.test.format; 11 12 import java.text.ParseException; 13 import java.util.Locale; 14 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 import org.junit.runners.JUnit4; 18 19 import android.icu.dev.test.TestFmwk; 20 import android.icu.impl.Utility; 21 import android.icu.text.DecimalFormat; 22 import android.icu.text.DecimalFormatSymbols; 23 import android.icu.text.NumberFormat; 24 import android.icu.testsharding.MainTestShard; 25 26 /** 27 * @test 28 * General test of Big NumberFormat 29 */ 30 @MainTestShard 31 @RunWith(JUnit4.class) 32 public class BigNumberFormatTest extends TestFmwk { 33 34 static final int ILLEGAL = -1; 35 36 @Test 37 public void TestExponent() { 38 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); 39 DecimalFormat fmt1 = new DecimalFormat("0.###E0", US); 40 DecimalFormat fmt2 = new DecimalFormat("0.###E+0", US); 41 Number n = new Long(1234); 42 expect(fmt1, n, "1.234E3"); 43 expect(fmt2, n, "1.234E+3"); 44 expect(fmt1, "1.234E3", n); 45 expect(fmt1, "1.234E+3", n); // Either format should parse "E+3" 46 expect(fmt2, "1.234E+3", n); 47 } 48 49 /** 50 * Test the functioning of the secondary grouping value. 51 */ 52 @Test 53 public void TestSecondaryGrouping() { 54 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); 55 DecimalFormat f = new DecimalFormat("#,##,###", US); 56 expect(f, new Long(123456789), "12,34,56,789"); 57 expectPat(f, "#,##,##0"); 58 f.applyPattern("#,###"); 59 f.setSecondaryGroupingSize(4); 60 expect(f, new Long(123456789), "12,3456,789"); 61 expectPat(f, "#,####,##0"); 62 63 // On Sun JDK 1.2-1.3, the hi_IN locale uses '0' for a zero digit, 64 // but on IBM JDK 1.2-1.3, the locale uses U+0966. 65 f = (DecimalFormat) NumberFormat.getInstance(new Locale("hi", "IN")); 66 String str = transmute("1,87,65,43,210", 67 f.getDecimalFormatSymbols().getZeroDigit()); 68 expect(f, new Long(1876543210), str); 69 } 70 71 private void expectPad(DecimalFormat fmt, String pat, int pos) { 72 expectPad(fmt, pat, pos, 0, (char)0); 73 } 74 75 private void expectPad(DecimalFormat fmt, String pat, 76 int pos, int width, char pad) { 77 int apos = 0, awidth = 0; 78 char apad = 0; 79 try { 80 fmt.applyPattern(pat); 81 apos = fmt.getPadPosition(); 82 awidth = fmt.getFormatWidth(); 83 apad = fmt.getPadCharacter(); 84 } catch (IllegalArgumentException e) { 85 apos = -1; 86 awidth = width; 87 apad = pad; 88 } 89 if (apos == pos && awidth == width && apad == pad) { 90 logln("Ok \"" + pat + "\" pos=" + apos + 91 ((pos == -1) ? "" : " width=" + awidth + " pad=" + apad)); 92 } else { 93 logln("FAIL \"" + pat + "\" pos=" + apos + 94 " width=" + awidth + " pad=" + apad + 95 ", expected " + pos + " " + width + " " + pad); 96 } 97 } 98 99 /** 100 */ 101 @Test 102 public void TestPatterns() { 103 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); 104 DecimalFormat fmt = new DecimalFormat("#", US); 105 106 expectPad(fmt, "*^#", DecimalFormat.PAD_BEFORE_PREFIX, 1, '^'); 107 expectPad(fmt, "$*^#", DecimalFormat.PAD_AFTER_PREFIX, 2, '^'); 108 expectPad(fmt, "#*^", DecimalFormat.PAD_BEFORE_SUFFIX, 1, '^'); 109 expectPad(fmt, "#$*^", DecimalFormat.PAD_AFTER_SUFFIX, 2, '^'); 110 expectPad(fmt, "$*^$#", ILLEGAL); 111 expectPad(fmt, "#$*^$", ILLEGAL); 112 expectPad(fmt, "'pre'#,##0*x'post'", DecimalFormat.PAD_BEFORE_SUFFIX, 113 12, 'x'); 114 expectPad(fmt, "''#0*x", DecimalFormat.PAD_BEFORE_SUFFIX, 115 3, 'x'); 116 expectPad(fmt, "'I''ll'*a###.##", DecimalFormat.PAD_AFTER_PREFIX, 117 10, 'a'); 118 119 fmt.applyPattern("AA#,##0.00ZZ"); 120 fmt.setPadCharacter('^'); 121 122 fmt.setFormatWidth(10); 123 124 fmt.setPadPosition(DecimalFormat.PAD_BEFORE_PREFIX); 125 expectPat(fmt, "*^AA#,##0.00ZZ"); 126 127 fmt.setPadPosition(DecimalFormat.PAD_BEFORE_SUFFIX); 128 expectPat(fmt, "AA#,##0.00*^ZZ"); 129 130 fmt.setPadPosition(DecimalFormat.PAD_AFTER_SUFFIX); 131 expectPat(fmt, "AA#,##0.00ZZ*^"); 132 133 // 12 3456789012 134 String exp = "AA*^#,##0.00ZZ"; 135 fmt.setFormatWidth(12); 136 fmt.setPadPosition(DecimalFormat.PAD_AFTER_PREFIX); 137 expectPat(fmt, exp); 138 139 fmt.setFormatWidth(13); 140 // 12 34567890123 141 expectPat(fmt, "AA*^##,##0.00ZZ"); 142 143 fmt.setFormatWidth(14); 144 // 12 345678901234 145 expectPat(fmt, "AA*^###,##0.00ZZ"); 146 147 fmt.setFormatWidth(15); 148 // 12 3456789012345 149 expectPat(fmt, "AA*^####,##0.00ZZ"); // This is the interesting case 150 151 fmt.setFormatWidth(16); 152 // 12 34567890123456 153 expectPat(fmt, "AA*^#####,##0.00ZZ"); 154 } 155 156 private void expectPat(DecimalFormat fmt, String exp) { 157 String pat = fmt.toPattern(); 158 if (pat.equals(exp)) { 159 logln("Ok \"" + pat + '"'); 160 } else { 161 errln("FAIL \"" + pat + "\", expected \"" + exp + '"'); 162 } 163 } 164 165 /** 166 * Test the handling of the AlphaWorks BigDecimal 167 */ 168 @Test 169 public void TestAlphaBigDecimal() { 170 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); 171 /*For ICU compatibility [Richard/GCL]*/ 172 expect(NumberFormat.getScientificInstance(Locale.US), 173 new Number[] { new android.icu.math.BigDecimal("12345.678901"), 174 }, 175 "1.2345678901E4"); 176 expect(new DecimalFormat("##0.####E0", US), 177 new Number[] { new android.icu.math.BigDecimal("12345.4999"), 178 new android.icu.math.BigDecimal("12344.5001"), 179 }, 180 "12.345E3"); 181 expect(new DecimalFormat("##0.####E0", US), 182 new Number[] { new android.icu.math.BigDecimal("12345.5000"), 183 new android.icu.math.BigDecimal("12346.5000"), 184 }, 185 "12.346E3"); 186 } 187 188 /** 189 */ 190 @Test 191 public void TestScientific() { 192 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); 193 /*For ICU compatibility [Richard/GCL]*/ 194 expect(NumberFormat.getScientificInstance(Locale.US), 195 new Number[] { new Double(12345.678901), 196 new java.math.BigDecimal("12345.678901"), 197 }, 198 "1.2345678901E4"); 199 expect(new DecimalFormat("##0.###E0", US), 200 new Double(12345), 201 "12.34E3"); 202 expect(new DecimalFormat("##0.###E0", US), 203 new Double(12345.00001), 204 "12.35E3"); 205 expect(new DecimalFormat("##0.####E0", US), 206 new Number[] { new Integer(12345), 207 new Long(12345), 208 new java.math.BigDecimal("12345.4999"), 209 new java.math.BigDecimal("12344.5001"), 210 }, 211 "12.345E3"); 212 expect(new DecimalFormat("##0.####E0", US), 213 new Number[] { new java.math.BigDecimal("12345.5000"), 214 new java.math.BigDecimal("12346.5000"), 215 }, 216 "12.346E3"); 217 /*For ICU compatibility [Richard/GCL]*/ 218 expect(NumberFormat.getScientificInstance(Locale.FRANCE), 219 new Double(12345.678901), 220 "1,2345678901E4"); 221 expect(new DecimalFormat("##0.####E0", US), 222 new Double(789.12345e-9), 223 "789.12E-9"); 224 expect(new DecimalFormat("##0.####E0", US), 225 new Double(780.e-9), 226 "780E-9"); 227 expect(new DecimalFormat(".###E0", US), 228 new Double(45678), 229 ".457E5"); 230 expect(new DecimalFormat(".###E0", US), 231 new Long(0), 232 ".0E0"); 233 expect(new DecimalFormat[] { new DecimalFormat("#E0", US), 234 new DecimalFormat("##E0", US), 235 new DecimalFormat("####E0", US), 236 new DecimalFormat("0E0", US), 237 new DecimalFormat("00E0", US), 238 new DecimalFormat("000E0", US), 239 }, 240 new Long(45678000), 241 new String[] { "4.5678E7", 242 "45.678E6", 243 "4567.8E4", 244 "5E7", 245 "46E6", 246 "457E5", 247 } 248 ); 249 expect(new DecimalFormat("###E0", US), 250 new Object[] { new Double(0.0000123), "12.3E-6", 251 new Double(0.000123), "123E-6", 252 new java.math.BigDecimal("0.00123"), "1.23E-3", // Cafe VM messes up Double(0.00123) 253 new Double(0.0123), "12.3E-3", 254 new Double(0.123), "123E-3", 255 new Double(1.23), "1.23E0", 256 new Double(12.3), "12.3E0", 257 new Double(123), "123E0", 258 new Double(1230), "1.23E3", 259 }); 260 expect(new DecimalFormat("0.#E+00", US), 261 new Object[] { new Double(0.00012), "1.2E-04", 262 new Long(12000), "1.2E+04", 263 }); 264 } 265 266 /** 267 */ 268 @Test 269 public void TestPad() { 270 DecimalFormatSymbols US = new DecimalFormatSymbols(Locale.US); 271 expect(new DecimalFormat("*^##.##", US), 272 new Object[] { new Long(0), "^^^^0", 273 new Double(-1.3), "^-1.3", 274 } 275 ); 276 expect(new DecimalFormat("##0.0####E0*_ 'g-m/s^2'", US), 277 new Object[] { new Long(0), "0.0E0______ g-m/s^2", 278 new Double(1.0/3), "333.333E-3_ g-m/s^2", 279 } 280 ); 281 expect(new DecimalFormat("##0.0####*_ 'g-m/s^2'", US), 282 new Object[] { new Long(0), "0.0______ g-m/s^2", 283 new Double(1.0/3), "0.33333__ g-m/s^2", 284 } 285 ); 286 expect(new DecimalFormat("*x#,###,###,##0.00;*x(#,###,###,##0.00)", US), 287 new Object[] { 288 new Long(-100), "xxxxxxxx(100.00)", 289 new Long(-1000), "xxxxxx(1,000.00)", 290 new Long(-1000000), "xx(1,000,000.00)", 291 new Long(-1000000000), "(1,000,000,000.00)", 292 }); 293 } 294 295 private void expect(NumberFormat fmt, Object[] data) { 296 for (int i=0; i<data.length; i+=2) { 297 expect(fmt, (Number) data[i], (String) data[i+1]); 298 } 299 } 300 301 private void expect(Object fmto, Object numo, Object expo) { 302 NumberFormat fmt = null, fmts[] = null; 303 Number num = null, nums[] = null; 304 String exp = null, exps[] = null; 305 if (fmto instanceof NumberFormat[]) { 306 fmts = (NumberFormat[]) fmto; 307 } else { 308 fmt = (NumberFormat) fmto; 309 } 310 if (numo instanceof Number[]) { 311 nums = (Number[]) numo; 312 } else { 313 num = (Number) numo; 314 } 315 if (expo instanceof String[]) { 316 exps = (String[]) expo; 317 } else { 318 exp = (String) expo; 319 } 320 int n = 1; 321 if (fmts != null) { 322 n = Math.max(n, fmts.length); 323 } 324 if (nums != null) { 325 n = Math.max(n, nums.length); 326 } 327 if (exps != null) { 328 n = Math.max(n, exps.length); 329 } 330 for (int i=0; i<n; ++i) { 331 expect(fmts == null ? fmt : fmts[i], 332 nums == null ? num : nums[i], 333 exps == null ? exp : exps[i]); 334 } 335 } 336 337 private static String showNumber(Number n) { 338 String cls = n.getClass().getName(); 339 if (!(n instanceof android.icu.math.BigDecimal 340 || n instanceof java.math.BigDecimal)) { 341 int i = cls.lastIndexOf('.'); 342 cls = cls.substring(i+1); 343 } 344 return n.toString() + " (" + cls + ')'; 345 } 346 347 private void expect(NumberFormat fmt, Number n, String exp) { 348 String saw = fmt.format(n); 349 String pat = ((DecimalFormat) fmt).toPattern(); 350 if (saw.equals(exp)) { 351 logln("Ok " + showNumber(n) + " x " + 352 pat + " = " + 353 Utility.escape(saw)); 354 } else { 355 errln("FAIL " + showNumber(n) + " x " + 356 pat + " = \"" + 357 Utility.escape(saw) + ", expected " + Utility.escape(exp)); 358 } 359 } 360 361 private void expect(NumberFormat fmt, String str, Number exp) { 362 Number saw = null; 363 try { 364 saw = fmt.parse(str); 365 } catch (ParseException e) { 366 saw = null; 367 } 368 String pat = ((DecimalFormat) fmt).toPattern(); 369 if (saw.equals(exp)) { 370 logln("Ok \"" + str + "\" x " + 371 pat + " = " + 372 showNumber(saw)); 373 } else { 374 errln("FAIL \"" + str + "\" x " + 375 pat + " = " + 376 showNumber(saw) + ", expected " + showNumber(exp)); 377 } 378 } 379 380 /** 381 * Given a string composed of [0-9] and other chars, convert the 382 * [0-9] chars to be offsets 0..9 from 'zero'. 383 */ 384 private static String transmute(String str, char zero) { 385 StringBuffer buf = new StringBuffer(); 386 for (int i=0; i<str.length(); ++i) { 387 char c = str.charAt(i); 388 if (c >= '0' && c <= '9') { 389 c = (char) (c - '0' + zero); 390 } 391 buf.append(c); 392 } 393 return buf.toString(); 394 } 395 396 @Test 397 public void Test4161100() { 398 NumberFormat f = NumberFormat.getInstance(); 399 f.setMinimumFractionDigits(1); 400 f.setMaximumFractionDigits(1); 401 double a = -0.09; 402 String s = f.format(a); 403 logln(a + " x " + 404 ((DecimalFormat) f).toPattern() + " = " + 405 s); 406 if (!s.equals("-0.1")) { 407 errln("FAIL"); 408 } 409 } 410 411 @Test 412 public void TestBigDecimalJ28() { 413 String[] DATA = { 414 "1", "1E0", 415 "-1", "-1E0", 416 "0", "0E0", 417 "12e34", "1.2E35", 418 "-12.3e-45", "-1.23E-44", 419 "0.73e-7", "7.3E-8", 420 }; 421 NumberFormat fmt = NumberFormat.getScientificInstance(Locale.US); 422 logln("Pattern: " + ((DecimalFormat)fmt).toPattern()); 423 for (int i=0; i<DATA.length; i+=2) { 424 String input = DATA[i]; 425 String exp = DATA[i+1]; 426 android.icu.math.BigDecimal bd = new android.icu.math.BigDecimal(input); 427 String output = fmt.format(bd); 428 if (output.equals(exp)) { 429 logln("input=" + input + " num=" + bd + " output=" + output); 430 } else { 431 errln("FAIL: input=" + input + " num=" + bd + " output=" + output + 432 " expected=" + exp); 433 } 434 } 435 } 436 @Test 437 public void TestBigDecimalRounding() { 438 // jb 3657 439 java.text.DecimalFormat jdkFormat=new java.text.DecimalFormat("###,###,###,##0"); 440 android.icu.text.DecimalFormat icuFormat=new android.icu.text.DecimalFormat("###,###,###,##0"); 441 String[] values = { 442 "-1.74", "-1.24", "-0.74", "-0.24", "0.24", "0.74", "1.24", "1.74" 443 }; 444 for (int i = 0; i < values.length; ++i) { 445 String val = values[i]; 446 java.math.BigDecimal bd = new java.math.BigDecimal(val); 447 String jdk = jdkFormat.format(bd); 448 String icu = icuFormat.format(bd); 449 logln("Format of BigDecimal " + val + " by JDK is " + jdk); 450 logln("Format of BigDecimal " + val + " by ICU is " + icu); 451 if (!jdk.equals(icu)) { 452 errln("BigDecimal jdk: " + jdk + " != icu: " + icu); 453 } 454 455 double d = bd.doubleValue(); 456 jdk = jdkFormat.format(d); 457 icu = icuFormat.format(d); 458 logln("Format of double " + val + " by JDK is " + jdk); 459 logln("Format of double " + val + " by ICU is " + icu); 460 if (!jdk.equals(icu)) { 461 errln("double jdk: " + jdk + " != icu: " + icu); 462 } 463 } 464 } 465 } 466