]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/incaltst.cpp
ICU-8.11.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / incaltst.cpp
1 /***********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2005, International Business Machines Corporation
4 * and others. All Rights Reserved.
5 ***********************************************************************/
6
7 /* Test Internationalized Calendars for C++ */
8
9 #include "unicode/utypes.h"
10 #include "string.h"
11 #include "unicode/locid.h"
12 #include "japancal.h"
13
14 #if !UCONFIG_NO_FORMATTING
15
16 #include <stdio.h>
17 #include "caltest.h"
18
19 #define CHECK(status, msg) \
20 if (U_FAILURE(status)) { \
21 errln((UnicodeString(u_errorName(status)) + UnicodeString(" : " ) )+ msg); \
22 return; \
23 }
24
25
26 static UnicodeString escape( const UnicodeString&src)
27 {
28 UnicodeString dst;
29 dst.remove();
30 for (int32_t i = 0; i < src.length(); ++i) {
31 UChar c = src[i];
32 if(c < 0x0080)
33 dst += c;
34 else {
35 dst += UnicodeString("[");
36 char buf [8];
37 sprintf(buf, "%#x", c);
38 dst += UnicodeString(buf);
39 dst += UnicodeString("]");
40 }
41 }
42
43 return dst;
44 }
45
46
47 #include "incaltst.h"
48 #include "unicode/gregocal.h"
49 #include "unicode/smpdtfmt.h"
50 #include "unicode/simpletz.h"
51
52 // *****************************************************************************
53 // class IntlCalendarTest
54 // *****************************************************************************
55 //--- move to CalendarTest?
56
57 static const double JULIAN_EPOCH = -210866760000000.;
58
59
60 // Turn this on to dump the calendar fields
61 #define U_DEBUG_DUMPCALS
62
63
64 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break
65
66
67 void IntlCalendarTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
68 {
69 if (exec) logln("TestSuite IntlCalendarTest");
70 switch (index) {
71 CASE(0,TestTypes);
72 CASE(1,TestGregorian);
73 CASE(2,TestBuddhist);
74 CASE(3,TestJapanese);
75 CASE(4,TestBuddhistFormat);
76 CASE(5,TestJapaneseFormat);
77 CASE(6,TestJapanese3860);
78 default: name = ""; break;
79 }
80 }
81
82 #undef CASE
83
84 // ---------------------------------------------------------------------------------
85
86
87 /**
88 * Test various API methods for API completeness.
89 */
90 void
91 IntlCalendarTest::TestTypes()
92 {
93 Calendar *c = NULL;
94 UErrorCode status = U_ZERO_ERROR;
95 int j;
96 const char *locs [40] = { "en_US_VALLEYGIRL",
97 "en_US_VALLEYGIRL@collation=phonebook;calendar=japanese",
98 "en_US_VALLEYGIRL@collation=phonebook;calendar=gregorian",
99 "ja_JP@calendar=japanese",
100 "th_TH@calendar=buddhist",
101 "ja_JP_TRADITIONAL",
102 "th_TH_TRADITIONAL",
103 "th_TH_TRADITIONAL@calendar=gregorian",
104 "en_US", NULL };
105 const char *types[40] = { "gregorian",
106 "japanese",
107 "gregorian",
108 "japanese",
109 "buddhist",
110 "japanese",
111 "buddhist",
112 "gregorian",
113 "gregorian", NULL };
114
115 for(j=0;locs[j];j++) {
116 logln(UnicodeString("Creating calendar of locale ") + locs[j]);
117 status = U_ZERO_ERROR;
118 c = Calendar::createInstance(locs[j], status);
119 CHECK(status, "creating '" + UnicodeString(locs[j]) + "' calendar");
120 if(U_SUCCESS(status)) {
121 logln(UnicodeString(" type is ") + c->getType());
122 if(strcmp(c->getType(), types[j])) {
123 errln(UnicodeString(locs[j]) + UnicodeString("Calendar type ") + c->getType() + " instead of " + types[j]);
124 }
125 }
126 delete c;
127 }
128 }
129
130
131
132 /**
133 * Run a test of a quasi-Gregorian calendar. This is a calendar
134 * that behaves like a Gregorian but has different year/era mappings.
135 * The int[] data array should have the format:
136 *
137 * { era, year, gregorianYear, month, dayOfMonth, ... ... , -1 }
138 */
139 void IntlCalendarTest::quasiGregorianTest(Calendar& cal, const Locale& gcl, const int32_t *data) {
140 UErrorCode status = U_ZERO_ERROR;
141 // As of JDK 1.4.1_01, using the Sun JDK GregorianCalendar as
142 // a reference throws us off by one hour. This is most likely
143 // due to the JDK 1.4 incorporation of historical time zones.
144 //java.util.Calendar grego = java.util.Calendar.getInstance();
145 Calendar *grego = Calendar::createInstance(gcl, status);
146 if (U_FAILURE(status)) {
147 dataerrln("Error calling Calendar::createInstance");
148 return;
149 }
150
151 int32_t tz1 = cal.get(UCAL_ZONE_OFFSET,status);
152 int32_t tz2 = grego -> get (UCAL_ZONE_OFFSET, status);
153 if(tz1 != tz2) {
154 errln((UnicodeString)"cal's tz " + tz1 + " != grego's tz " + tz2);
155 }
156
157 for (int32_t i=0; data[i]!=-1; ) {
158 int32_t era = data[i++];
159 int32_t year = data[i++];
160 int32_t gregorianYear = data[i++];
161 int32_t month = data[i++];
162 int32_t dayOfMonth = data[i++];
163
164 grego->clear();
165 grego->set(gregorianYear, month, dayOfMonth);
166 UDate D = grego->getTime(status);
167
168 cal.clear();
169 cal.set(UCAL_ERA, era);
170 cal.set(year, month, dayOfMonth);
171 UDate d = cal.getTime(status);
172 #ifdef U_DEBUG_DUMPCALS
173 logln((UnicodeString)"cal : " + CalendarTest::calToStr(cal));
174 logln((UnicodeString)"grego: " + CalendarTest::calToStr(*grego));
175 #endif
176 if (d == D) {
177 logln(UnicodeString("OK: ") + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
178 " => " + d + " (" + UnicodeString(cal.getType()) + ")");
179 } else {
180 errln(UnicodeString("Fail: (fields to millis)") + era + ":" + year + "/" + (month+1) + "/" + dayOfMonth +
181 " => " + d + ", expected " + D + " (" + UnicodeString(cal.getType()) + "Off by: " + (d-D));
182 }
183
184 // Now, set the gregorian millis on the other calendar
185 cal.clear();
186 cal.setTime(D, status);
187 int e = cal.get(UCAL_ERA, status);
188 int y = cal.get(UCAL_YEAR, status);
189 #ifdef U_DEBUG_DUMPCALS
190 logln((UnicodeString)"cal : " + CalendarTest::calToStr(cal));
191 logln((UnicodeString)"grego: " + CalendarTest::calToStr(*grego));
192 #endif
193 if (y == year && e == era) {
194 logln((UnicodeString)"OK: " + D + " => " + cal.get(UCAL_ERA, status) + ":" +
195 cal.get(UCAL_YEAR, status) + "/" +
196 (cal.get(UCAL_MONTH, status)+1) + "/" + cal.get(UCAL_DATE, status) + " (" + UnicodeString(cal.getType()) + ")");
197 } else {
198 errln((UnicodeString)"Fail: (millis to fields)" + D + " => " + cal.get(UCAL_ERA, status) + ":" +
199 cal.get(UCAL_YEAR, status) + "/" +
200 (cal.get(UCAL_MONTH, status)+1) + "/" + cal.get(UCAL_DATE, status) +
201 ", expected " + era + ":" + year + "/" + (month+1) + "/" +
202 dayOfMonth + " (" + UnicodeString(cal.getType()));
203 }
204 }
205 delete grego;
206 CHECK(status, "err during quasiGregorianTest()");
207 }
208
209 // Verify that Gregorian works like Gregorian
210 void IntlCalendarTest::TestGregorian() {
211 UDate timeA = Calendar::getNow();
212 int32_t data[] = {
213 GregorianCalendar::AD, 1868, 1868, UCAL_SEPTEMBER, 8,
214 GregorianCalendar::AD, 1868, 1868, UCAL_SEPTEMBER, 9,
215 GregorianCalendar::AD, 1869, 1869, UCAL_JUNE, 4,
216 GregorianCalendar::AD, 1912, 1912, UCAL_JULY, 29,
217 GregorianCalendar::AD, 1912, 1912, UCAL_JULY, 30,
218 GregorianCalendar::AD, 1912, 1912, UCAL_AUGUST, 1,
219 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
220 };
221
222 Calendar *cal;
223 UErrorCode status = U_ZERO_ERROR;
224 cal = Calendar::createInstance(/*"de_DE", */ status);
225 CHECK(status, UnicodeString("Creating de_CH calendar"));
226 // Sanity check the calendar
227 UDate timeB = Calendar::getNow();
228 UDate timeCal = cal->getTime(status);
229
230 if(!(timeA <= timeCal) || !(timeCal <= timeB)) {
231 errln((UnicodeString)"Error: Calendar time " + timeCal +
232 " is not within sampled times [" + timeA + " to " + timeB + "]!");
233 }
234 // end sanity check
235
236 // Note, the following is a good way to test the sanity of the constructed calendars,
237 // using Collation as a delay-loop:
238 //
239 // $ intltest format/IntlCalendarTest collate/G7CollationTest format/IntlCalendarTest
240
241 quasiGregorianTest(*cal,Locale("fr_FR"),data);
242 delete cal;
243 }
244
245 /**
246 * Verify that BuddhistCalendar shifts years to Buddhist Era but otherwise
247 * behaves like GregorianCalendar.
248 */
249 void IntlCalendarTest::TestBuddhist() {
250 // BE 2542 == 1999 CE
251 UDate timeA = Calendar::getNow();
252
253 int32_t data[] = {
254 0, // B. era [928479600000]
255 2542, // B. year
256 1999, // G. year
257 UCAL_JUNE, // month
258 4, // day
259
260 0, // B. era [-79204842000000]
261 3, // B. year
262 -540, // G. year
263 UCAL_FEBRUARY, // month
264 12, // day
265
266 0, // test month calculation: 4795 BE = 4252 AD is a leap year, but 4795 AD is not.
267 4795, // BE [72018057600000]
268 4252, // AD
269 UCAL_FEBRUARY,
270 29,
271
272 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
273 };
274 Calendar *cal;
275 UErrorCode status = U_ZERO_ERROR;
276 cal = Calendar::createInstance("th_TH@calendar=buddhist", status);
277 CHECK(status, UnicodeString("Creating th_TH@calendar=buddhist calendar"));
278
279 // Sanity check the calendar
280 UDate timeB = Calendar::getNow();
281 UDate timeCal = cal->getTime(status);
282
283 if(!(timeA <= timeCal) || !(timeCal <= timeB)) {
284 errln((UnicodeString)"Error: Calendar time " + timeCal +
285 " is not within sampled times [" + timeA + " to " + timeB + "]!");
286 }
287 // end sanity check
288
289
290 quasiGregorianTest(*cal,Locale("th_TH"),data);
291 delete cal;
292 }
293
294 /**
295 * Verify that JapaneseCalendar shifts years to Japanese Eras but otherwise
296 * behaves like GregorianCalendar.
297 */
298 void IntlCalendarTest::TestJapanese() {
299 UDate timeA = Calendar::getNow();
300
301 /* Sorry.. japancal.h is private! */
302 #define JapaneseCalendar_MEIJI 232
303 #define JapaneseCalendar_TAISHO 233
304 #define JapaneseCalendar_SHOWA 234
305 #define JapaneseCalendar_HEISEI 235
306
307 // BE 2542 == 1999 CE
308 int32_t data[] = {
309 // Jera Jyr Gyear m d
310 JapaneseCalendar_MEIJI, 1, 1868, UCAL_SEPTEMBER, 8,
311 JapaneseCalendar_MEIJI, 1, 1868, UCAL_SEPTEMBER, 9,
312 JapaneseCalendar_MEIJI, 2, 1869, UCAL_JUNE, 4,
313 JapaneseCalendar_MEIJI, 45, 1912, UCAL_JULY, 29,
314 JapaneseCalendar_TAISHO, 1, 1912, UCAL_JULY, 30,
315 JapaneseCalendar_TAISHO, 1, 1912, UCAL_AUGUST, 1,
316
317 // new tests (not in java)
318 JapaneseCalendar_SHOWA, 64, 1989, UCAL_JANUARY, 7, // Test current era transition (different code path than others)
319 JapaneseCalendar_HEISEI, 1, 1989, UCAL_JANUARY, 8,
320 JapaneseCalendar_HEISEI, 1, 1989, UCAL_JANUARY, 9,
321 JapaneseCalendar_HEISEI, 1, 1989, UCAL_DECEMBER, 20,
322 JapaneseCalendar_HEISEI, 15, 2003, UCAL_MAY, 22,
323 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1
324 };
325
326 Calendar *cal;
327 UErrorCode status = U_ZERO_ERROR;
328 cal = Calendar::createInstance("ja_JP@calendar=japanese", status);
329 CHECK(status, UnicodeString("Creating ja_JP@calendar=japanese calendar"));
330 // Sanity check the calendar
331 UDate timeB = Calendar::getNow();
332 UDate timeCal = cal->getTime(status);
333
334 if(!(timeA <= timeCal) || !(timeCal <= timeB)) {
335 errln((UnicodeString)"Error: Calendar time " + timeCal +
336 " is not within sampled times [" + timeA + " to " + timeB + "]!");
337 }
338 // end sanity check
339 quasiGregorianTest(*cal,Locale("ja_JP"),data);
340 delete cal;
341 }
342
343 void IntlCalendarTest::TestBuddhistFormat() {
344 UErrorCode status = U_ZERO_ERROR;
345
346 // Test simple parse/format with adopt
347
348 // First, a contrived english test..
349 UDate aDate = 999932400000.0;
350 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=buddhist"), status);
351 CHECK(status, "creating date format instance");
352 SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status);
353 CHECK(status, "creating gregorian date format instance");
354 if(!fmt) {
355 errln("Coudln't create en_US instance");
356 } else {
357 UnicodeString str;
358 fmt2->format(aDate, str);
359 logln(UnicodeString() + "Test Date: " + str);
360 str.remove();
361 fmt->format(aDate, str);
362 logln(UnicodeString() + "as Buddhist Calendar: " + escape(str));
363 UnicodeString expected("September 8, 2544 BE");
364 if(str != expected) {
365 errln("Expected " + escape(expected) + " but got " + escape(str));
366 }
367 UDate otherDate = fmt->parse(expected, status);
368 if(otherDate != aDate) {
369 UnicodeString str3;
370 fmt->format(otherDate, str3);
371 errln("Parse incorrect of " + escape(expected) + " - wanted " + aDate + " but got " + otherDate + ", " + escape(str3));
372 } else {
373 logln("Parsed OK: " + expected);
374 }
375 delete fmt;
376 }
377 delete fmt2;
378
379 CHECK(status, "Error occured testing Buddhist Calendar in English ");
380
381 status = U_ZERO_ERROR;
382 // Now, try in Thai
383 {
384 UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
385 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
386 UDate expectDate = 999932400000.0;
387 Locale loc("th_TH_TRADITIONAL"); // legacy
388
389 simpleTest(loc, expect, expectDate, status);
390 }
391 status = U_ZERO_ERROR;
392 {
393 UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
394 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e1e.\\u0e28. 2544");
395 UDate expectDate = 999932400000.0;
396 Locale loc("th_TH@calendar=buddhist");
397
398 simpleTest(loc, expect, expectDate, status);
399 }
400 status = U_ZERO_ERROR;
401 {
402 UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
403 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
404 UDate expectDate = 999932400000.0;
405 Locale loc("th_TH@calendar=gregorian");
406
407 simpleTest(loc, expect, expectDate, status);
408 }
409 status = U_ZERO_ERROR;
410 {
411 UnicodeString expect = CharsToUnicodeString("\\u0E27\\u0E31\\u0E19\\u0E40\\u0E2A\\u0E32\\u0E23\\u0E4C\\u0E17\\u0E35\\u0E48"
412 " 8 \\u0E01\\u0E31\\u0e19\\u0e22\\u0e32\\u0e22\\u0e19 \\u0e04.\\u0e28. 2001");
413 UDate expectDate = 999932400000.0;
414 Locale loc("th_TH_TRADITIONAL@calendar=gregorian");
415
416 simpleTest(loc, expect, expectDate, status);
417 }
418 }
419
420
421 void IntlCalendarTest::TestJapaneseFormat() {
422 Calendar *cal;
423 UErrorCode status = U_ZERO_ERROR;
424 cal = Calendar::createInstance("ja_JP_TRADITIONAL", status);
425 CHECK(status, UnicodeString("Creating ja_JP_TRADITIONAL calendar"));
426
427 Calendar *cal2 = cal->clone();
428 delete cal;
429 cal = NULL;
430
431 // Test simple parse/format with adopt
432
433 UDate aDate = 999932400000.0;
434 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("MMMM d, yy G"), Locale("en_US@calendar=japanese"), status);
435 SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status);
436 CHECK(status, "creating date format instance");
437 if(!fmt) {
438 errln("Coudln't create en_US instance");
439 } else {
440 UnicodeString str;
441 fmt2->format(aDate, str);
442 logln(UnicodeString() + "Test Date: " + str);
443 str.remove();
444 fmt->format(aDate, str);
445 logln(UnicodeString() + "as Japanese Calendar: " + str);
446 UnicodeString expected("September 8, 13 Heisei");
447 if(str != expected) {
448 errln("Expected " + expected + " but got " + str);
449 }
450 UDate otherDate = fmt->parse(expected, status);
451 if(otherDate != aDate) {
452 UnicodeString str3;
453 ParsePosition pp;
454 fmt->parse(expected, *cal2, pp);
455 fmt->format(otherDate, str3);
456 errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " + otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
457
458 } else {
459 logln("Parsed OK: " + expected);
460 }
461 delete fmt;
462 }
463
464 // Test parse with incomplete information
465 fmt = new SimpleDateFormat(UnicodeString("G y"), Locale("en_US@calendar=japanese"), status);
466 /* The test data below should points to 1868-09-08T00:00:00 in America/Los_Angeles.
467 * The time calculated by original test code uses -7:00 UTC offset, because it assumes
468 * DST is observed (because of a timezone bug, DST is observed for early 20th century
469 * day to infinite past time). The bug was fixed and DST is no longer used for time before
470 * 1900 for any zones. However, ICU timezone transition data is represented by 32-bit integer
471 * (sec) and cannot represent transitions before 1901 defined in Olson tzdata. For example,
472 * based on Olson definition, offset -7:52:58 should be used for Nov 18, 1883 or older dates.
473 * If ICU properly capture entire Olson zone definition, the start time of "Meiji 1" is
474 * -3197117222000. -Yoshito
475 */
476 /* TODO: When ICU support the Olson LMT offset for America/Los_Angeles, we need to update
477 * the reference data.
478 */
479 //aDate = -3197120400000.;
480 aDate = -3197116800000.;
481 CHECK(status, "creating date format instance");
482 if(!fmt) {
483 errln("Coudln't create en_US instance");
484 } else {
485 UnicodeString str;
486 fmt2->format(aDate, str);
487 logln(UnicodeString() + "Test Date: " + str);
488 str.remove();
489 fmt->format(aDate, str);
490 logln(UnicodeString() + "as Japanese Calendar: " + str);
491 UnicodeString expected("Meiji 1");
492 if(str != expected) {
493 errln("Expected " + expected + " but got " + str);
494 }
495 UDate otherDate = fmt->parse(expected, status);
496 if(otherDate != aDate) {
497 UnicodeString str3;
498 ParsePosition pp;
499 fmt->parse(expected, *cal2, pp);
500 fmt->format(otherDate, str3);
501 errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " +
502 otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
503 } else {
504 logln("Parsed OK: " + expected);
505 }
506 delete fmt;
507 }
508
509 delete cal2;
510 delete fmt2;
511 CHECK(status, "Error occured");
512
513 // Now, try in Japanese
514 {
515 UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
516 UDate expectDate = 999932400000.0; // Testing a recent date
517 Locale loc("ja_JP@calendar=japanese");
518
519 status = U_ZERO_ERROR;
520 simpleTest(loc, expect, expectDate, status);
521 }
522 {
523 UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
524 UDate expectDate = 999932400000.0; // Testing a recent date
525 Locale loc("ja_JP_TRADITIONAL"); // legacy
526
527 status = U_ZERO_ERROR;
528 simpleTest(loc, expect, expectDate, status);
529 }
530 {
531 UnicodeString expect = CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5\\u6728\\u66dc\\u65e5");
532 //UDate expectDate = -6106035600000.0;
533 UDate expectDate = -6106032000000.0; // 1776-07-04T00:00:00Z-0800
534 Locale loc("ja_JP@calendar=japanese");
535
536 status = U_ZERO_ERROR;
537 simpleTest(loc, expect, expectDate, status);
538
539 }
540 { // Jitterbug 1869 - this is an ambiguous era. (Showa 64 = Jan 6 1989, but Showa could be 2 other eras) )
541 UnicodeString expect = CharsToUnicodeString("\\u662d\\u548c64\\u5e741\\u67086\\u65e5\\u91d1\\u66dc\\u65e5");
542 UDate expectDate = 600076800000.0;
543 Locale loc("ja_JP@calendar=japanese");
544
545 status = U_ZERO_ERROR;
546 simpleTest(loc, expect, expectDate, status);
547
548 }
549 { // This Feb 29th falls on a leap year by gregorian year, but not by Japanese year.
550 UnicodeString expect = CharsToUnicodeString("\\u5EB7\\u6B632\\u5e742\\u670829\\u65e5\\u65e5\\u66dc\\u65e5");
551 // Add -1:00 to the following for historical TZ - aliu
552 //UDate expectDate = -16214403600000.0; // courtesy of date format round trip test
553 UDate expectDate = -16214400000000.0; // 1456-03-09T00:00:00Z-0800
554 Locale loc("ja_JP@calendar=japanese");
555
556 status = U_ZERO_ERROR;
557 simpleTest(loc, expect, expectDate, status);
558
559 }
560 }
561
562 void IntlCalendarTest::TestJapanese3860()
563 {
564 Calendar *cal;
565 UErrorCode status = U_ZERO_ERROR;
566 cal = Calendar::createInstance("ja_JP@calendar=japanese", status);
567 CHECK(status, UnicodeString("Creating ja_JP@calendar=japanese calendar"));
568 Calendar *cal2 = cal->clone();
569 SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("HH:mm:ss.S MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status);
570 UnicodeString str;
571
572
573 {
574 // Test simple parse/format with adopt
575 UDate aDate = 0;
576
577 // Test parse with missing era (should default to current era, heisei)
578 // Test parse with incomplete information
579 logln("Testing parse w/ missing era...");
580 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("y.M.d"), Locale("ja_JP@calendar=japanese"), status);
581 CHECK(status, "creating date format instance");
582 if(!fmt) {
583 errln("Coudln't create en_US instance");
584 } else {
585 UErrorCode s2 = U_ZERO_ERROR;
586 cal2->clear();
587 UnicodeString samplestr("1.1.9");
588 logln(UnicodeString() + "Test Year: " + samplestr);
589 aDate = fmt->parse(samplestr, s2);
590 ParsePosition pp=0;
591 fmt->parse(samplestr, *cal2, pp);
592 CHECK(s2, "parsing the 1.1.9 string");
593 logln("*cal2 after 119 parse:");
594 str.remove();
595 fmt2->format(aDate, str);
596 logln(UnicodeString() + "as Gregorian Calendar: " + str);
597
598 cal2->setTime(aDate, s2);
599 int32_t gotYear = cal2->get(UCAL_YEAR, s2);
600 int32_t gotEra = cal2->get(UCAL_ERA, s2);
601 int32_t expectYear = 1;
602 int32_t expectEra = JapaneseCalendar::kCurrentEra;
603 if((gotYear!=1) || (gotEra != expectEra)) {
604 errln(UnicodeString("parse "+samplestr+" of 'y.m.d' as Japanese Calendar, expected year ") + expectYear +
605 UnicodeString(" and era ") + expectEra +", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
606 } else {
607 logln(UnicodeString() + " year: " + gotYear + ", era: " + gotEra);
608 }
609 delete fmt;
610 }
611 }
612
613 #if 0
614 // this will NOT work - *all the time*. If it is the 1st of the month, for example it will get Jan 1 heisei 1 => jan 1 showa 64, wrong era.
615 {
616 // Test simple parse/format with adopt
617 UDate aDate = 0;
618
619 // Test parse with missing era (should default to current era, heisei)
620 // Test parse with incomplete information
621 logln("Testing parse w/ just year...");
622 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("y"), Locale("ja_JP@calendar=japanese"), status);
623 CHECK(status, "creating date format instance");
624 if(!fmt) {
625 errln("Coudln't create en_US instance");
626 } else {
627 UErrorCode s2 = U_ZERO_ERROR;
628 cal2->clear();
629 UnicodeString samplestr("1");
630 logln(UnicodeString() + "Test Year: " + samplestr);
631 aDate = fmt->parse(samplestr, s2);
632 ParsePosition pp=0;
633 fmt->parse(samplestr, *cal2, pp);
634 CHECK(s2, "parsing the 1 string");
635 logln("*cal2 after 1 parse:");
636 str.remove();
637 fmt2->format(aDate, str);
638 logln(UnicodeString() + "as Gregorian Calendar: " + str);
639
640 cal2->setTime(aDate, s2);
641 int32_t gotYear = cal2->get(UCAL_YEAR, s2);
642 int32_t gotEra = cal2->get(UCAL_ERA, s2);
643 int32_t expectYear = 1;
644 int32_t expectEra = JapaneseCalendar::kCurrentEra;
645 if((gotYear!=1) || (gotEra != expectEra)) {
646 errln(UnicodeString("parse "+samplestr+" of 'y' as Japanese Calendar, expected year ") + expectYear +
647 UnicodeString(" and era ") + expectEra +", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
648 } else {
649 logln(UnicodeString() + " year: " + gotYear + ", era: " + gotEra);
650 }
651 delete fmt;
652 }
653 }
654 #endif
655 delete cal2;
656 delete cal;
657 delete fmt2;
658 }
659
660 void IntlCalendarTest::simpleTest(const Locale& loc, const UnicodeString& expect, UDate expectDate, UErrorCode& status)
661 {
662 UnicodeString tmp;
663 UDate d;
664 DateFormat *fmt0 = DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull);
665
666 logln("Try format/parse of " + (UnicodeString)loc.getName());
667 DateFormat *fmt2 = DateFormat::createDateInstance(DateFormat::kFull, loc);
668 if(fmt2) {
669 fmt2->format(expectDate, tmp);
670 logln(escape(tmp) + " ( in locale " + loc.getName() + ")");
671 if(tmp != expect) {
672 errln(UnicodeString("Failed to format " ) + loc.getName() + " expected " + escape(expect) + " got " + escape(tmp) );
673 }
674
675 d = fmt2->parse(expect,status);
676 CHECK(status, "Error occured parsing " + UnicodeString(loc.getName()));
677 if(d != expectDate) {
678 fmt2->format(d,tmp);
679 errln(UnicodeString("Failed to parse " ) + escape(expect) + ", " + loc.getName() + " expect " + (double)expectDate + " got " + (double)d + " " + escape(tmp));
680 logln( "wanted " + escape(fmt0->format(expectDate,tmp.remove())) + " but got " + escape(fmt0->format(d,tmp.remove())));
681 }
682 delete fmt2;
683 } else {
684 errln((UnicodeString)"Can't create " + loc.getName() + " date instance");
685 }
686 delete fmt0;
687 }
688
689 #undef CHECK
690
691 #endif /* #if !UCONFIG_NO_FORMATTING */
692
693 //eof