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