home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / sdtfmtts.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  6.5 KB  |  222 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 "sdtfmtts.h"
  16.  
  17. #include "smpdtfmt.h"
  18. #include "dtfmtsym.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 IntlTestSimpleDateFormatAPI::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  25. {
  26.     if (exec) logln("TestSuite SimpleDateFormatAPI");
  27.     switch (index) {
  28.         case 0: name = "SimpleDateFormat API test"; 
  29.                 if (exec) {
  30.                     logln("SimpleDateFormat API test---"); logln("");
  31.                     UErrorCode status = U_ZERO_ERROR;
  32.                     Locale::setDefault(Locale::ENGLISH, status);
  33.                     if(U_FAILURE(status)) {
  34.                         errln("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.  * Test various generic API methods of SimpleDateFormat for API coverage.
  46.  */
  47. void IntlTestSimpleDateFormatAPI::testAPI(char *par)
  48. {
  49.     UErrorCode status = U_ZERO_ERROR;
  50.  
  51. // ======= Test constructors
  52.  
  53.     logln("Testing SimpleDateFormat constructors");
  54.  
  55.     SimpleDateFormat def(status);
  56.     if(U_FAILURE(status)) {
  57.         errln("ERROR: Could not create SimpleDateFormat (default)");
  58.     }
  59.  
  60.     status = U_ZERO_ERROR;
  61.     const UnicodeString pattern("yyyy.MM.dd G 'at' hh:mm:ss z");
  62.     SimpleDateFormat pat(pattern, status);
  63.     if(U_FAILURE(status)) {
  64.         errln("ERROR: Could not create SimpleDateFormat (pattern)");
  65.     }
  66.  
  67.     status = U_ZERO_ERROR;
  68.     SimpleDateFormat pat_fr(pattern, Locale::FRENCH, status);
  69.     if(U_FAILURE(status)) {
  70.         errln("ERROR: Could not create SimpleDateFormat (pattern French)");
  71.     }
  72.  
  73.     status = U_ZERO_ERROR;
  74.     DateFormatSymbols *symbols = new DateFormatSymbols(Locale::FRENCH, status);
  75.     if(U_FAILURE(status)) {
  76.         errln("ERROR: Could not create DateFormatSymbols (French)");
  77.     }
  78.  
  79.     status = U_ZERO_ERROR;
  80.     SimpleDateFormat cust1(pattern, symbols, status);
  81.     if(U_FAILURE(status)) {
  82.         errln("ERROR: Could not create SimpleDateFormat (pattern, symbols*)");
  83.     }
  84.  
  85.     status = U_ZERO_ERROR;
  86.     SimpleDateFormat cust2(pattern, *symbols, status);
  87.     if(U_FAILURE(status)) {
  88.         errln("ERROR: Could not create SimpleDateFormat (pattern, symbols)");
  89.     }
  90.  
  91.     SimpleDateFormat copy(pat);
  92.  
  93. // ======= Test clone(), assignment, and equality
  94.  
  95.     logln("Testing clone(), assignment and equality operators");
  96.  
  97.     if( ! (copy == pat) || copy != pat) {
  98.         errln("ERROR: Copy constructor (or ==) failed");
  99.     }
  100.  
  101.     copy = cust1;
  102.     if(copy != cust1) {
  103.         errln("ERROR: Assignment (or !=) failed");
  104.     }
  105.  
  106.     Format *clone = def.clone();
  107.     if( ! (*clone == def) ) {
  108.         errln("ERROR: Clone() (or ==) failed");
  109.     }
  110.     delete clone;
  111.  
  112. // ======= Test various format() methods
  113.  
  114.     logln("Testing various format() methods");
  115.  
  116.     UDate d = 837039928046.0;
  117.     Formattable fD(d, Formattable::kIsDate);
  118.  
  119.     UnicodeString res1, res2;
  120.     FieldPosition pos1(0), pos2(0);
  121.     
  122.     res1 = def.format(d, res1, pos1);
  123.     logln( (UnicodeString) "" + d + " formatted to " + res1);
  124.  
  125.     status = U_ZERO_ERROR;
  126.     res2 = cust1.format(fD, res2, pos2, status);
  127.     if(U_FAILURE(status)) {
  128.         errln("ERROR: format(Formattable [Date]) failed");
  129.     }
  130.     logln((UnicodeString) "" + fD.getDate() + " formatted to " + res2);
  131.  
  132. // ======= Test parse()
  133.  
  134.     logln("Testing parse()");
  135.  
  136.     UnicodeString text("02/03/76 2:50 AM, CST");
  137.     UDate result1, result2;
  138.     ParsePosition pos(0);
  139.     result1 = def.parse(text, pos);
  140.     logln(text + " parsed into " + result1);
  141.  
  142.     status = U_ZERO_ERROR;
  143.     result2 = def.parse(text, status);
  144.     if(U_FAILURE(status)) {
  145.         errln("ERROR: parse() failed");
  146.     }
  147.     logln(text + " parsed into " + result2);
  148.  
  149. // ======= Test getters and setters
  150.  
  151.     logln("Testing getters and setters");
  152.  
  153.     const DateFormatSymbols *syms = pat.getDateFormatSymbols();
  154.     DateFormatSymbols *newSyms = new DateFormatSymbols(*syms);
  155.     def.adoptDateFormatSymbols(newSyms);    
  156.     pat_fr.setDateFormatSymbols(*newSyms);
  157.     if( *(pat.getDateFormatSymbols()) != *(def.getDateFormatSymbols())) {
  158.         errln("ERROR: adopt or set DateFormatSymbols() failed");
  159.     }
  160.  
  161.     status = U_ZERO_ERROR;
  162.     UDate startDate = pat.get2DigitYearStart(status);
  163.     if(U_FAILURE(status)) {
  164.         errln("ERROR: getTwoDigitStartDate() failed");
  165.     }
  166.     
  167.     status = U_ZERO_ERROR;
  168.     pat_fr.set2DigitYearStart(startDate, status);
  169.     if(U_FAILURE(status)) {
  170.         errln("ERROR: setTwoDigitStartDate() failed");
  171.     }
  172.  
  173. // ======= Test applyPattern()
  174.  
  175.     logln("Testing applyPattern()");
  176.  
  177.     UnicodeString p1("yyyy.MM.dd G 'at' hh:mm:ss z");
  178.     logln("Applying pattern " + p1);
  179.     status = U_ZERO_ERROR;
  180.     pat.applyPattern(p1);
  181.  
  182.     UnicodeString s2;
  183.     s2 = pat.toPattern(s2);
  184.     logln("Extracted pattern is " + s2);
  185.     if(s2 != p1) {
  186.         errln("ERROR: toPattern() result did not match pattern applied");
  187.     }
  188.  
  189.     logln("Applying pattern " + p1);
  190.     status = U_ZERO_ERROR;
  191.     pat.applyLocalizedPattern(p1, status);
  192.     if(U_FAILURE(status)) {
  193.         errln("ERROR: applyPattern() failed with " + (int32_t) status);
  194.     }
  195.     UnicodeString s3;
  196.     status = U_ZERO_ERROR;
  197.     s3 = pat.toLocalizedPattern(s3, status);
  198.     if(U_FAILURE(status)) {
  199.         errln("ERROR: toLocalizedPattern() failed");
  200.     }
  201.     logln("Extracted pattern is " + s3);
  202.     if(s3 != p1) {
  203.         errln("ERROR: toLocalizedPattern() result did not match pattern applied");
  204.     }
  205.  
  206. // ======= Test getStaticClassID()
  207.  
  208.     logln("Testing getStaticClassID()");
  209.  
  210.     status = U_ZERO_ERROR;
  211.     DateFormat *test = new SimpleDateFormat(status);
  212.     if(U_FAILURE(status)) {
  213.         errln("ERROR: Couldn't create a SimpleDateFormat");
  214.     }
  215.  
  216.     if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
  217.         errln("ERROR: getDynamicClassID() didn't return the expected value");
  218.     }
  219.  
  220.     delete test;
  221. }
  222.