/******************************************************************** * COPYRIGHT: * Copyright (c) 1997-2001, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ /******************************************************************************** * * File CCALTST.C * * Modification History: * Name Description * Madhu Katragadda Creation *********************************************************************************/ /* C API AND FUNCTIONALITY TEST FOR CALENDAR (ucol.h)*/ #include "unicode/uloc.h" #include "unicode/utypes.h" #include "unicode/ucal.h" #include "unicode/udat.h" #include "unicode/ustring.h" #include "cintltst.h" #include "ccaltst.h" #include "cformtst.h" #include "cmemory.h" void addCalTest(TestNode** root); void addCalTest(TestNode** root) { addTest(root, &TestCalendar, "tsformat/ccaltst/TestCalendar"); addTest(root, &TestGetSetDateAPI, "tsformat/ccaltst/TestGetSetDateAPI"); addTest(root, &TestFieldGetSet, "tsformat/ccaltst/TestFieldGetSet"); addTest(root, &TestAddRollExtensive, "tsformat/ccaltst/TestAddRollExtensive"); addTest(root, &TestGetLimits, "tsformat/ccaltst/TestGetLimits"); addTest(root, &TestDOWProgression, "tsformat/ccaltst/TestDOWProgression"); addTest(root, &TestGMTvsLocal, "tsformat/ccaltst/TestGMTvsLocal"); } /* "GMT" */ static const UChar fgGMTID [] = { 0x0047, 0x004d, 0x0054, 0x0000 }; static void TestCalendar() { UCalendar *caldef = 0, *caldef2 = 0, *calfr = 0, *calit = 0; int32_t count, count2, offset,i; UChar *tzID = 0; UChar *tzdname = 0; UErrorCode status = U_ZERO_ERROR; UDate now; UDateFormat *datdef = 0; UChar *result = 0; int32_t resultlength, resultlengthneeded; char tempMsgBuf[256]; /*Testing countAvailableTimeZones*/ offset=0; log_verbose("\nTesting countAvialableTimeZoneIds\n"); count=ucal_countAvailableTZIDs(offset); log_verbose("The number of timezone id's present with offset 0 are %d:\n", count); if(count < 5) /* Don't hard code an exact == test here! */ log_err("FAIL: error in the countAvailableTZIDs - got %d expected at least 5 total\n", count); /*Testing getAvialableTZIDs*/ log_verbose("\nTesting getAvailableTimezoneids"); for(i=0;i= count*/ ucal_getAvailableTZIDs(offset, i, &status); if(U_SUCCESS(status)){ log_err("FAIL: Expected U_INDEX_OUTOFBOUNDS_ERROR where index > count for TZID's"); } if(status != U_INDEX_OUTOFBOUNDS_ERROR){ log_err("FAIL:for TZID index > count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", myErrorName(status)); } status=U_ZERO_ERROR; /*Testing the ucal_open() function*/ log_verbose("\nTesting the ucal_open()\n"); tzID=(UChar*)uprv_malloc(sizeof(UChar) * 4); u_uastrcpy(tzID, "PST"); caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status)); } caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_err("FAIL: error in ucal_open caldef : %s\n", myErrorName(status)); } u_strcpy(tzID, fgGMTID); calfr=ucal_open(tzID, u_strlen(tzID), "fr_FR", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)){ log_err("FAIL: error in ucal_open calfr : %s\n", myErrorName(status)); } calit=ucal_open(tzID, u_strlen(tzID), "it_IT", UCAL_TRADITIONAL, &status); if(U_FAILURE(status)) { log_err("FAIL: error in ucal_open calit : %s\n", myErrorName(status)); } /*Testing udat_getAvailable() and udat_countAvailable()*/ log_verbose("\nTesting getAvailableLocales and countAvailable()\n"); count=ucal_countAvailable(); /* use something sensible w/o hardcoding the count */ if(count > 0){ log_verbose("PASS: ucal_countAvialable() works fine\n"); log_verbose("The no: of locales for which calendars are avilable are %d\n", count); } else log_err("FAIL: Error in countAvialable()\n"); for(i=0;i val > max) && (val != UCAL_FRIDAY)){ log_err("FAIL: Min/max bad\n"); log_err("FAIL: Day of week %d out of range\n", val); log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val); } else log_verbose("getLimits successful\n"); val = ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status); min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MINIMUM, &status); max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MAXIMUM, &status); if ( (min != 0 || max != 5 ) && (min > val > max) && (val != 1)){ log_err("FAIL: Min/max bad\n"); log_err("FAIL: Day of week in month %d out of range\n", val); log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val); } else log_verbose("getLimits successful\n"); min=ucal_getLimit(cal, UCAL_MONTH, UCAL_MINIMUM, &status); max=ucal_getLimit(cal, UCAL_MONTH, UCAL_MAXIMUM, &status); gr_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_GREATEST_MINIMUM, &status); le_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_LEAST_MAXIMUM, &status); ac_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status); ac_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MAXIMUM, &status); if(U_FAILURE(status)){ log_err("Error in getLimits: %s\n", myErrorName(status)); } if(min!=0 || max!=11 || gr_min!=0 || le_max!=11 || ac_min!=0 || ac_max!=11) log_err("There is and error in getLimits in fetching the values\n"); else log_verbose("getLimits successful\n"); ucal_setDateTime(cal, 1999, UCAL_MARCH, 5, 4, 10, 35, &status); val=ucal_get(cal, UCAL_HOUR_OF_DAY, &status); min=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MINIMUM, &status); max=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MAXIMUM, &status); gr_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_GREATEST_MINIMUM, &status); le_max=ucal_getLimit(cal, UCAL_MINUTE, UCAL_LEAST_MAXIMUM, &status); ac_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status); ac_max=ucal_getLimit(cal, UCAL_SECOND, UCAL_ACTUAL_MAXIMUM, &status); if( (min!=0 || max!= 11 || gr_min!=0 || le_max!=60 || ac_min!=0 || ac_max!=60) && (min>val>max) && val!=4){ log_err("FAIL: Min/max bad\n"); log_err("FAIL: Hour of Day %d out of range\n", val); log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val); } else log_verbose("getLimits successful\n"); /*get BOGUS_LIMIT type*/ val=ucal_getLimit(cal, UCAL_SECOND, (UCalendarLimitType)99, &status); if(val != -1){ log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n"); } status=U_ZERO_ERROR; ucal_close(cal); uprv_free(tzID); } /* ------------------------------------- */ /** * Test that the days of the week progress properly when add is called repeatedly * for increments of 24 days. */ static void TestDOWProgression() { int32_t initialDOW, DOW, newDOW, expectedDOW; UCalendar *cal = 0; UDateFormat *datfor = 0; UDate date1; int32_t delta=24; UErrorCode status = U_ZERO_ERROR; UChar* tzID = 0; char tempMsgBuf[256]; tzID=(UChar*)uprv_malloc(sizeof(UChar) * 4); u_strcpy(tzID, fgGMTID); /*open the calendar used */ cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);; if (U_FAILURE(status)) { log_err("ucal_open failed: %s\n", myErrorName(status)); return; } datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status); if(U_FAILURE(status)){ log_err("error in creating the dateformat : %s\n", myErrorName(status)); } ucal_setDate(cal, 1999, UCAL_JANUARY, 1, &status); log_verbose("\nTesting the DOW progression\n"); initialDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status); if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", myErrorName(status) ); return; } newDOW = initialDOW; do { DOW = newDOW; log_verbose("DOW = %d...\n", DOW); date1=ucal_getMillis(cal, &status); if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", myErrorName(status)); return;} log_verbose("%s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1))); ucal_add(cal,UCAL_DAY_OF_WEEK, delta, &status); if (U_FAILURE(status)) { log_err("ucal_add() failed: %s\n", myErrorName(status)); return; } newDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status); if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", myErrorName(status)); return; } expectedDOW = 1 + (DOW + delta - 1) % 7; date1=ucal_getMillis(cal, &status); if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", myErrorName(status)); return;} if (newDOW != expectedDOW) { log_err("Day of week should be %d instead of %d on %s", expectedDOW, newDOW, u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) ); return; } } while (newDOW != initialDOW); ucal_close(cal); udat_close(datfor); uprv_free(tzID); } /* ------------------------------------- */ /** * Confirm that the offset between local time and GMT behaves as expected. */ static void TestGMTvsLocal() { log_verbose("\nTesting the offset between the GMT and local time\n"); testZones(1999, 1, 1, 12, 0, 0); testZones(1999, 4, 16, 18, 30, 0); testZones(1998, 12, 17, 19, 0, 0); } /* ------------------------------------- */ static void testZones(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc) { int32_t offset,utc, expected; UCalendar *gmtcal = 0, *cal = 0; UDate date1; double temp; UDateFormat *datfor = 0; UErrorCode status = U_ZERO_ERROR; UChar* tzID = 0; char tempMsgBuf[256]; tzID=(UChar*)uprv_malloc(sizeof(UChar) * 4); u_strcpy(tzID, fgGMTID); gmtcal=ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);; if (U_FAILURE(status)) { log_err("ucal_open failed: %s\n", myErrorName(status)); return; } u_uastrcpy(tzID, "PST"); cal = ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status); if (U_FAILURE(status)) { log_err("ucal_open failed: %s\n", myErrorName(status)); return; } datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status); if(U_FAILURE(status)){ log_err("error in creating the dateformat : %s\n", myErrorName(status)); } ucal_setDateTime(gmtcal, yr, mo - 1, dt, hr, mn, sc, &status); if (U_FAILURE(status)) { log_err("ucal_setDateTime failed: %s\n", myErrorName(status)); return; } ucal_set(gmtcal, UCAL_MILLISECOND, 0); date1 = ucal_getMillis(gmtcal, &status); if (U_FAILURE(status)) { log_err("ucal_getMillis failed: %s\n", myErrorName(status)); return; } log_verbose("date = %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) ); ucal_setMillis(cal, date1, &status); if (U_FAILURE(status)) { log_err("ucal_setMillis() failed: %s\n", myErrorName(status)); return; } offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status); offset += ucal_get(cal, UCAL_DST_OFFSET, &status); if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", myErrorName(status)); return; } temp=(double)((double)offset / 1000.0 / 60.0 / 60.0); /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/ utc = ((ucal_get(cal, UCAL_HOUR_OF_DAY, &status) * 60 + ucal_get(cal, UCAL_MINUTE, &status)) * 60 + ucal_get(cal, UCAL_SECOND, &status)) * 1000 + ucal_get(cal, UCAL_MILLISECOND, &status) - offset; if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", myErrorName(status)); return; } expected = ((hr * 60 + mn) * 60 + sc) * 1000; if (utc != expected) { temp=(double)(utc - expected)/ 1000 / 60 / 60.0; log_err("FAIL: Discrepancy of %d millis = %fhr\n", utc-expected, temp ); } else log_verbose("PASS: the offset between local and GMT is correct\n"); ucal_close(gmtcal); ucal_close(cal); udat_close(datfor); uprv_free(tzID); } /* ------------------------------------- */ /* INTERNAL FUNCTIONS USED */ /*------------------------------------------------------------------------------------------- */ /* ------------------------------------- */ static void checkDateTime(UCalendar* c, int32_t y, int32_t m, int32_t d, int32_t hr, int32_t min, int32_t sec, int32_t ms, UCalendarDateFields field) { UErrorCode status = U_ZERO_ERROR; if (ucal_get(c, UCAL_YEAR, &status) != y || ucal_get(c, UCAL_MONTH, &status) != m || ucal_get(c, UCAL_DATE, &status) != d || ucal_get(c, UCAL_HOUR, &status) != hr || ucal_get(c, UCAL_MINUTE, &status) != min || ucal_get(c, UCAL_SECOND, &status) != sec || ucal_get(c, UCAL_MILLISECOND, &status) != ms) { 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", (int32_t)field, y, m + 1, d, hr, min, sec, ms, ucal_get(c, UCAL_YEAR, &status), ucal_get(c, UCAL_MONTH, &status) + 1, ucal_get(c, UCAL_DATE, &status), ucal_get(c, UCAL_HOUR, &status), ucal_get(c, UCAL_MINUTE, &status) + 1, ucal_get(c, UCAL_SECOND, &status), ucal_get(c, UCAL_MILLISECOND, &status) ); if (U_FAILURE(status)){ log_err("ucal_get failed: %s\n", myErrorName(status)); return; } } else log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y, m + 1, d, hr, min, sec, ms); } /* ------------------------------------- */ static void checkDate(UCalendar* c, int32_t y, int32_t m, int32_t d) { UErrorCode status = U_ZERO_ERROR; if (ucal_get(c,UCAL_YEAR, &status) != y || ucal_get(c, UCAL_MONTH, &status) != m || ucal_get(c, UCAL_DATE, &status) != d) { log_err("FAILURE: Expected y/m/d of %d/%d/%d got %d/%d/%d\n", y, m + 1, d, ucal_get(c, UCAL_YEAR, &status), ucal_get(c, UCAL_MONTH, &status) + 1, ucal_get(c, UCAL_DATE, &status) ); if (U_FAILURE(status)) { log_err("ucal_get failed: %s\n", myErrorName(status)); return; } } else log_verbose("Confirmed: %d/%d/%d\n", y, m + 1, d); } /* ------------------------------------- */ /* ------------------------------------- */ static void verify1(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day) { UDate d1; UErrorCode status = U_ZERO_ERROR; if (ucal_get(c, UCAL_YEAR, &status) == year && ucal_get(c, UCAL_MONTH, &status) == month && ucal_get(c, UCAL_DATE, &status) == day) { if (U_FAILURE(status)) { log_err("FAIL: Calendar::get failed: %s\n", myErrorName(status)); return; } log_verbose("PASS: %s\n", msg); d1=ucal_getMillis(c, &status); if (U_FAILURE(status)) { log_err("ucal_getMillis failed: %s\n", myErrorName(status)); return; } /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/ } else { log_err("FAIL: %s\n", msg); d1=ucal_getMillis(c, &status); if (U_FAILURE(status)) { log_err("ucal_getMillis failed: %s\n", myErrorName(status) ); return; } log_err("Got %s Expected %d/%d/%d \n", austrdup(myDateFormat(dat, d1)), year, month + 1, day ); return; } } /* ------------------------------------ */ static void verify2(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day, int32_t hour, int32_t min, int32_t sec, int32_t am_pm) { UDate d1; UErrorCode status = U_ZERO_ERROR; char tempMsgBuf[256]; if (ucal_get(c, UCAL_YEAR, &status) == year && ucal_get(c, UCAL_MONTH, &status) == month && ucal_get(c, UCAL_DATE, &status) == day && ucal_get(c, UCAL_HOUR, &status) == hour && ucal_get(c, UCAL_MINUTE, &status) == min && ucal_get(c, UCAL_SECOND, &status) == sec && ucal_get(c, UCAL_AM_PM, &status) == am_pm ){ if (U_FAILURE(status)) { log_err("FAIL: Calendar::get failed: %s\n", myErrorName(status)); return; } log_verbose("PASS: %s\n", msg); d1=ucal_getMillis(c, &status); if (U_FAILURE(status)) { log_err("ucal_getMillis failed: %s\n", myErrorName(status)); return; } log_verbose("%s\n" , u_austrcpy(tempMsgBuf, myDateFormat(dat, d1)) ); } else { log_err("FAIL: %s\n", msg); d1=ucal_getMillis(c, &status); if (U_FAILURE(status)) { log_err("ucal_getMillis failed: %s\n", myErrorName(status)); return; } log_err("Got %s Expected %d/%d/%d/ %d:%d:%d %s\n", austrdup(myDateFormat(dat, d1)), year, month + 1, day, hour, min, sec, (am_pm==0) ? "AM": "PM"); return; } } /*--------------------------------------------- */