ccaltst.c revision c69afcec261fc345fda8daf46f0ea6b4351dc777
1/********************************************************************
2 * Copyright (c) 1997-2008, International Business Machines
3 * Corporation and others. All Rights Reserved.
4 ********************************************************************
5 *
6 * File CCALTST.C
7 *
8 * Modification History:
9 *        Name                     Description
10 *     Madhu Katragadda               Creation
11 ********************************************************************
12 */
13
14/* C API AND FUNCTIONALITY TEST FOR CALENDAR (ucol.h)*/
15
16#include "unicode/utypes.h"
17
18#if !UCONFIG_NO_FORMATTING
19
20#include <stdlib.h>
21
22#include "unicode/uloc.h"
23#include "unicode/ucal.h"
24#include "unicode/udat.h"
25#include "unicode/ustring.h"
26#include "cintltst.h"
27#include "ccaltst.h"
28#include "cformtst.h"
29#include "cstring.h"
30
31void TestGregorianChange(void);
32
33void addCalTest(TestNode** root);
34
35void addCalTest(TestNode** root)
36{
37
38    addTest(root, &TestCalendar, "tsformat/ccaltst/TestCalendar");
39    addTest(root, &TestGetSetDateAPI, "tsformat/ccaltst/TestGetSetDateAPI");
40    addTest(root, &TestFieldGetSet, "tsformat/ccaltst/TestFieldGetSet");
41    addTest(root, &TestAddRollExtensive, "tsformat/ccaltst/TestAddRollExtensive");
42    addTest(root, &TestGetLimits, "tsformat/ccaltst/TestGetLimits");
43    addTest(root, &TestDOWProgression, "tsformat/ccaltst/TestDOWProgression");
44    addTest(root, &TestGMTvsLocal, "tsformat/ccaltst/TestGMTvsLocal");
45    addTest(root, &TestGregorianChange, "tsformat/ccaltst/TestGregorianChange");
46}
47
48/* "GMT" */
49static const UChar fgGMTID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
50
51/* "PST" */
52static const UChar PST[] = {0x50, 0x53, 0x54, 0x00}; /* "PST" */
53
54static const UChar EUROPE_PARIS[] = {0x45, 0x75, 0x72, 0x6F, 0x70, 0x65, 0x2F, 0x50, 0x61, 0x72, 0x69, 0x73, 0x00}; /* "Europe/Paris" */
55
56static const UChar AMERICA_LOS_ANGELES[] = {0x41, 0x6D, 0x65, 0x72, 0x69, 0x63, 0x61, 0x2F,
57    0x4C, 0x6F, 0x73, 0x5F, 0x41, 0x6E, 0x67, 0x65, 0x6C, 0x65, 0x73, 0x00}; /* America/Los_Angeles */
58
59static void TestCalendar()
60{
61    UCalendar *caldef = 0, *caldef2 = 0, *calfr = 0, *calit = 0, *calfrclone = 0;
62    UEnumeration* uenum = NULL;
63    int32_t count, count2, i,j;
64    UChar tzID[4];
65    UChar *tzdname = 0;
66    UErrorCode status = U_ZERO_ERROR;
67    UDate now;
68    UDateFormat *datdef = 0;
69    UChar *result = 0;
70    int32_t resultlength, resultlengthneeded;
71    char tempMsgBuf[256];
72    UChar zone1[32], zone2[32];
73    const char *tzver = 0;
74    UChar canonicalID[64];
75    UBool isSystemID = FALSE;
76
77#ifdef U_USE_UCAL_OBSOLETE_2_8
78    /*Testing countAvailableTimeZones*/
79    int32_t offset=0;
80    log_verbose("\nTesting ucal_countAvailableTZIDs\n");
81    count=ucal_countAvailableTZIDs(offset);
82    log_verbose("The number of timezone id's present with offset 0 are %d:\n", count);
83    if(count < 5) /* Don't hard code an exact == test here! */
84        log_err("FAIL: error in the ucal_countAvailableTZIDs - got %d expected at least 5 total\n", count);
85
86    /*Testing getAvailableTZIDs*/
87    log_verbose("\nTesting ucal_getAvailableTZIDs");
88    for(i=0;i<count;i++){
89        ucal_getAvailableTZIDs(offset, i, &status);
90        if(U_FAILURE(status)){
91            log_err("FAIL: ucal_getAvailableTZIDs returned %s\n", u_errorName(status));
92        }
93        log_verbose("%s\n", u_austrcpy(tempMsgBuf, ucal_getAvailableTZIDs(offset, i, &status)));
94    }
95    /*get Illegal TZID where index >= count*/
96    ucal_getAvailableTZIDs(offset, i, &status);
97    if(status != U_INDEX_OUTOFBOUNDS_ERROR){
98        log_err("FAIL:for TZID index >= count Expected INDEX_OUTOFBOUNDS_ERROR Got %s\n", u_errorName(status));
99    }
100    status=U_ZERO_ERROR;
101#endif
102
103    /*Test ucal_openTimeZones & ucal_openCountryTimeZones*/
104    for (j=0; j<2; ++j) {
105        const char* api = (j==0) ? "ucal_openTimeZones()" :
106            "ucal_openCountryTimeZones(US)";
107        uenum = (j==0) ? ucal_openTimeZones(&status) :
108            ucal_openCountryTimeZones("US", &status);
109        if (U_FAILURE(status)) {
110            log_err("FAIL: %s failed with %s", api,
111                    u_errorName(status));
112        } else {
113            const char* id;
114            int32_t len;
115            count = uenum_count(uenum, &status);
116            log_verbose("%s returned %d timezone id's:\n", api, count);
117            if (count < 5) { /* Don't hard code an exact == test here! */
118                log_err("FAIL: in %s, got %d, expected at least 5\n", api, count);
119            }
120            uenum_reset(uenum, &status);
121            if (U_FAILURE(status)){
122                log_err("FAIL: uenum_reset for %s returned %s\n",
123                        api, u_errorName(status));
124            }
125            for (i=0; i<count; i++) {
126                id = uenum_next(uenum, &len, &status);
127                if (U_FAILURE(status)){
128                    log_err("FAIL: uenum_next for %s returned %s\n",
129                            api, u_errorName(status));
130                } else {
131                    log_verbose("%s\n", id);
132                }
133            }
134            /* Next one should be NULL */
135            id = uenum_next(uenum, &len, &status);
136            if (id != NULL) {
137                log_err("FAIL: uenum_next for %s returned %s, expected NULL\n",
138                        api, id);
139            }
140        }
141        uenum_close(uenum);
142    }
143
144    /*Test ucal_getDSTSavings*/
145    status = U_ZERO_ERROR;
146    i = ucal_getDSTSavings(fgGMTID, &status);
147    if (U_FAILURE(status)) {
148        log_err("FAIL: ucal_getDSTSavings(GMT) => %s\n",
149                u_errorName(status));
150    } else if (i != 0) {
151        log_err("FAIL: ucal_getDSTSavings(GMT) => %d, expect 0\n", i);
152    }
153    i = ucal_getDSTSavings(PST, &status);
154    if (U_FAILURE(status)) {
155        log_err("FAIL: ucal_getDSTSavings(PST) => %s\n",
156                u_errorName(status));
157    } else if (i != 1*60*60*1000) {
158        log_err("FAIL: ucal_getDSTSavings(PST) => %d, expect %d\n", i, 1*60*60*1000);
159    }
160
161    /*Test ucal_set/getDefaultTimeZone*/
162    status = U_ZERO_ERROR;
163    i = ucal_getDefaultTimeZone(zone1, sizeof(zone1)/sizeof(zone1[0]), &status);
164    if (U_FAILURE(status)) {
165        log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
166                u_errorName(status));
167    } else {
168        ucal_setDefaultTimeZone(EUROPE_PARIS, &status);
169        if (U_FAILURE(status)) {
170            log_err("FAIL: ucal_setDefaultTimeZone(Europe/Paris) => %s\n",
171                    u_errorName(status));
172        } else {
173            i = ucal_getDefaultTimeZone(zone2, sizeof(zone2)/sizeof(zone2[0]), &status);
174            if (U_FAILURE(status)) {
175                log_err("FAIL: ucal_getDefaultTimeZone() => %s\n",
176                        u_errorName(status));
177            } else {
178                if (u_strcmp(zone2, EUROPE_PARIS) != 0) {
179                    log_err("FAIL: ucal_getDefaultTimeZone() did not return Europe/Paris\n");
180                }
181            }
182        }
183        status = U_ZERO_ERROR;
184        ucal_setDefaultTimeZone(zone1, &status);
185    }
186
187    /*Test ucal_getTZDataVersion*/
188    status = U_ZERO_ERROR;
189    tzver = ucal_getTZDataVersion(&status);
190    if (U_FAILURE(status)) {
191        log_err("FAIL: ucal_getTZDataVersion() => %s\n", u_errorName(status));
192    } else if (uprv_strlen(tzver) != 5 /*4 digits + 1 letter*/) {
193        log_err("FAIL: Bad version string was returned by ucal_getTZDataVersion\n");
194    } else {
195        log_verbose("PASS: ucal_getTZDataVersion returned %s\n", tzver);
196    }
197
198    /*Testing ucal_getCanonicalTimeZoneID*/
199    status = U_ZERO_ERROR;
200    resultlength = ucal_getCanonicalTimeZoneID(PST, -1,
201        canonicalID, sizeof(canonicalID)/sizeof(UChar), &isSystemID, &status);
202    if (U_FAILURE(status)) {
203        log_err("FAIL: error in ucal_getCanonicalTimeZoneID : %s\n", u_errorName(status));
204    } else {
205        if (u_strcmp(AMERICA_LOS_ANGELES, canonicalID) != 0) {
206            log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) returned %s : expected - %s\n",
207                PST, canonicalID, AMERICA_LOS_ANGELES);
208        }
209        if (!isSystemID) {
210            log_err("FAIL: ucal_getCanonicalTimeZoneID(%s) set %d to isSystemID\n",
211                PST, isSystemID);
212        }
213    }
214
215    /*Testing the  ucal_open() function*/
216    status = U_ZERO_ERROR;
217    log_verbose("\nTesting the ucal_open()\n");
218    u_uastrcpy(tzID, "PST");
219    caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
220    if(U_FAILURE(status)){
221        log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status));
222    }
223
224    caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
225    if(U_FAILURE(status)){
226        log_err("FAIL: error in ucal_open caldef : %s\n", u_errorName(status));
227    }
228    u_strcpy(tzID, fgGMTID);
229    calfr=ucal_open(tzID, u_strlen(tzID), "fr_FR", UCAL_TRADITIONAL, &status);
230    if(U_FAILURE(status)){
231        log_err("FAIL: error in ucal_open calfr : %s\n", u_errorName(status));
232    }
233    calit=ucal_open(tzID, u_strlen(tzID), "it_IT", UCAL_TRADITIONAL, &status);
234    if(U_FAILURE(status))    {
235        log_err("FAIL: error in ucal_open calit : %s\n", u_errorName(status));
236    }
237
238    /*Testing the  clone() function*/
239    calfrclone = ucal_clone(calfr, &status);
240    if(U_FAILURE(status)){
241        log_err("FAIL: error in ucal_clone calfr : %s\n", u_errorName(status));
242    }
243
244    /*Testing udat_getAvailable() and udat_countAvailable()*/
245    log_verbose("\nTesting getAvailableLocales and countAvailable()\n");
246    count=ucal_countAvailable();
247    /* use something sensible w/o hardcoding the count */
248    if(count > 0) {
249        log_verbose("PASS: ucal_countAvailable() works fine\n");
250        log_verbose("The no: of locales for which calendars are avilable are %d\n", count);
251    } else {
252        log_data_err("FAIL: Error in countAvailable()\n");
253    }
254
255    for(i=0;i<count;i++) {
256       log_verbose("%s\n", ucal_getAvailable(i));
257    }
258
259
260    /*Testing the equality between calendar's*/
261    log_verbose("\nTesting ucal_equivalentTo()\n");
262    if(caldef && caldef2 && calfr && calit) {
263      if(ucal_equivalentTo(caldef, caldef2) == FALSE || ucal_equivalentTo(caldef, calfr)== TRUE ||
264        ucal_equivalentTo(caldef, calit)== TRUE || ucal_equivalentTo(calfr, calfrclone) == FALSE) {
265          log_err("FAIL: Error. equivalentTo test failed\n");
266      } else {
267          log_verbose("PASS: equivalentTo test passed\n");
268      }
269    }
270
271
272    /*Testing the current time and date using ucal_getnow()*/
273    log_verbose("\nTesting the ucal_getNow function to check if it is fetching tbe current time\n");
274    now=ucal_getNow();
275    /* open the date format and format the date to check the output */
276    datdef=udat_open(UDAT_FULL,UDAT_FULL ,NULL, NULL, 0,NULL,0,&status);
277    if(U_FAILURE(status)){
278        log_err("FAIL: error in creating the dateformat : %s\n", u_errorName(status));
279        return;
280    }
281    log_verbose("PASS: The current date and time fetched is %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datdef, now)) );
282
283
284    /*Testing the TimeZoneDisplayName */
285    log_verbose("\nTesting the fetching of time zone display name\n");
286    /*for the US locale */
287    resultlength=0;
288    resultlengthneeded=ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", NULL, resultlength, &status);
289
290    if(status==U_BUFFER_OVERFLOW_ERROR)
291    {
292        status=U_ZERO_ERROR;
293        resultlength=resultlengthneeded+1;
294        result=(UChar*)malloc(sizeof(UChar) * resultlength);
295        ucal_getTimeZoneDisplayName(caldef, UCAL_DST, "en_US", result, resultlength, &status);
296    }
297    if(U_FAILURE(status))    {
298        log_err("FAIL: Error in getting the timezone display name : %s\n", u_errorName(status));
299    }
300    else{
301        log_verbose("PASS: getting the time zone display name successful : %s, %d needed \n",
302            u_errorName(status), resultlengthneeded);
303    }
304
305
306#define expectPDT "Pacific Daylight Time"
307
308    tzdname=(UChar*)malloc(sizeof(UChar) * (sizeof(expectPDT)+1));
309    u_uastrcpy(tzdname, expectPDT);
310    if(u_strcmp(tzdname, result)==0){
311        log_verbose("PASS: got the correct time zone display name %s\n", u_austrcpy(tempMsgBuf, result) );
312    }
313    else{
314        log_err("FAIL: got the wrong time zone(DST) display name %s, wanted %s\n", austrdup(result) , expectPDT);
315    }
316
317    ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_DST, "en_US", result, resultlength, &status);
318    u_uastrcpy(tzdname, "PDT");
319    if(u_strcmp(tzdname, result) != 0){
320        log_err("FAIL: got the wrong time zone(SHORT_DST) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
321    }
322
323    ucal_getTimeZoneDisplayName(caldef, UCAL_STANDARD, "en_US", result, resultlength, &status);
324    u_uastrcpy(tzdname, "Pacific Standard Time");
325    if(u_strcmp(tzdname, result) != 0){
326        log_err("FAIL: got the wrong time zone(STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
327    }
328
329    ucal_getTimeZoneDisplayName(caldef, UCAL_SHORT_STANDARD, "en_US", result, resultlength, &status);
330    u_uastrcpy(tzdname, "PST");
331    if(u_strcmp(tzdname, result) != 0){
332        log_err("FAIL: got the wrong time zone(SHORT_STANDARD) display name %s, wanted %s\n", austrdup(result), austrdup(tzdname));
333    }
334
335
336    /*testing the setAttributes and getAttributes of a UCalendar*/
337    log_verbose("\nTesting the getAttributes and set Attributes\n");
338    count=ucal_getAttribute(calit, UCAL_LENIENT);
339    count2=ucal_getAttribute(calfr, UCAL_LENIENT);
340    ucal_setAttribute(calit, UCAL_LENIENT, 0);
341    ucal_setAttribute(caldef, UCAL_LENIENT, count2);
342    if( ucal_getAttribute(calit, UCAL_LENIENT) !=0 ||
343        ucal_getAttribute(calfr, UCAL_LENIENT)!=ucal_getAttribute(caldef, UCAL_LENIENT) )
344        log_err("FAIL: there is an error in getAttributes or setAttributes\n");
345    else
346        log_verbose("PASS: attribute set and got successfully\n");
347        /*set it back to orginal value */
348    log_verbose("Setting it back to normal\n");
349    ucal_setAttribute(calit, UCAL_LENIENT, count);
350    if(ucal_getAttribute(calit, UCAL_LENIENT)!=count)
351        log_err("FAIL: Error in setting the attribute back to normal\n");
352
353    /*setting the first day of the week to other values  */
354    count=ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK);
355    for (i=1; i<=7; ++i) {
356        ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,i);
357        if (ucal_getAttribute(calit, UCAL_FIRST_DAY_OF_WEEK) != i)
358            log_err("FAIL: set/getFirstDayOfWeek failed\n");
359    }
360    /*get bogus Attribute*/
361    count=ucal_getAttribute(calit, (UCalendarAttribute)99); /* BOGUS_ATTRIBUTE */
362    if(count != -1){
363        log_err("FAIL: get/bogus attribute should return -1\n");
364    }
365
366    /*set it back to normal */
367    ucal_setAttribute(calit, UCAL_FIRST_DAY_OF_WEEK,count);
368    /*setting minimal days of the week to other values */
369    count=ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK);
370    for (i=1; i<=7; ++i) {
371        ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,i);
372        if (ucal_getAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK) != i)
373            log_err("FAIL: set/getMinimalDaysInFirstWeek failed\n");
374    }
375    /*set it back to normal */
376    ucal_setAttribute(calit, UCAL_MINIMAL_DAYS_IN_FIRST_WEEK,count);
377
378
379    /*testing if the UCalendar's timezone is currently in day light saving's time*/
380    log_verbose("\nTesting if the UCalendar is currently in daylight saving's time\n");
381    ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 10, 45, 20, &status);
382    ucal_inDaylightTime(caldef, &status );
383    if(U_FAILURE(status))    {
384        log_err("Error in ucal_inDaylightTime: %s\n", u_errorName(status));
385    }
386    if(!ucal_inDaylightTime(caldef, &status))
387        log_verbose("PASS: It is  not in daylight saving's time\n");
388    else
389        log_err("FAIL: It is not in daylight saving's time\n");
390
391
392
393    /*closing the UCalendar*/
394    ucal_close(caldef);
395    ucal_close(caldef2);
396    ucal_close(calfr);
397    ucal_close(calit);
398    ucal_close(calfrclone);
399    /*closing the UDateFormat used */
400    udat_close(datdef);
401    free(result);
402    free(tzdname);
403}
404
405/*------------------------------------------------------*/
406/*Testing the getMillis, setMillis, setDate and setDateTime functions extensively*/
407
408static void TestGetSetDateAPI()
409{
410    UCalendar *caldef = 0, *caldef2 = 0;
411    UChar tzID[4];
412    UDate d1;
413    int32_t hour;
414    int32_t zoneOffset;
415    UDateFormat *datdef = 0;
416    UErrorCode status=U_ZERO_ERROR;
417    UDate d2= 837039928046.0;
418    UChar temp[30];
419
420    log_verbose("\nOpening the calendars()\n");
421    u_strcpy(tzID, fgGMTID);
422    /*open the calendars used */
423    caldef=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
424    caldef2=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
425    /*open the dateformat */
426    /* this is supposed to open default date format, but later on it treats it like it is "en_US"
427       - very bad if you try to run the tests on machine where default locale is NOT "en_US" */
428    /*datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,NULL,fgGMTID,-1, &status);*/
429    datdef=udat_open(UDAT_DEFAULT,UDAT_DEFAULT ,"en_US",fgGMTID,-1,NULL,0, &status);
430    if(U_FAILURE(status))
431    {
432        log_err("error in creating the dateformat : %s\n", u_errorName(status));
433        return;
434    }
435
436
437    /*Testing getMillis and setMillis */
438    log_verbose("\nTesting the date and time fetched in millis for a calendar using getMillis\n");
439    d1=ucal_getMillis(caldef, &status);
440    if(U_FAILURE(status)){
441        log_err("Error in getMillis : %s\n", u_errorName(status));
442    }
443
444    /*testing setMillis */
445    log_verbose("\nTesting the set date and time function using setMillis\n");
446    ucal_setMillis(caldef, d2, &status);
447    if(U_FAILURE(status)){
448        log_err("Error in setMillis : %s\n", u_errorName(status));
449    }
450
451    /*testing if the calendar date is set properly or not  */
452    d1=ucal_getMillis(caldef, &status);
453    if(u_strcmp(myDateFormat(datdef, d1), myDateFormat(datdef, d2))!=0)
454        log_err("error in setMillis or getMillis\n");
455    /*-------------------*/
456
457
458
459    ctest_setTimeZone(NULL, &status);
460
461    /*testing ucal_setTimeZone() function*/
462    log_verbose("\nTesting if the function ucal_setTimeZone() works fine\n");
463    ucal_setMillis(caldef2, d2, &status);
464    if(U_FAILURE(status)){
465        log_err("Error in getMillis : %s\n", u_errorName(status));;
466    }
467    hour=ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status);
468
469    u_uastrcpy(tzID, "PST");
470    ucal_setTimeZone(caldef2,tzID, 3, &status);
471    if(U_FAILURE(status)){
472        log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
473    }
474    else
475        log_verbose("ucal_setTimeZone worked fine\n");
476    if(hour == ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status))
477        log_err("FAIL: Error setting the time zone doesn't change the represented time\n");
478    else if((hour-8 + 1) != ucal_get(caldef2, UCAL_HOUR_OF_DAY, &status)) /*because it is not in daylight savings time */
479        log_err("FAIL: Error setTimeZone doesn't change the represented time correctly with 8 hour offset\n");
480    else
481        log_verbose("PASS: setTimeZone works fine\n");
482
483    /*testing setTimeZone roundtrip */
484    log_verbose("\nTesting setTimeZone() roundtrip\n");
485    u_strcpy(tzID, fgGMTID);
486    ucal_setTimeZone(caldef2, tzID, 3, &status);
487    if(U_FAILURE(status)){
488        log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
489    }
490    if(d2==ucal_getMillis(caldef2, &status))
491        log_verbose("PASS: setTimeZone roundtrip test passed\n");
492    else
493        log_err("FAIL: setTimeZone roundtrip test failed\n");
494
495    zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
496    if(U_FAILURE(status)){
497        log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
498    }
499    else if (zoneOffset != 0) {
500        log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
501    }
502
503    ucal_setTimeZone(caldef2, NULL, -1, &status);
504    if(U_FAILURE(status)){
505        log_err("Error in setting the time zone using ucal_setTimeZone(): %s\n", u_errorName(status));
506    }
507    if(ucal_getMillis(caldef2, &status))
508        log_verbose("PASS: setTimeZone roundtrip test passed\n");
509    else
510        log_err("FAIL: setTimeZone roundtrip test failed\n");
511
512    zoneOffset = ucal_get(caldef2, UCAL_ZONE_OFFSET, &status);
513    if(U_FAILURE(status)){
514        log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone(): %s\n", u_errorName(status));
515    }
516    else if (zoneOffset != -28800000) {
517        log_err("Error in getting the time zone using ucal_get() after using ucal_setTimeZone() offset=%d\n", zoneOffset);
518    }
519
520    ctest_resetTimeZone();
521
522/*----------------------------*     */
523
524
525
526    /*Testing  if setDate works fine  */
527    log_verbose("\nTesting the ucal_setDate() function \n");
528    u_uastrcpy(temp, "Dec 17, 1971 11:05:28 PM");
529    ucal_setDate(caldef,1971, UCAL_DECEMBER, 17, &status);
530    if(U_FAILURE(status)){
531        log_err("error in setting the calendar date : %s\n", u_errorName(status));
532    }
533    /*checking if the calendar date is set properly or not  */
534    d1=ucal_getMillis(caldef, &status);
535    if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
536        log_verbose("PASS:setDate works fine\n");
537    else
538        log_err("FAIL:Error in setDate()\n");
539
540
541    /* Testing setDate Extensively with various input values */
542    log_verbose("\nTesting ucal_setDate() extensively\n");
543    ucal_setDate(caldef, 1999, UCAL_JANUARY, 10, &status);
544    verify1("1999  10th day of January  is :", caldef, datdef, 1999, UCAL_JANUARY, 10);
545    ucal_setDate(caldef, 1999, UCAL_DECEMBER, 3, &status);
546    verify1("1999 3rd day of December  is :", caldef, datdef, 1999, UCAL_DECEMBER, 3);
547    ucal_setDate(caldef, 2000, UCAL_MAY, 3, &status);
548    verify1("2000 3rd day of May is :", caldef, datdef, 2000, UCAL_MAY, 3);
549    ucal_setDate(caldef, 1999, UCAL_AUGUST, 32, &status);
550    verify1("1999 32th day of August is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1);
551    ucal_setDate(caldef, 1999, UCAL_MARCH, 0, &status);
552    verify1("1999 0th day of March is :", caldef, datdef, 1999, UCAL_FEBRUARY, 28);
553    ucal_setDate(caldef, 0, UCAL_MARCH, 12, &status);
554
555    /*--------------------*/
556
557    /*Testing if setDateTime works fine */
558    log_verbose("\nTesting the ucal_setDateTime() function \n");
559    u_uastrcpy(temp, "May 3, 1972 4:30:42 PM");
560    ucal_setDateTime(caldef,1972, UCAL_MAY, 3, 16, 30, 42, &status);
561    if(U_FAILURE(status)){
562        log_err("error in setting the calendar date : %s\n", u_errorName(status));
563    }
564    /*checking  if the calendar date is set properly or not  */
565    d1=ucal_getMillis(caldef, &status);
566    if(u_strcmp(myDateFormat(datdef, d1), temp)==0)
567        log_verbose("PASS: setDateTime works fine\n");
568    else
569        log_err("FAIL: Error in setDateTime\n");
570
571
572
573    /*Testing setDateTime extensively with various input values*/
574    log_verbose("\nTesting ucal_setDateTime() function extensively\n");
575    ucal_setDateTime(caldef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, &status);
576    verify2("1999  10th day of October  at 6:45:30 is :", caldef, datdef, 1999, UCAL_OCTOBER, 10, 6, 45, 30, 0 );
577    ucal_setDateTime(caldef, 1999, UCAL_MARCH, 3, 15, 10, 55, &status);
578    verify2("1999 3rd day of March   at 15:10:55 is :", caldef, datdef, 1999, UCAL_MARCH, 3, 3, 10, 55, 1);
579    ucal_setDateTime(caldef, 1999, UCAL_MAY, 3, 25, 30, 45, &status);
580    verify2("1999 3rd day of May at 25:30:45 is :", caldef, datdef, 1999, UCAL_MAY, 4, 1, 30, 45, 0);
581    ucal_setDateTime(caldef, 1999, UCAL_AUGUST, 32, 22, 65, 40, &status);
582    verify2("1999 32th day of August at 22:65:40 is :", caldef, datdef, 1999, UCAL_SEPTEMBER, 1, 11, 5, 40,1);
583    ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, 0, 0, 0,&status);
584    verify2("1999 12th day of March at 0:0:0 is :", caldef, datdef, 1999, UCAL_MARCH, 12, 0, 0, 0, 0);
585    ucal_setDateTime(caldef, 1999, UCAL_MARCH, 12, -10, -10,0, &status);
586    verify2("1999 12th day of March is at -10:-10:0 :", caldef, datdef, 1999, UCAL_MARCH, 11, 1, 50, 0, 1);
587
588
589
590    /*close caldef and datdef*/
591    ucal_close(caldef);
592    ucal_close(caldef2);
593    udat_close(datdef);
594}
595
596/*----------------------------------------------------------- */
597/**
598 * Confirm the functioning of the calendar field related functions.
599 */
600static void TestFieldGetSet()
601{
602    UCalendar *cal = 0;
603    UChar tzID[4];
604    UDateFormat *datdef = 0;
605    UDate d1;
606    UErrorCode status=U_ZERO_ERROR;
607    log_verbose("\nFetching pointer to UCalendar using the ucal_open()\n");
608    u_strcpy(tzID, fgGMTID);
609    /*open the calendar used */
610    cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
611    if (U_FAILURE(status)) {
612        log_err("ucal_open failed: %s\n", u_errorName(status));
613        return;
614    }
615    datdef=udat_open(UDAT_SHORT,UDAT_SHORT ,NULL,fgGMTID,-1,NULL, 0, &status);
616    if(U_FAILURE(status))
617    {
618        log_err("error in creating the dateformat : %s\n", u_errorName(status));
619    }
620
621    /*Testing ucal_get()*/
622    log_verbose("\nTesting the ucal_get() function of Calendar\n");
623    ucal_setDateTime(cal, 1999, UCAL_MARCH, 12, 5, 25, 30, &status);
624    if(U_FAILURE(status)){
625        log_err("error in the setDateTime() : %s\n", u_errorName(status));
626    }
627    if(ucal_get(cal, UCAL_YEAR, &status)!=1999 || ucal_get(cal, UCAL_MONTH, &status)!=2 ||
628        ucal_get(cal, UCAL_DATE, &status)!=12 || ucal_get(cal, UCAL_HOUR, &status)!=5)
629        log_err("error in ucal_get()\n");
630    else if(ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status)!=2 || ucal_get(cal, UCAL_DAY_OF_WEEK, &status)!=6
631        || ucal_get(cal, UCAL_WEEK_OF_MONTH, &status)!=2 || ucal_get(cal, UCAL_WEEK_OF_YEAR, &status)!= 11)
632        log_err("FAIL: error in ucal_get()\n");
633    else
634        log_verbose("PASS: ucal_get() works fine\n");
635
636    /*Testing the ucal_set() , ucal_clear() functions of calendar*/
637    log_verbose("\nTesting the set, and clear  field functions of calendar\n");
638    ucal_setAttribute(cal, UCAL_LENIENT, 0);
639    ucal_clear(cal);
640    ucal_set(cal, UCAL_YEAR, 1997);
641    ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
642    ucal_set(cal, UCAL_DATE, 3);
643    verify1("1997 third day of June = ", cal, datdef, 1997, UCAL_JUNE, 3);
644    ucal_clear(cal);
645    ucal_set(cal, UCAL_YEAR, 1997);
646    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
647    ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
648    ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 1);
649    verify1("1997 first Tuesday in June = ", cal, datdef, 1997, UCAL_JUNE, 3);
650    ucal_clear(cal);
651    ucal_set(cal, UCAL_YEAR, 1997);
652    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
653    ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
654    ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, - 1);
655    verify1("1997 last Tuesday in June = ", cal, datdef,1997,   UCAL_JUNE, 24);
656    /*give undesirable input    */
657    status = U_ZERO_ERROR;
658    ucal_clear(cal);
659    ucal_set(cal, UCAL_YEAR, 1997);
660    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
661    ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
662    ucal_set(cal, UCAL_DAY_OF_WEEK_IN_MONTH, 0);
663    d1 = ucal_getMillis(cal, &status);
664    if (status != U_ILLEGAL_ARGUMENT_ERROR) {
665        log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 zero-th Tuesday in June\n");
666    } else {
667        log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
668    }
669    status = U_ZERO_ERROR;
670    ucal_clear(cal);
671    ucal_set(cal, UCAL_YEAR, 1997);
672    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
673    ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
674    ucal_set(cal, UCAL_WEEK_OF_MONTH, 1);
675    verify1("1997 Tuesday in week 1 of June = ", cal,datdef, 1997, UCAL_JUNE, 3);
676    ucal_clear(cal);
677    ucal_set(cal, UCAL_YEAR, 1997);
678    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
679    ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
680    ucal_set(cal, UCAL_WEEK_OF_MONTH, 5);
681    verify1("1997 Tuesday in week 5 of June = ", cal,datdef, 1997, UCAL_JULY, 1);
682    status = U_ZERO_ERROR;
683    ucal_clear(cal);
684    ucal_set(cal, UCAL_YEAR, 1997);
685    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
686    ucal_set(cal, UCAL_MONTH, UCAL_JUNE);
687    ucal_set(cal, UCAL_WEEK_OF_MONTH, 0);
688    d1 = ucal_getMillis(cal,&status);
689    if (status != U_ILLEGAL_ARGUMENT_ERROR){
690        log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1997 Tuesday zero-th week in June\n");
691    } else {
692        log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
693    }
694    status = U_ZERO_ERROR;
695    ucal_clear(cal);
696    ucal_set(cal, UCAL_YEAR_WOY, 1997);
697    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
698    ucal_set(cal, UCAL_WEEK_OF_YEAR, 1);
699    verify1("1997 Tuesday in week 1 of year = ", cal, datdef,1996, UCAL_DECEMBER, 31);
700    ucal_clear(cal);
701    ucal_set(cal, UCAL_YEAR, 1997);
702    ucal_set(cal, UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
703    ucal_set(cal, UCAL_WEEK_OF_YEAR, 10);
704    verify1("1997 Tuesday in week 10 of year = ", cal,datdef, 1997, UCAL_MARCH, 4);
705    ucal_clear(cal);
706    ucal_set(cal, UCAL_YEAR, 1999);
707    ucal_set(cal, UCAL_DAY_OF_YEAR, 1);
708    verify1("1999 1st day of the year =", cal, datdef, 1999, UCAL_JANUARY, 1);
709    ucal_set(cal, UCAL_MONTH, -3);
710    d1 = ucal_getMillis(cal,&status);
711    if (status != U_ILLEGAL_ARGUMENT_ERROR){
712        log_err("FAIL: U_ILLEGAL_ARGUMENT_ERROR was not returned for : 1999 -3th month\n");
713    } else {
714        log_verbose("PASS: U_ILLEGAL_ARGUMENT_ERROR as expected\n");
715    }
716
717    ucal_setAttribute(cal, UCAL_LENIENT, 1);
718
719    ucal_set(cal, UCAL_MONTH, -3);
720    verify1("1999 -3th month should be", cal, datdef, 1998, UCAL_OCTOBER, 1);
721
722
723    /*testing isSet and clearField()*/
724    if(!ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
725        log_err("FAIL: error in isSet\n");
726    else
727        log_verbose("PASS: isSet working fine\n");
728    ucal_clearField(cal, UCAL_WEEK_OF_YEAR);
729    if(ucal_isSet(cal, UCAL_WEEK_OF_YEAR))
730        log_err("FAIL: there is an error in clearField or isSet\n");
731    else
732        log_verbose("PASS :clearField working fine\n");
733
734    /*-------------------------------*/
735
736    ucal_close(cal);
737    udat_close(datdef);
738}
739
740
741
742/* ------------------------------------- */
743/**
744 * Execute adding and rolling in Calendar extensively,
745 */
746static void TestAddRollExtensive()
747{
748    UCalendar *cal = 0;
749    int32_t i,limit;
750    UChar tzID[4];
751    UCalendarDateFields e;
752    int32_t y,m,d,hr,min,sec,ms;
753    int32_t maxlimit = 40;
754    UErrorCode status = U_ZERO_ERROR;
755    y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
756
757    log_verbose("Testing add and roll extensively\n");
758
759    u_uastrcpy(tzID, "PST");
760    /*open the calendar used */
761    cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
762    if (U_FAILURE(status)) {
763        log_err("ucal_open() failed : %s\n", u_errorName(status));
764        return;
765    }
766
767    ucal_set(cal, UCAL_YEAR, y);
768    ucal_set(cal, UCAL_MONTH, m);
769    ucal_set(cal, UCAL_DATE, d);
770
771    /* Confirm that adding to various fields works.*/
772    log_verbose("\nTesting to confirm that adding to various fields works with ucal_add()\n");
773    checkDate(cal, y, m, d);
774    ucal_add(cal,UCAL_YEAR, 1, &status);
775    if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status)); return; }
776    y++;
777    checkDate(cal, y, m, d);
778    ucal_add(cal,UCAL_MONTH, 12, &status);
779    if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
780    y+=1;
781    checkDate(cal, y, m, d);
782    ucal_add(cal,UCAL_DATE, 1, &status);
783    if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
784    d++;
785    checkDate(cal, y, m, d);
786    ucal_add(cal,UCAL_DATE, 2, &status);
787    if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
788    d += 2;
789    checkDate(cal, y, m, d);
790    ucal_add(cal,UCAL_DATE, 28, &status);
791    if (U_FAILURE(status)) { log_err("ucal_add failed: %s\n", u_errorName(status) ); return; }
792    ++m;
793    checkDate(cal, y, m, d);
794    ucal_add(cal, (UCalendarDateFields)-1, 10, &status);
795    if(status==U_ILLEGAL_ARGUMENT_ERROR)
796        log_verbose("Pass: Illegal argument error as expected\n");
797    else{
798        log_err("Fail: No, illegal argument error as expected. Got....: %s\n", u_errorName(status));
799    }
800    status=U_ZERO_ERROR;
801
802
803    /*confirm that applying roll to various fields works fine*/
804    log_verbose("\nTesting to confirm that ucal_roll() works\n");
805    ucal_roll(cal, UCAL_DATE, -1, &status);
806    if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
807    d -=1;
808    checkDate(cal, y, m, d);
809    ucal_roll(cal, UCAL_MONTH, -2, &status);
810    if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
811    m -=2;
812    checkDate(cal, y, m, d);
813    ucal_roll(cal, UCAL_DATE, 1, &status);
814    if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
815    d +=1;
816    checkDate(cal, y, m, d);
817    ucal_roll(cal, UCAL_MONTH, -12, &status);
818    if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
819    checkDate(cal, y, m, d);
820    ucal_roll(cal, UCAL_YEAR, -1, &status);
821    if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
822    y -=1;
823    checkDate(cal, y, m, d);
824    ucal_roll(cal, UCAL_DATE, 29, &status);
825    if (U_FAILURE(status)) { log_err("ucal_roll failed: %s\n", u_errorName(status) ); return; }
826    d = 2;
827    checkDate(cal, y, m, d);
828    ucal_roll(cal, (UCalendarDateFields)-1, 10, &status);
829    if(status==U_ILLEGAL_ARGUMENT_ERROR)
830        log_verbose("Pass: illegal arguement error as expected\n");
831    else{
832        log_err("Fail: no illegal argument error got..: %s\n", u_errorName(status));
833        return;
834    }
835    status=U_ZERO_ERROR;
836    ucal_clear(cal);
837    ucal_setDateTime(cal, 1999, UCAL_FEBRUARY, 28, 10, 30, 45,  &status);
838    if(U_FAILURE(status)){
839        log_err("error is setting the datetime: %s\n", u_errorName(status));
840    }
841    ucal_add(cal, UCAL_MONTH, 1, &status);
842    checkDate(cal, 1999, UCAL_MARCH, 28);
843    ucal_add(cal, UCAL_MILLISECOND, 1000, &status);
844    checkDateTime(cal, 1999, UCAL_MARCH, 28, 10, 30, 46, 0, UCAL_MILLISECOND);
845
846    ucal_close(cal);
847/*--------------- */
848    status=U_ZERO_ERROR;
849    /* Testing add and roll extensively */
850    log_verbose("\nTesting the ucal_add() and ucal_roll() functions extensively\n");
851    y = 1997; m = UCAL_FEBRUARY; d = 1; hr = 1; min = 1; sec = 0; ms = 0;
852    cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);
853    if (U_FAILURE(status)) {
854        log_err("ucal_open failed: %s\n", u_errorName(status));
855        return;
856    }
857    ucal_set(cal, UCAL_YEAR, y);
858    ucal_set(cal, UCAL_MONTH, m);
859    ucal_set(cal, UCAL_DATE, d);
860    ucal_set(cal, UCAL_HOUR, hr);
861    ucal_set(cal, UCAL_MINUTE, min);
862    ucal_set(cal, UCAL_SECOND,sec);
863    ucal_set(cal, UCAL_MILLISECOND, ms);
864    status=U_ZERO_ERROR;
865
866    log_verbose("\nTesting UCalendar add...\n");
867    for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
868        limit = maxlimit;
869        status = U_ZERO_ERROR;
870        for (i = 0; i < limit; i++) {
871            ucal_add(cal, e, 1, &status);
872            if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
873        }
874        for (i = 0; i < limit; i++) {
875            ucal_add(cal, e, -1, &status);
876            if (U_FAILURE(status)) {
877                log_err("ucal_add -1 failed: %s\n", u_errorName(status));
878                return;
879            }
880        }
881        checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
882    }
883    log_verbose("\nTesting calendar ucal_roll()...\n");
884    for(e = UCAL_YEAR;e < UCAL_FIELD_COUNT; e=(UCalendarDateFields)((int32_t)e + 1)) {
885        limit = maxlimit;
886        status = U_ZERO_ERROR;
887        for (i = 0; i < limit; i++) {
888            ucal_roll(cal, e, 1, &status);
889            if (U_FAILURE(status)) {
890                limit = i;
891                status = U_ZERO_ERROR;
892            }
893        }
894        for (i = 0; i < limit; i++) {
895            ucal_roll(cal, e, -1, &status);
896            if (U_FAILURE(status)) {
897                log_err("ucal_roll -1 failed: %s\n", u_errorName(status));
898                return;
899            }
900        }
901        checkDateTime(cal, y, m, d, hr, min, sec, ms, e);
902    }
903
904    ucal_close(cal);
905}
906
907/*------------------------------------------------------ */
908/*Testing the Limits for various Fields of Calendar*/
909static void TestGetLimits()
910{
911    UCalendar *cal = 0;
912    int32_t min, max, gr_min, le_max, ac_min, ac_max, val;
913    UChar tzID[4];
914    UErrorCode status = U_ZERO_ERROR;
915
916
917    u_uastrcpy(tzID, "PST");
918    /*open the calendar used */
919    cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_GREGORIAN, &status);;
920    if (U_FAILURE(status)) {
921        log_err("ucal_open() for gregorian calendar failed in TestGetLimits: %s\n", u_errorName(status));
922        return;
923    }
924
925    log_verbose("\nTesting the getLimits function for various fields\n");
926
927
928
929    ucal_setDate(cal, 1999, UCAL_MARCH, 5, &status); /* Set the date to be March 5, 1999 */
930    val = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
931    min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MINIMUM, &status);
932    max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK, UCAL_MAXIMUM, &status);
933    if ( (min != UCAL_SUNDAY || max != UCAL_SATURDAY ) && (min > val && val > max)  && (val != UCAL_FRIDAY)){
934           log_err("FAIL: Min/max bad\n");
935           log_err("FAIL: Day of week %d out of range\n", val);
936           log_err("FAIL: FAIL: Day of week should be SUNDAY Got %d\n", val);
937    }
938    else
939        log_verbose("getLimits successful\n");
940
941    val = ucal_get(cal, UCAL_DAY_OF_WEEK_IN_MONTH, &status);
942    min = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MINIMUM, &status);
943    max = ucal_getLimit(cal, UCAL_DAY_OF_WEEK_IN_MONTH, UCAL_MAXIMUM, &status);
944    if ( (min != 0 || max != 5 ) && (min > val && val > max)  && (val != 1)){
945           log_err("FAIL: Min/max bad\n");
946           log_err("FAIL: Day of week in month %d out of range\n", val);
947           log_err("FAIL: FAIL: Day of week in month should be SUNDAY Got %d\n", val);
948
949    }
950    else
951        log_verbose("getLimits successful\n");
952
953    min=ucal_getLimit(cal, UCAL_MONTH, UCAL_MINIMUM, &status);
954    max=ucal_getLimit(cal, UCAL_MONTH, UCAL_MAXIMUM, &status);
955    gr_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_GREATEST_MINIMUM, &status);
956    le_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_LEAST_MAXIMUM, &status);
957    ac_min=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MINIMUM, &status);
958    ac_max=ucal_getLimit(cal, UCAL_MONTH, UCAL_ACTUAL_MAXIMUM, &status);
959    if(U_FAILURE(status)){
960        log_err("Error in getLimits: %s\n", u_errorName(status));
961    }
962    if(min!=0 || max!=11 || gr_min!=0 || le_max!=11 || ac_min!=0 || ac_max!=11)
963        log_err("There is and error in getLimits in fetching the values\n");
964    else
965        log_verbose("getLimits successful\n");
966
967    ucal_setDateTime(cal, 1999, UCAL_MARCH, 5, 4, 10, 35, &status);
968    val=ucal_get(cal, UCAL_HOUR_OF_DAY, &status);
969    min=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MINIMUM, &status);
970    max=ucal_getLimit(cal, UCAL_HOUR_OF_DAY, UCAL_MAXIMUM, &status);
971    gr_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_GREATEST_MINIMUM, &status);
972    le_max=ucal_getLimit(cal, UCAL_MINUTE, UCAL_LEAST_MAXIMUM, &status);
973    ac_min=ucal_getLimit(cal, UCAL_MINUTE, UCAL_ACTUAL_MINIMUM, &status);
974    ac_max=ucal_getLimit(cal, UCAL_SECOND, UCAL_ACTUAL_MAXIMUM, &status);
975    if( (min!=0 || max!= 11 || gr_min!=0 || le_max!=60 || ac_min!=0 || ac_max!=60) &&
976        (min>val && val>max) && val!=4){
977
978        log_err("FAIL: Min/max bad\n");
979        log_err("FAIL: Hour of Day %d out of range\n", val);
980        log_err("FAIL: HOUR_OF_DAY should be 4 Got %d\n", val);
981    }
982    else
983        log_verbose("getLimits successful\n");
984
985
986    /*get BOGUS_LIMIT type*/
987    val=ucal_getLimit(cal, UCAL_SECOND, (UCalendarLimitType)99, &status);
988    if(val != -1){
989        log_err("FAIL: ucal_getLimit() with BOGUS type should return -1\n");
990    }
991    status=U_ZERO_ERROR;
992
993
994    ucal_close(cal);
995}
996
997
998
999/* ------------------------------------- */
1000
1001/**
1002 * Test that the days of the week progress properly when add is called repeatedly
1003 * for increments of 24 days.
1004 */
1005static void TestDOWProgression()
1006{
1007    int32_t initialDOW, DOW, newDOW, expectedDOW;
1008    UCalendar *cal = 0;
1009    UDateFormat *datfor = 0;
1010    UDate date1;
1011    int32_t delta=24;
1012    UErrorCode status = U_ZERO_ERROR;
1013    UChar tzID[4];
1014    char tempMsgBuf[256];
1015    u_strcpy(tzID, fgGMTID);
1016    /*open the calendar used */
1017    cal=ucal_open(tzID, u_strlen(tzID), "en_US", UCAL_TRADITIONAL, &status);;
1018    if (U_FAILURE(status)) {
1019        log_err("ucal_open failed: %s\n", u_errorName(status));
1020        return;
1021    }
1022
1023    datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1024    if(U_FAILURE(status)){
1025        log_err("error in creating the dateformat : %s\n", u_errorName(status));
1026    }
1027
1028
1029    ucal_setDate(cal, 1999, UCAL_JANUARY, 1, &status);
1030
1031    log_verbose("\nTesting the DOW progression\n");
1032
1033    initialDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1034    if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status) ); return; }
1035    newDOW = initialDOW;
1036    do {
1037        DOW = newDOW;
1038        log_verbose("DOW = %d...\n", DOW);
1039        date1=ucal_getMillis(cal, &status);
1040        if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1041        log_verbose("%s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)));
1042
1043        ucal_add(cal,UCAL_DAY_OF_WEEK, delta, &status);
1044        if (U_FAILURE(status)) { log_err("ucal_add() failed: %s\n", u_errorName(status)); return; }
1045
1046        newDOW = ucal_get(cal, UCAL_DAY_OF_WEEK, &status);
1047        if (U_FAILURE(status)) { log_err("ucal_get() failed: %s\n", u_errorName(status)); return; }
1048        expectedDOW = 1 + (DOW + delta - 1) % 7;
1049        date1=ucal_getMillis(cal, &status);
1050        if(U_FAILURE(status)){ log_err("ucal_getMiilis() failed: %s\n", u_errorName(status)); return;}
1051        if (newDOW != expectedDOW) {
1052            log_err("Day of week should be %d instead of %d on %s", expectedDOW, newDOW,
1053                u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1054            return;
1055        }
1056    }
1057    while (newDOW != initialDOW);
1058
1059    ucal_close(cal);
1060    udat_close(datfor);
1061}
1062
1063/* ------------------------------------- */
1064
1065/**
1066 * Confirm that the offset between local time and GMT behaves as expected.
1067 */
1068static void TestGMTvsLocal()
1069{
1070    log_verbose("\nTesting the offset between the GMT and local time\n");
1071    testZones(1999, 1, 1, 12, 0, 0);
1072    testZones(1999, 4, 16, 18, 30, 0);
1073    testZones(1998, 12, 17, 19, 0, 0);
1074}
1075
1076/* ------------------------------------- */
1077
1078static void testZones(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
1079{
1080    int32_t offset,utc, expected;
1081    UCalendar *gmtcal = 0, *cal = 0;
1082    UDate date1;
1083    double temp;
1084    UDateFormat *datfor = 0;
1085    UErrorCode status = U_ZERO_ERROR;
1086    UChar tzID[4];
1087    char tempMsgBuf[256];
1088
1089    u_strcpy(tzID, fgGMTID);
1090    gmtcal=ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);;
1091    if (U_FAILURE(status)) {
1092        log_err("ucal_open failed: %s\n", u_errorName(status));
1093        return;
1094    }
1095    u_uastrcpy(tzID, "PST");
1096    cal = ucal_open(tzID, 3, "en_US", UCAL_TRADITIONAL, &status);
1097    if (U_FAILURE(status)) {
1098        log_err("ucal_open failed: %s\n", u_errorName(status));
1099        return;
1100    }
1101
1102    datfor=udat_open(UDAT_MEDIUM,UDAT_MEDIUM ,NULL, fgGMTID,-1,NULL, 0, &status);
1103    if(U_FAILURE(status)){
1104        log_err("error in creating the dateformat : %s\n", u_errorName(status));
1105    }
1106
1107    ucal_setDateTime(gmtcal, yr, mo - 1, dt, hr, mn, sc, &status);
1108    if (U_FAILURE(status)) {
1109        log_err("ucal_setDateTime failed: %s\n", u_errorName(status));
1110        return;
1111    }
1112    ucal_set(gmtcal, UCAL_MILLISECOND, 0);
1113    date1 = ucal_getMillis(gmtcal, &status);
1114    if (U_FAILURE(status)) {
1115        log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1116        return;
1117    }
1118    log_verbose("date = %s\n", u_austrcpy(tempMsgBuf, myDateFormat(datfor, date1)) );
1119
1120
1121    ucal_setMillis(cal, date1, &status);
1122    if (U_FAILURE(status)) {
1123        log_err("ucal_setMillis() failed: %s\n", u_errorName(status));
1124        return;
1125    }
1126
1127    offset = ucal_get(cal, UCAL_ZONE_OFFSET, &status);
1128    offset += ucal_get(cal, UCAL_DST_OFFSET, &status);
1129
1130    if (U_FAILURE(status)) {
1131        log_err("ucal_get() failed: %s\n", u_errorName(status));
1132        return;
1133    }
1134    temp=(double)((double)offset / 1000.0 / 60.0 / 60.0);
1135    /*printf("offset for %s %f hr\n", austrdup(myDateFormat(datfor, date1)), temp);*/
1136
1137    utc = ((ucal_get(cal, UCAL_HOUR_OF_DAY, &status) * 60 +
1138                    ucal_get(cal, UCAL_MINUTE, &status)) * 60 +
1139                   ucal_get(cal, UCAL_SECOND, &status)) * 1000 +
1140                    ucal_get(cal, UCAL_MILLISECOND, &status) - offset;
1141    if (U_FAILURE(status)) {
1142        log_err("ucal_get() failed: %s\n", u_errorName(status));
1143        return;
1144    }
1145
1146    expected = ((hr * 60 + mn) * 60 + sc) * 1000;
1147    if (utc != expected) {
1148        temp=(double)(utc - expected)/ 1000 / 60 / 60.0;
1149        log_err("FAIL: Discrepancy of %d  millis = %fhr\n", utc-expected, temp );
1150    }
1151    else
1152        log_verbose("PASS: the offset between local and GMT is correct\n");
1153    ucal_close(gmtcal);
1154    ucal_close(cal);
1155    udat_close(datfor);
1156}
1157
1158/* ------------------------------------- */
1159
1160
1161
1162
1163/* INTERNAL FUNCTIONS USED */
1164/*------------------------------------------------------------------------------------------- */
1165
1166/* ------------------------------------- */
1167static void checkDateTime(UCalendar* c,
1168                        int32_t y, int32_t m, int32_t d,
1169                        int32_t hr, int32_t min, int32_t sec,
1170                        int32_t ms, UCalendarDateFields field)
1171
1172{
1173    UErrorCode status = U_ZERO_ERROR;
1174    if (ucal_get(c, UCAL_YEAR, &status) != y ||
1175        ucal_get(c, UCAL_MONTH, &status) != m ||
1176        ucal_get(c, UCAL_DATE, &status) != d ||
1177        ucal_get(c, UCAL_HOUR, &status) != hr ||
1178        ucal_get(c, UCAL_MINUTE, &status) != min ||
1179        ucal_get(c, UCAL_SECOND, &status) != sec ||
1180        ucal_get(c, UCAL_MILLISECOND, &status) != ms) {
1181        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",
1182            (int32_t)field, y, m + 1, d, hr, min, sec, ms,
1183                    ucal_get(c, UCAL_YEAR, &status),
1184                    ucal_get(c, UCAL_MONTH, &status) + 1,
1185                    ucal_get(c, UCAL_DATE, &status),
1186                    ucal_get(c, UCAL_HOUR, &status),
1187                    ucal_get(c, UCAL_MINUTE, &status) + 1,
1188                    ucal_get(c, UCAL_SECOND, &status),
1189                    ucal_get(c, UCAL_MILLISECOND, &status) );
1190
1191                    if (U_FAILURE(status)){
1192                    log_err("ucal_get failed: %s\n", u_errorName(status));
1193                    return;
1194                    }
1195
1196     }
1197    else
1198        log_verbose("Confirmed: %d/%d/%d %d:%d:%d:%d\n", y, m + 1, d, hr, min, sec, ms);
1199
1200}
1201
1202/* ------------------------------------- */
1203static void checkDate(UCalendar* c, int32_t y, int32_t m, int32_t d)
1204{
1205    UErrorCode status = U_ZERO_ERROR;
1206    if (ucal_get(c,UCAL_YEAR, &status) != y ||
1207        ucal_get(c, UCAL_MONTH, &status) != m ||
1208        ucal_get(c, UCAL_DATE, &status) != d) {
1209
1210        log_err("FAILURE: Expected y/m/d of %d/%d/%d  got %d/%d/%d\n", y, m + 1, d,
1211                    ucal_get(c, UCAL_YEAR, &status),
1212                    ucal_get(c, UCAL_MONTH, &status) + 1,
1213                    ucal_get(c, UCAL_DATE, &status) );
1214
1215        if (U_FAILURE(status)) {
1216            log_err("ucal_get failed: %s\n", u_errorName(status));
1217            return;
1218        }
1219    }
1220    else
1221        log_verbose("Confirmed: %d/%d/%d\n", y, m + 1, d);
1222
1223
1224}
1225
1226/* ------------------------------------- */
1227
1228/* ------------------------------------- */
1229
1230static void verify1(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day)
1231{
1232    UDate d1;
1233    UErrorCode status = U_ZERO_ERROR;
1234    if (ucal_get(c, UCAL_YEAR, &status) == year &&
1235        ucal_get(c, UCAL_MONTH, &status) == month &&
1236        ucal_get(c, UCAL_DATE, &status) == day) {
1237        if (U_FAILURE(status)) {
1238            log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1239            return;
1240        }
1241        log_verbose("PASS: %s\n", msg);
1242        d1=ucal_getMillis(c, &status);
1243        if (U_FAILURE(status)) {
1244            log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1245            return;
1246        }
1247    /*log_verbose(austrdup(myDateFormat(dat, d1)) );*/
1248    }
1249    else {
1250        log_err("FAIL: %s\n", msg);
1251        d1=ucal_getMillis(c, &status);
1252        if (U_FAILURE(status)) {
1253            log_err("ucal_getMillis failed: %s\n", u_errorName(status) );
1254            return;
1255        }
1256        log_err("Got %s  Expected %d/%d/%d \n", austrdup(myDateFormat(dat, d1)), year, month + 1, day );
1257        return;
1258    }
1259
1260
1261}
1262
1263/* ------------------------------------ */
1264static void verify2(const char* msg, UCalendar* c, UDateFormat* dat, int32_t year, int32_t month, int32_t day,
1265                                                                     int32_t hour, int32_t min, int32_t sec, int32_t am_pm)
1266{
1267    UDate d1;
1268    UErrorCode status = U_ZERO_ERROR;
1269    char tempMsgBuf[256];
1270
1271    if (ucal_get(c, UCAL_YEAR, &status) == year &&
1272        ucal_get(c, UCAL_MONTH, &status) == month &&
1273        ucal_get(c, UCAL_DATE, &status) == day &&
1274        ucal_get(c, UCAL_HOUR, &status) == hour &&
1275        ucal_get(c, UCAL_MINUTE, &status) == min &&
1276        ucal_get(c, UCAL_SECOND, &status) == sec &&
1277        ucal_get(c, UCAL_AM_PM, &status) == am_pm ){
1278        if (U_FAILURE(status)) {
1279            log_err("FAIL: Calendar::get failed: %s\n", u_errorName(status));
1280            return;
1281        }
1282        log_verbose("PASS: %s\n", msg);
1283        d1=ucal_getMillis(c, &status);
1284        if (U_FAILURE(status)) {
1285            log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1286            return;
1287        }
1288        log_verbose("%s\n" , u_austrcpy(tempMsgBuf, myDateFormat(dat, d1)) );
1289    }
1290    else {
1291        log_err("FAIL: %s\n", msg);
1292        d1=ucal_getMillis(c, &status);
1293        if (U_FAILURE(status)) {
1294            log_err("ucal_getMillis failed: %s\n", u_errorName(status));
1295            return;
1296        }
1297        log_err("Got %s Expected %d/%d/%d/ %d:%d:%d  %s\n", austrdup(myDateFormat(dat, d1)),
1298            year, month + 1, day, hour, min, sec, (am_pm==0) ? "AM": "PM");
1299
1300        return;
1301    }
1302
1303
1304}
1305
1306void TestGregorianChange() {
1307    static const UChar utc[] = { 0x45, 0x74, 0x63, 0x2f, 0x47, 0x4d, 0x54, 0 }; /* "Etc/GMT" */
1308    const int32_t dayMillis = 86400 * INT64_C(1000);    /* 1 day = 86400 seconds */
1309    UCalendar *cal;
1310    UDate date;
1311    UErrorCode errorCode = U_ZERO_ERROR;
1312
1313    /* Test ucal_setGregorianChange() on a Gregorian calendar. */
1314    errorCode = U_ZERO_ERROR;
1315    cal = ucal_open(utc, -1, "", UCAL_GREGORIAN, &errorCode);
1316    if(U_FAILURE(errorCode)) {
1317        log_err("ucal_open(UTC) failed: %s\n", u_errorName(errorCode));
1318        return;
1319    }
1320    ucal_setGregorianChange(cal, -365 * (dayMillis * (UDate)1), &errorCode);
1321    if(U_FAILURE(errorCode)) {
1322        log_err("ucal_setGregorianChange(1969) failed: %s\n", u_errorName(errorCode));
1323    } else {
1324        date = ucal_getGregorianChange(cal, &errorCode);
1325        if(U_FAILURE(errorCode) || date != -365 * (dayMillis * (UDate)1)) {
1326            log_err("ucal_getGregorianChange() failed: %s, date = %f\n", u_errorName(errorCode), date);
1327        }
1328    }
1329    ucal_close(cal);
1330
1331    /* Test ucal_setGregorianChange() on a non-Gregorian calendar where it should fail. */
1332    errorCode = U_ZERO_ERROR;
1333    cal = ucal_open(utc, -1, "th@calendar=buddhist", UCAL_TRADITIONAL, &errorCode);
1334    if(U_FAILURE(errorCode)) {
1335        log_err("ucal_open(UTC, non-Gregorian) failed: %s\n", u_errorName(errorCode));
1336        return;
1337    }
1338    ucal_setGregorianChange(cal, -730 * (dayMillis * (UDate)1), &errorCode);
1339    if(errorCode != U_UNSUPPORTED_ERROR) {
1340        log_err("ucal_setGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1341                u_errorName(errorCode));
1342    }
1343    errorCode = U_ZERO_ERROR;
1344    date = ucal_getGregorianChange(cal, &errorCode);
1345    if(errorCode != U_UNSUPPORTED_ERROR) {
1346        log_err("ucal_getGregorianChange(non-Gregorian calendar) did not yield U_UNSUPPORTED_ERROR but %s\n",
1347                u_errorName(errorCode));
1348    }
1349    ucal_close(cal);
1350}
1351
1352#endif /* #if !UCONFIG_NO_FORMATTING */
1353