Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update getConstantDenominator to return uint64_t and enhance unit tes…
Browse files Browse the repository at this point in the history
…ts for constant denominators
younies committed Jan 21, 2025
1 parent 4ea2d9e commit c7e55db
Showing 2 changed files with 81 additions and 3 deletions.
4 changes: 2 additions & 2 deletions icu4c/source/i18n/measunit_extra.cpp
Original file line number Diff line number Diff line change
@@ -710,9 +710,9 @@ class Parser {
return result;
}

uint16_t getConstantDenominator() const {
uint64_t getConstantDenominator() const {
U_ASSERT(type == kConstantDenominator);
return static_cast<uint16_t>(constantDenominator);
return constantDenominator;
}

SingleUnitImpl getSingleUnit() const {
80 changes: 79 additions & 1 deletion icu4c/source/test/intltest/units_test.cpp
Original file line number Diff line number Diff line change
@@ -1168,6 +1168,76 @@ void UnitsTest::testUnitsConstantsDenomenator() {
const uint64_t expectedConstant;
} testCases[]{
{"meter-per-1000", 1000},
{"liter-per-1000-kiloliter", 1000},
{"liter-per-kilometer", 0},
{"second-per-1000-minute", 1000},
{"gram-per-1000-kilogram", 1000},
{"meter-per-100", 100},
{"portion-per-1", 1},
{"portion-per-2", 2},
{"portion-per-3", 3},
{"portion-per-4", 4},
{"portion-per-5", 5},
{"portion-per-6", 6},
{"portion-per-7", 7},
{"portion-per-8", 8},
{"portion-per-9", 9},
// Test for constant denominators that are powers of 10
{"portion-per-10", 10},
{"portion-per-100", 100},
{"portion-per-1000", 1000},
{"portion-per-10000", 10000},
{"portion-per-100000", 100000},
{"portion-per-1000000", 1000000},
{"portion-per-10000000", 10000000},
{"portion-per-100000000", 100000000},
{"portion-per-1000000000", 1000000000},
{"portion-per-10000000000", 10000000000},
{"portion-per-100000000000", 100000000000},
{"portion-per-1000000000000", 1000000000000},
{"portion-per-10000000000000", 10000000000000},
{"portion-per-100000000000000", 100000000000000},
{"portion-per-1000000000000000", 1000000000000000},
{"portion-per-10000000000000000", 10000000000000000},
{"portion-per-100000000000000000", 100000000000000000},
{"portion-per-1000000000000000000", 1000000000000000000},
// Test for constant denominators that are represented as scientific notation
// numbers.
{"portion-per-1e1", 10},
{"portion-per-1E1", 10},
{"portion-per-1e2", 100},
{"portion-per-1E2", 100},
{"portion-per-1e3", 1000},
{"portion-per-1E3", 1000},
{"portion-per-1e4", 10000},
{"portion-per-1E4", 10000},
{"portion-per-1e5", 100000},
{"portion-per-1E5", 100000},
{"portion-per-1e6", 1000000},
{"portion-per-1E6", 1000000},
{"portion-per-1e10", 10000000000},
{"portion-per-1E10", 10000000000},
{"portion-per-1e18", 1000000000000000000},
{"portion-per-1E18", 1000000000000000000},
// Test for constant denominators that are randomly selected.
{"liter-per-12345-kilometer", 12345},
{"per-1000-kilometer", 1000},
{"liter-per-1000-kiloliter", 1000},
// Test for constant denominators that give 0.
{"meter", 0},
{"meter-per-second", 0},
{"meter-per-square-second", 0},
// NOTE: The following constant denominator should be 0. However, since
// `100-kilometer` is treated as a unit in CLDR,
// the unit does not have a constant denominator.
// This issue should be addressed in CLDR.
{"meter-per-100-kilometer", 0},
// NOTE: the following CLDR identifier should be invalid, but because
// `100-kilometer` is considered a unit in CLDR,
// one `100` will be considered as a unit constant denominator and the other
// `100` will be considered part of the unit.
// This issue should be addressed in CLDR.
{"meter-per-100-100-kilometer", 100},
};

for (const auto &testCase : testCases) {
@@ -1181,12 +1251,20 @@ void UnitsTest::testUnitsConstantsDenomenator() {
continue;
}

auto complexity = unit.getComplexity(status);
if (status.errIfFailureAndReset("getComplexity(\"%s\")", testCase.source)) {
continue;
}

if (constant != testCase.expectedConstant) {
status.set(U_ILLEGAL_ARGUMENT_ERROR);
status.set(U_PARSE_ERROR);
if (status.errIfFailureAndReset("getConstantDenominator(\"%s\")", testCase.source)) {
continue;
}
}
if (constant != 0) {
assertEquals("getComplexity(\"%s\")", UMEASURE_UNIT_COMPOUND, complexity);
}
}
}

0 comments on commit c7e55db

Please sign in to comment.