PageRenderTime 114ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/external/icu4c/test/intltest/nmfmapts.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk
C++ | 398 lines | 310 code | 70 blank | 18 comment | 80 complexity | 0f82de46b6dde663655380d98c67ae1d MD5 | raw file
  1. /***********************************************************************
  2. * COPYRIGHT:
  3. * Copyright (c) 1997-2010, International Business Machines Corporation
  4. * and others. All Rights Reserved.
  5. ***********************************************************************/
  6. #include "unicode/utypes.h"
  7. #if !UCONFIG_NO_FORMATTING
  8. #include "nmfmapts.h"
  9. #include "unicode/numfmt.h"
  10. #include "unicode/decimfmt.h"
  11. #include "unicode/locid.h"
  12. #include "unicode/unum.h"
  13. #include "unicode/strenum.h"
  14. // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
  15. // try to test the full functionality. It just calls each function in the class and
  16. // verifies that it works on a basic level.
  17. void IntlTestNumberFormatAPI::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
  18. {
  19. if (exec) logln("TestSuite NumberFormatAPI");
  20. switch (index) {
  21. case 0: name = "NumberFormat API test";
  22. if (exec) {
  23. logln("NumberFormat API test---"); logln("");
  24. UErrorCode status = U_ZERO_ERROR;
  25. Locale saveLocale;
  26. Locale::setDefault(Locale::getEnglish(), status);
  27. if(U_FAILURE(status)) {
  28. errln("ERROR: Could not set default locale, test may not give correct results");
  29. }
  30. testAPI(/* par */);
  31. Locale::setDefault(saveLocale, status);
  32. }
  33. break;
  34. case 1: name = "NumberFormatRegistration";
  35. if (exec) {
  36. logln("NumberFormat Registration test---"); logln("");
  37. UErrorCode status = U_ZERO_ERROR;
  38. Locale saveLocale;
  39. Locale::setDefault(Locale::getEnglish(), status);
  40. if(U_FAILURE(status)) {
  41. errln("ERROR: Could not set default locale, test may not give correct results");
  42. }
  43. testRegistration();
  44. Locale::setDefault(saveLocale, status);
  45. }
  46. break;
  47. default: name = ""; break;
  48. }
  49. }
  50. /**
  51. * This test does round-trip testing (format -> parse -> format -> parse -> etc.) of
  52. * NumberFormat.
  53. */
  54. void IntlTestNumberFormatAPI::testAPI(/* char* par */)
  55. {
  56. UErrorCode status = U_ZERO_ERROR;
  57. // ======= Test constructors
  58. logln("Testing NumberFormat constructors");
  59. NumberFormat *def = NumberFormat::createInstance(status);
  60. if(U_FAILURE(status)) {
  61. dataerrln("ERROR: Could not create NumberFormat (default) - %s", u_errorName(status));
  62. }
  63. status = U_ZERO_ERROR;
  64. NumberFormat *fr = NumberFormat::createInstance(Locale::getFrench(), status);
  65. if(U_FAILURE(status)) {
  66. dataerrln("ERROR: Could not create NumberFormat (French) - %s", u_errorName(status));
  67. }
  68. NumberFormat *cur = NumberFormat::createCurrencyInstance(status);
  69. if(U_FAILURE(status)) {
  70. dataerrln("ERROR: Could not create NumberFormat (currency, default) - %s", u_errorName(status));
  71. }
  72. status = U_ZERO_ERROR;
  73. NumberFormat *cur_fr = NumberFormat::createCurrencyInstance(Locale::getFrench(), status);
  74. if(U_FAILURE(status)) {
  75. dataerrln("ERROR: Could not create NumberFormat (currency, French) - %s", u_errorName(status));
  76. }
  77. NumberFormat *per = NumberFormat::createPercentInstance(status);
  78. if(U_FAILURE(status)) {
  79. dataerrln("ERROR: Could not create NumberFormat (percent, default) - %s", u_errorName(status));
  80. }
  81. status = U_ZERO_ERROR;
  82. NumberFormat *per_fr = NumberFormat::createPercentInstance(Locale::getFrench(), status);
  83. if(U_FAILURE(status)) {
  84. dataerrln("ERROR: Could not create NumberFormat (percent, French) - %s", u_errorName(status));
  85. }
  86. // ======= Test equality
  87. if (per_fr != NULL && cur_fr != NULL)
  88. {
  89. logln("Testing equality operator");
  90. if( *per_fr == *cur_fr || ! ( *per_fr != *cur_fr) ) {
  91. errln("ERROR: == failed");
  92. }
  93. }
  94. // ======= Test various format() methods
  95. if (cur_fr != NULL)
  96. {
  97. logln("Testing various format() methods");
  98. double d = -10456.0037;
  99. int32_t l = 100000000;
  100. Formattable fD(d);
  101. Formattable fL(l);
  102. UnicodeString res1, res2, res3, res4, res5, res6;
  103. FieldPosition pos1(0), pos2(0), pos3(0), pos4(0);
  104. res1 = cur_fr->format(d, res1);
  105. logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1);
  106. res2 = cur_fr->format(l, res2);
  107. logln((UnicodeString) "" + (int32_t) l + " formatted to " + res2);
  108. res3 = cur_fr->format(d, res3, pos1);
  109. logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res3);
  110. res4 = cur_fr->format(l, res4, pos2);
  111. logln((UnicodeString) "" + (int32_t) l + " formatted to " + res4);
  112. status = U_ZERO_ERROR;
  113. res5 = cur_fr->format(fD, res5, pos3, status);
  114. if(U_FAILURE(status)) {
  115. errln("ERROR: format(Formattable [double]) failed");
  116. }
  117. logln((UnicodeString) "" + (int32_t) fD.getDouble() + " formatted to " + res5);
  118. status = U_ZERO_ERROR;
  119. res6 = cur_fr->format(fL, res6, pos4, status);
  120. if(U_FAILURE(status)) {
  121. errln("ERROR: format(Formattable [long]) failed");
  122. }
  123. logln((UnicodeString) "" + fL.getLong() + " formatted to " + res6);
  124. }
  125. // ======= Test parse()
  126. if (fr != NULL)
  127. {
  128. logln("Testing parse()");
  129. double d = -10456.0037;
  130. UnicodeString text("-10,456.0037");
  131. Formattable result1, result2, result3;
  132. ParsePosition pos(0), pos01(0);
  133. fr->parseObject(text, result1, pos);
  134. if(result1.getType() != Formattable::kDouble && result1.getDouble() != d) {
  135. errln("ERROR: Roundtrip failed (via parse()) for " + text);
  136. }
  137. logln(text + " parsed into " + (int32_t) result1.getDouble());
  138. fr->parse(text, result2, pos01);
  139. if(result2.getType() != Formattable::kDouble && result2.getDouble() != d) {
  140. errln("ERROR: Roundtrip failed (via parse()) for " + text);
  141. }
  142. logln(text + " parsed into " + (int32_t) result2.getDouble());
  143. status = U_ZERO_ERROR;
  144. fr->parse(text, result3, status);
  145. if(U_FAILURE(status)) {
  146. errln("ERROR: parse() failed");
  147. }
  148. if(result3.getType() != Formattable::kDouble && result3.getDouble() != d) {
  149. errln("ERROR: Roundtrip failed (via parse()) for " + text);
  150. }
  151. logln(text + " parsed into " + (int32_t) result3.getDouble());
  152. }
  153. // ======= Test getters and setters
  154. if (fr != NULL && def != NULL)
  155. {
  156. logln("Testing getters and setters");
  157. int32_t count = 0;
  158. const Locale *locales = NumberFormat::getAvailableLocales(count);
  159. logln((UnicodeString) "Got " + count + " locales" );
  160. for(int32_t i = 0; i < count; i++) {
  161. UnicodeString name(locales[i].getName(),"");
  162. logln(name);
  163. }
  164. fr->setParseIntegerOnly( def->isParseIntegerOnly() );
  165. if(fr->isParseIntegerOnly() != def->isParseIntegerOnly() ) {
  166. errln("ERROR: setParseIntegerOnly() failed");
  167. }
  168. fr->setGroupingUsed( def->isGroupingUsed() );
  169. if(fr->isGroupingUsed() != def->isGroupingUsed() ) {
  170. errln("ERROR: setGroupingUsed() failed");
  171. }
  172. fr->setMaximumIntegerDigits( def->getMaximumIntegerDigits() );
  173. if(fr->getMaximumIntegerDigits() != def->getMaximumIntegerDigits() ) {
  174. errln("ERROR: setMaximumIntegerDigits() failed");
  175. }
  176. fr->setMinimumIntegerDigits( def->getMinimumIntegerDigits() );
  177. if(fr->getMinimumIntegerDigits() != def->getMinimumIntegerDigits() ) {
  178. errln("ERROR: setMinimumIntegerDigits() failed");
  179. }
  180. fr->setMaximumFractionDigits( def->getMaximumFractionDigits() );
  181. if(fr->getMaximumFractionDigits() != def->getMaximumFractionDigits() ) {
  182. errln("ERROR: setMaximumFractionDigits() failed");
  183. }
  184. fr->setMinimumFractionDigits( def->getMinimumFractionDigits() );
  185. if(fr->getMinimumFractionDigits() != def->getMinimumFractionDigits() ) {
  186. errln("ERROR: setMinimumFractionDigits() failed");
  187. }
  188. }
  189. // ======= Test getStaticClassID()
  190. logln("Testing getStaticClassID()");
  191. status = U_ZERO_ERROR;
  192. NumberFormat *test = new DecimalFormat(status);
  193. if(U_FAILURE(status)) {
  194. errcheckln(status, "ERROR: Couldn't create a NumberFormat - %s", u_errorName(status));
  195. }
  196. if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  197. errln("ERROR: getDynamicClassID() didn't return the expected value");
  198. }
  199. delete test;
  200. delete def;
  201. delete fr;
  202. delete cur;
  203. delete cur_fr;
  204. delete per;
  205. delete per_fr;
  206. }
  207. #if !UCONFIG_NO_SERVICE
  208. #define SRC_LOC Locale::getFrance()
  209. #define SWAP_LOC Locale::getUS()
  210. class NFTestFactory : public SimpleNumberFormatFactory {
  211. NumberFormat* currencyStyle;
  212. public:
  213. NFTestFactory()
  214. : SimpleNumberFormatFactory(SRC_LOC, TRUE)
  215. {
  216. UErrorCode status = U_ZERO_ERROR;
  217. currencyStyle = NumberFormat::createInstance(SWAP_LOC, status);
  218. }
  219. virtual ~NFTestFactory()
  220. {
  221. delete currencyStyle;
  222. }
  223. virtual NumberFormat* createFormat(const Locale& /* loc */, UNumberFormatStyle formatType)
  224. {
  225. if (formatType == UNUM_CURRENCY) {
  226. return (NumberFormat*)currencyStyle->clone();
  227. }
  228. return NULL;
  229. }
  230. virtual inline UClassID getDynamicClassID() const
  231. {
  232. return (UClassID)&gID;
  233. }
  234. static inline UClassID getStaticClassID()
  235. {
  236. return (UClassID)&gID;
  237. }
  238. private:
  239. static char gID;
  240. };
  241. char NFTestFactory::gID = 0;
  242. #endif
  243. void
  244. IntlTestNumberFormatAPI::testRegistration()
  245. {
  246. #if !UCONFIG_NO_SERVICE
  247. UErrorCode status = U_ZERO_ERROR;
  248. LocalPointer<NumberFormat> f0(NumberFormat::createInstance(SWAP_LOC, status));
  249. LocalPointer<NumberFormat> f1(NumberFormat::createInstance(SRC_LOC, status));
  250. LocalPointer<NumberFormat> f2(NumberFormat::createCurrencyInstance(SRC_LOC, status));
  251. URegistryKey key = NumberFormat::registerFactory(new NFTestFactory(), status);
  252. LocalPointer<NumberFormat> f3(NumberFormat::createCurrencyInstance(SRC_LOC, status));
  253. LocalPointer<NumberFormat> f3a(NumberFormat::createCurrencyInstance(SRC_LOC, status));
  254. LocalPointer<NumberFormat> f4(NumberFormat::createInstance(SRC_LOC, status));
  255. StringEnumeration* locs = NumberFormat::getAvailableLocales();
  256. LocalUNumberFormatPointer uf3(unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(), NULL, &status));
  257. LocalUNumberFormatPointer uf4(unum_open(UNUM_DEFAULT, NULL, 0, SRC_LOC.getName(), NULL, &status));
  258. const UnicodeString* res;
  259. for (res = locs->snext(status); res; res = locs->snext(status)) {
  260. logln(*res); // service is still in synch
  261. }
  262. NumberFormat::unregister(key, status); // restore for other tests
  263. LocalPointer<NumberFormat> f5(NumberFormat::createCurrencyInstance(SRC_LOC, status));
  264. LocalUNumberFormatPointer uf5(unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(), NULL, &status));
  265. if (U_FAILURE(status)) {
  266. dataerrln("Error creating instnaces.");
  267. return;
  268. } else {
  269. float n = 1234.567f;
  270. UnicodeString res0, res1, res2, res3, res4, res5;
  271. UChar ures3[50];
  272. UChar ures4[50];
  273. UChar ures5[50];
  274. f0->format(n, res0);
  275. f1->format(n, res1);
  276. f2->format(n, res2);
  277. f3->format(n, res3);
  278. f4->format(n, res4);
  279. f5->format(n, res5);
  280. unum_formatDouble(uf3.getAlias(), n, ures3, 50, NULL, &status);
  281. unum_formatDouble(uf4.getAlias(), n, ures4, 50, NULL, &status);
  282. unum_formatDouble(uf5.getAlias(), n, ures5, 50, NULL, &status);
  283. logln((UnicodeString)"f0 swap int: " + res0);
  284. logln((UnicodeString)"f1 src int: " + res1);
  285. logln((UnicodeString)"f2 src cur: " + res2);
  286. logln((UnicodeString)"f3 reg cur: " + res3);
  287. logln((UnicodeString)"f4 reg int: " + res4);
  288. logln((UnicodeString)"f5 unreg cur: " + res5);
  289. log("uf3 reg cur: ");
  290. logln(ures3);
  291. log("uf4 reg int: ");
  292. logln(ures4);
  293. log("uf5 ureg cur: ");
  294. logln(ures5);
  295. if (f3.getAlias() == f3a.getAlias()) {
  296. errln("did not get new instance from service");
  297. f3a.orphan();
  298. }
  299. if (res3 != res0) {
  300. errln("registered service did not match");
  301. }
  302. if (res4 != res1) {
  303. errln("registered service did not inherit");
  304. }
  305. if (res5 != res2) {
  306. errln("unregistered service did not match original");
  307. }
  308. if (res0 != ures3) {
  309. errln("registered service did not match / unum");
  310. }
  311. if (res1 != ures4) {
  312. errln("registered service did not inherit / unum");
  313. }
  314. if (res2 != ures5) {
  315. errln("unregistered service did not match original / unum");
  316. }
  317. }
  318. for (res = locs->snext(status); res; res = locs->snext(status)) {
  319. errln(*res); // service should be out of synch
  320. }
  321. locs->reset(status); // now in synch again, we hope
  322. for (res = locs->snext(status); res; res = locs->snext(status)) {
  323. logln(*res);
  324. }
  325. delete locs;
  326. #endif
  327. }
  328. #endif /* #if !UCONFIG_NO_FORMATTING */