]> granicus.if.org Git - icu/commitdiff
fix small issue and add more test cases
authorYounies <younies.mahmoud@gmail.com>
Fri, 21 Feb 2020 00:42:29 +0000 (01:42 +0100)
committerYounies <younies.mahmoud@gmail.com>
Fri, 21 Feb 2020 00:42:29 +0000 (01:42 +0100)
icu4c/source/i18n/unitconverter.cpp
icu4c/source/test/intltest/unitstest.cpp

index 2b971da0e7275354b5b8f19d3c4469bbb024fcb9..e5e8a181c71dddf9afd4c36d2446d06efbbc830b 100644 (file)
@@ -234,13 +234,7 @@ void extractFactor(Factor &factor, StringPiece stringFactor, UErrorCode &status)
 // Load factor for a single source
 void loadSingleFactor(Factor &factor, StringPiece source, UErrorCode &status) {
     factor.source = source;
-    bool reciprocal = false;
 
-    // TODO(younies): illustrate this step.
-    if (source.substr(0, 7) == "one-per") {
-        reciprocal = true;
-        source = source.substr(8); // e.g. one-per-second --> second
-    }
     for (const auto &entry : temporarily::dataEntries) {
         if (entry.source == factor.source) {
             factor.target = entry.target;
@@ -248,10 +242,6 @@ void loadSingleFactor(Factor &factor, StringPiece source, UErrorCode &status) {
             factor.offset.setTo(entry.offset, status);
             factor.reciprocal = factor.reciprocal;
 
-            if (reciprocal) {
-                factor.flip(status);
-            }
-
             return;
         }
     }
@@ -269,10 +259,12 @@ void loadCompoundFactor(Factor &factor, StringPiece source, UErrorCode &status)
         auto singleUnit = TempSingleUnit::forMeasureUnit(singleUnits[i], status);
 
         loadSingleFactor(singleFactor, singleUnit.identifier, status);
-        singleFactor.power(singleUnit.dimensionality, status);
+
+        // You must apply SiPrefix before the power, because the power may be will flip the factor.
         singleFactor.applySiPrefix(singleUnit.siPrefix, status);
-        // TODO(younies): handle `one-per-second` case
 
+        singleFactor.power(singleUnit.dimensionality, status);
+        
         factor.multiplyBy(singleFactor, status);
     }
 }
index 997a1211e9b318c26c194536486c3c22a77d8611..21c1088a12a0479e27e3d89a767e40d4b11e2e4c 100644 (file)
@@ -27,6 +27,7 @@ class UnitsTest : public IntlTest {
     void testMass();
     void testTemperature();
     void testArea();
+    void testComplicatedUnits();
 
     // TODO(younies): fix this.
     void verifyTestCase(const UnitConversionTestCase &testCase);
@@ -44,6 +45,7 @@ void UnitsTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
     TESTCASE_AUTO(testMass);
     TESTCASE_AUTO(testTemperature);
     TESTCASE_AUTO(testArea);
+    TESTCASE_AUTO(testComplicatedUnits);
     TESTCASE_AUTO_END;
 }
 
@@ -179,4 +181,16 @@ void UnitsTest::testArea() {
     }
 }
 
+void UnitsTest::testComplicatedUnits() {
+        IcuTestErrorCode status(*this, "Units Area");
+
+    UnitConversionTestCase testCases[]{
+        {"meter-per-second", "meter-per-millisecond", 1000.0, 1.0} //
+    };
+
+    for (const auto &testCase : testCases) {
+        verifyTestCase(testCase);
+    }
+}
+
 #endif /* #if !UCONFIG_NO_FORMATTING */