1 /******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 1997-2009, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 7 #include "unicode/utypes.h" 8 9 #if !UCONFIG_NO_FORMATTING 10 11 #include "dcfmapts.h" 12 13 #include "unicode/decimfmt.h" 14 #include "unicode/dcfmtsym.h" 15 #include "unicode/parseerr.h" 16 17 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't 18 // try to test the full functionality. It just calls each function in the class and 19 // verifies that it works on a basic level. 20 21 void IntlTestDecimalFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) 22 { 23 if (exec) logln((UnicodeString)"TestSuite DecimalFormatAPI"); 24 switch (index) { 25 case 0: name = "DecimalFormat API test"; 26 if (exec) { 27 logln((UnicodeString)"DecimalFormat API test---"); logln((UnicodeString)""); 28 UErrorCode status = U_ZERO_ERROR; 29 Locale saveLocale; 30 Locale::setDefault(Locale::getEnglish(), status); 31 if(U_FAILURE(status)) { 32 errln((UnicodeString)"ERROR: Could not set default locale, test may not give correct results"); 33 } 34 testAPI(/*par*/); 35 Locale::setDefault(saveLocale, status); 36 } 37 break; 38 case 1: name = "Rounding test"; 39 if(exec) { 40 logln((UnicodeString)"DecimalFormat Rounding test---"); 41 testRounding(/*par*/); 42 } 43 break; 44 case 2: name = "Test6354"; 45 if(exec) { 46 logln((UnicodeString)"DecimalFormat Rounding Increment test---"); 47 testRoundingInc(/*par*/); 48 } 49 break; 50 default: name = ""; break; 51 } 52 } 53 54 /** 55 * This test checks various generic API methods in DecimalFormat to achieve 100% 56 * API coverage. 57 */ 58 void IntlTestDecimalFormatAPI::testAPI(/*char *par*/) 59 { 60 UErrorCode status = U_ZERO_ERROR; 61 62 // ======= Test constructors 63 64 logln((UnicodeString)"Testing DecimalFormat constructors"); 65 66 DecimalFormat def(status); 67 if(U_FAILURE(status)) { 68 errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status)); 69 return; 70 } 71 72 status = U_ZERO_ERROR; 73 const UnicodeString pattern("#,##0.# FF"); 74 DecimalFormat pat(pattern, status); 75 if(U_FAILURE(status)) { 76 errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern)"); 77 return; 78 } 79 80 status = U_ZERO_ERROR; 81 DecimalFormatSymbols *symbols = new DecimalFormatSymbols(Locale::getFrench(), status); 82 if(U_FAILURE(status)) { 83 errln((UnicodeString)"ERROR: Could not create DecimalFormatSymbols (French)"); 84 return; 85 } 86 87 status = U_ZERO_ERROR; 88 DecimalFormat cust1(pattern, symbols, status); 89 if(U_FAILURE(status)) { 90 errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern, symbols*)"); 91 } 92 93 status = U_ZERO_ERROR; 94 DecimalFormat cust2(pattern, *symbols, status); 95 if(U_FAILURE(status)) { 96 errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern, symbols)"); 97 } 98 99 DecimalFormat copy(pat); 100 101 // ======= Test clone(), assignment, and equality 102 103 logln((UnicodeString)"Testing clone(), assignment and equality operators"); 104 105 if( ! (copy == pat) || copy != pat) { 106 errln((UnicodeString)"ERROR: Copy constructor or == failed"); 107 } 108 109 copy = cust1; 110 if(copy != cust1) { 111 errln((UnicodeString)"ERROR: Assignment (or !=) failed"); 112 } 113 114 Format *clone = def.clone(); 115 if( ! (*clone == def) ) { 116 errln((UnicodeString)"ERROR: Clone() failed"); 117 } 118 delete clone; 119 120 // ======= Test various format() methods 121 122 logln((UnicodeString)"Testing various format() methods"); 123 124 double d = -10456.0037; 125 int32_t l = 100000000; 126 Formattable fD(d); 127 Formattable fL(l); 128 129 UnicodeString res1, res2, res3, res4; 130 FieldPosition pos1(0), pos2(0), pos3(0), pos4(0); 131 132 res1 = def.format(d, res1, pos1); 133 logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1); 134 135 res2 = pat.format(l, res2, pos2); 136 logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2); 137 138 status = U_ZERO_ERROR; 139 res3 = cust1.format(fD, res3, pos3, status); 140 if(U_FAILURE(status)) { 141 errln((UnicodeString)"ERROR: format(Formattable [double]) failed"); 142 } 143 logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res3); 144 145 status = U_ZERO_ERROR; 146 res4 = cust2.format(fL, res4, pos4, status); 147 if(U_FAILURE(status)) { 148 errln((UnicodeString)"ERROR: format(Formattable [long]) failed"); 149 } 150 logln((UnicodeString) "" + fL.getLong() + " formatted to " + res4); 151 152 // ======= Test parse() 153 154 logln((UnicodeString)"Testing parse()"); 155 156 UnicodeString text("-10,456.0037"); 157 Formattable result1, result2; 158 ParsePosition pos(0); 159 UnicodeString patt("#,##0.#"); 160 status = U_ZERO_ERROR; 161 pat.applyPattern(patt, status); 162 if(U_FAILURE(status)) { 163 errln((UnicodeString)"ERROR: applyPattern() failed"); 164 } 165 pat.parse(text, result1, pos); 166 if(result1.getType() != Formattable::kDouble && result1.getDouble() != d) { 167 errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text); 168 } 169 logln(text + " parsed into " + (int32_t) result1.getDouble()); 170 171 status = U_ZERO_ERROR; 172 pat.parse(text, result2, status); 173 if(U_FAILURE(status)) { 174 errln((UnicodeString)"ERROR: parse() failed"); 175 } 176 if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) { 177 errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text); 178 } 179 logln(text + " parsed into " + (int32_t) result2.getDouble()); 180 181 // ======= Test getters and setters 182 183 logln((UnicodeString)"Testing getters and setters"); 184 185 const DecimalFormatSymbols *syms = pat.getDecimalFormatSymbols(); 186 DecimalFormatSymbols *newSyms = new DecimalFormatSymbols(*syms); 187 def.setDecimalFormatSymbols(*newSyms); 188 def.adoptDecimalFormatSymbols(newSyms); // don't use newSyms after this 189 if( *(pat.getDecimalFormatSymbols()) != *(def.getDecimalFormatSymbols())) { 190 errln((UnicodeString)"ERROR: adopt or set DecimalFormatSymbols() failed"); 191 } 192 193 UnicodeString posPrefix; 194 pat.setPositivePrefix("+"); 195 posPrefix = pat.getPositivePrefix(posPrefix); 196 logln((UnicodeString)"Positive prefix (should be +): " + posPrefix); 197 if(posPrefix != "+") { 198 errln((UnicodeString)"ERROR: setPositivePrefix() failed"); 199 } 200 201 UnicodeString negPrefix; 202 pat.setNegativePrefix("-"); 203 negPrefix = pat.getNegativePrefix(negPrefix); 204 logln((UnicodeString)"Negative prefix (should be -): " + negPrefix); 205 if(negPrefix != "-") { 206 errln((UnicodeString)"ERROR: setNegativePrefix() failed"); 207 } 208 209 UnicodeString posSuffix; 210 pat.setPositiveSuffix("_"); 211 posSuffix = pat.getPositiveSuffix(posSuffix); 212 logln((UnicodeString)"Positive suffix (should be _): " + posSuffix); 213 if(posSuffix != "_") { 214 errln((UnicodeString)"ERROR: setPositiveSuffix() failed"); 215 } 216 217 UnicodeString negSuffix; 218 pat.setNegativeSuffix("~"); 219 negSuffix = pat.getNegativeSuffix(negSuffix); 220 logln((UnicodeString)"Negative suffix (should be ~): " + negSuffix); 221 if(negSuffix != "~") { 222 errln((UnicodeString)"ERROR: setNegativeSuffix() failed"); 223 } 224 225 int32_t multiplier = 0; 226 pat.setMultiplier(8); 227 multiplier = pat.getMultiplier(); 228 logln((UnicodeString)"Multiplier (should be 8): " + multiplier); 229 if(multiplier != 8) { 230 errln((UnicodeString)"ERROR: setMultiplier() failed"); 231 } 232 233 int32_t groupingSize = 0; 234 pat.setGroupingSize(2); 235 groupingSize = pat.getGroupingSize(); 236 logln((UnicodeString)"Grouping size (should be 2): " + (int32_t) groupingSize); 237 if(groupingSize != 2) { 238 errln((UnicodeString)"ERROR: setGroupingSize() failed"); 239 } 240 241 pat.setDecimalSeparatorAlwaysShown(TRUE); 242 UBool tf = pat.isDecimalSeparatorAlwaysShown(); 243 logln((UnicodeString)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString) (tf ? "TRUE" : "FALSE")); 244 if(tf != TRUE) { 245 errln((UnicodeString)"ERROR: setDecimalSeparatorAlwaysShown() failed"); 246 } 247 // Added by Ken Liu testing set/isExponentSignAlwaysShown 248 pat.setExponentSignAlwaysShown(TRUE); 249 UBool esas = pat.isExponentSignAlwaysShown(); 250 logln((UnicodeString)"ExponentSignAlwaysShown (should be TRUE) is " + (UnicodeString) (esas ? "TRUE" : "FALSE")); 251 if(esas != TRUE) { 252 errln((UnicodeString)"ERROR: ExponentSignAlwaysShown() failed"); 253 } 254 255 // Added by Ken Liu testing set/isScientificNotation 256 pat.setScientificNotation(TRUE); 257 UBool sn = pat.isScientificNotation(); 258 logln((UnicodeString)"isScientificNotation (should be TRUE) is " + (UnicodeString) (sn ? "TRUE" : "FALSE")); 259 if(sn != TRUE) { 260 errln((UnicodeString)"ERROR: setScientificNotation() failed"); 261 } 262 263 // Added by Ken Liu testing set/getMinimumExponentDigits 264 int8_t MinimumExponentDigits = 0; 265 pat.setMinimumExponentDigits(2); 266 MinimumExponentDigits = pat.getMinimumExponentDigits(); 267 logln((UnicodeString)"MinimumExponentDigits (should be 2) is " + (int8_t) MinimumExponentDigits); 268 if(MinimumExponentDigits != 2) { 269 errln((UnicodeString)"ERROR: setMinimumExponentDigits() failed"); 270 } 271 272 // Added by Ken Liu testing set/getRoundingIncrement 273 double RoundingIncrement = 0.0; 274 pat.setRoundingIncrement(2.0); 275 RoundingIncrement = pat.getRoundingIncrement(); 276 logln((UnicodeString)"RoundingIncrement (should be 2.0) is " + (double) RoundingIncrement); 277 if(RoundingIncrement != 2.0) { 278 errln((UnicodeString)"ERROR: setRoundingIncrement() failed"); 279 } 280 //end of Ken's Adding 281 282 UnicodeString funkyPat; 283 funkyPat = pat.toPattern(funkyPat); 284 logln((UnicodeString)"Pattern is " + funkyPat); 285 286 UnicodeString locPat; 287 locPat = pat.toLocalizedPattern(locPat); 288 logln((UnicodeString)"Localized pattern is " + locPat); 289 290 // ======= Test applyPattern() 291 292 logln((UnicodeString)"Testing applyPattern()"); 293 294 UnicodeString p1("#,##0.0#;(#,##0.0#)"); 295 logln((UnicodeString)"Applying pattern " + p1); 296 status = U_ZERO_ERROR; 297 pat.applyPattern(p1, status); 298 if(U_FAILURE(status)) { 299 errln((UnicodeString)"ERROR: applyPattern() failed with " + (int32_t) status); 300 } 301 UnicodeString s2; 302 s2 = pat.toPattern(s2); 303 logln((UnicodeString)"Extracted pattern is " + s2); 304 if(s2 != p1) { 305 errln((UnicodeString)"ERROR: toPattern() result did not match pattern applied"); 306 } 307 308 if(pat.getSecondaryGroupingSize() != 0) { 309 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat.getSecondaryGroupingSize()); 310 } 311 312 if(pat.getGroupingSize() != 3) { 313 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize()); 314 } 315 316 UnicodeString p2("#,##,##0.0# FF;(#,##,##0.0# FF)"); 317 logln((UnicodeString)"Applying pattern " + p2); 318 status = U_ZERO_ERROR; 319 pat.applyLocalizedPattern(p2, status); 320 if(U_FAILURE(status)) { 321 errln((UnicodeString)"ERROR: applyPattern() failed with " + (int32_t) status); 322 } 323 UnicodeString s3; 324 s3 = pat.toLocalizedPattern(s3); 325 logln((UnicodeString)"Extracted pattern is " + s3); 326 if(s3 != p2) { 327 errln((UnicodeString)"ERROR: toLocalizedPattern() result did not match pattern applied"); 328 } 329 330 status = U_ZERO_ERROR; 331 UParseError pe; 332 pat.applyLocalizedPattern(p2, pe, status); 333 if(U_FAILURE(status)) { 334 errln((UnicodeString)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status); 335 } 336 UnicodeString s4; 337 s4 = pat.toLocalizedPattern(s3); 338 logln((UnicodeString)"Extracted pattern is " + s4); 339 if(s4 != p2) { 340 errln((UnicodeString)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied"); 341 } 342 343 if(pat.getSecondaryGroupingSize() != 2) { 344 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat.getSecondaryGroupingSize()); 345 } 346 347 if(pat.getGroupingSize() != 3) { 348 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize()); 349 } 350 351 // ======= Test getStaticClassID() 352 353 logln((UnicodeString)"Testing getStaticClassID()"); 354 355 status = U_ZERO_ERROR; 356 NumberFormat *test = new DecimalFormat(status); 357 if(U_FAILURE(status)) { 358 errln((UnicodeString)"ERROR: Couldn't create a DecimalFormat"); 359 } 360 361 if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) { 362 errln((UnicodeString)"ERROR: getDynamicClassID() didn't return the expected value"); 363 } 364 365 delete test; 366 } 367 368 void IntlTestDecimalFormatAPI::testRounding(/*char *par*/) 369 { 370 UErrorCode status = U_ZERO_ERROR; 371 double Roundingnumber = 2.55; 372 double Roundingnumber1 = -2.55; 373 //+2.55 results -2.55 results 374 double result[]={ 3.0, -2.0, // kRoundCeiling 0, 375 2.0, -3.0, // kRoundFloor 1, 376 2.0, -2.0, // kRoundDown 2, 377 3.0, -3.0, // kRoundUp 3, 378 3.0, -3.0, // kRoundHalfEven 4, 379 3.0, -3.0, // kRoundHalfDown 5, 380 3.0, -3.0 // kRoundHalfUp 6 381 }; 382 DecimalFormat pat(status); 383 if(U_FAILURE(status)) { 384 errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status)); 385 return; 386 } 387 uint16_t mode; 388 uint16_t i=0; 389 UnicodeString message; 390 UnicodeString resultStr; 391 for(mode=0;mode < 7;mode++){ 392 pat.setRoundingMode((DecimalFormat::ERoundingMode)mode); 393 if(pat.getRoundingMode() != (DecimalFormat::ERoundingMode)mode){ 394 errln((UnicodeString)"SetRoundingMode or GetRoundingMode failed for mode=" + mode); 395 } 396 397 398 //for +2.55 with RoundingIncrement=1.0 399 pat.setRoundingIncrement(1.0); 400 pat.format(Roundingnumber, resultStr); 401 message= (UnicodeString)"round(" + (double)Roundingnumber + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>"); 402 verify(message, resultStr, result[i++]); 403 message.remove(); 404 resultStr.remove(); 405 406 //for -2.55 with RoundingIncrement=1.0 407 pat.format(Roundingnumber1, resultStr); 408 message= (UnicodeString)"round(" + (double)Roundingnumber1 + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>"); 409 verify(message, resultStr, result[i++]); 410 message.remove(); 411 resultStr.remove(); 412 } 413 414 } 415 void IntlTestDecimalFormatAPI::verify(const UnicodeString& message, const UnicodeString& got, double expected){ 416 logln((UnicodeString)message + got + (UnicodeString)" Expected : " + expected); 417 UnicodeString expectedStr(""); 418 expectedStr=expectedStr + expected; 419 if(got != expectedStr ) { 420 errln((UnicodeString)"ERROR: Round() failed: " + message + got + (UnicodeString)" Expected : " + expectedStr); 421 } 422 } 423 424 void IntlTestDecimalFormatAPI::testRoundingInc(/*char *par*/) 425 { 426 UErrorCode status = U_ZERO_ERROR; 427 DecimalFormat pat(UnicodeString("#,##0.00"),status); 428 if(U_FAILURE(status)) { 429 errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status)); 430 return; 431 } 432 433 // get default rounding increment 434 double roundingInc = pat.getRoundingIncrement(); 435 if (roundingInc != 0.0) { 436 errln((UnicodeString)"ERROR: Rounding increment not zero"); 437 return; 438 } 439 440 // set rounding mode with zero increment. Rounding 441 // increment should be set by this operation 442 pat.setRoundingMode((DecimalFormat::ERoundingMode)0); 443 roundingInc = pat.getRoundingIncrement(); 444 if (roundingInc == 0.0) { 445 errln((UnicodeString)"ERROR: Rounding increment zero"); 446 return; 447 } 448 } 449 450 #endif /* #if !UCONFIG_NO_FORMATTING */ 451