dtptngts.cpp revision c69afcec261fc345fda8daf46f0ea6b4351dc777
1/******************************************************************** 2 * COPYRIGHT: 3 * Copyright (c) 2008, International Business Machines Corporation and 4 * others. All Rights Reserved. 5 ********************************************************************/ 6 7#include "unicode/utypes.h" 8 9#if !UCONFIG_NO_FORMATTING 10 11#include <stdio.h> 12#include <stdlib.h> 13#include "dtptngts.h" 14 15#include "unicode/calendar.h" 16#include "unicode/smpdtfmt.h" 17#include "unicode/dtfmtsym.h" 18#include "unicode/dtptngen.h" 19#include "loctest.h" 20 21 22// This is an API test, not a unit test. It doesn't test very many cases, and doesn't 23// try to test the full functionality. It just calls each function in the class and 24// verifies that it works on a basic level. 25 26void IntlTestDateTimePatternGeneratorAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) 27{ 28 if (exec) logln("TestSuite DateTimePatternGeneratorAPI"); 29 switch (index) { 30 TESTCASE(0, testAPI); 31 default: name = ""; break; 32 } 33} 34 35#define MAX_LOCALE 4 36 37/** 38 * Test various generic API methods of DateTimePatternGenerator for API coverage. 39 */ 40void IntlTestDateTimePatternGeneratorAPI::testAPI(/*char *par*/) 41{ 42 UnicodeString patternData[] = { 43 UnicodeString("yM"), 44 UnicodeString("yMMM"), 45 UnicodeString("yMd"), 46 UnicodeString("yMMMd"), 47 UnicodeString("Md"), 48 UnicodeString("MMMd"), 49 UnicodeString("yQQQ"), 50 UnicodeString("hhmm"), 51 UnicodeString("HHmm"), 52 UnicodeString("mmss"), 53 UnicodeString(), 54 }; 55 56 const char* testLocale[MAX_LOCALE][3] = { 57 {"en", "US", ""}, 58 {"zh", "Hans", "CN"}, 59 {"de", "DE", ""}, 60 {"fi", "", ""}, 61 }; 62 63 UnicodeString patternResults[] = { 64 UnicodeString("1/1999"), // en_US 65 UnicodeString("Jan 1999"), 66 UnicodeString("1/13/1999"), 67 UnicodeString("Jan 13, 1999"), 68 UnicodeString("1/13"), 69 UnicodeString("Jan 13"), 70 UnicodeString("Q1 1999"), 71 UnicodeString("11:58 PM"), 72 UnicodeString("23:58"), 73 UnicodeString("58:59"), 74 UnicodeString("1999-1", -1, US_INV), // zh_Hans_CN 75 UnicodeString("1999-01", -1, US_INV), 76 CharsToUnicodeString("1999\\u5E741\\u670813\\u65E5"), 77 CharsToUnicodeString("1999\\u5E7401\\u670813\\u65E5"), 78 UnicodeString("1-13"), 79 UnicodeString("01-13"), 80 CharsToUnicodeString("1999\\u5E741\\u5B63"), 81 CharsToUnicodeString("\\u4E0B\\u534811:58"), 82 CharsToUnicodeString("23:58"), 83 UnicodeString("58:59"), 84 UnicodeString("1999-1"), // de_DE 85 UnicodeString("Jan 1999"), 86 UnicodeString("13.1.1999"), 87 UnicodeString("13. Jan 1999"), 88 UnicodeString("13.1."), 89 UnicodeString("13. Jan"), 90 UnicodeString("Q1 1999"), 91 UnicodeString("23:58"), 92 UnicodeString("23:58"), 93 UnicodeString("58:59"), 94 UnicodeString("1/1999"), // fi 95 UnicodeString("tammi 1999"), 96 UnicodeString("13.1.1999"), 97 UnicodeString("13. tammi 1999"), 98 UnicodeString("13.1."), 99 UnicodeString("13. tammi"), 100 UnicodeString("1. nelj. 1999"), 101 UnicodeString("23.58"), 102 UnicodeString("23.58"), 103 UnicodeString("58.59"), 104 UnicodeString(), 105 }; 106 107 UnicodeString patternTests2[] = { 108 UnicodeString("yyyyMMMdd"), 109 UnicodeString("EyyyyMMMdd"), 110 UnicodeString("yyyyMMdd"), 111 UnicodeString("yyyyMMM"), 112 UnicodeString("yyyyMM"), 113 UnicodeString("yyMM"), 114 UnicodeString("yMMMMMd"), 115 UnicodeString("EEEEEMMMMMd"), 116 UnicodeString("MMMd"), 117 UnicodeString("MMMdhmm"), 118 UnicodeString("EMMMdhmms"), 119 UnicodeString("MMdhmm"), 120 UnicodeString("EEEEMMMdhmms"), 121 UnicodeString("yyyyMMMddhhmmss"), 122 UnicodeString("EyyyyMMMddhhmmss"), 123 UnicodeString("hmm"), 124 UnicodeString("hhmm"), 125 UnicodeString(""), 126 }; 127 UnicodeString patternResults2[] = { 128 UnicodeString("Oct 14, 1999"), 129 UnicodeString("Thu, Oct 14, 1999"), 130 UnicodeString("10/14/1999"), 131 UnicodeString("Oct 1999"), 132 UnicodeString("10/1999"), 133 UnicodeString("10/99"), 134 UnicodeString("O/14/1999"), 135 UnicodeString("T, O 14"), 136 UnicodeString("Oct 14"), 137 UnicodeString("Oct 14 6:58 AM"), 138 UnicodeString("Thu, Oct 14 6:58:59 AM"), 139 UnicodeString("10/14 6:58 AM"), 140 UnicodeString("Thursday, Oct 14 6:58:59 AM"), 141 UnicodeString("Oct 14, 1999 06:58:59 AM"), 142 UnicodeString("Thu, Oct 14, 1999 06:58:59 AM"), 143 UnicodeString("6:58 AM"), 144 UnicodeString("06:58 AM"), 145 UnicodeString(""), 146 }; 147 148 // results for getSkeletons() and getPatternForSkeleton() 149 const UnicodeString testSkeletonsResults[] = { 150 UnicodeString("HH:mm"), 151 UnicodeString("MMMMd"), 152 UnicodeString("MMMMMd"), 153 }; 154 155 const UnicodeString testBaseSkeletonsResults[] = { 156 UnicodeString("Hm"), 157 UnicodeString("MMMd"), 158 UnicodeString("MMMd"), 159 }; 160 161 UnicodeString newDecimal(" "); // space 162 UnicodeString newAppendItemName("hrs."); 163 UnicodeString newAppendItemFormat("{1} {0}"); 164 UnicodeString newDateTimeFormat("{1} {0}"); 165 UErrorCode status = U_ZERO_ERROR; 166 UnicodeString conflictingPattern; 167 UDateTimePatternConflict conflictingStatus; 168 169 // ======= Test CreateInstance with default locale 170 logln("Testing DateTimePatternGenerator createInstance from default locale"); 171 172 DateTimePatternGenerator *instFromDefaultLocale=DateTimePatternGenerator::createInstance(status); 173 if (U_FAILURE(status)) { 174 dataerrln("ERROR: Could not create DateTimePatternGenerator (default) - exitting"); 175 return; 176 } 177 else { 178 delete instFromDefaultLocale; 179 } 180 181 // ======= Test CreateInstance with given locale 182 logln("Testing DateTimePatternGenerator createInstance from French locale"); 183 status = U_ZERO_ERROR; 184 DateTimePatternGenerator *instFromLocale=DateTimePatternGenerator::createInstance(Locale::getFrench(), status); 185 if (U_FAILURE(status)) { 186 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getFrench()) - exitting"); 187 return; 188 } 189 190 // ======= Test clone DateTimePatternGenerator 191 logln("Testing DateTimePatternGenerator::clone()"); 192 status = U_ZERO_ERROR; 193 194 195 UnicodeString decimalSymbol = instFromLocale->getDecimal(); 196 UnicodeString newDecimalSymbol = UnicodeString("*"); 197 decimalSymbol = instFromLocale->getDecimal(); 198 instFromLocale->setDecimal(newDecimalSymbol); 199 DateTimePatternGenerator *cloneDTPatternGen=instFromLocale->clone(); 200 decimalSymbol = cloneDTPatternGen->getDecimal(); 201 if (decimalSymbol != newDecimalSymbol) { 202 errln("ERROR: inconsistency is found in cloned object."); 203 } 204 if ( !(*cloneDTPatternGen == *instFromLocale) ) { 205 errln("ERROR: inconsistency is found in cloned object."); 206 } 207 208 if ( *cloneDTPatternGen != *instFromLocale ) { 209 errln("ERROR: inconsistency is found in cloned object."); 210 } 211 212 delete instFromLocale; 213 delete cloneDTPatternGen; 214 215 // ======= Test simple use cases 216 logln("Testing simple use cases"); 217 status = U_ZERO_ERROR; 218 Locale deLocale=Locale::getGermany(); 219 UDate sampleDate=LocaleTest::date(99, 9, 13, 23, 58, 59); 220 DateTimePatternGenerator *gen = DateTimePatternGenerator::createInstance(deLocale, status); 221 if (U_FAILURE(status)) { 222 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getGermany()) - exitting"); 223 return; 224 } 225 UnicodeString findPattern = gen->getBestPattern(UnicodeString("MMMddHmm"), status); 226 SimpleDateFormat *format = new SimpleDateFormat(findPattern, deLocale, status); 227 if (U_FAILURE(status)) { 228 dataerrln("ERROR: Could not create SimpleDateFormat (Locale::getGermany())"); 229 delete gen; 230 return; 231 } 232 TimeZone *zone = TimeZone::createTimeZone(UnicodeString("ECT")); 233 if (zone==NULL) { 234 dataerrln("ERROR: Could not create TimeZone ECT"); 235 delete gen; 236 delete format; 237 return; 238 } 239 format->setTimeZone(*zone); 240 UnicodeString dateReturned, expectedResult; 241 dateReturned.remove(); 242 dateReturned = format->format(sampleDate, dateReturned, status); 243 expectedResult=UnicodeString("14. Okt 8:58", -1, US_INV); 244 if ( dateReturned != expectedResult ) { 245 errln("ERROR: Simple test in getBestPattern with Locale::getGermany())."); 246 } 247 // add new pattern 248 status = U_ZERO_ERROR; 249 conflictingStatus = gen->addPattern(UnicodeString("d'. von' MMMM", -1, US_INV), true, conflictingPattern, status); 250 if (U_FAILURE(status)) { 251 errln("ERROR: Could not addPattern - d\'. von\' MMMM"); 252 } 253 status = U_ZERO_ERROR; 254 UnicodeString testPattern=gen->getBestPattern(UnicodeString("MMMMdd"), status); 255 testPattern=gen->getBestPattern(UnicodeString("MMMddHmm"), status); 256 format->applyPattern(gen->getBestPattern(UnicodeString("MMMMddHmm"), status)); 257 dateReturned.remove(); 258 dateReturned = format->format(sampleDate, dateReturned, status); 259 expectedResult=UnicodeString("14. von Oktober 8:58", -1, US_INV); 260 if ( dateReturned != expectedResult ) { 261 errln("ERROR: Simple test addPattern failed!: d\'. von\' MMMM "); 262 } 263 delete format; 264 265 // get a pattern and modify it 266 format = (SimpleDateFormat *)DateFormat::createDateTimeInstance(DateFormat::kFull, DateFormat::kFull, 267 deLocale); 268 format->setTimeZone(*zone); 269 UnicodeString pattern; 270 pattern = format->toPattern(pattern); 271 dateReturned.remove(); 272 dateReturned = format->format(sampleDate, dateReturned, status); 273 expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich"); 274 if ( dateReturned != expectedResult ) { 275 errln("ERROR: Simple test uses full date format."); 276 errln(UnicodeString(" Got: ") + dateReturned + UnicodeString(" Expected: ") + expectedResult); 277 } 278 279 // modify it to change the zone. 280 UnicodeString newPattern = gen->replaceFieldTypes(pattern, UnicodeString("vvvv"), status); 281 format->applyPattern(newPattern); 282 dateReturned.remove(); 283 dateReturned = format->format(sampleDate, dateReturned, status); 284 expectedResult=UnicodeString("Donnerstag, 14. Oktober 1999 08:58:59 Frankreich"); 285 if ( dateReturned != expectedResult ) { 286 errln("ERROR: Simple test modify the timezone!"); 287 errln(UnicodeString(" Got: ")+ dateReturned + UnicodeString(" Expected: ") + expectedResult); 288 } 289 290 // setDeciaml(), getDeciaml() 291 gen->setDecimal(newDecimal); 292 if (newDecimal != gen->getDecimal()) { 293 errln("ERROR: unexpected result from setDecimal() and getDecimal()!.\n"); 294 } 295 296 // setAppenItemName() , getAppendItemName() 297 gen->setAppendItemName(UDATPG_HOUR_FIELD, newAppendItemName); 298 if (newAppendItemName != gen->getAppendItemName(UDATPG_HOUR_FIELD)) { 299 errln("ERROR: unexpected result from setAppendItemName() and getAppendItemName()!.\n"); 300 } 301 302 // setAppenItemFormat() , getAppendItemFormat() 303 gen->setAppendItemFormat(UDATPG_HOUR_FIELD, newAppendItemFormat); 304 if (newAppendItemFormat != gen->getAppendItemFormat(UDATPG_HOUR_FIELD)) { 305 errln("ERROR: unexpected result from setAppendItemFormat() and getAppendItemFormat()!.\n"); 306 } 307 308 // setDateTimeFormat() , getDateTimeFormat() 309 gen->setDateTimeFormat(newDateTimeFormat); 310 if (newDateTimeFormat != gen->getDateTimeFormat()) { 311 errln("ERROR: unexpected result from setDateTimeFormat() and getDateTimeFormat()!.\n"); 312 } 313 314 // ======== Test getSkeleton and getBaseSkeleton 315 status = U_ZERO_ERROR; 316 pattern = UnicodeString("dd-MMM"); 317 UnicodeString expectedSkeleton = UnicodeString("MMMdd"); 318 UnicodeString expectedBaseSkeleton = UnicodeString("MMMd"); 319 UnicodeString retSkeleton = gen->getSkeleton(pattern, status); 320 if(U_FAILURE(status) || retSkeleton != expectedSkeleton ) { 321 errln("ERROR: Unexpected result from getSkeleton().\n"); 322 errln(UnicodeString(" Got: ") + retSkeleton + UnicodeString(" Expected: ") + expectedSkeleton ); 323 } 324 retSkeleton = gen->getBaseSkeleton(pattern, status); 325 if(U_FAILURE(status) || retSkeleton != expectedBaseSkeleton) { 326 errln("ERROR: Unexpected result from getBaseSkeleton().\n"); 327 errln(UnicodeString(" Got: ") + retSkeleton + UnicodeString(" Expected:")+ expectedBaseSkeleton); 328 } 329 330 pattern = UnicodeString("dd/MMMM/yy"); 331 expectedSkeleton = UnicodeString("yyMMMMdd"); 332 expectedBaseSkeleton = UnicodeString("yMMMd"); 333 retSkeleton = gen->getSkeleton(pattern, status); 334 if(U_FAILURE(status) || retSkeleton != expectedSkeleton ) { 335 errln("ERROR: Unexpected result from getSkeleton().\n"); 336 errln(UnicodeString(" Got: ") + retSkeleton + UnicodeString(" Expected: ") + expectedSkeleton ); 337 } 338 retSkeleton = gen->getBaseSkeleton(pattern, status); 339 if(U_FAILURE(status) || retSkeleton != expectedBaseSkeleton) { 340 errln("ERROR: Unexpected result from getBaseSkeleton().\n"); 341 errln(UnicodeString(" Got: ") + retSkeleton + UnicodeString(" Expected:")+ expectedBaseSkeleton); 342 } 343 delete format; 344 delete zone; 345 delete gen; 346 347 { 348 // Trac# 6104 349 status = U_ZERO_ERROR; 350 pattern = UnicodeString("YYYYMMM"); 351 UnicodeString expR = CharsToUnicodeString("1999/01"); 352 Locale loc("ja"); 353 UDate testDate1= LocaleTest::date(99, 0, 13, 23, 58, 59); 354 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status); 355 if(U_FAILURE(status)) { 356 dataerrln("ERROR: Could not create DateTimePatternGenerator"); 357 return; 358 } 359 UnicodeString bPattern = patGen->getBestPattern(pattern, status); 360 UnicodeString rDate; 361 SimpleDateFormat sdf(bPattern, loc, status); 362 rDate.remove(); 363 rDate = sdf.format(testDate1, rDate); 364 365 logln(UnicodeString(" ja locale with skeleton: YYYYMMM Best Pattern:") + bPattern); 366 logln(UnicodeString(" Formatted date:") + rDate); 367 368 if ( expR!= rDate ) { 369 errln(UnicodeString("\nERROR: Test Japanese month hack Got: ") + rDate + 370 UnicodeString(" Expected: ") + expR ); 371 } 372 373 delete patGen; 374 } 375 { // Trac# 6104 376 Locale loc("zh"); 377 UnicodeString expR = UnicodeString("1999-01", -1, US_INV); 378 UDate testDate1= LocaleTest::date(99, 0, 13, 23, 58, 59); 379 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status); 380 if(U_FAILURE(status)) { 381 dataerrln("ERROR: Could not create DateTimePatternGenerator"); 382 return; 383 } 384 UnicodeString bPattern = patGen->getBestPattern(pattern, status); 385 UnicodeString rDate; 386 SimpleDateFormat sdf(bPattern, loc, status); 387 rDate.remove(); 388 rDate = sdf.format(testDate1, rDate); 389 390 logln(UnicodeString(" zh locale with skeleton: YYYYMMM Best Pattern:") + bPattern); 391 logln(UnicodeString(" Formatted date:") + rDate); 392 if ( expR!= rDate ) { 393 errln(UnicodeString("\nERROR: Test Chinese month hack Got: ") + rDate + 394 UnicodeString(" Expected: ") + expR ); 395 } 396 delete patGen; 397 } 398 399 { 400 // Trac# 6172 duplicate time pattern 401 status = U_ZERO_ERROR; 402 pattern = UnicodeString("hmv"); 403 UnicodeString expR = UnicodeString("h:m a v"); 404 Locale loc("en"); 405 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status); 406 if(U_FAILURE(status)) { 407 dataerrln("ERROR: Could not create DateTimePatternGenerator"); 408 return; 409 } 410 UnicodeString bPattern = patGen->getBestPattern(pattern, status); 411 logln(UnicodeString(" en locale with skeleton: hmv Best Pattern:") + bPattern); 412 413 if ( expR!= bPattern ) { 414 errln(UnicodeString("\nERROR: Test EN time format Got: ") + bPattern + 415 UnicodeString(" Expected: ") + expR ); 416 } 417 418 delete patGen; 419 } 420 421 422 // ======= Test various skeletons. 423 logln("Testing DateTimePatternGenerator with various skeleton"); 424 425 status = U_ZERO_ERROR; 426 int32_t localeIndex=0; 427 int32_t resultIndex=0; 428 UnicodeString resultDate; 429 UDate testDate= LocaleTest::date(99, 0, 13, 23, 58, 59); 430 while (localeIndex < MAX_LOCALE ) 431 { 432 int32_t dataIndex=0; 433 UnicodeString bestPattern; 434 435 Locale loc(testLocale[localeIndex][0], testLocale[localeIndex][1], testLocale[localeIndex][2], ""); 436 logln("\n\n Locale: %s_%s_%s", testLocale[localeIndex][0], testLocale[localeIndex][1], testLocale[localeIndex][2]); 437 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(loc, status); 438 if(U_FAILURE(status)) { 439 dataerrln("ERROR: Could not create DateTimePatternGenerator with locale index:%d . - exitting\n", localeIndex); 440 return; 441 } 442 while (patternData[dataIndex].length() > 0) { 443 log(patternData[dataIndex]); 444 bestPattern = patGen->getBestPattern(patternData[dataIndex++], status); 445 logln(UnicodeString(" -> ") + bestPattern); 446 447 SimpleDateFormat sdf(bestPattern, loc, status); 448 resultDate.remove(); 449 resultDate = sdf.format(testDate, resultDate); 450 if ( resultDate != patternResults[resultIndex] ) { 451 errln(UnicodeString("\nERROR: Test various skeletons[") + (dataIndex-1) 452 + UnicodeString("]. Got: ") + resultDate + UnicodeString(" Expected: ") + patternResults[resultIndex] ); 453 } 454 455 resultIndex++; 456 } 457 delete patGen; 458 localeIndex++; 459 } 460 461 // ======= More tests ticket#6110 462 logln("Testing DateTimePatternGenerator with various skeleton"); 463 464 status = U_ZERO_ERROR; 465 localeIndex=0; 466 resultIndex=0; 467 testDate= LocaleTest::date(99, 9, 13, 23, 58, 59); 468 { 469 int32_t dataIndex=0; 470 UnicodeString bestPattern; 471 logln("\n\n Test various skeletons for English locale..."); 472 DateTimePatternGenerator *patGen=DateTimePatternGenerator::createInstance(Locale::getEnglish(), status); 473 if(U_FAILURE(status)) { 474 dataerrln("ERROR: Could not create DateTimePatternGenerator with locale English . - exitting\n"); 475 return; 476 } 477 TimeZone *enZone = TimeZone::createTimeZone(UnicodeString("ECT/GMT")); 478 if (enZone==NULL) { 479 dataerrln("ERROR: Could not create TimeZone ECT"); 480 delete patGen; 481 return; 482 } 483 SimpleDateFormat *enFormat = (SimpleDateFormat *)DateFormat::createDateTimeInstance(DateFormat::kFull, 484 DateFormat::kFull, Locale::getEnglish()); 485 enFormat->setTimeZone(*enZone); 486 while (patternTests2[dataIndex].length() > 0) { 487 logln(patternTests2[dataIndex]); 488 bestPattern = patGen->getBestPattern(patternTests2[dataIndex], status); 489 logln(UnicodeString(" -> ") + bestPattern); 490 enFormat->applyPattern(bestPattern); 491 resultDate.remove(); 492 resultDate = enFormat->format(testDate, resultDate); 493 if ( resultDate != patternResults2[resultIndex] ) { 494 errln(UnicodeString("\nERROR: Test various skeletons[") + dataIndex 495 + UnicodeString("]. Got: ") + resultDate + UnicodeString(" Expected: ") + 496 patternResults2[resultIndex] ); 497 } 498 dataIndex++; 499 resultIndex++; 500 } 501 delete patGen; 502 delete enZone; 503 delete enFormat; 504 } 505 506 507 508 // ======= Test random skeleton 509 DateTimePatternGenerator *randDTGen= DateTimePatternGenerator::createInstance(status); 510 if (U_FAILURE(status)) { 511 dataerrln("ERROR: Could not create DateTimePatternGenerator (Locale::getFrench()) - exitting"); 512 return; 513 } 514 UChar newChar; 515 int32_t i; 516 for (i=0; i<10; ++i) { 517 UnicodeString randomSkeleton; 518 int32_t len = rand() % 20; 519 for (int32_t j=0; j<len; ++j ) { 520 while ((newChar = (UChar)(rand()%0x7f))>=(UChar)0x20) { 521 randomSkeleton += newChar; 522 } 523 } 524 UnicodeString bestPattern = randDTGen->getBestPattern(randomSkeleton, status); 525 } 526 delete randDTGen; 527 528 // UnicodeString randomString=Unicode 529 // ======= Test getStaticClassID() 530 531 logln("Testing getStaticClassID()"); 532 status = U_ZERO_ERROR; 533 DateTimePatternGenerator *test= DateTimePatternGenerator::createInstance(status); 534 535 if(test->getDynamicClassID() != DateTimePatternGenerator::getStaticClassID()) { 536 errln("ERROR: getDynamicClassID() didn't return the expected value"); 537 } 538 delete test; 539 540 // ====== Test createEmptyInstance() 541 542 logln("Testing createEmptyInstance()"); 543 status = U_ZERO_ERROR; 544 545 test = DateTimePatternGenerator::createEmptyInstance(status); 546 if(U_FAILURE(status)) { 547 errln("ERROR: Fail to create an empty instance ! - exitting.\n"); 548 delete test; 549 return; 550 } 551 552 conflictingStatus = test->addPattern(UnicodeString("MMMMd"), true, conflictingPattern, status); 553 status = U_ZERO_ERROR; 554 testPattern=test->getBestPattern(UnicodeString("MMMMdd"), status); 555 conflictingStatus = test->addPattern(UnicodeString("HH:mm"), true, conflictingPattern, status); 556 conflictingStatus = test->addPattern(UnicodeString("MMMMMd"), true, conflictingPattern, status); //duplicate pattern 557 StringEnumeration *output=NULL; 558 output = test->getRedundants(status); 559 expectedResult=UnicodeString("MMMMd"); 560 if (output != NULL) { 561 output->reset(status); 562 const UnicodeString *dupPattern=output->snext(status); 563 if ( (dupPattern==NULL) || (*dupPattern != expectedResult) ) { 564 errln("ERROR: Fail in getRedundants !\n"); 565 } 566 } 567 568 // ======== Test getSkeletons and getBaseSkeletons 569 StringEnumeration* ptrSkeletonEnum = test->getSkeletons(status); 570 if(U_FAILURE(status)) { 571 errln("ERROR: Fail to get skeletons !\n"); 572 } 573 UnicodeString returnPattern, *ptrSkeleton; 574 ptrSkeletonEnum->reset(status); 575 int32_t count=ptrSkeletonEnum->count(status); 576 for (i=0; i<count; ++i) { 577 ptrSkeleton = (UnicodeString *)ptrSkeletonEnum->snext(status); 578 returnPattern = test->getPatternForSkeleton(*ptrSkeleton); 579 if ( returnPattern != testSkeletonsResults[i] ) { 580 errln(UnicodeString("ERROR: Unexpected result from getSkeletons and getPatternForSkeleton\nGot: ") + returnPattern 581 + UnicodeString("\nExpected: ") + testSkeletonsResults[i] 582 + UnicodeString("\n")); 583 } 584 } 585 StringEnumeration* ptrBaseSkeletonEnum = test->getBaseSkeletons(status); 586 if(U_FAILURE(status)) { 587 errln("ERROR: Fail to get base skeletons !\n"); 588 } 589 count=ptrBaseSkeletonEnum->count(status); 590 for (i=0; i<count; ++i) { 591 ptrSkeleton = (UnicodeString *)ptrBaseSkeletonEnum->snext(status); 592 if ( *ptrSkeleton != testBaseSkeletonsResults[i] ) { 593 errln("ERROR: Unexpected result from getBaseSkeletons() !\n"); 594 } 595 } 596 597 // ========= DateTimePatternGenerator sample code in Userguide 598 // set up the generator 599 Locale locale = Locale::getFrench(); 600 status = U_ZERO_ERROR; 601 DateTimePatternGenerator *generator = DateTimePatternGenerator::createInstance( locale, status); 602 603 // get a pattern for an abbreviated month and day 604 pattern = generator->getBestPattern(UnicodeString("MMMd"), status); 605 SimpleDateFormat formatter(pattern, locale, status); 606 607 zone = TimeZone::createTimeZone(UnicodeString("GMT")); 608 formatter.setTimeZone(*zone); 609 // use it to format (or parse) 610 UnicodeString formatted; 611 formatted = formatter.format(Calendar::getNow(), formatted, status); 612 // for French, the result is "13 sept." 613 formatted.remove(); 614 // cannot use the result from getNow() because the value change evreyday. 615 testDate= LocaleTest::date(99, 0, 13, 23, 58, 59); 616 formatted = formatter.format(testDate, formatted, status); 617 expectedResult=UnicodeString("14 janv."); 618 if ( formatted != expectedResult ) { 619 errln("ERROR: Userguide sample code result!"); 620 errln(UnicodeString(" Got: ")+ formatted + UnicodeString(" Expected: ") + expectedResult); 621 } 622 623 delete zone; 624 delete output; 625 delete ptrSkeletonEnum; 626 delete ptrBaseSkeletonEnum; 627 delete test; 628 delete generator; 629} 630 631#endif /* #if !UCONFIG_NO_FORMATTING */ 632