]> granicus.if.org Git - icu/commitdiff
fix compound basic unit
authorYounies Mahmoud <younies.mahmoud@gmail.com>
Tue, 14 Apr 2020 13:52:51 +0000 (15:52 +0200)
committerYounies Mahmoud <younies.mahmoud@gmail.com>
Tue, 14 Apr 2020 13:52:51 +0000 (15:52 +0200)
icu4c/source/i18n/unitconverter.cpp
icu4c/source/test/depstest/dependencies.txt
icu4c/source/test/intltest/unitstest.cpp

index 9fef63848f20b6f6c718e69b7d493557547dec0f..5d8c82048a616f689099a7a9dbc935291de9d81d 100644 (file)
@@ -30,11 +30,12 @@ extractConversionInfo(StringPiece source,
 }
 
 /**
- * Extracts the compound base unit of a compound unit (`source`). For example, if the source unit is `square-mile-per-hour`, the compound base unit will be `square-meter-per-second`
+ * Extracts the compound base unit of a compound unit (`source`). For example, if the source unit is
+ * `square-mile-per-hour`, the compound base unit will be `square-meter-per-second`
  */
 MeasureUnit extractCompoundBaseUnit(const MeasureUnit &source,
-                            const MaybeStackVector<ConversionRateInfo> &conversionRateInfoList,
-                            UErrorCode &status) {
+                                    const MaybeStackVector<ConversionRateInfo> &conversionRateInfoList,
+                                    UErrorCode &status) {
     MeasureUnit result;
     int32_t count;
     const auto singleUnits = source.splitToSingleUnits(count, status);
@@ -55,11 +56,15 @@ MeasureUnit extractCompoundBaseUnit(const MeasureUnit &source,
 
         // Multiply the power of the singleUnit by the power of the baseUnit. For example, square-hectare
         // must be p4-meter. (NOTE: hectare --> square-meter)
-        auto baseUnit = MeasureUnit::forIdentifier(rateInfo->baseUnit.toStringPiece(), status);
-        auto singleBaseUnit = SingleUnitImpl::forMeasureUnit(baseUnit, status);
-        singleBaseUnit.dimensionality *= singleUnit.getDimensionality(status);
-
-        result = result.product(singleBaseUnit.build(status), status);
+        auto compoundBaseUnit = MeasureUnit::forIdentifier(rateInfo->baseUnit.toStringPiece(), status);
+        int32_t baseUnitsCounts;
+        const auto singleBaseUnits = compoundBaseUnit.splitToSingleUnits(baseUnitsCounts, status);
+        for (int j = 0; j < baseUnitsCounts; j++) {
+            auto singleBaseUnit = SingleUnitImpl::forMeasureUnit(singleBaseUnits[j], status);
+            singleBaseUnit.dimensionality *= singleUnit.getDimensionality(status);
+
+            result = result.product(singleBaseUnit.build(status), status);
+        }
     }
 
     return result;
@@ -67,9 +72,9 @@ MeasureUnit extractCompoundBaseUnit(const MeasureUnit &source,
 
 } // namespace
 
-UnitsConvertibilityState U_I18N_API
-checkConvertibility(const MeasureUnit &source, const MeasureUnit &target,
-                const MaybeStackVector<ConversionRateInfo> &conversionRateInfoList, UErrorCode &status) {
+UnitsConvertibilityState U_I18N_API checkConvertibility(
+    const MeasureUnit &source, const MeasureUnit &target,
+    const MaybeStackVector<ConversionRateInfo> &conversionRateInfoList, UErrorCode &status) {
     auto sourceBaseUnit = extractCompoundBaseUnit(source, conversionRateInfoList, status);
     auto targetBaseUnit = extractCompoundBaseUnit(target, conversionRateInfoList, status);
 
index 022b144d39f4fc3d300ec0f83f6c98eb6c69203f..e39ab5f8b4e5209d799c3f9d24c687b32b2f9689 100644 (file)
@@ -1063,7 +1063,7 @@ group: sharedbreakiterator
     breakiterator
 
 group: units_extra
-    measunit_extra.o
+    measunit_extra.o unitsdata.o unitconverter.o 
   deps
     units ucharstriebuilder ucharstrie uclean_i18n
 
index 990a4e662dc899e4069b58dd42546faf7f494d86..1175a405390c323e3123bc92fba7e7041b0b911b 100644 (file)
@@ -75,7 +75,6 @@ void UnitsTest::testConversionCapability() {
         {"kilometer-per-second", "foot-per-second", CONVERTIBLE},               //
         {"square-hectare", "p4-foot", CONVERTIBLE},                             //
         {"square-kilometer-per-second", "second-per-square-meter", RECIPROCAL}, //
-        // BEGIN: a copy of tests from unitsTest.txt
         {"g-force", "meter-per-square-second", CONVERTIBLE},                               //
         {"ohm", "kilogram-square-meter-square-ampere-per-cubic-second", CONVERTIBLE},      //
         {"electronvolt", "kilogram-square-meter-per-square-second", CONVERTIBLE},          //
@@ -116,7 +115,6 @@ void UnitsTest::testConversionCapability() {
         {"ofhg", "kilogram-square-second-per-square-meter", CONVERTIBLE},                  //
         {"knot", "meter-per-second", CONVERTIBLE},                                         //
         {"volt", "kilogram-square-meter-ampere-per-cubic-second", CONVERTIBLE},            //
-        // END: a copy of tests from unitsTest.txt
     };
 
     for (const auto &testCase : testCases) {