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-2011, International Business Machines Corporation and * 7 * others. All Rights Reserved. * 8 ******************************************************************************* 9 */ 10 11 /** 12 * Port From: ICU4C v1.8.1 : format : IntlTestNumberFormat 13 * Source File: $ICU4CRoot/source/test/intltest/tsnmfmt.cpp 14 **/ 15 16 package ohos.global.icu.dev.test.format; 17 18 import java.util.Locale; 19 import java.util.Random; 20 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.junit.runners.JUnit4; 24 25 import ohos.global.icu.dev.test.TestFmwk; 26 import ohos.global.icu.text.DecimalFormat; 27 import ohos.global.icu.text.NumberFormat; 28 import ohos.global.icu.util.ULocale; 29 30 31 /** 32 * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of 33 * NumberFormat. 34 */ 35 36 @RunWith(JUnit4.class) 37 public class IntlTestNumberFormat extends TestFmwk { 38 39 public NumberFormat fNumberFormat; 40 41 /** 42 * Internal use 43 */ _testLocale(Locale locale)44 private void _testLocale(Locale locale) { 45 String localeName = locale + " (" + locale.getDisplayName() + ")"; 46 47 logln("Number test " + localeName); 48 fNumberFormat = NumberFormat.getInstance(locale); 49 _testFormat(); 50 51 logln("Currency test " + localeName); 52 fNumberFormat = NumberFormat.getCurrencyInstance(locale); 53 _testFormat(); 54 55 logln("Percent test " + localeName); 56 fNumberFormat = NumberFormat.getPercentInstance(locale); 57 _testFormat(); 58 59 if (locale.toString().compareTo("en_US_POSIX") != 0 ) { 60 logln("Scientific test " + localeName); 61 fNumberFormat = NumberFormat.getScientificInstance(locale); 62 _testFormat(); 63 } 64 } 65 66 /** 67 * call _testFormat for currency, percent and plain number instances 68 */ 69 @Test TestLocale()70 public void TestLocale() { 71 Locale locale = Locale.getDefault(); 72 String localeName = locale + " (" + locale.getDisplayName() + ")"; 73 74 logln("Number test " + localeName); 75 fNumberFormat = NumberFormat.getInstance(locale); 76 _testFormat(); 77 78 logln("Currency test " + localeName); 79 fNumberFormat = NumberFormat.getCurrencyInstance(locale); 80 _testFormat(); 81 82 logln("Percent test " + localeName); 83 fNumberFormat = NumberFormat.getPercentInstance(locale); 84 _testFormat(); 85 } 86 87 /** 88 * call tryIt with many variations, called by testLocale 89 */ _testFormat()90 private void _testFormat() { 91 92 if (fNumberFormat == null){ 93 errln("**** FAIL: Null format returned by createXxxInstance."); 94 return; 95 } 96 DecimalFormat s = (DecimalFormat)fNumberFormat; 97 logln("pattern :" + s.toPattern()); 98 99 tryIt(-2.02147304840132e-68); 100 tryIt(3.88057859588817e-68); 101 tryIt(-2.64651110485945e+65); 102 tryIt(9.29526819488338e+64); 103 104 tryIt(-2.02147304840132e-100); 105 tryIt(3.88057859588817e-096); 106 tryIt(-2.64651110485945e+306); 107 tryIt(9.29526819488338e+250); 108 109 tryIt(-9.18228054496402e+64); 110 tryIt(-9.69413034454191e+64); 111 112 tryIt(-9.18228054496402e+255); 113 tryIt(-9.69413034454191e+273); 114 115 116 tryIt(1.234e-200); 117 tryIt(-2.3e-168); 118 119 tryIt(Double.NaN); 120 tryIt(Double.POSITIVE_INFINITY); 121 tryIt(Double.NEGATIVE_INFINITY); 122 123 tryIt(251887531); 124 tryIt(5e-20 / 9); 125 tryIt(5e20 / 9); 126 tryIt(1.234e-50); 127 tryIt(9.99999999999996); 128 tryIt(9.999999999999996); 129 130 tryIt(5.06e-27); 131 132 tryIt(Integer.MIN_VALUE); 133 tryIt(Integer.MAX_VALUE); 134 tryIt((double)Integer.MIN_VALUE); 135 tryIt((double)Integer.MAX_VALUE); 136 tryIt(Integer.MIN_VALUE - 1.0); 137 tryIt(Integer.MAX_VALUE + 1.0); 138 139 tryIt(5.0 / 9.0 * 1e-20); 140 tryIt(4.0 / 9.0 * 1e-20); 141 tryIt(5.0 / 9.0 * 1e+20); 142 tryIt(4.0 / 9.0 * 1e+20); 143 144 tryIt(2147483647.); 145 tryIt(0); 146 tryIt(0.0); 147 tryIt(1); 148 tryIt(10); 149 tryIt(100); 150 tryIt(-1); 151 tryIt(-10); 152 tryIt(-100); 153 tryIt(-1913860352); 154 155 Random random = createRandom(); // use test framework's random seed 156 for (int j = 0; j < 10; j++) { 157 double d = random.nextDouble()*2e10 - 1e10; 158 tryIt(d); 159 160 } 161 } 162 163 /** 164 * Perform tests using aNumber and fNumberFormat, called in many variations 165 */ tryIt(double aNumber)166 public void tryIt(double aNumber) { 167 final int DEPTH = 10; 168 double[] number = new double[DEPTH]; 169 String[] string = new String[DEPTH]; 170 int numberMatch = 0; 171 int stringMatch = 0; 172 boolean dump = false; 173 int i; 174 175 String message = "Locale: " + fNumberFormat.getLocale(ULocale.VALID_LOCALE); 176 177 for (i = 0; i < DEPTH; i++) { 178 if (i == 0) { 179 number[i] = aNumber; 180 } else { 181 try { 182 number[i - 1] = fNumberFormat.parse(string[i - 1]).doubleValue(); 183 } catch(java.text.ParseException pe) { 184 errln("**** FAIL: Parse of " + string[i-1] + " failed: " + message); 185 dump = true; 186 break; 187 } 188 } 189 190 string[i] = fNumberFormat.format(number[i]); 191 if (i > 0) 192 { 193 if (numberMatch == 0 && number[i] == number[i-1]) 194 numberMatch = i; 195 else if (numberMatch > 0 && number[i] != number[i-1]) 196 { 197 errln("**** FAIL: Numeric mismatch after match: " + message); 198 dump = true; 199 break; 200 } 201 if (stringMatch == 0 && string[i] == string[i-1]) 202 stringMatch = i; 203 else if (stringMatch > 0 && string[i] != string[i-1]) 204 { 205 errln("**** FAIL: String mismatch after match: " + message); 206 dump = true; 207 break; 208 } 209 } 210 if (numberMatch > 0 && stringMatch > 0) 211 break; 212 213 if (i == DEPTH) 214 --i; 215 216 if (stringMatch > 2 || numberMatch > 2) 217 { 218 errln("**** FAIL: No string and/or number match within 2 iterations: " + message); 219 dump = true; 220 } 221 222 if (dump) 223 { 224 for (int k=0; k<=i; ++k) 225 { 226 logln(k + ": " + number[k] + " F> " + 227 string[k] + " P> "); 228 } 229 } 230 } 231 } 232 233 /** 234 * perform tests using aNumber and fNumberFormat, called in many variations 235 **/ tryIt(int aNumber)236 public void tryIt(int aNumber) { 237 long number; 238 239 String message = "Locale: " + fNumberFormat.getLocale(ULocale.VALID_LOCALE); 240 241 String stringNum = fNumberFormat.format(aNumber); 242 try { 243 number = fNumberFormat.parse(stringNum).longValue(); 244 } catch (java.text.ParseException pe) { 245 errln("**** FAIL: Parse of " + stringNum + " failed: " + message); 246 return; 247 } 248 249 if (number != aNumber) { 250 errln("**** FAIL: Parse of " + stringNum + " failed: " + message 251 + " Got:" + number 252 + " Expected:" + aNumber); 253 } 254 255 } 256 257 /** 258 * test NumberFormat::getAvailableLocales 259 **/ 260 @Test TestAvailableLocales()261 public void TestAvailableLocales() { 262 final Locale[] locales = NumberFormat.getAvailableLocales(); 263 int count = locales.length; 264 logln(count + " available locales"); 265 if (count != 0) 266 { 267 String all = ""; 268 for (int i = 0; i< count; ++i) 269 { 270 if (i!=0) 271 all += ", "; 272 all += locales[i].getDisplayName(); 273 } 274 logln(all); 275 } 276 else 277 errln("**** FAIL: Zero available locales or null array pointer"); 278 } 279 280 /** 281 * call testLocale for all locales 282 **/ 283 @Test TestMonster()284 public void TestMonster() { 285 final String SEP = "============================================================\n"; 286 int count; 287 final Locale[] allLocales = NumberFormat.getAvailableLocales(); 288 Locale[] locales = allLocales; 289 count = locales.length; 290 if (count != 0) 291 { 292 if (TestFmwk.getExhaustiveness() < 10 && count > 7) { 293 count = 7; 294 locales = new Locale[count]; 295 locales[0] = allLocales[0]; 296 locales[1] = allLocales[1]; 297 locales[2] = allLocales[2]; 298 // In a quick test, make sure we test locales that use 299 // currency prefix, currency suffix, and choice currency 300 // logic. Otherwise bugs in these areas can slip through. 301 locales[3] = new Locale("ar", "AE", ""); 302 locales[4] = new Locale("cs", "CZ", ""); 303 locales[5] = new Locale("en", "IN", ""); 304 locales[6] = new Locale("su", "", ""); 305 } 306 for (int i=0; i<count; ++i) 307 { 308 logln(SEP); 309 _testLocale(locales[i]); 310 } 311 } 312 313 logln(SEP); 314 } 315 } 316