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