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