]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/incaltst.cpp
ICU-8.11.1.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 aDate = -3197120400000.;
467 CHECK(status, "creating date format instance");
468 if(!fmt) {
469 errln("Coudln't create en_US instance");
470 } else {
471 UnicodeString str;
472 fmt2->format(aDate, str);
473 logln(UnicodeString() + "Test Date: " + str);
474 str.remove();
475 fmt->format(aDate, str);
476 logln(UnicodeString() + "as Japanese Calendar: " + str);
477 UnicodeString expected("Meiji 1");
478 if(str != expected) {
479 errln("Expected " + expected + " but got " + str);
480 }
481 UDate otherDate = fmt->parse(expected, status);
482 if(otherDate != aDate) {
483 UnicodeString str3;
484 ParsePosition pp;
485 fmt->parse(expected, *cal2, pp);
486 fmt->format(otherDate, str3);
487 errln("Parse incorrect of " + expected + " - wanted " + aDate + " but got " + " = " +
488 otherDate + ", " + str3 + " = " + CalendarTest::calToStr(*cal2) );
489 } else {
490 logln("Parsed OK: " + expected);
491 }
492 delete fmt;
493 }
494
495 delete cal2;
496 delete fmt2;
497 CHECK(status, "Error occured");
498
499 // Now, try in Japanese
500 {
501 UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
502 UDate expectDate = 999932400000.0; // Testing a recent date
503 Locale loc("ja_JP@calendar=japanese");
504
505 status = U_ZERO_ERROR;
506 simpleTest(loc, expect, expectDate, status);
507 }
508 {
509 UnicodeString expect = CharsToUnicodeString("\\u5e73\\u621013\\u5e749\\u67088\\u65e5\\u571f\\u66dc\\u65e5");
510 UDate expectDate = 999932400000.0; // Testing a recent date
511 Locale loc("ja_JP_TRADITIONAL"); // legacy
512
513 status = U_ZERO_ERROR;
514 simpleTest(loc, expect, expectDate, status);
515 }
516 {
517 UnicodeString expect = CharsToUnicodeString("\\u5b89\\u6c385\\u5e747\\u67084\\u65e5\\u6728\\u66dc\\u65e5");
518 UDate expectDate = -6106035600000.0;
519 Locale loc("ja_JP@calendar=japanese");
520
521 status = U_ZERO_ERROR;
522 simpleTest(loc, expect, expectDate, status);
523
524 }
525 { // Jitterbug 1869 - this is an ambiguous era. (Showa 64 = Jan 6 1989, but Showa could be 2 other eras) )
526 UnicodeString expect = CharsToUnicodeString("\\u662d\\u548c64\\u5e741\\u67086\\u65e5\\u91d1\\u66dc\\u65e5");
527 UDate expectDate = 600076800000.0;
528 Locale loc("ja_JP@calendar=japanese");
529
530 status = U_ZERO_ERROR;
531 simpleTest(loc, expect, expectDate, status);
532
533 }
534 { // This Feb 29th falls on a leap year by gregorian year, but not by Japanese year.
535 UnicodeString expect = CharsToUnicodeString("\\u5EB7\\u6B632\\u5e742\\u670829\\u65e5\\u65e5\\u66dc\\u65e5");
536 // Add -1:00 to the following for historical TZ - aliu
537 UDate expectDate = -16214403600000.0; // courtesy of date format round trip test
538 Locale loc("ja_JP@calendar=japanese");
539
540 status = U_ZERO_ERROR;
541 simpleTest(loc, expect, expectDate, status);
542
543 }
544 }
545
546 void IntlCalendarTest::TestJapanese3860()
547 {
548 Calendar *cal;
549 UErrorCode status = U_ZERO_ERROR;
550 cal = Calendar::createInstance("ja_JP@calendar=japanese", status);
551 CHECK(status, UnicodeString("Creating ja_JP@calendar=japanese calendar"));
552 Calendar *cal2 = cal->clone();
553 SimpleDateFormat *fmt2 = new SimpleDateFormat(UnicodeString("HH:mm:ss.S MMMM d, yyyy G"), Locale("en_US@calendar=gregorian"), status);
554 UnicodeString str;
555
556
557 {
558 // Test simple parse/format with adopt
559 UDate aDate = 0;
560
561 // Test parse with missing era (should default to current era, heisei)
562 // Test parse with incomplete information
563 logln("Testing parse w/ missing era...");
564 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("y.M.d"), Locale("ja_JP@calendar=japanese"), status);
565 CHECK(status, "creating date format instance");
566 if(!fmt) {
567 errln("Coudln't create en_US instance");
568 } else {
569 UErrorCode s2 = U_ZERO_ERROR;
570 cal2->clear();
571 UnicodeString samplestr("1.1.9");
572 logln(UnicodeString() + "Test Year: " + samplestr);
573 aDate = fmt->parse(samplestr, s2);
574 ParsePosition pp=0;
575 fmt->parse(samplestr, *cal2, pp);
576 CHECK(s2, "parsing the 1.1.9 string");
577 logln("*cal2 after 119 parse:");
578 str.remove();
579 fmt2->format(aDate, str);
580 logln(UnicodeString() + "as Gregorian Calendar: " + str);
581
582 cal2->setTime(aDate, s2);
583 int32_t gotYear = cal2->get(UCAL_YEAR, s2);
584 int32_t gotEra = cal2->get(UCAL_ERA, s2);
585 int32_t expectYear = 1;
586 int32_t expectEra = JapaneseCalendar::kCurrentEra;
587 if((gotYear!=1) || (gotEra != expectEra)) {
588 errln(UnicodeString("parse "+samplestr+" of 'y.m.d' as Japanese Calendar, expected year ") + expectYear +
589 UnicodeString(" and era ") + expectEra +", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
590 } else {
591 logln(UnicodeString() + " year: " + gotYear + ", era: " + gotEra);
592 }
593 delete fmt;
594 }
595 }
596
597 #if 0
598 // 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.
599 {
600 // Test simple parse/format with adopt
601 UDate aDate = 0;
602
603 // Test parse with missing era (should default to current era, heisei)
604 // Test parse with incomplete information
605 logln("Testing parse w/ just year...");
606 SimpleDateFormat *fmt = new SimpleDateFormat(UnicodeString("y"), Locale("ja_JP@calendar=japanese"), status);
607 CHECK(status, "creating date format instance");
608 if(!fmt) {
609 errln("Coudln't create en_US instance");
610 } else {
611 UErrorCode s2 = U_ZERO_ERROR;
612 cal2->clear();
613 UnicodeString samplestr("1");
614 logln(UnicodeString() + "Test Year: " + samplestr);
615 aDate = fmt->parse(samplestr, s2);
616 ParsePosition pp=0;
617 fmt->parse(samplestr, *cal2, pp);
618 CHECK(s2, "parsing the 1 string");
619 logln("*cal2 after 1 parse:");
620 str.remove();
621 fmt2->format(aDate, str);
622 logln(UnicodeString() + "as Gregorian Calendar: " + str);
623
624 cal2->setTime(aDate, s2);
625 int32_t gotYear = cal2->get(UCAL_YEAR, s2);
626 int32_t gotEra = cal2->get(UCAL_ERA, s2);
627 int32_t expectYear = 1;
628 int32_t expectEra = JapaneseCalendar::kCurrentEra;
629 if((gotYear!=1) || (gotEra != expectEra)) {
630 errln(UnicodeString("parse "+samplestr+" of 'y' as Japanese Calendar, expected year ") + expectYear +
631 UnicodeString(" and era ") + expectEra +", but got year " + gotYear + " and era " + gotEra + " (Gregorian:" + str +")");
632 } else {
633 logln(UnicodeString() + " year: " + gotYear + ", era: " + gotEra);
634 }
635 delete fmt;
636 }
637 }
638 #endif
639 delete cal2;
640 delete cal;
641 delete fmt2;
642 }
643
644 void IntlCalendarTest::simpleTest(const Locale& loc, const UnicodeString& expect, UDate expectDate, UErrorCode& status)
645 {
646 UnicodeString tmp;
647 UDate d;
648 DateFormat *fmt0 = DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull);
649
650 logln("Try format/parse of " + (UnicodeString)loc.getName());
651 DateFormat *fmt2 = DateFormat::createDateInstance(DateFormat::kFull, loc);
652 if(fmt2) {
653 fmt2->format(expectDate, tmp);
654 logln(escape(tmp) + " ( in locale " + loc.getName() + ")");
655 if(tmp != expect) {
656 errln(UnicodeString("Failed to format " ) + loc.getName() + " expected " + escape(expect) + " got " + escape(tmp) );
657 }
658
659 d = fmt2->parse(expect,status);
660 CHECK(status, "Error occured parsing " + UnicodeString(loc.getName()));
661 if(d != expectDate) {
662 fmt2->format(d,tmp);
663 errln(UnicodeString("Failed to parse " ) + escape(expect) + ", " + loc.getName() + " expect " + (double)expectDate + " got " + (double)d + " " + escape(tmp));
664 logln( "wanted " + escape(fmt0->format(expectDate,tmp.remove())) + " but got " + escape(fmt0->format(d,tmp.remove())));
665 }
666 delete fmt2;
667 } else {
668 errln((UnicodeString)"Can't create " + loc.getName() + " date instance");
669 }
670 delete fmt0;
671 }
672
673 #undef CHECK
674
675 #endif /* #if !UCONFIG_NO_FORMATTING */
676
677 //eof