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

  1. /*
  2. ********************************************************************
  3. * COPYRIGHT: 
  4. * (C) Copyright International Business Machines Corporation, 1998
  5. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  6. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  7. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  8. *
  9. ********************************************************************
  10. */
  11.  
  12. #include "dtfmtrtts.h"
  13.  
  14. #include <stdio.h>
  15.  
  16. #include "datefmt.h"
  17. #include "smpdtfmt.h"
  18. #include "gregocal.h"
  19.  
  20.  
  21. // *****************************************************************************
  22. // class DateFormatRoundTripTest
  23. // *****************************************************************************
  24.  
  25. // Useful for turning up subtle bugs: Change the following to TRUE, recompile,
  26. // and run while at lunch.
  27. bool_t DateFormatRoundTripTest::INFINITE = FALSE; // Warning -- makes test run infinite loop!!!
  28.  
  29. // If SPARSENESS is > 0, we don't run each exhaustive possibility.
  30. // There are 24 total possible tests per each locale.  A SPARSENESS
  31. // of 12 means we run half of them.  A SPARSENESS of 23 means we run
  32. // 1 of them.  SPARSENESS _must_ be in the range 0..23.
  33. int32_t DateFormatRoundTripTest::SPARSENESS = 18;
  34. int32_t DateFormatRoundTripTest::TRIALS = 4;
  35. int32_t DateFormatRoundTripTest::DEPTH = 5;
  36.  
  37.  
  38. #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
  39.  
  40. void 
  41. DateFormatRoundTripTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  42. {
  43.     // if (exec) logln((UnicodeString)"TestSuite NumberFormatRegressionTest");
  44.     switch (index) {
  45.         CASE(0,TestDateFormatRoundTrip)
  46.         default: name = ""; break;
  47.     }
  48. }
  49.  
  50. bool_t 
  51. DateFormatRoundTripTest::failure(UErrorCode status, const char* msg)
  52. {
  53.     if(U_FAILURE(status)) {
  54.         errln(UnicodeString("FAIL: ") + msg + " failed, error " + errorName(status));
  55.         return TRUE;
  56.     }
  57.  
  58.     return FALSE;
  59. }
  60.  
  61. // ==
  62.  
  63. void DateFormatRoundTripTest::TestDateFormatRoundTrip() 
  64. {
  65.     UErrorCode status = U_ZERO_ERROR;
  66.     dateFormat = new SimpleDateFormat("EEE MMM dd HH:mm:ss.SSS zzz yyyy G", status);
  67.     failure(status, "new SimpleDateFormat");
  68.  
  69.     getFieldCal = Calendar::createInstance(status);
  70.     failure(status, "Calendar::createInstance");
  71.  
  72.  
  73.     int32_t locCount = 0;
  74.     const Locale *avail = DateFormat::getAvailableLocales(locCount);
  75.     logln("DateFormat available locales: " + locCount);
  76.     if(quick) {
  77.         if(locCount > 5)
  78.             locCount = 5;
  79.         logln("Quick mode: only testing first 5 Locales");
  80.     }
  81.     TimeZone *tz = TimeZone::createDefault();
  82.     UnicodeString temp;
  83.     logln("Default TimeZone:             " + tz->getID(temp));
  84.     delete tz;
  85.  
  86.     if (INFINITE) {
  87.         // Special infinite loop test mode for finding hard to reproduce errors
  88.         Locale loc = Locale::getDefault();
  89.         logln("ENTERING INFINITE TEST LOOP FOR Locale: " + loc.getDisplayName(temp));
  90.         for(;;) 
  91.             test(loc);
  92.     }
  93.     else {
  94.         test(Locale::getDefault());
  95.  
  96.         for (int i=0; i < locCount; ++i) {
  97.             test(avail[i]);
  98.         }
  99.     }
  100.  
  101.     delete dateFormat;
  102.     delete getFieldCal;
  103. }
  104.  
  105. void DateFormatRoundTripTest::test(const Locale& loc) 
  106. {
  107.     UnicodeString temp;
  108.     if( ! INFINITE) 
  109.         logln("Locale: " + loc.getDisplayName(temp));
  110.  
  111.     // Total possibilities = 24
  112.     //  4 date
  113.     //  4 time
  114.     //  16 date-time
  115.     bool_t TEST_TABLE [24];//= new boolean[24];
  116.     int32_t i = 0;
  117.     for(i = 0; i < 24; ++i) 
  118.         TEST_TABLE[i] = TRUE;
  119.  
  120.     // If we have some sparseness, implement it here.  Sparseness decreases
  121.     // test time by eliminating some tests, up to 23.
  122.     for(i = 0; i < SPARSENESS; ) {
  123.         int random = (int)(randFraction() * 24);
  124.         if (random >= 0 && random < 24 && TEST_TABLE[i]) {
  125.             TEST_TABLE[i] = FALSE;
  126.             ++i;
  127.         }
  128.     }
  129.  
  130.     int32_t itable = 0;
  131.     int32_t style = 0;
  132.     for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) {
  133.         if(TEST_TABLE[itable++]) {
  134.             DateFormat *df = DateFormat::createDateInstance((DateFormat::EStyle)style, loc);
  135.             test(df);
  136.             delete df;
  137.         }
  138.     }
  139.     
  140.     for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) {
  141.         if (TEST_TABLE[itable++]) {
  142.             DateFormat *df = DateFormat::createTimeInstance((DateFormat::EStyle)style, loc);
  143.             test(df, TRUE);
  144.             delete df;
  145.         }
  146.     }
  147.     
  148.     for(int32_t dstyle = DateFormat::FULL; dstyle <= DateFormat::SHORT; ++dstyle) {
  149.         for(int32_t tstyle = DateFormat::FULL; tstyle <= DateFormat::SHORT; ++tstyle) {
  150.             if(TEST_TABLE[itable++]) {
  151.                 DateFormat *df = DateFormat::createDateTimeInstance((DateFormat::EStyle)dstyle, (DateFormat::EStyle)tstyle, loc);
  152.                 test(df);
  153.                 delete df;
  154.             }
  155.         }
  156.     }
  157. }
  158.  
  159. void DateFormatRoundTripTest::test(DateFormat *fmt, bool_t timeOnly) 
  160. {
  161.     UnicodeString pat;
  162.     if(fmt->getDynamicClassID() != SimpleDateFormat::getStaticClassID()) {
  163.         errln("DateFormat wasn't a SimpleDateFormat");
  164.         return;
  165.     }
  166.  
  167.     pat = ((SimpleDateFormat*)fmt)->toPattern(pat);
  168.  
  169.     // NOTE TO MAINTAINER
  170.     // This indexOf check into the pattern needs to be refined to ignore
  171.     // quoted characters.  Currently, this isn't a problem with the locale
  172.     // patterns we have, but it may be a problem later.
  173.  
  174.     bool_t hasEra = (pat.indexOf(UnicodeString("G")) != -1);
  175.     bool_t hasZone = (pat.indexOf(UnicodeString("z")) != -1);
  176.  
  177.     // Because patterns contain incomplete data representing the Date,
  178.     // we must be careful of how we do the roundtrip.  We start with
  179.     // a randomly generated Date because they're easier to generate.
  180.     // From this we get a string.  The string is our real starting point,
  181.     // because this string should parse the same way all the time.  Note
  182.     // that it will not necessarily parse back to the original date because
  183.     // of incompleteness in patterns.  For example, a time-only pattern won't
  184.     // parse back to the same date.
  185.  
  186.     //try {
  187.         for(int i = 0; i < TRIALS; ++i) {
  188.             UDate *d                = new UDate    [DEPTH];
  189.             UnicodeString *s    = new UnicodeString[DEPTH];
  190.  
  191.             d[0] = generateDate();
  192.  
  193.             UErrorCode status = U_ZERO_ERROR;
  194.  
  195.             // We go through this loop until we achieve a match or until
  196.             // the maximum loop count is reached.  We record the points at
  197.             // which the date and the string starts to match.  Once matching
  198.             // starts, it should continue.
  199.             int loop;
  200.             int dmatch = 0; // d[dmatch].getTime() == d[dmatch-1].getTime()
  201.             int smatch = 0; // s[smatch].equals(s[smatch-1])
  202.             for(loop = 0; loop < DEPTH; ++loop) {
  203.                 if (loop > 0)  {
  204.                     d[loop] = fmt->parse(s[loop-1], status);
  205.                     failure(status, "fmt->parse");
  206.                 }
  207.  
  208.                 s[loop] = fmt->format(d[loop], s[loop]);
  209.  
  210.                 if(loop > 0) {
  211.                     if(smatch == 0) {
  212.                         bool_t match = s[loop] == s[loop-1];
  213.                         if(smatch == 0) {
  214.                             if(match) 
  215.                                 smatch = loop;
  216.                         }
  217.                         else if( ! match) 
  218.                             errln("FAIL: String mismatch after match");
  219.                     }
  220.  
  221.                     if(dmatch == 0) {
  222.                         // {sfb} watch out here, this might not work
  223.                         bool_t match = d[loop]/*.getTime()*/ == d[loop-1]/*.getTime()*/;
  224.                         if(dmatch == 0) {
  225.                             if(match) 
  226.                                 dmatch = loop;
  227.                         }
  228.                         else if( ! match) 
  229.                             errln("FAIL: Date mismatch after match");
  230.                     }
  231.  
  232.                     if(smatch != 0 && dmatch != 0) 
  233.                         break;
  234.                 }
  235.             }
  236.             // At this point loop == DEPTH if we've failed, otherwise loop is the
  237.             // max(smatch, dmatch), that is, the index at which we have string and
  238.             // date matching.
  239.  
  240.             // Date usually matches in 2.  Exceptions handled below.
  241.             int maxDmatch = 2;
  242.             int maxSmatch = 1;
  243.             if (dmatch > maxDmatch) {
  244.                 // Time-only pattern with zone information and a starting date in PST.
  245.                 if(timeOnly && hasZone && fmt->getTimeZone().inDaylightTime(d[0], status) && ! failure(status, "TimeZone::inDST()")) {
  246.                     maxDmatch = 3;
  247.                     maxSmatch = 2;
  248.                 }
  249.             }
  250.  
  251.             // String usually matches in 1.  Exceptions are checked for here.
  252.             if(smatch > maxSmatch) { // Don't compute unless necessary
  253.                 // Starts in BC, with no era in pattern
  254.                 if( ! hasEra && getField(d[0], Calendar::ERA) == GregorianCalendar::BC)
  255.                     maxSmatch = 2;
  256.                 // Starts in DST, no year in pattern
  257.                 else if(fmt->getTimeZone().inDaylightTime(d[0], status) && ! failure(status, "foo") &&
  258.                          pat.indexOf(UnicodeString("yyyy")) == -1)
  259.                     maxSmatch = 2;
  260.                 // Two digit year with zone and year change and zone in pattern
  261.                 else if (hasZone &&
  262.                          fmt->getTimeZone().inDaylightTime(d[0], status) !=
  263.                          fmt->getTimeZone().inDaylightTime(d[dmatch], status) && ! failure(status, "foo") &&
  264.                          getField(d[0], Calendar::YEAR) !=
  265.                          getField(d[dmatch], Calendar::YEAR) &&
  266.                          pat.indexOf(UnicodeString("y")) != -1 &&
  267.                          pat.indexOf(UnicodeString("yyyy")) == -1)
  268.                     maxSmatch = 2;
  269.             }
  270.             
  271.             if(dmatch > maxDmatch || smatch > maxSmatch) {
  272.                 errln(UnicodeString("Pattern: ") + pat);
  273.                 logln(UnicodeString(" Date ") + dmatch + "  String " + smatch);
  274.                 
  275.                 for(int j = 0; j <= loop && j < DEPTH; ++j) {
  276.                     UnicodeString temp;
  277.                     FieldPosition pos(FieldPosition::DONT_CARE);
  278.                     logln((j>0?" P> ":"    ") + dateFormat->format(d[j], temp, pos) + " F> " +
  279.                           escape(s[j], temp) +
  280.                           (j > 0 && d[j]/*.getTime()*/==d[j-1]/*.getTime()*/?" d==":"") +
  281.                           (j > 0 && s[j] == s[j-1]?" s==":""));
  282.                 }
  283.             }
  284.         }
  285.     /*}
  286.     catch (ParseException e) {
  287.         errln("Exception: " + e.getMessage());
  288.         logln(e.toString());
  289.     }*/
  290. }
  291.  
  292. /**
  293.  * Return a field of the given date
  294.  */
  295. int32_t DateFormatRoundTripTest::getField(UDate d, int32_t f) {
  296.     // Should be synchronized, but we're single threaded so it's ok
  297.     UErrorCode status = U_ZERO_ERROR;
  298.     getFieldCal->setTime(d, status);
  299.     failure(status, "getfieldCal->setTime");
  300.     int32_t ret = getFieldCal->get((Calendar::EDateFields)f, status);
  301.     failure(status, "getfieldCal->get");
  302.     return ret;
  303. }
  304.  
  305. UnicodeString& DateFormatRoundTripTest::escape(const UnicodeString& src, UnicodeString& dst ) 
  306. {
  307.     dst.remove();
  308.     for (int32_t i = 0; i < src.size(); ++i) {
  309.         UChar c = src[i];
  310.         if(c < 0x0080) 
  311.             dst += c;
  312.         else {
  313.             dst += UnicodeString("[");
  314.             char buf [8];
  315.             sprintf(buf, "%#x", c);
  316.             dst += UnicodeString(buf);
  317.             dst += UnicodeString("]");
  318.         }
  319.     }
  320.  
  321.     return dst;
  322. }
  323.  
  324. UDate DateFormatRoundTripTest::generateDate() 
  325. {
  326.     double a = randFraction();
  327.     
  328.     // Now 'a' ranges from 0..1; scale it to range from 0 to 8000 years
  329.     a *= 8000;
  330.     
  331.     // Range from (4000-1970) BC to (8000-1970) AD
  332.     a -= 4000;
  333.     
  334.     // Now scale up to ms
  335.     a *= 365.25 * 24 * 60 * 60 * 1000;
  336.     
  337.     //return new Date((long)a);
  338.     return a;
  339. }
  340.  
  341. //eof
  342.