home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / nmfmapts.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  7.4 KB  |  233 lines

  1.  
  2. /*
  3. ********************************************************************
  4. * COPYRIGHT: 
  5. * (C) Copyright Taligent, Inc., 1997
  6. * (C) Copyright International Business Machines Corporation, 1997 - 1998
  7. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  8. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  9. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  10. *
  11. ********************************************************************
  12. */
  13.  
  14. #include "utypes.h"
  15. #include "nmfmapts.h"
  16.  
  17. #include "numfmt.h"
  18. #include "decimfmt.h"
  19. #include "locid.h"
  20.  
  21. // This is an API test, not a unit test.  It doesn't test very many cases, and doesn't
  22. // try to test the full functionality.  It just calls each function in the class and
  23. // verifies that it works on a basic level.
  24.  
  25. void IntlTestNumberFormatAPI::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  26. {
  27.     if (exec) logln("TestSuite NumberFormatAPI");
  28.     switch (index) {
  29.         case 0: name = "NumberFormat API test"; 
  30.                 if (exec) {
  31.                     logln("NumberFormat API test---"); logln("");
  32.                     UErrorCode status = U_ZERO_ERROR;
  33.                     Locale::setDefault(Locale::ENGLISH, status);
  34.                     if(U_FAILURE(status)) {
  35.                         errln("ERROR: Could not set default locale, test may not give correct results");
  36.                     }
  37.                     testAPI(par);
  38.                 }
  39.                 break;
  40.  
  41.         default: name = ""; break;
  42.     }
  43. }
  44.  
  45. /**
  46.  * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
  47.  * NumberFormat.
  48.  */
  49. void IntlTestNumberFormatAPI::testAPI(char *par)
  50. {
  51.     UErrorCode status = U_ZERO_ERROR;
  52.  
  53. // ======= Test constructors
  54.  
  55.     logln("Testing NumberFormat constructors");
  56.  
  57.     NumberFormat *def = NumberFormat::createInstance(status);
  58.     if(U_FAILURE(status)) {
  59.         errln("ERROR: Could not create NumberFormat (default)");
  60.     }
  61.  
  62.     status = U_ZERO_ERROR;
  63.     NumberFormat *fr = NumberFormat::createInstance(Locale::FRENCH, status);
  64.     if(U_FAILURE(status)) {
  65.         errln("ERROR: Could not create NumberFormat (French)");
  66.     }
  67.  
  68.     NumberFormat *cur = NumberFormat::createCurrencyInstance(status);
  69.     if(U_FAILURE(status)) {
  70.         errln("ERROR: Could not create NumberFormat (currency, default)");
  71.     }
  72.  
  73.     status = U_ZERO_ERROR;
  74.     NumberFormat *cur_fr = NumberFormat::createCurrencyInstance(Locale::FRENCH, status);
  75.     if(U_FAILURE(status)) {
  76.         errln("ERROR: Could not create NumberFormat (currency, French)");
  77.     }
  78.  
  79.     NumberFormat *per = NumberFormat::createPercentInstance(status);
  80.     if(U_FAILURE(status)) {
  81.         errln("ERROR: Could not create NumberFormat (percent, default)");
  82.     }
  83.  
  84.     status = U_ZERO_ERROR;
  85.     NumberFormat *per_fr = NumberFormat::createPercentInstance(Locale::FRENCH, status);
  86.     if(U_FAILURE(status)) {
  87.         errln("ERROR: Could not create NumberFormat (percent, French)");
  88.     }
  89.  
  90. // ======= Test equality
  91.  
  92.     logln("Testing equality operator");
  93.     
  94.     if( *per_fr == *cur_fr || ! ( *per_fr != *cur_fr) ) {
  95.         errln("ERROR: == failed");
  96.     }
  97.  
  98. // ======= Test various format() methods
  99.  
  100.     logln("Testing various format() methods");
  101.  
  102.     double d = -10456.0037;
  103.     int32_t l = 100000000;
  104.     Formattable fD(d);
  105.     Formattable fL(l);
  106.  
  107.     UnicodeString res1, res2, res3, res4, res5, res6;
  108.     FieldPosition pos1(0), pos2(0), pos3(0), pos4(0);
  109.     
  110.     res1 = cur_fr->format(d, res1);
  111.     logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1);
  112.  
  113.     res2 = cur_fr->format(l, res2);
  114.     logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2);
  115.  
  116.     res3 = cur_fr->format(d, res3, pos1);
  117.     logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res3);
  118.  
  119.     res4 = cur_fr->format(l, res4, pos2);
  120.     logln((UnicodeString) "" + (int32_t) l + " formatted to " + res4);
  121.  
  122.     status = U_ZERO_ERROR;
  123.     res5 = cur_fr->format(fD, res5, pos3, status);
  124.     if(U_FAILURE(status)) {
  125.         errln("ERROR: format(Formattable [double]) failed");
  126.     }
  127.     logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res5);
  128.  
  129.     status = U_ZERO_ERROR;
  130.     res6 = cur_fr->format(fL, res6, pos4, status);
  131.     if(U_FAILURE(status)) {
  132.         errln("ERROR: format(Formattable [long]) failed");
  133.     }
  134.     logln((UnicodeString) "" + fL.getLong() + " formatted to " + res6);
  135.  
  136.  
  137. // ======= Test parse()
  138.  
  139.     logln("Testing parse()");
  140.  
  141.     UnicodeString text("-10,456.0037");
  142.     Formattable result1, result2, result3;
  143.     ParsePosition pos(0), pos01(0);
  144.     fr->parseObject(text, result1, pos);
  145.     if(result1.getType() != Formattable::kDouble && result1.getDouble() != d) {
  146.         errln("ERROR: Roundtrip failed (via parse()) for " + text);
  147.     }
  148.     logln(text + " parsed into " + (int32_t) result1.getDouble());
  149.  
  150.     fr->parse(text, result2, pos01);
  151.     if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
  152.         errln("ERROR: Roundtrip failed (via parse()) for " + text);
  153.     }
  154.     logln(text + " parsed into " + (int32_t) result2.getDouble());
  155.  
  156.     status = U_ZERO_ERROR;
  157.     fr->parse(text, result3, status);
  158.     if(U_FAILURE(status)) {
  159.         errln("ERROR: parse() failed");
  160.     }
  161.     if(result3.getType() != Formattable::kDouble && result3.getDouble() != d) {
  162.         errln("ERROR: Roundtrip failed (via parse()) for " + text);
  163.     }
  164.     logln(text + " parsed into " + (int32_t) result3.getDouble());
  165.  
  166.  
  167. // ======= Test getters and setters
  168.  
  169.     logln("Testing getters and setters");
  170.  
  171.     int32_t count = 0;
  172.     const Locale *locales = NumberFormat::getAvailableLocales(count);
  173.     logln((UnicodeString) "Got " + count + " locales" );
  174.     for(int32_t i = 0; i < count; i++) {
  175.         UnicodeString name;
  176.         name = locales[i].getName(name);
  177.         logln(name);
  178.     }
  179.  
  180.     fr->setParseIntegerOnly( def->isParseIntegerOnly() );
  181.     if(fr->isParseIntegerOnly() != def->isParseIntegerOnly() ) {
  182.         errln("ERROR: setParseIntegerOnly() failed");
  183.     }
  184.  
  185.     fr->setGroupingUsed( def->isGroupingUsed() );
  186.     if(fr->isGroupingUsed() != def->isGroupingUsed() ) {
  187.         errln("ERROR: setGroupingUsed() failed");
  188.     }
  189.  
  190.     fr->setMaximumIntegerDigits( def->getMaximumIntegerDigits() );
  191.     if(fr->getMaximumIntegerDigits() != def->getMaximumIntegerDigits() ) {
  192.         errln("ERROR: setMaximumIntegerDigits() failed");
  193.     }
  194.  
  195.     fr->setMinimumIntegerDigits( def->getMinimumIntegerDigits() );
  196.     if(fr->getMinimumIntegerDigits() != def->getMinimumIntegerDigits() ) {
  197.         errln("ERROR: setMinimumIntegerDigits() failed");
  198.     }
  199.  
  200.     fr->setMaximumFractionDigits( def->getMaximumFractionDigits() );
  201.     if(fr->getMaximumFractionDigits() != def->getMaximumFractionDigits() ) {
  202.         errln("ERROR: setMaximumFractionDigits() failed");
  203.     }
  204.  
  205.     fr->setMinimumFractionDigits( def->getMinimumFractionDigits() );
  206.     if(fr->getMinimumFractionDigits() != def->getMinimumFractionDigits() ) {
  207.         errln("ERROR: setMinimumFractionDigits() failed");
  208.     }
  209.  
  210.  
  211. // ======= Test getStaticClassID()
  212.  
  213.     logln("Testing getStaticClassID()");
  214.  
  215.     status = U_ZERO_ERROR;
  216.     NumberFormat *test = new DecimalFormat(status);
  217.     if(U_FAILURE(status)) {
  218.         errln("ERROR: Couldn't create a NumberFormat");
  219.     }
  220.  
  221.     if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  222.         errln("ERROR: getDynamicClassID() didn't return the expected value");
  223.     }
  224.  
  225.     delete test;
  226.     delete def;
  227.     delete fr;
  228.     delete cur;
  229.     delete cur_fr;
  230.     delete per;
  231.     delete per_fr;
  232. }
  233.