]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/caltest.cpp
ICU-491.11.3.tar.gz
[apple/icu.git] / icuSources / test / intltest / caltest.cpp
1 /************************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2012, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ************************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "caltest.h"
12 #include "unicode/dtfmtsym.h"
13 #include "unicode/gregocal.h"
14 #include "hebrwcal.h"
15 #include "unicode/smpdtfmt.h"
16 #include "unicode/simpletz.h"
17 #include "dbgutil.h"
18 #include "unicode/udat.h"
19 #include "unicode/ustring.h"
20 #include "cstring.h"
21 #include "unicode/localpointer.h"
22
23 #define mkcstr(U) u_austrcpy(calloc(8, u_strlen(U) + 1), U)
24
25 #define TEST_CHECK_STATUS {if (U_FAILURE(status)) {errln("%s:%d: Test failure. status=%s", \
26 __FILE__, __LINE__, u_errorName(status)); return;}}
27
28 #define TEST_ASSERT(expr) {if ((expr)==FALSE) {errln("%s:%d: Test failure \n", __FILE__, __LINE__);};}
29
30 // *****************************************************************************
31 // class CalendarTest
32 // *****************************************************************************
33
34 UnicodeString CalendarTest::calToStr(const Calendar & cal)
35 {
36 UnicodeString out;
37 UErrorCode status = U_ZERO_ERROR;
38 int i;
39 UDate d;
40 for(i = 0;i<UCAL_FIELD_COUNT;i++) {
41 out += (UnicodeString("") + fieldName((UCalendarDateFields)i) + "=" + cal.get((UCalendarDateFields)i, status) + UnicodeString(" "));
42 }
43 out += "[" + UnicodeString(cal.getType()) + "]";
44
45 if(cal.inDaylightTime(status)) {
46 out += UnicodeString(" (in DST), zone=");
47 }
48 else {
49 out += UnicodeString(", zone=");
50 }
51
52 UnicodeString str2;
53 out += cal.getTimeZone().getDisplayName(str2);
54 d = cal.getTime(status);
55 out += UnicodeString(" :","") + d;
56
57 return out;
58 }
59
60 void CalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
61 {
62 if (exec) logln("TestSuite TestCalendar");
63 switch (index) {
64 case 0:
65 name = "TestDOW943";
66 if (exec) {
67 logln("TestDOW943---"); logln("");
68 TestDOW943();
69 }
70 break;
71 case 1:
72 name = "TestClonesUnique908";
73 if (exec) {
74 logln("TestClonesUnique908---"); logln("");
75 TestClonesUnique908();
76 }
77 break;
78 case 2:
79 name = "TestGregorianChange768";
80 if (exec) {
81 logln("TestGregorianChange768---"); logln("");
82 TestGregorianChange768();
83 }
84 break;
85 case 3:
86 name = "TestDisambiguation765";
87 if (exec) {
88 logln("TestDisambiguation765---"); logln("");
89 TestDisambiguation765();
90 }
91 break;
92 case 4:
93 name = "TestGMTvsLocal4064654";
94 if (exec) {
95 logln("TestGMTvsLocal4064654---"); logln("");
96 TestGMTvsLocal4064654();
97 }
98 break;
99 case 5:
100 name = "TestAddSetOrder621";
101 if (exec) {
102 logln("TestAddSetOrder621---"); logln("");
103 TestAddSetOrder621();
104 }
105 break;
106 case 6:
107 name = "TestAdd520";
108 if (exec) {
109 logln("TestAdd520---"); logln("");
110 TestAdd520();
111 }
112 break;
113 case 7:
114 name = "TestFieldSet4781";
115 if (exec) {
116 logln("TestFieldSet4781---"); logln("");
117 TestFieldSet4781();
118 }
119 break;
120 case 8:
121 name = "TestSerialize337";
122 if (exec) {
123 logln("TestSerialize337---"); logln("");
124 // TestSerialize337();
125 }
126 break;
127 case 9:
128 name = "TestSecondsZero121";
129 if (exec) {
130 logln("TestSecondsZero121---"); logln("");
131 TestSecondsZero121();
132 }
133 break;
134 case 10:
135 name = "TestAddSetGet0610";
136 if (exec) {
137 logln("TestAddSetGet0610---"); logln("");
138 TestAddSetGet0610();
139 }
140 break;
141 case 11:
142 name = "TestFields060";
143 if (exec) {
144 logln("TestFields060---"); logln("");
145 TestFields060();
146 }
147 break;
148 case 12:
149 name = "TestEpochStartFields";
150 if (exec) {
151 logln("TestEpochStartFields---"); logln("");
152 TestEpochStartFields();
153 }
154 break;
155 case 13:
156 name = "TestDOWProgression";
157 if (exec) {
158 logln("TestDOWProgression---"); logln("");
159 TestDOWProgression();
160 }
161 break;
162 case 14:
163 name = "TestGenericAPI";
164 if (exec) {
165 logln("TestGenericAPI---"); logln("");
166 TestGenericAPI();
167 }
168 break;
169 case 15:
170 name = "TestAddRollExtensive";
171 if (exec) {
172 logln("TestAddRollExtensive---"); logln("");
173 TestAddRollExtensive();
174 }
175 break;
176 case 16:
177 name = "TestDOW_LOCALandYEAR_WOY";
178 if (exec) {
179 logln("TestDOW_LOCALandYEAR_WOY---"); logln("");
180 TestDOW_LOCALandYEAR_WOY();
181 }
182 break;
183 case 17:
184 name = "TestWOY";
185 if (exec) {
186 logln("TestWOY---"); logln("");
187 TestWOY();
188 }
189 break;
190 case 18:
191 name = "TestRog";
192 if (exec) {
193 logln("TestRog---"); logln("");
194 TestRog();
195 }
196 break;
197 case 19:
198 name = "TestYWOY";
199 if (exec) {
200 logln("TestYWOY---"); logln("");
201 TestYWOY();
202 }
203 break;
204 case 20:
205 name = "TestJD";
206 if(exec) {
207 logln("TestJD---"); logln("");
208 TestJD();
209 }
210 break;
211 case 21:
212 name = "TestDebug";
213 if(exec) {
214 logln("TestDebug---"); logln("");
215 TestDebug();
216 }
217 break;
218 case 22:
219 name = "Test6703";
220 if(exec) {
221 logln("Test6703---"); logln("");
222 Test6703();
223 }
224 break;
225 case 23:
226 name = "Test3785";
227 if(exec) {
228 logln("Test3785---"); logln("");
229 Test3785();
230 }
231 break;
232 case 24:
233 name = "Test1624";
234 if(exec) {
235 logln("Test1624---"); logln("");
236 Test1624();
237 }
238 break;
239 case 25:
240 name = "TestTimeStamp";
241 if(exec) {
242 logln("TestTimeStamp---"); logln("");
243 TestTimeStamp();
244 }
245 break;
246 case 26:
247 name = "TestISO8601";
248 if(exec) {
249 logln("TestISO8601---"); logln("");
250 TestISO8601();
251 }
252 break;
253 case 27:
254 name = "TestAmbiguousWallTimeAPIs";
255 if(exec) {
256 logln("TestAmbiguousWallTimeAPIs---"); logln("");
257 TestAmbiguousWallTimeAPIs();
258 }
259 break;
260 case 28:
261 name = "TestRepeatedWallTime";
262 if(exec) {
263 logln("TestRepeatedWallTime---"); logln("");
264 TestRepeatedWallTime();
265 }
266 break;
267 case 29:
268 name = "TestSkippedWallTime";
269 if(exec) {
270 logln("TestSkippedWallTime---"); logln("");
271 TestSkippedWallTime();
272 }
273 break;
274 case 30:
275 name = "TestCloneLocale";
276 if(exec) {
277 logln("TestCloneLocale---"); logln("");
278 TestCloneLocale();
279 }
280 break;
281 default: name = ""; break;
282 }
283 }
284
285 // ---------------------------------------------------------------------------------
286
287 UnicodeString CalendarTest::fieldName(UCalendarDateFields f) {
288 switch (f) {
289 #define FIELD_NAME_STR(x) case x: return (#x+5)
290 FIELD_NAME_STR( UCAL_ERA );
291 FIELD_NAME_STR( UCAL_YEAR );
292 FIELD_NAME_STR( UCAL_MONTH );
293 FIELD_NAME_STR( UCAL_WEEK_OF_YEAR );
294 FIELD_NAME_STR( UCAL_WEEK_OF_MONTH );
295 FIELD_NAME_STR( UCAL_DATE );
296 FIELD_NAME_STR( UCAL_DAY_OF_YEAR );
297 FIELD_NAME_STR( UCAL_DAY_OF_WEEK );
298 FIELD_NAME_STR( UCAL_DAY_OF_WEEK_IN_MONTH );
299 FIELD_NAME_STR( UCAL_AM_PM );
300 FIELD_NAME_STR( UCAL_HOUR );
301 FIELD_NAME_STR( UCAL_HOUR_OF_DAY );
302 FIELD_NAME_STR( UCAL_MINUTE );
303 FIELD_NAME_STR( UCAL_SECOND );
304 FIELD_NAME_STR( UCAL_MILLISECOND );
305 FIELD_NAME_STR( UCAL_ZONE_OFFSET );
306 FIELD_NAME_STR( UCAL_DST_OFFSET );
307 FIELD_NAME_STR( UCAL_YEAR_WOY );
308 FIELD_NAME_STR( UCAL_DOW_LOCAL );
309 FIELD_NAME_STR( UCAL_EXTENDED_YEAR );
310 FIELD_NAME_STR( UCAL_JULIAN_DAY );
311 FIELD_NAME_STR( UCAL_MILLISECONDS_IN_DAY );
312 #undef FIELD_NAME_STR
313 default:
314 return UnicodeString("") + ((int32_t)f);
315 }
316 }
317
318 /**
319 * Test various API methods for API completeness.
320 */
321 void
322 CalendarTest::TestGenericAPI()
323 {
324 UErrorCode status = U_ZERO_ERROR;
325 UDate d;
326 UnicodeString str;
327 UBool eq = FALSE,b4 = FALSE,af = FALSE;
328
329 UDate when = date(90, UCAL_APRIL, 15);
330
331 UnicodeString tzid("TestZone");
332 int32_t tzoffset = 123400;
333
334 SimpleTimeZone *zone = new SimpleTimeZone(tzoffset, tzid);
335 Calendar *cal = Calendar::createInstance(zone->clone(), status);
336 if (failure(status, "Calendar::createInstance", TRUE)) return;
337
338 if (*zone != cal->getTimeZone()) errln("FAIL: Calendar::getTimeZone failed");
339
340 Calendar *cal2 = Calendar::createInstance(cal->getTimeZone(), status);
341 if (failure(status, "Calendar::createInstance")) return;
342 cal->setTime(when, status);
343 cal2->setTime(when, status);
344 if (failure(status, "Calendar::setTime")) return;
345
346 if (!(*cal == *cal2)) errln("FAIL: Calendar::operator== failed");
347 if ((*cal != *cal2)) errln("FAIL: Calendar::operator!= failed");
348 if (!cal->equals(*cal2, status) ||
349 cal->before(*cal2, status) ||
350 cal->after(*cal2, status) ||
351 U_FAILURE(status)) errln("FAIL: equals/before/after failed");
352
353 logln(UnicodeString("cal=") +cal->getTime(status) + UnicodeString(calToStr(*cal)));
354 logln(UnicodeString("cal2=") +cal2->getTime(status) + UnicodeString(calToStr(*cal2)));
355 logln("cal2->setTime(when+1000)");
356 cal2->setTime(when + 1000, status);
357 logln(UnicodeString("cal2=") +cal2->getTime(status) + UnicodeString(calToStr(*cal2)));
358
359 if (failure(status, "Calendar::setTime")) return;
360 if (cal->equals(*cal2, status) ||
361 cal2->before(*cal, status) ||
362 cal->after(*cal2, status) ||
363 U_FAILURE(status)) errln("FAIL: equals/before/after failed after setTime(+1000)");
364
365 logln("cal->roll(UCAL_SECOND)");
366 cal->roll(UCAL_SECOND, (UBool) TRUE, status);
367 logln(UnicodeString("cal=") +cal->getTime(status) + UnicodeString(calToStr(*cal)));
368 cal->roll(UCAL_SECOND, (int32_t)0, status);
369 logln(UnicodeString("cal=") +cal->getTime(status) + UnicodeString(calToStr(*cal)));
370 if (failure(status, "Calendar::roll")) return;
371
372 if (!(eq=cal->equals(*cal2, status)) ||
373 (b4=cal->before(*cal2, status)) ||
374 (af=cal->after(*cal2, status)) ||
375 U_FAILURE(status)) {
376 errln("FAIL: equals[%c]/before[%c]/after[%c] failed after roll 1 second [should be T/F/F]",
377 eq?'T':'F',
378 b4?'T':'F',
379 af?'T':'F');
380 logln(UnicodeString("cal=") +cal->getTime(status) + UnicodeString(calToStr(*cal)));
381 logln(UnicodeString("cal2=") +cal2->getTime(status) + UnicodeString(calToStr(*cal2)));
382 }
383
384 // Roll back to January
385 cal->roll(UCAL_MONTH, (int32_t)(1 + UCAL_DECEMBER - cal->get(UCAL_MONTH, status)), status);
386 if (failure(status, "Calendar::roll")) return;
387 if (cal->equals(*cal2, status) ||
388 cal2->before(*cal, status) ||
389 cal->after(*cal2, status) ||
390 U_FAILURE(status)) errln("FAIL: equals/before/after failed after rollback to January");
391
392 TimeZone *z = cal->orphanTimeZone();
393 if (z->getID(str) != tzid ||
394 z->getRawOffset() != tzoffset)
395 errln("FAIL: orphanTimeZone failed");
396
397 int32_t i;
398 for (i=0; i<2; ++i)
399 {
400 UBool lenient = ( i > 0 );
401 cal->setLenient(lenient);
402 if (lenient != cal->isLenient()) errln("FAIL: setLenient/isLenient failed");
403 // Later: Check for lenient behavior
404 }
405
406 for (i=UCAL_SUNDAY; i<=UCAL_SATURDAY; ++i)
407 {
408 cal->setFirstDayOfWeek((UCalendarDaysOfWeek)i);
409 if (cal->getFirstDayOfWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");
410 UErrorCode aStatus = U_ZERO_ERROR;
411 if (cal->getFirstDayOfWeek(aStatus) != i || U_FAILURE(aStatus)) errln("FAIL: getFirstDayOfWeek(status) failed");
412 }
413
414 for (i=1; i<=7; ++i)
415 {
416 cal->setMinimalDaysInFirstWeek((uint8_t)i);
417 if (cal->getMinimalDaysInFirstWeek() != i) errln("FAIL: set/getFirstDayOfWeek failed");
418 }
419
420 for (i=0; i<UCAL_FIELD_COUNT; ++i)
421 {
422 if (cal->getMinimum((UCalendarDateFields)i) > cal->getGreatestMinimum((UCalendarDateFields)i))
423 errln("FAIL: getMinimum larger than getGreatestMinimum for field " + i);
424 if (cal->getLeastMaximum((UCalendarDateFields)i) > cal->getMaximum((UCalendarDateFields)i))
425 errln("FAIL: getLeastMaximum larger than getMaximum for field " + i);
426 if (cal->getMinimum((UCalendarDateFields)i) >= cal->getMaximum((UCalendarDateFields)i))
427 errln("FAIL: getMinimum not less than getMaximum for field " + i);
428 }
429
430 cal->adoptTimeZone(TimeZone::createDefault());
431 cal->clear();
432 cal->set(1984, 5, 24);
433 if (cal->getTime(status) != date(84, 5, 24) || U_FAILURE(status))
434 errln("FAIL: Calendar::set(3 args) failed");
435
436 cal->clear();
437 cal->set(1985, 3, 2, 11, 49);
438 if (cal->getTime(status) != date(85, 3, 2, 11, 49) || U_FAILURE(status))
439 errln("FAIL: Calendar::set(5 args) failed");
440
441 cal->clear();
442 cal->set(1995, 9, 12, 1, 39, 55);
443 if (cal->getTime(status) != date(95, 9, 12, 1, 39, 55) || U_FAILURE(status))
444 errln("FAIL: Calendar::set(6 args) failed");
445
446 cal->getTime(status);
447 if (failure(status, "Calendar::getTime")) return;
448 for (i=0; i<UCAL_FIELD_COUNT; ++i)
449 {
450 switch(i) {
451 case UCAL_YEAR: case UCAL_MONTH: case UCAL_DATE:
452 case UCAL_HOUR_OF_DAY: case UCAL_MINUTE: case UCAL_SECOND:
453 case UCAL_EXTENDED_YEAR:
454 if (!cal->isSet((UCalendarDateFields)i)) errln("FAIL: Calendar::isSet F, should be T " + fieldName((UCalendarDateFields)i));
455 break;
456 default:
457 if (cal->isSet((UCalendarDateFields)i)) errln("FAIL: Calendar::isSet = T, should be F " + fieldName((UCalendarDateFields)i));
458 }
459 cal->clear((UCalendarDateFields)i);
460 if (cal->isSet((UCalendarDateFields)i)) errln("FAIL: Calendar::clear/isSet failed " + fieldName((UCalendarDateFields)i));
461 }
462
463 if(cal->getActualMinimum(Calendar::SECOND, status) != 0){
464 errln("Calendar is suppose to return 0 for getActualMinimum");
465 }
466
467 Calendar *cal3 = Calendar::createInstance(status);
468 cal3->roll(Calendar::SECOND, (int32_t)0, status);
469 if (failure(status, "Calendar::roll(EDateFields, int32_t, UErrorCode)")) return;
470
471 delete cal;
472 delete cal2;
473 delete cal3;
474
475 int32_t count;
476 const Locale* loc = Calendar::getAvailableLocales(count);
477 if (count < 1 || loc == 0)
478 {
479 dataerrln("FAIL: getAvailableLocales failed");
480 }
481 else
482 {
483 for (i=0; i<count; ++i)
484 {
485 cal = Calendar::createInstance(loc[i], status);
486 if (failure(status, "Calendar::createInstance")) return;
487 delete cal;
488 }
489 }
490
491 cal = Calendar::createInstance(TimeZone::createDefault(), Locale::getEnglish(), status);
492 if (failure(status, "Calendar::createInstance")) return;
493 delete cal;
494
495 cal = Calendar::createInstance(*zone, Locale::getEnglish(), status);
496 if (failure(status, "Calendar::createInstance")) return;
497 delete cal;
498
499 GregorianCalendar *gc = new GregorianCalendar(*zone, status);
500 if (failure(status, "new GregorianCalendar")) return;
501 delete gc;
502
503 gc = new GregorianCalendar(Locale::getEnglish(), status);
504 if (failure(status, "new GregorianCalendar")) return;
505 delete gc;
506
507 gc = new GregorianCalendar(Locale::getEnglish(), status);
508 delete gc;
509
510 gc = new GregorianCalendar(*zone, Locale::getEnglish(), status);
511 if (failure(status, "new GregorianCalendar")) return;
512 delete gc;
513
514 gc = new GregorianCalendar(zone, status);
515 if (failure(status, "new GregorianCalendar")) return;
516 delete gc;
517
518 gc = new GregorianCalendar(1998, 10, 14, 21, 43, status);
519 if (gc->getTime(status) != (d =date(98, 10, 14, 21, 43) )|| U_FAILURE(status))
520 errln("FAIL: new GregorianCalendar(ymdhm) failed with " + UnicodeString(u_errorName(status)) + ", cal=" + gc->getTime(status) + UnicodeString(calToStr(*gc)) + ", d=" + d);
521 else
522 logln(UnicodeString("GOOD: cal=") +gc->getTime(status) + UnicodeString(calToStr(*gc)) + ", d=" + d);
523 delete gc;
524
525 gc = new GregorianCalendar(1998, 10, 14, 21, 43, 55, status);
526 if (gc->getTime(status) != (d=date(98, 10, 14, 21, 43, 55)) || U_FAILURE(status))
527 errln("FAIL: new GregorianCalendar(ymdhms) failed with " + UnicodeString(u_errorName(status)));
528
529 GregorianCalendar gc2(Locale::getEnglish(), status);
530 if (failure(status, "new GregorianCalendar")) return;
531 gc2 = *gc;
532 if (gc2 != *gc || !(gc2 == *gc)) errln("FAIL: GregorianCalendar assignment/operator==/operator!= failed");
533 delete gc;
534 delete z;
535
536 /* Code coverage for Calendar class. */
537 cal = Calendar::createInstance(status);
538 if (failure(status, "Calendar::createInstance")) {
539 return;
540 }else {
541 ((Calendar *)cal)->roll(UCAL_HOUR, (int32_t)100, status);
542 ((Calendar *)cal)->clear(UCAL_HOUR);
543 #if !UCONFIG_NO_SERVICE
544 URegistryKey key = cal->registerFactory(NULL, status);
545 cal->unregister(key, status);
546 #endif
547 }
548 delete cal;
549
550 status = U_ZERO_ERROR;
551 cal = Calendar::createInstance(Locale("he_IL@calendar=hebrew"), status);
552 if (failure(status, "Calendar::createInstance")) {
553 return;
554 } else {
555 cal->roll(Calendar::MONTH, (int32_t)100, status);
556 }
557
558 StringEnumeration *en = Calendar::getKeywordValuesForLocale(NULL, Locale::getDefault(),FALSE, status);
559 if (en == NULL || U_FAILURE(status)) {
560 dataerrln("FAIL: getKeywordValuesForLocale for Calendar. : %s", u_errorName(status));
561 }
562 delete en;
563 delete cal;
564 }
565
566 // -------------------------------------
567
568 /**
569 * This test confirms the correct behavior of add when incrementing
570 * through subsequent days.
571 */
572 void
573 CalendarTest::TestRog()
574 {
575 UErrorCode status = U_ZERO_ERROR;
576 GregorianCalendar* gc = new GregorianCalendar(status);
577 if (failure(status, "new GregorianCalendar", TRUE)) return;
578 int32_t year = 1997, month = UCAL_APRIL, date = 1;
579 gc->set(year, month, date);
580 gc->set(UCAL_HOUR_OF_DAY, 23);
581 gc->set(UCAL_MINUTE, 0);
582 gc->set(UCAL_SECOND, 0);
583 gc->set(UCAL_MILLISECOND, 0);
584 for (int32_t i = 0; i < 9; i++, gc->add(UCAL_DATE, 1, status)) {
585 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
586 if (gc->get(UCAL_YEAR, status) != year ||
587 gc->get(UCAL_MONTH, status) != month ||
588 gc->get(UCAL_DATE, status) != (date + i)) errln("FAIL: Date wrong");
589 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
590 }
591 delete gc;
592 }
593
594 // -------------------------------------
595
596 /**
597 * Test the handling of the day of the week, checking for correctness and
598 * for correct minimum and maximum values.
599 */
600 void
601 CalendarTest::TestDOW943()
602 {
603 dowTest(FALSE);
604 dowTest(TRUE);
605 }
606
607 void CalendarTest::dowTest(UBool lenient)
608 {
609 UErrorCode status = U_ZERO_ERROR;
610 GregorianCalendar* cal = new GregorianCalendar(status);
611 if (failure(status, "new GregorianCalendar", TRUE)) return;
612 logln("cal - Aug 12, 1997\n");
613 cal->set(1997, UCAL_AUGUST, 12);
614 cal->getTime(status);
615 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
616 logln((lenient?UnicodeString("LENIENT0: "):UnicodeString("nonlenient0: ")) + UnicodeString(calToStr(*cal)));
617 cal->setLenient(lenient);
618 logln("cal - Dec 1, 1996\n");
619 cal->set(1996, UCAL_DECEMBER, 1);
620 logln((lenient?UnicodeString("LENIENT: "):UnicodeString("nonlenient: ")) + UnicodeString(calToStr(*cal)));
621 int32_t dow = cal->get(UCAL_DAY_OF_WEEK, status);
622 if (U_FAILURE(status)) { errln("Calendar::get failed [%s]", u_errorName(status)); return; }
623 int32_t min = cal->getMinimum(UCAL_DAY_OF_WEEK);
624 int32_t max = cal->getMaximum(UCAL_DAY_OF_WEEK);
625 if (dow < min ||
626 dow > max) errln(UnicodeString("FAIL: Day of week ") + (int32_t)dow + " out of range");
627 if (dow != UCAL_SUNDAY) errln("FAIL: Day of week should be SUNDAY[%d] not %d", UCAL_SUNDAY, dow);
628 if (min != UCAL_SUNDAY ||
629 max != UCAL_SATURDAY) errln("FAIL: Min/max bad");
630 delete cal;
631 }
632
633 // -------------------------------------
634
635 /**
636 * Confirm that cloned Calendar objects do not inadvertently share substructures.
637 */
638 void
639 CalendarTest::TestClonesUnique908()
640 {
641 UErrorCode status = U_ZERO_ERROR;
642 Calendar *c = Calendar::createInstance(status);
643 if (failure(status, "Calendar::createInstance", TRUE)) return;
644 Calendar *d = (Calendar*) c->clone();
645 c->set(UCAL_MILLISECOND, 123);
646 d->set(UCAL_MILLISECOND, 456);
647 if (c->get(UCAL_MILLISECOND, status) != 123 ||
648 d->get(UCAL_MILLISECOND, status) != 456) {
649 errln("FAIL: Clones share fields");
650 }
651 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
652 delete c;
653 delete d;
654 }
655
656 // -------------------------------------
657
658 /**
659 * Confirm that the Gregorian cutoff value works as advertised.
660 */
661 void
662 CalendarTest::TestGregorianChange768()
663 {
664 UBool b;
665 UErrorCode status = U_ZERO_ERROR;
666 UnicodeString str;
667 GregorianCalendar* c = new GregorianCalendar(status);
668 if (failure(status, "new GregorianCalendar", TRUE)) return;
669 logln(UnicodeString("With cutoff ") + dateToString(c->getGregorianChange(), str));
670 b = c->isLeapYear(1800);
671 logln(UnicodeString(" isLeapYear(1800) = ") + (b ? "true" : "false"));
672 logln(UnicodeString(" (should be FALSE)"));
673 if (b) errln("FAIL");
674 c->setGregorianChange(date(0, 0, 1), status);
675 if (U_FAILURE(status)) { errln("GregorianCalendar::setGregorianChange failed"); return; }
676 logln(UnicodeString("With cutoff ") + dateToString(c->getGregorianChange(), str));
677 b = c->isLeapYear(1800);
678 logln(UnicodeString(" isLeapYear(1800) = ") + (b ? "true" : "false"));
679 logln(UnicodeString(" (should be TRUE)"));
680 if (!b) errln("FAIL");
681 delete c;
682 }
683
684 // -------------------------------------
685
686 /**
687 * Confirm the functioning of the field disambiguation algorithm.
688 */
689 void
690 CalendarTest::TestDisambiguation765()
691 {
692 UErrorCode status = U_ZERO_ERROR;
693 Calendar *c = Calendar::createInstance("en_US", status);
694 if (failure(status, "Calendar::createInstance", TRUE)) return;
695 c->setLenient(FALSE);
696 c->clear();
697 c->set(UCAL_YEAR, 1997);
698 c->set(UCAL_MONTH, UCAL_JUNE);
699 c->set(UCAL_DATE, 3);
700 verify765("1997 third day of June = ", c, 1997, UCAL_JUNE, 3);
701 c->clear();
702 c->set(UCAL_YEAR, 1997);
703 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
704 c->set(UCAL_MONTH, UCAL_JUNE);
705 c->set(UCAL_DAY_OF_WEEK_IN_MONTH, 1);
706 verify765("1997 first Tuesday in June = ", c, 1997, UCAL_JUNE, 3);
707 c->clear();
708 c->set(UCAL_YEAR, 1997);
709 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
710 c->set(UCAL_MONTH, UCAL_JUNE);
711 c->set(UCAL_DAY_OF_WEEK_IN_MONTH, - 1);
712 verify765("1997 last Tuesday in June = ", c, 1997, UCAL_JUNE, 24);
713
714 status = U_ZERO_ERROR;
715 c->clear();
716 c->set(UCAL_YEAR, 1997);
717 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
718 c->set(UCAL_MONTH, UCAL_JUNE);
719 c->set(UCAL_DAY_OF_WEEK_IN_MONTH, 0);
720 c->getTime(status);
721 verify765("1997 zero-th Tuesday in June = ", status);
722
723 c->clear();
724 c->set(UCAL_YEAR, 1997);
725 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
726 c->set(UCAL_MONTH, UCAL_JUNE);
727 c->set(UCAL_WEEK_OF_MONTH, 1);
728 verify765("1997 Tuesday in week 1 of June = ", c, 1997, UCAL_JUNE, 3);
729 c->clear();
730 c->set(UCAL_YEAR, 1997);
731 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
732 c->set(UCAL_MONTH, UCAL_JUNE);
733 c->set(UCAL_WEEK_OF_MONTH, 5);
734 verify765("1997 Tuesday in week 5 of June = ", c, 1997, UCAL_JULY, 1);
735
736 status = U_ZERO_ERROR;
737 c->clear();
738 c->set(UCAL_YEAR, 1997);
739 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
740 c->set(UCAL_MONTH, UCAL_JUNE);
741 c->set(UCAL_WEEK_OF_MONTH, 0);
742 c->setMinimalDaysInFirstWeek(1);
743 c->getTime(status);
744 verify765("1997 Tuesday in week 0 of June = ", status);
745
746 /* Note: The following test used to expect YEAR 1997, WOY 1 to
747 * resolve to a date in Dec 1996; that is, to behave as if
748 * YEAR_WOY were 1997. With the addition of a new explicit
749 * YEAR_WOY field, YEAR_WOY must itself be set if that is what is
750 * desired. Using YEAR in combination with WOY is ambiguous, and
751 * results in the first WOY/DOW day of the year satisfying the
752 * given fields (there may be up to two such days). In this case,
753 * it propertly resolves to Tue Dec 30 1997, which has a WOY value
754 * of 1 (for YEAR_WOY 1998) and a DOW of Tuesday, and falls in the
755 * _calendar_ year 1997, as specified. - aliu */
756 c->clear();
757 c->set(UCAL_YEAR_WOY, 1997); // aliu
758 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
759 c->set(UCAL_WEEK_OF_YEAR, 1);
760 verify765("1997 Tuesday in week 1 of yearWOY = ", c, 1996, UCAL_DECEMBER, 31);
761 c->clear(); // - add test for YEAR
762 c->setMinimalDaysInFirstWeek(1);
763 c->set(UCAL_YEAR, 1997);
764 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
765 c->set(UCAL_WEEK_OF_YEAR, 1);
766 verify765("1997 Tuesday in week 1 of year = ", c, 1997, UCAL_DECEMBER, 30);
767 c->clear();
768 c->set(UCAL_YEAR, 1997);
769 c->set(UCAL_DAY_OF_WEEK, UCAL_TUESDAY);
770 c->set(UCAL_WEEK_OF_YEAR, 10);
771 verify765("1997 Tuesday in week 10 of year = ", c, 1997, UCAL_MARCH, 4);
772 //try {
773
774 // {sfb} week 0 is no longer a valid week of year
775 /*c->clear();
776 c->set(Calendar::YEAR, 1997);
777 c->set(Calendar::DAY_OF_WEEK, Calendar::TUESDAY);
778 //c->set(Calendar::WEEK_OF_YEAR, 0);
779 c->set(Calendar::WEEK_OF_YEAR, 1);
780 verify765("1997 Tuesday in week 0 of year = ", c, 1996, Calendar::DECEMBER, 24);*/
781
782 //}
783 //catch(IllegalArgumentException ex) {
784 // errln("FAIL: Exception seen:");
785 // ex.printStackTrace(log);
786 //}
787 delete c;
788 }
789
790 // -------------------------------------
791
792 void
793 CalendarTest::verify765(const UnicodeString& msg, Calendar* c, int32_t year, int32_t month, int32_t day)
794 {
795 UnicodeString str;
796 UErrorCode status = U_ZERO_ERROR;
797 int32_t y = c->get(UCAL_YEAR, status);
798 int32_t m = c->get(UCAL_MONTH, status);
799 int32_t d = c->get(UCAL_DATE, status);
800 if ( y == year &&
801 m == month &&
802 d == day) {
803 if (U_FAILURE(status)) { errln("FAIL: Calendar::get failed"); return; }
804 logln("PASS: " + msg + dateToString(c->getTime(status), str));
805 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
806 }
807 else {
808 errln("FAIL: " + msg + dateToString(c->getTime(status), str) + "; expected " + (int32_t)year + "/" + (int32_t)(month + 1) + "/" + (int32_t)day +
809 "; got " + (int32_t)y + "/" + (int32_t)(m + 1) + "/" + (int32_t)d + " for Locale: " + c->getLocaleID(ULOC_ACTUAL_LOCALE,status));
810 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
811 }
812 }
813
814 // -------------------------------------
815
816 void
817 CalendarTest::verify765(const UnicodeString& msg/*, IllegalArgumentException e*/, UErrorCode status)
818 {
819 if (status != U_ILLEGAL_ARGUMENT_ERROR) errln("FAIL: No IllegalArgumentException for " + msg);
820 else logln("PASS: " + msg + "IllegalArgument as expected");
821 }
822
823 // -------------------------------------
824
825 /**
826 * Confirm that the offset between local time and GMT behaves as expected.
827 */
828 void
829 CalendarTest::TestGMTvsLocal4064654()
830 {
831 test4064654(1997, 1, 1, 12, 0, 0);
832 test4064654(1997, 4, 16, 18, 30, 0);
833 }
834
835 // -------------------------------------
836
837 void
838 CalendarTest::test4064654(int32_t yr, int32_t mo, int32_t dt, int32_t hr, int32_t mn, int32_t sc)
839 {
840 UDate date;
841 UErrorCode status = U_ZERO_ERROR;
842 UnicodeString str;
843 Calendar *gmtcal = Calendar::createInstance(status);
844 if (failure(status, "Calendar::createInstance", TRUE)) return;
845 gmtcal->adoptTimeZone(TimeZone::createTimeZone("Africa/Casablanca"));
846 gmtcal->set(yr, mo - 1, dt, hr, mn, sc);
847 gmtcal->set(UCAL_MILLISECOND, 0);
848 date = gmtcal->getTime(status);
849 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
850 logln("date = " + dateToString(date, str));
851 Calendar *cal = Calendar::createInstance(status);
852 if (U_FAILURE(status)) { errln("Calendar::createInstance failed"); return; }
853 cal->setTime(date, status);
854 if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
855 int32_t offset = cal->getTimeZone().getOffset((uint8_t)cal->get(UCAL_ERA, status),
856 cal->get(UCAL_YEAR, status),
857 cal->get(UCAL_MONTH, status),
858 cal->get(UCAL_DATE, status),
859 (uint8_t)cal->get(UCAL_DAY_OF_WEEK, status),
860 cal->get(UCAL_MILLISECOND, status), status);
861 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
862 logln("offset for " + dateToString(date, str) + "= " + (offset / 1000 / 60 / 60.0) + "hr");
863 int32_t utc = ((cal->get(UCAL_HOUR_OF_DAY, status) * 60 +
864 cal->get(UCAL_MINUTE, status)) * 60 +
865 cal->get(UCAL_SECOND, status)) * 1000 +
866 cal->get(UCAL_MILLISECOND, status) - offset;
867 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
868 int32_t expected = ((hr * 60 + mn) * 60 + sc) * 1000;
869 if (utc != expected) errln(UnicodeString("FAIL: Discrepancy of ") + (utc - expected) +
870 " millis = " + ((utc - expected) / 1000 / 60 / 60.0) + " hr");
871 delete gmtcal;
872 delete cal;
873 }
874
875 // -------------------------------------
876
877 /**
878 * The operations of adding and setting should not exhibit pathological
879 * dependence on the order of operations. This test checks for this.
880 */
881 void
882 CalendarTest::TestAddSetOrder621()
883 {
884 UDate d = date(97, 4, 14, 13, 23, 45);
885 UErrorCode status = U_ZERO_ERROR;
886 Calendar *cal = Calendar::createInstance(status);
887 if (failure(status, "Calendar::createInstance", TRUE)) return;
888
889 cal->setTime(d, status);
890 if (U_FAILURE(status)) {
891 errln("Calendar::setTime failed");
892 delete cal;
893 return;
894 }
895 cal->add(UCAL_DATE, - 5, status);
896 if (U_FAILURE(status)) {
897 errln("Calendar::add failed");
898 delete cal;
899 return;
900 }
901 cal->set(UCAL_HOUR_OF_DAY, 0);
902 cal->set(UCAL_MINUTE, 0);
903 cal->set(UCAL_SECOND, 0);
904 UnicodeString s;
905 dateToString(cal->getTime(status), s);
906 if (U_FAILURE(status)) {
907 errln("Calendar::getTime failed");
908 delete cal;
909 return;
910 }
911 delete cal;
912
913 cal = Calendar::createInstance(status);
914 if (U_FAILURE(status)) {
915 errln("Calendar::createInstance failed");
916 delete cal;
917 return;
918 }
919 cal->setTime(d, status);
920 if (U_FAILURE(status)) {
921 errln("Calendar::setTime failed");
922 delete cal;
923 return;
924 }
925 cal->set(UCAL_HOUR_OF_DAY, 0);
926 cal->set(UCAL_MINUTE, 0);
927 cal->set(UCAL_SECOND, 0);
928 cal->add(UCAL_DATE, - 5, status);
929 if (U_FAILURE(status)) {
930 errln("Calendar::add failed");
931 delete cal;
932 return;
933 }
934 UnicodeString s2;
935 dateToString(cal->getTime(status), s2);
936 if (U_FAILURE(status)) {
937 errln("Calendar::getTime failed");
938 delete cal;
939 return;
940 }
941 if (s == s2)
942 logln("Pass: " + s + " == " + s2);
943 else
944 errln("FAIL: " + s + " != " + s2);
945 delete cal;
946 }
947
948 // -------------------------------------
949
950 /**
951 * Confirm that adding to various fields works.
952 */
953 void
954 CalendarTest::TestAdd520()
955 {
956 int32_t y = 1997, m = UCAL_FEBRUARY, d = 1;
957 UErrorCode status = U_ZERO_ERROR;
958 GregorianCalendar *temp = new GregorianCalendar(y, m, d, status);
959 if (failure(status, "new GregorianCalendar", TRUE)) return;
960 check520(temp, y, m, d);
961 temp->add(UCAL_YEAR, 1, status);
962 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
963 y++;
964 check520(temp, y, m, d);
965 temp->add(UCAL_MONTH, 1, status);
966 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
967 m++;
968 check520(temp, y, m, d);
969 temp->add(UCAL_DATE, 1, status);
970 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
971 d++;
972 check520(temp, y, m, d);
973 temp->add(UCAL_DATE, 2, status);
974 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
975 d += 2;
976 check520(temp, y, m, d);
977 temp->add(UCAL_DATE, 28, status);
978 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
979 d = 1;++m;
980 check520(temp, y, m, d);
981 delete temp;
982 }
983
984 // -------------------------------------
985
986 /**
987 * Execute adding and rolling in GregorianCalendar extensively,
988 */
989 void
990 CalendarTest::TestAddRollExtensive()
991 {
992 int32_t maxlimit = 40;
993 int32_t y = 1997, m = UCAL_FEBRUARY, d = 1, hr = 1, min = 1, sec = 0, ms = 0;
994 UErrorCode status = U_ZERO_ERROR;
995 GregorianCalendar *temp = new GregorianCalendar(y, m, d, status);
996 if (failure(status, "new GregorianCalendar", TRUE)) return;
997
998 temp->set(UCAL_HOUR, hr);
999 temp->set(UCAL_MINUTE, min);
1000 temp->set(UCAL_SECOND, sec);
1001 temp->set(UCAL_MILLISECOND, ms);
1002 temp->setMinimalDaysInFirstWeek(1);
1003
1004 UCalendarDateFields e;
1005
1006 logln("Testing GregorianCalendar add...");
1007 e = UCAL_YEAR;
1008 while (e < UCAL_FIELD_COUNT) {
1009 int32_t i;
1010 int32_t limit = maxlimit;
1011 status = U_ZERO_ERROR;
1012 for (i = 0; i < limit; i++) {
1013 temp->add(e, 1, status);
1014 if (U_FAILURE(status)) { limit = i; status = U_ZERO_ERROR; }
1015 }
1016 for (i = 0; i < limit; i++) {
1017 temp->add(e, -1, status);
1018 if (U_FAILURE(status)) { errln("GregorianCalendar::add -1 failed"); return; }
1019 }
1020 check520(temp, y, m, d, hr, min, sec, ms, e);
1021
1022 e = (UCalendarDateFields) ((int32_t) e + 1);
1023 }
1024
1025 logln("Testing GregorianCalendar roll...");
1026 e = UCAL_YEAR;
1027 while (e < UCAL_FIELD_COUNT) {
1028 int32_t i;
1029 int32_t limit = maxlimit;
1030 status = U_ZERO_ERROR;
1031 for (i = 0; i < limit; i++) {
1032 logln(calToStr(*temp) + UnicodeString(" " ) + fieldName(e) + UnicodeString("++") );
1033 temp->roll(e, 1, status);
1034 if (U_FAILURE(status)) {
1035 logln("caltest.cpp:%d e=%d, i=%d - roll(+) err %s\n", __LINE__, (int) e, (int) i, u_errorName(status));
1036 logln(calToStr(*temp));
1037 limit = i; status = U_ZERO_ERROR;
1038 }
1039 }
1040 for (i = 0; i < limit; i++) {
1041 logln("caltest.cpp:%d e=%d, i=%d\n", __LINE__, (int) e, (int) i);
1042 logln(calToStr(*temp) + UnicodeString(" " ) + fieldName(e) + UnicodeString("--") );
1043 temp->roll(e, -1, status);
1044 if (U_FAILURE(status)) { errln(UnicodeString("GregorianCalendar::roll ") + CalendarTest::fieldName(e) + " count=" + UnicodeString('@'+i) + " by -1 failed with " + u_errorName(status) ); return; }
1045 }
1046 check520(temp, y, m, d, hr, min, sec, ms, e);
1047
1048 e = (UCalendarDateFields) ((int32_t) e + 1);
1049 }
1050
1051 delete temp;
1052 }
1053
1054 // -------------------------------------
1055 void
1056 CalendarTest::check520(Calendar* c,
1057 int32_t y, int32_t m, int32_t d,
1058 int32_t hr, int32_t min, int32_t sec,
1059 int32_t ms, UCalendarDateFields field)
1060
1061 {
1062 UErrorCode status = U_ZERO_ERROR;
1063 if (c->get(UCAL_YEAR, status) != y ||
1064 c->get(UCAL_MONTH, status) != m ||
1065 c->get(UCAL_DATE, status) != d ||
1066 c->get(UCAL_HOUR, status) != hr ||
1067 c->get(UCAL_MINUTE, status) != min ||
1068 c->get(UCAL_SECOND, status) != sec ||
1069 c->get(UCAL_MILLISECOND, status) != ms) {
1070 errln(UnicodeString("U_FAILURE for field ") + (int32_t)field +
1071 ": Expected y/m/d h:m:s:ms of " +
1072 y + "/" + (m + 1) + "/" + d + " " +
1073 hr + ":" + min + ":" + sec + ":" + ms +
1074 "; got " + c->get(UCAL_YEAR, status) +
1075 "/" + (c->get(UCAL_MONTH, status) + 1) +
1076 "/" + c->get(UCAL_DATE, status) +
1077 " " + c->get(UCAL_HOUR, status) + ":" +
1078 c->get(UCAL_MINUTE, status) + ":" +
1079 c->get(UCAL_SECOND, status) + ":" +
1080 c->get(UCAL_MILLISECOND, status)
1081 );
1082
1083 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1084 }
1085 else
1086 logln(UnicodeString("Confirmed: ") + y + "/" +
1087 (m + 1) + "/" + d + " " +
1088 hr + ":" + min + ":" + sec + ":" + ms);
1089 }
1090
1091 // -------------------------------------
1092 void
1093 CalendarTest::check520(Calendar* c,
1094 int32_t y, int32_t m, int32_t d)
1095
1096 {
1097 UErrorCode status = U_ZERO_ERROR;
1098 if (c->get(UCAL_YEAR, status) != y ||
1099 c->get(UCAL_MONTH, status) != m ||
1100 c->get(UCAL_DATE, status) != d) {
1101 errln(UnicodeString("FAILURE: Expected y/m/d of ") +
1102 y + "/" + (m + 1) + "/" + d + " " +
1103 "; got " + c->get(UCAL_YEAR, status) +
1104 "/" + (c->get(UCAL_MONTH, status) + 1) +
1105 "/" + c->get(UCAL_DATE, status)
1106 );
1107
1108 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1109 }
1110 else
1111 logln(UnicodeString("Confirmed: ") + y + "/" +
1112 (m + 1) + "/" + d);
1113 }
1114
1115 // -------------------------------------
1116
1117 /**
1118 * Test that setting of fields works. In particular, make sure that all instances
1119 * of GregorianCalendar don't share a static instance of the fields array.
1120 */
1121 void
1122 CalendarTest::TestFieldSet4781()
1123 {
1124 // try {
1125 UErrorCode status = U_ZERO_ERROR;
1126 GregorianCalendar *g = new GregorianCalendar(status);
1127 if (failure(status, "new GregorianCalendar", TRUE)) return;
1128 GregorianCalendar *g2 = new GregorianCalendar(status);
1129 if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
1130 g2->set(UCAL_HOUR, 12, status);
1131 g2->set(UCAL_MINUTE, 0, status);
1132 g2->set(UCAL_SECOND, 0, status);
1133 if (U_FAILURE(status)) { errln("Calendar::set failed"); return; }
1134 if (*g == *g2) logln("Same");
1135 else logln("Different");
1136 //}
1137 //catch(IllegalArgumentException e) {
1138 //errln("Unexpected exception seen: " + e);
1139 //}
1140 delete g;
1141 delete g2;
1142 }
1143
1144 // -------------------------------------
1145
1146 /* We don't support serialization on C++
1147 void
1148 CalendarTest::TestSerialize337()
1149 {
1150 Calendar cal = Calendar::getInstance();
1151 UBool ok = FALSE;
1152 try {
1153 FileOutputStream f = new FileOutputStream(FILENAME);
1154 ObjectOutput s = new ObjectOutputStream(f);
1155 s.writeObject(PREFIX);
1156 s.writeObject(cal);
1157 s.writeObject(POSTFIX);
1158 f.close();
1159 FileInputStream in = new FileInputStream(FILENAME);
1160 ObjectInputStream t = new ObjectInputStream(in);
1161 UnicodeString& pre = (UnicodeString&) t.readObject();
1162 Calendar c = (Calendar) t.readObject();
1163 UnicodeString& post = (UnicodeString&) t.readObject();
1164 in.close();
1165 ok = pre.equals(PREFIX) &&
1166 post.equals(POSTFIX) &&
1167 cal->equals(c);
1168 File fl = new File(FILENAME);
1169 fl.delete();
1170 }
1171 catch(IOException e) {
1172 errln("FAIL: Exception received:");
1173 e.printStackTrace(log);
1174 }
1175 catch(ClassNotFoundException e) {
1176 errln("FAIL: Exception received:");
1177 e.printStackTrace(log);
1178 }
1179 if (!ok) errln("Serialization of Calendar object failed.");
1180 }
1181
1182 UnicodeString& CalendarTest::PREFIX = "abc";
1183
1184 UnicodeString& CalendarTest::POSTFIX = "def";
1185
1186 UnicodeString& CalendarTest::FILENAME = "tmp337.bin";
1187 */
1188
1189 // -------------------------------------
1190
1191 /**
1192 * Verify that the seconds of a Calendar can be zeroed out through the
1193 * expected sequence of operations.
1194 */
1195 void
1196 CalendarTest::TestSecondsZero121()
1197 {
1198 UErrorCode status = U_ZERO_ERROR;
1199 Calendar *cal = new GregorianCalendar(status);
1200 if (failure(status, "new GregorianCalendar", TRUE)) return;
1201 cal->setTime(Calendar::getNow(), status);
1202 if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
1203 cal->set(UCAL_SECOND, 0);
1204 if (U_FAILURE(status)) { errln("Calendar::set failed"); return; }
1205 UDate d = cal->getTime(status);
1206 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
1207 UnicodeString s;
1208 dateToString(d, s);
1209 if (s.indexOf("DATE_FORMAT_FAILURE") >= 0) {
1210 dataerrln("Got: \"DATE_FORMAT_FAILURE\".");
1211 } else if (s.indexOf(":00 ") < 0) {
1212 errln("Expected to see :00 in " + s);
1213 }
1214 delete cal;
1215 }
1216
1217 // -------------------------------------
1218
1219 /**
1220 * Verify that a specific sequence of adding and setting works as expected;
1221 * it should not vary depending on when and whether the get method is
1222 * called.
1223 */
1224 void
1225 CalendarTest::TestAddSetGet0610()
1226 {
1227 UnicodeString EXPECTED_0610("1993/0/5", "");
1228 UErrorCode status = U_ZERO_ERROR;
1229 {
1230 Calendar *calendar = new GregorianCalendar(status);
1231 if (failure(status, "new GregorianCalendar", TRUE)) return;
1232 calendar->set(1993, UCAL_JANUARY, 4);
1233 logln("1A) " + value(calendar));
1234 calendar->add(UCAL_DATE, 1, status);
1235 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
1236 UnicodeString v = value(calendar);
1237 logln("1B) " + v);
1238 logln("--) 1993/0/5");
1239 if (!(v == EXPECTED_0610)) errln("Expected " + EXPECTED_0610 + "; saw " + v);
1240 delete calendar;
1241 }
1242 {
1243 Calendar *calendar = new GregorianCalendar(1993, UCAL_JANUARY, 4, status);
1244 if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
1245 logln("2A) " + value(calendar));
1246 calendar->add(UCAL_DATE, 1, status);
1247 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
1248 UnicodeString v = value(calendar);
1249 logln("2B) " + v);
1250 logln("--) 1993/0/5");
1251 if (!(v == EXPECTED_0610)) errln("Expected " + EXPECTED_0610 + "; saw " + v);
1252 delete calendar;
1253 }
1254 {
1255 Calendar *calendar = new GregorianCalendar(1993, UCAL_JANUARY, 4, status);
1256 if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
1257 logln("3A) " + value(calendar));
1258 calendar->getTime(status);
1259 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
1260 calendar->add(UCAL_DATE, 1, status);
1261 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
1262 UnicodeString v = value(calendar);
1263 logln("3B) " + v);
1264 logln("--) 1993/0/5");
1265 if (!(v == EXPECTED_0610)) errln("Expected " + EXPECTED_0610 + "; saw " + v);
1266 delete calendar;
1267 }
1268 }
1269
1270 // -------------------------------------
1271
1272 UnicodeString
1273 CalendarTest::value(Calendar* calendar)
1274 {
1275 UErrorCode status = U_ZERO_ERROR;
1276 return UnicodeString("") + (int32_t)calendar->get(UCAL_YEAR, status) +
1277 "/" + (int32_t)calendar->get(UCAL_MONTH, status) +
1278 "/" + (int32_t)calendar->get(UCAL_DATE, status) +
1279 (U_FAILURE(status) ? " FAIL: Calendar::get failed" : "");
1280 }
1281
1282
1283 // -------------------------------------
1284
1285 /**
1286 * Verify that various fields on a known date are set correctly.
1287 */
1288 void
1289 CalendarTest::TestFields060()
1290 {
1291 UErrorCode status = U_ZERO_ERROR;
1292 int32_t year = 1997;
1293 int32_t month = UCAL_OCTOBER;
1294 int32_t dDate = 22;
1295 GregorianCalendar *calendar = 0;
1296 calendar = new GregorianCalendar(year, month, dDate, status);
1297 if (failure(status, "new GregorianCalendar", TRUE)) return;
1298 for (int32_t i = 0; i < EXPECTED_FIELDS_length;) {
1299 UCalendarDateFields field = (UCalendarDateFields)EXPECTED_FIELDS[i++];
1300 int32_t expected = EXPECTED_FIELDS[i++];
1301 if (calendar->get(field, status) != expected) {
1302 errln(UnicodeString("Expected field ") + (int32_t)field + " to have value " + (int32_t)expected +
1303 "; received " + (int32_t)calendar->get(field, status) + " instead");
1304 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1305 }
1306 }
1307 delete calendar;
1308 }
1309
1310 int32_t CalendarTest::EXPECTED_FIELDS[] = {
1311 UCAL_YEAR, 1997,
1312 UCAL_MONTH, UCAL_OCTOBER,
1313 UCAL_DATE, 22,
1314 UCAL_DAY_OF_WEEK, UCAL_WEDNESDAY,
1315 UCAL_DAY_OF_WEEK_IN_MONTH, 4,
1316 UCAL_DAY_OF_YEAR, 295
1317 };
1318
1319 const int32_t CalendarTest::EXPECTED_FIELDS_length = (int32_t)(sizeof(CalendarTest::EXPECTED_FIELDS) /
1320 sizeof(CalendarTest::EXPECTED_FIELDS[0]));
1321
1322 // -------------------------------------
1323
1324 /**
1325 * Verify that various fields on a known date are set correctly. In this
1326 * case, the start of the epoch (January 1 1970).
1327 */
1328 void
1329 CalendarTest::TestEpochStartFields()
1330 {
1331 UErrorCode status = U_ZERO_ERROR;
1332 TimeZone *z = TimeZone::createDefault();
1333 Calendar *c = Calendar::createInstance(status);
1334 if (failure(status, "Calendar::createInstance", TRUE)) return;
1335 UDate d = - z->getRawOffset();
1336 GregorianCalendar *gc = new GregorianCalendar(status);
1337 if (U_FAILURE(status)) { errln("Couldn't create GregorianCalendar"); return; }
1338 gc->setTimeZone(*z);
1339 gc->setTime(d, status);
1340 if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
1341 UBool idt = gc->inDaylightTime(status);
1342 if (U_FAILURE(status)) { errln("GregorianCalendar::inDaylightTime failed"); return; }
1343 if (idt) {
1344 UnicodeString str;
1345 logln("Warning: Skipping test because " + dateToString(d, str) + " is in DST.");
1346 }
1347 else {
1348 c->setTime(d, status);
1349 if (U_FAILURE(status)) { errln("Calendar::setTime failed"); return; }
1350 for (int32_t i = 0; i < UCAL_ZONE_OFFSET;++i) {
1351 if (c->get((UCalendarDateFields)i, status) != EPOCH_FIELDS[i])
1352 dataerrln(UnicodeString("Expected field ") + i + " to have value " + EPOCH_FIELDS[i] +
1353 "; saw " + c->get((UCalendarDateFields)i, status) + " instead");
1354 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1355 }
1356 if (c->get(UCAL_ZONE_OFFSET, status) != z->getRawOffset())
1357 {
1358 errln(UnicodeString("Expected field ZONE_OFFSET to have value ") + z->getRawOffset() +
1359 "; saw " + c->get(UCAL_ZONE_OFFSET, status) + " instead");
1360 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1361 }
1362 if (c->get(UCAL_DST_OFFSET, status) != 0)
1363 {
1364 errln(UnicodeString("Expected field DST_OFFSET to have value 0") +
1365 "; saw " + c->get(UCAL_DST_OFFSET, status) + " instead");
1366 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1367 }
1368 }
1369 delete c;
1370 delete z;
1371 delete gc;
1372 }
1373
1374 int32_t CalendarTest::EPOCH_FIELDS[] = {
1375 1, 1970, 0, 1, 1, 1, 1, 5, 1, 0, 0, 0, 0, 0, 0, - 28800000, 0
1376 };
1377
1378 // -------------------------------------
1379
1380 /**
1381 * Test that the days of the week progress properly when add is called repeatedly
1382 * for increments of 24 days.
1383 */
1384 void
1385 CalendarTest::TestDOWProgression()
1386 {
1387 UErrorCode status = U_ZERO_ERROR;
1388 Calendar *cal = new GregorianCalendar(1972, UCAL_OCTOBER, 26, status);
1389 if (failure(status, "new GregorianCalendar", TRUE)) return;
1390 marchByDelta(cal, 24);
1391 delete cal;
1392 }
1393
1394 // -------------------------------------
1395
1396 void
1397 CalendarTest::TestDOW_LOCALandYEAR_WOY()
1398 {
1399 /* Note: I've commented out the loop_addroll tests for YEAR and
1400 * YEAR_WOY below because these two fields should NOT behave
1401 * identically when adding. YEAR should keep the month/dom
1402 * invariant. YEAR_WOY should keep the woy/dow invariant. I've
1403 * added a new test that checks for this in place of the old call
1404 * to loop_addroll. - aliu */
1405 UErrorCode status = U_ZERO_ERROR;
1406 int32_t times = 20;
1407 Calendar *cal=Calendar::createInstance(Locale::getGermany(), status);
1408 if (failure(status, "Calendar::createInstance", TRUE)) return;
1409 SimpleDateFormat *sdf=new SimpleDateFormat(UnicodeString("YYYY'-W'ww-ee"), Locale::getGermany(), status);
1410 if (U_FAILURE(status)) { dataerrln("Couldn't create SimpleDateFormat - %s", u_errorName(status)); return; }
1411
1412 // ICU no longer use localized date-time pattern characters by default.
1413 // So we set pattern chars using 'J' instead of 'Y'.
1414 DateFormatSymbols *dfs = new DateFormatSymbols(Locale::getGermany(), status);
1415 dfs->setLocalPatternChars(UnicodeString("GyMdkHmsSEDFwWahKzJeugAZvcLQq"));
1416 sdf->adoptDateFormatSymbols(dfs);
1417 sdf->applyLocalizedPattern(UnicodeString("JJJJ'-W'ww-ee"), status);
1418 if (U_FAILURE(status)) { errln("Couldn't apply localized pattern"); return; }
1419
1420 cal->clear();
1421 cal->set(1997, UCAL_DECEMBER, 25);
1422 doYEAR_WOYLoop(cal, sdf, times, status);
1423 //loop_addroll(cal, /*sdf,*/ times, UCAL_YEAR_WOY, UCAL_YEAR, status);
1424 yearAddTest(*cal, status); // aliu
1425 loop_addroll(cal, /*sdf,*/ times, UCAL_DOW_LOCAL, UCAL_DAY_OF_WEEK, status);
1426 if (U_FAILURE(status)) { errln("Error in parse/calculate test for 1997"); return; }
1427
1428 cal->clear();
1429 cal->set(1998, UCAL_DECEMBER, 25);
1430 doYEAR_WOYLoop(cal, sdf, times, status);
1431 //loop_addroll(cal, /*sdf,*/ times, UCAL_YEAR_WOY, UCAL_YEAR, status);
1432 yearAddTest(*cal, status); // aliu
1433 loop_addroll(cal, /*sdf,*/ times, UCAL_DOW_LOCAL, UCAL_DAY_OF_WEEK, status);
1434 if (U_FAILURE(status)) { errln("Error in parse/calculate test for 1998"); return; }
1435
1436 cal->clear();
1437 cal->set(1582, UCAL_OCTOBER, 1);
1438 doYEAR_WOYLoop(cal, sdf, times, status);
1439 //loop_addroll(cal, /*sdf,*/ times, Calendar::YEAR_WOY, Calendar::YEAR, status);
1440 yearAddTest(*cal, status); // aliu
1441 loop_addroll(cal, /*sdf,*/ times, UCAL_DOW_LOCAL, UCAL_DAY_OF_WEEK, status);
1442 if (U_FAILURE(status)) { errln("Error in parse/calculate test for 1582"); return; }
1443 delete sdf;
1444 delete cal;
1445
1446 return;
1447 }
1448
1449 /**
1450 * Confirm that adding a YEAR and adding a YEAR_WOY work properly for
1451 * the given Calendar at its current setting.
1452 */
1453 void CalendarTest::yearAddTest(Calendar& cal, UErrorCode& status) {
1454 /**
1455 * When adding the YEAR, the month and day should remain constant.
1456 * When adding the YEAR_WOY, the WOY and DOW should remain constant. - aliu
1457 * Examples:
1458 * Wed Jan 14 1998 / 1998-W03-03 Add(YEAR_WOY, 1) -> Wed Jan 20 1999 / 1999-W03-03
1459 * Add(YEAR, 1) -> Thu Jan 14 1999 / 1999-W02-04
1460 * Thu Jan 14 1999 / 1999-W02-04 Add(YEAR_WOY, 1) -> Thu Jan 13 2000 / 2000-W02-04
1461 * Add(YEAR, 1) -> Fri Jan 14 2000 / 2000-W02-05
1462 * Sun Oct 31 1582 / 1582-W42-07 Add(YEAR_WOY, 1) -> Sun Oct 23 1583 / 1583-W42-07
1463 * Add(YEAR, 1) -> Mon Oct 31 1583 / 1583-W44-01
1464 */
1465 int32_t y = cal.get(UCAL_YEAR, status);
1466 int32_t mon = cal.get(UCAL_MONTH, status);
1467 int32_t day = cal.get(UCAL_DATE, status);
1468 int32_t ywy = cal.get(UCAL_YEAR_WOY, status);
1469 int32_t woy = cal.get(UCAL_WEEK_OF_YEAR, status);
1470 int32_t dow = cal.get(UCAL_DOW_LOCAL, status);
1471 UDate t = cal.getTime(status);
1472
1473 if(U_FAILURE(status)){
1474 errln(UnicodeString("Failed to create Calendar for locale. Error: ") + UnicodeString(u_errorName(status)));
1475 return;
1476 }
1477 UnicodeString str, str2;
1478 SimpleDateFormat fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"), status);
1479 fmt.setCalendar(cal);
1480
1481 fmt.format(t, str.remove());
1482 str += ".add(YEAR, 1) =>";
1483 cal.add(UCAL_YEAR, 1, status);
1484 int32_t y2 = cal.get(UCAL_YEAR, status);
1485 int32_t mon2 = cal.get(UCAL_MONTH, status);
1486 int32_t day2 = cal.get(UCAL_DATE, status);
1487 fmt.format(cal.getTime(status), str);
1488 if (y2 != (y+1) || mon2 != mon || day2 != day) {
1489 str += (UnicodeString)", expected year " +
1490 (y+1) + ", month " + (mon+1) + ", day " + day;
1491 errln((UnicodeString)"FAIL: " + str);
1492 logln( UnicodeString(" -> ") + CalendarTest::calToStr(cal) );
1493 } else {
1494 logln(str);
1495 }
1496
1497 fmt.format(t, str.remove());
1498 str += ".add(YEAR_WOY, 1)=>";
1499 cal.setTime(t, status);
1500 logln( UnicodeString(" <- ") + CalendarTest::calToStr(cal) );
1501 cal.add(UCAL_YEAR_WOY, 1, status);
1502 int32_t ywy2 = cal.get(UCAL_YEAR_WOY, status);
1503 int32_t woy2 = cal.get(UCAL_WEEK_OF_YEAR, status);
1504 int32_t dow2 = cal.get(UCAL_DOW_LOCAL, status);
1505 fmt.format(cal.getTime(status), str);
1506 if (ywy2 != (ywy+1) || woy2 != woy || dow2 != dow) {
1507 str += (UnicodeString)", expected yearWOY " +
1508 (ywy+1) + ", woy " + woy + ", dowLocal " + dow;
1509 errln((UnicodeString)"FAIL: " + str);
1510 logln( UnicodeString(" -> ") + CalendarTest::calToStr(cal) );
1511 } else {
1512 logln(str);
1513 }
1514 }
1515
1516 // -------------------------------------
1517
1518 void CalendarTest::loop_addroll(Calendar *cal, /*SimpleDateFormat *sdf,*/ int times, UCalendarDateFields field, UCalendarDateFields field2, UErrorCode& errorCode) {
1519 Calendar *calclone;
1520 SimpleDateFormat fmt(UnicodeString("EEE MMM dd yyyy / YYYY'-W'ww-ee"), errorCode);
1521 fmt.setCalendar(*cal);
1522 int i;
1523
1524 for(i = 0; i<times; i++) {
1525 calclone = cal->clone();
1526 UDate start = cal->getTime(errorCode);
1527 cal->add(field,1,errorCode);
1528 if (U_FAILURE(errorCode)) { errln("Error in add"); delete calclone; return; }
1529 calclone->add(field2,1,errorCode);
1530 if (U_FAILURE(errorCode)) { errln("Error in add"); delete calclone; return; }
1531 if(cal->getTime(errorCode) != calclone->getTime(errorCode)) {
1532 UnicodeString str("FAIL: Results of add differ. "), str2;
1533 str += fmt.format(start, str2) + " ";
1534 str += UnicodeString("Add(") + fieldName(field) + ", 1) -> " +
1535 fmt.format(cal->getTime(errorCode), str2.remove()) + "; ";
1536 str += UnicodeString("Add(") + fieldName(field2) + ", 1) -> " +
1537 fmt.format(calclone->getTime(errorCode), str2.remove());
1538 errln(str);
1539 delete calclone;
1540 return;
1541 }
1542 delete calclone;
1543 }
1544
1545 for(i = 0; i<times; i++) {
1546 calclone = cal->clone();
1547 cal->roll(field,(int32_t)1,errorCode);
1548 if (U_FAILURE(errorCode)) { errln("Error in roll"); delete calclone; return; }
1549 calclone->roll(field2,(int32_t)1,errorCode);
1550 if (U_FAILURE(errorCode)) { errln("Error in roll"); delete calclone; return; }
1551 if(cal->getTime(errorCode) != calclone->getTime(errorCode)) {
1552 delete calclone;
1553 errln("Results of roll differ!");
1554 return;
1555 }
1556 delete calclone;
1557 }
1558 }
1559
1560 // -------------------------------------
1561
1562 void
1563 CalendarTest::doYEAR_WOYLoop(Calendar *cal, SimpleDateFormat *sdf,
1564 int32_t times, UErrorCode& errorCode) {
1565
1566 UnicodeString us;
1567 UDate tst, original;
1568 Calendar *tstres = new GregorianCalendar(Locale::getGermany(), errorCode);
1569 for(int i=0; i<times; ++i) {
1570 sdf->format(Formattable(cal->getTime(errorCode),Formattable::kIsDate), us, errorCode);
1571 //logln("expected: "+us);
1572 if (U_FAILURE(errorCode)) { errln("Format error"); return; }
1573 tst=sdf->parse(us,errorCode);
1574 if (U_FAILURE(errorCode)) { errln("Parse error"); return; }
1575 tstres->clear();
1576 tstres->setTime(tst, errorCode);
1577 //logln((UnicodeString)"Parsed week of year is "+tstres->get(UCAL_WEEK_OF_YEAR, errorCode));
1578 if (U_FAILURE(errorCode)) { errln("Set time error"); return; }
1579 original = cal->getTime(errorCode);
1580 us.remove();
1581 sdf->format(Formattable(tst,Formattable::kIsDate), us, errorCode);
1582 //logln("got: "+us);
1583 if (U_FAILURE(errorCode)) { errln("Get time error"); return; }
1584 if(original!=tst) {
1585 us.remove();
1586 sdf->format(Formattable(original, Formattable::kIsDate), us, errorCode);
1587 errln("FAIL: Parsed time doesn't match with regular");
1588 logln("expected "+us + " " + calToStr(*cal));
1589 us.remove();
1590 sdf->format(Formattable(tst, Formattable::kIsDate), us, errorCode);
1591 logln("got "+us + " " + calToStr(*tstres));
1592 }
1593 tstres->clear();
1594 tstres->set(UCAL_YEAR_WOY, cal->get(UCAL_YEAR_WOY, errorCode));
1595 tstres->set(UCAL_WEEK_OF_YEAR, cal->get(UCAL_WEEK_OF_YEAR, errorCode));
1596 tstres->set(UCAL_DOW_LOCAL, cal->get(UCAL_DOW_LOCAL, errorCode));
1597 if(cal->get(UCAL_YEAR, errorCode) != tstres->get(UCAL_YEAR, errorCode)) {
1598 errln("FAIL: Different Year!");
1599 logln((UnicodeString)"Expected "+cal->get(UCAL_YEAR, errorCode));
1600 logln((UnicodeString)"Got "+tstres->get(UCAL_YEAR, errorCode));
1601 return;
1602 }
1603 if(cal->get(UCAL_DAY_OF_YEAR, errorCode) != tstres->get(UCAL_DAY_OF_YEAR, errorCode)) {
1604 errln("FAIL: Different Day Of Year!");
1605 logln((UnicodeString)"Expected "+cal->get(UCAL_DAY_OF_YEAR, errorCode));
1606 logln((UnicodeString)"Got "+tstres->get(UCAL_DAY_OF_YEAR, errorCode));
1607 return;
1608 }
1609 //logln(calToStr(*cal));
1610 cal->add(UCAL_DATE, 1, errorCode);
1611 if (U_FAILURE(errorCode)) { errln("Add error"); return; }
1612 us.remove();
1613 }
1614 delete (tstres);
1615 }
1616 // -------------------------------------
1617
1618 void
1619 CalendarTest::marchByDelta(Calendar* cal, int32_t delta)
1620 {
1621 UErrorCode status = U_ZERO_ERROR;
1622 Calendar *cur = (Calendar*) cal->clone();
1623 int32_t initialDOW = cur->get(UCAL_DAY_OF_WEEK, status);
1624 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1625 int32_t DOW, newDOW = initialDOW;
1626 do {
1627 UnicodeString str;
1628 DOW = newDOW;
1629 logln(UnicodeString("DOW = ") + DOW + " " + dateToString(cur->getTime(status), str));
1630 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
1631 cur->add(UCAL_DAY_OF_WEEK, delta, status);
1632 if (U_FAILURE(status)) { errln("Calendar::add failed"); return; }
1633 newDOW = cur->get(UCAL_DAY_OF_WEEK, status);
1634 if (U_FAILURE(status)) { errln("Calendar::get failed"); return; }
1635 int32_t expectedDOW = 1 + (DOW + delta - 1) % 7;
1636 if (newDOW != expectedDOW) {
1637 errln(UnicodeString("Day of week should be ") + expectedDOW + " instead of " + newDOW +
1638 " on " + dateToString(cur->getTime(status), str));
1639 if (U_FAILURE(status)) { errln("Calendar::getTime failed"); return; }
1640 return;
1641 }
1642 }
1643 while (newDOW != initialDOW);
1644 delete cur;
1645 }
1646
1647 #define CHECK(status, msg) \
1648 if (U_FAILURE(status)) { \
1649 errcheckln(status, msg); \
1650 return; \
1651 }
1652
1653 void CalendarTest::TestWOY(void) {
1654 /*
1655 FDW = Mon, MDFW = 4:
1656 Sun Dec 26 1999, WOY 51
1657 Mon Dec 27 1999, WOY 52
1658 Tue Dec 28 1999, WOY 52
1659 Wed Dec 29 1999, WOY 52
1660 Thu Dec 30 1999, WOY 52
1661 Fri Dec 31 1999, WOY 52
1662 Sat Jan 01 2000, WOY 52 ***
1663 Sun Jan 02 2000, WOY 52 ***
1664 Mon Jan 03 2000, WOY 1
1665 Tue Jan 04 2000, WOY 1
1666 Wed Jan 05 2000, WOY 1
1667 Thu Jan 06 2000, WOY 1
1668 Fri Jan 07 2000, WOY 1
1669 Sat Jan 08 2000, WOY 1
1670 Sun Jan 09 2000, WOY 1
1671 Mon Jan 10 2000, WOY 2
1672
1673 FDW = Mon, MDFW = 2:
1674 Sun Dec 26 1999, WOY 52
1675 Mon Dec 27 1999, WOY 1 ***
1676 Tue Dec 28 1999, WOY 1 ***
1677 Wed Dec 29 1999, WOY 1 ***
1678 Thu Dec 30 1999, WOY 1 ***
1679 Fri Dec 31 1999, WOY 1 ***
1680 Sat Jan 01 2000, WOY 1
1681 Sun Jan 02 2000, WOY 1
1682 Mon Jan 03 2000, WOY 2
1683 Tue Jan 04 2000, WOY 2
1684 Wed Jan 05 2000, WOY 2
1685 Thu Jan 06 2000, WOY 2
1686 Fri Jan 07 2000, WOY 2
1687 Sat Jan 08 2000, WOY 2
1688 Sun Jan 09 2000, WOY 2
1689 Mon Jan 10 2000, WOY 3
1690 */
1691
1692 UnicodeString str;
1693 UErrorCode status = U_ZERO_ERROR;
1694 int32_t i;
1695
1696 GregorianCalendar cal(status);
1697 SimpleDateFormat fmt(UnicodeString("EEE MMM dd yyyy', WOY' w"), status);
1698 if (failure(status, "Cannot construct calendar/format", TRUE)) return;
1699
1700 UCalendarDaysOfWeek fdw = (UCalendarDaysOfWeek) 0;
1701
1702 //for (int8_t pass=2; pass<=2; ++pass) {
1703 for (int8_t pass=1; pass<=2; ++pass) {
1704 switch (pass) {
1705 case 1:
1706 fdw = UCAL_MONDAY;
1707 cal.setFirstDayOfWeek(fdw);
1708 cal.setMinimalDaysInFirstWeek(4);
1709 fmt.adoptCalendar(cal.clone());
1710 break;
1711 case 2:
1712 fdw = UCAL_MONDAY;
1713 cal.setFirstDayOfWeek(fdw);
1714 cal.setMinimalDaysInFirstWeek(2);
1715 fmt.adoptCalendar(cal.clone());
1716 break;
1717 }
1718
1719 //for (i=2; i<=6; ++i) {
1720 for (i=0; i<16; ++i) {
1721 UDate t, t2;
1722 int32_t t_y, t_woy, t_dow;
1723 cal.clear();
1724 cal.set(1999, UCAL_DECEMBER, 26 + i);
1725 fmt.format(t = cal.getTime(status), str.remove());
1726 CHECK(status, "Fail: getTime failed");
1727 logln(UnicodeString("* ") + str);
1728 int32_t dow = cal.get(UCAL_DAY_OF_WEEK, status);
1729 int32_t woy = cal.get(UCAL_WEEK_OF_YEAR, status);
1730 int32_t year = cal.get(UCAL_YEAR, status);
1731 int32_t mon = cal.get(UCAL_MONTH, status);
1732 logln(calToStr(cal));
1733 CHECK(status, "Fail: get failed");
1734 int32_t dowLocal = dow - fdw;
1735 if (dowLocal < 0) dowLocal += 7;
1736 dowLocal++;
1737 int32_t yearWoy = year;
1738 if (mon == UCAL_JANUARY) {
1739 if (woy >= 52) --yearWoy;
1740 } else {
1741 if (woy == 1) ++yearWoy;
1742 }
1743
1744 // Basic fields->time check y/woy/dow
1745 // Since Y/WOY is ambiguous, we do a check of the fields,
1746 // not of the specific time.
1747 cal.clear();
1748 cal.set(UCAL_YEAR, year);
1749 cal.set(UCAL_WEEK_OF_YEAR, woy);
1750 cal.set(UCAL_DAY_OF_WEEK, dow);
1751 t_y = cal.get(UCAL_YEAR, status);
1752 t_woy = cal.get(UCAL_WEEK_OF_YEAR, status);
1753 t_dow = cal.get(UCAL_DAY_OF_WEEK, status);
1754 CHECK(status, "Fail: get failed");
1755 if (t_y != year || t_woy != woy || t_dow != dow) {
1756 str = "Fail: y/woy/dow fields->time => ";
1757 fmt.format(cal.getTime(status), str);
1758 errln(str);
1759 logln(calToStr(cal));
1760 logln("[get!=set] Y%d!=%d || woy%d!=%d || dow%d!=%d\n",
1761 t_y, year, t_woy, woy, t_dow, dow);
1762 } else {
1763 logln("y/woy/dow fields->time OK");
1764 }
1765
1766 // Basic fields->time check y/woy/dow_local
1767 // Since Y/WOY is ambiguous, we do a check of the fields,
1768 // not of the specific time.
1769 cal.clear();
1770 cal.set(UCAL_YEAR, year);
1771 cal.set(UCAL_WEEK_OF_YEAR, woy);
1772 cal.set(UCAL_DOW_LOCAL, dowLocal);
1773 t_y = cal.get(UCAL_YEAR, status);
1774 t_woy = cal.get(UCAL_WEEK_OF_YEAR, status);
1775 t_dow = cal.get(UCAL_DOW_LOCAL, status);
1776 CHECK(status, "Fail: get failed");
1777 if (t_y != year || t_woy != woy || t_dow != dowLocal) {
1778 str = "Fail: y/woy/dow_local fields->time => ";
1779 fmt.format(cal.getTime(status), str);
1780 errln(str);
1781 }
1782
1783 // Basic fields->time check y_woy/woy/dow
1784 cal.clear();
1785 cal.set(UCAL_YEAR_WOY, yearWoy);
1786 cal.set(UCAL_WEEK_OF_YEAR, woy);
1787 cal.set(UCAL_DAY_OF_WEEK, dow);
1788 t2 = cal.getTime(status);
1789 CHECK(status, "Fail: getTime failed");
1790 if (t != t2) {
1791 str = "Fail: y_woy/woy/dow fields->time => ";
1792 fmt.format(t2, str);
1793 errln(str);
1794 logln(calToStr(cal));
1795 logln("%.f != %.f\n", t, t2);
1796 } else {
1797 logln("y_woy/woy/dow OK");
1798 }
1799
1800 // Basic fields->time check y_woy/woy/dow_local
1801 cal.clear();
1802 cal.set(UCAL_YEAR_WOY, yearWoy);
1803 cal.set(UCAL_WEEK_OF_YEAR, woy);
1804 cal.set(UCAL_DOW_LOCAL, dowLocal);
1805 t2 = cal.getTime(status);
1806 CHECK(status, "Fail: getTime failed");
1807 if (t != t2) {
1808 str = "Fail: y_woy/woy/dow_local fields->time => ";
1809 fmt.format(t2, str);
1810 errln(str);
1811 }
1812
1813 logln("Testing DOW_LOCAL.. dow%d\n", dow);
1814 // Make sure DOW_LOCAL disambiguates over DOW
1815 int32_t wrongDow = dow - 3;
1816 if (wrongDow < 1) wrongDow += 7;
1817 cal.setTime(t, status);
1818 cal.set(UCAL_DAY_OF_WEEK, wrongDow);
1819 cal.set(UCAL_DOW_LOCAL, dowLocal);
1820 t2 = cal.getTime(status);
1821 CHECK(status, "Fail: set/getTime failed");
1822 if (t != t2) {
1823 str = "Fail: DOW_LOCAL fields->time => ";
1824 fmt.format(t2, str);
1825 errln(str);
1826 logln(calToStr(cal));
1827 logln("%.f : DOW%d, DOW_LOCAL%d -> %.f\n",
1828 t, wrongDow, dowLocal, t2);
1829 }
1830
1831 // Make sure DOW disambiguates over DOW_LOCAL
1832 int32_t wrongDowLocal = dowLocal - 3;
1833 if (wrongDowLocal < 1) wrongDowLocal += 7;
1834 cal.setTime(t, status);
1835 cal.set(UCAL_DOW_LOCAL, wrongDowLocal);
1836 cal.set(UCAL_DAY_OF_WEEK, dow);
1837 t2 = cal.getTime(status);
1838 CHECK(status, "Fail: set/getTime failed");
1839 if (t != t2) {
1840 str = "Fail: DOW fields->time => ";
1841 fmt.format(t2, str);
1842 errln(str);
1843 }
1844
1845 // Make sure YEAR_WOY disambiguates over YEAR
1846 cal.setTime(t, status);
1847 cal.set(UCAL_YEAR, year - 2);
1848 cal.set(UCAL_YEAR_WOY, yearWoy);
1849 t2 = cal.getTime(status);
1850 CHECK(status, "Fail: set/getTime failed");
1851 if (t != t2) {
1852 str = "Fail: YEAR_WOY fields->time => ";
1853 fmt.format(t2, str);
1854 errln(str);
1855 }
1856
1857 // Make sure YEAR disambiguates over YEAR_WOY
1858 cal.setTime(t, status);
1859 cal.set(UCAL_YEAR_WOY, yearWoy - 2);
1860 cal.set(UCAL_YEAR, year);
1861 t2 = cal.getTime(status);
1862 CHECK(status, "Fail: set/getTime failed");
1863 if (t != t2) {
1864 str = "Fail: YEAR fields->time => ";
1865 fmt.format(t2, str);
1866 errln(str);
1867 }
1868 }
1869 }
1870
1871 /*
1872 FDW = Mon, MDFW = 4:
1873 Sun Dec 26 1999, WOY 51
1874 Mon Dec 27 1999, WOY 52
1875 Tue Dec 28 1999, WOY 52
1876 Wed Dec 29 1999, WOY 52
1877 Thu Dec 30 1999, WOY 52
1878 Fri Dec 31 1999, WOY 52
1879 Sat Jan 01 2000, WOY 52
1880 Sun Jan 02 2000, WOY 52
1881 */
1882
1883 // Roll the DOW_LOCAL within week 52
1884 for (i=27; i<=33; ++i) {
1885 int32_t amount;
1886 for (amount=-7; amount<=7; ++amount) {
1887 str = "roll(";
1888 cal.set(1999, UCAL_DECEMBER, i);
1889 UDate t, t2;
1890 fmt.format(cal.getTime(status), str);
1891 CHECK(status, "Fail: getTime failed");
1892 str += UnicodeString(", ") + amount + ") = ";
1893
1894 cal.roll(UCAL_DOW_LOCAL, amount, status);
1895 CHECK(status, "Fail: roll failed");
1896
1897 t = cal.getTime(status);
1898 int32_t newDom = i + amount;
1899 while (newDom < 27) newDom += 7;
1900 while (newDom > 33) newDom -= 7;
1901 cal.set(1999, UCAL_DECEMBER, newDom);
1902 t2 = cal.getTime(status);
1903 CHECK(status, "Fail: getTime failed");
1904 fmt.format(t, str);
1905
1906 if (t != t2) {
1907 str.append(", exp ");
1908 fmt.format(t2, str);
1909 errln(str);
1910 } else {
1911 logln(str);
1912 }
1913 }
1914 }
1915 }
1916
1917 void CalendarTest::TestYWOY()
1918 {
1919 UnicodeString str;
1920 UErrorCode status = U_ZERO_ERROR;
1921
1922 GregorianCalendar cal(status);
1923 if (failure(status, "construct GregorianCalendar", TRUE)) return;
1924
1925 cal.setFirstDayOfWeek(UCAL_SUNDAY);
1926 cal.setMinimalDaysInFirstWeek(1);
1927
1928 logln("Setting: ywoy=2004, woy=1, dow=MONDAY");
1929 cal.clear();
1930 cal.set(UCAL_YEAR_WOY,2004);
1931 cal.set(UCAL_WEEK_OF_YEAR,1);
1932 cal.set(UCAL_DAY_OF_WEEK, UCAL_MONDAY);
1933
1934 logln(calToStr(cal));
1935 if(cal.get(UCAL_YEAR, status) != 2003) {
1936 errln("year not 2003");
1937 }
1938
1939 logln("+ setting DOW to THURSDAY");
1940 cal.clear();
1941 cal.set(UCAL_YEAR_WOY,2004);
1942 cal.set(UCAL_WEEK_OF_YEAR,1);
1943 cal.set(UCAL_DAY_OF_WEEK, UCAL_THURSDAY);
1944
1945 logln(calToStr(cal));
1946 if(cal.get(UCAL_YEAR, status) != 2004) {
1947 errln("year not 2004");
1948 }
1949
1950 logln("+ setting DOW_LOCAL to 1");
1951 cal.clear();
1952 cal.set(UCAL_YEAR_WOY,2004);
1953 cal.set(UCAL_WEEK_OF_YEAR,1);
1954 cal.set(UCAL_DAY_OF_WEEK, UCAL_THURSDAY);
1955 cal.set(UCAL_DOW_LOCAL, 1);
1956
1957 logln(calToStr(cal));
1958 if(cal.get(UCAL_YEAR, status) != 2003) {
1959 errln("year not 2003");
1960 }
1961
1962 cal.setFirstDayOfWeek(UCAL_MONDAY);
1963 cal.setMinimalDaysInFirstWeek(4);
1964 UDate t = 946713600000.;
1965 cal.setTime(t, status);
1966 cal.set(UCAL_DAY_OF_WEEK, 4);
1967 cal.set(UCAL_DOW_LOCAL, 6);
1968 if(cal.getTime(status) != t) {
1969 logln(calToStr(cal));
1970 errln("FAIL: DOW_LOCAL did not take precedence");
1971 }
1972
1973 }
1974
1975 void CalendarTest::TestJD()
1976 {
1977 int32_t jd;
1978 static const int32_t kEpochStartAsJulianDay = 2440588;
1979 UErrorCode status = U_ZERO_ERROR;
1980 GregorianCalendar cal(status);
1981 if (failure(status, "construct GregorianCalendar", TRUE)) return;
1982 cal.setTimeZone(*TimeZone::getGMT());
1983 cal.clear();
1984 jd = cal.get(UCAL_JULIAN_DAY, status);
1985 if(jd != kEpochStartAsJulianDay) {
1986 errln("Wanted JD of %d at time=0, [epoch 1970] but got %d\n", kEpochStartAsJulianDay, jd);
1987 } else {
1988 logln("Wanted JD of %d at time=0, [epoch 1970], got %d\n", kEpochStartAsJulianDay, jd);
1989 }
1990
1991 cal.setTime(Calendar::getNow(), status);
1992 cal.clear();
1993 cal.set(UCAL_JULIAN_DAY, kEpochStartAsJulianDay);
1994 UDate epochTime = cal.getTime(status);
1995 if(epochTime != 0) {
1996 errln("Wanted time of 0 at jd=%d, got %.1lf\n", kEpochStartAsJulianDay, epochTime);
1997 } else {
1998 logln("Wanted time of 0 at jd=%d, got %.1lf\n", kEpochStartAsJulianDay, epochTime);
1999 }
2000
2001 }
2002
2003 // make sure the ctestfw utilities are in sync with the Calendar
2004 void CalendarTest::TestDebug()
2005 {
2006 for(int32_t t=0;t<=UDBG_ENUM_COUNT;t++) {
2007 int32_t count = udbg_enumCount((UDebugEnumType)t);
2008 if(count == -1) {
2009 logln("enumCount(%d) returned -1", count);
2010 continue;
2011 }
2012 for(int32_t i=0;i<=count;i++) {
2013 if(t<=UDBG_HIGHEST_CONTIGUOUS_ENUM && i<count) {
2014 if( i!=udbg_enumArrayValue((UDebugEnumType)t, i)) {
2015 errln("FAIL: udbg_enumArrayValue(%d,%d) returned %d, expected %d", t, i, udbg_enumArrayValue((UDebugEnumType)t,i), i);
2016 }
2017 } else {
2018 logln("Testing count+1:");
2019 }
2020 const char *name = udbg_enumName((UDebugEnumType)t,i);
2021 if(name==NULL) {
2022 if(i==count || t>UDBG_HIGHEST_CONTIGUOUS_ENUM ) {
2023 logln(" null name - expected.\n");
2024 } else {
2025 errln("FAIL: udbg_enumName(%d,%d) returned NULL", t, i);
2026 }
2027 name = "(null)";
2028 }
2029 logln("udbg_enumArrayValue(%d,%d) = %s, returned %d", t, i,
2030 name, udbg_enumArrayValue((UDebugEnumType)t,i));
2031 logln("udbg_enumString = " + udbg_enumString((UDebugEnumType)t,i));
2032 }
2033 if(udbg_enumExpectedCount((UDebugEnumType)t) != count && t<=UDBG_HIGHEST_CONTIGUOUS_ENUM) {
2034 errln("FAIL: udbg_enumExpectedCount(%d): %d, != UCAL_FIELD_COUNT=%d ", t, udbg_enumExpectedCount((UDebugEnumType)t), count);
2035 } else {
2036 logln("udbg_ucal_fieldCount: %d, UCAL_FIELD_COUNT=udbg_enumCount %d ", udbg_enumExpectedCount((UDebugEnumType)t), count);
2037 }
2038 }
2039 }
2040
2041
2042 #undef CHECK
2043
2044 // List of interesting locales
2045 const char *CalendarTest::testLocaleID(int32_t i)
2046 {
2047 switch(i) {
2048 case 0: return "he_IL@calendar=hebrew";
2049 case 1: return "en_US@calendar=hebrew";
2050 case 2: return "fr_FR@calendar=hebrew";
2051 case 3: return "fi_FI@calendar=hebrew";
2052 case 4: return "nl_NL@calendar=hebrew";
2053 case 5: return "hu_HU@calendar=hebrew";
2054 case 6: return "nl_BE@currency=MTL;calendar=islamic";
2055 case 7: return "th_TH_TRADITIONAL@calendar=gregorian";
2056 case 8: return "ar_JO@calendar=islamic-civil";
2057 case 9: return "fi_FI@calendar=islamic";
2058 case 10: return "fr_CH@calendar=islamic-civil";
2059 case 11: return "he_IL@calendar=islamic-civil";
2060 case 12: return "hu_HU@calendar=buddhist";
2061 case 13: return "hu_HU@calendar=islamic";
2062 case 14: return "en_US@calendar=japanese";
2063 default: return NULL;
2064 }
2065 }
2066
2067 int32_t CalendarTest::testLocaleCount()
2068 {
2069 static int32_t gLocaleCount = -1;
2070 if(gLocaleCount < 0) {
2071 int32_t i;
2072 for(i=0;testLocaleID(i) != NULL;i++) {
2073 ;
2074 }
2075 gLocaleCount = i;
2076 }
2077 return gLocaleCount;
2078 }
2079
2080 static UDate doMinDateOfCalendar(Calendar* adopt, UBool &isGregorian, UErrorCode& status) {
2081 if(U_FAILURE(status)) return 0.0;
2082
2083 adopt->clear();
2084 adopt->set(UCAL_EXTENDED_YEAR, adopt->getActualMinimum(UCAL_EXTENDED_YEAR, status));
2085 UDate ret = adopt->getTime(status);
2086 isGregorian = dynamic_cast<GregorianCalendar*>(adopt) != NULL;
2087 delete adopt;
2088 return ret;
2089 }
2090
2091 UDate CalendarTest::minDateOfCalendar(const Locale& locale, UBool &isGregorian, UErrorCode& status) {
2092 if(U_FAILURE(status)) return 0.0;
2093 return doMinDateOfCalendar(Calendar::createInstance(locale, status), isGregorian, status);
2094 }
2095
2096 UDate CalendarTest::minDateOfCalendar(const Calendar& cal, UBool &isGregorian, UErrorCode& status) {
2097 if(U_FAILURE(status)) return 0.0;
2098 return doMinDateOfCalendar(cal.clone(), isGregorian, status);
2099 }
2100
2101 void CalendarTest::Test6703()
2102 {
2103 UErrorCode status = U_ZERO_ERROR;
2104 Calendar *cal;
2105
2106 Locale loc1("en@calendar=fubar");
2107 cal = Calendar::createInstance(loc1, status);
2108 if (failure(status, "Calendar::createInstance", TRUE)) return;
2109 delete cal;
2110
2111 status = U_ZERO_ERROR;
2112 Locale loc2("en");
2113 cal = Calendar::createInstance(loc2, status);
2114 if (failure(status, "Calendar::createInstance")) return;
2115 delete cal;
2116
2117 status = U_ZERO_ERROR;
2118 Locale loc3("en@calendar=roc");
2119 cal = Calendar::createInstance(loc3, status);
2120 if (failure(status, "Calendar::createInstance")) return;
2121 delete cal;
2122
2123 return;
2124 }
2125
2126 void CalendarTest::Test3785()
2127 {
2128 UErrorCode status = U_ZERO_ERROR;
2129 UnicodeString uzone = UNICODE_STRING_SIMPLE("Europe/Paris");
2130 UnicodeString exp1 = UNICODE_STRING_SIMPLE("Mon 30 Jumada II 1433 AH, 01:47:03");
2131 UnicodeString exp2 = UNICODE_STRING_SIMPLE("Mon 1 Rajab 1433 AH, 01:47:04");
2132
2133 LocalUDateFormatPointer df(udat_open(UDAT_NONE, UDAT_NONE, "en@calendar=islamic", uzone.getTerminatedBuffer(),
2134 uzone.length(), NULL, 0, &status));
2135 if (df.isNull() || U_FAILURE(status)) return;
2136
2137 UChar upattern[64];
2138 u_uastrcpy(upattern, "EEE d MMMM y G, HH:mm:ss");
2139 udat_applyPattern(df.getAlias(), FALSE, upattern, u_strlen(upattern));
2140
2141 UChar ubuffer[1024];
2142 UDate ud0 = 1337557623000.0;
2143
2144 status = U_ZERO_ERROR;
2145 udat_format(df.getAlias(), ud0, ubuffer, 1024, NULL, &status);
2146 if (U_FAILURE(status)) {
2147 errln("Error formatting date 1\n");
2148 return;
2149 }
2150 //printf("formatted: '%s'\n", mkcstr(ubuffer));
2151
2152 UnicodeString act1(ubuffer);
2153 if ( act1 != exp1 ) {
2154 errln("Unexpected result from date 1 format\n");
2155 }
2156 ud0 += 1000.0; // add one second
2157
2158 status = U_ZERO_ERROR;
2159 udat_format(df.getAlias(), ud0, ubuffer, 1024, NULL, &status);
2160 if (U_FAILURE(status)) {
2161 errln("Error formatting date 2\n");
2162 return;
2163 }
2164 //printf("formatted: '%s'\n", mkcstr(ubuffer));
2165 UnicodeString act2(ubuffer);
2166 if ( act2 != exp2 ) {
2167 errln("Unexpected result from date 2 format\n");
2168 }
2169
2170 return;
2171 }
2172
2173 void CalendarTest::Test1624() {
2174 UErrorCode status = U_ZERO_ERROR;
2175 Locale loc("he_IL@calendar=hebrew");
2176 HebrewCalendar hc(loc,status);
2177
2178 for (int32_t year = 5600; year < 5800; year++ ) {
2179
2180 for (int32_t month = HebrewCalendar::TISHRI; month <= HebrewCalendar::ELUL; month++) {
2181 // skip the adar 1 month if year is not a leap year
2182 if (HebrewCalendar::isLeapYear(year) == FALSE && month == HebrewCalendar::ADAR_1) {
2183 continue;
2184 }
2185 int32_t day = 15;
2186 hc.set(year,month,day);
2187 int32_t dayHC = hc.get(UCAL_DATE,status);
2188 int32_t monthHC = hc.get(UCAL_MONTH,status);
2189 int32_t yearHC = hc.get(UCAL_YEAR,status);
2190
2191 if (failure(status, "HebrewCalendar.get()", TRUE)) continue;
2192
2193 if (dayHC != day) {
2194 errln(" ==> day %d incorrect, should be: %d\n",dayHC,day);
2195 break;
2196 }
2197 if (monthHC != month) {
2198 errln(" ==> month %d incorrect, should be: %d\n",monthHC,month);
2199 break;
2200 }
2201 if (yearHC != year) {
2202 errln(" ==> day %d incorrect, should be: %d\n",yearHC,year);
2203 break;
2204 }
2205 }
2206 }
2207 return;
2208 }
2209
2210 void CalendarTest::TestTimeStamp() {
2211 UErrorCode status = U_ZERO_ERROR;
2212 UDate start = 0.0, time;
2213 Calendar *cal;
2214
2215 // Create a new Gregorian Calendar.
2216 cal = Calendar::createInstance("en_US@calender=gregorian", status);
2217 if (U_FAILURE(status)) {
2218 dataerrln("Error creating Gregorian calendar.");
2219 return;
2220 }
2221
2222 for (int i = 0; i < 20000; i++) {
2223 // Set the Gregorian Calendar to a specific date for testing.
2224 cal->set(2009, UCAL_JULY, 3, 0, 49, 46);
2225
2226 time = cal->getTime(status);
2227 if (U_FAILURE(status)) {
2228 errln("Error calling getTime()");
2229 break;
2230 }
2231
2232 if (i == 0) {
2233 start = time;
2234 } else {
2235 if (start != time) {
2236 errln("start and time not equal.");
2237 break;
2238 }
2239 }
2240 }
2241
2242 delete cal;
2243 }
2244
2245 void CalendarTest::TestISO8601() {
2246 const char* TEST_LOCALES[] = {
2247 "en_US@calendar=iso8601",
2248 "en_US@calendar=Iso8601",
2249 "th_TH@calendar=iso8601",
2250 "ar_EG@calendar=iso8601",
2251 NULL
2252 };
2253
2254 int32_t TEST_DATA[][3] = {
2255 {2008, 1, 2008},
2256 {2009, 1, 2009},
2257 {2010, 53, 2009},
2258 {2011, 52, 2010},
2259 {2012, 52, 2011},
2260 {2013, 1, 2013},
2261 {2014, 1, 2014},
2262 {0, 0, 0},
2263 };
2264
2265 for (int i = 0; TEST_LOCALES[i] != NULL; i++) {
2266 UErrorCode status = U_ZERO_ERROR;
2267 Calendar *cal = Calendar::createInstance(TEST_LOCALES[i], status);
2268 if (U_FAILURE(status)) {
2269 errln("Error: Failed to create a calendar for locale: %s", TEST_LOCALES[i]);
2270 continue;
2271 }
2272 if (uprv_strcmp(cal->getType(), "gregorian") != 0) {
2273 errln("Error: Gregorian calendar is not used for locale: %s", TEST_LOCALES[i]);
2274 continue;
2275 }
2276 for (int j = 0; TEST_DATA[j][0] != 0; j++) {
2277 cal->set(TEST_DATA[j][0], UCAL_JANUARY, 1);
2278 int32_t weekNum = cal->get(UCAL_WEEK_OF_YEAR, status);
2279 int32_t weekYear = cal->get(UCAL_YEAR_WOY, status);
2280 if (U_FAILURE(status)) {
2281 errln("Error: Failed to get week of year");
2282 break;
2283 }
2284 if (weekNum != TEST_DATA[j][1] || weekYear != TEST_DATA[j][2]) {
2285 errln("Error: Incorrect week of year on January 1st, %d for locale %s: Returned [weekNum=%d, weekYear=%d], Expected [weekNum=%d, weekYear=%d]",
2286 TEST_DATA[j][0], TEST_LOCALES[i], weekNum, weekYear, TEST_DATA[j][1], TEST_DATA[j][2]);
2287 }
2288 }
2289 delete cal;
2290 }
2291
2292 }
2293
2294 void
2295 CalendarTest::TestAmbiguousWallTimeAPIs(void) {
2296 UErrorCode status = U_ZERO_ERROR;
2297 Calendar* cal = Calendar::createInstance(status);
2298 if (U_FAILURE(status)) {
2299 errln("Fail: Error creating a calendar instance.");
2300 return;
2301 }
2302
2303 if (cal->getRepeatedWallTimeOption() != UCAL_WALLTIME_LAST) {
2304 errln("Fail: Default repeted time option is not UCAL_WALLTIME_LAST");
2305 }
2306 if (cal->getSkippedWallTimeOption() != UCAL_WALLTIME_LAST) {
2307 errln("Fail: Default skipped time option is not UCAL_WALLTIME_LAST");
2308 }
2309
2310 Calendar* cal2 = cal->clone();
2311
2312 if (*cal != *cal2) {
2313 errln("Fail: Cloned calendar != the original");
2314 }
2315 if (!cal->equals(*cal2, status)) {
2316 errln("Fail: The time of cloned calendar is not equal to the original");
2317 } else if (U_FAILURE(status)) {
2318 errln("Fail: Error equals");
2319 }
2320 status = U_ZERO_ERROR;
2321
2322 cal2->setRepeatedWallTimeOption(UCAL_WALLTIME_FIRST);
2323 cal2->setSkippedWallTimeOption(UCAL_WALLTIME_FIRST);
2324
2325 if (*cal == *cal2) {
2326 errln("Fail: Cloned and modified calendar == the original");
2327 }
2328 if (!cal->equals(*cal2, status)) {
2329 errln("Fail: The time of cloned calendar is not equal to the original after changing wall time options");
2330 } else if (U_FAILURE(status)) {
2331 errln("Fail: Error equals after changing wall time options");
2332 }
2333 status = U_ZERO_ERROR;
2334
2335 if (cal2->getRepeatedWallTimeOption() != UCAL_WALLTIME_FIRST) {
2336 errln("Fail: Repeted time option is not UCAL_WALLTIME_FIRST");
2337 }
2338 if (cal2->getSkippedWallTimeOption() != UCAL_WALLTIME_FIRST) {
2339 errln("Fail: Skipped time option is not UCAL_WALLTIME_FIRST");
2340 }
2341
2342 cal2->setRepeatedWallTimeOption(UCAL_WALLTIME_NEXT_VALID);
2343 if (cal2->getRepeatedWallTimeOption() != UCAL_WALLTIME_FIRST) {
2344 errln("Fail: Repeated wall time option was updated other than UCAL_WALLTIME_FIRST");
2345 }
2346
2347 delete cal;
2348 delete cal2;
2349 }
2350
2351 class CalFields {
2352 public:
2353 CalFields(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t min, int32_t sec);
2354 CalFields(const Calendar& cal, UErrorCode& status);
2355 void setTo(Calendar& cal) const;
2356 char* toString(char* buf, int32_t len) const;
2357 UBool operator==(const CalFields& rhs) const;
2358 UBool operator!=(const CalFields& rhs) const;
2359
2360 private:
2361 int32_t year;
2362 int32_t month;
2363 int32_t day;
2364 int32_t hour;
2365 int32_t min;
2366 int32_t sec;
2367 };
2368
2369 CalFields::CalFields(int32_t year, int32_t month, int32_t day, int32_t hour, int32_t min, int32_t sec)
2370 : year(year), month(month), day(day), hour(hour), min(min), sec(sec) {
2371 }
2372
2373 CalFields::CalFields(const Calendar& cal, UErrorCode& status) {
2374 year = cal.get(UCAL_YEAR, status);
2375 month = cal.get(UCAL_MONTH, status) + 1;
2376 day = cal.get(UCAL_DAY_OF_MONTH, status);
2377 hour = cal.get(UCAL_HOUR_OF_DAY, status);
2378 min = cal.get(UCAL_MINUTE, status);
2379 sec = cal.get(UCAL_SECOND, status);
2380 }
2381
2382 void
2383 CalFields::setTo(Calendar& cal) const {
2384 cal.clear();
2385 cal.set(year, month - 1, day, hour, min, sec);
2386 }
2387
2388 char*
2389 CalFields::toString(char* buf, int32_t len) const {
2390 char local[32];
2391 sprintf(local, "%04d-%02d-%02d %02d:%02d:%02d", year, month, day, hour, min, sec);
2392 uprv_strncpy(buf, local, len - 1);
2393 buf[len - 1] = 0;
2394 return buf;
2395 }
2396
2397 UBool
2398 CalFields::operator==(const CalFields& rhs) const {
2399 return year == rhs.year
2400 && month == rhs.month
2401 && day == rhs.day
2402 && hour == rhs.hour
2403 && min == rhs.min
2404 && sec == rhs.sec;
2405 }
2406
2407 UBool
2408 CalFields::operator!=(const CalFields& rhs) const {
2409 return !(*this == rhs);
2410 }
2411
2412 typedef struct {
2413 const char* tzid;
2414 const CalFields in;
2415 const CalFields expLastGMT;
2416 const CalFields expFirstGMT;
2417 } RepeatedWallTimeTestData;
2418
2419 static const RepeatedWallTimeTestData RPDATA[] =
2420 {
2421 // Time zone Input wall time WALLTIME_LAST in GMT WALLTIME_FIRST in GMT
2422 {"America/New_York", CalFields(2011,11,6,0,59,59), CalFields(2011,11,6,4,59,59), CalFields(2011,11,6,4,59,59)},
2423 {"America/New_York", CalFields(2011,11,6,1,0,0), CalFields(2011,11,6,6,0,0), CalFields(2011,11,6,5,0,0)},
2424 {"America/New_York", CalFields(2011,11,6,1,0,1), CalFields(2011,11,6,6,0,1), CalFields(2011,11,6,5,0,1)},
2425 {"America/New_York", CalFields(2011,11,6,1,30,0), CalFields(2011,11,6,6,30,0), CalFields(2011,11,6,5,30,0)},
2426 {"America/New_York", CalFields(2011,11,6,1,59,59), CalFields(2011,11,6,6,59,59), CalFields(2011,11,6,5,59,59)},
2427 {"America/New_York", CalFields(2011,11,6,2,0,0), CalFields(2011,11,6,7,0,0), CalFields(2011,11,6,7,0,0)},
2428 {"America/New_York", CalFields(2011,11,6,2,0,1), CalFields(2011,11,6,7,0,1), CalFields(2011,11,6,7,0,1)},
2429
2430 {"Australia/Lord_Howe", CalFields(2011,4,3,1,29,59), CalFields(2011,4,2,14,29,59), CalFields(2011,4,2,14,29,59)},
2431 {"Australia/Lord_Howe", CalFields(2011,4,3,1,30,0), CalFields(2011,4,2,15,0,0), CalFields(2011,4,2,14,30,0)},
2432 {"Australia/Lord_Howe", CalFields(2011,4,3,1,45,0), CalFields(2011,4,2,15,15,0), CalFields(2011,4,2,14,45,0)},
2433 {"Australia/Lord_Howe", CalFields(2011,4,3,1,59,59), CalFields(2011,4,2,15,29,59), CalFields(2011,4,2,14,59,59)},
2434 {"Australia/Lord_Howe", CalFields(2011,4,3,2,0,0), CalFields(2011,4,2,15,30,0), CalFields(2011,4,2,15,30,0)},
2435 {"Australia/Lord_Howe", CalFields(2011,4,3,2,0,1), CalFields(2011,4,2,15,30,1), CalFields(2011,4,2,15,30,1)},
2436
2437 {NULL, CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0)}
2438 };
2439
2440 void CalendarTest::TestRepeatedWallTime(void) {
2441 UErrorCode status = U_ZERO_ERROR;
2442 GregorianCalendar calGMT((const TimeZone&)*TimeZone::getGMT(), status);
2443 GregorianCalendar calDefault(status);
2444 GregorianCalendar calLast(status);
2445 GregorianCalendar calFirst(status);
2446
2447 if (U_FAILURE(status)) {
2448 errln("Fail: Failed to create a calendar object.");
2449 return;
2450 }
2451
2452 calLast.setRepeatedWallTimeOption(UCAL_WALLTIME_LAST);
2453 calFirst.setRepeatedWallTimeOption(UCAL_WALLTIME_FIRST);
2454
2455 for (int32_t i = 0; RPDATA[i].tzid != NULL; i++) {
2456 char buf[32];
2457 TimeZone *tz = TimeZone::createTimeZone(RPDATA[i].tzid);
2458
2459 // UCAL_WALLTIME_LAST
2460 status = U_ZERO_ERROR;
2461 calLast.setTimeZone(*tz);
2462 RPDATA[i].in.setTo(calLast);
2463 calGMT.setTime(calLast.getTime(status), status);
2464 CalFields outLastGMT(calGMT, status);
2465 if (U_FAILURE(status)) {
2466 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (UCAL_WALLTIME_LAST) - ")
2467 + RPDATA[i].in.toString(buf, sizeof(buf)) + "[" + RPDATA[i].tzid + "]");
2468 } else {
2469 if (outLastGMT != RPDATA[i].expLastGMT) {
2470 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_LAST ") + RPDATA[i].in.toString(buf, sizeof(buf)) + "[" + RPDATA[i].tzid + "] is parsed as "
2471 + outLastGMT.toString(buf, sizeof(buf)) + "[GMT]. Expected: " + RPDATA[i].expLastGMT.toString(buf, sizeof(buf)) + "[GMT]");
2472 }
2473 }
2474
2475 // default
2476 status = U_ZERO_ERROR;
2477 calDefault.setTimeZone(*tz);
2478 RPDATA[i].in.setTo(calDefault);
2479 calGMT.setTime(calDefault.getTime(status), status);
2480 CalFields outDefGMT(calGMT, status);
2481 if (U_FAILURE(status)) {
2482 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (default) - ")
2483 + RPDATA[i].in.toString(buf, sizeof(buf)) + "[" + RPDATA[i].tzid + "]");
2484 } else {
2485 if (outDefGMT != RPDATA[i].expLastGMT) {
2486 dataerrln(UnicodeString("Fail: (default) ") + RPDATA[i].in.toString(buf, sizeof(buf)) + "[" + RPDATA[i].tzid + "] is parsed as "
2487 + outDefGMT.toString(buf, sizeof(buf)) + "[GMT]. Expected: " + RPDATA[i].expLastGMT.toString(buf, sizeof(buf)) + "[GMT]");
2488 }
2489 }
2490
2491 // UCAL_WALLTIME_FIRST
2492 status = U_ZERO_ERROR;
2493 calFirst.setTimeZone(*tz);
2494 RPDATA[i].in.setTo(calFirst);
2495 calGMT.setTime(calFirst.getTime(status), status);
2496 CalFields outFirstGMT(calGMT, status);
2497 if (U_FAILURE(status)) {
2498 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (UCAL_WALLTIME_FIRST) - ")
2499 + RPDATA[i].in.toString(buf, sizeof(buf)) + "[" + RPDATA[i].tzid + "]");
2500 } else {
2501 if (outFirstGMT != RPDATA[i].expFirstGMT) {
2502 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_FIRST ") + RPDATA[i].in.toString(buf, sizeof(buf)) + "[" + RPDATA[i].tzid + "] is parsed as "
2503 + outFirstGMT.toString(buf, sizeof(buf)) + "[GMT]. Expected: " + RPDATA[i].expFirstGMT.toString(buf, sizeof(buf)) + "[GMT]");
2504 }
2505 }
2506 delete tz;
2507 }
2508 }
2509
2510 typedef struct {
2511 const char* tzid;
2512 const CalFields in;
2513 UBool isValid;
2514 const CalFields expLastGMT;
2515 const CalFields expFirstGMT;
2516 const CalFields expNextAvailGMT;
2517 } SkippedWallTimeTestData;
2518
2519 static SkippedWallTimeTestData SKDATA[] =
2520 {
2521 // Time zone Input wall time valid? WALLTIME_LAST in GMT WALLTIME_FIRST in GMT WALLTIME_NEXT_VALID in GMT
2522 {"America/New_York", CalFields(2011,3,13,1,59,59), TRUE, CalFields(2011,3,13,6,59,59), CalFields(2011,3,13,6,59,59), CalFields(2011,3,13,6,59,59)},
2523 {"America/New_York", CalFields(2011,3,13,2,0,0), FALSE, CalFields(2011,3,13,7,0,0), CalFields(2011,3,13,6,0,0), CalFields(2011,3,13,7,0,0)},
2524 {"America/New_York", CalFields(2011,3,13,2,1,0), FALSE, CalFields(2011,3,13,7,1,0), CalFields(2011,3,13,6,1,0), CalFields(2011,3,13,7,0,0)},
2525 {"America/New_York", CalFields(2011,3,13,2,30,0), FALSE, CalFields(2011,3,13,7,30,0), CalFields(2011,3,13,6,30,0), CalFields(2011,3,13,7,0,0)},
2526 {"America/New_York", CalFields(2011,3,13,2,59,59), FALSE, CalFields(2011,3,13,7,59,59), CalFields(2011,3,13,6,59,59), CalFields(2011,3,13,7,0,0)},
2527 {"America/New_York", CalFields(2011,3,13,3,0,0), TRUE, CalFields(2011,3,13,7,0,0), CalFields(2011,3,13,7,0,0), CalFields(2011,3,13,7,0,0)},
2528
2529 {"Pacific/Apia", CalFields(2011,12,29,23,59,59), TRUE, CalFields(2011,12,30,9,59,59), CalFields(2011,12,30,9,59,59), CalFields(2011,12,30,9,59,59)},
2530 {"Pacific/Apia", CalFields(2011,12,30,0,0,0), FALSE, CalFields(2011,12,30,10,0,0), CalFields(2011,12,29,10,0,0), CalFields(2011,12,30,10,0,0)},
2531 {"Pacific/Apia", CalFields(2011,12,30,12,0,0), FALSE, CalFields(2011,12,30,22,0,0), CalFields(2011,12,29,22,0,0), CalFields(2011,12,30,10,0,0)},
2532 {"Pacific/Apia", CalFields(2011,12,30,23,59,59), FALSE, CalFields(2011,12,31,9,59,59), CalFields(2011,12,30,9,59,59), CalFields(2011,12,30,10,0,0)},
2533 {"Pacific/Apia", CalFields(2011,12,31,0,0,0), TRUE, CalFields(2011,12,30,10,0,0), CalFields(2011,12,30,10,0,0), CalFields(2011,12,30,10,0,0)},
2534
2535 {NULL, CalFields(0,0,0,0,0,0), TRUE, CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0), CalFields(0,0,0,0,0,0)}
2536 };
2537
2538
2539 void CalendarTest::TestSkippedWallTime(void) {
2540 UErrorCode status = U_ZERO_ERROR;
2541 GregorianCalendar calGMT((const TimeZone&)*TimeZone::getGMT(), status);
2542 GregorianCalendar calDefault(status);
2543 GregorianCalendar calLast(status);
2544 GregorianCalendar calFirst(status);
2545 GregorianCalendar calNextAvail(status);
2546
2547 if (U_FAILURE(status)) {
2548 errln("Fail: Failed to create a calendar object.");
2549 return;
2550 }
2551
2552 calLast.setSkippedWallTimeOption(UCAL_WALLTIME_LAST);
2553 calFirst.setSkippedWallTimeOption(UCAL_WALLTIME_FIRST);
2554 calNextAvail.setSkippedWallTimeOption(UCAL_WALLTIME_NEXT_VALID);
2555
2556 for (int32_t i = 0; SKDATA[i].tzid != NULL; i++) {
2557 UDate d;
2558 char buf[32];
2559 TimeZone *tz = TimeZone::createTimeZone(SKDATA[i].tzid);
2560
2561 for (int32_t j = 0; j < 2; j++) {
2562 UBool bLenient = (j == 0);
2563
2564 // UCAL_WALLTIME_LAST
2565 status = U_ZERO_ERROR;
2566 calLast.setLenient(bLenient);
2567 calLast.setTimeZone(*tz);
2568 SKDATA[i].in.setTo(calLast);
2569 d = calLast.getTime(status);
2570 if (bLenient || SKDATA[i].isValid) {
2571 calGMT.setTime(d, status);
2572 CalFields outLastGMT(calGMT, status);
2573 if (U_FAILURE(status)) {
2574 errln(UnicodeString("Fail: Failed to get/set time calLast/calGMT (UCAL_WALLTIME_LAST) - ")
2575 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2576 } else {
2577 if (outLastGMT != SKDATA[i].expLastGMT) {
2578 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_LAST ") + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "] is parsed as "
2579 + outLastGMT.toString(buf, sizeof(buf)) + "[GMT]. Expected: " + SKDATA[i].expLastGMT.toString(buf, sizeof(buf)) + "[GMT]");
2580 }
2581 }
2582 } else if (U_SUCCESS(status)) {
2583 // strict, invalid wall time - must report an error
2584 dataerrln(UnicodeString("Fail: An error expected (UCAL_WALLTIME_LAST)") +
2585 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2586 }
2587
2588 // default
2589 status = U_ZERO_ERROR;
2590 calDefault.setLenient(bLenient);
2591 calDefault.setTimeZone(*tz);
2592 SKDATA[i].in.setTo(calDefault);
2593 d = calDefault.getTime(status);
2594 if (bLenient || SKDATA[i].isValid) {
2595 calGMT.setTime(d, status);
2596 CalFields outDefGMT(calGMT, status);
2597 if (U_FAILURE(status)) {
2598 errln(UnicodeString("Fail: Failed to get/set time calDefault/calGMT (default) - ")
2599 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2600 } else {
2601 if (outDefGMT != SKDATA[i].expLastGMT) {
2602 dataerrln(UnicodeString("Fail: (default) ") + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "] is parsed as "
2603 + outDefGMT.toString(buf, sizeof(buf)) + "[GMT]. Expected: " + SKDATA[i].expLastGMT.toString(buf, sizeof(buf)) + "[GMT]");
2604 }
2605 }
2606 } else if (U_SUCCESS(status)) {
2607 // strict, invalid wall time - must report an error
2608 dataerrln(UnicodeString("Fail: An error expected (default)") +
2609 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2610 }
2611
2612 // UCAL_WALLTIME_FIRST
2613 status = U_ZERO_ERROR;
2614 calFirst.setLenient(bLenient);
2615 calFirst.setTimeZone(*tz);
2616 SKDATA[i].in.setTo(calFirst);
2617 d = calFirst.getTime(status);
2618 if (bLenient || SKDATA[i].isValid) {
2619 calGMT.setTime(d, status);
2620 CalFields outFirstGMT(calGMT, status);
2621 if (U_FAILURE(status)) {
2622 errln(UnicodeString("Fail: Failed to get/set time calFirst/calGMT (UCAL_WALLTIME_FIRST) - ")
2623 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2624 } else {
2625 if (outFirstGMT != SKDATA[i].expFirstGMT) {
2626 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_FIRST ") + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "] is parsed as "
2627 + outFirstGMT.toString(buf, sizeof(buf)) + "[GMT]. Expected: " + SKDATA[i].expFirstGMT.toString(buf, sizeof(buf)) + "[GMT]");
2628 }
2629 }
2630 } else if (U_SUCCESS(status)) {
2631 // strict, invalid wall time - must report an error
2632 dataerrln(UnicodeString("Fail: An error expected (UCAL_WALLTIME_FIRST)") +
2633 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2634 }
2635
2636 // UCAL_WALLTIME_NEXT_VALID
2637 status = U_ZERO_ERROR;
2638 calNextAvail.setLenient(bLenient);
2639 calNextAvail.setTimeZone(*tz);
2640 SKDATA[i].in.setTo(calNextAvail);
2641 d = calNextAvail.getTime(status);
2642 if (bLenient || SKDATA[i].isValid) {
2643 calGMT.setTime(d, status);
2644 CalFields outNextAvailGMT(calGMT, status);
2645 if (U_FAILURE(status)) {
2646 errln(UnicodeString("Fail: Failed to get/set time calNextAvail/calGMT (UCAL_WALLTIME_NEXT_VALID) - ")
2647 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2648 } else {
2649 if (outNextAvailGMT != SKDATA[i].expNextAvailGMT) {
2650 dataerrln(UnicodeString("Fail: UCAL_WALLTIME_NEXT_VALID ") + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "] is parsed as "
2651 + outNextAvailGMT.toString(buf, sizeof(buf)) + "[GMT]. Expected: " + SKDATA[i].expNextAvailGMT.toString(buf, sizeof(buf)) + "[GMT]");
2652 }
2653 }
2654 } else if (U_SUCCESS(status)) {
2655 // strict, invalid wall time - must report an error
2656 dataerrln(UnicodeString("Fail: An error expected (UCAL_WALLTIME_NEXT_VALID)") +
2657 + SKDATA[i].in.toString(buf, sizeof(buf)) + "[" + SKDATA[i].tzid + "]");
2658 }
2659 }
2660
2661 delete tz;
2662 }
2663 }
2664
2665 void CalendarTest::TestCloneLocale(void) {
2666 UErrorCode status = U_ZERO_ERROR;
2667 LocalPointer<Calendar> cal(Calendar::createInstance(TimeZone::getGMT()->clone(),
2668 Locale::createFromName("en"), status));
2669 TEST_CHECK_STATUS;
2670 Locale l0 = cal->getLocale(ULOC_VALID_LOCALE, status);
2671 TEST_CHECK_STATUS;
2672 LocalPointer<Calendar> cal2(cal->clone());
2673 Locale l = cal2->getLocale(ULOC_VALID_LOCALE, status);
2674 if(l0!=l) {
2675 errln("Error: cloned locale %s != original locale %s, status %s\n", l0.getName(), l.getName(), u_errorName(status));
2676 }
2677 TEST_CHECK_STATUS;
2678 }
2679
2680 #endif /* #if !UCONFIG_NO_FORMATTING */
2681
2682 //eof