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