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

  1.  
  2. /*
  3. ********************************************************************
  4. * COPYRIGHT: 
  5. * (C) Copyright Taligent, Inc., 1997
  6. * (C) Copyright International Business Machines Corporation, 1997 - 1998
  7. * Copyright (C) 1999 Alan Liu and others. All rights reserved.
  8. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  9. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  10. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  11. *
  12. ********************************************************************
  13. */
  14.  
  15. #include "utypes.h"
  16. #include "dtfmapts.h"
  17.  
  18. #include "datefmt.h"
  19. #include "smpdtfmt.h"
  20. #include "decimfmt.h"
  21. #include "choicfmt.h"
  22. #include "msgfmt.h"
  23.  
  24.  
  25. // This is an API test, not a unit test.  It doesn't test very many cases, and doesn't
  26. // try to test the full functionality.  It just calls each function in the class and
  27. // verifies that it works on a basic level.
  28.  
  29. void IntlTestDateFormatAPI::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  30. {
  31.     if (exec) logln("TestSuite DateFormatAPI");
  32.     switch (index) {
  33.         case 0: name = "DateFormat API test"; 
  34.                 if (exec) {
  35.                     logln("DateFormat API test---"); logln("");
  36.                     UErrorCode status = U_ZERO_ERROR;
  37.                     Locale::setDefault(Locale::ENGLISH, status);
  38.                     if(U_FAILURE(status)) {
  39.                         errln("ERROR: Could not set default locale, test may not give correct results");
  40.                     }
  41.                     testAPI(par);
  42.                 }
  43.                 break;
  44.  
  45.         case 1: name = "TestEquals"; 
  46.                 if (exec) {
  47.                     logln("TestEquals---"); logln("");
  48.                     TestEquals();
  49.                 }
  50.                 break;
  51.  
  52.         case 2: name = "TestNameHiding"; 
  53.                 if (exec) {
  54.                     logln("TestNameHiding---"); logln("");
  55.                     TestNameHiding();
  56.                 }
  57.                 break;
  58.  
  59.         default: name = ""; break;
  60.     }
  61. }
  62.  
  63. /**
  64.  * Test that the equals method works correctly.
  65.  */
  66. void IntlTestDateFormatAPI::TestEquals()
  67. {
  68.     UErrorCode status = U_ZERO_ERROR;
  69.     // Create two objects at different system times
  70.     DateFormat *a = DateFormat::createInstance();
  71.     UDate start = Calendar::getNow();
  72.     while (Calendar::getNow() == start) ; // Wait for time to change
  73.     DateFormat *b = DateFormat::createInstance();
  74.  
  75.     if (!(*a == *b))
  76.         errln("FAIL: DateFormat objects created at different times are unequal.");
  77.  
  78.     if (b->getDynamicClassID() == SimpleDateFormat::getStaticClassID())
  79.     {
  80.         double ONE_YEAR = 365*24*60*60*1000.0;
  81.         ((SimpleDateFormat*)b)->set2DigitYearStart(start + 50*ONE_YEAR, status);
  82.         if (U_FAILURE(status))
  83.             errln("FAIL: setTwoDigitStartDate failed.");
  84.         else if (*a == *b)
  85.             errln("FAIL: DateFormat objects with different two digit start dates are equal.");
  86.     }
  87.     delete a;
  88.     delete b;
  89. }
  90.  
  91. /**
  92.  * This test checks various generic API methods in DateFormat to achieve 100%
  93.  * API coverage.
  94.  */
  95. void IntlTestDateFormatAPI::testAPI(char *par)
  96. {
  97.     UErrorCode status = U_ZERO_ERROR;
  98.  
  99. // ======= Test constructors
  100.  
  101.     logln("Testing DateFormat constructors");
  102.  
  103.     DateFormat *def = DateFormat::createInstance();
  104.     DateFormat *fr = DateFormat::createTimeInstance(DateFormat::FULL, Locale::FRENCH);
  105.     DateFormat *it = DateFormat::createDateInstance(DateFormat::MEDIUM, Locale::ITALIAN);
  106.     DateFormat *de = DateFormat::createDateTimeInstance(DateFormat::LONG, DateFormat::LONG, Locale::GERMAN);
  107.  
  108. // ======= Test equality
  109.  
  110.     logln("Testing equality operator");
  111.     
  112.     if( *fr == *it ) {
  113.         errln("ERROR: == failed");
  114.     }
  115.  
  116. // ======= Test various format() methods
  117.  
  118.     logln("Testing various format() methods");
  119.  
  120.     UDate d = 837039928046.0;
  121.     Formattable fD(d, Formattable::kIsDate);
  122.  
  123.     UnicodeString res1, res2, res3;
  124.     FieldPosition pos1(0), pos2(0);
  125.     
  126.     status = U_ZERO_ERROR;
  127.     res1 = fr->format(d, res1, pos1, status);
  128.     if(U_FAILURE(status)) {
  129.         errln("ERROR: format() failed (French)");
  130.     }
  131.     logln( (UnicodeString) "" + d + " formatted to " + res1);
  132.  
  133.     res2 = it->format(d, res2, pos2);
  134.     logln( (UnicodeString) "" + d + " formatted to " + res2);
  135.  
  136.     res3 = de->format(d, res3);
  137.     logln( (UnicodeString) "" + d + " formatted to " + res3);
  138.  
  139. // ======= Test parse()
  140.  
  141.     logln("Testing parse()");
  142.  
  143.     UnicodeString text("02/03/76 2:50 AM, CST");
  144.     Formattable result1;
  145.     UDate result2, result3;
  146.     ParsePosition pos(0), pos01(0);
  147.     def->parseObject(text, result1, pos);
  148.     if(result1.getType() != Formattable::kDate) {
  149.         errln("ERROR: parseObject() failed for " + text);
  150.     }
  151.     logln(text + " parsed into " + result1.getDate());
  152.  
  153.     status = U_ZERO_ERROR;
  154.     result2 = def->parse(text, status);
  155.     if(U_FAILURE(status)) {
  156.         errln("ERROR: parse() failed");
  157.     }
  158.     logln(text + " parsed into " + result2);
  159.  
  160.     result3 = def->parse(text, pos01);
  161.     logln(text + " parsed into " + result3);
  162.  
  163.  
  164. // ======= Test getters and setters
  165.  
  166.     logln("Testing getters and setters");
  167.  
  168.     int32_t count = 0;
  169.     const Locale *locales = DateFormat::getAvailableLocales(count);
  170.     logln((UnicodeString) "Got " + count + " locales" );
  171.     for(int32_t i = 0; i < count; i++) {
  172.         UnicodeString name;
  173.         name = locales[i].getName(name);
  174.         logln(name);
  175.     }
  176.  
  177.     fr->setLenient(it->isLenient());
  178.     if(fr->isLenient() != it->isLenient()) {
  179.         errln("ERROR: setLenient() failed");
  180.     }
  181.  
  182.     const Calendar *cal = def->getCalendar();
  183.     Calendar *newCal = cal->clone();
  184.     de->adoptCalendar(newCal);  
  185.     it->setCalendar(*newCal);
  186.     if( *(de->getCalendar()) != *(it->getCalendar())) {
  187.         errln("ERROR: adopt or set Calendar() failed");
  188.     }
  189.  
  190.     const NumberFormat *nf = def->getNumberFormat();
  191.     NumberFormat *newNf = (NumberFormat*) nf->clone();
  192.     de->adoptNumberFormat(newNf);   
  193.     it->setNumberFormat(*newNf);
  194.     if( *(de->getNumberFormat()) != *(it->getNumberFormat())) {
  195.         errln("ERROR: adopt or set NumberFormat() failed");
  196.     }
  197.  
  198.     const TimeZone& tz = def->getTimeZone();
  199.     TimeZone *newTz = tz.clone();
  200.     de->adoptTimeZone(newTz);   
  201.     it->setTimeZone(*newTz);
  202.     if( de->getTimeZone() != it->getTimeZone()) {
  203.         errln("ERROR: adopt or set TimeZone() failed");
  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 DateFormat");
  214.     }
  215.  
  216.     if(test->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
  217.         errln("ERROR: getDynamicClassID() didn't return the expected value");
  218.     }
  219.  
  220.     delete test;
  221.     delete def;
  222.     delete fr;
  223.     delete it;
  224.     delete de;
  225. }
  226.  
  227. /**
  228.  * Test hiding of parse() and format() APIs in the Format hierarchy.
  229.  * We test the entire hierarchy, even though this test is located in
  230.  * the DateFormat API test.
  231.  */
  232. void
  233. IntlTestDateFormatAPI::TestNameHiding() {
  234.  
  235.     // N.B.: This test passes if it COMPILES, since it's a test of
  236.     // compile-time name hiding.
  237.  
  238.     UErrorCode status = U_ZERO_ERROR;
  239.     Formattable dateObj(0, Formattable::kIsDate);
  240.     Formattable numObj(3.1415926535897932384626433832795);
  241.     Formattable obj;
  242.     UnicodeString str;
  243.     FieldPosition fpos;
  244.     ParsePosition ppos;
  245.  
  246.     // DateFormat calling Format API
  247.     {
  248.         logln("DateFormat");
  249.         DateFormat *dateFmt = DateFormat::createInstance();
  250.         if (dateFmt) {
  251.             dateFmt->format(dateObj, str, status);
  252.             dateFmt->format(dateObj, str, fpos, status);
  253.             delete dateFmt;
  254.         } else {
  255.             errln("FAIL: Can't create DateFormat");
  256.         }
  257.     }
  258.  
  259.     // SimpleDateFormat calling Format & DateFormat API
  260.     {
  261.         logln("SimpleDateFormat");
  262.         status = U_ZERO_ERROR;
  263.         SimpleDateFormat sdf(status);
  264.         // Format API
  265.         sdf.format(dateObj, str, status);
  266.         sdf.format(dateObj, str, fpos, status);
  267.         // DateFormat API
  268.         sdf.format((UDate)0, str, fpos);
  269.         sdf.format((UDate)0, str);
  270.         sdf.parse(str, status);
  271.         sdf.parse(str, ppos);
  272.     }
  273.  
  274.     // NumberFormat calling Format API
  275.     {
  276.         logln("NumberFormat");
  277.         status = U_ZERO_ERROR;
  278.         NumberFormat *fmt = NumberFormat::createInstance(status);
  279.         if (fmt) {
  280.             fmt->format(numObj, str, status);
  281.             fmt->format(numObj, str, fpos, status);
  282.             delete fmt;
  283.         } else {
  284.             errln("FAIL: Can't create NumberFormat");
  285.         }
  286.     }
  287.  
  288.     // DecimalFormat calling Format & NumberFormat API
  289.     {
  290.         logln("DecimalFormat");
  291.         status = U_ZERO_ERROR;
  292.         DecimalFormat fmt(status);
  293.         // Format API
  294.         fmt.format(numObj, str, status);
  295.         fmt.format(numObj, str, fpos, status);
  296.         // NumberFormat API
  297.         fmt.format(2.71828, str);
  298.         fmt.format((int32_t)1234567, str);
  299.         fmt.format(1.41421, str, fpos);
  300.         fmt.format((int32_t)9876543, str, fpos);
  301.         fmt.parse(str, obj, ppos);
  302.         fmt.parse(str, obj, status);
  303.     }
  304.  
  305.     // ChoiceFormat calling Format & NumberFormat API
  306.     {
  307.         logln("ChoiceFormat");
  308.         status = U_ZERO_ERROR;
  309.         ChoiceFormat fmt("0#foo|1#foos|2#foos", status);
  310.         // Format API
  311.         fmt.format(numObj, str, status);
  312.         fmt.format(numObj, str, fpos, status);
  313.         // NumberFormat API
  314.         fmt.format(2.71828, str);
  315.         fmt.format((int32_t)1234567, str);
  316.         fmt.format(1.41421, str, fpos);
  317.         fmt.format((int32_t)9876543, str, fpos);
  318.         fmt.parse(str, obj, ppos);
  319.         fmt.parse(str, obj, status);
  320.     }
  321.  
  322.     // MessageFormat calling Format API
  323.     {
  324.         logln("MessageFormat");
  325.         status = U_ZERO_ERROR;
  326.         MessageFormat fmt("", status);
  327.         // Format API
  328.         // We use dateObj, which MessageFormat should reject.
  329.         // We're testing name hiding, not the format method.
  330.         fmt.format(dateObj, str, status);
  331.         fmt.format(dateObj, str, fpos, status);
  332.     }
  333. }
  334.