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