From 42e3b43c7863bb5c4da2bfa952766af40c46c7b9 Mon Sep 17 00:00:00 2001 From: Younies Mahmoud Date: Tue, 11 Feb 2025 14:04:34 +0000 Subject: [PATCH] ICU-22781 Add test for portion format with parts per billion (C++) See #3389 --- icu4c/source/test/intltest/numbertest.h | 1 + icu4c/source/test/intltest/numbertest_api.cpp | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/icu4c/source/test/intltest/numbertest.h b/icu4c/source/test/intltest/numbertest.h index d384e0f22f2c..fe0d63393006 100644 --- a/icu4c/source/test/intltest/numbertest.h +++ b/icu4c/source/test/intltest/numbertest.h @@ -103,6 +103,7 @@ class NumberFormatterApiTest : public IntlTestWithFieldPosition { void toDecimalNumber(); void microPropsInternals(); void formatUnitsAliases(); + void TestPortionFormat(); void testIssue22378(); void runIndexedTest(int32_t index, UBool exec, const char*& name, char* par = nullptr) override; diff --git a/icu4c/source/test/intltest/numbertest_api.cpp b/icu4c/source/test/intltest/numbertest_api.cpp index 4938818468e7..b44997a98168 100644 --- a/icu4c/source/test/intltest/numbertest_api.cpp +++ b/icu4c/source/test/intltest/numbertest_api.cpp @@ -133,6 +133,7 @@ void NumberFormatterApiTest::runIndexedTest(int32_t index, UBool exec, const cha TESTCASE_AUTO(toDecimalNumber); TESTCASE_AUTO(microPropsInternals); TESTCASE_AUTO(formatUnitsAliases); + TESTCASE_AUTO(TestPortionFormat); TESTCASE_AUTO(testIssue22378); TESTCASE_AUTO_END; } @@ -6085,6 +6086,44 @@ void NumberFormatterApiTest::formatUnitsAliases() { } } +void NumberFormatterApiTest::TestPortionFormat() { + IcuTestErrorCode status(*this, "TestPortionFormat"); + + struct TestCase { + const char *unitIdentifier; + const char *locale; + double inputValue; + UnicodeString expectedOutput; + } testCases[]{ + {"portion-per-1e9", "en-US", 1, "1 part per billion"}, + {"portion-per-1e9", "en-US", 2, "2 parts per billion"}, + {"portion-per-1e9", "en-US", 1000000, "1,000,000 parts per billion"}, + {"portion-per-1e9", "de-DE", 1000000, "1.000.000 Milliardstel"}, + {"portion-per-1e1", "en-US", 1, "UNKNOWN"}, + {"portion-per-1e2", "en-US", 1, "UNKNOWN"}, + {"portion-per-1e3", "en-US", 1, "UNKNOWN"}, + {"portion-per-1e4", "en-US", 1, "UNKNOWN"}, + {"portion-per-1e5", "en-US", 1, "UNKNOWN"}, + {"portion-per-1e6", "en-US", 1, "UNKNOWN"}, + {"portion-per-1e7", "en-US", 1, "UNKNOWN"}, + {"portion-per-1e8", "en-US", 1, "UNKNOWN"}, + }; + + for (auto testCase : testCases) { + if (uprv_strcmp(testCase.unitIdentifier, "portion-per-1e9") != 0) { + logKnownIssue("CLDR-18274", "The data for portion-per-XYZ is not determined yet."); + continue; + } + MeasureUnit unit = MeasureUnit::forIdentifier(testCase.unitIdentifier, status); + LocalizedNumberFormatter lnf = + NumberFormatter::withLocale(Locale::forLanguageTag(testCase.locale, status)) + .unit(unit) + .unitWidth(UNumberUnitWidth::UNUM_UNIT_WIDTH_FULL_NAME); + UnicodeString actualOutput = lnf.formatDouble(testCase.inputValue, status).toString(status); + assertEquals("test portion format", testCase.expectedOutput, actualOutput); + } +} + void NumberFormatterApiTest::testIssue22378() { IcuTestErrorCode status(*this, "testIssue22378");