From 8c2de1401e08a2e2637125cdd8e850462b8853f2 Mon Sep 17 00:00:00 2001 From: Shane Carr Date: Fri, 15 Feb 2019 02:15:18 -0800 Subject: [PATCH] ICU-20144 Adding better documentation and behavior testing on NumberingSystem. --- icu4c/source/i18n/unicode/numsys.h | 6 ++++-- icu4c/source/i18n/unicode/unumsys.h | 1 + icu4c/source/test/intltest/numfmtst.cpp | 16 +++++++++++++++- .../src/com/ibm/icu/text/NumberingSystem.java | 5 ++++- .../icu/dev/test/format/NumberFormatTest.java | 11 +++++++++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/icu4c/source/i18n/unicode/numsys.h b/icu4c/source/i18n/unicode/numsys.h index 9ec3f13fd10..38dbc266353 100644 --- a/icu4c/source/i18n/unicode/numsys.h +++ b/icu4c/source/i18n/unicode/numsys.h @@ -106,9 +106,9 @@ public: /** * Return a StringEnumeration over all the names of numbering systems known to ICU. + * The numbering system names will be in alphabetical (invariant) order. * @stable ICU 4.2 */ - static StringEnumeration * U_EXPORT2 getAvailableNames(UErrorCode& status); /** @@ -119,8 +119,10 @@ public: * default, native, traditional, finance - do not identify specific numbering systems, * but rather key values that may only be used as part of a locale, which in turn * defines how they are mapped to a specific numbering system such as "latn" or "hant". + * * @param name The name of the numbering system. - * @param status ICU status + * @param status ICU status; set to U_UNSUPPORTED_ERROR if numbering system not found. + * @return The NumberingSystem instance, or nullptr if not found. * @stable ICU 4.2 */ static NumberingSystem* U_EXPORT2 createInstanceByName(const char* name, UErrorCode& status); diff --git a/icu4c/source/i18n/unicode/unumsys.h b/icu4c/source/i18n/unicode/unumsys.h index 795ec60255f..1631c234fd2 100644 --- a/icu4c/source/i18n/unicode/unumsys.h +++ b/icu4c/source/i18n/unicode/unumsys.h @@ -105,6 +105,7 @@ U_NAMESPACE_END /** * Returns an enumeration over the names of all of the predefined numbering systems known * to ICU. + * The numbering system names will be in alphabetical (invariant) order. * @param status A pointer to a UErrorCode to receive any errors. * @return A pointer to a UEnumeration that must be closed with uenum_close(), * or NULL if an error occurred. diff --git a/icu4c/source/test/intltest/numfmtst.cpp b/icu4c/source/test/intltest/numfmtst.cpp index e1fc7ae7355..9d773fc3d74 100644 --- a/icu4c/source/test/intltest/numfmtst.cpp +++ b/icu4c/source/test/intltest/numfmtst.cpp @@ -6954,7 +6954,7 @@ void NumberFormatTest::TestExplicitParents() { * Test available numbering systems API. */ void NumberFormatTest::TestAvailableNumberingSystems() { - UErrorCode status = U_ZERO_ERROR; + IcuTestErrorCode status(*this, "TestAvailableNumberingSystems"); StringEnumeration *availableNumberingSystems = NumberingSystem::getAvailableNames(status); CHECK_DATA(status, "NumberingSystem::getAvailableNames()") @@ -6969,6 +6969,7 @@ void NumberFormatTest::TestAvailableNumberingSystems() { /* one that we initially thought. */ int32_t len; + const char* prevName = nullptr; for ( int32_t i = 0 ; i < nsCount ; i++ ) { const char *nsname = availableNumberingSystems->next(&len,status); NumberingSystem* ns = NumberingSystem::createInstanceByName(nsname,status); @@ -6976,10 +6977,23 @@ void NumberFormatTest::TestAvailableNumberingSystems() { if ( uprv_strcmp(nsname,ns->getName()) ) { errln("FAIL: Numbering system name didn't match for name = %s\n",nsname); } + if (prevName != nullptr) { + int comp = uprv_strcmp(prevName, nsname); + assertTrue( + UnicodeString(u"NS names should be in alphabetical order: ") + + prevName + u" vs " + nsname, + // TODO: Why are there duplicates? This doesn't work if comp < 0 + comp <= 0); + } + prevName = nsname; delete ns; } + LocalPointer dummy(NumberingSystem::createInstanceByName("dummy", status), status); + status.expectErrorAndReset(U_UNSUPPORTED_ERROR); + assertTrue("Non-existent numbering system should return null", dummy.isNull()); + delete availableNumberingSystems; } diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/NumberingSystem.java b/icu4j/main/classes/core/src/com/ibm/icu/text/NumberingSystem.java index c23246eab2c..3adee1d32c2 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/NumberingSystem.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/NumberingSystem.java @@ -223,6 +223,7 @@ public class NumberingSystem { * @param name The name of the desired numbering system. Numbering system * names often correspond with the name of the script they are associated * with. For example, "thai" for Thai digits, "hebr" for Hebrew numerals. + * @return The NumberingSystem instance, or null if not available. * @stable ICU 4.2 */ public static NumberingSystem getInstanceByName(String name) { @@ -257,6 +258,8 @@ public class NumberingSystem { /** * Returns a string array containing a list of the names of numbering systems * currently known to ICU. + * + * @return An array of strings in alphabetical (invariant) order. * @stable ICU 4.2 */ public static String [] getAvailableNames() { @@ -266,7 +269,7 @@ public class NumberingSystem { UResourceBundle temp; String nsName; - ArrayList output = new ArrayList(); + ArrayList output = new ArrayList<>(); UResourceBundleIterator it = nsCurrent.getIterator(); while (it.hasNext()) { temp = it.next(); diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index 24695a24af2..7cefa5e09a8 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -1912,8 +1912,16 @@ public class NumberFormatTest extends TestFmwk { if (availableNames == null || availableNames.length <= 0) { errln("ERROR: NumberingSystem.getAvailableNames() returned a null or empty array."); } else { + // Check for alphabetical order + for (int i=0; i