Home | History | Annotate | Download | only in cintltst
      1 /********************************************************************
      2  * Copyright (c) 1997-2010, International Business Machines
      3  * Corporation and others. All Rights Reserved.
      4  ********************************************************************
      5  *
      6  * File CCALTST.C
      7  *
      8  * Modification History:
      9  *        Name                     Description
     10  *     Madhu Katragadda               Creation
     11  ********************************************************************
     12  */
     13 
     14 /* C API AND FUNCTIONALITY TEST FOR CALENDAR (ucol.h)*/
     15 
     16 #include "unicode/utypes.h"
     17 
     18 #if !UCONFIG_NO_FORMATTING
     19 
     20 #include <stdlib.h>
     21 #include <string.h>
     22 
     23 #include "unicode/uloc.h"
     24 #include "unicode/ucal.h"
     25 #include "unicode/udat.h"
     26 #include "unicode/ustring.h"
     27 #include "cintltst.h"
     28 #include "ccaltst.h"
     29 #include "cformtst.h"
     30 #include "cstring.h"
     31 #include "ulist.h"
     32 
     33 void TestGregorianChange(void);
     34 
     35 void addCalTest(TestNode** root);
     36 
     37 void addCalTest(TestNode** root)
     38 {
     39 
     40     addTest(root, &TestCalendar, "tsformat/ccaltst/TestCalendar");
     41     addTest(root, &TestGetSetDateAPI, "tsformat/ccaltst/TestGetSetDateAPI");
     42     addTest(root, &TestFieldGetSet, "tsformat/ccaltst/TestFieldGetSet");
     43     addTest(root, &TestAddRollExtensive, "tsformat/ccaltst/TestAddRollExtensive");
     44     addTest(root, &TestGetLimits, "tsformat/ccaltst/TestGetLimits");
     45     addTest(root, &TestDOWProgression, "tsformat/ccaltst/TestDOWProgression");
     46     addTest(root, &TestGMTvsLocal, "tsformat/ccaltst/TestGMTvsLocal");
     47     addTest(root, &TestGregorianChange, "tsformat/ccaltst/TestGregorianChange");
     48     addTest(root, &TestGetKeywordValuesForLocale, "tsformat/ccaltst/TestGetKeywordValuesForLocale");
     49     addTest(root, &TestWeekend, "tsformat/ccaltst/TestWeekend");
     50 }
     51 
     52 /* "GMT" */
     53 static const UChar fgGMTID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
     54 
     55 /* "PST" */
     56 static const UChar PST[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
     57 
     58 static const UChar EUROPE_PARIS[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
     59 
     60 static const UChar AMERICA_LOS_ANGELES[] = {0x41, 0x6D, 0x65, 0x72, 0x69, 0x63, 0x61, 0x2F,
     61     0x4C, 0x6F, 0x73, 0x5F, 0x41, 0x6E, 0x67, 0x65, 0x6C, 0x65, 0x73, 0x00}; /* America/Los_Angeles */
     62 
     63 typedef struct {
     64     const char *    locale;
     65     UCalendarType   calType;
     66     const char *    expectedResult;
     67 } UCalGetTypeTest;
     68 
     69 static const UCalGetTypeTest ucalGetTypeTests[] = {
     70     { "en_US",                   UCAL_GREGORIAN, "gregorian" },
     71     { "ja_JP@calendar=japanese", UCAL_DEFAULT,   "japanese"  },
     72     { "th_TH",                   UCAL_GREGORIAN, "gregorian" },
     73     { "th_TH",                   UCAL_DEFAULT,   "buddhist"  },
     74     { "ja_JP@calendar=japanese", UCAL_GREGORIAN, "gregorian" },
     75     { "",                        UCAL_GREGORIAN, "gregorian" },
     76     { NULL,                      UCAL_GREGORIAN, "gregorian" },
     77     { NULL, 0, NULL } /* terminator */
     78 };
     79 
     80 static void TestCalendar()
     81 {
     82     UCalendar *caldef = 0, *caldef2 = 0, *calfr = 0, *calit = 0, *calfrclone = 0;
     83     UEnumeration* uenum = NULL;
     84     int32_t count, count2, i,j;
     85     UChar tzID[4];
     86     UChar *tzdname = 0;
     87     UErrorCode status = U_ZERO_ERROR;
     88     UDate now;
     89     UDateFormat *datdef = 0;
     90     UChar *result = 0;
     91     int32_t resultlength, resultlengthneeded;
     92     char tempMsgBuf[256];
     93     UChar zone1[32], zone2[32];
     94     const char *tzver = 0;
     95     UChar canonicalID[64];
     96     UBool isSystemID = FALSE;
     97     const UCalGetTypeTest * ucalGetTypeTestPtr;
     98 
     99 #ifdef U_USE_UCAL_OBSOLETE_2_8
    100     /*Testing countAvailableTimeZones*/
    101     int32_t offset=0;
    102     log_verbose("\nTesting ucal_countAvailableTZIDs\n");
    103     count=ucal_countAvailableTZIDs(offset);
    104     log_verbose("The number of timezone id's present with offset 0 are %d:\n", count);
    105     if(count < 5) /* Don't hard code an exact == test here! */
    106         log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count);
    107 
    108     /*Testing getAvailableTZIDs*/
    109     log_verbose("\nTesting ucal_getAvailableTZIDs");
    110     for(i=0;i<count;i++){
    111         ucal_getAvailableTZIDs(offset, i, &status);
    112         if(U_FAILURE(status)){
    113             log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status));
    114         }
    115         log_verbose("%s\n", u_austrcpy(tempMsgBuf, ucal_getAvailableTZIDs(offset, i, &status)));
    116     }
    117     /*get Illegal TZID where index >= count*/
    118     ucal_getAvailableTZIDs(offset, i, &status);
    119     if(status != U_INDEX_OUTOFBOUNDS_ERROR){
    120         log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status));
    121     }
    122     status=U_ZERO_ERROR;
    123 #endif
    124 
    125     /*Test ucal_openTimeZones & ucal_openCountryTimeZones*/
    126     for (j=0; j<2; ++j) {
    127         const char* api = (j==0) ? "ucal_openTimeZones()" :
    128             "ucal_openCountryTimeZones(US)";
    129         uenum = (j==0) ? ucal_openTimeZones(&status) :
    130             ucal_openCountryTimeZones("US", &status);
    131         if (U_FAILURE(status)) {
    132             log_err("FAIL: %s failed with %s", api,
    133                     u_errorName(status));
    134         } else {
    135             const char* id;
    136             int32_t len;
    137             count = uenum_count(uenum, &status);
    138             log_verbose("%s returned %d timezone id's:\n", api, count);
    139             if (count < 5) { /* Don't hard code an exact == test here! */
    140                 log_data_err("FAIL: in %s, got %d, expected at least 5 -> %s (Are you missing data?)\n", api, count, u_errorName(status));
    141             }
    142             uenum_reset(uenum, &status);
    143             if (U_FAILURE(status)){
    144                 log_err("FAIL: uenum_reset for %s returned %s\n",
    145                         api, u_errorName(status));
    146             }
    147             for (i=0; i<count; i++) {
    148                 id = uenum_next(uenum, &len, &status);
    149                 if (U_FAILURE(status)){
    150                     log_err("FAIL: uenum_next for %s returned %s\n",
    151                             api, u_errorName(status));
    152                 } else {
    153                     log_verbose("%s\n", id);
    154                 }
    155             }
    156             /* Next one should be NULL */
    157             id = uenum_next(uenum, &len, &status);
    158             if (id != NULL) {
    159                 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
    160                         api, id);
    161             }
    162         }
    163         uenum_close(uenum);
    164     }
    165 
    166     /*Test ucal_getDSTSavings*/
    167     status = U_ZERO_ERROR;
    168     i = ucal_getDSTSavings(fgGMTID, &status);
    169     if (U_FAILURE(status)) {
    170         log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
    171                 u_errorName(status));
    172     } else if (i != 0) {
    173         log_data_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0 (Are you missing data?)\n", i);
    174     }
    175     i = ucal_getDSTSavings(PST, &status);
    176     if (U_FAILURE(status)) {
    177         log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
    178                 u_errorName(status));
    179     } else if (i != 1*60*60*1000) {
    180         log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i, 1*60*60*1000);
    181     }
    182 
    183     /*Test ucal_set/getDefaultTimeZone*/
    184     status = U_ZERO_ERROR;
    185     i = ucal_getDefaultTimeZone(zone1, sizeof(zone1)/sizeof(zone1[0]), &status);
    186     if (U_FAILURE(status)) {
    187         log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
    188                 u_errorName(status));
    189     } else {
    190         ucal_setDefaultTimeZone(EUROPE_PARIS, &status);
    191         if (U_FAILURE(status)) {
    192             log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
    193                     u_errorName(status));
    194         } else {
    195             i = ucal_getDefaultTimeZone(zone2, sizeof(zone2)/sizeof(zone2[0]), &status);
    196             if (U_FAILURE(status)) {
    197                 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
    198                         u_errorName(status));
    199             } else {
    200                 if (u_strcmp(zone2, EUROPE_PARIS) != 0) {
    201                     log_data_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris (Are you missing data?)\n");
    202                 }
    203             }
    204         }
    205         status = U_ZERO_ERROR;
    206         ucal_setDefaultTimeZone(zone1, &status);
    207     }
    208 
    209     /*Test ucal_getTZDataVersion*/
    210     status = U_ZERO_ERROR;
    211     tzver = ucal_getTZDataVersion(&status);
    212     if (U_FAILURE(status)) {
    213         log_err_status(status, "FAIL: ucal_getTZDataVersion() => %s\n", u_errorName(status));
    214     } else if (uprv_strlen(tzver) != 5 /*4 digits + 1 letter*/) {
    215         log_err("FAIL: Bad version string was returned by ucal_getTZDataVersion\n");
    216     } else {
    217         log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver);
    218     }
    219 
    220     /*Testing ucal_getCanonicalTimeZoneID*/
    221     status = U_ZERO_ERROR;
    222     resultlength = ucal_getCanonicalTimeZoneID(PST, -1,
    223         canonicalID, sizeof(canonicalID)/sizeof(UChar), &isSystemID, &status);
    224     if (U_FAILURE(status)) {
    225         log_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status));
    226     } else {
    227         if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) {
    228             log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s (Are you missing data?)\n",
    229                 PST, canonicalID, AMERICA_LOS_ANGELES);
    230         }
    231         if (!isSystemID) {
    232             log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID (Are you missing data?)\n",
    233                 PST, isSystemID);
    234         }
    235     }
    236 
    237     /*Testing the  ucal_open() function*/
    238     status = U_ZERO_ERROR;
    239     log_verbose("\nTesting the ucal_open()\n");
    240     u_uastrcpy(tzID, "PST");
    241     caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
    242     if(U_FAILURE(status)){
    243         log_data_err("FAIL: error in ucal_open caldef : %s\n - (Are you missing data?)", u_errorName(status));
    244     }
    245 
    246     caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
    247     if(U_FAILURE(status)){
    248         log_data_err("FAIL: error in ucal_open caldef : %s - (Are you missing data?)\n", u_errorName(status));
    249     }
    250     u_strcpy(tzID, fgGMTID);
    251     calfr=ucal_open(tzID, u_strlen(tzID), "fr_FR", UCAL_TRADITIONAL, &status);
    252     if(U_FAILURE(status)){
    253         log_data_err("FAIL: error in ucal_open calfr : %s - (Are you missing data?)\n", u_errorName(status));
    254     }
    255     calit=ucal_open(tzID, u_strlen(tzID), "it_IT", UCAL_TRADITIONAL, &status);
    256     if(U_FAILURE(status))    {
    257         log_data_err("FAIL: error in ucal_open calit : %s - (Are you missing data?)\n", u_errorName(status));
    258     }
    259 
    260     /*Testing the  clone() function*/
    261     calfrclone = ucal_clone(calfr, &status);
    262     if(U_FAILURE(status)){
    263         log_data_err("FAIL: error in ucal_clone calfr : %s - (Are you missing data?)\n", u_errorName(status));
    264     }
    265 
    266     /*Testing udat_getAvailable() and udat_countAvailable()*/
    267     log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
    268     count=ucal_countAvailable();
    269     /* use something sensible w/o hardcoding the count */
    270     if(count > 0) {
    271         log_verbose("PASS: ucal_countAvailable() works fine\n");
    272         log_verbose("The no: of locales for which calendars are avilable are %d\n", count);
    273     } else {
    274         log_data_err("FAIL: Error in countAvailable()\n");
    275     }
    276 
    277     for(i=0;i<count;i++) {
    278        log_verbose("%s\n", ucal_getAvailable(i));
    279     }
    280 
    281 
    282     /*Testing the equality between calendar's*/
    283     log_verbose("\nTesting ucal_equivalentTo()\n");
    284     if(caldef && caldef2 && calfr && calit) {
    285       if(ucal_equivalentTo(caldef, caldef2) == FALSE || ucal_equivalentTo(caldef, calfr)== TRUE ||
    286         ucal_equivalentTo(caldef, calit)== TRUE || ucal_equivalentTo(calfr, calfrclone) == FALSE) {
    287           log_data_err("FAIL: Error. equivalentTo test failed (Are you missing data?)\n");
    288       } else {
    289           log_verbose("PASS: equivalentTo test passed\n");
    290       }
    291     }
    292 
    293 
    294     /*Testing the current time and date using ucal_getnow()*/
    295     log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
    296     now=ucal_getNow();
    297     /* open the date format and format the date to check the output */
    298     datdef=udat_open(UDAT_FULL,UDAT_FULL ,NULL, NULL, 0,NULL,0,&status);
    299     if(U_FAILURE(status)){
    300         log_data_err("FAIL: error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
    301         return;
    302     }
    303     log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datdef, now)) );
    304 
    305 
    306     /*Testing the TimeZoneDisplayName */
    307     log_verbose("\nTesting the fetching of time zone display name\n");
    308     /*for the US locale */
    309     resultlength=0;
    310     resultlengthneeded=ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", NULL, resultlength, &status);
    311 
    312     if(status==U_BUFFER_OVERFLOW_ERROR)
    313     {
    314         status=U_ZERO_ERROR;
    315         resultlength=resultlengthneeded+1;
    316         result=(UChar*)malloc(sizeof(UChar) * resultlength);
    317         ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", result, resultlength, &status);
    318     }
    319     if(U_FAILURE(status))    {
    320         log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status));
    321     }
    322     else{
    323         log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
    324             u_errorName(status), resultlengthneeded);
    325     }
    326 
    327 
    328 #define expectPDT "Pacific Daylight Time"
    329 
    330     tzdname=(UChar*)malloc(sizeof(UChar) * (sizeof(expectPDT)+1));
    331     u_uastrcpy(tzdname, expectPDT);
    332     if(u_strcmp(tzdname, result)==0){
    333         log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf, result) );
    334     }
    335     else{
    336         log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result) , expectPDT);
    337     }
    338 
    339     ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_DST, "en_US", result, resultlength, &status);
    340     u_uastrcpy(tzdname, "PDT");
    341     if(u_strcmp(tzdname, result) != 0){
    342         log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
    343     }
    344 
    345     ucal_getTimeZoneDisplayName(caldef, UCAL_STANDARD, "en_US", result, resultlength, &status);
    346     u_uastrcpy(tzdname, "Pacific Standard Time");
    347     if(u_strcmp(tzdname, result) != 0){
    348         log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
    349     }
    350 
    351     ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_STANDARD, "en_US", result, resultlength, &status);
    352     u_uastrcpy(tzdname, "PST");
    353     if(u_strcmp(tzdname, result) != 0){
    354         log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
    355     }
    356 
    357 
    358     /*testing the setAttributes and getAttributes of a UCalendar*/
    359     log_verbose("\nTesting the getAttributes and set Attributes\n");
    360     count=ucal_getAttribute(calit, UCAL_LENIENT);
    361     count2=ucal_getAttribute(calfr, UCAL_LENIENT);
    362     ucal_setAttribute(calit, UCAL_LENIENT, 0);
    363     ucal_setAttribute(caldef, UCAL_LENIENT, count2);
    364     if( ucal_getAttribute(calit, UCAL_LENIENT) !=0 ||
    365         ucal_getAttribute(calfr, UCAL_LENIENT)!=ucal_getAttribute(caldef, UCAL_LENIENT) )
    366         log_err("FAIL: there is an error in getAttributes or setAttributes\n");
    367     else
    368         log_verbose("PASS: attribute set and got successfully\n");
    369         /*set it back to orginal value */
    370     log_verbose("Setting it back to normal\n");
    371     ucal_setAttribute(calit, UCAL_LENIENT, count);
    372     if(ucal_getAttribute(calit, UCAL_LENIENT)!=count)
    373         log_err("FAIL: Error in setting the attribute back to normal\n");
    374 
    375     /*setting the first day of the week to other values  */
    376     count=ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK);
    377     for (i=1; i<=7; ++i) {
    378         ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,i);
    379         if (ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK) != i)
    380             log_err("FAIL: set/getFirstDayOfWeek failed\n");
    381     }
    382     /*get bogus Attribute*/
    383     count=ucal_getAttribute(calit, (UCalendarAttribute)99); /* BOGUS_ATTRIBUTE */
    384     if(count != -1){
    385         log_err("FAIL: get/bogus attribute should return -1\n");
    386     }
    387 
    388     /*set it back to normal */
    389     ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,count);
    390     /*setting minimal days of the week to other values */
    391     count=ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
    392     for (i=1; i<=7; ++i) {
    393         ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,i);
    394         if (ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK) != i)
    395             log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
    396     }
    397     /*set it back to normal */
    398     ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,count);
    399 
    400 
    401     /*testing if the UCalendar's timezone is currently in day light saving's time*/
    402     log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
    403     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 10, 45, 20, &status);
    404     ucal_inDaylightTime(caldef, &status );
    405     if(U_FAILURE(status))    {
    406         log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status));
    407     }
    408     if(!ucal_inDaylightTime(caldef, &status))
    409         log_verbose("PASS: It is not in daylight saving's time\n");
    410     else
    411         log_err("FAIL: It is not in daylight saving's time\n");
    412 
    413     /*closing the UCalendar*/
    414     ucal_close(caldef);
    415     ucal_close(caldef2);
    416     ucal_close(calfr);
    417     ucal_close(calit);
    418     ucal_close(calfrclone);
    419 
    420     /*testing ucal_getType, and ucal_open with UCAL_GREGORIAN*/
    421     for (ucalGetTypeTestPtr = ucalGetTypeTests; ucalGetTypeTestPtr->expectedResult != NULL; ++ucalGetTypeTestPtr) {
    422         const char * localeToDisplay = (ucalGetTypeTestPtr->locale != NULL)? ucalGetTypeTestPtr->locale: "<NULL>";
    423         status = U_ZERO_ERROR;
    424         caldef = ucal_open(NULL, 0, ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType, &status);
    425         if ( U_SUCCESS(status) ) {
    426             const char * calType = ucal_getType(caldef, &status);
    427             if ( U_SUCCESS(status) && calType != NULL ) {
    428                 if ( strcmp( calType, ucalGetTypeTestPtr->expectedResult ) != 0 ) {
    429                     log_err("FAIL: ucal_open %s type %d does not return %s calendar\n", localeToDisplay,
    430                                                 ucalGetTypeTestPtr->calType, ucalGetTypeTestPtr->expectedResult);
    431                 }
    432             } else {
    433                 log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
    434             }
    435             ucal_close(caldef);
    436         } else {
    437             log_err("FAIL: ucal_open %s type %d fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
    438         }
    439     }
    440 
    441     /*closing the UDateFormat used */
    442     udat_close(datdef);
    443     free(result);
    444     free(tzdname);
    445 }
    446 
    447 /*------------------------------------------------------*/
    448 /*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
    449 
    450 static void TestGetSetDateAPI()
    451 {
    452     UCalendar *caldef = 0, *caldef2 = 0;
    453     UChar tzID[4];
    454     UDate d1;
    455     int32_t hour;
    456     int32_t zoneOffset;
    457     UDateFormat *datdef = 0;
    458     UErrorCode status=U_ZERO_ERROR;
    459     UDate d2= 837039928046.0;
    460     UChar temp[30];
    461 
    462     log_verbose("\nOpening the calendars()\n");
    463     u_strcpy(tzID, fgGMTID);
    464     /*open the calendars used */
    465     caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
    466     caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
    467     /*open the dateformat */
    468     /* this is supposed to open default date format, but later on it treats it like it is "en_US"
    469        - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
    470     /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
    471     datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US",fgGMTID,-1,NULL,0, &status);
    472     if(U_FAILURE(status))
    473     {
    474         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
    475         return;
    476     }
    477 
    478 
    479     /*Testing getMillis and setMillis */
    480     log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
    481     d1=ucal_getMillis(caldef, &status);
    482     if(U_FAILURE(status)){
    483         log_err("Error in getMillis : %s\n", u_errorName(status));
    484     }
    485 
    486     /*testing setMillis */
    487     log_verbose("\nTesting the set date and time function using setMillis\n");
    488     ucal_setMillis(caldef, d2, &status);
    489     if(U_FAILURE(status)){
    490         log_err("Error in setMillis : %s\n", u_errorName(status));
    491     }
    492 
    493     /*testing if the calendar date is set properly or not  */
    494     d1=ucal_getMillis(caldef, &status);
    495     if(u_strcmp(myDateFormat(datdef, d1), myDateFormat(datdef, d2))!=0)
    496         log_err("error in setMillis or getMillis\n");
    497     /*-------------------*/
    498 
    499 
    500 
    501     ctest_setTimeZone(NULL, &status);
    502 
    503     /*testing ucal_setTimeZone() function*/
    504     log_verbose("\nTesting if the function ucal_setTimeZone() works fine\n");
    505     ucal_setMillis(caldef2, d2, &status);
    506     if(U_FAILURE(status)){
    507         log_err("Error in getMillis : %s\n", u_errorName(status));;
    508     }
    509     hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
    510 
    511     u_uastrcpy(tzID, "PST");
    512     ucal_setTimeZone(caldef2,tzID, 3, &status);
    513     if(U_FAILURE(status)){
    514         log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
    515     }
    516     else
    517         log_verbose("ucal_setTimeZone worked fine\n");
    518     if(hour == ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status))
    519         log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
    520     else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */
    521         log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
    522     else
    523         log_verbose("PASS: setTimeZone works fine\n");
    524 
    525     /*testing setTimeZone roundtrip */
    526     log_verbose("\nTesting setTimeZone() roundtrip\n");
    527     u_strcpy(tzID, fgGMTID);
    528     ucal_setTimeZone(caldef2, tzID, 3, &status);
    529     if(U_FAILURE(status)){
    530         log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
    531     }
    532     if(d2==ucal_getMillis(caldef2, &status))
    533         log_verbose("PASS: setTimeZone roundtrip test passed\n");
    534     else
    535         log_err("FAIL: setTimeZone roundtrip test failed\n");
    536 
    537     zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
    538     if(U_FAILURE(status)){
    539         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
    540     }
    541     else if (zoneOffset != 0) {
    542         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
    543     }
    544 
    545     ucal_setTimeZone(caldef2, NULL, -1, &status);
    546     if(U_FAILURE(status)){
    547         log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
    548     }
    549     if(ucal_getMillis(caldef2, &status))
    550         log_verbose("PASS: setTimeZone roundtrip test passed\n");
    551     else
    552         log_err("FAIL: setTimeZone roundtrip test failed\n");
    553 
    554     zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
    555     if(U_FAILURE(status)){
    556         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
    557     }
    558     else if (zoneOffset != -28800000) {
    559         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
    560     }
    561 
    562     ctest_resetTimeZone();
    563 
    564 /*----------------------------*     */
    565 
    566 
    567 
    568     /*Testing  if setDate works fine  */
    569     log_verbose("\nTesting the ucal_setDate() function \n");
    570     u_uastrcpy(temp, "Dec 17, 1971 11:05:28 PM");
    571     ucal_setDate(caldef,1971, UCAL_DECEMBER, 17, &status);
    572     if(U_FAILURE(status)){
    573         log_err("error in setting the calendar date : %s\n", u_errorName(status));
    574     }
    575     /*checking if the calendar date is set properly or not  */
    576     d1=ucal_getMillis(caldef, &status);
    577     if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
    578         log_verbose("PASS:setDate works fine\n");
    579     else
    580         log_err("FAIL:Error in setDate()\n");
    581 
    582 
    583     /* Testing setDate Extensively with various input values */
    584     log_verbose("\nTesting ucal_setDate() extensively\n");
    585     ucal_setDate(caldef, 1999, UCAL_JANUARY, 10, &status);
    586     verify1("1999  10th day of January  is :", caldef, datdef, 1999, UCAL_JANUARY, 10);
    587     ucal_setDate(caldef, 1999, UCAL_DECEMBER, 3, &status);
    588     verify1("1999 3rd day of December  is :", caldef, datdef, 1999, UCAL_DECEMBER, 3);
    589     ucal_setDate(caldef, 2000, UCAL_MAY, 3, &status);
    590     verify1("2000 3rd day of May is :", caldef, datdef, 2000, UCAL_MAY, 3);
    591     ucal_setDate(caldef, 1999, UCAL_AUGUST, 32, &status);
    592     verify1("1999 32th day of August is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1);
    593     ucal_setDate(caldef, 1999, UCAL_MARCH, 0, &status);
    594     verify1("1999 0th day of March is :", caldef, datdef, 1999, UCAL_FEBRUARY, 28);
    595     ucal_setDate(caldef, 0, UCAL_MARCH, 12, &status);
    596 
    597     /*--------------------*/
    598 
    599     /*Testing if setDateTime works fine */
    600     log_verbose("\nTesting the ucal_setDateTime() function \n");
    601     u_uastrcpy(temp, "May 3, 1972 4:30:42 PM");
    602     ucal_setDateTime(caldef,1972, UCAL_MAY, 3, 16, 30, 42, &status);
    603     if(U_FAILURE(status)){
    604         log_err("error in setting the calendar date : %s\n", u_errorName(status));
    605     }
    606     /*checking  if the calendar date is set properly or not  */
    607     d1=ucal_getMillis(caldef, &status);
    608     if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
    609         log_verbose("PASS: setDateTime works fine\n");
    610     else
    611         log_err("FAIL: Error in setDateTime\n");
    612 
    613 
    614 
    615     /*Testing setDateTime extensively with various input values*/
    616     log_verbose("\nTesting ucal_setDateTime() function extensively\n");
    617     ucal_setDateTime(caldef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, &status);
    618     verify2("1999  10th day of October  at 6:45:30 is :", caldef, datdef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, 0 );
    619     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 15, 10, 55, &status);
    620     verify2("1999 3rd day of March   at 15:10:55 is :", caldef, datdef, 1999, UCAL_MARCH, 3, 3, 10, 55, 1);
    621     ucal_setDateTime(caldef, 1999, UCAL_MAY, 3, 25, 30, 45, &status);
    622     verify2("1999 3rd day of May at 25:30:45 is :", caldef, datdef, 1999, UCAL_MAY, 4, 1, 30, 45, 0);
    623     ucal_setDateTime(caldef, 1999, UCAL_AUGUST, 32, 22, 65, 40, &status);
    624     verify2("1999 32th day of August at 22:65:40 is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1, 11, 5, 40,1);
    625     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, 0, 0, 0,&status);
    626     verify2("1999 12th day of March at 0:0:0 is :", caldef, datdef, 1999, UCAL_MARCH, 12, 0, 0, 0, 0);
    627     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, -10, -10,0, &status);
    628     verify2("1999 12th day of March is at -10:-10:0 :", caldef, datdef, 1999, UCAL_MARCH, 11, 1, 50, 0, 1);
    629 
    630 
    631 
    632     /*close caldef and datdef*/
    633     ucal_close(caldef);
    634     ucal_close(caldef2);
    635     udat_close(datdef);
    636 }
    637 
    638 /*----------------------------------------------------------- */
    639 /**
    640  * Confirm the functioning of the calendar field related functions.
    641  */
    642 static void TestFieldGetSet()
    643 {
    644     UCalendar *cal = 0;
    645     UChar tzID[4];
    646     UDateFormat *datdef = 0;
    647     UDate d1;
    648     UErrorCode status=U_ZERO_ERROR;
    649     log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
    650     u_strcpy(tzID, fgGMTID);
    651     /*open the calendar used */
    652     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
    653     if (U_FAILURE(status)) {
    654         log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
    655         return;
    656     }
    657     datdef=udat_open(UDAT_SHORT,UDAT_SHORT ,NULL,fgGMTID,-1,NULL, 0, &status);
    658     if(U_FAILURE(status))
    659     {
    660         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
    661     }
    662 
    663     /*Testing ucal_get()*/
    664     log_verbose("\nTesting the ucal_get() function of Calendar\n");
    665     ucal_setDateTime(cal, 1999, UCAL_MARCH, 12, 5, 25, 30, &status);
    666     if(U_FAILURE(status)){
    667         log_data_err("error in the setDateTime() : %s (Are you missing data?)\n", u_errorName(status));
    668     }
    669     if(ucal_get(cal, UCAL_YEAR, &status)!=1999 || ucal_get(cal, UCAL_MONTH, &status)!=2 ||
    670         ucal_get(cal, UCAL_DATE, &status)!=12 || ucal_get(cal, UCAL_HOUR, &status)!=5)
    671         log_data_err("error in ucal_get() -> %s (Are you missing data?)\n", u_errorName(status));
    672     else if(ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status)!=2 || ucal_get(cal, UCAL_DAY_OF_WEEK, &status)!=6
    673         || ucal_get(cal, UCAL_WEEK_OF_MONTH, &status)!=2 || ucal_get(cal, UCAL_WEEK_OF_YEAR, &status)!= 10)
    674         log_err("FAIL: error in ucal_get()\n");
    675     else
    676         log_verbose("PASS: ucal_get() works fine\n");
    677 
    678     /*Testing the ucal_set() , ucal_clear() functions of calendar*/
    679     log_verbose("\nTesting the set, and clear  field functions of calendar\n");
    680     ucal_setAttribute(cal, UCAL_LENIENT, 0);
    681     ucal_clear(cal);
    682     ucal_set(cal, UCAL_YEAR, 1997);
    683     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
    684     ucal_set(cal, UCAL_DATE, 3);
    685     verify1("1997 third day of June = ", cal, datdef, 1997, UCAL_JUNE, 3);
    686     ucal_clear(cal);
    687     ucal_set(cal, UCAL_YEAR, 1997);
    688     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    689     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
    690     ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 1);
    691     verify1("1997 first Tuesday in June = ", cal, datdef, 1997, UCAL_JUNE, 3);
    692     ucal_clear(cal);
    693     ucal_set(cal, UCAL_YEAR, 1997);
    694     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    695     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
    696     ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, - 1);
    697     verify1("1997 last Tuesday in June = ", cal, datdef,1997,   UCAL_JUNE, 24);
    698     /*give undesirable input    */
    699     status = U_ZERO_ERROR;
    700     ucal_clear(cal);
    701     ucal_set(cal, UCAL_YEAR, 1997);
    702     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    703     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
    704     ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 0);
    705     d1 = ucal_getMillis(cal, &status);
    706     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
    707         log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 zero-th Tuesday in June\n");
    708     } else {
    709         log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
    710     }
    711     status = U_ZERO_ERROR;
    712     ucal_clear(cal);
    713     ucal_set(cal, UCAL_YEAR, 1997);
    714     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    715     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
    716     ucal_set(cal, UCAL_WEEK_OF_MONTH, 1);
    717     verify1("1997 Tuesday in week 1 of June = ", cal,datdef, 1997, UCAL_JUNE, 3);
    718     ucal_clear(cal);
    719     ucal_set(cal, UCAL_YEAR, 1997);
    720     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    721     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
    722     ucal_set(cal, UCAL_WEEK_OF_MONTH, 5);
    723     verify1("1997 Tuesday in week 5 of June = ", cal,datdef, 1997, UCAL_JULY, 1);
    724     status = U_ZERO_ERROR;
    725     ucal_clear(cal);
    726     ucal_set(cal, UCAL_YEAR, 1997);
    727     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    728     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
    729     ucal_set(cal, UCAL_WEEK_OF_MONTH, 0);
    730     ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
    731     d1 = ucal_getMillis(cal,&status);
    732     if (status != U_ILLEGAL_ARGUMENT_ERROR){
    733         log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 Tuesday zero-th week in June\n");
    734     } else {
    735         log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
    736     }
    737     status = U_ZERO_ERROR;
    738     ucal_clear(cal);
    739     ucal_set(cal, UCAL_YEAR_WOY, 1997);
    740     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    741     ucal_set(cal, UCAL_WEEK_OF_YEAR, 1);
    742     verify1("1997 Tuesday in week 1 of year = ", cal, datdef,1996, UCAL_DECEMBER, 31);
    743     ucal_clear(cal);
    744     ucal_set(cal, UCAL_YEAR, 1997);
    745     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
    746     ucal_set(cal, UCAL_WEEK_OF_YEAR, 10);
    747     verify1("1997 Tuesday in week 10 of year = ", cal,datdef, 1997, UCAL_MARCH, 4);
    748     ucal_clear(cal);
    749     ucal_set(cal, UCAL_YEAR, 1999);
    750     ucal_set(cal, UCAL_DAY_OF_YEAR, 1);
    751     verify1("1999 1st day of the year =", cal, datdef, 1999, UCAL_JANUARY, 1);
    752     ucal_set(cal, UCAL_MONTH, -3);
    753     d1 = ucal_getMillis(cal,&status);
    754     if (status != U_ILLEGAL_ARGUMENT_ERROR){
    755         log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1999 -3th month\n");
    756     } else {
    757         log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
    758     }
    759 
    760     ucal_setAttribute(cal, UCAL_LENIENT, 1);
    761 
    762     ucal_set(cal, UCAL_MONTH, -3);
    763     verify1("1999 -3th month should be", cal, datdef, 1998, UCAL_OCTOBER, 1);
    764 
    765 
    766     /*testing isSet and clearField()*/
    767     if(!ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
    768         log_err("FAIL: error in isSet\n");
    769     else
    770         log_verbose("PASS: isSet working fine\n");
    771     ucal_clearField(cal, UCAL_WEEK_OF_YEAR);
    772     if(ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
    773         log_err("FAIL: there is an error in clearField or isSet\n");
    774     else
    775         log_verbose("PASS :clearField working fine\n");
    776 
    777     /*-------------------------------*/
    778 
    779     ucal_close(cal);
    780     udat_close(datdef);
    781 }
    782 
    783 
    784 
    785 /* ------------------------------------- */
    786 /**
    787  * Execute adding and rolling in Calendar extensively,
    788  */
    789 static void TestAddRollExtensive()
    790 {
    791     UCalendar *cal = 0;
    792     int32_t i,limit;
    793     UChar tzID[4];
    794     UCalendarDateFields e;
    795     int32_t y,m,d,hr,min,sec,ms;
    796     int32_t maxlimit = 40;
    797     UErrorCode status = U_ZERO_ERROR;
    798     y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
    799 
    800     log_verbose("Testing add and roll extensively\n");
    801 
    802     u_uastrcpy(tzID, "PST");
    803     /*open the calendar used */
    804     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
    805     if (U_FAILURE(status)) {
    806         log_data_err("ucal_open() failed : %s - (Are you missing data?)\n", u_errorName(status));
    807         return;
    808     }
    809 
    810     ucal_set(cal, UCAL_YEAR, y);
    811     ucal_set(cal, UCAL_MONTH, m);
    812     ucal_set(cal, UCAL_DATE, d);
    813     ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
    814 
    815     /* Confirm that adding to various fields works.*/
    816     log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
    817     checkDate(cal, y, m, d);
    818     ucal_add(cal,UCAL_YEAR, 1, &status);
    819     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status)); return; }
    820     y++;
    821     checkDate(cal, y, m, d);
    822     ucal_add(cal,UCAL_MONTH, 12, &status);
    823     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
    824     y+=1;
    825     checkDate(cal, y, m, d);
    826     ucal_add(cal,UCAL_DATE, 1, &status);
    827     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
    828     d++;
    829     checkDate(cal, y, m, d);
    830     ucal_add(cal,UCAL_DATE, 2, &status);
    831     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
    832     d += 2;
    833     checkDate(cal, y, m, d);
    834     ucal_add(cal,UCAL_DATE, 28, &status);
    835     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
    836     ++m;
    837     checkDate(cal, y, m, d);
    838     ucal_add(cal, (UCalendarDateFields)-1, 10, &status);
    839     if(status==U_ILLEGAL_ARGUMENT_ERROR)
    840         log_verbose("Pass: Illegal argument error as expected\n");
    841     else{
    842         log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status));
    843     }
    844     status=U_ZERO_ERROR;
    845 
    846 
    847     /*confirm that applying roll to various fields works fine*/
    848     log_verbose("\nTesting to confirm that ucal_roll() works\n");
    849     ucal_roll(cal, UCAL_DATE, -1, &status);
    850     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
    851     d -=1;
    852     checkDate(cal, y, m, d);
    853     ucal_roll(cal, UCAL_MONTH, -2, &status);
    854     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
    855     m -=2;
    856     checkDate(cal, y, m, d);
    857     ucal_roll(cal, UCAL_DATE, 1, &status);
    858     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
    859     d +=1;
    860     checkDate(cal, y, m, d);
    861     ucal_roll(cal, UCAL_MONTH, -12, &status);
    862     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
    863     checkDate(cal, y, m, d);
    864     ucal_roll(cal, UCAL_YEAR, -1, &status);
    865     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
    866     y -=1;
    867     checkDate(cal, y, m, d);
    868     ucal_roll(cal, UCAL_DATE, 29, &status);
    869     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
    870     d = 2;
    871     checkDate(cal, y, m, d);
    872     ucal_roll(cal, (UCalendarDateFields)-1, 10, &status);
    873     if(status==U_ILLEGAL_ARGUMENT_ERROR)
    874         log_verbose("Pass: illegal arguement error as expected\n");
    875     else{
    876         log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status));
    877         return;
    878     }
    879     status=U_ZERO_ERROR;
    880     ucal_clear(cal);
    881     ucal_setDateTime(cal, 1999, UCAL_FEBRUARY, 28, 10, 30, 45,  &status);
    882     if(U_FAILURE(status)){
    883         log_err("error is setting the datetime: %s\n", u_errorName(status));
    884     }
    885     ucal_add(cal, UCAL_MONTH, 1, &status);
    886     checkDate(cal, 1999, UCAL_MARCH, 28);
    887     ucal_add(cal, UCAL_MILLISECOND, 1000, &status);
    888     checkDateTime(cal, 1999, UCAL_MARCH, 28, 10, 30, 46, 0, UCAL_MILLISECOND);
    889 
    890     ucal_close(cal);
    891 /*--------------- */
    892     status=U_ZERO_ERROR;
    893     /* Testing add and roll extensively */
    894     log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
    895     y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
    896     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
    897     if (U_FAILURE(status)) {
    898         log_err("ucal_open failed: %s\n", u_errorName(status));
    899         return;
    900     }
    901     ucal_set(cal, UCAL_YEAR, y);
    902     ucal_set(cal, UCAL_MONTH, m);
    903     ucal_set(cal, UCAL_DATE, d);
    904     ucal_set(cal, UCAL_HOUR, hr);
    905     ucal_set(cal, UCAL_MINUTE, min);
    906     ucal_set(cal, UCAL_SECOND,sec);
    907     ucal_set(cal, UCAL_MILLISECOND, ms);
    908     ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
    909     status=U_ZERO_ERROR;
    910 
    911     log_verbose("\nTesting UCalendar add...\n");
    912     for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
    913         limit = maxlimit;
    914         status = U_ZERO_ERROR;
    915         for (i = 0; i < limit; i++) {
    916             ucal_add(cal, e, 1, &status);
    917             if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
    918         }
    919         for (i = 0; i < limit; i++) {
    920             ucal_add(cal, e, -1, &status);
    921             if (U_FAILURE(status)) {
    922                 log_err("ucal_add -1 failed: %s\n", u_errorName(status));
    923                 return;
    924             }
    925         }
    926         checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
    927     }
    928     log_verbose("\nTesting calendar ucal_roll()...\n");
    929     for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
    930         limit = maxlimit;
    931         status = U_ZERO_ERROR;
    932         for (i = 0; i < limit; i++) {
    933             ucal_roll(cal, e, 1, &status);
    934             if (U_FAILURE(status)) {
    935                 limit = i;
    936                 status = U_ZERO_ERROR;
    937             }
    938         }
    939         for (i = 0; i < limit; i++) {
    940             ucal_roll(cal, e, -1, &status);
    941             if (U_FAILURE(status)) {
    942                 log_err("ucal_roll -1 failed: %s\n", u_errorName(status));
    943                 return;
    944             }
    945         }
    946         checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
    947     }
    948 
    949     ucal_close(cal);
    950 }
    951 
    952 /*------------------------------------------------------ */
    953 /*Testing the Limits for various Fields of Calendar*/
    954 static void TestGetLimits()
    955 {
    956     UCalendar *cal = 0;
    957     int32_t min, max, gr_min, le_max, ac_min, ac_max, val;
    958     UChar tzID[4];
    959     UErrorCode status = U_ZERO_ERROR;
    960 
    961 
    962     u_uastrcpy(tzID, "PST");
    963     /*open the calendar used */
    964     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
    965     if (U_FAILURE(status)) {
    966         log_data_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s - (Are you missing data?)\n", u_errorName(status));
    967         return;
    968     }
    969 
    970     log_verbose("\nTesting the getLimits function for various fields\n");
    971 
    972 
    973 
    974     ucal_setDate(cal, 1999, UCAL_MARCH, 5, &status); /* Set the date to be March 5, 1999 */
    975     val = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
    976     min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MINIMUM, &status);
    977     max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MAXIMUM, &status);
    978     if ( (min != UCAL_SUNDAY || max != UCAL_SATURDAY ) && (min > val && val > max)  && (val != UCAL_FRIDAY)){
    979            log_err("FAIL: Min/max bad\n");
    980            log_err("FAIL: Day of week %d out of range\n", val);
    981            log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val);
    982     }
    983     else
    984         log_verbose("getLimits successful\n");
    985 
    986     val = ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status);
    987     min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MINIMUM, &status);
    988     max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MAXIMUM, &status);
    989     if ( (min != 0 || max != 5 ) && (min > val && val > max)  && (val != 1)){
    990            log_err("FAIL: Min/max bad\n");
    991            log_err("FAIL: Day of week in month %d out of range\n", val);
    992            log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val);
    993 
    994     }
    995     else
    996         log_verbose("getLimits successful\n");
    997 
    998     min=ucal_getLimit(cal, UCAL_MONTH, UCAL_MINIMUM, &status);
    999     max=ucal_getLimit(cal, UCAL_MONTH, UCAL_MAXIMUM, &status);
   1000     gr_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_GREATEST_MINIMUM, &status);
   1001     le_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_LEAST_MAXIMUM, &status);
   1002     ac_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status);
   1003     ac_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MAXIMUM, &status);
   1004     if(U_FAILURE(status)){
   1005         log_err("Error in getLimits: %s\n", u_errorName(status));
   1006     }
   1007     if(min!=0 || max!=11 || gr_min!=0 || le_max!=11 || ac_min!=0 || ac_max!=11)
   1008         log_err("There is and error in getLimits in fetching the values\n");
   1009     else
   1010         log_verbose("getLimits successful\n");
   1011 
   1012     ucal_setDateTime(cal, 1999, UCAL_MARCH, 5, 4, 10, 35, &status);
   1013     val=ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
   1014     min=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MINIMUM, &status);
   1015     max=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MAXIMUM, &status);
   1016     gr_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_GREATEST_MINIMUM, &status);
   1017     le_max=ucal_getLimit(cal, UCAL_MINUTE, UCAL_LEAST_MAXIMUM, &status);
   1018     ac_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status);
   1019     ac_max=ucal_getLimit(cal, UCAL_SECOND, UCAL_ACTUAL_MAXIMUM, &status);
   1020     if( (min!=0 || max!= 11 || gr_min!=0 || le_max!=60 || ac_min!=0 || ac_max!=60) &&
   1021         (min>val && val>max) && val!=4){
   1022 
   1023         log_err("FAIL: Min/max bad\n");
   1024         log_err("FAIL: Hour of Day %d out of range\n", val);
   1025         log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val);
   1026     }
   1027     else
   1028         log_verbose("getLimits successful\n");
   1029 
   1030 
   1031     /*get BOGUS_LIMIT type*/
   1032     val=ucal_getLimit(cal, UCAL_SECOND, (UCalendarLimitType)99, &status);
   1033     if(val != -1){
   1034         log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
   1035     }
   1036     status=U_ZERO_ERROR;
   1037 
   1038 
   1039     ucal_close(cal);
   1040 }
   1041 
   1042 
   1043 
   1044 /* ------------------------------------- */
   1045 
   1046 /**
   1047  * Test that the days of the week progress properly when add is called repeatedly
   1048  * for increments of 24 days.
   1049  */
   1050 static void TestDOWProgression()
   1051 {
   1052     int32_t initialDOW, DOW, newDOW, expectedDOW;
   1053     UCalendar *cal = 0;
   1054     UDateFormat *datfor = 0;
   1055     UDate date1;
   1056     int32_t delta=24;
   1057     UErrorCode status = U_ZERO_ERROR;
   1058     UChar tzID[4];
   1059     char tempMsgBuf[256];
   1060     u_strcpy(tzID, fgGMTID);
   1061     /*open the calendar used */
   1062     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);;
   1063     if (U_FAILURE(status)) {
   1064         log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
   1065         return;
   1066     }
   1067 
   1068     datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
   1069     if(U_FAILURE(status)){
   1070         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
   1071     }
   1072 
   1073 
   1074     ucal_setDate(cal, 1999, UCAL_JANUARY, 1, &status);
   1075 
   1076     log_verbose("\nTesting the DOW progression\n");
   1077 
   1078     initialDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
   1079     if (U_FAILURE(status)) { log_data_err("ucal_get() failed: %s (Are you missing data?)\n", u_errorName(status) ); return; }
   1080     newDOW = initialDOW;
   1081     do {
   1082         DOW = newDOW;
   1083         log_verbose("DOW = %d...\n", DOW);
   1084         date1=ucal_getMillis(cal, &status);
   1085         if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
   1086         log_verbose("%s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)));
   1087 
   1088         ucal_add(cal,UCAL_DAY_OF_WEEK, delta, &status);
   1089         if (U_FAILURE(status)) { log_err("ucal_add() failed: %s\n", u_errorName(status)); return; }
   1090 
   1091         newDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
   1092         if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status)); return; }
   1093         expectedDOW = 1 + (DOW + delta - 1) % 7;
   1094         date1=ucal_getMillis(cal, &status);
   1095         if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
   1096         if (newDOW != expectedDOW) {
   1097             log_err("Day of week should be %d instead of %d on %s", expectedDOW, newDOW,
   1098                 u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
   1099             return;
   1100         }
   1101     }
   1102     while (newDOW != initialDOW);
   1103 
   1104     ucal_close(cal);
   1105     udat_close(datfor);
   1106 }
   1107 
   1108 /* ------------------------------------- */
   1109 
   1110 /**
   1111  * Confirm that the offset between local time and GMT behaves as expected.
   1112  */
   1113 static void TestGMTvsLocal()
   1114 {
   1115     log_verbose("\nTesting the offset between the GMT and local time\n");
   1116     testZones(1999, 1, 1, 12, 0, 0);
   1117     testZones(1999, 4, 16, 18, 30, 0);
   1118     testZones(1998, 12, 17, 19, 0, 0);
   1119 }
   1120 
   1121 /* ------------------------------------- */
   1122 
   1123 static void testZones(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
   1124 {
   1125     int32_t offset,utc, expected;
   1126     UCalendar *gmtcal = 0, *cal = 0;
   1127     UDate date1;
   1128     double temp;
   1129     UDateFormat *datfor = 0;
   1130     UErrorCode status = U_ZERO_ERROR;
   1131     UChar tzID[4];
   1132     char tempMsgBuf[256];
   1133 
   1134     u_strcpy(tzID, fgGMTID);
   1135     gmtcal=ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);;
   1136     if (U_FAILURE(status)) {
   1137         log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
   1138         return;
   1139     }
   1140     u_uastrcpy(tzID, "PST");
   1141     cal = ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);
   1142     if (U_FAILURE(status)) {
   1143         log_err("ucal_open failed: %s\n", u_errorName(status));
   1144         return;
   1145     }
   1146 
   1147     datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
   1148     if(U_FAILURE(status)){
   1149         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
   1150     }
   1151 
   1152     ucal_setDateTime(gmtcal, yr, mo - 1, dt, hr, mn, sc, &status);
   1153     if (U_FAILURE(status)) {
   1154         log_data_err("ucal_setDateTime failed: %s (Are you missing data?)\n", u_errorName(status));
   1155         return;
   1156     }
   1157     ucal_set(gmtcal, UCAL_MILLISECOND, 0);
   1158     date1 = ucal_getMillis(gmtcal, &status);
   1159     if (U_FAILURE(status)) {
   1160         log_err("ucal_getMillis failed: %s\n", u_errorName(status));
   1161         return;
   1162     }
   1163     log_verbose("date = %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
   1164 
   1165 
   1166     ucal_setMillis(cal, date1, &status);
   1167     if (U_FAILURE(status)) {
   1168         log_err("ucal_setMillis() failed: %s\n", u_errorName(status));
   1169         return;
   1170     }
   1171 
   1172     offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
   1173     offset += ucal_get(cal, UCAL_DST_OFFSET, &status);
   1174 
   1175     if (U_FAILURE(status)) {
   1176         log_err("ucal_get() failed: %s\n", u_errorName(status));
   1177         return;
   1178     }
   1179     temp=(double)((double)offset / 1000.0 / 60.0 / 60.0);
   1180     /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
   1181 
   1182     utc = ((ucal_get(cal, UCAL_HOUR_OF_DAY, &status) * 60 +
   1183                     ucal_get(cal, UCAL_MINUTE, &status)) * 60 +
   1184                    ucal_get(cal, UCAL_SECOND, &status)) * 1000 +
   1185                     ucal_get(cal, UCAL_MILLISECOND, &status) - offset;
   1186     if (U_FAILURE(status)) {
   1187         log_err("ucal_get() failed: %s\n", u_errorName(status));
   1188         return;
   1189     }
   1190 
   1191     expected = ((hr * 60 + mn) * 60 + sc) * 1000;
   1192     if (utc != expected) {
   1193         temp=(double)(utc - expected)/ 1000 / 60 / 60.0;
   1194         log_err("FAIL: Discrepancy of %d  millis = %fhr\n", utc-expected, temp );
   1195     }
   1196     else
   1197         log_verbose("PASS: the offset between local and GMT is correct\n");
   1198     ucal_close(gmtcal);
   1199     ucal_close(cal);
   1200     udat_close(datfor);
   1201 }
   1202 
   1203 /* ------------------------------------- */
   1204 
   1205 
   1206 
   1207 
   1208 /* INTERNAL FUNCTIONS USED */
   1209 /*------------------------------------------------------------------------------------------- */
   1210 
   1211 /* ------------------------------------- */
   1212 static void checkDateTime(UCalendar* c,
   1213                         int32_t y, int32_t m, int32_t d,
   1214                         int32_t hr, int32_t min, int32_t sec,
   1215                         int32_t ms, UCalendarDateFields field)
   1216 
   1217 {
   1218     UErrorCode status = U_ZERO_ERROR;
   1219     if (ucal_get(c, UCAL_YEAR, &status) != y ||
   1220         ucal_get(c, UCAL_MONTH, &status) != m ||
   1221         ucal_get(c, UCAL_DATE, &status) != d ||
   1222         ucal_get(c, UCAL_HOUR, &status) != hr ||
   1223         ucal_get(c, UCAL_MINUTE, &status) != min ||
   1224         ucal_get(c, UCAL_SECOND, &status) != sec ||
   1225         ucal_get(c, UCAL_MILLISECOND, &status) != ms) {
   1226         log_err("U_FAILURE for field  %d, Expected y/m/d h:m:s:ms of %d/%d/%d %d:%d:%d:%d  got %d/%d/%d %d:%d:%d:%d\n",
   1227             (int32_t)field, y, m + 1, d, hr, min, sec, ms,
   1228                     ucal_get(c, UCAL_YEAR, &status),
   1229                     ucal_get(c, UCAL_MONTH, &status) + 1,
   1230                     ucal_get(c, UCAL_DATE, &status),
   1231                     ucal_get(c, UCAL_HOUR, &status),
   1232                     ucal_get(c, UCAL_MINUTE, &status) + 1,
   1233                     ucal_get(c, UCAL_SECOND, &status),
   1234                     ucal_get(c, UCAL_MILLISECOND, &status) );
   1235 
   1236                     if (U_FAILURE(status)){
   1237                     log_err("ucal_get failed: %s\n", u_errorName(status));
   1238                     return;
   1239                     }
   1240 
   1241      }
   1242     else
   1243         log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y, m + 1, d, hr, min, sec, ms);
   1244 
   1245 }
   1246 
   1247 /* ------------------------------------- */
   1248 static void checkDate(UCalendar* c, int32_t y, int32_t m, int32_t d)
   1249 {
   1250     UErrorCode status = U_ZERO_ERROR;
   1251     if (ucal_get(c,UCAL_YEAR, &status) != y ||
   1252         ucal_get(c, UCAL_MONTH, &status) != m ||
   1253         ucal_get(c, UCAL_DATE, &status) != d) {
   1254 
   1255         log_err("FAILURE: Expected y/m/d of %d/%d/%d  got %d/%d/%d\n", y, m + 1, d,
   1256                     ucal_get(c, UCAL_YEAR, &status),
   1257                     ucal_get(c, UCAL_MONTH, &status) + 1,
   1258                     ucal_get(c, UCAL_DATE, &status) );
   1259 
   1260         if (U_FAILURE(status)) {
   1261             log_err("ucal_get failed: %s\n", u_errorName(status));
   1262             return;
   1263         }
   1264     }
   1265     else
   1266         log_verbose("Confirmed: %d/%d/%d\n", y, m + 1, d);
   1267 
   1268 
   1269 }
   1270 
   1271 /* ------------------------------------- */
   1272 
   1273 /* ------------------------------------- */
   1274 
   1275 static void verify1(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day)
   1276 {
   1277     UDate d1;
   1278     UErrorCode status = U_ZERO_ERROR;
   1279     if (ucal_get(c, UCAL_YEAR, &status) == year &&
   1280         ucal_get(c, UCAL_MONTH, &status) == month &&
   1281         ucal_get(c, UCAL_DATE, &status) == day) {
   1282         if (U_FAILURE(status)) {
   1283             log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
   1284             return;
   1285         }
   1286         log_verbose("PASS: %s\n", msg);
   1287         d1=ucal_getMillis(c, &status);
   1288         if (U_FAILURE(status)) {
   1289             log_err("ucal_getMillis failed: %s\n", u_errorName(status));
   1290             return;
   1291         }
   1292     /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
   1293     }
   1294     else {
   1295         log_err("FAIL: %s\n", msg);
   1296         d1=ucal_getMillis(c, &status);
   1297         if (U_FAILURE(status)) {
   1298             log_err("ucal_getMillis failed: %s\n", u_errorName(status) );
   1299             return;
   1300         }
   1301         log_err("Got %s  Expected %d/%d/%d \n", austrdup(myDateFormat(dat, d1)), year, month + 1, day );
   1302         return;
   1303     }
   1304 
   1305 
   1306 }
   1307 
   1308 /* ------------------------------------ */
   1309 static void verify2(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day,
   1310                                                                      int32_t hour, int32_t min, int32_t sec, int32_t am_pm)
   1311 {
   1312     UDate d1;
   1313     UErrorCode status = U_ZERO_ERROR;
   1314     char tempMsgBuf[256];
   1315 
   1316     if (ucal_get(c, UCAL_YEAR, &status) == year &&
   1317         ucal_get(c, UCAL_MONTH, &status) == month &&
   1318         ucal_get(c, UCAL_DATE, &status) == day &&
   1319         ucal_get(c, UCAL_HOUR, &status) == hour &&
   1320         ucal_get(c, UCAL_MINUTE, &status) == min &&
   1321         ucal_get(c, UCAL_SECOND, &status) == sec &&
   1322         ucal_get(c, UCAL_AM_PM, &status) == am_pm ){
   1323         if (U_FAILURE(status)) {
   1324             log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
   1325             return;
   1326         }
   1327         log_verbose("PASS: %s\n", msg);
   1328         d1=ucal_getMillis(c, &status);
   1329         if (U_FAILURE(status)) {
   1330             log_err("ucal_getMillis failed: %s\n", u_errorName(status));
   1331             return;
   1332         }
   1333         log_verbose("%s\n" , u_austrcpy(tempMsgBuf, myDateFormat(dat, d1)) );
   1334     }
   1335     else {
   1336         log_err("FAIL: %s\n", msg);
   1337         d1=ucal_getMillis(c, &status);
   1338         if (U_FAILURE(status)) {
   1339             log_err("ucal_getMillis failed: %s\n", u_errorName(status));
   1340             return;
   1341         }
   1342         log_err("Got %s Expected %d/%d/%d/ %d:%d:%d  %s\n", austrdup(myDateFormat(dat, d1)),
   1343             year, month + 1, day, hour, min, sec, (am_pm==0) ? "AM": "PM");
   1344 
   1345         return;
   1346     }
   1347 
   1348 
   1349 }
   1350 
   1351 void TestGregorianChange() {
   1352     static const UChar utc[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
   1353     const int32_t dayMillis = 86400 * INT64_C(1000);    /* 1 day = 86400 seconds */
   1354     UCalendar *cal;
   1355     UDate date;
   1356     UErrorCode errorCode = U_ZERO_ERROR;
   1357 
   1358     /* Test ucal_setGregorianChange() on a Gregorian calendar. */
   1359     errorCode = U_ZERO_ERROR;
   1360     cal = ucal_open(utc, -1, "", UCAL_GREGORIAN, &errorCode);
   1361     if(U_FAILURE(errorCode)) {
   1362         log_data_err("ucal_open(UTC) failed: %s - (Are you missing data?)\n", u_errorName(errorCode));
   1363         return;
   1364     }
   1365     ucal_setGregorianChange(cal, -365 * (dayMillis * (UDate)1), &errorCode);
   1366     if(U_FAILURE(errorCode)) {
   1367         log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode));
   1368     } else {
   1369         date = ucal_getGregorianChange(cal, &errorCode);
   1370         if(U_FAILURE(errorCode) || date != -365 * (dayMillis * (UDate)1)) {
   1371             log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode), date);
   1372         }
   1373     }
   1374     ucal_close(cal);
   1375 
   1376     /* Test ucal_setGregorianChange() on a non-Gregorian calendar where it should fail. */
   1377     errorCode = U_ZERO_ERROR;
   1378     cal = ucal_open(utc, -1, "th@calendar=buddhist", UCAL_TRADITIONAL, &errorCode);
   1379     if(U_FAILURE(errorCode)) {
   1380         log_err("ucal_open(UTC, non-Gregorian) failed: %s\n", u_errorName(errorCode));
   1381         return;
   1382     }
   1383     ucal_setGregorianChange(cal, -730 * (dayMillis * (UDate)1), &errorCode);
   1384     if(errorCode != U_UNSUPPORTED_ERROR) {
   1385         log_err("ucal_setGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
   1386                 u_errorName(errorCode));
   1387     }
   1388     errorCode = U_ZERO_ERROR;
   1389     date = ucal_getGregorianChange(cal, &errorCode);
   1390     if(errorCode != U_UNSUPPORTED_ERROR) {
   1391         log_err("ucal_getGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
   1392                 u_errorName(errorCode));
   1393     }
   1394     ucal_close(cal);
   1395 }
   1396 
   1397 static void TestGetKeywordValuesForLocale() {
   1398 #define PREFERRED_SIZE 15
   1399 #define MAX_NUMBER_OF_KEYWORDS 4
   1400     const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS+1] = {
   1401             { "root",        "gregorian", NULL, NULL, NULL },
   1402             { "und",         "gregorian", NULL, NULL, NULL },
   1403             { "en_US",       "gregorian", NULL, NULL, NULL },
   1404             { "en_029",      "gregorian", NULL, NULL, NULL },
   1405             { "th_TH",       "buddhist", "gregorian", NULL, NULL },
   1406             { "und_TH",      "buddhist", "gregorian", NULL, NULL },
   1407             { "en_TH",       "buddhist", "gregorian", NULL, NULL },
   1408             { "he_IL",       "gregorian", "hebrew", "islamic", "islamic-civil" },
   1409             { "ar_EG",       "gregorian", "coptic", "islamic", "islamic-civil" },
   1410             { "ja",          "gregorian", "japanese", NULL, NULL },
   1411             { "ps_Guru_IN",  "gregorian", "indian", NULL, NULL },
   1412             { "th@calendar=gregorian", "buddhist", "gregorian", NULL, NULL },
   1413             { "en@calendar=islamic",   "gregorian", NULL, NULL, NULL },
   1414             { "zh_TW",       "gregorian", "roc", "chinese", NULL },
   1415             { "ar_IR",       "gregorian", "persian", "islamic", "islamic-civil" },
   1416     };
   1417     const int32_t EXPECTED_SIZE[PREFERRED_SIZE] = { 1, 1, 1, 1, 2, 2, 2, 4, 4, 2, 2, 2, 1, 3, 4 };
   1418     UErrorCode status = U_ZERO_ERROR;
   1419     int32_t i, size, j;
   1420     UEnumeration *all, *pref;
   1421     const char *loc = NULL;
   1422     UBool matchPref, matchAll;
   1423     const char *value;
   1424     int32_t valueLength;
   1425     UList *ALLList = NULL;
   1426 
   1427     UEnumeration *ALL = ucal_getKeywordValuesForLocale("calendar", uloc_getDefault(), FALSE, &status);
   1428     if (U_SUCCESS(status)) {
   1429         for (i = 0; i < PREFERRED_SIZE; i++) {
   1430             pref = NULL;
   1431             all = NULL;
   1432             loc = PREFERRED[i][0];
   1433             pref = ucal_getKeywordValuesForLocale("calendar", loc, TRUE, &status);
   1434             matchPref = FALSE;
   1435             matchAll = FALSE;
   1436 
   1437             value = NULL;
   1438             valueLength = 0;
   1439 
   1440             if (U_SUCCESS(status) && uenum_count(pref, &status) == EXPECTED_SIZE[i]) {
   1441                 matchPref = TRUE;
   1442                 for (j = 0; j < EXPECTED_SIZE[i]; j++) {
   1443                     if ((value = uenum_next(pref, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
   1444                         if (uprv_strcmp(value, PREFERRED[i][j+1]) != 0) {
   1445                             matchPref = FALSE;
   1446                             break;
   1447                         }
   1448                     } else {
   1449                         matchPref = FALSE;
   1450                         log_err("ERROR getting keyword value for locale \"%s\"\n", loc);
   1451                         break;
   1452                     }
   1453                 }
   1454             }
   1455 
   1456             if (!matchPref) {
   1457                 log_err("FAIL: Preferred values for locale \"%s\" does not match expected.\n", loc);
   1458                 break;
   1459             }
   1460             uenum_close(pref);
   1461 
   1462             all = ucal_getKeywordValuesForLocale("calendar", loc, FALSE, &status);
   1463 
   1464             size = uenum_count(all, &status);
   1465 
   1466             if (U_SUCCESS(status) && size == uenum_count(ALL, &status)) {
   1467                 matchAll = TRUE;
   1468                 ALLList = ulist_getListFromEnum(ALL);
   1469                 for (j = 0; j < size; j++) {
   1470                     if ((value = uenum_next(all, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
   1471                         if (!ulist_containsString(ALLList, value, uprv_strlen(value))) {
   1472                             log_err("Locale %s have %s not in ALL\n", loc, value);
   1473                             matchAll = FALSE;
   1474                             break;
   1475                         }
   1476                     } else {
   1477                         matchAll = FALSE;
   1478                         log_err("ERROR getting \"all\" keyword value for locale \"%s\"\n", loc);
   1479                         break;
   1480                     }
   1481                 }
   1482             }
   1483             if (!matchAll) {
   1484                 log_err("FAIL: All values for locale \"%s\" does not match expected.\n", loc);
   1485             }
   1486 
   1487             uenum_close(all);
   1488         }
   1489     } else {
   1490         log_err_status(status, "Failed to get ALL keyword values for default locale %s: %s.\n", uloc_getDefault(), u_errorName(status));
   1491     }
   1492     uenum_close(ALL);
   1493 }
   1494 
   1495 /*
   1496  * Weekend tests, ported from
   1497  * icu4j/trunk/main/tests/core/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java
   1498  * and extended a bit. Notes below from IBMCalendarTest.java ...
   1499  * This test tests for specific locale data. This is probably okay
   1500  * as far as US data is concerned, but if the Arabic/Yemen data
   1501  * changes, this test will have to be updated.
   1502  */
   1503 
   1504 typedef struct {
   1505     int32_t year;
   1506     int32_t month;
   1507     int32_t day;
   1508     int32_t hour;
   1509     int32_t millisecOffset;
   1510     UBool   isWeekend;
   1511 } TestWeekendDates;
   1512 typedef struct {
   1513     const char * locale;
   1514     const        TestWeekendDates * dates;
   1515     int32_t      numDates;
   1516 } TestWeekendDatesList;
   1517 
   1518 static const TestWeekendDates weekendDates_en_US[] = {
   1519     { 2000, UCAL_MARCH, 17, 23,  0, 0 }, /* Fri 23:00        */
   1520     { 2000, UCAL_MARCH, 18,  0, -1, 0 }, /* Fri 23:59:59.999 */
   1521     { 2000, UCAL_MARCH, 18,  0,  0, 1 }, /* Sat 00:00        */
   1522     { 2000, UCAL_MARCH, 18, 15,  0, 1 }, /* Sat 15:00        */
   1523     { 2000, UCAL_MARCH, 19, 23,  0, 1 }, /* Sun 23:00        */
   1524     { 2000, UCAL_MARCH, 20,  0, -1, 1 }, /* Sun 23:59:59.999 */
   1525     { 2000, UCAL_MARCH, 20,  0,  0, 0 }, /* Mon 00:00        */
   1526     { 2000, UCAL_MARCH, 20,  8,  0, 0 }, /* Mon 08:00        */
   1527 };
   1528 static const TestWeekendDates weekendDates_ar_YE[] = {
   1529     { 2000, UCAL_MARCH, 15, 23,  0, 0 }, /* Wed 23:00        */
   1530     { 2000, UCAL_MARCH, 16,  0, -1, 0 }, /* Wed 23:59:59.999 */
   1531     { 2000, UCAL_MARCH, 16,  0,  0, 1 }, /* Thu 00:00        */
   1532     { 2000, UCAL_MARCH, 16, 15,  0, 1 }, /* Thu 15:00        */
   1533     { 2000, UCAL_MARCH, 17, 23,  0, 1 }, /* Fri 23:00        */
   1534     { 2000, UCAL_MARCH, 18,  0, -1, 1 }, /* Fri 23:59:59.999 */
   1535     { 2000, UCAL_MARCH, 18,  0,  0, 0 }, /* Sat 00:00        */
   1536     { 2000, UCAL_MARCH, 18,  8,  0, 0 }, /* Sat 08:00        */
   1537 };
   1538 static const TestWeekendDatesList testDates[] = {
   1539     { "en_US", weekendDates_en_US, sizeof(weekendDates_en_US)/sizeof(weekendDates_en_US[0]) },
   1540     { "ar_YE", weekendDates_ar_YE, sizeof(weekendDates_ar_YE)/sizeof(weekendDates_ar_YE[0]) },
   1541 };
   1542 
   1543 typedef struct {
   1544     UCalendarDaysOfWeek  dayOfWeek;
   1545     UCalendarWeekdayType dayType;
   1546     int32_t              transition; /* transition time if dayType is UCAL_WEEKEND_ONSET or UCAL_WEEKEND_CEASE; else must be 0 */
   1547 } TestDaysOfWeek;
   1548 typedef struct {
   1549     const char *           locale;
   1550     const TestDaysOfWeek * days;
   1551     int32_t                numDays;
   1552 } TestDaysOfWeekList;
   1553 
   1554 static const TestDaysOfWeek daysOfWeek_en_US[] = {
   1555     { UCAL_MONDAY,   UCAL_WEEKDAY,       0        },
   1556     { UCAL_FRIDAY,   UCAL_WEEKDAY,       0        },
   1557     { UCAL_SATURDAY, UCAL_WEEKEND,       0        },
   1558     { UCAL_SUNDAY,   UCAL_WEEKEND_CEASE, 86400000 },
   1559 };
   1560 static const TestDaysOfWeek daysOfWeek_ar_YE[] = { /* Thursday:Friday */
   1561     { UCAL_WEDNESDAY,UCAL_WEEKDAY,       0        },
   1562     { UCAL_SATURDAY, UCAL_WEEKDAY,       0        },
   1563     { UCAL_THURSDAY, UCAL_WEEKEND,       0        },
   1564     { UCAL_FRIDAY,   UCAL_WEEKEND_CEASE, 86400000 },
   1565 };
   1566 static const TestDaysOfWeekList testDays[] = {
   1567     { "en_US", daysOfWeek_en_US, sizeof(daysOfWeek_en_US)/sizeof(daysOfWeek_en_US[0]) },
   1568     { "ar_YE", daysOfWeek_ar_YE, sizeof(daysOfWeek_ar_YE)/sizeof(daysOfWeek_ar_YE[0]) },
   1569 };
   1570 
   1571 static const UChar logDateFormat[] = { 0x0045,0x0045,0x0045,0x0020,0x004D,0x004D,0x004D,0x0020,0x0064,0x0064,0x0020,0x0079,
   1572                                        0x0079,0x0079,0x0079,0x0020,0x0047,0x0020,0x0048,0x0048,0x003A,0x006D,0x006D,0x003A,
   1573                                        0x0073,0x0073,0x002E,0x0053,0x0053,0x0053,0 }; /* "EEE MMM dd yyyy G HH:mm:ss.SSS" */
   1574 enum { kFormattedDateMax = 2*sizeof(logDateFormat)/sizeof(logDateFormat[0]) };
   1575 
   1576 static void TestWeekend() {
   1577     const TestWeekendDatesList * testDatesPtr = testDates;
   1578     const TestDaysOfWeekList *   testDaysPtr = testDays;
   1579     int32_t count, subCount;
   1580 
   1581     UErrorCode fmtStatus = U_ZERO_ERROR;
   1582     UDateFormat * fmt = udat_open(UDAT_NONE, UDAT_NONE, "en", NULL, 0, NULL, 0, &fmtStatus);
   1583     if (U_SUCCESS(fmtStatus)) {
   1584         udat_applyPattern(fmt, FALSE, logDateFormat, -1);
   1585     }
   1586 	for (count = sizeof(testDates)/sizeof(testDates[0]); count-- > 0; ++testDatesPtr) {
   1587         UErrorCode status = U_ZERO_ERROR;
   1588 		UCalendar * cal = ucal_open(NULL, 0, testDatesPtr->locale, UCAL_GREGORIAN, &status);
   1589 		log_verbose("locale: %s\n", testDatesPtr->locale);
   1590 		if (U_SUCCESS(status)) {
   1591 			const TestWeekendDates * weekendDatesPtr = testDatesPtr->dates;
   1592 			for (subCount = testDatesPtr->numDates; subCount--; ++weekendDatesPtr) {
   1593 				UDate dateToTest;
   1594 				UBool isWeekend;
   1595 				char  fmtDateBytes[kFormattedDateMax] = "<could not format test date>"; /* initialize for failure */
   1596 
   1597 				ucal_clear(cal);
   1598 				ucal_setDateTime(cal, weekendDatesPtr->year, weekendDatesPtr->month, weekendDatesPtr->day,
   1599 								 weekendDatesPtr->hour, 0, 0, &status);
   1600 				dateToTest = ucal_getMillis(cal, &status) + weekendDatesPtr->millisecOffset;
   1601 				isWeekend = ucal_isWeekend(cal, dateToTest, &status);
   1602 				if (U_SUCCESS(fmtStatus)) {
   1603 				    UChar fmtDate[kFormattedDateMax];
   1604 				    (void)udat_format(fmt, dateToTest, fmtDate, kFormattedDateMax, NULL, &fmtStatus);
   1605 				    if (U_SUCCESS(fmtStatus)) {
   1606 						u_austrncpy(fmtDateBytes, fmtDate, kFormattedDateMax);
   1607 						fmtDateBytes[kFormattedDateMax-1] = 0;
   1608 				    } else {
   1609 				    	fmtStatus = U_ZERO_ERROR;
   1610 				    }
   1611 				}
   1612 				if ( U_FAILURE(status) ) {
   1613 					log_err("FAIL: locale %s date %s isWeekend() status %s\n", testDatesPtr->locale, fmtDateBytes, u_errorName(status) );
   1614 					status = U_ZERO_ERROR;
   1615 				} else if ( (isWeekend!=0) != (weekendDatesPtr->isWeekend!=0) ) {
   1616 					log_err("FAIL: locale %s date %s isWeekend %d, expected the opposite\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
   1617 				} else {
   1618 					log_verbose("OK:   locale %s date %s isWeekend %d\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
   1619 				}
   1620 			}
   1621 			ucal_close(cal);
   1622 		} else {
   1623 			log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDatesPtr->locale, u_errorName(status) );
   1624 		}
   1625 	}
   1626     if (U_SUCCESS(fmtStatus)) {
   1627         udat_close(fmt);
   1628     }
   1629 
   1630     for (count = sizeof(testDays)/sizeof(testDays[0]); count-- > 0; ++testDaysPtr) {
   1631         UErrorCode status = U_ZERO_ERROR;
   1632         UCalendar * cal = ucal_open(NULL, 0, testDaysPtr->locale, UCAL_GREGORIAN, &status);
   1633         log_verbose("locale: %s\n", testDaysPtr->locale);
   1634         if (U_SUCCESS(status)) {
   1635             const TestDaysOfWeek * daysOfWeekPtr = testDaysPtr->days;
   1636             for (subCount = testDaysPtr->numDays; subCount--; ++daysOfWeekPtr) {
   1637                 int32_t transition = 0;
   1638                 UCalendarWeekdayType dayType = ucal_getDayOfWeekType(cal, daysOfWeekPtr->dayOfWeek, &status);
   1639                 if ( dayType == UCAL_WEEKEND_ONSET || dayType == UCAL_WEEKEND_CEASE ) {
   1640                     transition = ucal_getWeekendTransition(cal, daysOfWeekPtr->dayOfWeek, &status);
   1641                 }
   1642                 if ( U_FAILURE(status) ) {
   1643 					log_err("FAIL: locale %s DOW %d getDayOfWeekType() status %s\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, u_errorName(status) );
   1644 					status = U_ZERO_ERROR;
   1645                 } else if ( dayType != daysOfWeekPtr->dayType || transition != daysOfWeekPtr->transition ) {
   1646 					log_err("FAIL: locale %s DOW %d type %d, expected %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType, daysOfWeekPtr->dayType );
   1647                 } else {
   1648 					log_verbose("OK:   locale %s DOW %d type %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType );
   1649                 }
   1650             }
   1651             ucal_close(cal);
   1652         } else {
   1653             log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDaysPtr->locale, u_errorName(status) );
   1654         }
   1655     }
   1656 }
   1657 
   1658 #endif /* #if !UCONFIG_NO_FORMATTING */
   1659