1 /********************************************************************
3 * Copyright (c) 1997-2013, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/currpinf.h"
14 #include "unicode/dcfmtsym.h"
15 #include "unicode/decimfmt.h"
16 #include "unicode/fmtable.h"
17 #include "unicode/localpointer.h"
18 #include "unicode/parseerr.h"
19 #include "unicode/stringpiece.h"
22 #include "plurrule_impl.h"
25 #define LENGTHOF(array) ((int32_t)(sizeof(array)/sizeof((array)[0])))
27 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
28 // try to test the full functionality. It just calls each function in the class and
29 // verifies that it works on a basic level.
31 void IntlTestDecimalFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
33 if (exec) logln((UnicodeString)"TestSuite DecimalFormatAPI");
35 case 0: name = "DecimalFormat API test";
37 logln((UnicodeString)"DecimalFormat API test---"); logln((UnicodeString)"");
38 UErrorCode status = U_ZERO_ERROR;
40 Locale::setDefault(Locale::getEnglish(), status);
41 if(U_FAILURE(status)) {
42 errln((UnicodeString)"ERROR: Could not set default locale, test may not give correct results");
45 Locale::setDefault(saveLocale, status);
48 case 1: name = "Rounding test";
50 logln((UnicodeString)"DecimalFormat Rounding test---");
51 testRounding(/*par*/);
54 case 2: name = "Test6354";
56 logln((UnicodeString)"DecimalFormat Rounding Increment test---");
57 testRoundingInc(/*par*/);
60 case 3: name = "TestCurrencyPluralInfo";
62 logln((UnicodeString)"CurrencyPluralInfo API test---");
63 TestCurrencyPluralInfo();
66 case 4: name = "TestScale";
68 logln((UnicodeString)"Scale test---");
72 case 5: name = "TestFixedDecimal";
74 logln((UnicodeString)"TestFixedDecimal ---");
78 default: name = ""; break;
83 * This test checks various generic API methods in DecimalFormat to achieve 100%
86 void IntlTestDecimalFormatAPI::testAPI(/*char *par*/)
88 UErrorCode status = U_ZERO_ERROR;
90 // ======= Test constructors
92 logln((UnicodeString)"Testing DecimalFormat constructors");
94 DecimalFormat def(status);
95 if(U_FAILURE(status)) {
96 errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
100 status = U_ZERO_ERROR;
101 const UnicodeString pattern("#,##0.# FF");
102 DecimalFormat pat(pattern, status);
103 if(U_FAILURE(status)) {
104 errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern)");
108 status = U_ZERO_ERROR;
109 DecimalFormatSymbols *symbols = new DecimalFormatSymbols(Locale::getFrench(), status);
110 if(U_FAILURE(status)) {
111 errln((UnicodeString)"ERROR: Could not create DecimalFormatSymbols (French)");
115 status = U_ZERO_ERROR;
116 DecimalFormat cust1(pattern, symbols, status);
117 if(U_FAILURE(status)) {
118 errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern, symbols*)");
121 status = U_ZERO_ERROR;
122 DecimalFormat cust2(pattern, *symbols, status);
123 if(U_FAILURE(status)) {
124 errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern, symbols)");
127 DecimalFormat copy(pat);
129 // ======= Test clone(), assignment, and equality
131 logln((UnicodeString)"Testing clone(), assignment and equality operators");
133 if( ! (copy == pat) || copy != pat) {
134 errln((UnicodeString)"ERROR: Copy constructor or == failed");
139 errln((UnicodeString)"ERROR: Assignment (or !=) failed");
142 Format *clone = def.clone();
143 if( ! (*clone == def) ) {
144 errln((UnicodeString)"ERROR: Clone() failed");
148 // ======= Test various format() methods
150 logln((UnicodeString)"Testing various format() methods");
152 double d = -10456.0037;
153 int32_t l = 100000000;
157 UnicodeString res1, res2, res3, res4;
158 FieldPosition pos1(0), pos2(0), pos3(0), pos4(0);
160 res1 = def.format(d, res1, pos1);
161 logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1);
163 res2 = pat.format(l, res2, pos2);
164 logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2);
166 status = U_ZERO_ERROR;
167 res3 = cust1.format(fD, res3, pos3, status);
168 if(U_FAILURE(status)) {
169 errln((UnicodeString)"ERROR: format(Formattable [double]) failed");
171 logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res3);
173 status = U_ZERO_ERROR;
174 res4 = cust2.format(fL, res4, pos4, status);
175 if(U_FAILURE(status)) {
176 errln((UnicodeString)"ERROR: format(Formattable [long]) failed");
178 logln((UnicodeString) "" + fL.getLong() + " formatted to " + res4);
180 // ======= Test parse()
182 logln((UnicodeString)"Testing parse()");
184 UnicodeString text("-10,456.0037");
185 Formattable result1, result2;
186 ParsePosition pos(0);
187 UnicodeString patt("#,##0.#");
188 status = U_ZERO_ERROR;
189 pat.applyPattern(patt, status);
190 if(U_FAILURE(status)) {
191 errln((UnicodeString)"ERROR: applyPattern() failed");
193 pat.parse(text, result1, pos);
194 if(result1.getType() != Formattable::kDouble && result1.getDouble() != d) {
195 errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text);
197 logln(text + " parsed into " + (int32_t) result1.getDouble());
199 status = U_ZERO_ERROR;
200 pat.parse(text, result2, status);
201 if(U_FAILURE(status)) {
202 errln((UnicodeString)"ERROR: parse() failed");
204 if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
205 errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text);
207 logln(text + " parsed into " + (int32_t) result2.getDouble());
209 // ======= Test getters and setters
211 logln((UnicodeString)"Testing getters and setters");
213 const DecimalFormatSymbols *syms = pat.getDecimalFormatSymbols();
214 DecimalFormatSymbols *newSyms = new DecimalFormatSymbols(*syms);
215 def.setDecimalFormatSymbols(*newSyms);
216 def.adoptDecimalFormatSymbols(newSyms); // don't use newSyms after this
217 if( *(pat.getDecimalFormatSymbols()) != *(def.getDecimalFormatSymbols())) {
218 errln((UnicodeString)"ERROR: adopt or set DecimalFormatSymbols() failed");
221 UnicodeString posPrefix;
222 pat.setPositivePrefix("+");
223 posPrefix = pat.getPositivePrefix(posPrefix);
224 logln((UnicodeString)"Positive prefix (should be +): " + posPrefix);
225 if(posPrefix != "+") {
226 errln((UnicodeString)"ERROR: setPositivePrefix() failed");
229 UnicodeString negPrefix;
230 pat.setNegativePrefix("-");
231 negPrefix = pat.getNegativePrefix(negPrefix);
232 logln((UnicodeString)"Negative prefix (should be -): " + negPrefix);
233 if(negPrefix != "-") {
234 errln((UnicodeString)"ERROR: setNegativePrefix() failed");
237 UnicodeString posSuffix;
238 pat.setPositiveSuffix("_");
239 posSuffix = pat.getPositiveSuffix(posSuffix);
240 logln((UnicodeString)"Positive suffix (should be _): " + posSuffix);
241 if(posSuffix != "_") {
242 errln((UnicodeString)"ERROR: setPositiveSuffix() failed");
245 UnicodeString negSuffix;
246 pat.setNegativeSuffix("~");
247 negSuffix = pat.getNegativeSuffix(negSuffix);
248 logln((UnicodeString)"Negative suffix (should be ~): " + negSuffix);
249 if(negSuffix != "~") {
250 errln((UnicodeString)"ERROR: setNegativeSuffix() failed");
253 int32_t multiplier = 0;
254 pat.setMultiplier(8);
255 multiplier = pat.getMultiplier();
256 logln((UnicodeString)"Multiplier (should be 8): " + multiplier);
257 if(multiplier != 8) {
258 errln((UnicodeString)"ERROR: setMultiplier() failed");
261 int32_t groupingSize = 0;
262 pat.setGroupingSize(2);
263 groupingSize = pat.getGroupingSize();
264 logln((UnicodeString)"Grouping size (should be 2): " + (int32_t) groupingSize);
265 if(groupingSize != 2) {
266 errln((UnicodeString)"ERROR: setGroupingSize() failed");
269 pat.setDecimalSeparatorAlwaysShown(TRUE);
270 UBool tf = pat.isDecimalSeparatorAlwaysShown();
271 logln((UnicodeString)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString) (tf ? "TRUE" : "FALSE"));
273 errln((UnicodeString)"ERROR: setDecimalSeparatorAlwaysShown() failed");
275 // Added by Ken Liu testing set/isExponentSignAlwaysShown
276 pat.setExponentSignAlwaysShown(TRUE);
277 UBool esas = pat.isExponentSignAlwaysShown();
278 logln((UnicodeString)"ExponentSignAlwaysShown (should be TRUE) is " + (UnicodeString) (esas ? "TRUE" : "FALSE"));
280 errln((UnicodeString)"ERROR: ExponentSignAlwaysShown() failed");
283 // Added by Ken Liu testing set/isScientificNotation
284 pat.setScientificNotation(TRUE);
285 UBool sn = pat.isScientificNotation();
286 logln((UnicodeString)"isScientificNotation (should be TRUE) is " + (UnicodeString) (sn ? "TRUE" : "FALSE"));
288 errln((UnicodeString)"ERROR: setScientificNotation() failed");
291 // Added by Ken Liu testing set/getMinimumExponentDigits
292 int8_t MinimumExponentDigits = 0;
293 pat.setMinimumExponentDigits(2);
294 MinimumExponentDigits = pat.getMinimumExponentDigits();
295 logln((UnicodeString)"MinimumExponentDigits (should be 2) is " + (int8_t) MinimumExponentDigits);
296 if(MinimumExponentDigits != 2) {
297 errln((UnicodeString)"ERROR: setMinimumExponentDigits() failed");
300 // Added by Ken Liu testing set/getRoundingIncrement
301 double RoundingIncrement = 0.0;
302 pat.setRoundingIncrement(2.0);
303 RoundingIncrement = pat.getRoundingIncrement();
304 logln((UnicodeString)"RoundingIncrement (should be 2.0) is " + (double) RoundingIncrement);
305 if(RoundingIncrement != 2.0) {
306 errln((UnicodeString)"ERROR: setRoundingIncrement() failed");
308 //end of Ken's Adding
310 UnicodeString funkyPat;
311 funkyPat = pat.toPattern(funkyPat);
312 logln((UnicodeString)"Pattern is " + funkyPat);
314 UnicodeString locPat;
315 locPat = pat.toLocalizedPattern(locPat);
316 logln((UnicodeString)"Localized pattern is " + locPat);
318 // ======= Test applyPattern()
320 logln((UnicodeString)"Testing applyPattern()");
322 UnicodeString p1("#,##0.0#;(#,##0.0#)");
323 logln((UnicodeString)"Applying pattern " + p1);
324 status = U_ZERO_ERROR;
325 pat.applyPattern(p1, status);
326 if(U_FAILURE(status)) {
327 errln((UnicodeString)"ERROR: applyPattern() failed with " + (int32_t) status);
330 s2 = pat.toPattern(s2);
331 logln((UnicodeString)"Extracted pattern is " + s2);
333 errln((UnicodeString)"ERROR: toPattern() result did not match pattern applied");
336 if(pat.getSecondaryGroupingSize() != 0) {
337 errln("FAIL: Secondary Grouping Size should be 0, not %d\n", pat.getSecondaryGroupingSize());
340 if(pat.getGroupingSize() != 3) {
341 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
344 UnicodeString p2("#,##,##0.0# FF;(#,##,##0.0# FF)");
345 logln((UnicodeString)"Applying pattern " + p2);
346 status = U_ZERO_ERROR;
347 pat.applyLocalizedPattern(p2, status);
348 if(U_FAILURE(status)) {
349 errln((UnicodeString)"ERROR: applyPattern() failed with " + (int32_t) status);
352 s3 = pat.toLocalizedPattern(s3);
353 logln((UnicodeString)"Extracted pattern is " + s3);
355 errln((UnicodeString)"ERROR: toLocalizedPattern() result did not match pattern applied");
358 status = U_ZERO_ERROR;
360 pat.applyLocalizedPattern(p2, pe, status);
361 if(U_FAILURE(status)) {
362 errln((UnicodeString)"ERROR: applyPattern((with ParseError)) failed with " + (int32_t) status);
365 s4 = pat.toLocalizedPattern(s3);
366 logln((UnicodeString)"Extracted pattern is " + s4);
368 errln((UnicodeString)"ERROR: toLocalizedPattern(with ParseErr) result did not match pattern applied");
371 if(pat.getSecondaryGroupingSize() != 2) {
372 errln("FAIL: Secondary Grouping Size should be 2, not %d\n", pat.getSecondaryGroupingSize());
375 if(pat.getGroupingSize() != 3) {
376 errln("FAIL: Primary Grouping Size should be 3, not %d\n", pat.getGroupingSize());
379 // ======= Test getStaticClassID()
381 logln((UnicodeString)"Testing getStaticClassID()");
383 status = U_ZERO_ERROR;
384 NumberFormat *test = new DecimalFormat(status);
385 if(U_FAILURE(status)) {
386 errln((UnicodeString)"ERROR: Couldn't create a DecimalFormat");
389 if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
390 errln((UnicodeString)"ERROR: getDynamicClassID() didn't return the expected value");
396 void IntlTestDecimalFormatAPI::TestCurrencyPluralInfo(){
397 UErrorCode status = U_ZERO_ERROR;
399 CurrencyPluralInfo *cpi = new CurrencyPluralInfo(status);
400 if(U_FAILURE(status)) {
401 errln((UnicodeString)"ERROR: CurrencyPluralInfo(UErrorCode) could not be created");
404 CurrencyPluralInfo cpi1 = *cpi;
406 if(cpi->getDynamicClassID() != CurrencyPluralInfo::getStaticClassID()){
407 errln((UnicodeString)"ERROR: CurrencyPluralInfo::getDynamicClassID() didn't return the expected value");
410 cpi->setCurrencyPluralPattern("","",status);
411 if(U_FAILURE(status)) {
412 errln((UnicodeString)"ERROR: CurrencyPluralInfo::setCurrencyPluralPattern");
415 cpi->setLocale(Locale::getCanada(), status);
416 if(U_FAILURE(status)) {
417 errln((UnicodeString)"ERROR: CurrencyPluralInfo::setLocale");
420 cpi->setPluralRules("",status);
421 if(U_FAILURE(status)) {
422 errln((UnicodeString)"ERROR: CurrencyPluralInfo::setPluralRules");
425 DecimalFormat *df = new DecimalFormat(status);
426 if(U_FAILURE(status)) {
427 errcheckln(status, "ERROR: Could not create DecimalFormat - %s", u_errorName(status));
430 df->adoptCurrencyPluralInfo(cpi);
432 df->getCurrencyPluralInfo();
434 df->setCurrencyPluralInfo(cpi1);
439 void IntlTestDecimalFormatAPI::testRounding(/*char *par*/)
441 UErrorCode status = U_ZERO_ERROR;
442 double Roundingnumber = 2.55;
443 double Roundingnumber1 = -2.55;
444 //+2.55 results -2.55 results
445 double result[]={ 3.0, -2.0, // kRoundCeiling 0,
446 2.0, -3.0, // kRoundFloor 1,
447 2.0, -2.0, // kRoundDown 2,
448 3.0, -3.0, // kRoundUp 3,
449 3.0, -3.0, // kRoundHalfEven 4,
450 3.0, -3.0, // kRoundHalfDown 5,
451 3.0, -3.0 // kRoundHalfUp 6
453 DecimalFormat pat(status);
454 if(U_FAILURE(status)) {
455 errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
460 UnicodeString message;
461 UnicodeString resultStr;
462 for(mode=0;mode < 7;mode++){
463 pat.setRoundingMode((DecimalFormat::ERoundingMode)mode);
464 if(pat.getRoundingMode() != (DecimalFormat::ERoundingMode)mode){
465 errln((UnicodeString)"SetRoundingMode or GetRoundingMode failed for mode=" + mode);
469 //for +2.55 with RoundingIncrement=1.0
470 pat.setRoundingIncrement(1.0);
471 pat.format(Roundingnumber, resultStr);
472 message= (UnicodeString)"Round() failed: round(" + (double)Roundingnumber + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
473 verify(message, resultStr, result[i++]);
477 //for -2.55 with RoundingIncrement=1.0
478 pat.format(Roundingnumber1, resultStr);
479 message= (UnicodeString)"Round() failed: round(" + (double)Roundingnumber1 + UnicodeString(",") + mode + UnicodeString(",FALSE) with RoundingIncrement=1.0==>");
480 verify(message, resultStr, result[i++]);
486 void IntlTestDecimalFormatAPI::verify(const UnicodeString& message, const UnicodeString& got, double expected){
487 logln((UnicodeString)message + got + (UnicodeString)" Expected : " + expected);
488 UnicodeString expectedStr("");
489 expectedStr=expectedStr + expected;
490 if(got != expectedStr ) {
491 errln((UnicodeString)"ERROR: " + message + got + (UnicodeString)" Expected : " + expectedStr);
495 void IntlTestDecimalFormatAPI::verifyString(const UnicodeString& message, const UnicodeString& got, UnicodeString& expected){
496 logln((UnicodeString)message + got + (UnicodeString)" Expected : " + expected);
497 if(got != expected ) {
498 errln((UnicodeString)"ERROR: " + message + got + (UnicodeString)" Expected : " + expected);
502 void IntlTestDecimalFormatAPI::testRoundingInc(/*char *par*/)
504 UErrorCode status = U_ZERO_ERROR;
505 DecimalFormat pat(UnicodeString("#,##0.00"),status);
506 if(U_FAILURE(status)) {
507 errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
511 // get default rounding increment
512 double roundingInc = pat.getRoundingIncrement();
513 if (roundingInc != 0.0) {
514 errln((UnicodeString)"ERROR: Rounding increment not zero");
518 // With rounding now being handled by decNumber, we no longer
519 // set a rounding increment to enable non-default mode rounding,
520 // checking of which was the original point of this test.
522 // set rounding mode with zero increment. Rounding
523 // increment should not be set by this operation
524 pat.setRoundingMode((DecimalFormat::ERoundingMode)0);
525 roundingInc = pat.getRoundingIncrement();
526 if (roundingInc != 0.0) {
527 errln((UnicodeString)"ERROR: Rounding increment not zero after setRoundingMode");
532 void IntlTestDecimalFormatAPI::TestScale()
534 typedef struct TestData {
537 const char *expectedOutput;
540 static TestData testData[] = {
541 { 100.0, 3, "100,000" },
542 { 10034.0, -2, "100.34" },
543 { 0.86, -3, "0.0009" },
544 { -0.000455, 1, "-0%" },
545 { -0.000555, 1, "-1%" },
546 { 0.000455, 1, "0%" },
547 { 0.000555, 1, "1%" },
550 UErrorCode status = U_ZERO_ERROR;
551 DecimalFormat pat(status);
552 if(U_FAILURE(status)) {
553 errcheckln(status, "ERROR: Could not create DecimalFormat (default) - %s", u_errorName(status));
557 UnicodeString message;
558 UnicodeString resultStr;
560 UnicodeString percentPattern("#,##0%");
561 pat.setMaximumFractionDigits(4);
563 for(int32_t i=0; i < LENGTHOF(testData); i++) {
565 pat.applyPattern(percentPattern,status);
567 pat.setAttribute(UNUM_SCALE,testData[i].inputScale,status);
568 pat.format(testData[i].inputValue, resultStr);
569 message = UnicodeString("Unexpected output for ") + testData[i].inputValue + UnicodeString(" and scale ") +
570 testData[i].inputScale + UnicodeString(". Got: ");
571 exp = testData[i].expectedOutput;
572 verifyString(message, resultStr, exp);
580 #define ASSERT_EQUAL(expect, actual) { char tmp[200]; sprintf(tmp, "(%g==%g)", (double)(expect), (double)(actual)); \
581 assertTrue(tmp, ((expect)==(actual)), FALSE, TRUE, __FILE__, __LINE__); }
583 void IntlTestDecimalFormatAPI::TestFixedDecimal() {
584 UErrorCode status = U_ZERO_ERROR;
586 LocalPointer<DecimalFormat> df(new DecimalFormat("###", status));
587 TEST_ASSERT_STATUS(status);
588 FixedDecimal fd = df->getFixedDecimal(44, status);
589 TEST_ASSERT_STATUS(status);
590 ASSERT_EQUAL(44, fd.source);
591 ASSERT_EQUAL(0, fd.visibleDecimalDigitCount);
593 df.adoptInstead(new DecimalFormat("###.00##", status));
594 TEST_ASSERT_STATUS(status);
595 fd = df->getFixedDecimal(123.456, status);
596 TEST_ASSERT_STATUS(status);
597 ASSERT_EQUAL(3, fd.visibleDecimalDigitCount);
598 ASSERT_EQUAL(456, fd.decimalDigits);
599 ASSERT_EQUAL(456, fd.decimalDigitsWithoutTrailingZeros);
600 ASSERT_EQUAL(123, fd.intValue);
601 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
602 ASSERT_EQUAL(FALSE, fd.isNegative);
604 df.adoptInstead(new DecimalFormat("###", status));
605 TEST_ASSERT_STATUS(status);
606 fd = df->getFixedDecimal(123.456, status);
607 TEST_ASSERT_STATUS(status);
608 ASSERT_EQUAL(0, fd.visibleDecimalDigitCount);
609 ASSERT_EQUAL(0, fd.decimalDigits);
610 ASSERT_EQUAL(0, fd.decimalDigitsWithoutTrailingZeros);
611 ASSERT_EQUAL(123, fd.intValue);
612 ASSERT_EQUAL(TRUE, fd.hasIntegerValue);
613 ASSERT_EQUAL(FALSE, fd.isNegative);
615 df.adoptInstead(new DecimalFormat("###.0", status));
616 TEST_ASSERT_STATUS(status);
617 fd = df->getFixedDecimal(123.01, status);
618 TEST_ASSERT_STATUS(status);
619 ASSERT_EQUAL(1, fd.visibleDecimalDigitCount);
620 ASSERT_EQUAL(0, fd.decimalDigits);
621 ASSERT_EQUAL(0, fd.decimalDigitsWithoutTrailingZeros);
622 ASSERT_EQUAL(123, fd.intValue);
623 ASSERT_EQUAL(TRUE, fd.hasIntegerValue);
624 ASSERT_EQUAL(FALSE, fd.isNegative);
626 df.adoptInstead(new DecimalFormat("###.0", status));
627 TEST_ASSERT_STATUS(status);
628 fd = df->getFixedDecimal(123.06, status);
629 TEST_ASSERT_STATUS(status);
630 ASSERT_EQUAL(1, fd.visibleDecimalDigitCount);
631 ASSERT_EQUAL(1, fd.decimalDigits);
632 ASSERT_EQUAL(1, fd.decimalDigitsWithoutTrailingZeros);
633 ASSERT_EQUAL(123, fd.intValue);
634 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
635 ASSERT_EQUAL(FALSE, fd.isNegative);
637 df.adoptInstead(new DecimalFormat("@@@@@", status)); // Significant Digits
638 TEST_ASSERT_STATUS(status);
639 fd = df->getFixedDecimal(123, status);
640 TEST_ASSERT_STATUS(status);
641 ASSERT_EQUAL(2, fd.visibleDecimalDigitCount);
642 ASSERT_EQUAL(0, fd.decimalDigits);
643 ASSERT_EQUAL(0, fd.decimalDigitsWithoutTrailingZeros);
644 ASSERT_EQUAL(123, fd.intValue);
645 ASSERT_EQUAL(TRUE, fd.hasIntegerValue);
646 ASSERT_EQUAL(FALSE, fd.isNegative);
648 df.adoptInstead(new DecimalFormat("@@@@@", status)); // Significant Digits
649 TEST_ASSERT_STATUS(status);
650 fd = df->getFixedDecimal(1.23, status);
651 TEST_ASSERT_STATUS(status);
652 ASSERT_EQUAL(4, fd.visibleDecimalDigitCount);
653 ASSERT_EQUAL(2300, fd.decimalDigits);
654 ASSERT_EQUAL(23, fd.decimalDigitsWithoutTrailingZeros);
655 ASSERT_EQUAL(1, fd.intValue);
656 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
657 ASSERT_EQUAL(FALSE, fd.isNegative);
659 fd = df->getFixedDecimal(uprv_getInfinity(), status);
660 TEST_ASSERT_STATUS(status);
661 ASSERT_EQUAL(TRUE, fd.isNanOrInfinity);
662 fd = df->getFixedDecimal(0.0, status);
663 ASSERT_EQUAL(FALSE, fd.isNanOrInfinity);
664 fd = df->getFixedDecimal(uprv_getNaN(), status);
665 ASSERT_EQUAL(TRUE, fd.isNanOrInfinity);
666 TEST_ASSERT_STATUS(status);
668 // Test Big Decimal input.
669 // 22 digits before and after decimal, will exceed the precision of a double
670 // and force DecimalFormat::getFixedDecimal() to work with a digit list.
671 df.adoptInstead(new DecimalFormat("#####################0.00####################", status));
672 TEST_ASSERT_STATUS(status);
673 Formattable fable("12.34", status);
674 TEST_ASSERT_STATUS(status);
675 fd = df->getFixedDecimal(fable, status);
676 TEST_ASSERT_STATUS(status);
677 ASSERT_EQUAL(2, fd.visibleDecimalDigitCount);
678 ASSERT_EQUAL(34, fd.decimalDigits);
679 ASSERT_EQUAL(34, fd.decimalDigitsWithoutTrailingZeros);
680 ASSERT_EQUAL(12, fd.intValue);
681 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
682 ASSERT_EQUAL(FALSE, fd.isNegative);
684 fable.setDecimalNumber("12.345678901234567890123456789", status);
685 TEST_ASSERT_STATUS(status);
686 fd = df->getFixedDecimal(fable, status);
687 TEST_ASSERT_STATUS(status);
688 ASSERT_EQUAL(22, fd.visibleDecimalDigitCount);
689 ASSERT_EQUAL(345678901234567890LL, fd.decimalDigits);
690 ASSERT_EQUAL(34567890123456789LL, fd.decimalDigitsWithoutTrailingZeros);
691 ASSERT_EQUAL(12, fd.intValue);
692 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
693 ASSERT_EQUAL(FALSE, fd.isNegative);
695 // On field overflow, Integer part is truncated on the left, fraction part on the right.
696 fable.setDecimalNumber("123456789012345678901234567890.123456789012345678901234567890", status);
697 TEST_ASSERT_STATUS(status);
698 fd = df->getFixedDecimal(fable, status);
699 TEST_ASSERT_STATUS(status);
700 ASSERT_EQUAL(22, fd.visibleDecimalDigitCount);
701 ASSERT_EQUAL(123456789012345678LL, fd.decimalDigits);
702 ASSERT_EQUAL(123456789012345678LL, fd.decimalDigitsWithoutTrailingZeros);
703 ASSERT_EQUAL(345678901234567890LL, fd.intValue);
704 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
705 ASSERT_EQUAL(FALSE, fd.isNegative);
707 // Digits way to the right of the decimal but within the format's precision aren't truncated
708 fable.setDecimalNumber("1.0000000000000000000012", status);
709 TEST_ASSERT_STATUS(status);
710 fd = df->getFixedDecimal(fable, status);
711 TEST_ASSERT_STATUS(status);
712 ASSERT_EQUAL(22, fd.visibleDecimalDigitCount);
713 ASSERT_EQUAL(12, fd.decimalDigits);
714 ASSERT_EQUAL(12, fd.decimalDigitsWithoutTrailingZeros);
715 ASSERT_EQUAL(1, fd.intValue);
716 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
717 ASSERT_EQUAL(FALSE, fd.isNegative);
719 // Digits beyond the precision of the format are rounded away
720 fable.setDecimalNumber("1.000000000000000000000012", status);
721 TEST_ASSERT_STATUS(status);
722 fd = df->getFixedDecimal(fable, status);
723 TEST_ASSERT_STATUS(status);
724 ASSERT_EQUAL(2, fd.visibleDecimalDigitCount);
725 ASSERT_EQUAL(0, fd.decimalDigits);
726 ASSERT_EQUAL(0, fd.decimalDigitsWithoutTrailingZeros);
727 ASSERT_EQUAL(1, fd.intValue);
728 ASSERT_EQUAL(TRUE, fd.hasIntegerValue);
729 ASSERT_EQUAL(FALSE, fd.isNegative);
731 // Negative numbers come through
732 fable.setDecimalNumber("-1.0000000000000000000012", status);
733 TEST_ASSERT_STATUS(status);
734 fd = df->getFixedDecimal(fable, status);
735 TEST_ASSERT_STATUS(status);
736 ASSERT_EQUAL(22, fd.visibleDecimalDigitCount);
737 ASSERT_EQUAL(12, fd.decimalDigits);
738 ASSERT_EQUAL(12, fd.decimalDigitsWithoutTrailingZeros);
739 ASSERT_EQUAL(1, fd.intValue);
740 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
741 ASSERT_EQUAL(TRUE, fd.isNegative);
743 // MinFractionDigits from format larger than from number.
744 fable.setDecimalNumber("1000000000000000000000.3", status);
745 TEST_ASSERT_STATUS(status);
746 fd = df->getFixedDecimal(fable, status);
747 TEST_ASSERT_STATUS(status);
748 ASSERT_EQUAL(2, fd.visibleDecimalDigitCount);
749 ASSERT_EQUAL(30, fd.decimalDigits);
750 ASSERT_EQUAL(3, fd.decimalDigitsWithoutTrailingZeros);
751 ASSERT_EQUAL(100000000000000000LL, fd.intValue);
752 ASSERT_EQUAL(FALSE, fd.hasIntegerValue);
753 ASSERT_EQUAL(FALSE, fd.isNegative);
755 // Test some int64_t values that are out of the range of a double
756 fable.setInt64(4503599627370496LL);
757 TEST_ASSERT_STATUS(status);
758 fd = df->getFixedDecimal(fable, status);
759 TEST_ASSERT_STATUS(status);
760 ASSERT_EQUAL(2, fd.visibleDecimalDigitCount);
761 ASSERT_EQUAL(0, fd.decimalDigits);
762 ASSERT_EQUAL(0, fd.decimalDigitsWithoutTrailingZeros);
763 ASSERT_EQUAL(4503599627370496LL, fd.intValue);
764 ASSERT_EQUAL(TRUE, fd.hasIntegerValue);
765 ASSERT_EQUAL(FALSE, fd.isNegative);
767 fable.setInt64(4503599627370497LL);
768 TEST_ASSERT_STATUS(status);
769 fd = df->getFixedDecimal(fable, status);
770 TEST_ASSERT_STATUS(status);
771 ASSERT_EQUAL(2, fd.visibleDecimalDigitCount);
772 ASSERT_EQUAL(0, fd.decimalDigits);
773 ASSERT_EQUAL(0, fd.decimalDigitsWithoutTrailingZeros);
774 ASSERT_EQUAL(4503599627370497LL, fd.intValue);
775 ASSERT_EQUAL(TRUE, fd.hasIntegerValue);
776 ASSERT_EQUAL(FALSE, fd.isNegative);
778 fable.setInt64(9223372036854775807LL);
779 TEST_ASSERT_STATUS(status);
780 fd = df->getFixedDecimal(fable, status);
781 TEST_ASSERT_STATUS(status);
782 ASSERT_EQUAL(2, fd.visibleDecimalDigitCount);
783 ASSERT_EQUAL(0, fd.decimalDigits);
784 ASSERT_EQUAL(0, fd.decimalDigitsWithoutTrailingZeros);
785 // note: going through DigitList path to FixedDecimal, which is trimming
786 // int64_t fields to 18 digits. See ticket Ticket #10374
787 // ASSERT_EQUAL(223372036854775807LL, fd.intValue);
788 if (!(fd.intValue == 223372036854775807LL || fd.intValue == 9223372036854775807LL)) {
789 dataerrln("File %s, Line %d, fd.intValue = %lld", __FILE__, __LINE__, fd.intValue);
791 ASSERT_EQUAL(TRUE, fd.hasIntegerValue);
792 ASSERT_EQUAL(FALSE, fd.isNegative);
796 #endif /* #if !UCONFIG_NO_FORMATTING */