home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / caltest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  40.4 KB  |  1,162 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 "caltest.h"
  15. #include "gregocal.h"
  16. #include "smpdtfmt.h"
  17. #include "simpletz.h"
  18.  
  19. // *****************************************************************************
  20. // class CalendarTest
  21. // *****************************************************************************
  22.  
  23. void CalendarTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  24. {
  25.     if (exec) logln("TestSuite TestCalendar");
  26.     switch (index) {
  27.         case 0:
  28.             name = "TestDOW943";
  29.             if (exec) {
  30.                 logln("TestDOW943---"); logln("");
  31.                 TestDOW943();
  32.             }
  33.             break;
  34.         case 1:
  35.             name = "TestClonesUnique908";
  36.             if (exec) {
  37.                 logln("TestClonesUnique908---"); logln("");
  38.                 TestClonesUnique908();
  39.             }
  40.             break;
  41.         case 2:
  42.             name = "TestGregorianChange768";
  43.             if (exec) {
  44.                 logln("TestGregorianChange768---"); logln("");
  45.                 TestGregorianChange768();
  46.             }
  47.             break;
  48.         case 3:
  49.             name = "TestDisambiguation765";
  50.             if (exec) {
  51.                 logln("TestDisambiguation765---"); logln("");
  52.                 TestDisambiguation765();
  53.             }
  54.             break;
  55.         case 4:
  56.             name = "TestGMTvsLocal4064654";
  57.             if (exec) {
  58.                 logln("TestGMTvsLocal4064654---"); logln("");
  59.                 TestGMTvsLocal4064654();
  60.             }
  61.             break;
  62.         case 5:
  63.             name = "TestAddSetOrder621";
  64.             if (exec) {
  65.                 logln("TestAddSetOrder621---"); logln("");
  66.                 TestAddSetOrder621();
  67.             }
  68.             break;
  69.         case 6:
  70.             name = "TestAdd520";
  71.             if (exec) {
  72.                 logln("TestAdd520---"); logln("");
  73.                 TestAdd520();
  74.             }
  75.             break;
  76.         case 7:
  77.             name = "TestFieldSet4781";
  78.             if (exec) {
  79.                 logln("TestFieldSet4781---"); logln("");
  80.                 TestFieldSet4781();
  81.             }
  82.             break;
  83.         case 8:
  84.             name = "TestSerialize337";
  85.             if (exec) {
  86.                 logln("TestSerialize337---"); logln("");
  87.             //  TestSerialize337();
  88.             }
  89.             break;
  90.         case 9:
  91.             name = "TestSecondsZero121";
  92.             if (exec) {
  93.                 logln("TestSecondsZero121---"); logln("");
  94.                 TestSecondsZero121();
  95.             }
  96.             break;
  97.         case 10:
  98.             name = "TestAddSetGet0610";
  99.             if (exec) {
  100.                 logln("TestAddSetGet0610---"); logln("");
  101.                 TestAddSetGet0610();
  102.             }
  103.             break;
  104.         case 11:
  105.             name = "TestFields060";
  106.             if (exec) {
  107.                 logln("TestFields060---"); logln("");
  108.                 TestFields060();
  109.             }
  110.             break;
  111.         case 12:
  112.             name = "TestEpochStartFields";
  113.             if (exec) {
  114.                 logln("TestEpochStartFields---"); logln("");
  115.                 TestEpochStartFields();
  116.             }
  117.             break;
  118.         case 13:
  119.             name = "TestDOWProgression";
  120.             if (exec) {
  121.                 logln("TestDOWProgression---"); logln("");
  122.                 TestDOWProgression();
  123.             }
  124.             break;
  125.         case 14:
  126.             name = "TestGenericAPI";
  127.             if (exec) {
  128.                 logln("TestGenericAPI---"); logln("");
  129.                 TestGenericAPI();
  130.             }
  131.             break;
  132.         case 15:
  133.             name = "TestAddRollExtensive";
  134.             if (exec) {
  135.                 logln("TestAddRollExtensive---"); logln("");
  136.                 TestAddRollExtensive();
  137.             }
  138.             break;
  139.         default: name = ""; break;
  140.     }
  141. }
  142.  
  143. // ---------------------------------------------------------------------------------
  144.  
  145. /**
  146.  * Test various API methods for API completeness.
  147.  */
  148. void
  149. CalendarTest::TestGenericAPI()
  150. {
  151.     UErrorCode status = U_ZERO_ERROR;
  152.     UnicodeString str;
  153.  
  154.     UDate when = date(90, Calendar::APRIL, 15);
  155.  
  156.     UnicodeString tzid("TestZone");
  157.     int32_t tzoffset = 123400;
  158.  
  159.     SimpleTimeZone *zone = new SimpleTimeZone(tzoffset, tzid);
  160.     Calendar *cal = Calendar::createInstance(zone->clone(), status);
  161.     if (failure(status, "Calendar::createInstance")) return;
  162.  
  163.     if (*zone != cal->getTimeZone()) errln("FAIL: Calendar::getTimeZone failed");
  164.  
  165.     Calendar *cal2 = Calendar::createInstance(cal->getTimeZone(), status);
  166.     if (failure(status, "Calendar::createInstance")) return;
  167.     cal->setTime(when, status);
  168.     cal2->setTime(when, status);
  169.     if (failure(status, "Calendar::setTime")) return;
  170.     
  171.     if (!(*cal == *cal2)) errln("FAIL: Calendar::operator== failed");
  172.     if ((*cal != *cal2))  errln("FAIL: Calendar::operator!= failed");
  173.     if (!cal->equals(*cal2, status) ||
  174.         cal->before(*cal2, status) ||
  175.         cal->after(*cal2, status) ||
  176.         U_FAILURE(status)) errln("FAIL: equals/before/after failed");
  177.  
  178.     cal2->setTime(when + 1000, status);
  179.     if (failure(status, "Calendar::setTime")) return;
  180.     if (cal->equals(*cal2, status) ||
  181.         cal2->before(*cal, status) ||
  182.         cal->after(*cal2, status) ||
  183.         U_FAILURE(status)) errln("FAIL: equals/before/after failed");
  184.  
  185.     cal->roll(Calendar::SECOND, (bool_t) TRUE, status);
  186.     if (failure(status, "Calendar::roll")) return;
  187.     if (!cal->equals(*cal2, status) ||
  188.         cal->before(*cal2, status) ||
  189.         cal->after(*cal2, status) ||
  190.         U_FAILURE(status)) errln("FAIL: equals/before/after failed");
  191.  
  192.     // Roll back to January
  193.     cal->roll(Calendar::MONTH, (int32_t)(1 + Calendar::DECEMBER - cal->get(Calendar::MONTH, status)), status);
  194.     if (failure(status, "Calendar::roll")) return;
  195.     if (cal->equals(*cal2, status) ||
  196.         cal2->before(*cal, status) ||
  197.         cal->after(*cal2, status) ||
  198.         U_FAILURE(status)) errln("FAIL: equals/before/after failed");
  199.  
  200.     TimeZone *z = cal->orphanTimeZone();
  201.     if (z->getID(str) != tzid ||
  202.         z->getRawOffset() != tzoffset)
  203.         errln("FAIL: orphanTimeZone failed");
  204.  
  205.     int32_t i;
  206.     for (i=0; i<2; ++i)
  207.     {
  208.         bool_t lenient = ( i > 0 );
  209.         cal->setLenient(lenient);
  210.         if (lenient != cal->isLenient()) errln("FAIL: setLenient/isLenient failed");
  211.         // Later: Check for lenient behavior
  212.     }
  213.  
  214.     for (i=Calendar::SUNDAY; i<=Calendar::SATURDAY; ++i)
  215.     {
  216.         cal->setFirstDayOfWeek((Calendar::EDaysOfWeek)i);
  217.         if (cal->getFirstDayOfWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");
  218.     }
  219.  
  220.     for (i=0; i<=7; ++i)
  221.     {
  222.         cal->setMinimalDaysInFirstWeek((uint8_t)i);
  223.         if (cal->getMinimalDaysInFirstWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");
  224.     }
  225.  
  226.     for (i=0; i<Calendar::FIELD_COUNT; ++i)
  227.     {
  228.         if (cal->getMinimum((Calendar::EDateFields)i) != cal->getGreatestMinimum((Calendar::EDateFields)i))
  229.             errln("FAIL: getMinimum doesn't match getGreatestMinimum for field " + i);
  230.         if (cal->getLeastMaximum((Calendar::EDateFields)i) > cal->getMaximum((Calendar::EDateFields)i))
  231.             errln("FAIL: getLeastMaximum larger than getMaximum for field " + i);
  232.         if (cal->getMinimum((Calendar::EDateFields)i) >= cal->getMaximum((Calendar::EDateFields)i))
  233.             errln("FAIL: getMinimum not less than getMaximum for field " + i);
  234.     }
  235.  
  236.     cal->adoptTimeZone(TimeZone::createDefault());
  237.     cal->clear();
  238.     cal->set(1984, 5, 24);
  239.     if (cal->getTime(status) != date(84, 5, 24) || U_FAILURE(status))
  240.         errln("FAIL: Calendar::set(3 args) failed");
  241.  
  242.     cal->clear();
  243.     cal->set(1985, 3, 2, 11, 49);
  244.     if (cal->getTime(status) != date(85, 3, 2, 11, 49) || U_FAILURE(status))
  245.         errln("FAIL: Calendar::set(5 args) failed");
  246.  
  247.     cal->clear();
  248.     cal->set(1995, 9, 12, 1, 39, 55);
  249.     if (cal->getTime(status) != date(95, 9, 12, 1, 39, 55) || U_FAILURE(status))
  250.         errln("FAIL: Calendar::set(6 args) failed");
  251.  
  252.     cal->getTime(status);
  253.     if (failure(status, "Calendar::getTime")) return;
  254.     for (i=0; i<Calendar::FIELD_COUNT; ++i)
  255.     {
  256.         switch(i) {
  257.             case Calendar::YEAR: case Calendar::MONTH: case Calendar::DATE:
  258.             case Calendar::HOUR_OF_DAY: case Calendar::MINUTE: case Calendar::SECOND:
  259.                 if (!cal->isSet((Calendar::EDateFields)i)) errln("FAIL: Calendar::isSet failed");
  260.                 break;
  261.             default:
  262.                 if (cal->isSet((Calendar::EDateFields)i)) errln("FAIL: Calendar::isSet failed");
  263.         }
  264.         cal->clear((Calendar::EDateFields)i);
  265.         if (cal->isSet((Calendar::EDateFields)i)) errln("FAIL: Calendar::clear/isSet failed");
  266.     }
  267.  
  268.     delete cal;
  269.     delete cal2;
  270.  
  271.     int32_t count;
  272.     const Locale* loc = Calendar::getAvailableLocales(count);
  273.     if (count < 1 || loc == 0)
  274.     {
  275.         errln("FAIL: getAvailableLocales failed");
  276.     }
  277.     else
  278.     {
  279.         for (i=0; i<count; ++i)
  280.         {
  281.             cal = Calendar::createInstance(loc[i], status);
  282.             if (failure(status, "Calendar::createInstance")) return;
  283.             delete cal;
  284.         }
  285.     }
  286.  
  287.     cal = Calendar::createInstance(TimeZone::createDefault(), Locale::ENGLISH, status);
  288.     if (failure(status, "Calendar::createInstance")) return;
  289.     delete cal;
  290.  
  291.     cal = Calendar::createInstance(*zone, Locale::ENGLISH, status);
  292.     if (failure(status, "Calendar::createInstance")) return;
  293.     delete cal;
  294.  
  295.     GregorianCalendar *gc = new GregorianCalendar(*zone, status);
  296.     if (failure(status, "new GregorianCalendar")) return;
  297.     delete gc;
  298.  
  299.     gc = new GregorianCalendar(Locale::ENGLISH, status);
  300.     if (failure(status, "new GregorianCalendar")) return;
  301.     delete gc;
  302.  
  303.     gc = new GregorianCalendar(Locale::ENGLISH, status);
  304.     delete gc;
  305.  
  306.     gc = new GregorianCalendar(*zone, Locale::ENGLISH, status);
  307.     if (failure(status, "new GregorianCalendar")) return;
  308.     delete gc;
  309.  
  310.     gc = new GregorianCalendar(zone, status);
  311.     if (failure(status, "new GregorianCalendar")) return;
  312.     delete gc;
  313.  
  314.     gc = new GregorianCalendar(1998, 10, 14, 21, 43, status);
  315.     if (gc->getTime(status) != date(98, 10, 14, 21, 43) || U_FAILURE(status))
  316.         errln("FAIL: new GregorianCalendar(ymdhm) failed");
  317.     delete gc;
  318.  
  319.     gc = new GregorianCalendar(1998, 10, 14, 21, 43, 55, status);
  320.     if (gc->getTime(status) != date(98, 10, 14, 21, 43, 55) || U_FAILURE(status))
  321.         errln("FAIL: new GregorianCalendar(ymdhms) failed");
  322.  
  323.     GregorianCalendar gc2(Locale::ENGLISH, status);
  324.     if (failure(status, "new GregorianCalendar")) return;
  325.     gc2 = *gc;
  326.     if (gc2 != *gc || !(gc2 == *gc)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed");
  327.     delete gc;
  328.     delete z;
  329. }
  330.  
  331. // -------------------------------------
  332.  
  333. /**
  334.  * This test confirms the correct behavior of add when incrementing
  335.  * through subsequent days.
  336.  */
  337. void
  338. CalendarTest::TestRog()
  339. {
  340.     UErrorCode status = U_ZERO_ERROR;
  341.     GregorianCalendar* gc = new GregorianCalendar(status);
  342.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  343.     int32_t year = 1997, month = Calendar::APRIL, date = 1;
  344.     gc->set(year, month, date);
  345.     gc->set(Calendar::HOUR_OF_DAY, 23);
  346.     gc->set(Calendar::MINUTE, 0);
  347.     gc->set(Calendar::SECOND, 0);
  348.     gc->set(Calendar::MILLISECOND, 0);
  349.     for (int32_t i = 0; i < 9; i++, gc->add(Calendar::DATE, 1, status)) {
  350.         if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  351.         if (gc->get(Calendar::YEAR, status) != year ||
  352.             gc->get(Calendar::MONTH, status) != month ||
  353.             gc->get(Calendar::DATE, status) != (date + i)) errln("FAIL: Date wrong");
  354.         if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  355.     }
  356.     delete gc;
  357. }
  358.  
  359. // -------------------------------------
  360.  
  361. /**
  362.  * Test the handling of the day of the week, checking for correctness and
  363.  * for correct minimum and maximum values.
  364.  */
  365. void
  366. CalendarTest::TestDOW943()
  367. {
  368.     dowTest(FALSE);
  369.     dowTest(TRUE);
  370. }
  371.  
  372. void CalendarTest::dowTest(bool_t lenient)
  373. {
  374.     UErrorCode status = U_ZERO_ERROR;
  375.     GregorianCalendar* cal = new GregorianCalendar(status);
  376.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  377.     cal->set(1997, Calendar::AUGUST, 12);
  378.     cal->getTime(status);
  379.     if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  380.     cal->setLenient(lenient);
  381.     cal->set(1996, Calendar::DECEMBER, 1);
  382.     int32_t dow = cal->get(Calendar::DAY_OF_WEEK, status);
  383.     if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  384.     int32_t min = cal->getMinimum(Calendar::DAY_OF_WEEK);
  385.     int32_t max = cal->getMaximum(Calendar::DAY_OF_WEEK);
  386.     if (dow < min ||
  387.         dow > max) errln(UnicodeString("FAIL: Day of week ") + (int32_t)dow + " out of range");
  388.     if (dow != Calendar::SUNDAY) errln("FAIL: Day of week should be SUNDAY");
  389.     if (min != Calendar::SUNDAY ||
  390.         max != Calendar::SATURDAY) errln("FAIL: Min/max bad");
  391.     delete cal;
  392. }
  393.  
  394. // -------------------------------------
  395.  
  396. /** 
  397.  * Confirm that cloned Calendar objects do not inadvertently share substructures.
  398.  */
  399. void
  400. CalendarTest::TestClonesUnique908()
  401. {
  402.     UErrorCode status = U_ZERO_ERROR;
  403.     Calendar *c = Calendar::createInstance(status);
  404.     if (U_FAILURE(status)) { errln("Calendar::createInstance failed"); return; }
  405.     Calendar *d = (Calendar*) c->clone();
  406.     c->set(Calendar::MILLISECOND, 123);
  407.     d->set(Calendar::MILLISECOND, 456);
  408.     if (c->get(Calendar::MILLISECOND, status) != 123 ||
  409.         d->get(Calendar::MILLISECOND, status) != 456) {
  410.         errln("FAIL: Clones share fields");
  411.     }
  412.     if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  413.     delete c;
  414.     delete d;
  415. }
  416.  
  417. // -------------------------------------
  418.  
  419. /**
  420.  * Confirm that the Gregorian cutoff value works as advertised.
  421.  */
  422. void
  423. CalendarTest::TestGregorianChange768()
  424. {
  425.     bool_t b;
  426.     UErrorCode status = U_ZERO_ERROR;
  427.     UnicodeString str;
  428.     GregorianCalendar* c = new GregorianCalendar(status);
  429.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  430.     logln(UnicodeString("With cutoff ") + dateToString(c->getGregorianChange(), str));
  431.     logln(UnicodeString(" isLeapYear(1800) = ") + ((b = c->isLeapYear(1800)) ? "true" : "false"));
  432.     logln(UnicodeString(" (should be FALSE)"));
  433.     if (b) errln("FAIL");
  434.     c->setGregorianChange(date(0, 0, 1), status);
  435.     if (U_FAILURE(status)) { errln("GregorianCalendar::setGregorianChange failed"); return; }
  436.     logln(UnicodeString("With cutoff ") + dateToString(c->getGregorianChange(), str));
  437.     logln(UnicodeString(" isLeapYear(1800) = ") + ((b = c->isLeapYear(1800)) ? "true" : "false"));
  438.     logln(UnicodeString(" (should be TRUE)"));
  439.     if (!b) errln("FAIL");
  440.     delete c;
  441. }
  442.  
  443. // -------------------------------------
  444.  
  445. /**
  446.  * Confirm the functioning of the field disambiguation algorithm.
  447.  */
  448. void
  449. CalendarTest::TestDisambiguation765()
  450. {
  451.     UErrorCode status = U_ZERO_ERROR;
  452.     Calendar *c = Calendar::createInstance(status);
  453.     if (U_FAILURE(status)) { errln("Calendar::createInstance failed"); return; }
  454.     c->setLenient(FALSE);
  455.     c->clear();
  456.     c->set(Calendar::YEAR, 1997);
  457.     c->set(Calendar::MONTH, Calendar::JUNE);
  458.     c->set(Calendar::DATE, 3);
  459.     verify765("1997 third day of June = ", c, 1997, Calendar::JUNE, 3);
  460.     c->clear();
  461.     c->set(Calendar::YEAR, 1997);
  462.     c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  463.     c->set(Calendar::MONTH, Calendar::JUNE);
  464.     c->set(Calendar::DAY_OF_WEEK_IN_MONTH, 1);
  465.     verify765("1997 first Tuesday in June = ", c, 1997, Calendar::JUNE, 3);
  466.     c->clear();
  467.     c->set(Calendar::YEAR, 1997);
  468.     c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  469.     c->set(Calendar::MONTH, Calendar::JUNE);
  470.     c->set(Calendar::DAY_OF_WEEK_IN_MONTH, - 1);
  471.     verify765("1997 last Tuesday in June = ", c, 1997, Calendar::JUNE, 24);
  472.     // IllegalArgumentException e = null;
  473.     status = U_ZERO_ERROR;
  474.     //try {
  475.         c->clear();
  476.         c->set(Calendar::YEAR, 1997);
  477.         c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  478.         c->set(Calendar::MONTH, Calendar::JUNE);
  479.         c->set(Calendar::DAY_OF_WEEK_IN_MONTH, 0);
  480.         c->getTime(status);
  481.     //}
  482.     //catch(IllegalArgumentException ex) {
  483.     //    e = ex;
  484.     //}
  485.     verify765("1997 zero-th Tuesday in June = ", status);
  486.     c->clear();
  487.     c->set(Calendar::YEAR, 1997);
  488.     c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  489.     c->set(Calendar::MONTH, Calendar::JUNE);
  490.     c->set(Calendar::WEEK_OF_MONTH, 1);
  491.     verify765("1997 Tuesday in week 1 of June = ", c, 1997, Calendar::JUNE, 3);
  492.     c->clear();
  493.     c->set(Calendar::YEAR, 1997);
  494.     c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  495.     c->set(Calendar::MONTH, Calendar::JUNE);
  496.     c->set(Calendar::WEEK_OF_MONTH, 5);
  497.     verify765("1997 Tuesday in week 5 of June = ", c, 1997, Calendar::JULY, 1);
  498.     status = U_ZERO_ERROR;
  499.     //try {
  500.         c->clear();
  501.         c->set(Calendar::YEAR, 1997);
  502.         c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  503.         c->set(Calendar::MONTH, Calendar::JUNE);
  504.         c->set(Calendar::WEEK_OF_MONTH, 0);
  505.         verify765("1997 Tuesday in week 0 of June = ", c, 1997, Calendar::MAY, 27);
  506.     //}
  507.     //catch(IllegalArgumentException ex) {
  508.     //    errln("FAIL: Exception seen:");
  509.     //    ex.printStackTrace(log);
  510.     //}
  511.     c->clear();
  512.     c->set(Calendar::YEAR, 1997);
  513.     c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  514.     c->set(Calendar::WEEK_OF_YEAR, 1);
  515.     verify765("1997 Tuesday in week 1 of year = ", c, 1996, Calendar::DECEMBER, 31);
  516.     c->clear();
  517.     c->set(Calendar::YEAR, 1997);
  518.     c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  519.     c->set(Calendar::WEEK_OF_YEAR, 10);
  520.     verify765("1997 Tuesday in week 10 of year = ", c, 1997, Calendar::MARCH, 4);
  521.     //try {
  522.  
  523.     // {sfb} week 0 is no longer a valid week of year
  524.     /*c->clear();
  525.     c->set(Calendar::YEAR, 1997);
  526.     c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
  527.     //c->set(Calendar::WEEK_OF_YEAR, 0);
  528.     c->set(Calendar::WEEK_OF_YEAR, 1);
  529.     verify765("1997 Tuesday in week 0 of year = ", c, 1996, Calendar::DECEMBER, 24);*/
  530.  
  531.     //}
  532.     //catch(IllegalArgumentException ex) {
  533.     //    errln("FAIL: Exception seen:");
  534.     //    ex.printStackTrace(log);
  535.     //}
  536.     delete c;
  537. }
  538.  
  539. // -------------------------------------
  540.  
  541. void
  542. CalendarTest::verify765(const UnicodeString& msg, Calendar* c, int32_t year, int32_t month, int32_t day)
  543. {
  544.     UnicodeString str;
  545.     UErrorCode status = U_ZERO_ERROR;
  546.     if (c->get(Calendar::YEAR, status) == year &&
  547.         c->get(Calendar::MONTH, status) == month &&
  548.         c->get(Calendar::DATE, status) == day) {
  549.         if (U_FAILURE(status)) { errln("FAIL: Calendar::get failed"); return; }
  550.         logln("PASS: " + msg + dateToString(c->getTime(status), str));
  551.         if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  552.     }
  553.     else {
  554.         errln("FAIL: " + msg + dateToString(c->getTime(status), str) + "; expected " + (int32_t)year + "/" + (int32_t)(month + 1) + "/" + (int32_t)day);
  555.         if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  556.     }
  557. }
  558.  
  559. // -------------------------------------
  560.  
  561. void
  562. CalendarTest::verify765(const UnicodeString& msg/*, IllegalArgumentException e*/, UErrorCode status)
  563. {
  564.     if (status != U_ILLEGAL_ARGUMENT_ERROR) errln("FAIL: No IllegalArgumentException for " + msg);
  565.     else logln("PASS: " + msg + "IllegalArgument as expected");
  566. }
  567.  
  568. // -------------------------------------
  569.  
  570. /**
  571.  * Confirm that the offset between local time and GMT behaves as expected.
  572.  */
  573. void
  574. CalendarTest::TestGMTvsLocal4064654()
  575. {
  576.     test4064654(1997, 1, 1, 12, 0, 0);
  577.     test4064654(1997, 4, 16, 18, 30, 0);
  578. }
  579.  
  580. // -------------------------------------
  581.  
  582. void
  583. CalendarTest::test4064654(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
  584. {
  585.     UDate date;
  586.     UErrorCode status = U_ZERO_ERROR;
  587.     UnicodeString str;
  588.     Calendar *gmtcal = Calendar::createInstance(status);
  589.     if (U_FAILURE(status)) { errln("Calendar::createInstance failed"); return; }
  590.     gmtcal->adoptTimeZone(TimeZone::createTimeZone("Africa/Casablanca"));
  591.     gmtcal->set(yr, mo - 1, dt, hr, mn, sc);
  592.     gmtcal->set(Calendar::MILLISECOND, 0);
  593.     date = gmtcal->getTime(status);
  594.     if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  595.     logln("date = " + dateToString(date, str));
  596.     Calendar *cal = Calendar::createInstance(status);
  597.     if (U_FAILURE(status)) { errln("Calendar::createInstance failed"); return; }
  598.     cal->setTime(date, status);
  599.     if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
  600.     int32_t offset = cal->getTimeZone().getOffset((uint8_t)cal->get(Calendar::ERA, status),
  601.                                                   cal->get(Calendar::YEAR, status),
  602.                                                   cal->get(Calendar::MONTH, status),
  603.                                                   cal->get(Calendar::DATE, status),
  604.                                                   (uint8_t)cal->get(Calendar::DAY_OF_WEEK, status),
  605.                                                   cal->get(Calendar::MILLISECOND, status));
  606.     if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  607.     logln("offset for " + dateToString(date, str) + "= " + (offset / 1000 / 60 / 60.0) + "hr");
  608.     int32_t utc = ((cal->get(Calendar::HOUR_OF_DAY, status) * 60 +
  609.                     cal->get(Calendar::MINUTE, status)) * 60 +
  610.                    cal->get(Calendar::SECOND, status)) * 1000 +
  611.         cal->get(Calendar::MILLISECOND, status) - offset;
  612.     if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  613.     int32_t expected = ((hr * 60 + mn) * 60 + sc) * 1000;
  614.     if (utc != expected) errln(UnicodeString("FAIL: Discrepancy of ") + (utc - expected) +
  615.                                " millis = " + ((utc - expected) / 1000 / 60 / 60.0) + " hr");
  616.     delete gmtcal;
  617.     delete cal;
  618. }
  619.  
  620. // -------------------------------------
  621.  
  622. /**
  623.  * The operations of adding and setting should not exhibit pathological
  624.  * dependence on the order of operations.  This test checks for this.
  625.  */
  626. void
  627. CalendarTest::TestAddSetOrder621()
  628. {
  629.     UDate d = date(97, 4, 14, 13, 23, 45);
  630.     UErrorCode status = U_ZERO_ERROR;
  631.     Calendar *cal = Calendar::createInstance(status);
  632.     if (U_FAILURE(status)) { 
  633.         errln("Calendar::createInstance failed"); 
  634.         delete cal;
  635.         return; 
  636.     }
  637.     cal->setTime(d, status);
  638.     if (U_FAILURE(status)) { 
  639.         errln("Calendar::setTime failed"); 
  640.         delete cal;
  641.         return; 
  642.     }
  643.     cal->add(Calendar::DATE, - 5, status);
  644.     if (U_FAILURE(status)) { 
  645.         errln("Calendar::add failed"); 
  646.         delete cal;
  647.         return; 
  648.     }
  649.     cal->set(Calendar::HOUR_OF_DAY, 0);
  650.     cal->set(Calendar::MINUTE, 0);
  651.     cal->set(Calendar::SECOND, 0);
  652.     UnicodeString s;
  653.     dateToString(cal->getTime(status), s);
  654.     if (U_FAILURE(status)) { 
  655.         errln("Calendar::getTime failed"); 
  656.         delete cal;
  657.         return; 
  658.     }
  659.     delete cal;
  660.  
  661.     cal = Calendar::createInstance(status);
  662.     if (U_FAILURE(status)) { 
  663.         errln("Calendar::createInstance failed"); 
  664.         delete cal;
  665.         return; 
  666.     }
  667.     cal->setTime(d, status);
  668.     if (U_FAILURE(status)) { 
  669.         errln("Calendar::setTime failed"); 
  670.         delete cal;
  671.         return; 
  672.     }
  673.     cal->set(Calendar::HOUR_OF_DAY, 0);
  674.     cal->set(Calendar::MINUTE, 0);
  675.     cal->set(Calendar::SECOND, 0);
  676.     cal->add(Calendar::DATE, - 5, status);
  677.     if (U_FAILURE(status)) { 
  678.         errln("Calendar::add failed"); 
  679.         delete cal;
  680.         return; 
  681.     }
  682.     UnicodeString s2;
  683.     dateToString(cal->getTime(status), s2);
  684.     if (U_FAILURE(status)) { 
  685.         errln("Calendar::getTime failed"); 
  686.         delete cal;
  687.         return; 
  688.     }
  689.     if (s == s2) 
  690.         logln("Pass: " + s + " == " + s2);
  691.     else 
  692.         errln("FAIL: " + s + " != " + s2);
  693.     delete cal;
  694. }
  695.  
  696. // -------------------------------------
  697.  
  698. /**
  699.  * Confirm that adding to various fields works.
  700.  */
  701. void
  702. CalendarTest::TestAdd520()
  703. {
  704.     int32_t y = 1997, m = Calendar::FEBRUARY, d = 1;
  705.     UErrorCode status = U_ZERO_ERROR;
  706.     GregorianCalendar *temp = new GregorianCalendar(y, m, d, status);
  707.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  708.     check520(temp, y, m, d);
  709.     temp->add(temp->YEAR, 1, status);
  710.     if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  711.     y++;
  712.     check520(temp, y, m, d);
  713.     temp->add(temp->MONTH, 1, status);
  714.     if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  715.     m++;
  716.     check520(temp, y, m, d);
  717.     temp->add(temp->DATE, 1, status);
  718.     if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  719.     d++;
  720.     check520(temp, y, m, d);
  721.     temp->add(temp->DATE, 2, status);
  722.     if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  723.     d += 2;
  724.     check520(temp, y, m, d);
  725.     temp->add(temp->DATE, 28, status);
  726.     if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  727.     d = 1;++m;
  728.     check520(temp, y, m, d);
  729.     delete temp;
  730. }
  731.  
  732. // -------------------------------------
  733.  
  734. /**
  735.  * Execute adding and rolling in GregorianCalendar extensively,
  736.  */
  737. void
  738. CalendarTest::TestAddRollExtensive()
  739. {
  740.     int32_t maxlimit = 40;
  741.     int32_t y = 1997, m = Calendar::FEBRUARY, d = 1, hr = 1, min = 1, sec = 0, ms = 0;
  742.     UErrorCode status = U_ZERO_ERROR;
  743.     GregorianCalendar *temp = new GregorianCalendar(y, m, d, status);
  744.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  745.  
  746.     temp->set(Calendar::HOUR, hr);
  747.     temp->set(Calendar::MINUTE, min);
  748.     temp->set(Calendar::SECOND, sec);
  749.     temp->set(Calendar::MILLISECOND, ms);
  750.  
  751.     Calendar::EDateFields e;
  752.  
  753.     logln("Testing GregorianCalendar add...");
  754.     e = Calendar::YEAR;
  755.     while (e < Calendar::FIELD_COUNT) {
  756.         int32_t i;
  757.         int32_t limit = maxlimit;
  758.         status = U_ZERO_ERROR;
  759.         for (i = 0; i < limit; i++) {
  760.             temp->add(e, 1, status);
  761.             if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
  762.         }
  763.         for (i = 0; i < limit; i++) {
  764.             temp->add(e, -1, status);
  765.             if (U_FAILURE(status)) { errln("GregorianCalendar::add -1 failed"); return; }
  766.         }
  767.         check520(temp, y, m, d, hr, min, sec, ms, e);
  768.  
  769.         e = (Calendar::EDateFields) ((int32_t) e + 1);
  770.     }
  771.  
  772.     logln("Testing GregorianCalendar roll...");
  773.     e = Calendar::YEAR;
  774.     while (e < Calendar::FIELD_COUNT) {
  775.         int32_t i;
  776.         int32_t limit = maxlimit;
  777.         status = U_ZERO_ERROR;
  778.         for (i = 0; i < limit; i++) {
  779.             temp->roll(e, 1, status);
  780.             if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
  781.         }
  782.         for (i = 0; i < limit; i++) {
  783.             temp->roll(e, -1, status);
  784.             if (U_FAILURE(status)) { errln("GregorianCalendar::roll -1 failed"); return; }
  785.         }
  786.         check520(temp, y, m, d, hr, min, sec, ms, e);
  787.  
  788.         e = (Calendar::EDateFields) ((int32_t) e + 1);
  789.     }
  790.  
  791.     delete temp;
  792. }
  793.  
  794. // -------------------------------------
  795. void 
  796. CalendarTest::check520(Calendar* c, 
  797.                         int32_t y, int32_t m, int32_t d, 
  798.                         int32_t hr, int32_t min, int32_t sec, 
  799.                         int32_t ms, Calendar::EDateFields field)
  800.  
  801. {
  802.     UErrorCode status = U_ZERO_ERROR;
  803.     if (c->get(Calendar::YEAR, status) != y ||
  804.         c->get(Calendar::MONTH, status) != m ||
  805.         c->get(Calendar::DATE, status) != d ||
  806.         c->get(Calendar::HOUR, status) != hr ||
  807.         c->get(Calendar::MINUTE, status) != min ||
  808.         c->get(Calendar::SECOND, status) != sec ||
  809.         c->get(Calendar::MILLISECOND, status) != ms) {
  810.         errln(UnicodeString("U_FAILURE for field ") + (int32_t)field + 
  811.                 ": Expected y/m/d h:m:s:ms of " +
  812.                 y + "/" + (m + 1) + "/" + d + " " +
  813.               hr + ":" + min + ":" + sec + ":" + ms +
  814.               "; got " + c->get(Calendar::YEAR, status) +
  815.               "/" + (c->get(Calendar::MONTH, status) + 1) +
  816.               "/" + c->get(Calendar::DATE, status) +
  817.               " " + c->get(Calendar::HOUR, status) + ":" +
  818.               c->get(Calendar::MINUTE, status) + ":" + 
  819.               c->get(Calendar::SECOND, status) + ":" +
  820.               c->get(Calendar::MILLISECOND, status)
  821.               );
  822.  
  823.         if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  824.     }
  825.     else 
  826.         logln(UnicodeString("Confirmed: ") + y + "/" +
  827.                 (m + 1) + "/" + d + " " +
  828.                 hr + ":" + min + ":" + sec + ":" + ms);
  829. }
  830.  
  831. // -------------------------------------
  832. void 
  833. CalendarTest::check520(Calendar* c, 
  834.                         int32_t y, int32_t m, int32_t d)
  835.  
  836. {
  837.     UErrorCode status = U_ZERO_ERROR;
  838.     if (c->get(Calendar::YEAR, status) != y ||
  839.         c->get(Calendar::MONTH, status) != m ||
  840.         c->get(Calendar::DATE, status) != d) {
  841.         errln(UnicodeString("FAILURE: Expected y/m/d of ") +
  842.               y + "/" + (m + 1) + "/" + d + " " +
  843.               "; got " + c->get(Calendar::YEAR, status) +
  844.               "/" + (c->get(Calendar::MONTH, status) + 1) +
  845.               "/" + c->get(Calendar::DATE, status)
  846.               );
  847.  
  848.         if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  849.     }
  850.     else 
  851.         logln(UnicodeString("Confirmed: ") + y + "/" +
  852.                 (m + 1) + "/" + d);
  853. }
  854.  
  855. // -------------------------------------
  856.  
  857. /**
  858.  * Test that setting of fields works.  In particular, make sure that all instances
  859.  * of GregorianCalendar don't share a static instance of the fields array.
  860.  */
  861. void
  862. CalendarTest::TestFieldSet4781()
  863. {
  864.     // try {
  865.         UErrorCode status = U_ZERO_ERROR;
  866.         GregorianCalendar *g = new GregorianCalendar(status);
  867.         if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  868.         GregorianCalendar *g2 = new GregorianCalendar(status);
  869.         if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  870.         g2->set(Calendar::HOUR, 12, status);
  871.         g2->set(Calendar::MINUTE, 0, status);
  872.         g2->set(Calendar::SECOND, 0, status);
  873.         if (U_FAILURE(status)) { errln("Calendar::set failed"); return; }
  874.         if (*g == *g2) logln("Same");
  875.         else logln("Different");
  876.     //}
  877.         //catch(IllegalArgumentException e) {
  878.         //errln("Unexpected exception seen: " + e);
  879.     //}
  880.         delete g;
  881.         delete g2;
  882. }
  883.  
  884. // -------------------------------------
  885.  
  886. /* We don't support serialization on C++
  887. void
  888. CalendarTest::TestSerialize337()
  889. {
  890.     Calendar cal = Calendar::getInstance();
  891.     bool_t ok = FALSE;
  892.     try {
  893.         FileOutputStream f = new FileOutputStream(FILENAME);
  894.         ObjectOutput s = new ObjectOutputStream(f);
  895.         s.writeObject(PREFIX);
  896.         s.writeObject(cal);
  897.         s.writeObject(POSTFIX);
  898.         f.close();
  899.         FileInputStream in = new FileInputStream(FILENAME);
  900.         ObjectInputStream t = new ObjectInputStream(in);
  901.         UnicodeString& pre = (UnicodeString&) t.readObject();
  902.         Calendar c = (Calendar) t.readObject();
  903.         UnicodeString& post = (UnicodeString&) t.readObject();
  904.         in.close();
  905.         ok = pre.equals(PREFIX) &&
  906.             post.equals(POSTFIX) &&
  907.             cal->equals(c);
  908.         File fl = new File(FILENAME);
  909.         fl.delete();
  910.     }
  911.     catch(IOException e) {
  912.         errln("FAIL: Exception received:");
  913.         e.printStackTrace(log);
  914.     }
  915.     catch(ClassNotFoundException e) {
  916.         errln("FAIL: Exception received:");
  917.         e.printStackTrace(log);
  918.     }
  919.     if (!ok) errln("Serialization of Calendar object failed.");
  920. }
  921.  
  922. UnicodeString& CalendarTest::PREFIX = "abc";
  923.  
  924. UnicodeString& CalendarTest::POSTFIX = "def";
  925.  
  926. UnicodeString& CalendarTest::FILENAME = "tmp337.bin";
  927.  */
  928.  
  929. // -------------------------------------
  930.  
  931. /**
  932.  * Verify that the seconds of a Calendar can be zeroed out through the
  933.  * expected sequence of operations.
  934.  */ 
  935. void
  936. CalendarTest::TestSecondsZero121()
  937. {
  938.     UErrorCode status = U_ZERO_ERROR;
  939.     Calendar *cal = new GregorianCalendar(status);
  940.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  941.     cal->setTime(Calendar::getNow(), status);
  942.     if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
  943.     cal->set(Calendar::SECOND, 0);
  944.     if (U_FAILURE(status)) { errln("Calendar::set failed"); return; }
  945.     UDate d = cal->getTime(status);
  946.     if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  947.     UnicodeString s;
  948.     dateToString(d, s);
  949.     if (s.indexOf(":00 ") < 0) errln("Expected to see :00 in " + s);
  950.     delete cal;
  951. }
  952.  
  953. // -------------------------------------
  954.  
  955. /**
  956.  * Verify that a specific sequence of adding and setting works as expected;
  957.  * it should not vary depending on when and whether the get method is
  958.  * called.
  959.  */
  960. void
  961. CalendarTest::TestAddSetGet0610()
  962. {
  963.     UErrorCode status = U_ZERO_ERROR;
  964.     {
  965.         Calendar *calendar = new GregorianCalendar(status);
  966.         if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  967.         calendar->set(1993, Calendar::JANUARY, 4);
  968.         logln("1A) " + value(calendar));
  969.         calendar->add(Calendar::DATE, 1, status);
  970.         if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  971.         UnicodeString v = value(calendar);
  972.         logln("1B) " + v);
  973.         logln("--) 1993/0/5");
  974.         if (!(v == EXPECTED_0610)) errln("Expected " + EXPECTED_0610 + "; saw " + v);
  975.         delete calendar;
  976.     }
  977.     {
  978.         Calendar *calendar = new GregorianCalendar(1993, Calendar::JANUARY, 4, status);
  979.         if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  980.         logln("2A) " + value(calendar));
  981.         calendar->add(Calendar::DATE, 1, status);
  982.         if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  983.         UnicodeString v = value(calendar);
  984.         logln("2B) " + v);
  985.         logln("--) 1993/0/5");
  986.         if (!(v == EXPECTED_0610)) errln("Expected " + EXPECTED_0610 + "; saw " + v);
  987.         delete calendar;
  988.     }
  989.     {
  990.         Calendar *calendar = new GregorianCalendar(1993, Calendar::JANUARY, 4, status);
  991.         if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  992.         logln("3A) " + value(calendar));
  993.         calendar->getTime(status);
  994.         if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  995.         calendar->add(Calendar::DATE, 1, status);
  996.         if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  997.         UnicodeString v = value(calendar);
  998.         logln("3B) " + v);
  999.         logln("--) 1993/0/5");
  1000.         if (!(v == EXPECTED_0610)) errln("Expected " + EXPECTED_0610 + "; saw " + v);
  1001.         delete calendar;
  1002.     }
  1003. }
  1004.  
  1005. // -------------------------------------
  1006.  
  1007. UnicodeString
  1008. CalendarTest::value(Calendar* calendar)
  1009. {
  1010.     UErrorCode status = U_ZERO_ERROR;
  1011.     return UnicodeString("") + (int32_t)calendar->get(Calendar::YEAR, status) +
  1012.         "/" + (int32_t)calendar->get(Calendar::MONTH, status) +
  1013.         "/" + (int32_t)calendar->get(Calendar::DATE, status) +
  1014.         (U_FAILURE(status) ? " FAIL: Calendar::get failed" : "");
  1015. }
  1016.  
  1017. UnicodeString CalendarTest::EXPECTED_0610 = "1993/0/5";
  1018.  
  1019. // -------------------------------------
  1020.  
  1021. /**
  1022.  * Verify that various fields on a known date are set correctly.
  1023.  */
  1024. void
  1025. CalendarTest::TestFields060()
  1026. {
  1027.     UErrorCode status = U_ZERO_ERROR;
  1028.     int32_t year = 1997;
  1029.     int32_t month = Calendar::OCTOBER;
  1030.     int32_t dDate = 22;
  1031.     GregorianCalendar *calendar = 0;
  1032.     calendar = new GregorianCalendar(year, month, dDate, status);
  1033.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  1034.     for (int32_t i = 0; i < EXPECTED_FIELDS_length;) {
  1035.         Calendar::EDateFields field = (Calendar::EDateFields)EXPECTED_FIELDS[i++];
  1036.         int32_t expected = EXPECTED_FIELDS[i++];
  1037.         if (calendar->get(field, status) != expected) {
  1038.             errln(UnicodeString("Expected field ") + (int32_t)field + " to have value " + (int32_t)expected +
  1039.                   "; received " + (int32_t)calendar->get(field, status) + " instead");
  1040.             if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  1041.         }
  1042.     }
  1043.     delete calendar;
  1044. }
  1045.  
  1046. int32_t CalendarTest::EXPECTED_FIELDS[] = {
  1047.     Calendar::YEAR, 1997,
  1048.     Calendar::MONTH, Calendar::OCTOBER,
  1049.     Calendar::DAY_OF_MONTH, 22,
  1050.     Calendar::DAY_OF_WEEK, Calendar::WEDNESDAY,
  1051.     Calendar::DAY_OF_WEEK_IN_MONTH, 4,
  1052.     Calendar::DAY_OF_YEAR, 295
  1053. };
  1054.  
  1055. const int32_t CalendarTest::EXPECTED_FIELDS_length = sizeof(CalendarTest::EXPECTED_FIELDS) /
  1056.     sizeof(CalendarTest::EXPECTED_FIELDS[0]);
  1057.  
  1058. // -------------------------------------
  1059.  
  1060. /**
  1061.  * Verify that various fields on a known date are set correctly.  In this
  1062.  * case, the start of the epoch (January 1 1970).
  1063.  */
  1064. void
  1065. CalendarTest::TestEpochStartFields()
  1066. {
  1067.     UErrorCode status = U_ZERO_ERROR;
  1068.     TimeZone *z = TimeZone::createDefault();
  1069.     Calendar *c = Calendar::createInstance(status);
  1070.     if (U_FAILURE(status)) { errln("Calendar::createInstance failed"); return; }
  1071.     UDate d = - z->getRawOffset();
  1072.     GregorianCalendar *gc = new GregorianCalendar(status);
  1073.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  1074.     gc->setTimeZone(*z);
  1075.     gc->setTime(d, status);
  1076.     if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
  1077.     bool_t idt = gc->inDaylightTime(status);
  1078.     if (U_FAILURE(status)) { errln("GregorianCalendar::inDaylightTime failed"); return; }
  1079.     if (idt) {
  1080.         UnicodeString str;
  1081.         logln("Warning: Skipping test because " + dateToString(d, str) + " is in DST.");
  1082.     }
  1083.     else {
  1084.         c->setTime(d, status);
  1085.         if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
  1086.         for (int32_t i = 0; i < Calendar::ZONE_OFFSET;++i) {
  1087.             if (c->get((Calendar::EDateFields)i, status) != EPOCH_FIELDS[i])
  1088.                 errln(UnicodeString("Expected field ") + i + " to have value " + EPOCH_FIELDS[i] +
  1089.                       "; saw " + c->get((Calendar::EDateFields)i, status) + " instead");
  1090.             if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  1091.         }
  1092.         if (c->get(Calendar::ZONE_OFFSET, status) != z->getRawOffset())
  1093.         {
  1094.             errln(UnicodeString("Expected field ZONE_OFFSET to have value ") + z->getRawOffset() +
  1095.                   "; saw " + c->get(Calendar::ZONE_OFFSET, status) + " instead");
  1096.             if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  1097.         }
  1098.         if (c->get(Calendar::DST_OFFSET, status) != 0)
  1099.         {
  1100.             errln(UnicodeString("Expected field DST_OFFSET to have value 0") +
  1101.                   "; saw " + c->get(Calendar::DST_OFFSET, status) + " instead");
  1102.             if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  1103.         }
  1104.     }
  1105.     delete c;
  1106.     delete z;
  1107.     delete gc;
  1108. }
  1109.  
  1110. int32_t CalendarTest::EPOCH_FIELDS[] = {
  1111.     1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, - 28800000, 0
  1112. };
  1113.  
  1114. // -------------------------------------
  1115.  
  1116. /**
  1117.  * Test that the days of the week progress properly when add is called repeatedly
  1118.  * for increments of 24 days.
  1119.  */
  1120. void
  1121. CalendarTest::TestDOWProgression()
  1122. {
  1123.     UErrorCode status = U_ZERO_ERROR;
  1124.     Calendar *cal = new GregorianCalendar(1972, Calendar::OCTOBER, 26, status);
  1125.     if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
  1126.     marchByDelta(cal, 24);
  1127.     delete cal;
  1128. }
  1129.  
  1130. // -------------------------------------
  1131.  
  1132. void
  1133. CalendarTest::marchByDelta(Calendar* cal, int32_t delta)
  1134. {
  1135.     UErrorCode status = U_ZERO_ERROR;
  1136.     Calendar *cur = (Calendar*) cal->clone();
  1137.     int32_t initialDOW = cur->get(Calendar::DAY_OF_WEEK, status);
  1138.     if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  1139.     int32_t DOW, newDOW = initialDOW;
  1140.     do {
  1141.         UnicodeString str;
  1142.         DOW = newDOW;
  1143.         logln(UnicodeString("DOW = ") + DOW + "  " + dateToString(cur->getTime(status), str));
  1144.         if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  1145.         cur->add(Calendar::DAY_OF_WEEK, delta, status);
  1146.         if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
  1147.         newDOW = cur->get(Calendar::DAY_OF_WEEK, status);
  1148.         if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
  1149.         int32_t expectedDOW = 1 + (DOW + delta - 1) % 7;
  1150.         if (newDOW != expectedDOW) {
  1151.             errln(UnicodeString("Day of week should be ") + expectedDOW + " instead of " + newDOW +
  1152.                   " on " + dateToString(cur->getTime(status), str));
  1153.             if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
  1154.             return;
  1155.         }
  1156.     }
  1157.     while (newDOW != initialDOW);
  1158.     delete cur;
  1159. }
  1160.  
  1161. //eof
  1162.