X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/374ca955a76ecab1204ca8bfa63ff9238d998416..c5116b9f5a666b9d59f443b3770acd6ef64dc6c3:/icuSources/test/intltest/nmfmapts.cpp diff --git a/icuSources/test/intltest/nmfmapts.cpp b/icuSources/test/intltest/nmfmapts.cpp index 8d8875e0..07b20446 100644 --- a/icuSources/test/intltest/nmfmapts.cpp +++ b/icuSources/test/intltest/nmfmapts.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /*********************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2004, International Business Machines Corporation + * Copyright (c) 1997-2010, International Business Machines Corporation * and others. All Rights Reserved. ***********************************************************************/ @@ -28,22 +30,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 +70,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; @@ -113,7 +122,7 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) Formattable fL(l); UnicodeString res1, res2, res3, res4, res5, res6; - FieldPosition pos1(0), pos2(0), pos3(0), pos4(0); + FieldPosition pos1(FieldPosition::DONT_CARE), pos2(FieldPosition::DONT_CARE), pos3(FieldPosition::DONT_CARE), pos4(FieldPosition::DONT_CARE); res1 = cur_fr->format(d, res1); logln( (UnicodeString) "" + (int32_t) d + " formatted to " + res1); @@ -140,12 +149,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 +181,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 +225,7 @@ void IntlTestNumberFormatAPI::testAPI(/* char* par */) if(fr->getMinimumFractionDigits() != def->getMinimumFractionDigits() ) { errln("ERROR: setMinimumFractionDigits() failed"); } - +} // ======= Test getStaticClassID() @@ -222,7 +234,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()) { @@ -290,18 +302,18 @@ IntlTestNumberFormatAPI::testRegistration() #if !UCONFIG_NO_SERVICE 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); + 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); - NumberFormat* f3 = NumberFormat::createCurrencyInstance(SRC_LOC, status); - NumberFormat* f3a = NumberFormat::createCurrencyInstance(SRC_LOC, status); - NumberFormat* f4 = NumberFormat::createInstance(SRC_LOC, 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(); - 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); + 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)) { @@ -309,74 +321,68 @@ IntlTestNumberFormatAPI::testRegistration() } 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"); + 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 { - 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"); - } + 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"); + 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 }