1 /*********************************************************************** 2 * Copyright (c) 1997-2009, International Business Machines Corporation 3 * and others. All Rights Reserved. 4 ***********************************************************************/ 5 6 #include "unicode/utypes.h" 7 8 #if !UCONFIG_NO_FORMATTING 9 10 #include "miscdtfm.h" 11 12 #include "unicode/format.h" 13 #include "unicode/decimfmt.h" 14 #include "unicode/datefmt.h" 15 #include "unicode/smpdtfmt.h" 16 #include "unicode/dtfmtsym.h" 17 #include "unicode/locid.h" 18 #include "unicode/msgfmt.h" 19 #include "unicode/numfmt.h" 20 #include "unicode/choicfmt.h" 21 #include "unicode/gregocal.h" 22 23 // ***************************************************************************** 24 // class DateFormatMiscTests 25 // ***************************************************************************** 26 27 #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break; 28 29 void 30 DateFormatMiscTests::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ ) 31 { 32 // if (exec) logln((UnicodeString)"TestSuite DateFormatMiscTests"); 33 switch (index) { 34 CASE(0, test4097450) 35 CASE(1, test4099975) 36 CASE(2, test4117335) 37 38 default: name = ""; break; 39 } 40 } 41 42 UBool 43 DateFormatMiscTests::failure(UErrorCode status, const char* msg) 44 { 45 if(U_FAILURE(status)) { 46 errcheckln(status, UnicodeString("FAIL: ") + msg + " failed, error " + u_errorName(status)); 47 return TRUE; 48 } 49 50 return FALSE; 51 } 52 53 /* 54 * @bug 4097450 55 */ 56 void 57 DateFormatMiscTests::test4097450() 58 { 59 // 60 // Date parse requiring 4 digit year. 61 // 62 UnicodeString dstring [] = { 63 UnicodeString("97"), 64 UnicodeString("1997"), 65 UnicodeString("97"), 66 UnicodeString("1997"), 67 UnicodeString("01"), 68 UnicodeString("2001"), 69 UnicodeString("01"), 70 UnicodeString("2001"), 71 UnicodeString("1"), 72 UnicodeString("1"), 73 UnicodeString("11"), 74 UnicodeString("11"), 75 UnicodeString("111"), 76 UnicodeString("111") 77 }; 78 79 UnicodeString dformat [] = { 80 UnicodeString("yy"), 81 UnicodeString("yy"), 82 UnicodeString("yyyy"), 83 UnicodeString("yyyy"), 84 UnicodeString("yy"), 85 UnicodeString("yy"), 86 UnicodeString("yyyy"), 87 UnicodeString("yyyy"), 88 UnicodeString("yy"), 89 UnicodeString("yyyy"), 90 UnicodeString("yy"), 91 UnicodeString("yyyy"), 92 UnicodeString("yy"), 93 UnicodeString("yyyy") 94 }; 95 96 /* UBool dresult [] = { 97 TRUE, 98 FALSE, 99 FALSE, 100 TRUE, 101 TRUE, 102 FALSE, 103 FALSE, 104 TRUE, 105 FALSE, 106 FALSE, 107 TRUE, 108 FALSE, 109 FALSE, 110 FALSE 111 };*/ 112 113 UErrorCode status = U_ZERO_ERROR; 114 SimpleDateFormat *formatter; 115 SimpleDateFormat *resultFormatter = new SimpleDateFormat((UnicodeString)"yyyy", status); 116 failure(status, "new SimpleDateFormat"); 117 118 logln("Format\tSource\tResult"); 119 logln("-------\t-------\t-------"); 120 for (int i = 0; i < 14/*dstring.length*/; i++) 121 { 122 log(dformat[i] + "\t" + dstring[i] + "\t"); 123 formatter = new SimpleDateFormat(dformat[i], status); 124 if(failure(status, "new SimpleDateFormat")) return; 125 //try { 126 UnicodeString str; 127 FieldPosition pos(FieldPosition::DONT_CARE); 128 logln(resultFormatter->format(formatter->parse(dstring[i], status), str, pos)); 129 failure(status, "resultFormatter->format"); 130 //if ( !dresult[i] ) System.out.print(" <-- error!"); 131 /*} 132 catch (ParseException exception) { 133 //if ( dresult[i] ) System.out.print(" <-- error!"); 134 System.out.print("exception --> " + exception); 135 }*/ 136 delete formatter; 137 logln(); 138 } 139 140 delete resultFormatter; 141 } 142 143 /* 144 * @bug 4099975 145 */ 146 void 147 DateFormatMiscTests::test4099975() 148 { 149 /** 150 * Test Constructor SimpleDateFormat::SimpleDateFormat (const UnicodeString & pattern, 151 * const DateFormatSymbols & formatData, UErrorCode & status ) 152 * The DateFormatSymbols object is NOT adopted; Modifying the original DateFormatSymbols 153 * should not change the SimpleDateFormat's behavior. 154 */ 155 UDate d = Calendar::getNow(); 156 { 157 UErrorCode status = U_ZERO_ERROR; 158 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status); 159 if(failure(status, "new DateFormatSymbols")) return; 160 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), *symbols, status); 161 if(failure(status, "new SimpleDateFormat")) return; 162 UnicodeString format0; 163 format0 = df->format(d, format0); 164 UnicodeString localizedPattern0; 165 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status); 166 failure(status, "df->toLocalizedPattern"); 167 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field 168 UnicodeString format1; 169 format1 = df->format(d, format1); 170 if (format0 != format1) { 171 errln(UnicodeString("Formats are different. format0: ") + format0 172 + UnicodeString("; format1: ") + format1); 173 } 174 UnicodeString localizedPattern1; 175 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status); 176 failure(status, "df->toLocalizedPattern"); 177 if (localizedPattern0 != localizedPattern1) { 178 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0 179 + UnicodeString("; localizedPattern1: ") + localizedPattern1); 180 } 181 delete symbols; 182 delete df; 183 } 184 /* 185 * Test void SimpleDateFormat::setDateFormatSymbols ( const DateFormatSymbols & newFormatSymbols ) 186 * Modifying the original DateFormatSymbols should not change the SimpleDateFormat's behavior. 187 */ 188 { 189 UErrorCode status = U_ZERO_ERROR; 190 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status); 191 if(failure(status, "new DateFormatSymbols")) return; 192 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), status); 193 if(failure(status, "new SimpleDateFormat")) return; 194 df->setDateFormatSymbols(*symbols); 195 UnicodeString format0; 196 format0 = df->format(d, format0); 197 UnicodeString localizedPattern0; 198 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status); 199 failure(status, "df->toLocalizedPattern"); 200 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field 201 UnicodeString format1; 202 format1 = df->format(d, format1); 203 if (format0 != format1) { 204 errln(UnicodeString("Formats are different. format0: ") + format0 205 + UnicodeString("; format1: ") + format1); 206 } 207 UnicodeString localizedPattern1; 208 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status); 209 failure(status, "df->toLocalizedPattern"); 210 if (localizedPattern0 != localizedPattern1) { 211 errln(UnicodeString("Pattern has been changed. localizedPattern0: ") + localizedPattern0 212 + UnicodeString("; localizedPattern1: ") + localizedPattern1); 213 } 214 delete symbols; 215 delete df; 216 217 } 218 //Test the pointer version of the constructor (and the adoptDateFormatSymbols method) 219 { 220 UErrorCode status = U_ZERO_ERROR; 221 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status); 222 if(failure(status, "new DateFormatSymbols")) return; 223 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), symbols, status); 224 if(failure(status, "new SimpleDateFormat")) return; 225 UnicodeString format0; 226 format0 = df->format(d, format0); 227 UnicodeString localizedPattern0; 228 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status); 229 failure(status, "df->toLocalizedPattern"); 230 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field 231 UnicodeString format1; 232 format1 = df->format(d, format1); 233 if (format0 != format1) { 234 errln(UnicodeString("Formats are different. format0: ") + format0 235 + UnicodeString("; format1: ") + format1); 236 } 237 UnicodeString localizedPattern1; 238 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status); 239 failure(status, "df->toLocalizedPattern"); 240 if (localizedPattern0 == localizedPattern1) { 241 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0 242 + UnicodeString("; localizedPattern1: ") + localizedPattern1); 243 } 244 //delete symbols; the caller is no longer responsible for deleting the symbols 245 delete df; 246 } 247 // 248 { 249 UErrorCode status = U_ZERO_ERROR; 250 DateFormatSymbols* symbols = new DateFormatSymbols(Locale::getUS(), status); 251 failure(status, "new DateFormatSymbols"); 252 SimpleDateFormat *df = new SimpleDateFormat(UnicodeString("E hh:mm"), status); 253 if(failure(status, "new SimpleDateFormat")) return; 254 df-> adoptDateFormatSymbols(symbols); 255 UnicodeString format0; 256 format0 = df->format(d, format0); 257 UnicodeString localizedPattern0; 258 localizedPattern0 = df->toLocalizedPattern(localizedPattern0, status); 259 failure(status, "df->toLocalizedPattern"); 260 symbols->setLocalPatternChars(UnicodeString("abcdefghijklmonpqr")); // change value of field 261 UnicodeString format1; 262 format1 = df->format(d, format1); 263 if (format0 != format1) { 264 errln(UnicodeString("Formats are different. format0: ") + format0 265 + UnicodeString("; format1: ") + format1); 266 } 267 UnicodeString localizedPattern1; 268 localizedPattern1 = df->toLocalizedPattern(localizedPattern1, status); 269 failure(status, "df->toLocalizedPattern"); 270 if (localizedPattern0 == localizedPattern1) { 271 errln(UnicodeString("Pattern should have been changed. localizedPattern0: ") + localizedPattern0 272 + UnicodeString("; localizedPattern1: ") + localizedPattern1); 273 } 274 //delete symbols; the caller is no longer responsible for deleting the symbols 275 delete df; 276 } 277 } 278 279 /* 280 * @test @(#)bug4117335.java 1.1 3/5/98 281 * 282 * @bug 4117335 283 */ 284 void 285 DateFormatMiscTests::test4117335() 286 { 287 //UnicodeString bc = "\u7d00\u5143\u524d"; 288 UChar bcC [] = { 289 0x7D00, 290 0x5143, 291 0x524D 292 }; 293 UnicodeString bc(bcC, 3, 3); 294 295 //UnicodeString ad = "\u897f\u66a6"; 296 UChar adC [] = { 297 0x897F, 298 0x66A6 299 }; 300 UnicodeString ad(adC, 2, 2); 301 302 //UnicodeString jstLong = "\u65e5\u672c\u6a19\u6e96\u6642"; 303 UChar jstLongC [] = { 304 0x65e5, 305 0x672c, 306 0x6a19, 307 0x6e96, 308 0x6642 309 }; 310 UChar jdtLongC [] = {0x65E5, 0x672C, 0x590F, 0x6642, 0x9593}; 311 312 UnicodeString jstLong(jstLongC, 5, 5); 313 314 UnicodeString jstShort = "JST"; 315 316 UnicodeString tzID = "Asia/Tokyo"; 317 318 UnicodeString jdtLong(jdtLongC, 5, 5); 319 320 UnicodeString jdtShort = "JDT"; 321 UErrorCode status = U_ZERO_ERROR; 322 DateFormatSymbols *symbols = new DateFormatSymbols(Locale::getJapan(), status); 323 if(U_FAILURE(status)) { 324 errcheckln(status, "Failure creating DateFormatSymbols, %s", u_errorName(status)); 325 delete symbols; 326 return; 327 } 328 failure(status, "new DateFormatSymbols"); 329 int32_t eraCount = 0; 330 const UnicodeString *eras = symbols->getEras(eraCount); 331 332 logln(UnicodeString("BC = ") + eras[0]); 333 if (eras[0] != bc) { 334 errln("*** Should have been " + bc); 335 //throw new Exception("Error in BC"); 336 } 337 338 logln(UnicodeString("AD = ") + eras[1]); 339 if (eras[1] != ad) { 340 errln("*** Should have been " + ad); 341 //throw new Exception("Error in AD"); 342 } 343 344 int32_t rowCount, colCount; 345 const UnicodeString **zones = symbols->getZoneStrings(rowCount, colCount); 346 //don't hard code the index .. compute it. 347 int32_t index = -1; 348 for (int32_t i = 0; i < rowCount; ++i) { 349 if (tzID == (zones[i][0])) { 350 index = i; 351 break; 352 } 353 } 354 logln(UnicodeString("Long zone name = ") + zones[index][1]); 355 if (zones[index][1] != jstLong) { 356 errln("*** Should have been " + prettify(jstLong)+ " but it is: " + prettify(zones[index][1])); 357 //throw new Exception("Error in long TZ name"); 358 } 359 logln(UnicodeString("Short zone name = ") + zones[index][2]); 360 if (zones[index][2] != jstShort) { 361 errln("*** Should have been " + prettify(jstShort) + " but it is: " + prettify(zones[index][2])); 362 //throw new Exception("Error in short TZ name"); 363 } 364 logln(UnicodeString("Long zone name = ") + zones[index][3]); 365 if (zones[index][3] != jdtLong) { 366 errln("*** Should have been " + prettify(jstLong) + " but it is: " + prettify(zones[index][3])); 367 //throw new Exception("Error in long TZ name"); 368 } 369 logln(UnicodeString("SHORT zone name = ") + zones[index][4]); 370 if (zones[index][4] != jdtShort) { 371 errln("*** Should have been " + prettify(jstShort)+ " but it is: " + prettify(zones[index][4])); 372 //throw new Exception("Error in short TZ name"); 373 } 374 delete symbols; 375 376 } 377 378 #endif /* #if !UCONFIG_NO_FORMATTING */ 379