1 /******************************************************************** 2 * Copyright (c) 1997-2013, International Business Machines 3 * Corporation and others. All Rights Reserved. 4 ********************************************************************/ 5 6 #include "unicode/utypes.h" 7 8 #if !UCONFIG_NO_FORMATTING 9 10 #include "tsdtfmsy.h" 11 12 #include "unicode/dtfmtsym.h" 13 14 15 //-------------------------------------------------------------------- 16 // Time bomb - allows temporary behavior that expires at a given 17 // release 18 //-------------------------------------------------------------------- 19 20 void IntlTestDateFormatSymbols::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) 21 { 22 if (exec) logln("TestSuite DateFormatSymbols"); 23 switch (index) { 24 TESTCASE(0,TestSymbols); 25 TESTCASE(1,TestGetMonths); 26 TESTCASE(2,TestGetMonths2); 27 TESTCASE(3,TestGetWeekdays2); 28 TESTCASE(4,TestGetEraNames); 29 TESTCASE(5,TestGetSetSpecificItems); 30 default: name = ""; break; 31 } 32 } 33 34 /** 35 * Test getMonths. 36 */ 37 void IntlTestDateFormatSymbols::TestGetMonths() 38 { 39 UErrorCode status = U_ZERO_ERROR; 40 int32_t cnt; 41 const UnicodeString* month; 42 DateFormatSymbols *symbol; 43 44 symbol=new DateFormatSymbols(Locale::getDefault(), status); 45 46 month=symbol->getMonths(cnt); 47 48 logln((UnicodeString)"size = " + cnt); 49 50 for (int32_t i=0; i<cnt; ++i) 51 { 52 logln(month[i]); 53 } 54 55 delete symbol; 56 } 57 58 void IntlTestDateFormatSymbols::TestGetMonths2() 59 { 60 UErrorCode status = U_ZERO_ERROR; 61 DateFormatSymbols *symbol; 62 63 symbol=new DateFormatSymbols(Locale::getDefault(), status); 64 65 DateFormatSymbols::DtContextType context[] = {DateFormatSymbols::STANDALONE, DateFormatSymbols::FORMAT}; 66 DateFormatSymbols::DtWidthType width[] = {DateFormatSymbols::WIDE, DateFormatSymbols::ABBREVIATED, DateFormatSymbols::NARROW}; 67 68 for (int32_t i = 0; i < 2; i++) { 69 for (int32_t j = 0; j < 3; j++) { 70 int32_t cnt; 71 const UnicodeString * month = symbol->getMonths(cnt,context[i],width[j]); 72 73 logln((UnicodeString)"size = " + cnt); 74 75 for (int32_t k = 0; k < cnt; k++) { 76 logln(month[k]); 77 } 78 } 79 } 80 delete symbol; 81 } 82 83 void IntlTestDateFormatSymbols::TestGetWeekdays2() 84 { 85 UErrorCode status = U_ZERO_ERROR; 86 DateFormatSymbols *symbol; 87 88 symbol=new DateFormatSymbols(Locale::getDefault(), status); 89 90 DateFormatSymbols::DtContextType context[] = {DateFormatSymbols::STANDALONE, DateFormatSymbols::FORMAT}; 91 DateFormatSymbols::DtWidthType width[] = {DateFormatSymbols::WIDE, DateFormatSymbols::ABBREVIATED, DateFormatSymbols::NARROW}; 92 93 for (int32_t i = 0; i < 2; i++) { 94 for (int32_t j = 0; j < 3; j++) { 95 int32_t cnt; 96 const UnicodeString * wd = symbol->getWeekdays(cnt,context[i],width[j]); 97 98 logln((UnicodeString)"size = " + cnt); 99 100 for (int32_t k = 0; k < cnt; k++) { 101 logln(wd[k]); 102 } 103 } 104 } 105 delete symbol; 106 } 107 108 109 void IntlTestDateFormatSymbols::TestGetEraNames() 110 { 111 UErrorCode status = U_ZERO_ERROR; 112 int32_t cnt; 113 const UnicodeString* name; 114 DateFormatSymbols *symbol; 115 116 symbol=new DateFormatSymbols(Locale::getDefault(), status); 117 118 name=symbol->getEraNames(cnt); 119 120 logln((UnicodeString)"size = " + cnt); 121 122 for (int32_t i=0; i<cnt; ++i) 123 { 124 logln(name[i]); 125 } 126 127 delete symbol; 128 } 129 130 UBool IntlTestDateFormatSymbols::UnicodeStringsArePrefixes(int32_t count, int32_t prefixLen, const UnicodeString *prefixArray, const UnicodeString *baseArray) 131 { 132 int32_t i; 133 for (i = 0; i < count; i++) { 134 if (baseArray[i].compare(0, prefixLen, prefixArray[i]) != 0) { 135 errln("ERROR: Mismatch example: expect prefix \"" + prefixArray[i] + "\" of base \"" + baseArray[i] + "\"."); 136 return FALSE; 137 } 138 } 139 return TRUE; 140 } 141 142 void IntlTestDateFormatSymbols::TestGetSetSpecificItems() 143 { 144 UErrorCode status = U_ZERO_ERROR; 145 DateFormatSymbols *symbol=new DateFormatSymbols(Locale::getEnglish(), status); 146 if(U_FAILURE(status)) { 147 dataerrln("ERROR: Couldn't create English DateFormatSymbols " + (UnicodeString)u_errorName(status)); 148 return; 149 } 150 int32_t cntFmtAbbrev, cntFmtShort, cntStdAloneShort; 151 const UnicodeString * wdFmtAbbrev = symbol->getWeekdays(cntFmtAbbrev,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED); 152 const UnicodeString * wdFmtShort = symbol->getWeekdays(cntFmtShort,DateFormatSymbols::FORMAT,DateFormatSymbols::SHORT); 153 const UnicodeString * wdStdAloneShort = symbol->getWeekdays(cntStdAloneShort,DateFormatSymbols::STANDALONE,DateFormatSymbols::SHORT); 154 // Expect that English short names are prefixes of abbreviated names 155 if (cntFmtShort != cntFmtAbbrev || !UnicodeStringsArePrefixes(cntFmtAbbrev, 2, wdFmtShort, wdFmtAbbrev)) { 156 errln("ERROR: English format short weekday names don't match prefixes of format abbreviated names"); 157 } 158 if (cntStdAloneShort != cntFmtAbbrev || !UnicodeStringsArePrefixes(cntFmtAbbrev, 2, wdStdAloneShort, wdFmtAbbrev)) { 159 errln("ERROR: English standalone short weekday names don't match prefixes of format abbreviated names"); 160 } 161 162 delete symbol; 163 } 164 165 /** 166 * Test the API of DateFormatSymbols; primarily a simple get/set set. 167 */ 168 void IntlTestDateFormatSymbols::TestSymbols(/* char *par */) 169 { 170 UErrorCode status = U_ZERO_ERROR; 171 172 DateFormatSymbols fr(Locale::getFrench(), status); 173 if(U_FAILURE(status)) { 174 dataerrln("ERROR: Couldn't create French DateFormatSymbols " + (UnicodeString)u_errorName(status)); 175 return; 176 } 177 178 status = U_ZERO_ERROR; 179 DateFormatSymbols fr2(Locale::getFrench(), status); 180 if(U_FAILURE(status)) { 181 errcheckln(status, "ERROR: Couldn't create French DateFormatSymbols " + (UnicodeString)u_errorName(status)); 182 return; 183 } 184 185 status = U_ZERO_ERROR; 186 DateFormatSymbols en(Locale::getEnglish(), status); 187 if(U_FAILURE(status)) { 188 errcheckln(status, "ERROR: Couldn't create English DateFormatSymbols " + (UnicodeString)u_errorName(status)); 189 return; 190 } 191 192 if(en == fr || ! (en != fr) ) { 193 errln("ERROR: English DateFormatSymbols equal to French"); 194 } 195 196 // just do some VERY basic tests to make sure that get/set work 197 198 int32_t count = 0; 199 const UnicodeString *eras = en.getEras(count); 200 if(count == 0) { 201 errln("ERROR: 0 english eras.. exiting..\n"); 202 return; 203 } 204 int32_t eraNamesCount = 0; 205 const UnicodeString *eraNames = en.getEraNames(eraNamesCount); 206 if(eraNamesCount == 0) { 207 errln("ERROR: 0 english eraNames\n"); 208 } else if ( eraNames[0].length() <= eras[0].length() ) { 209 // At least for English we know a wide eraName should be longer than an abbrev era 210 errln("ERROR: english eraNames[0] not longer than eras[0]\n"); 211 } 212 int32_t narrowErasCount = 0; 213 const UnicodeString *narrowEras = en.getNarrowEras(narrowErasCount); 214 if(narrowErasCount == 0) { 215 errln("ERROR: 0 english narrowEras\n"); 216 } else if ( narrowEras[0].length() >= eras[0].length() ) { 217 // At least for English we know a narrowEra should be shorter than an abbrev era 218 errln("ERROR: english narrowEras[0] not shorter than eras[0]\n"); 219 } 220 221 fr.setEras(eras, count); 222 if( *en.getEras(count) != *fr.getEras(count)) { 223 errln("ERROR: setEras() failed"); 224 } 225 226 const UnicodeString *months = en.getMonths(count); 227 fr.setMonths(months, count); 228 if( *en.getMonths(count) != *fr.getMonths(count)) { 229 errln("ERROR: setMonths() failed"); 230 } 231 232 const UnicodeString *shortMonths = en.getShortMonths(count); 233 fr.setShortMonths(shortMonths, count); 234 if( *en.getShortMonths(count) != *fr.getShortMonths(count)) { 235 errln("ERROR: setShortMonths() failed"); 236 } 237 238 const UnicodeString *wideMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE); 239 fr2.setMonths(wideMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE); 240 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE) != 241 *fr2.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE )) { 242 errln("ERROR: setMonths(FORMAT,WIDE) failed"); 243 } 244 245 const UnicodeString *abbrMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED); 246 fr2.setMonths(abbrMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED); 247 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED) != 248 *fr2.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED )) { 249 errln("ERROR: setMonths(FORMAT,ABBREVIATED) failed"); 250 } 251 252 const UnicodeString *narrowMonths = en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW); 253 fr.setMonths(narrowMonths, count, DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW); 254 if( *en.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW) != 255 *fr.getMonths(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW )) { 256 errln("ERROR: setMonths(FORMAT,NARROW) failed"); 257 } 258 259 const UnicodeString *standaloneWideMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE); 260 fr.setMonths(standaloneWideMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE); 261 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE) != 262 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE )) { 263 errln("ERROR: setMonths(STANDALONE,WIDE) failed"); 264 } 265 266 const UnicodeString *standaloneShortMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED); 267 fr.setMonths(standaloneShortMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED); 268 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED) != 269 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED )) { 270 errln("ERROR: setMonths(STANDALONE,ABBREVIATED) failed"); 271 } 272 273 const UnicodeString *standaloneNarrowMonths = en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW); 274 fr.setMonths(standaloneNarrowMonths, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW); 275 if( *en.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW) != 276 *fr.getMonths(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW )) { 277 errln("ERROR: setMonths(STANDALONE,NARROW) failed"); 278 } 279 280 const UnicodeString *weekdays = en.getWeekdays(count); 281 fr.setWeekdays(weekdays, count); 282 if( *en.getWeekdays(count) != *fr.getWeekdays(count)) { 283 errln("ERROR: setWeekdays() failed"); 284 } 285 286 const UnicodeString *shortWeekdays = en.getShortWeekdays(count); 287 fr.setShortWeekdays(shortWeekdays, count); 288 if( *en.getShortWeekdays(count) != *fr.getShortWeekdays(count)) { 289 errln("ERROR: setShortWeekdays() failed"); 290 } 291 292 const UnicodeString *wideWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE); 293 fr2.setWeekdays(wideWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE); 294 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE) != 295 *fr2.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::WIDE )) { 296 errln("ERROR: setWeekdays(FORMAT,WIDE) failed"); 297 } 298 299 const UnicodeString *abbrWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED); 300 fr2.setWeekdays(abbrWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED); 301 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED) != 302 *fr2.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::ABBREVIATED )) { 303 errln("ERROR: setWeekdays(FORMAT,ABBREVIATED) failed"); 304 } 305 306 const UnicodeString *narrowWeekdays = en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW); 307 fr.setWeekdays(narrowWeekdays, count, DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW); 308 if( *en.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW) != 309 *fr.getWeekdays(count,DateFormatSymbols::FORMAT,DateFormatSymbols::NARROW )) { 310 errln("ERROR: setWeekdays(FORMAT,NARROW) failed"); 311 } 312 313 const UnicodeString *standaloneWideWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE); 314 fr.setWeekdays(standaloneWideWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE); 315 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE) != 316 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::WIDE )) { 317 errln("ERROR: setWeekdays(STANDALONE,WIDE) failed"); 318 } 319 320 const UnicodeString *standaloneShortWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED); 321 fr.setWeekdays(standaloneShortWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED); 322 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED) != 323 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::ABBREVIATED )) { 324 errln("ERROR: setWeekdays(STANDALONE,ABBREVIATED) failed"); 325 } 326 327 const UnicodeString *standaloneNarrowWeekdays = en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW); 328 fr.setWeekdays(standaloneNarrowWeekdays, count, DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW); 329 if( *en.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW) != 330 *fr.getWeekdays(count,DateFormatSymbols::STANDALONE,DateFormatSymbols::NARROW )) { 331 errln("ERROR: setWeekdays(STANDALONE,NARROW) failed"); 332 } 333 334 const UnicodeString *wideQuarters = en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE); 335 fr2.setQuarters(wideQuarters, count, DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE); 336 if( *en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE) != 337 *fr2.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::WIDE )) { 338 errln("ERROR: setQuarters(FORMAT, WIDE) failed"); 339 } 340 341 const UnicodeString *abbrQuarters = en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED); 342 fr2.setQuarters(abbrQuarters, count, DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED); 343 if( *en.getQuarters(count,DateFormatSymbols::FORMAT, DateFormatSymbols::ABBREVIATED) != 344 *fr2.getQuarters(count,DateFormatSymbols::FORMAT ,DateFormatSymbols::ABBREVIATED )) { 345 errln("ERROR: setQuarters(FORMAT, ABBREVIATED) failed"); 346 } 347 348 const UnicodeString *standaloneWideQuarters = en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE); 349 fr.setQuarters(standaloneWideQuarters, count, DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE); 350 if( *en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE) != 351 *fr.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::WIDE )) { 352 errln("ERROR: setQuarters(STANDALONE, WIDE) failed"); 353 } 354 355 const UnicodeString *standaloneShortQuarters = en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED); 356 fr.setQuarters(standaloneShortQuarters, count, DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED); 357 if( *en.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED) != 358 *fr.getQuarters(count,DateFormatSymbols::STANDALONE, DateFormatSymbols::ABBREVIATED )) { 359 errln("ERROR: setQuarters(STANDALONE, ABBREVIATED) failed"); 360 } 361 362 const UnicodeString *ampms = en.getAmPmStrings(count); 363 fr.setAmPmStrings(ampms, count); 364 if( *en.getAmPmStrings(count) != *fr.getAmPmStrings(count)) { 365 errln("ERROR: setAmPmStrings() failed"); 366 } 367 368 int32_t rowCount = 0, columnCount = 0; 369 const UnicodeString **strings = en.getZoneStrings(rowCount, columnCount); 370 fr.setZoneStrings(strings, rowCount, columnCount); 371 const UnicodeString **strings1 = fr.getZoneStrings(rowCount, columnCount); 372 for(int32_t i = 0; i < rowCount; i++) { 373 for(int32_t j = 0; j < columnCount; j++) { 374 if( strings[i][j] != strings1[i][j] ) { 375 errln("ERROR: setZoneStrings() failed"); 376 } 377 } 378 } 379 380 UnicodeString localPattern, pat1, pat2; 381 localPattern = en.getLocalPatternChars(localPattern); 382 fr.setLocalPatternChars(localPattern); 383 if( en.getLocalPatternChars(pat1) != fr.getLocalPatternChars(pat2)) { 384 errln("ERROR: setLocalPatternChars() failed"); 385 } 386 387 388 status = U_ZERO_ERROR; 389 DateFormatSymbols foo(status); 390 DateFormatSymbols bar(foo); 391 392 en = fr; 393 394 if(en != fr) { 395 errln("ERROR: Assignment operator failed"); 396 } 397 if(foo != bar) { 398 errln("ERROR: Copy Constructor failed"); 399 } 400 } 401 402 #endif /* #if !UCONFIG_NO_FORMATTING */ 403