• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /********************************************************************
2  * Copyright (c) 1997-2014, 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 void TestAddRollEra0AndEraBounds(void);
36 void TestGetTZTransition(void);
37 
38 void TestGetWindowsTimeZoneID(void);
39 void TestGetTimeZoneIDByWindowsID(void);
40 
41 void addCalTest(TestNode** root);
42 
addCalTest(TestNode ** root)43 void addCalTest(TestNode** root)
44 {
45 
46     addTest(root, &TestCalendar, "tsformat/ccaltst/TestCalendar");
47     addTest(root, &TestGetSetDateAPI, "tsformat/ccaltst/TestGetSetDateAPI");
48     addTest(root, &TestFieldGetSet, "tsformat/ccaltst/TestFieldGetSet");
49     addTest(root, &TestAddRollExtensive, "tsformat/ccaltst/TestAddRollExtensive");
50     addTest(root, &TestGetLimits, "tsformat/ccaltst/TestGetLimits");
51     addTest(root, &TestDOWProgression, "tsformat/ccaltst/TestDOWProgression");
52     addTest(root, &TestGMTvsLocal, "tsformat/ccaltst/TestGMTvsLocal");
53     addTest(root, &TestGregorianChange, "tsformat/ccaltst/TestGregorianChange");
54     addTest(root, &TestGetKeywordValuesForLocale, "tsformat/ccaltst/TestGetKeywordValuesForLocale");
55     addTest(root, &TestWeekend, "tsformat/ccaltst/TestWeekend");
56     addTest(root, &TestFieldDifference, "tsformat/ccaltst/TestFieldDifference");
57     addTest(root, &TestAmbiguousWallTime, "tsformat/ccaltst/TestAmbiguousWallTime");
58     addTest(root, &TestAddRollEra0AndEraBounds, "tsformat/ccaltst/TestAddRollEra0AndEraBounds");
59     addTest(root, &TestGetTZTransition, "tsformat/ccaltst/TestGetTZTransition");
60     addTest(root, &TestGetWindowsTimeZoneID, "tsformat/ccaltst/TestGetWindowsTimeZoneID");
61     addTest(root, &TestGetTimeZoneIDByWindowsID, "tsformat/ccaltst/TestGetTimeZoneIDByWindowsID");
62 }
63 
64 /* "GMT" */
65 static const UChar fgGMTID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
66 
67 /* "PST" */
68 static const UChar PST[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
69 
70 static const UChar EUROPE_PARIS[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
71 
72 static const UChar AMERICA_LOS_ANGELES[] = {0x41, 0x6D, 0x65, 0x72, 0x69, 0x63, 0x61, 0x2F,
73     0x4C, 0x6F, 0x73, 0x5F, 0x41, 0x6E, 0x67, 0x65, 0x6C, 0x65, 0x73, 0x00}; /* America/Los_Angeles */
74 
75 typedef struct {
76     const char *    locale;
77     UCalendarType   calType;
78     const char *    expectedResult;
79 } UCalGetTypeTest;
80 
81 static const UCalGetTypeTest ucalGetTypeTests[] = {
82     { "en_US",                   UCAL_GREGORIAN, "gregorian" },
83     { "ja_JP@calendar=japanese", UCAL_DEFAULT,   "japanese"  },
84     { "th_TH",                   UCAL_GREGORIAN, "gregorian" },
85     { "th_TH",                   UCAL_DEFAULT,   "gregorian"  },  // android-changed
86     // { "th_TH",                   UCAL_DEFAULT,   "buddhist"  },
87     { "th-TH-u-ca-gregory",      UCAL_DEFAULT,   "gregorian" },
88     { "ja_JP@calendar=japanese", UCAL_GREGORIAN, "gregorian" },
89     { "",                        UCAL_GREGORIAN, "gregorian" },
90     { NULL,                      UCAL_GREGORIAN, "gregorian" },
91     { NULL, 0, NULL } /* terminator */
92 };
93 
TestCalendar()94 static void TestCalendar()
95 {
96     UCalendar *caldef = 0, *caldef2 = 0, *calfr = 0, *calit = 0, *calfrclone = 0;
97     UEnumeration* uenum = NULL;
98     int32_t count, count2, i,j;
99     UChar tzID[4];
100     UChar *tzdname = 0;
101     UErrorCode status = U_ZERO_ERROR;
102     UDate now;
103     UDateFormat *datdef = 0;
104     UChar *result = 0;
105     int32_t resultlength, resultlengthneeded;
106     char tempMsgBuf[256];
107     UChar zone1[32], zone2[32];
108     const char *tzver = 0;
109     UChar canonicalID[64];
110     UBool isSystemID = FALSE;
111     const UCalGetTypeTest * ucalGetTypeTestPtr;
112 
113 #ifdef U_USE_UCAL_OBSOLETE_2_8
114     /*Testing countAvailableTimeZones*/
115     int32_t offset=0;
116     log_verbose("\nTesting ucal_countAvailableTZIDs\n");
117     count=ucal_countAvailableTZIDs(offset);
118     log_verbose("The number of timezone id's present with offset 0 are %d:\n", count);
119     if(count < 5) /* Don't hard code an exact == test here! */
120         log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count);
121 
122     /*Testing getAvailableTZIDs*/
123     log_verbose("\nTesting ucal_getAvailableTZIDs");
124     for(i=0;i<count;i++){
125         ucal_getAvailableTZIDs(offset, i, &status);
126         if(U_FAILURE(status)){
127             log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status));
128         }
129         log_verbose("%s\n", u_austrcpy(tempMsgBuf, ucal_getAvailableTZIDs(offset, i, &status)));
130     }
131     /*get Illegal TZID where index >= count*/
132     ucal_getAvailableTZIDs(offset, i, &status);
133     if(status != U_INDEX_OUTOFBOUNDS_ERROR){
134         log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status));
135     }
136     status=U_ZERO_ERROR;
137 #endif
138 
139     /*Test ucal_openTimeZones, ucal_openCountryTimeZones and ucal_openTimeZoneIDEnumeration */
140     for (j=0; j<6; ++j) {
141         const char *api = "?";
142         const int32_t offsetMinus5 = -5*60*60*1000;
143         switch (j) {
144         case 0:
145             api = "ucal_openTimeZones()";
146             uenum = ucal_openTimeZones(&status);
147             break;
148         case 1:
149             api = "ucal_openCountryTimeZones(US)";
150             uenum = ucal_openCountryTimeZones("US", &status);
151             break;
152         case 2:
153             api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL)";
154             uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL, NULL, NULL, &status);
155             break;
156         case 3:
157             api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_CANONICAL_LOCATION, CA, NULL)";
158             uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_CANONICAL_LOCATION, "CA", NULL, &status);
159             break;
160         case 4:
161             api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_ANY, NULL, -5 hour)";
162             uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, NULL, &offsetMinus5, &status);
163             break;
164         case 5:
165             api = "ucal_openTimeZoneIDEnumerarion(UCAL_ZONE_TYPE_ANY, US, -5 hour)";
166             uenum = ucal_openTimeZoneIDEnumeration(UCAL_ZONE_TYPE_ANY, "US", &offsetMinus5, &status);
167             break;
168         }
169         if (U_FAILURE(status)) {
170             log_err_status(status, "FAIL: %s failed with %s\n", api,
171                     u_errorName(status));
172         } else {
173             const char* id;
174             int32_t len;
175             count = uenum_count(uenum, &status);
176             log_verbose("%s returned %d timezone id's:\n", api, count);
177             if (count < 5) { /* Don't hard code an exact == test here! */
178                 log_data_err("FAIL: in %s, got %d, expected at least 5 -> %s (Are you missing data?)\n", api, count, u_errorName(status));
179             }
180             uenum_reset(uenum, &status);
181             if (U_FAILURE(status)){
182                 log_err("FAIL: uenum_reset for %s returned %s\n",
183                         api, u_errorName(status));
184             }
185             for (i=0; i<count; i++) {
186                 id = uenum_next(uenum, &len, &status);
187                 if (U_FAILURE(status)){
188                     log_err("FAIL: uenum_next for %s returned %s\n",
189                             api, u_errorName(status));
190                 } else {
191                     log_verbose("%s\n", id);
192                 }
193             }
194             /* Next one should be NULL */
195             id = uenum_next(uenum, &len, &status);
196             if (id != NULL) {
197                 log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
198                         api, id);
199             }
200         }
201         uenum_close(uenum);
202     }
203 
204     /*Test ucal_getDSTSavings*/
205     status = U_ZERO_ERROR;
206     i = ucal_getDSTSavings(fgGMTID, &status);
207     if (U_FAILURE(status)) {
208         log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
209                 u_errorName(status));
210     } else if (i != 0) {
211         log_data_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0 (Are you missing data?)\n", i);
212     }
213     i = ucal_getDSTSavings(PST, &status);
214     if (U_FAILURE(status)) {
215         log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
216                 u_errorName(status));
217     } else if (i != 1*60*60*1000) {
218         log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i, 1*60*60*1000);
219     }
220 
221     /*Test ucal_set/getDefaultTimeZone*/
222     status = U_ZERO_ERROR;
223     i = ucal_getDefaultTimeZone(zone1, sizeof(zone1)/sizeof(zone1[0]), &status);
224     if (U_FAILURE(status)) {
225         log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
226                 u_errorName(status));
227     } else {
228         ucal_setDefaultTimeZone(EUROPE_PARIS, &status);
229         if (U_FAILURE(status)) {
230             log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
231                     u_errorName(status));
232         } else {
233             i = ucal_getDefaultTimeZone(zone2, sizeof(zone2)/sizeof(zone2[0]), &status);
234             if (U_FAILURE(status)) {
235                 log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
236                         u_errorName(status));
237             } else {
238                 if (u_strcmp(zone2, EUROPE_PARIS) != 0) {
239                     log_data_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris (Are you missing data?)\n");
240                 }
241             }
242         }
243         status = U_ZERO_ERROR;
244         ucal_setDefaultTimeZone(zone1, &status);
245     }
246 
247     /*Test ucal_getTZDataVersion*/
248     status = U_ZERO_ERROR;
249     tzver = ucal_getTZDataVersion(&status);
250     if (U_FAILURE(status)) {
251         log_err_status(status, "FAIL: ucal_getTZDataVersion() => %s\n", u_errorName(status));
252     } else if (uprv_strlen(tzver) != 5 /*4 digits + 1 letter*/) {
253         log_err("FAIL: Bad version string was returned by ucal_getTZDataVersion\n");
254     } else {
255         log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver);
256     }
257 
258     /*Testing ucal_getCanonicalTimeZoneID*/
259     status = U_ZERO_ERROR;
260     resultlength = ucal_getCanonicalTimeZoneID(PST, -1,
261         canonicalID, sizeof(canonicalID)/sizeof(UChar), &isSystemID, &status);
262     if (U_FAILURE(status)) {
263         log_data_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status));
264     } else {
265         if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) {
266             log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s (Are you missing data?)\n",
267                 PST, canonicalID, AMERICA_LOS_ANGELES);
268         }
269         if (!isSystemID) {
270             log_data_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID (Are you missing data?)\n",
271                 PST, isSystemID);
272         }
273     }
274 
275     /*Testing the  ucal_open() function*/
276     status = U_ZERO_ERROR;
277     log_verbose("\nTesting the ucal_open()\n");
278     u_uastrcpy(tzID, "PST");
279     caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
280     if(U_FAILURE(status)){
281         log_data_err("FAIL: error in ucal_open caldef : %s\n - (Are you missing data?)", u_errorName(status));
282     }
283 
284     caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
285     if(U_FAILURE(status)){
286         log_data_err("FAIL: error in ucal_open caldef : %s - (Are you missing data?)\n", u_errorName(status));
287     }
288     u_strcpy(tzID, fgGMTID);
289     calfr=ucal_open(tzID, u_strlen(tzID), "fr_FR", UCAL_TRADITIONAL, &status);
290     if(U_FAILURE(status)){
291         log_data_err("FAIL: error in ucal_open calfr : %s - (Are you missing data?)\n", u_errorName(status));
292     }
293     calit=ucal_open(tzID, u_strlen(tzID), "it_IT", UCAL_TRADITIONAL, &status);
294     if(U_FAILURE(status))    {
295         log_data_err("FAIL: error in ucal_open calit : %s - (Are you missing data?)\n", u_errorName(status));
296     }
297 
298     /*Testing the  clone() function*/
299     calfrclone = ucal_clone(calfr, &status);
300     if(U_FAILURE(status)){
301         log_data_err("FAIL: error in ucal_clone calfr : %s - (Are you missing data?)\n", u_errorName(status));
302     }
303 
304     /*Testing udat_getAvailable() and udat_countAvailable()*/
305     log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
306     count=ucal_countAvailable();
307     /* use something sensible w/o hardcoding the count */
308     if(count > 0) {
309         log_verbose("PASS: ucal_countAvailable() works fine\n");
310         log_verbose("The no: of locales for which calendars are avilable are %d\n", count);
311     } else {
312         log_data_err("FAIL: Error in countAvailable()\n");
313     }
314 
315     for(i=0;i<count;i++) {
316        log_verbose("%s\n", ucal_getAvailable(i));
317     }
318 
319 
320     /*Testing the equality between calendar's*/
321     log_verbose("\nTesting ucal_equivalentTo()\n");
322     if(caldef && caldef2 && calfr && calit) {
323       if(ucal_equivalentTo(caldef, caldef2) == FALSE || ucal_equivalentTo(caldef, calfr)== TRUE ||
324         ucal_equivalentTo(caldef, calit)== TRUE || ucal_equivalentTo(calfr, calfrclone) == FALSE) {
325           log_data_err("FAIL: Error. equivalentTo test failed (Are you missing data?)\n");
326       } else {
327           log_verbose("PASS: equivalentTo test passed\n");
328       }
329     }
330 
331 
332     /*Testing the current time and date using ucal_getnow()*/
333     log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
334     now=ucal_getNow();
335     /* open the date format and format the date to check the output */
336     datdef=udat_open(UDAT_FULL,UDAT_FULL ,NULL, NULL, 0,NULL,0,&status);
337     if(U_FAILURE(status)){
338         log_data_err("FAIL: error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
339         return;
340     }
341     log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datdef, now)) );
342 
343 
344     /*Testing the TimeZoneDisplayName */
345     log_verbose("\nTesting the fetching of time zone display name\n");
346     /*for the US locale */
347     resultlength=0;
348     resultlengthneeded=ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", NULL, resultlength, &status);
349 
350     if(status==U_BUFFER_OVERFLOW_ERROR)
351     {
352         status=U_ZERO_ERROR;
353         resultlength=resultlengthneeded+1;
354         result=(UChar*)malloc(sizeof(UChar) * resultlength);
355         ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", result, resultlength, &status);
356     }
357     if(U_FAILURE(status))    {
358         log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status));
359     }
360     else{
361         log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
362             u_errorName(status), resultlengthneeded);
363     }
364 
365 
366 #define expectPDT "Pacific Daylight Time"
367 
368     tzdname=(UChar*)malloc(sizeof(UChar) * (sizeof(expectPDT)+1));
369     u_uastrcpy(tzdname, expectPDT);
370     if(u_strcmp(tzdname, result)==0){
371         log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf, result) );
372     }
373     else{
374         log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result) , expectPDT);
375     }
376 
377     ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_DST, "en_US", result, resultlength, &status);
378     u_uastrcpy(tzdname, "PDT");
379     if(u_strcmp(tzdname, result) != 0){
380         log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
381     }
382 
383     ucal_getTimeZoneDisplayName(caldef, UCAL_STANDARD, "en_US", result, resultlength, &status);
384     u_uastrcpy(tzdname, "Pacific Standard Time");
385     if(u_strcmp(tzdname, result) != 0){
386         log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
387     }
388 
389     ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_STANDARD, "en_US", result, resultlength, &status);
390     u_uastrcpy(tzdname, "PST");
391     if(u_strcmp(tzdname, result) != 0){
392         log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
393     }
394 
395 
396     /*testing the setAttributes and getAttributes of a UCalendar*/
397     log_verbose("\nTesting the getAttributes and set Attributes\n");
398     count=ucal_getAttribute(calit, UCAL_LENIENT);
399     count2=ucal_getAttribute(calfr, UCAL_LENIENT);
400     ucal_setAttribute(calit, UCAL_LENIENT, 0);
401     ucal_setAttribute(caldef, UCAL_LENIENT, count2);
402     if( ucal_getAttribute(calit, UCAL_LENIENT) !=0 ||
403         ucal_getAttribute(calfr, UCAL_LENIENT)!=ucal_getAttribute(caldef, UCAL_LENIENT) )
404         log_err("FAIL: there is an error in getAttributes or setAttributes\n");
405     else
406         log_verbose("PASS: attribute set and got successfully\n");
407         /*set it back to orginal value */
408     log_verbose("Setting it back to normal\n");
409     ucal_setAttribute(calit, UCAL_LENIENT, count);
410     if(ucal_getAttribute(calit, UCAL_LENIENT)!=count)
411         log_err("FAIL: Error in setting the attribute back to normal\n");
412 
413     /*setting the first day of the week to other values  */
414     count=ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK);
415     for (i=1; i<=7; ++i) {
416         ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,i);
417         if (ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK) != i)
418             log_err("FAIL: set/getFirstDayOfWeek failed\n");
419     }
420     /*get bogus Attribute*/
421     count=ucal_getAttribute(calit, (UCalendarAttribute)99); /* BOGUS_ATTRIBUTE */
422     if(count != -1){
423         log_err("FAIL: get/bogus attribute should return -1\n");
424     }
425 
426     /*set it back to normal */
427     ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,count);
428     /*setting minimal days of the week to other values */
429     count=ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
430     for (i=1; i<=7; ++i) {
431         ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,i);
432         if (ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK) != i)
433             log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
434     }
435     /*set it back to normal */
436     ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,count);
437 
438 
439     /*testing if the UCalendar's timezone is currently in day light saving's time*/
440     log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
441     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 10, 45, 20, &status);
442     ucal_inDaylightTime(caldef, &status );
443     if(U_FAILURE(status))    {
444         log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status));
445     }
446     if(!ucal_inDaylightTime(caldef, &status))
447         log_verbose("PASS: It is not in daylight saving's time\n");
448     else
449         log_err("FAIL: It is not in daylight saving's time\n");
450 
451     /*closing the UCalendar*/
452     ucal_close(caldef);
453     ucal_close(caldef2);
454     ucal_close(calfr);
455     ucal_close(calit);
456     ucal_close(calfrclone);
457 
458     /*testing ucal_getType, and ucal_open with UCAL_GREGORIAN*/
459     for (ucalGetTypeTestPtr = ucalGetTypeTests; ucalGetTypeTestPtr->expectedResult != NULL; ++ucalGetTypeTestPtr) {
460         const char * localeToDisplay = (ucalGetTypeTestPtr->locale != NULL)? ucalGetTypeTestPtr->locale: "<NULL>";
461         status = U_ZERO_ERROR;
462         caldef = ucal_open(NULL, 0, ucalGetTypeTestPtr->locale, ucalGetTypeTestPtr->calType, &status);
463         if ( U_SUCCESS(status) ) {
464             const char * calType = ucal_getType(caldef, &status);
465             if ( U_SUCCESS(status) && calType != NULL ) {
466                 if ( uprv_strcmp( calType, ucalGetTypeTestPtr->expectedResult ) != 0 ) {
467                     log_err("FAIL: ucal_open %s type %d does not return %s calendar\n", localeToDisplay,
468                                                 ucalGetTypeTestPtr->calType, ucalGetTypeTestPtr->expectedResult);
469                 }
470             } else {
471                 log_err("FAIL: ucal_open %s type %d, then ucal_getType fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
472             }
473             ucal_close(caldef);
474         } else {
475             log_err("FAIL: ucal_open %s type %d fails\n", localeToDisplay, ucalGetTypeTestPtr->calType);
476         }
477     }
478 
479     /*closing the UDateFormat used */
480     udat_close(datdef);
481     free(result);
482     free(tzdname);
483 }
484 
485 /*------------------------------------------------------*/
486 /*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
487 
TestGetSetDateAPI()488 static void TestGetSetDateAPI()
489 {
490     UCalendar *caldef = 0, *caldef2 = 0, *caldef3 = 0;
491     UChar tzID[4];
492     UDate d1;
493     int32_t hour;
494     int32_t zoneOffset;
495     UDateFormat *datdef = 0;
496     UErrorCode status=U_ZERO_ERROR;
497     UDate d2= 837039928046.0;
498     UChar temp[30];
499 	double testMillis;
500 	int32_t dateBit;
501     UChar id[4];
502     int32_t idLen;
503 
504     log_verbose("\nOpening the calendars()\n");
505     u_strcpy(tzID, fgGMTID);
506     /*open the calendars used */
507     caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
508     caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
509     caldef3=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
510     /*open the dateformat */
511     /* this is supposed to open default date format, but later on it treats it like it is "en_US"
512        - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
513     /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
514     datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US",fgGMTID,-1,NULL,0, &status);
515     if(U_FAILURE(status))
516     {
517         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
518         return;
519     }
520 
521     /*Testing getMillis and setMillis */
522     log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
523     d1=ucal_getMillis(caldef, &status);
524     if(U_FAILURE(status)){
525         log_err("Error in getMillis : %s\n", u_errorName(status));
526     }
527 
528     /*testing setMillis */
529     log_verbose("\nTesting the set date and time function using setMillis\n");
530     ucal_setMillis(caldef, d2, &status);
531     if(U_FAILURE(status)){
532         log_err("Error in setMillis : %s\n", u_errorName(status));
533     }
534 
535     /*testing if the calendar date is set properly or not  */
536     d1=ucal_getMillis(caldef, &status);
537     if(u_strcmp(myDateFormat(datdef, d1), myDateFormat(datdef, d2))!=0)
538         log_err("error in setMillis or getMillis\n");
539     /*-------------------*/
540 
541     /*testing large negative millis*/
542 	/*test a previously failed millis and beyond the lower bounds - ICU trac #9403 */
543 	// -184303902611600000.0         - just beyond lower bounds (#9403 sets U_ILLEGAL_ARGUMENT_ERROR in strict mode)
544 	// -46447814188001000.0	         - fixed by #9403
545 
546     log_verbose("\nTesting very large valid millis & invalid setMillis values (in both strict & lienent modes) detected\n");
547 
548 	testMillis = -46447814188001000.0;	// point where floorDivide in handleComputeFields failed as per #9403
549 	log_verbose("using value[%lf]\n", testMillis);
550     ucal_setAttribute(caldef3, UCAL_LENIENT, 0);
551     ucal_setMillis(caldef3, testMillis, &status);
552    	if(U_FAILURE(status)){
553    		log_err("Fail: setMillis incorrectly detected invalid value : for millis : %e : returned  : %s\n", testMillis, u_errorName(status));
554 		status = U_ZERO_ERROR;
555 	}
556 
557     log_verbose("\nTesting invalid setMillis values detected\n");
558 	testMillis = -184303902611600000.0;
559 	log_verbose("using value[%lf]\n", testMillis);
560     ucal_setAttribute(caldef3, UCAL_LENIENT, 1);
561    	ucal_setMillis(caldef3, testMillis, &status);
562    	if(U_FAILURE(status)){
563    		log_err("Fail: setMillis incorrectly detected invalid value : for millis : %e : returned  : %s\n", testMillis, u_errorName(status));
564 		status = U_ZERO_ERROR;
565    	} else {
566         dateBit = ucal_get(caldef2, UCAL_MILLISECOND, &status);
567         if(testMillis == dateBit)
568         {
569 		    log_err("Fail: error in setMillis, allowed invalid value %e : returns millisecond : %d", testMillis, dateBit);
570         } else {
571             log_verbose("Pass: setMillis correctly pinned min, returned : %d", dateBit);
572         }
573 	}
574 
575     log_verbose("\nTesting invalid setMillis values detected\n");
576 	testMillis = -184303902611600000.0;
577 	log_verbose("using value[%lf]\n", testMillis);
578     ucal_setAttribute(caldef3, UCAL_LENIENT, 0);
579    	ucal_setMillis(caldef3, testMillis, &status);
580    	if(U_FAILURE(status)){
581    		log_verbose("Pass: Illegal argument error as expected : for millis : %e : returned  : %s\n", testMillis, u_errorName(status));
582 		status = U_ZERO_ERROR;
583    	} else {
584 		dateBit = ucal_get(caldef3, UCAL_DAY_OF_MONTH, &status);
585 		log_err("Fail: error in setMillis, allowed invalid value %e : returns DayOfMonth : %d", testMillis, dateBit);
586 	}
587 	/*-------------------*/
588 
589 
590     ctest_setTimeZone(NULL, &status);
591 
592     /*testing ucal_setTimeZone() and ucal_getTimeZoneID function*/
593     log_verbose("\nTesting if the function ucal_setTimeZone() and ucal_getTimeZoneID work fine\n");
594     idLen = ucal_getTimeZoneID(caldef2, id, sizeof(id)/sizeof(id[0]), &status);
595     (void)idLen;    /* Suppress set but not used warning. */
596     if (U_FAILURE(status)) {
597         log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
598     } else if (u_strcmp(id, fgGMTID) != 0) {
599         log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(fgGMTID));
600     } else {
601         log_verbose("PASS: getTimeZoneID works fine\n");
602     }
603 
604     ucal_setMillis(caldef2, d2, &status);
605     if(U_FAILURE(status)){
606         log_err("Error in getMillis : %s\n", u_errorName(status));
607     }
608     hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
609 
610     u_uastrcpy(tzID, "PST");
611     ucal_setTimeZone(caldef2,tzID, 3, &status);
612     if(U_FAILURE(status)){
613         log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
614     }
615     else
616         log_verbose("ucal_setTimeZone worked fine\n");
617 
618     idLen = ucal_getTimeZoneID(caldef2, id, sizeof(id)/sizeof(id[0]), &status);
619     if (U_FAILURE(status)) {
620         log_err("Error in getTimeZoneID : %s\n", u_errorName(status));
621     } else if (u_strcmp(id, tzID) != 0) {
622         log_err("FAIL: getTimeZoneID returns a wrong ID: actual=%d, expected=%s\n", austrdup(id), austrdup(tzID));
623     } else {
624         log_verbose("PASS: getTimeZoneID works fine\n");
625     }
626 
627     if(hour == ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status))
628         log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
629     else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */
630         log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
631     else
632         log_verbose("PASS: setTimeZone works fine\n");
633 
634     /*testing setTimeZone roundtrip */
635     log_verbose("\nTesting setTimeZone() roundtrip\n");
636     u_strcpy(tzID, fgGMTID);
637     ucal_setTimeZone(caldef2, tzID, 3, &status);
638     if(U_FAILURE(status)){
639         log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
640     }
641     if(d2==ucal_getMillis(caldef2, &status))
642         log_verbose("PASS: setTimeZone roundtrip test passed\n");
643     else
644         log_err("FAIL: setTimeZone roundtrip test failed\n");
645 
646     zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
647     if(U_FAILURE(status)){
648         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
649     }
650     else if (zoneOffset != 0) {
651         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
652     }
653 
654     ucal_setTimeZone(caldef2, NULL, -1, &status);
655     if(U_FAILURE(status)){
656         log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
657     }
658     if(ucal_getMillis(caldef2, &status))
659         log_verbose("PASS: setTimeZone roundtrip test passed\n");
660     else
661         log_err("FAIL: setTimeZone roundtrip test failed\n");
662 
663     zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
664     if(U_FAILURE(status)){
665         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
666     }
667     else if (zoneOffset != -28800000) {
668         log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
669     }
670 
671     ctest_resetTimeZone();
672 
673 /*----------------------------*     */
674 
675 
676 
677     /*Testing  if setDate works fine  */
678     log_verbose("\nTesting the ucal_setDate() function \n");
679     u_uastrcpy(temp, "Dec 17, 1971, 11:05:28 PM");
680     ucal_setDate(caldef,1971, UCAL_DECEMBER, 17, &status);
681     if(U_FAILURE(status)){
682         log_err("error in setting the calendar date : %s\n", u_errorName(status));
683     }
684     /*checking if the calendar date is set properly or not  */
685     d1=ucal_getMillis(caldef, &status);
686     if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
687         log_verbose("PASS:setDate works fine\n");
688     else
689         log_err("FAIL:Error in setDate()\n");
690 
691 
692     /* Testing setDate Extensively with various input values */
693     log_verbose("\nTesting ucal_setDate() extensively\n");
694     ucal_setDate(caldef, 1999, UCAL_JANUARY, 10, &status);
695     verify1("1999  10th day of January  is :", caldef, datdef, 1999, UCAL_JANUARY, 10);
696     ucal_setDate(caldef, 1999, UCAL_DECEMBER, 3, &status);
697     verify1("1999 3rd day of December  is :", caldef, datdef, 1999, UCAL_DECEMBER, 3);
698     ucal_setDate(caldef, 2000, UCAL_MAY, 3, &status);
699     verify1("2000 3rd day of May is :", caldef, datdef, 2000, UCAL_MAY, 3);
700     ucal_setDate(caldef, 1999, UCAL_AUGUST, 32, &status);
701     verify1("1999 32th day of August is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1);
702     ucal_setDate(caldef, 1999, UCAL_MARCH, 0, &status);
703     verify1("1999 0th day of March is :", caldef, datdef, 1999, UCAL_FEBRUARY, 28);
704     ucal_setDate(caldef, 0, UCAL_MARCH, 12, &status);
705 
706     /*--------------------*/
707 
708     /*Testing if setDateTime works fine */
709     log_verbose("\nTesting the ucal_setDateTime() function \n");
710     u_uastrcpy(temp, "May 3, 1972, 4:30:42 PM");
711     ucal_setDateTime(caldef,1972, UCAL_MAY, 3, 16, 30, 42, &status);
712     if(U_FAILURE(status)){
713         log_err("error in setting the calendar date : %s\n", u_errorName(status));
714     }
715     /*checking  if the calendar date is set properly or not  */
716     d1=ucal_getMillis(caldef, &status);
717     if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
718         log_verbose("PASS: setDateTime works fine\n");
719     else
720         log_err("FAIL: Error in setDateTime\n");
721 
722 
723 
724     /*Testing setDateTime extensively with various input values*/
725     log_verbose("\nTesting ucal_setDateTime() function extensively\n");
726     ucal_setDateTime(caldef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, &status);
727     verify2("1999  10th day of October  at 6:45:30 is :", caldef, datdef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, 0 );
728     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 15, 10, 55, &status);
729     verify2("1999 3rd day of March   at 15:10:55 is :", caldef, datdef, 1999, UCAL_MARCH, 3, 3, 10, 55, 1);
730     ucal_setDateTime(caldef, 1999, UCAL_MAY, 3, 25, 30, 45, &status);
731     verify2("1999 3rd day of May at 25:30:45 is :", caldef, datdef, 1999, UCAL_MAY, 4, 1, 30, 45, 0);
732     ucal_setDateTime(caldef, 1999, UCAL_AUGUST, 32, 22, 65, 40, &status);
733     verify2("1999 32th day of August at 22:65:40 is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1, 11, 5, 40,1);
734     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, 0, 0, 0,&status);
735     verify2("1999 12th day of March at 0:0:0 is :", caldef, datdef, 1999, UCAL_MARCH, 12, 0, 0, 0, 0);
736     ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, -10, -10,0, &status);
737     verify2("1999 12th day of March is at -10:-10:0 :", caldef, datdef, 1999, UCAL_MARCH, 11, 1, 50, 0, 1);
738 
739 
740 
741     /*close caldef and datdef*/
742     ucal_close(caldef);
743     ucal_close(caldef2);
744     ucal_close(caldef3);
745     udat_close(datdef);
746 }
747 
748 /*----------------------------------------------------------- */
749 /**
750  * Confirm the functioning of the calendar field related functions.
751  */
TestFieldGetSet()752 static void TestFieldGetSet()
753 {
754     UCalendar *cal = 0;
755     UChar tzID[4];
756     UDateFormat *datdef = 0;
757     UDate d1 = 0;
758     UErrorCode status=U_ZERO_ERROR;
759     (void)d1;   /* Suppress set but not used warning. */
760     log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
761     u_strcpy(tzID, fgGMTID);
762     /*open the calendar used */
763     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
764     if (U_FAILURE(status)) {
765         log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
766         return;
767     }
768     datdef=udat_open(UDAT_SHORT,UDAT_SHORT ,NULL,fgGMTID,-1,NULL, 0, &status);
769     if(U_FAILURE(status))
770     {
771         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
772     }
773 
774     /*Testing ucal_get()*/
775     log_verbose("\nTesting the ucal_get() function of Calendar\n");
776     ucal_setDateTime(cal, 1999, UCAL_MARCH, 12, 5, 25, 30, &status);
777     if(U_FAILURE(status)){
778         log_data_err("error in the setDateTime() : %s (Are you missing data?)\n", u_errorName(status));
779     }
780     if(ucal_get(cal, UCAL_YEAR, &status)!=1999 || ucal_get(cal, UCAL_MONTH, &status)!=2 ||
781         ucal_get(cal, UCAL_DATE, &status)!=12 || ucal_get(cal, UCAL_HOUR, &status)!=5)
782         log_data_err("error in ucal_get() -> %s (Are you missing data?)\n", u_errorName(status));
783     else if(ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status)!=2 || ucal_get(cal, UCAL_DAY_OF_WEEK, &status)!=6
784         || ucal_get(cal, UCAL_WEEK_OF_MONTH, &status)!=2 || ucal_get(cal, UCAL_WEEK_OF_YEAR, &status)!= 11)
785         log_err("FAIL: error in ucal_get()\n");
786     else
787         log_verbose("PASS: ucal_get() works fine\n");
788 
789     /*Testing the ucal_set() , ucal_clear() functions of calendar*/
790     log_verbose("\nTesting the set, and clear  field functions of calendar\n");
791     ucal_setAttribute(cal, UCAL_LENIENT, 0);
792     ucal_clear(cal);
793     ucal_set(cal, UCAL_YEAR, 1997);
794     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
795     ucal_set(cal, UCAL_DATE, 3);
796     verify1("1997 third day of June = ", cal, datdef, 1997, UCAL_JUNE, 3);
797     ucal_clear(cal);
798     ucal_set(cal, UCAL_YEAR, 1997);
799     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
800     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
801     ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 1);
802     verify1("1997 first Tuesday in June = ", cal, datdef, 1997, UCAL_JUNE, 3);
803     ucal_clear(cal);
804     ucal_set(cal, UCAL_YEAR, 1997);
805     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
806     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
807     ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, - 1);
808     verify1("1997 last Tuesday in June = ", cal, datdef,1997,   UCAL_JUNE, 24);
809     /*give undesirable input    */
810     status = U_ZERO_ERROR;
811     ucal_clear(cal);
812     ucal_set(cal, UCAL_YEAR, 1997);
813     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
814     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
815     ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 0);
816     d1 = ucal_getMillis(cal, &status);
817     if (status != U_ILLEGAL_ARGUMENT_ERROR) {
818         log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 zero-th Tuesday in June\n");
819     } else {
820         log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
821     }
822     status = U_ZERO_ERROR;
823     ucal_clear(cal);
824     ucal_set(cal, UCAL_YEAR, 1997);
825     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
826     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
827     ucal_set(cal, UCAL_WEEK_OF_MONTH, 1);
828     verify1("1997 Tuesday in week 1 of June = ", cal,datdef, 1997, UCAL_JUNE, 3);
829     ucal_clear(cal);
830     ucal_set(cal, UCAL_YEAR, 1997);
831     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
832     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
833     ucal_set(cal, UCAL_WEEK_OF_MONTH, 5);
834     verify1("1997 Tuesday in week 5 of June = ", cal,datdef, 1997, UCAL_JULY, 1);
835     status = U_ZERO_ERROR;
836     ucal_clear(cal);
837     ucal_set(cal, UCAL_YEAR, 1997);
838     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
839     ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
840     ucal_set(cal, UCAL_WEEK_OF_MONTH, 0);
841     ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
842     d1 = ucal_getMillis(cal,&status);
843     if (status != U_ILLEGAL_ARGUMENT_ERROR){
844         log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 Tuesday zero-th week in June\n");
845     } else {
846         log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
847     }
848     status = U_ZERO_ERROR;
849     ucal_clear(cal);
850     ucal_set(cal, UCAL_YEAR_WOY, 1997);
851     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
852     ucal_set(cal, UCAL_WEEK_OF_YEAR, 1);
853     verify1("1997 Tuesday in week 1 of year = ", cal, datdef,1996, UCAL_DECEMBER, 31);
854     ucal_clear(cal);
855     ucal_set(cal, UCAL_YEAR, 1997);
856     ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
857     ucal_set(cal, UCAL_WEEK_OF_YEAR, 10);
858     verify1("1997 Tuesday in week 10 of year = ", cal,datdef, 1997, UCAL_MARCH, 4);
859     ucal_clear(cal);
860     ucal_set(cal, UCAL_YEAR, 1999);
861     ucal_set(cal, UCAL_DAY_OF_YEAR, 1);
862     verify1("1999 1st day of the year =", cal, datdef, 1999, UCAL_JANUARY, 1);
863     ucal_set(cal, UCAL_MONTH, -3);
864     d1 = ucal_getMillis(cal,&status);
865     if (status != U_ILLEGAL_ARGUMENT_ERROR){
866         log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1999 -3th month\n");
867     } else {
868         log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
869     }
870 
871     ucal_setAttribute(cal, UCAL_LENIENT, 1);
872 
873     ucal_set(cal, UCAL_MONTH, -3);
874     verify1("1999 -3th month should be", cal, datdef, 1998, UCAL_OCTOBER, 1);
875 
876 
877     /*testing isSet and clearField()*/
878     if(!ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
879         log_err("FAIL: error in isSet\n");
880     else
881         log_verbose("PASS: isSet working fine\n");
882     ucal_clearField(cal, UCAL_WEEK_OF_YEAR);
883     if(ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
884         log_err("FAIL: there is an error in clearField or isSet\n");
885     else
886         log_verbose("PASS :clearField working fine\n");
887 
888     /*-------------------------------*/
889 
890     ucal_close(cal);
891     udat_close(datdef);
892 }
893 
894 typedef struct {
895     const char * zone;
896     int32_t      year;
897     int32_t      month;
898     int32_t      day;
899     int32_t      hour;
900 } TransitionItem;
901 
902 static const TransitionItem transitionItems[] = {
903     { "America/Caracas", 2007, UCAL_DECEMBER,  8, 10 }, /* day before change in UCAL_ZONE_OFFSET */
904     { "US/Pacific",      2011,    UCAL_MARCH, 12, 10 }, /* day before change in UCAL_DST_OFFSET */
905     { NULL,                 0,             0,  0,  0 }
906 };
907 
908 /* ------------------------------------- */
909 /**
910  * Execute adding and rolling in Calendar extensively,
911  */
TestAddRollExtensive()912 static void TestAddRollExtensive()
913 {
914     const TransitionItem * itemPtr;
915     UCalendar *cal = 0;
916     int32_t i,limit;
917     UChar tzID[32];
918     UCalendarDateFields e;
919     int32_t y,m,d,hr,min,sec,ms;
920     int32_t maxlimit = 40;
921     UErrorCode status = U_ZERO_ERROR;
922     y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
923 
924     log_verbose("Testing add and roll extensively\n");
925 
926     u_uastrcpy(tzID, "PST");
927     /*open the calendar used */
928     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
929     if (U_FAILURE(status)) {
930         log_data_err("ucal_open() failed : %s - (Are you missing data?)\n", u_errorName(status));
931         return;
932     }
933 
934     ucal_set(cal, UCAL_YEAR, y);
935     ucal_set(cal, UCAL_MONTH, m);
936     ucal_set(cal, UCAL_DATE, d);
937     ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
938 
939     /* Confirm that adding to various fields works.*/
940     log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
941     checkDate(cal, y, m, d);
942     ucal_add(cal,UCAL_YEAR, 1, &status);
943     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status)); return; }
944     y++;
945     checkDate(cal, y, m, d);
946     ucal_add(cal,UCAL_MONTH, 12, &status);
947     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
948     y+=1;
949     checkDate(cal, y, m, d);
950     ucal_add(cal,UCAL_DATE, 1, &status);
951     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
952     d++;
953     checkDate(cal, y, m, d);
954     ucal_add(cal,UCAL_DATE, 2, &status);
955     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
956     d += 2;
957     checkDate(cal, y, m, d);
958     ucal_add(cal,UCAL_DATE, 28, &status);
959     if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
960     ++m;
961     checkDate(cal, y, m, d);
962     ucal_add(cal, (UCalendarDateFields)-1, 10, &status);
963     if(status==U_ILLEGAL_ARGUMENT_ERROR)
964         log_verbose("Pass: Illegal argument error as expected\n");
965     else{
966         log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status));
967     }
968     status=U_ZERO_ERROR;
969 
970 
971     /*confirm that applying roll to various fields works fine*/
972     log_verbose("\nTesting to confirm that ucal_roll() works\n");
973     ucal_roll(cal, UCAL_DATE, -1, &status);
974     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
975     d -=1;
976     checkDate(cal, y, m, d);
977     ucal_roll(cal, UCAL_MONTH, -2, &status);
978     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
979     m -=2;
980     checkDate(cal, y, m, d);
981     ucal_roll(cal, UCAL_DATE, 1, &status);
982     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
983     d +=1;
984     checkDate(cal, y, m, d);
985     ucal_roll(cal, UCAL_MONTH, -12, &status);
986     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
987     checkDate(cal, y, m, d);
988     ucal_roll(cal, UCAL_YEAR, -1, &status);
989     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
990     y -=1;
991     checkDate(cal, y, m, d);
992     ucal_roll(cal, UCAL_DATE, 29, &status);
993     if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
994     d = 2;
995     checkDate(cal, y, m, d);
996     ucal_roll(cal, (UCalendarDateFields)-1, 10, &status);
997     if(status==U_ILLEGAL_ARGUMENT_ERROR)
998         log_verbose("Pass: illegal arguement error as expected\n");
999     else{
1000         log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status));
1001         return;
1002     }
1003     status=U_ZERO_ERROR;
1004     ucal_clear(cal);
1005     ucal_setDateTime(cal, 1999, UCAL_FEBRUARY, 28, 10, 30, 45,  &status);
1006     if(U_FAILURE(status)){
1007         log_err("error is setting the datetime: %s\n", u_errorName(status));
1008     }
1009     ucal_add(cal, UCAL_MONTH, 1, &status);
1010     checkDate(cal, 1999, UCAL_MARCH, 28);
1011     ucal_add(cal, UCAL_MILLISECOND, 1000, &status);
1012     checkDateTime(cal, 1999, UCAL_MARCH, 28, 10, 30, 46, 0, UCAL_MILLISECOND);
1013 
1014     ucal_close(cal);
1015 /*--------------- */
1016     status=U_ZERO_ERROR;
1017     /* Testing add and roll extensively */
1018     log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
1019     y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
1020     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
1021     if (U_FAILURE(status)) {
1022         log_err("ucal_open failed: %s\n", u_errorName(status));
1023         return;
1024     }
1025     ucal_set(cal, UCAL_YEAR, y);
1026     ucal_set(cal, UCAL_MONTH, m);
1027     ucal_set(cal, UCAL_DATE, d);
1028     ucal_set(cal, UCAL_HOUR, hr);
1029     ucal_set(cal, UCAL_MINUTE, min);
1030     ucal_set(cal, UCAL_SECOND,sec);
1031     ucal_set(cal, UCAL_MILLISECOND, ms);
1032     ucal_setAttribute(cal, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,1);
1033     status=U_ZERO_ERROR;
1034 
1035     log_verbose("\nTesting UCalendar add...\n");
1036     for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
1037         limit = maxlimit;
1038         status = U_ZERO_ERROR;
1039         for (i = 0; i < limit; i++) {
1040             ucal_add(cal, e, 1, &status);
1041             if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
1042         }
1043         for (i = 0; i < limit; i++) {
1044             ucal_add(cal, e, -1, &status);
1045             if (U_FAILURE(status)) {
1046                 log_err("ucal_add -1 failed: %s\n", u_errorName(status));
1047                 return;
1048             }
1049         }
1050         checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
1051     }
1052     log_verbose("\nTesting calendar ucal_roll()...\n");
1053     for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
1054         limit = maxlimit;
1055         status = U_ZERO_ERROR;
1056         for (i = 0; i < limit; i++) {
1057             ucal_roll(cal, e, 1, &status);
1058             if (U_FAILURE(status)) {
1059                 limit = i;
1060                 status = U_ZERO_ERROR;
1061             }
1062         }
1063         for (i = 0; i < limit; i++) {
1064             ucal_roll(cal, e, -1, &status);
1065             if (U_FAILURE(status)) {
1066                 log_err("ucal_roll -1 failed: %s\n", u_errorName(status));
1067                 return;
1068             }
1069         }
1070         checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
1071     }
1072 
1073     ucal_close(cal);
1074 /*--------------- */
1075     log_verbose("\nTesting ucal_add() across ZONE_OFFSET and DST_OFFSE transitions.\n");
1076     for (itemPtr = transitionItems; itemPtr->zone != NULL; itemPtr++) {
1077         status=U_ZERO_ERROR;
1078         u_uastrcpy(tzID, itemPtr->zone);
1079         cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);
1080         if (U_FAILURE(status)) {
1081             log_err("ucal_open failed for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1082             continue;
1083         }
1084         ucal_setDateTime(cal, itemPtr->year, itemPtr->month, itemPtr->day, itemPtr->hour, 0, 0, &status);
1085         ucal_add(cal, UCAL_DATE, 1, &status);
1086         hr = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1087         if ( U_FAILURE(status) ) {
1088             log_err("ucal_add failed adding day across transition for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1089         } else if ( hr != itemPtr->hour ) {
1090             log_err("ucal_add produced wrong hour %d when adding day across transition for zone %s\n", hr, itemPtr->zone);
1091         } else {
1092             ucal_add(cal, UCAL_DATE, -1, &status);
1093             hr = ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1094             if ( U_FAILURE(status) ) {
1095                 log_err("ucal_add failed subtracting day across transition for zone %s: %s\n", itemPtr->zone, u_errorName(status));
1096             } else if ( hr != itemPtr->hour ) {
1097                 log_err("ucal_add produced wrong hour %d when subtracting day across transition for zone %s\n", hr, itemPtr->zone);
1098             }
1099         }
1100         ucal_close(cal);
1101     }
1102 }
1103 
1104 /*------------------------------------------------------ */
1105 /*Testing the Limits for various Fields of Calendar*/
TestGetLimits()1106 static void TestGetLimits()
1107 {
1108     UCalendar *cal = 0;
1109     int32_t min, max, gr_min, le_max, ac_min, ac_max, val;
1110     UChar tzID[4];
1111     UErrorCode status = U_ZERO_ERROR;
1112 
1113 
1114     u_uastrcpy(tzID, "PST");
1115     /*open the calendar used */
1116     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
1117     if (U_FAILURE(status)) {
1118         log_data_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s - (Are you missing data?)\n", u_errorName(status));
1119         return;
1120     }
1121 
1122     log_verbose("\nTesting the getLimits function for various fields\n");
1123 
1124 
1125 
1126     ucal_setDate(cal, 1999, UCAL_MARCH, 5, &status); /* Set the date to be March 5, 1999 */
1127     val = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1128     min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MINIMUM, &status);
1129     max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MAXIMUM, &status);
1130     if ( (min != UCAL_SUNDAY || max != UCAL_SATURDAY ) && (min > val && val > max)  && (val != UCAL_FRIDAY)){
1131            log_err("FAIL: Min/max bad\n");
1132            log_err("FAIL: Day of week %d out of range\n", val);
1133            log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val);
1134     }
1135     else
1136         log_verbose("getLimits successful\n");
1137 
1138     val = ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status);
1139     min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MINIMUM, &status);
1140     max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MAXIMUM, &status);
1141     if ( (min != 0 || max != 5 ) && (min > val && val > max)  && (val != 1)){
1142            log_err("FAIL: Min/max bad\n");
1143            log_err("FAIL: Day of week in month %d out of range\n", val);
1144            log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val);
1145 
1146     }
1147     else
1148         log_verbose("getLimits successful\n");
1149 
1150     min=ucal_getLimit(cal, UCAL_MONTH, UCAL_MINIMUM, &status);
1151     max=ucal_getLimit(cal, UCAL_MONTH, UCAL_MAXIMUM, &status);
1152     gr_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_GREATEST_MINIMUM, &status);
1153     le_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_LEAST_MAXIMUM, &status);
1154     ac_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status);
1155     ac_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MAXIMUM, &status);
1156     if(U_FAILURE(status)){
1157         log_err("Error in getLimits: %s\n", u_errorName(status));
1158     }
1159     if(min!=0 || max!=11 || gr_min!=0 || le_max!=11 || ac_min!=0 || ac_max!=11)
1160         log_err("There is and error in getLimits in fetching the values\n");
1161     else
1162         log_verbose("getLimits successful\n");
1163 
1164     ucal_setDateTime(cal, 1999, UCAL_MARCH, 5, 4, 10, 35, &status);
1165     val=ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
1166     min=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MINIMUM, &status);
1167     max=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MAXIMUM, &status);
1168     gr_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_GREATEST_MINIMUM, &status);
1169     le_max=ucal_getLimit(cal, UCAL_MINUTE, UCAL_LEAST_MAXIMUM, &status);
1170     ac_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status);
1171     ac_max=ucal_getLimit(cal, UCAL_SECOND, UCAL_ACTUAL_MAXIMUM, &status);
1172     if( (min!=0 || max!= 11 || gr_min!=0 || le_max!=60 || ac_min!=0 || ac_max!=60) &&
1173         (min>val && val>max) && val!=4){
1174 
1175         log_err("FAIL: Min/max bad\n");
1176         log_err("FAIL: Hour of Day %d out of range\n", val);
1177         log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val);
1178     }
1179     else
1180         log_verbose("getLimits successful\n");
1181 
1182 
1183     /*get BOGUS_LIMIT type*/
1184     val=ucal_getLimit(cal, UCAL_SECOND, (UCalendarLimitType)99, &status);
1185     if(val != -1){
1186         log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
1187     }
1188     status=U_ZERO_ERROR;
1189 
1190 
1191     ucal_close(cal);
1192 }
1193 
1194 
1195 
1196 /* ------------------------------------- */
1197 
1198 /**
1199  * Test that the days of the week progress properly when add is called repeatedly
1200  * for increments of 24 days.
1201  */
TestDOWProgression()1202 static void TestDOWProgression()
1203 {
1204     int32_t initialDOW, DOW, newDOW, expectedDOW;
1205     UCalendar *cal = 0;
1206     UDateFormat *datfor = 0;
1207     UDate date1;
1208     int32_t delta=24;
1209     UErrorCode status = U_ZERO_ERROR;
1210     UChar tzID[4];
1211     char tempMsgBuf[256];
1212     u_strcpy(tzID, fgGMTID);
1213     /*open the calendar used */
1214     cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);;
1215     if (U_FAILURE(status)) {
1216         log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
1217         return;
1218     }
1219 
1220     datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1221     if(U_FAILURE(status)){
1222         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
1223     }
1224 
1225 
1226     ucal_setDate(cal, 1999, UCAL_JANUARY, 1, &status);
1227 
1228     log_verbose("\nTesting the DOW progression\n");
1229 
1230     initialDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1231     if (U_FAILURE(status)) { log_data_err("ucal_get() failed: %s (Are you missing data?)\n", u_errorName(status) ); return; }
1232     newDOW = initialDOW;
1233     do {
1234         DOW = newDOW;
1235         log_verbose("DOW = %d...\n", DOW);
1236         date1=ucal_getMillis(cal, &status);
1237         if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1238         log_verbose("%s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)));
1239 
1240         ucal_add(cal,UCAL_DAY_OF_WEEK, delta, &status);
1241         if (U_FAILURE(status)) { log_err("ucal_add() failed: %s\n", u_errorName(status)); return; }
1242 
1243         newDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1244         if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status)); return; }
1245         expectedDOW = 1 + (DOW + delta - 1) % 7;
1246         date1=ucal_getMillis(cal, &status);
1247         if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1248         if (newDOW != expectedDOW) {
1249             log_err("Day of week should be %d instead of %d on %s", expectedDOW, newDOW,
1250                 u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1251             return;
1252         }
1253     }
1254     while (newDOW != initialDOW);
1255 
1256     ucal_close(cal);
1257     udat_close(datfor);
1258 }
1259 
1260 /* ------------------------------------- */
1261 
1262 /**
1263  * Confirm that the offset between local time and GMT behaves as expected.
1264  */
TestGMTvsLocal()1265 static void TestGMTvsLocal()
1266 {
1267     log_verbose("\nTesting the offset between the GMT and local time\n");
1268     testZones(1999, 1, 1, 12, 0, 0);
1269     testZones(1999, 4, 16, 18, 30, 0);
1270     testZones(1998, 12, 17, 19, 0, 0);
1271 }
1272 
1273 /* ------------------------------------- */
1274 
testZones(int32_t yr,int32_t mo,int32_t dt,int32_t hr,int32_t mn,int32_t sc)1275 static void testZones(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
1276 {
1277     int32_t offset,utc, expected;
1278     UCalendar *gmtcal = 0, *cal = 0;
1279     UDate date1;
1280     double temp;
1281     UDateFormat *datfor = 0;
1282     UErrorCode status = U_ZERO_ERROR;
1283     UChar tzID[4];
1284     char tempMsgBuf[256];
1285 
1286     u_strcpy(tzID, fgGMTID);
1287     gmtcal=ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);;
1288     if (U_FAILURE(status)) {
1289         log_data_err("ucal_open failed: %s - (Are you missing data?)\n", u_errorName(status));
1290         return;
1291     }
1292     u_uastrcpy(tzID, "PST");
1293     cal = ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);
1294     if (U_FAILURE(status)) {
1295         log_err("ucal_open failed: %s\n", u_errorName(status));
1296         return;
1297     }
1298 
1299     datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1300     if(U_FAILURE(status)){
1301         log_data_err("error in creating the dateformat : %s (Are you missing data?)\n", u_errorName(status));
1302     }
1303 
1304     ucal_setDateTime(gmtcal, yr, mo - 1, dt, hr, mn, sc, &status);
1305     if (U_FAILURE(status)) {
1306         log_data_err("ucal_setDateTime failed: %s (Are you missing data?)\n", u_errorName(status));
1307         return;
1308     }
1309     ucal_set(gmtcal, UCAL_MILLISECOND, 0);
1310     date1 = ucal_getMillis(gmtcal, &status);
1311     if (U_FAILURE(status)) {
1312         log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1313         return;
1314     }
1315     log_verbose("date = %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1316 
1317 
1318     ucal_setMillis(cal, date1, &status);
1319     if (U_FAILURE(status)) {
1320         log_err("ucal_setMillis() failed: %s\n", u_errorName(status));
1321         return;
1322     }
1323 
1324     offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
1325     offset += ucal_get(cal, UCAL_DST_OFFSET, &status);
1326 
1327     if (U_FAILURE(status)) {
1328         log_err("ucal_get() failed: %s\n", u_errorName(status));
1329         return;
1330     }
1331     temp=(double)((double)offset / 1000.0 / 60.0 / 60.0);
1332     /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
1333 
1334     utc = ((ucal_get(cal, UCAL_HOUR_OF_DAY, &status) * 60 +
1335                     ucal_get(cal, UCAL_MINUTE, &status)) * 60 +
1336                    ucal_get(cal, UCAL_SECOND, &status)) * 1000 +
1337                     ucal_get(cal, UCAL_MILLISECOND, &status) - offset;
1338     if (U_FAILURE(status)) {
1339         log_err("ucal_get() failed: %s\n", u_errorName(status));
1340         return;
1341     }
1342 
1343     expected = ((hr * 60 + mn) * 60 + sc) * 1000;
1344     if (utc != expected) {
1345         temp=(double)(utc - expected)/ 1000 / 60 / 60.0;
1346         log_err("FAIL: Discrepancy of %d  millis = %fhr\n", utc-expected, temp );
1347     }
1348     else
1349         log_verbose("PASS: the offset between local and GMT is correct\n");
1350     ucal_close(gmtcal);
1351     ucal_close(cal);
1352     udat_close(datfor);
1353 }
1354 
1355 /* ------------------------------------- */
1356 
1357 
1358 
1359 
1360 /* INTERNAL FUNCTIONS USED */
1361 /*------------------------------------------------------------------------------------------- */
1362 
1363 /* ------------------------------------- */
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)1364 static void checkDateTime(UCalendar* c,
1365                         int32_t y, int32_t m, int32_t d,
1366                         int32_t hr, int32_t min, int32_t sec,
1367                         int32_t ms, UCalendarDateFields field)
1368 
1369 {
1370     UErrorCode status = U_ZERO_ERROR;
1371     if (ucal_get(c, UCAL_YEAR, &status) != y ||
1372         ucal_get(c, UCAL_MONTH, &status) != m ||
1373         ucal_get(c, UCAL_DATE, &status) != d ||
1374         ucal_get(c, UCAL_HOUR, &status) != hr ||
1375         ucal_get(c, UCAL_MINUTE, &status) != min ||
1376         ucal_get(c, UCAL_SECOND, &status) != sec ||
1377         ucal_get(c, UCAL_MILLISECOND, &status) != ms) {
1378         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",
1379             (int32_t)field, y, m + 1, d, hr, min, sec, ms,
1380                     ucal_get(c, UCAL_YEAR, &status),
1381                     ucal_get(c, UCAL_MONTH, &status) + 1,
1382                     ucal_get(c, UCAL_DATE, &status),
1383                     ucal_get(c, UCAL_HOUR, &status),
1384                     ucal_get(c, UCAL_MINUTE, &status) + 1,
1385                     ucal_get(c, UCAL_SECOND, &status),
1386                     ucal_get(c, UCAL_MILLISECOND, &status) );
1387 
1388                     if (U_FAILURE(status)){
1389                     log_err("ucal_get failed: %s\n", u_errorName(status));
1390                     return;
1391                     }
1392 
1393      }
1394     else
1395         log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y, m + 1, d, hr, min, sec, ms);
1396 
1397 }
1398 
1399 /* ------------------------------------- */
checkDate(UCalendar * c,int32_t y,int32_t m,int32_t d)1400 static void checkDate(UCalendar* c, int32_t y, int32_t m, int32_t d)
1401 {
1402     UErrorCode status = U_ZERO_ERROR;
1403     if (ucal_get(c,UCAL_YEAR, &status) != y ||
1404         ucal_get(c, UCAL_MONTH, &status) != m ||
1405         ucal_get(c, UCAL_DATE, &status) != d) {
1406 
1407         log_err("FAILURE: Expected y/m/d of %d/%d/%d  got %d/%d/%d\n", y, m + 1, d,
1408                     ucal_get(c, UCAL_YEAR, &status),
1409                     ucal_get(c, UCAL_MONTH, &status) + 1,
1410                     ucal_get(c, UCAL_DATE, &status) );
1411 
1412         if (U_FAILURE(status)) {
1413             log_err("ucal_get failed: %s\n", u_errorName(status));
1414             return;
1415         }
1416     }
1417     else
1418         log_verbose("Confirmed: %d/%d/%d\n", y, m + 1, d);
1419 
1420 
1421 }
1422 
1423 /* ------------------------------------- */
1424 
1425 /* ------------------------------------- */
1426 
verify1(const char * msg,UCalendar * c,UDateFormat * dat,int32_t year,int32_t month,int32_t day)1427 static void verify1(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day)
1428 {
1429     UDate d1;
1430     UErrorCode status = U_ZERO_ERROR;
1431     if (ucal_get(c, UCAL_YEAR, &status) == year &&
1432         ucal_get(c, UCAL_MONTH, &status) == month &&
1433         ucal_get(c, UCAL_DATE, &status) == day) {
1434         if (U_FAILURE(status)) {
1435             log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1436             return;
1437         }
1438         log_verbose("PASS: %s\n", msg);
1439         d1=ucal_getMillis(c, &status);
1440         if (U_FAILURE(status)) {
1441             log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1442             return;
1443         }
1444     /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
1445     }
1446     else {
1447         log_err("FAIL: %s\n", msg);
1448         d1=ucal_getMillis(c, &status);
1449         if (U_FAILURE(status)) {
1450             log_err("ucal_getMillis failed: %s\n", u_errorName(status) );
1451             return;
1452         }
1453         log_err("Got %s  Expected %d/%d/%d \n", austrdup(myDateFormat(dat, d1)), year, month + 1, day );
1454         return;
1455     }
1456 
1457 
1458 }
1459 
1460 /* ------------------------------------ */
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)1461 static void verify2(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day,
1462                                                                      int32_t hour, int32_t min, int32_t sec, int32_t am_pm)
1463 {
1464     UDate d1;
1465     UErrorCode status = U_ZERO_ERROR;
1466     char tempMsgBuf[256];
1467 
1468     if (ucal_get(c, UCAL_YEAR, &status) == year &&
1469         ucal_get(c, UCAL_MONTH, &status) == month &&
1470         ucal_get(c, UCAL_DATE, &status) == day &&
1471         ucal_get(c, UCAL_HOUR, &status) == hour &&
1472         ucal_get(c, UCAL_MINUTE, &status) == min &&
1473         ucal_get(c, UCAL_SECOND, &status) == sec &&
1474         ucal_get(c, UCAL_AM_PM, &status) == am_pm ){
1475         if (U_FAILURE(status)) {
1476             log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1477             return;
1478         }
1479         log_verbose("PASS: %s\n", msg);
1480         d1=ucal_getMillis(c, &status);
1481         if (U_FAILURE(status)) {
1482             log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1483             return;
1484         }
1485         log_verbose("%s\n" , u_austrcpy(tempMsgBuf, myDateFormat(dat, d1)) );
1486     }
1487     else {
1488         log_err("FAIL: %s\n", msg);
1489         d1=ucal_getMillis(c, &status);
1490         if (U_FAILURE(status)) {
1491             log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1492             return;
1493         }
1494         log_err("Got %s Expected %d/%d/%d/ %d:%d:%d  %s\n", austrdup(myDateFormat(dat, d1)),
1495             year, month + 1, day, hour, min, sec, (am_pm==0) ? "AM": "PM");
1496 
1497         return;
1498     }
1499 
1500 
1501 }
1502 
TestGregorianChange()1503 void TestGregorianChange() {
1504     static const UChar utc[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
1505     const int32_t dayMillis = 86400 * INT64_C(1000);    /* 1 day = 86400 seconds */
1506     UCalendar *cal;
1507     UDate date;
1508     UErrorCode errorCode = U_ZERO_ERROR;
1509 
1510     /* Test ucal_setGregorianChange() on a Gregorian calendar. */
1511     errorCode = U_ZERO_ERROR;
1512     cal = ucal_open(utc, -1, "", UCAL_GREGORIAN, &errorCode);
1513     if(U_FAILURE(errorCode)) {
1514         log_data_err("ucal_open(UTC) failed: %s - (Are you missing data?)\n", u_errorName(errorCode));
1515         return;
1516     }
1517     ucal_setGregorianChange(cal, -365 * (dayMillis * (UDate)1), &errorCode);
1518     if(U_FAILURE(errorCode)) {
1519         log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode));
1520     } else {
1521         date = ucal_getGregorianChange(cal, &errorCode);
1522         if(U_FAILURE(errorCode) || date != -365 * (dayMillis * (UDate)1)) {
1523             log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode), date);
1524         }
1525     }
1526     ucal_close(cal);
1527 
1528     /* Test ucal_setGregorianChange() on a non-Gregorian calendar where it should fail. */
1529     errorCode = U_ZERO_ERROR;
1530     cal = ucal_open(utc, -1, "th@calendar=buddhist", UCAL_TRADITIONAL, &errorCode);
1531     if(U_FAILURE(errorCode)) {
1532         log_err("ucal_open(UTC, non-Gregorian) failed: %s\n", u_errorName(errorCode));
1533         return;
1534     }
1535     ucal_setGregorianChange(cal, -730 * (dayMillis * (UDate)1), &errorCode);
1536     if(errorCode != U_UNSUPPORTED_ERROR) {
1537         log_err("ucal_setGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1538                 u_errorName(errorCode));
1539     }
1540     errorCode = U_ZERO_ERROR;
1541     date = ucal_getGregorianChange(cal, &errorCode);
1542     if(errorCode != U_UNSUPPORTED_ERROR) {
1543         log_err("ucal_getGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1544                 u_errorName(errorCode));
1545     }
1546     ucal_close(cal);
1547 }
1548 
TestGetKeywordValuesForLocale()1549 static void TestGetKeywordValuesForLocale() {
1550 #define PREFERRED_SIZE 15
1551 #define MAX_NUMBER_OF_KEYWORDS 5
1552     const char *PREFERRED[PREFERRED_SIZE][MAX_NUMBER_OF_KEYWORDS+1] = {
1553             { "root",        "gregorian", NULL, NULL, NULL, NULL },
1554             { "und",         "gregorian", NULL, NULL, NULL, NULL },
1555             { "en_US",       "gregorian", NULL, NULL, NULL, NULL },
1556             { "en_029",      "gregorian", NULL, NULL, NULL, NULL },
1557             { "th_TH",       "gregorian", "buddhist", NULL, NULL, NULL },  // android-changed
1558             { "und_TH",      "gregorian", "buddhist", NULL, NULL, NULL },  // android-changed
1559             { "en_TH",       "gregorian", "buddhist", NULL, NULL, NULL },  // android-changed
1560             // { "th_TH",       "buddhist", "gregorian", NULL, NULL, NULL },
1561             // { "und_TH",      "buddhist", "gregorian", NULL, NULL, NULL },
1562             // { "en_TH",       "buddhist", "gregorian", NULL, NULL, NULL },
1563             { "he_IL",       "gregorian", "hebrew", "islamic", "islamic-civil", "islamic-tbla" },
1564             { "ar_EG",       "gregorian", "coptic", "islamic", "islamic-civil", "islamic-tbla" },
1565             { "ja",          "gregorian", "japanese", NULL, NULL, NULL },
1566             { "ps_Guru_IN",  "gregorian", "indian", NULL, NULL, NULL },
1567             { "th@calendar=gregorian", "gregorian", "buddhist", NULL, NULL, NULL },
1568             { "en@calendar=islamic",   "gregorian", NULL, NULL, NULL, NULL },
1569             { "zh_TW",       "gregorian", "roc", "chinese", NULL, NULL },
1570             { "ar_IR",       "gregorian", "persian", "islamic", "islamic-civil", "islamic-tbla" },  // android-changed
1571     };
1572     const int32_t EXPECTED_SIZE[PREFERRED_SIZE] = { 1, 1, 1, 1, 2, 2, 2, 5, 5, 2, 2, 2, 1, 3, 5 };
1573     UErrorCode status = U_ZERO_ERROR;
1574     int32_t i, size, j;
1575     UEnumeration *all, *pref;
1576     const char *loc = NULL;
1577     UBool matchPref, matchAll;
1578     const char *value;
1579     int32_t valueLength;
1580     UList *ALLList = NULL;
1581 
1582     UEnumeration *ALL = ucal_getKeywordValuesForLocale("calendar", uloc_getDefault(), FALSE, &status);
1583     if (U_SUCCESS(status)) {
1584         for (i = 0; i < PREFERRED_SIZE; i++) {
1585             pref = NULL;
1586             all = NULL;
1587             loc = PREFERRED[i][0];
1588             pref = ucal_getKeywordValuesForLocale("calendar", loc, TRUE, &status);
1589             matchPref = FALSE;
1590             matchAll = FALSE;
1591 
1592             value = NULL;
1593             valueLength = 0;
1594 
1595             if (U_SUCCESS(status) && uenum_count(pref, &status) == EXPECTED_SIZE[i]) {
1596                 matchPref = TRUE;
1597                 for (j = 0; j < EXPECTED_SIZE[i]; j++) {
1598                     if ((value = uenum_next(pref, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
1599                         if (uprv_strcmp(value, PREFERRED[i][j+1]) != 0) {
1600                             matchPref = FALSE;
1601                             break;
1602                         }
1603                     } else {
1604                         matchPref = FALSE;
1605                         log_err("ERROR getting keyword value for locale \"%s\"\n", loc);
1606                         break;
1607                     }
1608                 }
1609             }
1610 
1611             if (!matchPref) {
1612                 log_err("FAIL: Preferred values for locale \"%s\" does not match expected.\n", loc);
1613                 break;
1614             }
1615             uenum_close(pref);
1616 
1617             all = ucal_getKeywordValuesForLocale("calendar", loc, FALSE, &status);
1618 
1619             size = uenum_count(all, &status);
1620 
1621             if (U_SUCCESS(status) && size == uenum_count(ALL, &status)) {
1622                 matchAll = TRUE;
1623                 ALLList = ulist_getListFromEnum(ALL);
1624                 for (j = 0; j < size; j++) {
1625                     if ((value = uenum_next(all, &valueLength, &status)) != NULL && U_SUCCESS(status)) {
1626                         if (!ulist_containsString(ALLList, value, uprv_strlen(value))) {
1627                             log_err("Locale %s have %s not in ALL\n", loc, value);
1628                             matchAll = FALSE;
1629                             break;
1630                         }
1631                     } else {
1632                         matchAll = FALSE;
1633                         log_err("ERROR getting \"all\" keyword value for locale \"%s\"\n", loc);
1634                         break;
1635                     }
1636                 }
1637             }
1638             if (!matchAll) {
1639                 log_err("FAIL: All values for locale \"%s\" does not match expected.\n", loc);
1640             }
1641 
1642             uenum_close(all);
1643         }
1644     } else {
1645         log_err_status(status, "Failed to get ALL keyword values for default locale %s: %s.\n", uloc_getDefault(), u_errorName(status));
1646     }
1647     uenum_close(ALL);
1648 }
1649 
1650 /*
1651  * Weekend tests, ported from
1652  * icu4j/trunk/main/tests/core/src/com/ibm/icu/dev/test/calendar/IBMCalendarTest.java
1653  * and extended a bit. Notes below from IBMCalendarTest.java ...
1654  * This test tests for specific locale data. This is probably okay
1655  * as far as US data is concerned, but if the Arabic/Yemen data
1656  * changes, this test will have to be updated.
1657  */
1658 
1659 typedef struct {
1660     int32_t year;
1661     int32_t month;
1662     int32_t day;
1663     int32_t hour;
1664     int32_t millisecOffset;
1665     UBool   isWeekend;
1666 } TestWeekendDates;
1667 typedef struct {
1668     const char * locale;
1669     const        TestWeekendDates * dates;
1670     int32_t      numDates;
1671 } TestWeekendDatesList;
1672 
1673 static const TestWeekendDates weekendDates_en_US[] = {
1674     { 2000, UCAL_MARCH, 17, 23,  0, 0 }, /* Fri 23:00        */
1675     { 2000, UCAL_MARCH, 18,  0, -1, 0 }, /* Fri 23:59:59.999 */
1676     { 2000, UCAL_MARCH, 18,  0,  0, 1 }, /* Sat 00:00        */
1677     { 2000, UCAL_MARCH, 18, 15,  0, 1 }, /* Sat 15:00        */
1678     { 2000, UCAL_MARCH, 19, 23,  0, 1 }, /* Sun 23:00        */
1679     { 2000, UCAL_MARCH, 20,  0, -1, 1 }, /* Sun 23:59:59.999 */
1680     { 2000, UCAL_MARCH, 20,  0,  0, 0 }, /* Mon 00:00        */
1681     { 2000, UCAL_MARCH, 20,  8,  0, 0 }, /* Mon 08:00        */
1682 };
1683 static const TestWeekendDates weekendDates_ar_OM[] = {
1684     { 2000, UCAL_MARCH, 15, 23,  0, 0 }, /* Wed 23:00        */
1685     { 2000, UCAL_MARCH, 16,  0, -1, 0 }, /* Wed 23:59:59.999 */
1686     { 2000, UCAL_MARCH, 16,  0,  0, 1 }, /* Thu 00:00        */
1687     { 2000, UCAL_MARCH, 16, 15,  0, 1 }, /* Thu 15:00        */
1688     { 2000, UCAL_MARCH, 17, 23,  0, 1 }, /* Fri 23:00        */
1689     { 2000, UCAL_MARCH, 18,  0, -1, 1 }, /* Fri 23:59:59.999 */
1690     { 2000, UCAL_MARCH, 18,  0,  0, 0 }, /* Sat 00:00        */
1691     { 2000, UCAL_MARCH, 18,  8,  0, 0 }, /* Sat 08:00        */
1692 };
1693 static const TestWeekendDatesList testDates[] = {
1694     { "en_US", weekendDates_en_US, sizeof(weekendDates_en_US)/sizeof(weekendDates_en_US[0]) },
1695     { "ar_OM", weekendDates_ar_OM, sizeof(weekendDates_ar_OM)/sizeof(weekendDates_ar_OM[0]) },
1696 };
1697 
1698 typedef struct {
1699     UCalendarDaysOfWeek  dayOfWeek;
1700     UCalendarWeekdayType dayType;
1701     int32_t              transition; /* transition time if dayType is UCAL_WEEKEND_ONSET or UCAL_WEEKEND_CEASE; else must be 0 */
1702 } TestDaysOfWeek;
1703 typedef struct {
1704     const char *           locale;
1705     const TestDaysOfWeek * days;
1706     int32_t                numDays;
1707 } TestDaysOfWeekList;
1708 
1709 static const TestDaysOfWeek daysOfWeek_en_US[] = {
1710     { UCAL_MONDAY,   UCAL_WEEKDAY,       0        },
1711     { UCAL_FRIDAY,   UCAL_WEEKDAY,       0        },
1712     { UCAL_SATURDAY, UCAL_WEEKEND,       0        },
1713     { UCAL_SUNDAY,   UCAL_WEEKEND,       0        },
1714 };
1715 static const TestDaysOfWeek daysOfWeek_ar_OM[] = { /* Thursday:Friday */
1716     { UCAL_WEDNESDAY,UCAL_WEEKDAY,       0        },
1717     { UCAL_SATURDAY, UCAL_WEEKDAY,       0        },
1718     { UCAL_THURSDAY, UCAL_WEEKEND,       0        },
1719     { UCAL_FRIDAY,   UCAL_WEEKEND,       0        },
1720 };
1721 static const TestDaysOfWeek daysOfWeek_hi_IN[] = { /* Sunday only */
1722     { UCAL_MONDAY,   UCAL_WEEKDAY,       0        },
1723     { UCAL_FRIDAY,   UCAL_WEEKDAY,       0        },
1724     { UCAL_SATURDAY, UCAL_WEEKDAY,       0        },
1725     { UCAL_SUNDAY,   UCAL_WEEKEND,       0        },
1726 };
1727 static const TestDaysOfWeekList testDays[] = {
1728     { "en_US", daysOfWeek_en_US, sizeof(daysOfWeek_en_US)/sizeof(daysOfWeek_en_US[0]) },
1729     { "ar_OM", daysOfWeek_ar_OM, sizeof(daysOfWeek_ar_OM)/sizeof(daysOfWeek_ar_OM[0]) },
1730     { "hi_IN", daysOfWeek_hi_IN, sizeof(daysOfWeek_hi_IN)/sizeof(daysOfWeek_hi_IN[0]) },
1731 };
1732 
1733 static const UChar logDateFormat[] = { 0x0045,0x0045,0x0045,0x0020,0x004D,0x004D,0x004D,0x0020,0x0064,0x0064,0x0020,0x0079,
1734                                        0x0079,0x0079,0x0079,0x0020,0x0047,0x0020,0x0048,0x0048,0x003A,0x006D,0x006D,0x003A,
1735                                        0x0073,0x0073,0x002E,0x0053,0x0053,0x0053,0 }; /* "EEE MMM dd yyyy G HH:mm:ss.SSS" */
1736 enum { kFormattedDateMax = 2*sizeof(logDateFormat)/sizeof(logDateFormat[0]) };
1737 
TestWeekend()1738 static void TestWeekend() {
1739     const TestWeekendDatesList * testDatesPtr = testDates;
1740     const TestDaysOfWeekList *   testDaysPtr = testDays;
1741     int32_t count, subCount;
1742 
1743     UErrorCode fmtStatus = U_ZERO_ERROR;
1744     UDateFormat * fmt = udat_open(UDAT_NONE, UDAT_NONE, "en", NULL, 0, NULL, 0, &fmtStatus);
1745     if (U_SUCCESS(fmtStatus)) {
1746         udat_applyPattern(fmt, FALSE, logDateFormat, -1);
1747     } else {
1748         log_data_err("Unable to create UDateFormat - %s\n", u_errorName(fmtStatus));
1749         return;
1750     }
1751     for (count = sizeof(testDates)/sizeof(testDates[0]); count-- > 0; ++testDatesPtr) {
1752         UErrorCode status = U_ZERO_ERROR;
1753         UCalendar * cal = ucal_open(NULL, 0, testDatesPtr->locale, UCAL_GREGORIAN, &status);
1754         log_verbose("locale: %s\n", testDatesPtr->locale);
1755         if (U_SUCCESS(status)) {
1756             const TestWeekendDates * weekendDatesPtr = testDatesPtr->dates;
1757             for (subCount = testDatesPtr->numDates; subCount--; ++weekendDatesPtr) {
1758                 UDate dateToTest;
1759                 UBool isWeekend;
1760                 char  fmtDateBytes[kFormattedDateMax] = "<could not format test date>"; /* initialize for failure */
1761 
1762                 ucal_clear(cal);
1763                 ucal_setDateTime(cal, weekendDatesPtr->year, weekendDatesPtr->month, weekendDatesPtr->day,
1764                                  weekendDatesPtr->hour, 0, 0, &status);
1765                 dateToTest = ucal_getMillis(cal, &status) + weekendDatesPtr->millisecOffset;
1766                 isWeekend = ucal_isWeekend(cal, dateToTest, &status);
1767                 if (U_SUCCESS(fmtStatus)) {
1768                     UChar fmtDate[kFormattedDateMax];
1769                     (void)udat_format(fmt, dateToTest, fmtDate, kFormattedDateMax, NULL, &fmtStatus);
1770                     if (U_SUCCESS(fmtStatus)) {
1771                         u_austrncpy(fmtDateBytes, fmtDate, kFormattedDateMax);
1772                         fmtDateBytes[kFormattedDateMax-1] = 0;
1773                     } else {
1774                         fmtStatus = U_ZERO_ERROR;
1775                     }
1776                 }
1777                 if ( U_FAILURE(status) ) {
1778                     log_err("FAIL: locale %s date %s isWeekend() status %s\n", testDatesPtr->locale, fmtDateBytes, u_errorName(status) );
1779                     status = U_ZERO_ERROR;
1780                 } else if ( (isWeekend!=0) != (weekendDatesPtr->isWeekend!=0) ) {
1781                     log_err("FAIL: locale %s date %s isWeekend %d, expected the opposite\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
1782                 } else {
1783                     log_verbose("OK:   locale %s date %s isWeekend %d\n", testDatesPtr->locale, fmtDateBytes, isWeekend );
1784                 }
1785             }
1786             ucal_close(cal);
1787         } else {
1788             log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDatesPtr->locale, u_errorName(status) );
1789         }
1790     }
1791     if (U_SUCCESS(fmtStatus)) {
1792         udat_close(fmt);
1793     }
1794 
1795     for (count = sizeof(testDays)/sizeof(testDays[0]); count-- > 0; ++testDaysPtr) {
1796         UErrorCode status = U_ZERO_ERROR;
1797         UCalendar * cal = ucal_open(NULL, 0, testDaysPtr->locale, UCAL_GREGORIAN, &status);
1798         log_verbose("locale: %s\n", testDaysPtr->locale);
1799         if (U_SUCCESS(status)) {
1800             const TestDaysOfWeek * daysOfWeekPtr = testDaysPtr->days;
1801             for (subCount = testDaysPtr->numDays; subCount--; ++daysOfWeekPtr) {
1802                 int32_t transition = 0;
1803                 UCalendarWeekdayType dayType = ucal_getDayOfWeekType(cal, daysOfWeekPtr->dayOfWeek, &status);
1804                 if ( dayType == UCAL_WEEKEND_ONSET || dayType == UCAL_WEEKEND_CEASE ) {
1805                     transition = ucal_getWeekendTransition(cal, daysOfWeekPtr->dayOfWeek, &status);
1806                 }
1807                 if ( U_FAILURE(status) ) {
1808                     log_err("FAIL: locale %s DOW %d getDayOfWeekType() status %s\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, u_errorName(status) );
1809                     status = U_ZERO_ERROR;
1810                 } else if ( dayType != daysOfWeekPtr->dayType || transition != daysOfWeekPtr->transition ) {
1811                     log_err("FAIL: locale %s DOW %d type %d, expected %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType, daysOfWeekPtr->dayType );
1812                 } else {
1813                     log_verbose("OK:   locale %s DOW %d type %d\n", testDaysPtr->locale, daysOfWeekPtr->dayOfWeek, dayType );
1814                 }
1815             }
1816             ucal_close(cal);
1817         } else {
1818             log_data_err("FAIL: ucal_open for locale %s failed: %s - (Are you missing data?)\n", testDaysPtr->locale, u_errorName(status) );
1819         }
1820     }
1821 }
1822 
1823 /**
1824  * TestFieldDifference
1825  */
1826 
1827 typedef struct {
1828     const UChar * timezone;
1829     const char *  locale;
1830     UDate         start;
1831     UDate         target;
1832     UBool         progressive; /* TRUE to compute progressive difference for each field, FALSE to reset calendar after each call */
1833     int32_t       yDiff;
1834     int32_t       MDiff;
1835     int32_t       dDiff;
1836     int32_t       HDiff;
1837     int32_t       mDiff;
1838     int32_t       sDiff; /* 0x7FFFFFFF indicates overflow error expected */
1839 } TFDItem;
1840 
1841 static const UChar tzUSPacific[] = { 0x55,0x53,0x2F,0x50,0x61,0x63,0x69,0x66,0x69,0x63,0 }; /* "US/Pacific" */
1842 static const UChar tzGMT[] = { 0x47,0x4D,0x54,0 }; /* "GMT" */
1843 
1844 static const TFDItem tfdItems[] = {
1845     /* timezone    locale          start              target           progres  yDf  MDf    dDf     HDf       mDf         sDf */
1846     /* For these we compute the progressive difference for each field - not resetting the calendar after each call */
1847     { tzUSPacific, "en_US",        1267459800000.0,   1277772600000.0,  TRUE,     0,   3,    27,      9,       40,          0 }, /* 2010-Mar-01 08:10 -> 2010-Jun-28 17:50 */
1848     { tzUSPacific, "en_US",        1267459800000.0,   1299089280000.0,  TRUE,     1,   0,     1,      1,       58,          0 }, /* 2010-Mar-01 08:10 -> 2011-Mar-02 10:08 */
1849     /* For these we compute the total difference for each field - resetting the calendar after each call */
1850     { tzGMT,       "en_US",        0.0,               1073692800000.0,  FALSE,   34, 408, 12427, 298248, 17894880, 1073692800 }, /* 1970-Jan-01 00:00 -> 2004-Jan-10 00:00 */
1851     { tzGMT,       "en_US",        0.0,               1073779200000.0,  FALSE,   34, 408, 12428, 298272, 17896320, 1073779200 }, /* 1970-Jan-01 00:00 -> 2004-Jan-11 00:00 */
1852     { tzGMT,       "en_US",        0.0,               2147472000000.0,  FALSE,   68, 816, 24855, 596520, 35791200, 2147472000 }, /* 1970-Jan-01 00:00 -> 2038-Jan-19 00:00 */
1853     { tzGMT,       "en_US",        0.0,               2147558400000.0,  FALSE,   68, 816, 24856, 596544, 35792640, 0x7FFFFFFF }, /* 1970-Jan-01 00:00 -> 2038-Jan-20 00:00, seconds diff overflow */
1854     { tzGMT,       "en_US",        0.0,              -1073692800000.0,  FALSE,  -34,-408,-12427,-298248,-17894880,-1073692800 }, /* 1970-Jan-01 00:00 -> 1935-Dec-24 00:00 */
1855     { tzGMT,       "en_US",        0.0,              -1073779200000.0,  FALSE,  -34,-408,-12428,-298272,-17896320,-1073779200 }, /* 1970-Jan-01 00:00 -> 1935-Dec-23 00:00 */
1856     /* check fwd/backward on either side of era boundary and across era boundary */
1857     { tzGMT,       "en_US",       -61978089600000.0,-61820409600000.0,  FALSE,    4,  59,  1825,  43800,  2628000,  157680000 }, /* CE   5-Dec-31 00:00 -> CE  10-Dec-30 00:00 */
1858     { tzGMT,       "en_US",       -61820409600000.0,-61978089600000.0,  FALSE,   -4, -59, -1825, -43800, -2628000, -157680000 }, /* CE  10-Dec-30 00:00 -> CE   5-Dec-31 00:00 */
1859     { tzGMT,       "en_US",       -62451129600000.0,-62293449600000.0,  FALSE,    4,  59,  1825,  43800,  2628000,  157680000 }, /* BCE 10-Jan-04 00:00 -> BCE  5-Jan-03 00:00 */
1860     { tzGMT,       "en_US",       -62293449600000.0,-62451129600000.0,  FALSE,   -4, -59, -1825, -43800, -2628000, -157680000 }, /* BCE  5-Jan-03 00:00 -> BCE 10-Jan-04 00:00 */
1861     { tzGMT,       "en_US",       -62293449600000.0,-61978089600000.0,  FALSE,    9, 119,  3650,  87600,  5256000,  315360000 }, /* BCE  5-Jan-03 00:00 -> CE   5-Dec-31 00:00 */
1862     { tzGMT,       "en_US",       -61978089600000.0,-62293449600000.0,  FALSE,   -9,-119, -3650, -87600, -5256000, -315360000 }, /* CE   5-Dec-31 00:00 -> BCE  5-Jan-03 00:00 */
1863     { tzGMT, "en@calendar=roc",    -1672704000000.0, -1515024000000.0,  FALSE,    4,  59,  1825,  43800,  2628000,  157680000 }, /* MG   5-Dec-30 00:00 -> MG  10-Dec-29 00:00 */
1864     { tzGMT, "en@calendar=roc",    -1515024000000.0, -1672704000000.0,  FALSE,   -4, -59, -1825, -43800, -2628000, -157680000 }, /* MG  10-Dec-29 00:00 -> MG   5-Dec-30 00:00 */
1865     { tzGMT, "en@calendar=roc",    -2145744000000.0, -1988064000000.0,  FALSE,    4,  59,  1825,  43800,  2628000,  157680000 }, /* BMG 10-Jan-03 00:00 -> BMG  5-Jan-02 00:00 */
1866     { tzGMT, "en@calendar=roc",    -1988064000000.0, -2145744000000.0,  FALSE,   -4, -59, -1825, -43800, -2628000, -157680000 }, /* BMG  5-Jan-02 00:00 -> BMG 10-Jan-03 00:00 */
1867     { tzGMT, "en@calendar=roc",    -1988064000000.0, -1672704000000.0,  FALSE,    9, 119,  3650,  87600,  5256000,  315360000 }, /* BMG  5-Jan-02 00:00 -> MG   5-Dec-30 00:00 */
1868     { tzGMT, "en@calendar=roc",    -1672704000000.0, -1988064000000.0,  FALSE,   -9,-119, -3650, -87600, -5256000, -315360000 }, /* MG   5-Dec-30 00:00 -> BMG  5-Jan-02 00:00 */
1869     { tzGMT, "en@calendar=coptic",-53026531200000.0,-52868851200000.0,  FALSE,    4,  64,  1825,  43800,  2628000,  157680000 }, /* Er1  5-Nas-05 00:00 -> Er1 10-Nas-04 00:00 */
1870     { tzGMT, "en@calendar=coptic",-52868851200000.0,-53026531200000.0,  FALSE,   -4, -64, -1825, -43800, -2628000, -157680000 }, /* Er1 10-Nas-04 00:00 -> Er1  5-Nas-05 00:00 */
1871     { tzGMT, "en@calendar=coptic",-53499571200000.0,-53341891200000.0,  FALSE,    4,  64,  1825,  43800,  2628000,  157680000 }, /* Er0 10-Tou-04 00:00 -> Er0  5-Tou-02 00:00 */
1872     { tzGMT, "en@calendar=coptic",-53341891200000.0,-53499571200000.0,  FALSE,   -4, -64, -1825, -43800, -2628000, -157680000 }, /* Er0  5-Tou-02 00:00 -> Er0 10-Tou-04 00:00 */
1873     { tzGMT, "en@calendar=coptic",-53341891200000.0,-53026531200000.0,  FALSE,    9, 129,  3650,  87600,  5256000,  315360000 }, /* Er0  5-Tou-02 00:00 -> Er1  5-Nas-05 00:00 */
1874     { tzGMT, "en@calendar=coptic",-53026531200000.0,-53341891200000.0,  FALSE,   -9,-129, -3650, -87600, -5256000, -315360000 }, /* Er1  5-Nas-05 00:00 -> Er0  5-Tou-02 00:00 */
1875     { NULL,        NULL,           0.0,               0.0,              FALSE,    0,   0,     0,      0,        0,          0 }  /* terminator */
1876 };
1877 
TestFieldDifference()1878 void TestFieldDifference() {
1879     const TFDItem * tfdItemPtr;
1880     for (tfdItemPtr = tfdItems; tfdItemPtr->timezone != NULL; tfdItemPtr++) {
1881         UErrorCode status = U_ZERO_ERROR;
1882         UCalendar* ucal = ucal_open(tfdItemPtr->timezone, -1, tfdItemPtr->locale, UCAL_DEFAULT, &status);
1883         if (U_FAILURE(status)) {
1884             log_err("FAIL: for locale \"%s\", ucal_open had status %s\n", tfdItemPtr->locale, u_errorName(status) );
1885         } else {
1886             int32_t yDf, MDf, dDf, HDf, mDf, sDf;
1887             if (tfdItemPtr->progressive) {
1888                 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1889                 yDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_YEAR, &status);
1890                 MDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MONTH, &status);
1891                 dDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_DATE, &status);
1892                 HDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_HOUR, &status);
1893                 mDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MINUTE, &status);
1894                 sDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_SECOND, &status);
1895                 if (U_FAILURE(status)) {
1896                     log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference had status %s\n",
1897                             tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
1898                 } else if ( yDf !=  tfdItemPtr->yDiff ||
1899                             MDf !=  tfdItemPtr->MDiff ||
1900                             dDf !=  tfdItemPtr->dDiff ||
1901                             HDf !=  tfdItemPtr->HDiff ||
1902                             mDf !=  tfdItemPtr->mDiff ||
1903                             sDf !=  tfdItemPtr->sDiff ) {
1904                     log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected y-M-d-H-m-s progressive diffs %d-%d-%d-%d-%d-%d, got %d-%d-%d-%d-%d-%d\n",
1905                             tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target,
1906                             tfdItemPtr->yDiff, tfdItemPtr->MDiff, tfdItemPtr->dDiff, tfdItemPtr->HDiff, tfdItemPtr->mDiff, tfdItemPtr->sDiff,
1907                             yDf, MDf, dDf, HDf, mDf, sDf);
1908                 }
1909             } else {
1910                 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1911                 yDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_YEAR, &status);
1912                 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1913                 MDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MONTH, &status);
1914                 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1915                 dDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_DATE, &status);
1916                 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1917                 HDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_HOUR, &status);
1918                 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1919                 mDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_MINUTE, &status);
1920                 if (U_FAILURE(status)) {
1921                     log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference (y-M-d-H-m) had status %s\n",
1922                             tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
1923                 } else if ( yDf !=  tfdItemPtr->yDiff ||
1924                             MDf !=  tfdItemPtr->MDiff ||
1925                             dDf !=  tfdItemPtr->dDiff ||
1926                             HDf !=  tfdItemPtr->HDiff ||
1927                             mDf !=  tfdItemPtr->mDiff ) {
1928                     log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected y-M-d-H-m total diffs %d-%d-%d-%d-%d, got %d-%d-%d-%d-%d\n",
1929                             tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target,
1930                             tfdItemPtr->yDiff, tfdItemPtr->MDiff, tfdItemPtr->dDiff, tfdItemPtr->HDiff, tfdItemPtr->mDiff,
1931                             yDf, MDf, dDf, HDf, mDf);
1932                 }
1933                 ucal_setMillis(ucal, tfdItemPtr->start, &status);
1934                 sDf = ucal_getFieldDifference(ucal, tfdItemPtr->target, UCAL_SECOND, &status);
1935                 if (tfdItemPtr->sDiff != 0x7FFFFFFF) {
1936                     if (U_FAILURE(status)) {
1937                         log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, ucal_setMillis or ucal_getFieldDifference (seconds) had status %s\n",
1938                                 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, u_errorName(status) );
1939                     } else if (sDf !=  tfdItemPtr->sDiff) {
1940                         log_data_err("FAIL: for locale \"%s\", start %.1f, target %.1f, expected seconds progressive diff %d, got %d\n",
1941                                 tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target, tfdItemPtr->sDiff, sDf);
1942                     }
1943                 } else if (!U_FAILURE(status)) {
1944                     log_err("FAIL: for locale \"%s\", start %.1f, target %.1f, for ucal_getFieldDifference (seconds) expected overflow error, got none\n",
1945                             tfdItemPtr->locale, tfdItemPtr->start, tfdItemPtr->target );
1946                 }
1947             }
1948             ucal_close(ucal);
1949         }
1950     }
1951 }
1952 
TestAmbiguousWallTime()1953 void TestAmbiguousWallTime() {
1954     UErrorCode status = U_ZERO_ERROR;
1955     UChar tzID[32];
1956     UCalendar* ucal;
1957     UDate t, expected;
1958 
1959     u_uastrcpy(tzID, "America/New_York");
1960     ucal = ucal_open(tzID, -1, NULL, UCAL_DEFAULT, &status);
1961     if (U_FAILURE(status)) {
1962         log_err("FAIL: Failed to create a calendar");
1963         return;
1964     }
1965 
1966     if (ucal_getAttribute(ucal, UCAL_REPEATED_WALL_TIME) != UCAL_WALLTIME_LAST) {
1967         log_err("FAIL: Default UCAL_REPEATED_WALL_TIME value is not UCAL_WALLTIME_LAST");
1968     }
1969 
1970     if (ucal_getAttribute(ucal, UCAL_SKIPPED_WALL_TIME) != UCAL_WALLTIME_LAST) {
1971         log_err("FAIL: Default UCAL_SKIPPED_WALL_TIME value is not UCAL_WALLTIME_LAST");
1972     }
1973 
1974     /* UCAL_WALLTIME_FIRST on US fall transition */
1975     ucal_setAttribute(ucal, UCAL_REPEATED_WALL_TIME, UCAL_WALLTIME_FIRST);
1976     ucal_clear(ucal);
1977     ucal_setDateTime(ucal, 2011, 11-1, 6, 1, 30, 0, &status);
1978     t = ucal_getMillis(ucal, &status);
1979     expected = 1320557400000.0; /* 2011-11-06T05:30:00Z */
1980     if (U_FAILURE(status)) {
1981         log_err("FAIL: Calculating time 2011-11-06 01:30:00 with UCAL_WALLTIME_FIRST - %s\n", u_errorName(status));
1982         status = U_ZERO_ERROR;
1983     } else if (t != expected) {
1984         log_data_err("FAIL: 2011-11-06 01:30:00 with UCAL_WALLTIME_FIRST - got: %f, expected: %f\n", t, expected);
1985     }
1986 
1987     /* UCAL_WALLTIME_LAST on US fall transition */
1988     ucal_setAttribute(ucal, UCAL_REPEATED_WALL_TIME, UCAL_WALLTIME_LAST);
1989     ucal_clear(ucal);
1990     ucal_setDateTime(ucal, 2011, 11-1, 6, 1, 30, 0, &status);
1991     t = ucal_getMillis(ucal, &status);
1992     expected = 1320561000000.0; /* 2011-11-06T06:30:00Z */
1993     if (U_FAILURE(status)) {
1994         log_err("FAIL: Calculating time 2011-11-06 01:30:00 with UCAL_WALLTIME_LAST - %s\n", u_errorName(status));
1995         status = U_ZERO_ERROR;
1996     } else if (t != expected) {
1997         log_data_err("FAIL: 2011-11-06 01:30:00 with UCAL_WALLTIME_LAST - got: %f, expected: %f\n", t, expected);
1998     }
1999 
2000     /* UCAL_WALLTIME_FIRST on US spring transition */
2001     ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_FIRST);
2002     ucal_clear(ucal);
2003     ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2004     t = ucal_getMillis(ucal, &status);
2005     expected = 1299997800000.0; /* 2011-03-13T06:30:00Z */
2006     if (U_FAILURE(status)) {
2007         log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_FIRST - %s\n", u_errorName(status));
2008         status = U_ZERO_ERROR;
2009     } else if (t != expected) {
2010         log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_FIRST - got: %f, expected: %f\n", t, expected);
2011     }
2012 
2013     /* UCAL_WALLTIME_LAST on US spring transition */
2014     ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_LAST);
2015     ucal_clear(ucal);
2016     ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2017     t = ucal_getMillis(ucal, &status);
2018     expected = 1300001400000.0; /* 2011-03-13T07:30:00Z */
2019     if (U_FAILURE(status)) {
2020         log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_LAST - %s\n", u_errorName(status));
2021         status = U_ZERO_ERROR;
2022     } else if (t != expected) {
2023         log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_LAST - got: %f, expected: %f\n", t, expected);
2024     }
2025 
2026     /* UCAL_WALLTIME_NEXT_VALID on US spring transition */
2027     ucal_setAttribute(ucal, UCAL_SKIPPED_WALL_TIME, UCAL_WALLTIME_NEXT_VALID);
2028     ucal_clear(ucal);
2029     ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2030     t = ucal_getMillis(ucal, &status);
2031     expected = 1299999600000.0; /* 2011-03-13T07:00:00Z */
2032     if (U_FAILURE(status)) {
2033         log_err("FAIL: Calculating time 2011-03-13 02:30:00 with UCAL_WALLTIME_NEXT_VALID - %s\n", u_errorName(status));
2034         status = U_ZERO_ERROR;
2035     } else if (t != expected) {
2036         log_data_err("FAIL: 2011-03-13 02:30:00 with UCAL_WALLTIME_NEXT_VALID - got: %f, expected: %f\n", t, expected);
2037     }
2038 
2039     /* non-lenient on US spring transition */
2040     ucal_setAttribute(ucal, UCAL_LENIENT, 0);
2041     ucal_clear(ucal);
2042     ucal_setDateTime(ucal, 2011, 3-1, 13, 2, 30, 0, &status);
2043     t = ucal_getMillis(ucal, &status);
2044     if (U_SUCCESS(status)) {
2045         /* must return error */
2046         log_data_err("FAIL: Non-lenient did not fail with 2011-03-13 02:30:00\n");
2047         status = U_ZERO_ERROR;
2048     }
2049 
2050     ucal_close(ucal);
2051 }
2052 
2053 /**
2054  * TestAddRollEra0AndEraBounds, for #9226
2055  */
2056 
2057  typedef struct {
2058      const char * locale;
2059      UBool era0YearsGoBackwards; /* until we have API to get this, per #9393 */
2060  } EraTestItem;
2061 
2062 static const EraTestItem eraTestItems[] = {
2063     /* calendars with non-modern era 0 that goes backwards, max era == 1 */
2064     { "en@calendar=gregorian", TRUE },
2065     { "en@calendar=roc", TRUE },
2066     { "en@calendar=coptic", TRUE },
2067     /* calendars with non-modern era 0 that goes forwards, max era > 1 */
2068     { "en@calendar=japanese", FALSE },
2069     { "en@calendar=chinese", FALSE },
2070     /* calendars with non-modern era 0 that goes forwards, max era == 1 */
2071     { "en@calendar=ethiopic", FALSE },
2072     /* calendars with only one era  = 0, forwards */
2073     { "en@calendar=buddhist", FALSE },
2074     { "en@calendar=hebrew", FALSE },
2075     { "en@calendar=islamic", FALSE },
2076     { "en@calendar=indian", FALSE },
2077     { "en@calendar=persian", FALSE },
2078     { "en@calendar=ethiopic-amete-alem", FALSE },
2079     { NULL, FALSE }
2080 };
2081 
2082 static const UChar zoneGMT[] = { 0x47,0x4D,0x54,0 };
2083 
TestAddRollEra0AndEraBounds()2084 void TestAddRollEra0AndEraBounds() {
2085     const EraTestItem * eraTestItemPtr;
2086     for (eraTestItemPtr = eraTestItems; eraTestItemPtr->locale != NULL; eraTestItemPtr++) {
2087         UErrorCode status = U_ZERO_ERROR;
2088         UCalendar *ucalTest = ucal_open(zoneGMT, -1, eraTestItemPtr->locale, UCAL_DEFAULT, &status);
2089         if ( U_SUCCESS(status) ) {
2090             int32_t yrBefore, yrAfter, yrMax, eraAfter, eraMax, eraNow;
2091 
2092             status = U_ZERO_ERROR;
2093             ucal_clear(ucalTest);
2094             ucal_set(ucalTest, UCAL_YEAR, 2);
2095             ucal_set(ucalTest, UCAL_ERA, 0);
2096             yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2097             ucal_add(ucalTest, UCAL_YEAR, 1, &status);
2098             yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2099             if (U_FAILURE(status)) {
2100                 log_err("FAIL: set era 0 year 2 then add 1 year and get year for %s, error %s\n",
2101                         eraTestItemPtr->locale, u_errorName(status));
2102             } else if ( (eraTestItemPtr->era0YearsGoBackwards && yrAfter>yrBefore) ||
2103                         (!eraTestItemPtr->era0YearsGoBackwards && yrAfter<yrBefore) ) {
2104                 log_err("FAIL: era 0 add 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2105             }
2106 
2107             status = U_ZERO_ERROR;
2108             ucal_clear(ucalTest);
2109             ucal_set(ucalTest, UCAL_YEAR, 2);
2110             ucal_set(ucalTest, UCAL_ERA, 0);
2111             yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2112             ucal_roll(ucalTest, UCAL_YEAR, 1, &status);
2113             yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2114             if (U_FAILURE(status)) {
2115                 log_err("FAIL: set era 0 year 2 then roll 1 year and get year for %s, error %s\n",
2116                         eraTestItemPtr->locale, u_errorName(status));
2117             } else if ( (eraTestItemPtr->era0YearsGoBackwards && yrAfter>yrBefore) ||
2118                         (!eraTestItemPtr->era0YearsGoBackwards && yrAfter<yrBefore) ) {
2119                 log_err("FAIL: era 0 roll 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2120             }
2121 
2122             status = U_ZERO_ERROR;
2123             ucal_clear(ucalTest);
2124             ucal_set(ucalTest, UCAL_YEAR, 1);
2125             ucal_set(ucalTest, UCAL_ERA, 0);
2126             if (eraTestItemPtr->era0YearsGoBackwards) {
2127                 ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* roll forward in time to era 0 boundary */
2128                 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2129                 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2130                 if (U_FAILURE(status)) {
2131                     log_err("FAIL: set era 0 year 1 then roll 1 year and get year,era for %s, error %s\n",
2132                             eraTestItemPtr->locale, u_errorName(status));
2133                 /* all calendars with era0YearsGoBackwards have "unbounded" era0 year values, so we should pin at yr 1 */
2134                 } else if (eraAfter != 0 || yrAfter != 1) {
2135                     log_err("FAIL: era 0 roll 1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2136                             eraTestItemPtr->locale, eraAfter, yrAfter);
2137                 }
2138             } else {
2139                 /* roll backward in time to where era 0 years go negative, except for the Chinese
2140                    calendar, which uses negative eras instead of having years outside the range 1-60 */
2141                 const char * calType = ucal_getType(ucalTest, &status);
2142                 ucal_roll(ucalTest, UCAL_YEAR, -2, &status);
2143                 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2144                 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2145                 if (U_FAILURE(status)) {
2146                     log_err("FAIL: set era 0 year 1 then roll -2 years and get year,era for %s, error %s\n",
2147                             eraTestItemPtr->locale, u_errorName(status));
2148                 } else if ( uprv_strcmp(calType,"chinese")!=0 && (eraAfter != 0 || yrAfter != -1) ) {
2149                     log_err("FAIL: era 0 roll -2 years from year 1 does not stay within era or produce year -1 for %s (get era %d year %d)\n",
2150                             eraTestItemPtr->locale, eraAfter, yrAfter);
2151                 }
2152             }
2153 
2154             status = U_ZERO_ERROR;
2155             ucal_clear(ucalTest);
2156             {
2157                 int32_t eraMin = ucal_getLimit(ucalTest, UCAL_ERA, UCAL_MINIMUM, &status);
2158                 const char * calType = ucal_getType(ucalTest, &status);
2159                 if (eraMin != 0 && uprv_strcmp(calType, "chinese") != 0) {
2160                     log_err("FAIL: ucal_getLimit returns minimum era %d (should be 0) for calType %s, error %s\n", eraMin, calType, u_errorName(status));
2161                 }
2162             }
2163 
2164             status = U_ZERO_ERROR;
2165             ucal_clear(ucalTest);
2166             ucal_set(ucalTest, UCAL_YEAR, 1);
2167             ucal_set(ucalTest, UCAL_ERA, 0);
2168             eraMax = ucal_getLimit(ucalTest, UCAL_ERA, UCAL_MAXIMUM, &status);
2169             if ( U_SUCCESS(status) && eraMax > 0 ) {
2170                 /* try similar tests for era 1 (if calendar has it), in which years always go forward */
2171                 status = U_ZERO_ERROR;
2172                 ucal_clear(ucalTest);
2173                 ucal_set(ucalTest, UCAL_YEAR, 2);
2174                 ucal_set(ucalTest, UCAL_ERA, 1);
2175                 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2176                 ucal_add(ucalTest, UCAL_YEAR, 1, &status);
2177                 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2178                 if (U_FAILURE(status)) {
2179                     log_err("FAIL: set era 1 year 2 then add 1 year and get year for %s, error %s\n",
2180                             eraTestItemPtr->locale, u_errorName(status));
2181                 } else if ( yrAfter<yrBefore ) {
2182                     log_err("FAIL: era 1 add 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2183                 }
2184 
2185                 status = U_ZERO_ERROR;
2186                 ucal_clear(ucalTest);
2187                 ucal_set(ucalTest, UCAL_YEAR, 2);
2188                 ucal_set(ucalTest, UCAL_ERA, 1);
2189                 yrBefore = ucal_get(ucalTest, UCAL_YEAR, &status);
2190                 ucal_roll(ucalTest, UCAL_YEAR, 1, &status);
2191                 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2192                 if (U_FAILURE(status)) {
2193                     log_err("FAIL: set era 1 year 2 then roll 1 year and get year for %s, error %s\n",
2194                             eraTestItemPtr->locale, u_errorName(status));
2195                 } else if ( yrAfter<yrBefore ) {
2196                     log_err("FAIL: era 1 roll 1 year does not move forward in time for %s\n", eraTestItemPtr->locale);
2197                 }
2198 
2199                 status = U_ZERO_ERROR;
2200                 ucal_clear(ucalTest);
2201                 ucal_set(ucalTest, UCAL_YEAR, 1);
2202                 ucal_set(ucalTest, UCAL_ERA, 1);
2203                 yrMax = ucal_getLimit(ucalTest, UCAL_YEAR, UCAL_ACTUAL_MAXIMUM, &status); /* max year value for era 1 */
2204                 ucal_roll(ucalTest, UCAL_YEAR, -1, &status); /* roll down which should pin or wrap to end */
2205                 yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2206                 eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2207                 if (U_FAILURE(status)) {
2208                     log_err("FAIL: set era 1 year 1 then roll -1 year and get year,era for %s, error %s\n",
2209                             eraTestItemPtr->locale, u_errorName(status));
2210                 /* if yrMax is reasonable we should wrap to that, else we should pin at yr 1 */
2211                 } else if (yrMax >= 32768) {
2212                     if (eraAfter != 1 || yrAfter != 1) {
2213                         log_err("FAIL: era 1 roll -1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2214                                 eraTestItemPtr->locale, eraAfter, yrAfter);
2215                     }
2216                 } else if (eraAfter != 1 || yrAfter != yrMax) {
2217                     log_err("FAIL: era 1 roll -1 year from year 1 does not stay within era or wrap to year %d for %s (get era %d year %d)\n",
2218                             yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2219                 } else {
2220                     /* now roll up which should wrap to beginning */
2221                     ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* now roll up which should wrap to beginning */
2222                     yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2223                     eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2224                     if (U_FAILURE(status)) {
2225                         log_err("FAIL: era 1 roll 1 year from end and get year,era for %s, error %s\n",
2226                                 eraTestItemPtr->locale, u_errorName(status));
2227                     } else if (eraAfter != 1 || yrAfter != 1) {
2228                         log_err("FAIL: era 1 roll 1 year from year %d does not stay within era or wrap to year 1 for %s (get era %d year %d)\n",
2229                                 yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2230                     }
2231                 }
2232 
2233                 /* if current era  > 1, try the same roll tests for current era */
2234                 ucal_setMillis(ucalTest, ucal_getNow(), &status);
2235                 eraNow = ucal_get(ucalTest, UCAL_ERA, &status);
2236                 if ( U_SUCCESS(status) && eraNow > 1 ) {
2237                     status = U_ZERO_ERROR;
2238                     ucal_clear(ucalTest);
2239                     ucal_set(ucalTest, UCAL_YEAR, 1);
2240                     ucal_set(ucalTest, UCAL_ERA, eraNow);
2241                     yrMax = ucal_getLimit(ucalTest, UCAL_YEAR, UCAL_ACTUAL_MAXIMUM, &status); /* max year value for this era */
2242                     ucal_roll(ucalTest, UCAL_YEAR, -1, &status);
2243                     yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2244                     eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2245                     if (U_FAILURE(status)) {
2246                         log_err("FAIL: set era %d year 1 then roll -1 year and get year,era for %s, error %s\n",
2247                                 eraNow, eraTestItemPtr->locale, u_errorName(status));
2248                     /* if yrMax is reasonable we should wrap to that, else we should pin at yr 1 */
2249                     } else if (yrMax >= 32768) {
2250                         if (eraAfter != eraNow || yrAfter != 1) {
2251                             log_err("FAIL: era %d roll -1 year from year 1 does not stay within era or pin to year 1 for %s (get era %d year %d)\n",
2252                                     eraNow, eraTestItemPtr->locale, eraAfter, yrAfter);
2253                         }
2254                     } else if (eraAfter != eraNow || yrAfter != yrMax) {
2255                         log_err("FAIL: era %d roll -1 year from year 1 does not stay within era or wrap to year %d for %s (get era %d year %d)\n",
2256                                 eraNow, yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2257                     } else {
2258                         /* now roll up which should wrap to beginning */
2259                         ucal_roll(ucalTest, UCAL_YEAR, 1, &status); /* now roll up which should wrap to beginning */
2260                         yrAfter = ucal_get(ucalTest, UCAL_YEAR, &status);
2261                         eraAfter = ucal_get(ucalTest, UCAL_ERA, &status);
2262                         if (U_FAILURE(status)) {
2263                             log_err("FAIL: era %d roll 1 year from end and get year,era for %s, error %s\n",
2264                                     eraNow, eraTestItemPtr->locale, u_errorName(status));
2265                         } else if (eraAfter != eraNow || yrAfter != 1) {
2266                             log_err("FAIL: era %d roll 1 year from year %d does not stay within era or wrap to year 1 for %s (get era %d year %d)\n",
2267                                     eraNow, yrMax, eraTestItemPtr->locale, eraAfter, yrAfter);
2268                         }
2269                     }
2270                 }
2271             }
2272 
2273             ucal_close(ucalTest);
2274         } else {
2275             log_data_err("FAIL: ucal_open fails for zone GMT, locale %s, UCAL_DEFAULT\n", eraTestItemPtr->locale);
2276         }
2277     }
2278 }
2279 
2280 /**
2281  * TestGetTZTransition, for #9606
2282  */
2283 
2284 typedef struct {
2285     const char *descrip;    /* test description */
2286     const UChar * zoneName; /* pointer to zero-terminated zone name */
2287     int32_t year;           /* starting point for test is gregorian calendar noon on day specified by y,M,d here */
2288     int32_t month;
2289     int32_t day;
2290     UBool hasPrev;          /* does it have a previous transition from starting point? If so we test inclusive from that */
2291     UBool hasNext;          /* does it have a next transition from starting point? If so we test inclusive from that */
2292 } TZTransitionItem;
2293 
2294 /* have zoneGMT above */
2295 static const UChar zoneUSPacific[] = { 0x55,0x53,0x2F,0x50,0x61,0x63,0x69,0x66,0x69,0x63,0 }; /* "US/Pacific" */
2296 static const UChar zoneCairo[]     = { 0x41,0x66,0x72,0x69,0x63,0x61,0x2F,0x43,0x61,0x69,0x72,0x6F,0 }; /* "Africa/Cairo", DST cancelled since 2011 */
2297 static const UChar zoneIceland[]   = { 0x41,0x74,0x6C,0x61,0x6E,0x74,0x69,0x63,0x2F,0x52,0x65,0x79,0x6B,0x6A,0x61,0x76,0x69,0x6B,0 }; /* "Atlantic/Reykjavik", always on DST (since when?) */
2298 
2299 static const TZTransitionItem tzTransitionItems[] = {
2300     { "USPacific mid 2012", zoneUSPacific, 2012, UCAL_JULY, 1, TRUE , TRUE  },
2301     { "USPacific mid  100", zoneUSPacific,  100, UCAL_JULY, 1, FALSE, TRUE  }, /* no transitions before 100 CE... */
2302     // { "Cairo     mid 2012", zoneCairo,     2012, UCAL_JULY, 1, TRUE , FALSE }, /* DST cancelled since 2011 */  // android-changed: disable for tzdata 2014c change to Egypt.
2303     { "Iceland   mid 2012", zoneIceland,   2012, UCAL_JULY, 1, TRUE , FALSE }, /* always on DST */
2304     { NULL,                 NULL,             0,         0, 0, FALSE, FALSE } /* terminator */
2305 };
2306 
TestGetTZTransition()2307 void TestGetTZTransition() {
2308     UErrorCode status = U_ZERO_ERROR;
2309     UCalendar * ucal = ucal_open(zoneGMT, -1, "en", UCAL_GREGORIAN, &status);
2310     if ( U_SUCCESS(status) ) {
2311         const TZTransitionItem * itemPtr;
2312         for (itemPtr = tzTransitionItems; itemPtr->descrip != NULL; itemPtr++) {
2313             UDate curMillis;
2314             ucal_setTimeZone(ucal, itemPtr->zoneName, -1, &status);
2315             ucal_setDateTime(ucal, itemPtr->year, itemPtr->month, itemPtr->day, 12, 0, 0, &status);
2316             curMillis = ucal_getMillis(ucal, &status);
2317             (void)curMillis;    /* Suppress set but not used warning. */
2318             if ( U_SUCCESS(status) ) {
2319                 UDate transition1, transition2;
2320                 UBool result;
2321 
2322                 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_PREVIOUS, &transition1, &status);
2323                 if (U_FAILURE(status) || result != itemPtr->hasPrev) {
2324                     log_data_err("FAIL: %s ucal_getTimeZoneTransitionDate prev status %s, expected result %d but got %d\n",
2325                             itemPtr->descrip, u_errorName(status), itemPtr->hasPrev, result);
2326                 } else if (result) {
2327                     ucal_setMillis(ucal, transition1, &status);
2328                     result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_PREVIOUS_INCLUSIVE, &transition2, &status);
2329                     if (U_FAILURE(status) || !result || transition2 != transition1) {
2330                         log_err("FAIL: %s ucal_getTimeZoneTransitionDate prev_inc status %s, result %d, expected date %.1f but got %.1f\n",
2331                                 itemPtr->descrip, u_errorName(status), result, transition1, transition2);
2332                     }
2333                 }
2334                 status = U_ZERO_ERROR;
2335 
2336                 result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_NEXT, &transition1, &status);
2337                 if (U_FAILURE(status) || result != itemPtr->hasNext) {
2338                     log_data_err("FAIL: %s ucal_getTimeZoneTransitionDate next status %s, expected result %d but got %d\n",
2339                             itemPtr->descrip, u_errorName(status), itemPtr->hasNext, result);
2340                 } else if (result) {
2341                     ucal_setMillis(ucal, transition1, &status);
2342                     result = ucal_getTimeZoneTransitionDate(ucal, UCAL_TZ_TRANSITION_NEXT_INCLUSIVE, &transition2, &status);
2343                     if (U_FAILURE(status) || !result || transition2 != transition1) {
2344                         log_err("FAIL: %s ucal_getTimeZoneTransitionDate next_inc status %s, result %d, expected date %.1f but got %.1f\n",
2345                                 itemPtr->descrip, u_errorName(status), result, transition1, transition2);
2346                     }
2347                 }
2348                 status = U_ZERO_ERROR;
2349             } else {
2350                 log_data_err("FAIL setup: can't setup calendar for %s, status %s\n",
2351                             itemPtr->descrip, u_errorName(status));
2352                 status = U_ZERO_ERROR;
2353             }
2354         }
2355         ucal_close(ucal);
2356     } else {
2357         log_data_err("FAIL setup: ucal_open status %s\n", u_errorName(status));
2358     }
2359 }
2360 
2361 static const UChar winEastern[] = /* Eastern Standard Time */
2362     {0x45,0x61,0x73,0x74,0x65,0x72,0x6E,0x20,0x53,0x74,0x61,0x6E,0x64,0x61,0x72,0x64,0x20,0x54,0x69,0x6D,0x65,0x00};
2363 
2364 static const UChar tzNewYork[] = /* America/New_York */
2365     {0x41,0x6D,0x65,0x72,0x69,0x63,0x61,0x2F,0x4E,0x65,0x77,0x5F,0x59,0x6F,0x72,0x6B,0x00};
2366 static const UChar tzTronto[] = /* America/Toronto */
2367     {0x41,0x6D,0x65,0x72,0x69,0x63,0x61,0x2F,0x54,0x6F,0x72,0x6F,0x6E,0x74,0x6F,0x00};
2368 
2369 static const UChar sBogus[] = /* Bogus */
2370     {0x42,0x6F,0x67,0x75,0x73,0x00};
2371 
TestGetWindowsTimeZoneID()2372 void TestGetWindowsTimeZoneID() {
2373     UErrorCode status;
2374     UChar winID[64];
2375     int32_t len;
2376 
2377     {
2378         status = U_ZERO_ERROR;
2379         len = ucal_getWindowsTimeZoneID(tzNewYork, u_strlen(tzNewYork), winID, sizeof(winID)/sizeof(winID[0]), &status);
2380         if (U_FAILURE(status)) {
2381             log_data_err("FAIL: Windows ID for America/New_York, status %s\n", u_errorName(status));
2382         } else if (len != u_strlen(winEastern) || u_strncmp(winID, winEastern, len) != 0) {
2383             log_data_err("FAIL: Windows ID for America/New_York\n");
2384         }
2385     }
2386     {
2387         status = U_ZERO_ERROR;
2388         len = ucal_getWindowsTimeZoneID(tzTronto, u_strlen(tzTronto), winID, sizeof(winID)/sizeof(winID[0]), &status);
2389         if (U_FAILURE(status)) {
2390             log_data_err("FAIL: Windows ID for America/Toronto, status %s\n", u_errorName(status));
2391         } else if (len != u_strlen(winEastern) || u_strncmp(winID, winEastern, len) != 0) {
2392             log_data_err("FAIL: Windows ID for America/Toronto\n");
2393         }
2394     }
2395     {
2396         status = U_ZERO_ERROR;
2397         len = ucal_getWindowsTimeZoneID(sBogus, u_strlen(sBogus), winID, sizeof(winID)/sizeof(winID[0]), &status);
2398         if (U_FAILURE(status)) {
2399             log_data_err("FAIL: Windows ID for Bogus, status %s\n", u_errorName(status));
2400         } else if (len != 0) {
2401             log_data_err("FAIL: Windows ID for Bogus\n");
2402         }
2403     }
2404 }
2405 
TestGetTimeZoneIDByWindowsID()2406 void TestGetTimeZoneIDByWindowsID() {
2407     UErrorCode status;
2408     UChar tzID[64];
2409     int32_t len;
2410 
2411     {
2412         status = U_ZERO_ERROR;
2413         len = ucal_getTimeZoneIDForWindowsID(winEastern, -1, NULL, tzID, sizeof(tzID)/sizeof(tzID[0]), &status);
2414         if (U_FAILURE(status)) {
2415             log_data_err("FAIL: TZ ID for Eastern Standard Time, status %s\n", u_errorName(status));
2416         } else if (len != u_strlen(tzNewYork) || u_strncmp(tzID, tzNewYork, len) != 0) {
2417             log_err("FAIL: TZ ID for Eastern Standard Time\n");
2418         }
2419     }
2420     {
2421         status = U_ZERO_ERROR;
2422         len = ucal_getTimeZoneIDForWindowsID(winEastern, u_strlen(winEastern), "US", tzID, sizeof(tzID)/sizeof(tzID[0]), &status);
2423         if (U_FAILURE(status)) {
2424             log_data_err("FAIL: TZ ID for Eastern Standard Time - US, status %s\n", u_errorName(status));
2425         } else if (len != u_strlen(tzNewYork) || u_strncmp(tzID, tzNewYork, len) != 0) {
2426             log_err("FAIL: TZ ID for Eastern Standard Time - US\n");
2427         }
2428     }
2429     {
2430         status = U_ZERO_ERROR;
2431         len = ucal_getTimeZoneIDForWindowsID(winEastern, u_strlen(winEastern), "CA", tzID, sizeof(tzID)/sizeof(tzID[0]), &status);
2432         if (U_FAILURE(status)) {
2433             log_data_err("FAIL: TZ ID for Eastern Standard Time - CA, status %s\n", u_errorName(status));
2434         } else if (len != u_strlen(tzTronto) || u_strncmp(tzID, tzTronto, len) != 0) {
2435             log_err("FAIL: TZ ID for Eastern Standard Time - CA\n");
2436         }
2437     }
2438 
2439     {
2440         status = U_ZERO_ERROR;
2441         len = ucal_getTimeZoneIDForWindowsID(sBogus, -1, NULL, tzID, sizeof(tzID)/sizeof(tzID[0]), &status);
2442         if (U_FAILURE(status)) {
2443             log_data_err("FAIL: TZ ID for Bogus, status %s\n", u_errorName(status));
2444         } else if (len != 0) {
2445             log_err("FAIL: TZ ID for Bogus\n");
2446         }
2447     }
2448 }
2449 
2450 
2451 #endif /* #if !UCONFIG_NO_FORMATTING */
2452