X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/b75a7d8f3b4adbae880cab104ce2c6a50eee4db2..e4f10fab0c078f399c9deef476d9c9b73b47dff8:/icuSources/test/intltest/nmfmapts.cpp diff --git a/icuSources/test/intltest/nmfmapts.cpp b/icuSources/test/intltest/nmfmapts.cpp index 4f143c69..ca89948b 100644 --- a/icuSources/test/intltest/nmfmapts.cpp +++ b/icuSources/test/intltest/nmfmapts.cpp @@ -1,8 +1,8 @@ -/******************************************************************** +/*********************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2003, International Business Machines Corporation and - * others. All Rights Reserved. - ********************************************************************/ + * Copyright (c) 1997-2010, International Business Machines Corporation + * and others. All Rights Reserved. + ***********************************************************************/ #include "unicode/utypes.h" @@ -28,22 +28,26 @@ void IntlTestNumberFormatAPI::runIndexedTest( int32_t index, UBool exec, const c if (exec) { logln("NumberFormat API test---"); logln(""); UErrorCode status = U_ZERO_ERROR; + Locale saveLocale; Locale::setDefault(Locale::getEnglish(), status); if(U_FAILURE(status)) { errln("ERROR: Could not set default locale, test may not give correct results"); } testAPI(/* par */); + Locale::setDefault(saveLocale, status); } break; case 1: name = "NumberFormatRegistration"; if (exec) { logln("NumberFormat Registration test---"); logln(""); UErrorCode status = U_ZERO_ERROR; + Locale saveLocale; Locale::setDefault(Locale::getEnglish(), status); if(U_FAILURE(status)) { errln("ERROR: Could not set default locale, test may not give correct results"); } testRegistration(); + Locale::setDefault(saveLocale, status); } break; default: name = ""; break; @@ -64,47 +68,50 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) NumberFormat *def = NumberFormat::createInstance(status); if(U_FAILURE(status)) { - errln("ERROR: Could not create NumberFormat (default)"); + dataerrln("ERROR: Could not create NumberFormat (default) - %s", u_errorName(status)); } status = U_ZERO_ERROR; NumberFormat *fr = NumberFormat::createInstance(Locale::getFrench(), status); if(U_FAILURE(status)) { - errln("ERROR: Could not create NumberFormat (French)"); + dataerrln("ERROR: Could not create NumberFormat (French) - %s", u_errorName(status)); } NumberFormat *cur = NumberFormat::createCurrencyInstance(status); if(U_FAILURE(status)) { - errln("ERROR: Could not create NumberFormat (currency, default)"); + dataerrln("ERROR: Could not create NumberFormat (currency, default) - %s", u_errorName(status)); } status = U_ZERO_ERROR; NumberFormat *cur_fr = NumberFormat::createCurrencyInstance(Locale::getFrench(), status); if(U_FAILURE(status)) { - errln("ERROR: Could not create NumberFormat (currency, French)"); + dataerrln("ERROR: Could not create NumberFormat (currency, French) - %s", u_errorName(status)); } NumberFormat *per = NumberFormat::createPercentInstance(status); if(U_FAILURE(status)) { - errln("ERROR: Could not create NumberFormat (percent, default)"); + dataerrln("ERROR: Could not create NumberFormat (percent, default) - %s", u_errorName(status)); } status = U_ZERO_ERROR; NumberFormat *per_fr = NumberFormat::createPercentInstance(Locale::getFrench(), status); if(U_FAILURE(status)) { - errln("ERROR: Could not create NumberFormat (percent, French)"); + dataerrln("ERROR: Could not create NumberFormat (percent, French) - %s", u_errorName(status)); } // ======= Test equality - +if (per_fr != NULL && cur_fr != NULL) +{ logln("Testing equality operator"); if( *per_fr == *cur_fr || ! ( *per_fr != *cur_fr) ) { errln("ERROR: == failed"); } +} // ======= Test various format() methods - +if (cur_fr != NULL) +{ logln("Testing various format() methods"); double d = -10456.0037; @@ -140,12 +147,14 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) errln("ERROR: format(Formattable [long]) failed"); } logln((UnicodeString) "" + fL.getLong() + " formatted to " + res6); - +} // ======= Test parse() - +if (fr != NULL) +{ logln("Testing parse()"); + double d = -10456.0037; UnicodeString text("-10,456.0037"); Formattable result1, result2, result3; ParsePosition pos(0), pos01(0); @@ -170,10 +179,11 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) errln("ERROR: Roundtrip failed (via parse()) for " + text); } logln(text + " parsed into " + (int32_t) result3.getDouble()); - +} // ======= Test getters and setters - +if (fr != NULL && def != NULL) +{ logln("Testing getters and setters"); int32_t count = 0; @@ -213,7 +223,7 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) if(fr->getMinimumFractionDigits() != def->getMinimumFractionDigits() ) { errln("ERROR: setMinimumFractionDigits() failed"); } - +} // ======= Test getStaticClassID() @@ -222,7 +232,7 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) status = U_ZERO_ERROR; NumberFormat *test = new DecimalFormat(status); if(U_FAILURE(status)) { - errln("ERROR: Couldn't create a NumberFormat"); + errcheckln(status, "ERROR: Couldn't create a NumberFormat - %s", u_errorName(status)); } if(test->getDynamicClassID() != DecimalFormat::getStaticClassID()) { @@ -238,41 +248,43 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) delete per_fr; } +#if !UCONFIG_NO_SERVICE + #define SRC_LOC Locale::getFrance() #define SWAP_LOC Locale::getUS() class NFTestFactory : public SimpleNumberFormatFactory { - NumberFormat* currencyStyle; + NumberFormat* currencyStyle; public: - NFTestFactory() - : SimpleNumberFormatFactory(SRC_LOC, TRUE) - { - UErrorCode status = U_ZERO_ERROR; - currencyStyle = NumberFormat::createInstance(SWAP_LOC, status); - } - - virtual ~NFTestFactory() - { - delete currencyStyle; - } - - virtual NumberFormat* createFormat(const Locale& loc, UNumberFormatStyle formatType) - { - if (formatType == UNUM_CURRENCY) { - return (NumberFormat*)currencyStyle->clone(); - } - return NULL; - } + NFTestFactory() + : SimpleNumberFormatFactory(SRC_LOC, TRUE) + { + UErrorCode status = U_ZERO_ERROR; + currencyStyle = NumberFormat::createInstance(SWAP_LOC, status); + } + + virtual ~NFTestFactory() + { + delete currencyStyle; + } + + virtual NumberFormat* createFormat(const Locale& /* loc */, UNumberFormatStyle formatType) + { + if (formatType == UNUM_CURRENCY) { + return (NumberFormat*)currencyStyle->clone(); + } + return NULL; + } virtual inline UClassID getDynamicClassID() const { - return (UClassID)&gID; + return (UClassID)&gID; } static inline UClassID getStaticClassID() { - return (UClassID)&gID; + return (UClassID)&gID; } private: @@ -280,110 +292,106 @@ private: }; char NFTestFactory::gID = 0; +#endif void IntlTestNumberFormatAPI::testRegistration() { - - UErrorCode status = U_ZERO_ERROR; - - NumberFormat* f0 = NumberFormat::createInstance(SWAP_LOC, status); - NumberFormat* f1 = NumberFormat::createInstance(SRC_LOC, status); - NumberFormat* f2 = NumberFormat::createCurrencyInstance(SRC_LOC, status); - URegistryKey key = NumberFormat::registerFactory(new NFTestFactory(), status); - NumberFormat* f3 = NumberFormat::createCurrencyInstance(SRC_LOC, status); - NumberFormat* f3a = NumberFormat::createCurrencyInstance(SRC_LOC, status); - NumberFormat* f4 = NumberFormat::createInstance(SRC_LOC, status); - - StringEnumeration* locs = NumberFormat::getAvailableLocales(); - - UNumberFormat* uf3 = unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(),NULL, &status); - UNumberFormat* uf4 = unum_open(UNUM_DEFAULT, NULL, 0, SRC_LOC.getName(), NULL, &status); +#if !UCONFIG_NO_SERVICE + UErrorCode status = U_ZERO_ERROR; + + LocalPointer f0(NumberFormat::createInstance(SWAP_LOC, status)); + LocalPointer f1(NumberFormat::createInstance(SRC_LOC, status)); + LocalPointer f2(NumberFormat::createCurrencyInstance(SRC_LOC, status)); + URegistryKey key = NumberFormat::registerFactory(new NFTestFactory(), status); + LocalPointer f3(NumberFormat::createCurrencyInstance(SRC_LOC, status)); + LocalPointer f3a(NumberFormat::createCurrencyInstance(SRC_LOC, status)); + LocalPointer f4(NumberFormat::createInstance(SRC_LOC, status)); + + StringEnumeration* locs = NumberFormat::getAvailableLocales(); + + LocalUNumberFormatPointer uf3(unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(), NULL, &status)); + LocalUNumberFormatPointer uf4(unum_open(UNUM_DEFAULT, NULL, 0, SRC_LOC.getName(), NULL, &status)); const UnicodeString* res; - for (res = locs->snext(status); res; res = locs->snext(status)) { - logln(*res); // service is still in synch - } - - NumberFormat::unregister(key, status); // restore for other tests - NumberFormat* f5 = NumberFormat::createCurrencyInstance(SRC_LOC, status); - UNumberFormat* uf5 = unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(),NULL, &status); - - float n = 1234.567f; - UnicodeString res0, res1, res2, res3, res4, res5; - UChar ures3[50]; - UChar ures4[50]; - UChar ures5[50]; - - f0->format(n, res0); - f1->format(n, res1); - f2->format(n, res2); - f3->format(n, res3); - f4->format(n, res4); - f5->format(n, res5); - - unum_formatDouble(uf3, n, ures3, 50, NULL, &status); - unum_formatDouble(uf4, n, ures4, 50, NULL, &status); - unum_formatDouble(uf5, n, ures5, 50, NULL, &status); - - logln((UnicodeString)"f0 swap int: " + res0); - logln((UnicodeString)"f1 src int: " + res1); - logln((UnicodeString)"f2 src cur: " + res2); - logln((UnicodeString)"f3 reg cur: " + res3); - logln((UnicodeString)"f4 reg int: " + res4); - logln((UnicodeString)"f5 unreg cur: " + res5); - log("uf3 reg cur: "); - logln(ures3); - log("uf4 reg int: "); - logln(ures4); - log("uf5 ureg cur: "); - logln(ures5); - - if (f3 == f3a) { - errln("did not get new instance from service"); - } else { - delete f3a; - } - if (res3 != res0) { - errln("registered service did not match"); - } - if (res4 != res1) { - errln("registered service did not inherit"); - } - if (res5 != res2) { - errln("unregistered service did not match original"); - } - - if (res0 != ures3) { - errln("registered service did not match / unum"); - } - if (res1 != ures4) { - errln("registered service did not inherit / unum"); - } - if (res2 != ures5) { - errln("unregistered service did not match original / unum"); - } - - unum_close(uf5); - delete f5; - unum_close(uf4); - unum_close(uf3); - delete f4; - delete f3; - delete f2; - delete f1; - delete f0; - - for (res = locs->snext(status); res; res = locs->snext(status)) { - errln(*res); // service should be out of synch - } - - locs->reset(status); // now in synch again, we hope - for (res = locs->snext(status); res; res = locs->snext(status)) { - logln(*res); - } - - delete locs; + for (res = locs->snext(status); res; res = locs->snext(status)) { + logln(*res); // service is still in synch + } + + NumberFormat::unregister(key, status); // restore for other tests + LocalPointer f5(NumberFormat::createCurrencyInstance(SRC_LOC, status)); + LocalUNumberFormatPointer uf5(unum_open(UNUM_CURRENCY, NULL, 0, SRC_LOC.getName(), NULL, &status)); + + if (U_FAILURE(status)) { + dataerrln("Error creating instnaces."); + return; + } else { + float n = 1234.567f; + UnicodeString res0, res1, res2, res3, res4, res5; + UChar ures3[50]; + UChar ures4[50]; + UChar ures5[50]; + + f0->format(n, res0); + f1->format(n, res1); + f2->format(n, res2); + f3->format(n, res3); + f4->format(n, res4); + f5->format(n, res5); + + unum_formatDouble(uf3.getAlias(), n, ures3, 50, NULL, &status); + unum_formatDouble(uf4.getAlias(), n, ures4, 50, NULL, &status); + unum_formatDouble(uf5.getAlias(), n, ures5, 50, NULL, &status); + + logln((UnicodeString)"f0 swap int: " + res0); + logln((UnicodeString)"f1 src int: " + res1); + logln((UnicodeString)"f2 src cur: " + res2); + logln((UnicodeString)"f3 reg cur: " + res3); + logln((UnicodeString)"f4 reg int: " + res4); + logln((UnicodeString)"f5 unreg cur: " + res5); + log("uf3 reg cur: "); + logln(ures3); + log("uf4 reg int: "); + logln(ures4); + log("uf5 ureg cur: "); + logln(ures5); + + if (f3.getAlias() == f3a.getAlias()) { + errln("did not get new instance from service"); + f3a.orphan(); + } + if (res3 != res0) { + errln("registered service did not match"); + } + if (res4 != res1) { + errln("registered service did not inherit"); + } + if (res5 != res2) { + errln("unregistered service did not match original"); + } + + if (res0 != ures3) { + errln("registered service did not match / unum"); + } + if (res1 != ures4) { + errln("registered service did not inherit / unum"); + } + if (res2 != ures5) { + errln("unregistered service did not match original / unum"); + } + } + + for (res = locs->snext(status); res; res = locs->snext(status)) { + errln(*res); // service should be out of synch + } + + locs->reset(status); // now in synch again, we hope + for (res = locs->snext(status); res; res = locs->snext(status)) { + logln(*res); + } + + delete locs; +#endif }