From d8635a5d31f59e98cb69ab2e64459f52dfcfa86e Mon Sep 17 00:00:00 2001
From: Hugo van der Merwe <17109322+hugovdm@users.noreply.github.com>
Date: Sat, 28 Mar 2020 19:29:37 +0100
Subject: [PATCH] Move testGetConversionRateInfo to UnitsDataTest
---
icu4c/source/test/intltest/Makefile.in | 2 +-
icu4c/source/test/intltest/intltest.vcxproj | 1 +
.../test/intltest/intltest.vcxproj.filters | 3 +
icu4c/source/test/intltest/itformat.cpp | 10 ++
icu4c/source/test/intltest/unitsdatatest.cpp | 123 ++++++++++++++++++
icu4c/source/test/intltest/unitstest.cpp | 97 --------------
6 files changed, 138 insertions(+), 98 deletions(-)
create mode 100644 icu4c/source/test/intltest/unitsdatatest.cpp
diff --git a/icu4c/source/test/intltest/Makefile.in b/icu4c/source/test/intltest/Makefile.in
index 5878d6b19ca..cb356c44db9 100644
--- a/icu4c/source/test/intltest/Makefile.in
+++ b/icu4c/source/test/intltest/Makefile.in
@@ -69,7 +69,7 @@ string_segment_test.o \
numbertest_parse.o numbertest_doubleconversion.o numbertest_skeletons.o \
static_unisets_test.o numfmtdatadriventest.o numbertest_range.o erarulestest.o \
formattedvaluetest.o formatted_string_builder_test.o numbertest_permutation.o \
-unitstest.o unitsroutertest.o
+unitsdatatest.o unitstest.o unitsroutertest.o
DEPS = $(OBJECTS:.o=.d)
diff --git a/icu4c/source/test/intltest/intltest.vcxproj b/icu4c/source/test/intltest/intltest.vcxproj
index 78084b976c8..f659cda193c 100644
--- a/icu4c/source/test/intltest/intltest.vcxproj
+++ b/icu4c/source/test/intltest/intltest.vcxproj
@@ -301,6 +301,7 @@
+
diff --git a/icu4c/source/test/intltest/intltest.vcxproj.filters b/icu4c/source/test/intltest/intltest.vcxproj.filters
index adf9068a838..bba4e921982 100644
--- a/icu4c/source/test/intltest/intltest.vcxproj.filters
+++ b/icu4c/source/test/intltest/intltest.vcxproj.filters
@@ -550,6 +550,9 @@
locales & resources
+
+ formatting
+
formatting
diff --git a/icu4c/source/test/intltest/itformat.cpp b/icu4c/source/test/intltest/itformat.cpp
index 7ee86dbd9ab..a2b5ffec67a 100644
--- a/icu4c/source/test/intltest/itformat.cpp
+++ b/icu4c/source/test/intltest/itformat.cpp
@@ -74,6 +74,7 @@ extern IntlTest *createScientificNumberFormatterTest();
extern IntlTest *createFormattedValueTest();
extern IntlTest *createFormattedStringBuilderTest();
extern IntlTest *createStringSegmentTest();
+extern IntlTest *createUnitsDataTest();
extern IntlTest *createUnitsTest();
extern IntlTest *createUnitsRouterTest();
@@ -259,6 +260,15 @@ void IntlTestFormat::runIndexedTest( int32_t index, UBool exec, const char* &nam
}
break;
case 57:
+ name = "UnitsDataTest";
+ if (exec) {
+ logln("UnitsDataTest test---");
+ logln((UnicodeString)"");
+ LocalPointer test(createUnitsDataTest());
+ callTest(*test, par);
+ }
+ break;
+ case 58:
name = "UnitsRouterTest";
if (exec) {
logln("UnitsRouterTest test---");
diff --git a/icu4c/source/test/intltest/unitsdatatest.cpp b/icu4c/source/test/intltest/unitsdatatest.cpp
new file mode 100644
index 00000000000..225482d80c8
--- /dev/null
+++ b/icu4c/source/test/intltest/unitsdatatest.cpp
@@ -0,0 +1,123 @@
+// © 2020 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html#License
+
+#if !UCONFIG_NO_FORMATTING
+
+#include "intltest.h"
+#include "unitsdata.h"
+
+class UnitsDataTest : public IntlTest {
+ public:
+ UnitsDataTest() {}
+
+ void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = NULL);
+
+ void testGetConversionRateInfo();
+};
+
+extern IntlTest *createUnitsDataTest() { return new UnitsDataTest(); }
+
+void UnitsDataTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
+ if (exec) { logln("TestSuite UnitsDataTest: "); }
+ TESTCASE_AUTO_BEGIN;
+ TESTCASE_AUTO(testGetConversionRateInfo);
+ TESTCASE_AUTO_END;
+}
+
+void UnitsDataTest::testGetConversionRateInfo() {
+ const int MAX_NUM_RATES = 5;
+ struct {
+ // The source unit passed to getConversionRateInfo.
+ const char *sourceUnit;
+ // The target unit passed to getConversionRateInfo.
+ const char *targetUnit;
+ // Expected: units whose conversion rates are expected in the results.
+ const char *expectedOutputs[MAX_NUM_RATES];
+ // Expected "base unit", to serve as pivot between source and target.
+ const char *expectedBaseUnit;
+ } testCases[]{
+ {"centimeter-per-square-milligram",
+ "inch-per-square-ounce",
+ {"meter", "gram", "inch", "ounce", NULL},
+ "meter-per-square-kilogram"},
+
+ {"liter", "gallon", {"liter", "gallon", NULL, NULL, NULL}, "cubic-meter"},
+
+ // Sequence
+ {"stone-and-pound", "ton", {"pound", "stone", "ton", NULL, NULL}, "kilogram"},
+
+ {"mile-per-hour",
+ "dekameter-per-hour",
+ {"mile", "hour", "meter", NULL, NULL},
+ "meter-per-second"},
+
+ // Power: watt
+ {"watt",
+ "horsepower",
+ {"watt", "horsepower", NULL, NULL, NULL},
+ "kilogram-square-meter-per-cubic-second"},
+
+ // Energy: joule
+ {"therm-us",
+ "kilogram-square-meter-per-square-second",
+ {"therm-us", "kilogram", "meter", "second", NULL},
+ "kilogram-square-meter-per-square-second"},
+
+ // WIP/FIXME(hugovdm): I think I found a bug in targetBaseUnit.product():
+ // Target Base: x =>
+ //
+ // // Joule-per-meter
+ // {"therm-us-per-meter",
+ // "joule-per-meter",
+ // {"therm-us", "joule", "meter", NULL, NULL},
+ // "kilogram-meter-per-square-second"},
+
+ // TODO: include capacitance test case with base unit:
+ // pow4-second-square-ampere-per-kilogram-square-meter;
+ };
+ for (const auto &t : testCases) {
+ logln("---testing: source=\"%s\", target=\"%s\", expectedBaseUnit=\"%s\"", t.sourceUnit,
+ t.targetUnit, t.expectedBaseUnit);
+ IcuTestErrorCode status(*this, "testGetConversionRateInfo");
+
+ MeasureUnit baseCompoundUnit;
+ MeasureUnit sourceUnit = MeasureUnit::forIdentifier(t.sourceUnit, status);
+ MeasureUnit targetUnit = MeasureUnit::forIdentifier(t.targetUnit, status);
+ MaybeStackVector conversionInfo =
+ getConversionRatesInfo(sourceUnit, targetUnit, &baseCompoundUnit, status);
+ if (status.errIfFailureAndReset("getConversionRatesInfo(<%s>, <%s>, ...)",
+ sourceUnit.getIdentifier(), targetUnit.getIdentifier())) {
+ continue;
+ }
+
+ assertEquals("baseCompoundUnit returned by getConversionRatesInfo", t.expectedBaseUnit,
+ baseCompoundUnit.getIdentifier());
+ int countExpected;
+ for (countExpected = 0; countExpected < MAX_NUM_RATES; countExpected++) {
+ auto expected = t.expectedOutputs[countExpected];
+ if (expected == NULL) break;
+ // Check if this conversion rate was expected
+ bool found = false;
+ for (int i = 0; i < conversionInfo.length(); i++) {
+ auto cri = conversionInfo[i];
+ if (strcmp(expected, cri->sourceUnit.data()) == 0) {
+ found = true;
+ break;
+ }
+ }
+ assertTrue(UnicodeString("<") + expected + "> expected", found);
+ }
+ assertEquals("number of conversion rates", countExpected, conversionInfo.length());
+
+ // Convenience output for debugging
+ for (int i = 0; i < conversionInfo.length(); i++) {
+ ConversionRateInfo *cri = conversionInfo[i];
+ logln("* conversionInfo %d: source=\"%s\", baseUnit=\"%s\", factor=\"%s\", "
+ "offset=\"%s\"",
+ i, cri->sourceUnit.data(), cri->baseUnit.data(), cri->factor.data(),
+ cri->offset.data());
+ }
+ }
+}
+
+#endif /* #if !UCONFIG_NO_FORMATTING */
diff --git a/icu4c/source/test/intltest/unitstest.cpp b/icu4c/source/test/intltest/unitstest.cpp
index a130e38dc0c..9675cbbfc42 100644
--- a/icu4c/source/test/intltest/unitstest.cpp
+++ b/icu4c/source/test/intltest/unitstest.cpp
@@ -59,7 +59,6 @@ void UnitsTest::runIndexedTest(int32_t index, UBool exec, const char *&name, cha
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(testConversions);
TESTCASE_AUTO(testPreferences);
- TESTCASE_AUTO(testGetConversionRateInfo);
TESTCASE_AUTO(testGetUnitsData);
TESTCASE_AUTO(testBasic);
@@ -580,102 +579,6 @@ void UnitsTest::testPreferences() {
}
}
-void UnitsTest::testGetConversionRateInfo() {
- const int MAX_NUM_RATES = 5;
- struct {
- // The source unit passed to getConversionRateInfo.
- const char *sourceUnit;
- // The target unit passed to getConversionRateInfo.
- const char *targetUnit;
- // Expected: units whose conversion rates are expected in the results.
- const char *expectedOutputs[MAX_NUM_RATES];
- // Expected "base unit", to serve as pivot between source and target.
- const char *expectedBaseUnit;
- } testCases[]{
- {"centimeter-per-square-milligram",
- "inch-per-square-ounce",
- {"meter", "gram", "inch", "ounce", NULL},
- "meter-per-square-kilogram"},
-
- {"liter", "gallon", {"liter", "gallon", NULL, NULL, NULL}, "cubic-meter"},
-
- // Sequence
- {"stone-and-pound", "ton", {"pound", "stone", "ton", NULL, NULL}, "kilogram"},
-
- {"mile-per-hour",
- "dekameter-per-hour",
- {"mile", "hour", "meter", NULL, NULL},
- "meter-per-second"},
-
- // Power: watt
- {"watt",
- "horsepower",
- {"watt", "horsepower", NULL, NULL, NULL},
- "kilogram-square-meter-per-cubic-second"},
-
- // Energy: joule
- {"therm-us",
- "kilogram-square-meter-per-square-second",
- {"therm-us", "kilogram", "meter", "second", NULL},
- "kilogram-square-meter-per-square-second"},
-
- // WIP/FIXME(hugovdm): I think I found a bug in targetBaseUnit.product():
- // Target Base: x =>
- //
- // // Joule-per-meter
- // {"therm-us-per-meter",
- // "joule-per-meter",
- // {"therm-us", "joule", "meter", NULL, NULL},
- // "kilogram-meter-per-square-second"},
-
- // TODO: include capacitance test case with base unit:
- // pow4-second-square-ampere-per-kilogram-square-meter;
- };
- for (const auto &t : testCases) {
- logln("---testing: source=\"%s\", target=\"%s\", expectedBaseUnit=\"%s\"", t.sourceUnit,
- t.targetUnit, t.expectedBaseUnit);
- IcuTestErrorCode status(*this, "testGetConversionRateInfo");
-
- MeasureUnit baseCompoundUnit;
- MeasureUnit sourceUnit = MeasureUnit::forIdentifier(t.sourceUnit, status);
- MeasureUnit targetUnit = MeasureUnit::forIdentifier(t.targetUnit, status);
- MaybeStackVector conversionInfo =
- getConversionRatesInfo(sourceUnit, targetUnit, &baseCompoundUnit, status);
- if (status.errIfFailureAndReset("getConversionRatesInfo(<%s>, <%s>, ...)",
- sourceUnit.getIdentifier(), targetUnit.getIdentifier())) {
- continue;
- }
-
- assertEquals("baseCompoundUnit returned by getConversionRatesInfo", t.expectedBaseUnit,
- baseCompoundUnit.getIdentifier());
- int countExpected;
- for (countExpected = 0; countExpected < MAX_NUM_RATES; countExpected++) {
- auto expected = t.expectedOutputs[countExpected];
- if (expected == NULL) break;
- // Check if this conversion rate was expected
- bool found = false;
- for (int i = 0; i < conversionInfo.length(); i++) {
- auto cri = conversionInfo[i];
- if (strcmp(expected, cri->sourceUnit.data()) == 0) {
- found = true;
- break;
- }
- }
- assertTrue(UnicodeString("<") + expected + "> expected", found);
- }
- assertEquals("number of conversion rates", countExpected, conversionInfo.length());
-
- // Convenience output for debugging
- for (int i = 0; i < conversionInfo.length(); i++) {
- ConversionRateInfo *cri = conversionInfo[i];
- logln("* conversionInfo %d: source=\"%s\", baseUnit=\"%s\", factor=\"%s\", "
- "offset=\"%s\"",
- i, cri->sourceUnit.data(), cri->baseUnit.data(), cri->factor.data(),
- cri->offset.data());
- }
- }
-}
-
// We test "successfully loading some data", not specific output values, since
// this would duplicate some of the input data. We leave end-to-end testing to
// take care of that. Running `intltest` with `-v` will print out the loaded
--
2.40.0