PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/deps/third_party/icu38/source/test/intltest/miscdtfm.cpp

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