home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / dcfmapts.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  9.6 KB  |  291 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 "dcfmapts.h"
  16.  
  17. #include "decimfmt.h"
  18. #include "dcfmtsym.h"
  19.  
  20. // This is an API test, not a unit test.  It doesn't test very many cases, and doesn't
  21. // try to test the full functionality.  It just calls each function in the class and
  22. // verifies that it works on a basic level.
  23.  
  24. void IntlTestDecimalFormatAPI::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  25. {
  26.     if (exec) logln((UnicodeString)"TestSuite DecimalFormatAPI");
  27.     switch (index) {
  28.         case 0: name = "DecimalFormat API test"; 
  29.                 if (exec) {
  30.                     logln((UnicodeString)"DecimalFormat API test---"); logln((UnicodeString)"");
  31.                     UErrorCode status = U_ZERO_ERROR;
  32.                     Locale::setDefault(Locale::ENGLISH, status);
  33.                     if(U_FAILURE(status)) {
  34.                         errln((UnicodeString)"ERROR: Could not set default locale, test may not give correct results");
  35.                     }
  36.                     testAPI(par);
  37.                 }
  38.                 break;
  39.  
  40.         default: name = ""; break;
  41.     }
  42. }
  43.  
  44. /**
  45.  * This test checks various generic API methods in DecimalFormat to achieve 100%
  46.  * API coverage.
  47.  */
  48. void IntlTestDecimalFormatAPI::testAPI(char *par)
  49. {
  50.     UErrorCode status = U_ZERO_ERROR;
  51.  
  52. // ======= Test constructors
  53.  
  54.     logln((UnicodeString)"Testing DecimalFormat constructors");
  55.  
  56.     DecimalFormat def(status);
  57.     if(U_FAILURE(status)) {
  58.         errln((UnicodeString)"ERROR: Could not create DecimalFormat (default)");
  59.     }
  60.  
  61.     status = U_ZERO_ERROR;
  62.     const UnicodeString pattern("#,##0.# FF");
  63.     DecimalFormat pat(pattern, status);
  64.     if(U_FAILURE(status)) {
  65.         errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern)");
  66.     }
  67.  
  68.     status = U_ZERO_ERROR;
  69.     DecimalFormatSymbols *symbols = new DecimalFormatSymbols(Locale::FRENCH, status);
  70.     if(U_FAILURE(status)) {
  71.         errln((UnicodeString)"ERROR: Could not create DecimalFormatSymbols (French)");
  72.     }
  73.  
  74.     status = U_ZERO_ERROR;
  75.     DecimalFormat cust1(pattern, symbols, status);
  76.     if(U_FAILURE(status)) {
  77.         errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern, symbols*)");
  78.     }
  79.  
  80.     status = U_ZERO_ERROR;
  81.     DecimalFormat cust2(pattern, *symbols, status);
  82.     if(U_FAILURE(status)) {
  83.         errln((UnicodeString)"ERROR: Could not create DecimalFormat (pattern, symbols)");
  84.     }
  85.  
  86.     DecimalFormat copy(pat);
  87.  
  88. // ======= Test clone(), assignment, and equality
  89.  
  90.     logln((UnicodeString)"Testing clone(), assignment and equality operators");
  91.  
  92.     if( ! (copy == pat) || copy != pat) {
  93.         errln((UnicodeString)"ERROR: Copy constructor or == failed");
  94.     }
  95.  
  96.     copy = cust1;
  97.     if(copy != cust1) {
  98.         errln((UnicodeString)"ERROR: Assignment (or !=) failed");
  99.     }
  100.  
  101.     Format *clone = def.clone();
  102.     if( ! (*clone == def) ) {
  103.         errln((UnicodeString)"ERROR: Clone() failed");
  104.     }
  105.     delete clone;
  106.  
  107. // ======= Test various format() methods
  108.  
  109.     logln((UnicodeString)"Testing various format() methods");
  110.  
  111.     double d = -10456.0037;
  112.     int32_t l = 100000000;
  113.     Formattable fD(d);
  114.     Formattable fL(l);
  115.  
  116.     UnicodeString res1, res2, res3, res4;
  117.     FieldPosition pos1(0), pos2(0), pos3(0), pos4(0);
  118.     
  119.     res1 = def.format(d, res1, pos1);
  120.     logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1);
  121.  
  122.     res2 = pat.format(l, res2, pos2);
  123.     logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2);
  124.  
  125.     status = U_ZERO_ERROR;
  126.     res3 = cust1.format(fD, res3, pos3, status);
  127.     if(U_FAILURE(status)) {
  128.         errln((UnicodeString)"ERROR: format(Formattable [double]) failed");
  129.     }
  130.     logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res3);
  131.  
  132.     status = U_ZERO_ERROR;
  133.     res4 = cust2.format(fL, res4, pos4, status);
  134.     if(U_FAILURE(status)) {
  135.         errln((UnicodeString)"ERROR: format(Formattable [long]) failed");
  136.     }
  137.     logln((UnicodeString) "" + fL.getLong() + " formatted to " + res4);
  138.  
  139. // ======= Test parse()
  140.  
  141.     logln((UnicodeString)"Testing parse()");
  142.  
  143.     UnicodeString text("-10,456.0037");
  144.     Formattable result1, result2;
  145.     ParsePosition pos(0);
  146.     UnicodeString patt("#,##0.#");
  147.     status = U_ZERO_ERROR;
  148.     pat.applyPattern(patt, status);
  149.     if(U_FAILURE(status)) {
  150.         errln((UnicodeString)"ERROR: applyPattern() failed");
  151.     }
  152.     pat.parse(text, result1, pos);
  153.     if(result1.getType() != Formattable::kDouble && result1.getDouble() != d) {
  154.         errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text);
  155.     }
  156.     logln(text + " parsed into " + (int32_t) result1.getDouble());
  157.  
  158.     status = U_ZERO_ERROR;
  159.     pat.parse(text, result2, status);
  160.     if(U_FAILURE(status)) {
  161.         errln((UnicodeString)"ERROR: parse() failed");
  162.     }
  163.     if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
  164.         errln((UnicodeString)"ERROR: Roundtrip failed (via parse()) for " + text);
  165.     }
  166.     logln(text + " parsed into " + (int32_t) result2.getDouble());
  167.  
  168. // ======= Test getters and setters
  169.  
  170.     logln((UnicodeString)"Testing getters and setters");
  171.  
  172.     const DecimalFormatSymbols *syms = pat.getDecimalFormatSymbols();
  173.     DecimalFormatSymbols *newSyms = new DecimalFormatSymbols(*syms);
  174.     def.adoptDecimalFormatSymbols(newSyms); 
  175.     def.setDecimalFormatSymbols(*newSyms);
  176.     if( *(pat.getDecimalFormatSymbols()) != *(def.getDecimalFormatSymbols())) {
  177.         errln((UnicodeString)"ERROR: adopt or set DecimalFormatSymbols() failed");
  178.     }
  179.  
  180.     UnicodeString posPrefix;
  181.     pat.setPositivePrefix("+");
  182.     posPrefix = pat.getPositivePrefix(posPrefix);
  183.     logln((UnicodeString)"Positive prefix (should be +): " + posPrefix);
  184.     if(posPrefix != "+") {
  185.         errln((UnicodeString)"ERROR: setPositivePrefix() failed");
  186.     }
  187.  
  188.     UnicodeString negPrefix;
  189.     pat.setNegativePrefix("-");
  190.     negPrefix = pat.getNegativePrefix(negPrefix);
  191.     logln((UnicodeString)"Negative prefix (should be -): " + negPrefix);
  192.     if(negPrefix != "-") {
  193.         errln((UnicodeString)"ERROR: setNegativePrefix() failed");
  194.     }
  195.  
  196.     UnicodeString posSuffix;
  197.     pat.setPositiveSuffix("_");
  198.     posSuffix = pat.getPositiveSuffix(posSuffix);
  199.     logln((UnicodeString)"Positive suffix (should be _): " + posSuffix);
  200.     if(posSuffix != "_") {
  201.         errln((UnicodeString)"ERROR: setPositiveSuffix() failed");
  202.     }
  203.  
  204.     UnicodeString negSuffix;
  205.     pat.setNegativeSuffix("~");
  206.     negSuffix = pat.getNegativeSuffix(negSuffix);
  207.     logln((UnicodeString)"Negative suffix (should be ~): " + negSuffix);
  208.     if(negSuffix != "~") {
  209.         errln((UnicodeString)"ERROR: setNegativeSuffix() failed");
  210.     }
  211.  
  212.     int32_t multiplier = 0;
  213.     pat.setMultiplier(8);
  214.     multiplier = pat.getMultiplier();
  215.     logln((UnicodeString)"Multiplier (should be 8): " + multiplier);
  216.     if(multiplier != 8) {
  217.         errln((UnicodeString)"ERROR: setMultiplier() failed");
  218.     }
  219.  
  220.     int32_t groupingSize = 0;
  221.     pat.setGroupingSize(2);
  222.     groupingSize = pat.getGroupingSize();
  223.     logln((UnicodeString)"Grouping size (should be 2): " + (int32_t) groupingSize);
  224.     if(groupingSize != 2) {
  225.         errln((UnicodeString)"ERROR: setGroupingSize() failed");
  226.     }
  227.  
  228.     pat.setDecimalSeparatorAlwaysShown(TRUE);
  229.     bool_t tf = pat.isDecimalSeparatorAlwaysShown();
  230.     logln((UnicodeString)"DecimalSeparatorIsAlwaysShown (should be TRUE) is " + (UnicodeString) (tf ? "TRUE" : "FALSE"));
  231.     if(tf != TRUE) {
  232.         errln((UnicodeString)"ERROR: setDecimalSeparatorAlwaysShown() failed");
  233.     }
  234.  
  235.     UnicodeString funkyPat;
  236.     funkyPat = pat.toPattern(funkyPat);
  237.     logln((UnicodeString)"Pattern is " + funkyPat);
  238.  
  239.     UnicodeString locPat;
  240.     locPat = pat.toLocalizedPattern(locPat);
  241.     logln((UnicodeString)"Localized pattern is " + locPat);
  242.  
  243. // ======= Test applyPattern()
  244.  
  245.     logln((UnicodeString)"Testing applyPattern()");
  246.  
  247.     UnicodeString p1("#,##0.0#;(#,##0.0#)");
  248.     logln((UnicodeString)"Applying pattern " + p1);
  249.     status = U_ZERO_ERROR;
  250.     pat.applyPattern(p1, status);
  251.     if(U_FAILURE(status)) {
  252.         errln((UnicodeString)"ERROR: applyPattern() failed with " + (int32_t) status);
  253.     }
  254.     UnicodeString s2;
  255.     s2 = pat.toPattern(s2);
  256.     logln((UnicodeString)"Extracted pattern is " + s2);
  257.     if(s2 != p1) {
  258.         errln((UnicodeString)"ERROR: toPattern() result did not match pattern applied");
  259.     }
  260.  
  261.     UnicodeString p2("#,##0.0# FF;(#,##0.0# FF)");
  262.     logln((UnicodeString)"Applying pattern " + p2);
  263.     status = U_ZERO_ERROR;
  264.     pat.applyLocalizedPattern(p2, status);
  265.     if(U_FAILURE(status)) {
  266.         errln((UnicodeString)"ERROR: applyPattern() failed with " + (int32_t) status);
  267.     }
  268.     UnicodeString s3;
  269.     s3 = pat.toLocalizedPattern(s3);
  270.     logln((UnicodeString)"Extracted pattern is " + s3);
  271.     if(s3 != p2) {
  272.         errln((UnicodeString)"ERROR: toLocalizedPattern() result did not match pattern applied");
  273.     }
  274.  
  275. // ======= Test getStaticClassID()
  276.  
  277.     logln((UnicodeString)"Testing getStaticClassID()");
  278.  
  279.     status = U_ZERO_ERROR;
  280.     NumberFormat *test = new DecimalFormat(status);
  281.     if(U_FAILURE(status)) {
  282.         errln((UnicodeString)"ERROR: Couldn't create a DecimalFormat");
  283.     }
  284.  
  285.     if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  286.         errln((UnicodeString)"ERROR: getDynamicClassID() didn't return the expected value");
  287.     }
  288.  
  289.     delete test;
  290. }
  291.