• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2020 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html#License
3 
4 #include "unicode/utypes.h"
5 
6 #if !UCONFIG_NO_FORMATTING
7 
8 #include <cmath>
9 #include <iostream>
10 
11 #include "charstr.h"
12 #include "cmemory.h"
13 #include "filestrm.h"
14 #include "intltest.h"
15 #include "number_decimalquantity.h"
16 #include "putilimp.h"
17 #include "unicode/ctest.h"
18 #include "unicode/measunit.h"
19 #include "unicode/measure.h"
20 #include "unicode/unistr.h"
21 #include "unicode/unum.h"
22 #include "unicode/ures.h"
23 #include "units_complexconverter.h"
24 #include "units_converter.h"
25 #include "units_data.h"
26 #include "units_router.h"
27 #include "uparse.h"
28 #include "uresimp.h"
29 
30 struct UnitConversionTestCase {
31     const StringPiece source;
32     const StringPiece target;
33     const double inputValue;
34     const double expectedValue;
35 };
36 
37 using ::icu::number::impl::DecimalQuantity;
38 using namespace ::icu::units;
39 
40 class UnitsTest : public IntlTest {
41   public:
UnitsTest()42     UnitsTest() {}
43 
44     void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = NULL);
45 
46     void testUnitConstantFreshness();
47     void testExtractConvertibility();
48     void testConversionInfo();
49     void testConverterWithCLDRTests();
50     void testComplexUnitsConverter();
51     void testComplexUnitsConverterSorting();
52     void testUnitPreferencesWithCLDRTests();
53     void testConverter();
54 };
55 
createUnitsTest()56 extern IntlTest *createUnitsTest() { return new UnitsTest(); }
57 
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)58 void UnitsTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
59     if (exec) {
60         logln("TestSuite UnitsTest: ");
61     }
62     TESTCASE_AUTO_BEGIN;
63     TESTCASE_AUTO(testUnitConstantFreshness);
64     TESTCASE_AUTO(testExtractConvertibility);
65     TESTCASE_AUTO(testConversionInfo);
66     TESTCASE_AUTO(testConverterWithCLDRTests);
67     TESTCASE_AUTO(testComplexUnitsConverter);
68     TESTCASE_AUTO(testComplexUnitsConverterSorting);
69     TESTCASE_AUTO(testUnitPreferencesWithCLDRTests);
70     TESTCASE_AUTO(testConverter);
71     TESTCASE_AUTO_END;
72 }
73 
74 // Tests the hard-coded constants in the code against constants that appear in
75 // units.txt.
testUnitConstantFreshness()76 void UnitsTest::testUnitConstantFreshness() {
77     IcuTestErrorCode status(*this, "testUnitConstantFreshness");
78     LocalUResourceBundlePointer unitsBundle(ures_openDirect(NULL, "units", status));
79     LocalUResourceBundlePointer unitConstants(
80         ures_getByKey(unitsBundle.getAlias(), "unitConstants", NULL, status));
81 
82     while (ures_hasNext(unitConstants.getAlias())) {
83         int32_t len;
84         const char *constant = NULL;
85         ures_getNextString(unitConstants.getAlias(), &len, &constant, status);
86 
87         Factor factor;
88         addSingleFactorConstant(constant, 1, POSITIVE, factor, status);
89         if (status.errDataIfFailureAndReset(
90                 "addSingleFactorConstant(<%s>, ...).\n\n"
91                 "If U_INVALID_FORMAT_ERROR, please check that \"icu4c/source/i18n/units_converter.cpp\" "
92                 "has all constants? Is \"%s\" a new constant?\n"
93                 "See docs/processes/release/tasks/updating-measure-unit.md for more information.\n",
94                 constant, constant)) {
95             continue;
96         }
97 
98         // Check the values of constants that have a simple numeric value
99         factor.substituteConstants();
100         int32_t uLen;
101         UnicodeString uVal = ures_getStringByKey(unitConstants.getAlias(), constant, &uLen, status);
102         CharString val;
103         val.appendInvariantChars(uVal, status);
104         if (status.errDataIfFailureAndReset("Failed to get constant value for %s.", constant)) {
105             continue;
106         }
107         DecimalQuantity dqVal;
108         UErrorCode parseStatus = U_ZERO_ERROR;
109         // TODO(units): unify with strToDouble() in units_converter.cpp
110         dqVal.setToDecNumber(val.toStringPiece(), parseStatus);
111         if (!U_SUCCESS(parseStatus)) {
112             // Not simple to parse, skip validating this constant's value. (We
113             // leave catching mistakes to the data-driven integration tests.)
114             continue;
115         }
116         double expectedNumerator = dqVal.toDouble();
117         assertEquals(UnicodeString("Constant ") + constant + u" numerator", expectedNumerator,
118                      factor.factorNum);
119         assertEquals(UnicodeString("Constant ") + constant + u" denominator", 1.0, factor.factorDen);
120     }
121 }
122 
testExtractConvertibility()123 void UnitsTest::testExtractConvertibility() {
124     IcuTestErrorCode status(*this, "UnitsTest::testExtractConvertibility");
125 
126     struct TestCase {
127         const char *const source;
128         const char *const target;
129         const Convertibility expectedState;
130     } testCases[]{
131         {"meter", "foot", CONVERTIBLE},                                              //
132         {"kilometer", "foot", CONVERTIBLE},                                          //
133         {"hectare", "square-foot", CONVERTIBLE},                                     //
134         {"kilometer-per-second", "second-per-meter", RECIPROCAL},                    //
135         {"square-meter", "square-foot", CONVERTIBLE},                                //
136         {"kilometer-per-second", "foot-per-second", CONVERTIBLE},                    //
137         {"square-hectare", "pow4-foot", CONVERTIBLE},                                //
138         {"square-kilometer-per-second", "second-per-square-meter", RECIPROCAL},      //
139         {"cubic-kilometer-per-second-meter", "second-per-square-meter", RECIPROCAL}, //
140         {"square-meter-per-square-hour", "hectare-per-square-second", CONVERTIBLE},  //
141         {"hertz", "revolution-per-second", CONVERTIBLE},                             //
142         {"millimeter", "meter", CONVERTIBLE},                                        //
143         {"yard", "meter", CONVERTIBLE},                                              //
144         {"ounce-troy", "kilogram", CONVERTIBLE},                                     //
145         {"percent", "portion", CONVERTIBLE},                                         //
146         {"ofhg", "kilogram-per-square-meter-square-second", CONVERTIBLE},            //
147         {"second-per-meter", "meter-per-second", RECIPROCAL},                        //
148     };
149 
150     for (const auto &testCase : testCases) {
151         MeasureUnitImpl source = MeasureUnitImpl::forIdentifier(testCase.source, status);
152         if (status.errIfFailureAndReset("source MeasureUnitImpl::forIdentifier(\"%s\", ...)",
153                                         testCase.source)) {
154             continue;
155         }
156         MeasureUnitImpl target = MeasureUnitImpl::forIdentifier(testCase.target, status);
157         if (status.errIfFailureAndReset("target MeasureUnitImpl::forIdentifier(\"%s\", ...)",
158                                         testCase.target)) {
159             continue;
160         }
161 
162         ConversionRates conversionRates(status);
163         if (status.errIfFailureAndReset("conversionRates(status)")) {
164             continue;
165         }
166         auto convertibility = extractConvertibility(source, target, conversionRates, status);
167         if (status.errIfFailureAndReset("extractConvertibility(<%s>, <%s>, ...)", testCase.source,
168                                         testCase.target)) {
169             continue;
170         }
171 
172         assertEquals(UnicodeString("Conversion Capability: ") + testCase.source + " to " +
173                          testCase.target,
174                      testCase.expectedState, convertibility);
175     }
176 }
177 
testConversionInfo()178 void UnitsTest::testConversionInfo() {
179     IcuTestErrorCode status(*this, "UnitsTest::testExtractConvertibility");
180     // Test Cases
181     struct TestCase {
182         const char *source;
183         const char *target;
184         const ConversionInfo expectedConversionInfo;
185     } testCases[]{
186         {
187             "meter",
188             "meter",
189             {1.0, 0, false},
190         },
191         {
192             "meter",
193             "foot",
194             {3.28084, 0, false},
195         },
196         {
197             "foot",
198             "meter",
199             {0.3048, 0, false},
200         },
201         {
202             "celsius",
203             "kelvin",
204             {1, 273.15, false},
205         },
206         {
207             "fahrenheit",
208             "kelvin",
209             {5.0 / 9.0, 255.372, false},
210         },
211         {
212             "fahrenheit",
213             "celsius",
214             {5.0 / 9.0, -17.7777777778, false},
215         },
216         {
217             "celsius",
218             "fahrenheit",
219             {9.0 / 5.0, 32, false},
220         },
221         {
222             "fahrenheit",
223             "fahrenheit",
224             {1.0, 0, false},
225         },
226         {
227             "mile-per-gallon",
228             "liter-per-100-kilometer",
229             {0.00425143707, 0, true},
230         },
231     };
232 
233     ConversionRates rates(status);
234     for (const auto &testCase : testCases) {
235         auto sourceImpl = MeasureUnitImpl::forIdentifier(testCase.source, status);
236         auto targetImpl = MeasureUnitImpl::forIdentifier(testCase.target, status);
237         UnitsConverter unitsConverter(sourceImpl, targetImpl, rates, status);
238 
239         if (status.errIfFailureAndReset()) {
240             continue;
241         }
242 
243         ConversionInfo actualConversionInfo = unitsConverter.getConversionInfo();
244         UnicodeString message =
245             UnicodeString("testConverter: ") + testCase.source + " to " + testCase.target;
246 
247         double maxDelta = 1e-6 * uprv_fabs(testCase.expectedConversionInfo.conversionRate);
248         if (testCase.expectedConversionInfo.conversionRate == 0) {
249             maxDelta = 1e-12;
250         }
251         assertEqualsNear(message + ", conversion rate: ", testCase.expectedConversionInfo.conversionRate,
252                          actualConversionInfo.conversionRate, maxDelta);
253 
254         maxDelta = 1e-6 * uprv_fabs(testCase.expectedConversionInfo.offset);
255         if (testCase.expectedConversionInfo.offset == 0) {
256             maxDelta = 1e-12;
257         }
258         assertEqualsNear(message + ", offset: ", testCase.expectedConversionInfo.offset, actualConversionInfo.offset,
259                          maxDelta);
260 
261         assertEquals(message + ", reciprocal: ", testCase.expectedConversionInfo.reciprocal,
262                      actualConversionInfo.reciprocal);
263     }
264 }
265 
testConverter()266 void UnitsTest::testConverter() {
267     IcuTestErrorCode status(*this, "UnitsTest::testConverter");
268 
269     // Test Cases
270     struct TestCase {
271         const char *source;
272         const char *target;
273         const double inputValue;
274         const double expectedValue;
275     } testCases[]{
276         // SI Prefixes
277         {"gram", "kilogram", 1.0, 0.001},
278         {"milligram", "kilogram", 1.0, 0.000001},
279         {"microgram", "kilogram", 1.0, 0.000000001},
280         {"megagram", "gram", 1.0, 1000000},
281         {"megagram", "kilogram", 1.0, 1000},
282         {"gigabyte", "byte", 1.0, 1000000000},
283         {"megawatt", "watt", 1.0, 1000000},
284         {"megawatt", "kilowatt", 1.0, 1000},
285         // Binary Prefixes
286         {"kilobyte", "byte", 1, 1000},
287         {"kibibyte", "byte", 1, 1024},
288         {"mebibyte", "byte", 1, 1048576},
289         {"gibibyte", "kibibyte", 1, 1048576},
290         {"pebibyte", "tebibyte", 4, 4096},
291         {"zebibyte", "pebibyte", 1.0 / 16, 65536.0},
292         {"yobibyte", "exbibyte", 1, 1048576},
293         // Mass
294         {"gram", "kilogram", 1.0, 0.001},
295         {"pound", "kilogram", 1.0, 0.453592},
296         {"pound", "kilogram", 2.0, 0.907185},
297         {"ounce", "pound", 16.0, 1.0},
298         {"ounce", "kilogram", 16.0, 0.453592},
299         {"ton", "pound", 1.0, 2000},
300         {"stone", "pound", 1.0, 14},
301         {"stone", "kilogram", 1.0, 6.35029},
302         // Temperature
303         {"celsius", "fahrenheit", 0.0, 32.0},
304         {"celsius", "fahrenheit", 10.0, 50.0},
305         {"celsius", "fahrenheit", 1000, 1832},
306         {"fahrenheit", "celsius", 32.0, 0.0},
307         {"fahrenheit", "celsius", 89.6, 32},
308         {"fahrenheit", "fahrenheit", 1000, 1000},
309         {"kelvin", "fahrenheit", 0.0, -459.67},
310         {"kelvin", "fahrenheit", 300, 80.33},
311         {"kelvin", "celsius", 0.0, -273.15},
312         {"kelvin", "celsius", 300.0, 26.85},
313         // Area
314         {"square-meter", "square-yard", 10.0, 11.9599},
315         {"hectare", "square-yard", 1.0, 11959.9},
316         {"square-mile", "square-foot", 0.0001, 2787.84},
317         {"hectare", "square-yard", 1.0, 11959.9},
318         {"hectare", "square-meter", 1.0, 10000},
319         {"hectare", "square-meter", 0.0, 0.0},
320         {"square-mile", "square-foot", 0.0001, 2787.84},
321         {"square-yard", "square-foot", 10, 90},
322         {"square-yard", "square-foot", 0, 0},
323         {"square-yard", "square-foot", 0.000001, 0.000009},
324         {"square-mile", "square-foot", 0.0, 0.0},
325         // Fuel Consumption
326         {"cubic-meter-per-meter", "mile-per-gallon", 2.1383143939394E-6, 1.1},
327         {"cubic-meter-per-meter", "mile-per-gallon", 2.6134953703704E-6, 0.9},
328     };
329 
330     for (const auto &testCase : testCases) {
331         MeasureUnitImpl source = MeasureUnitImpl::forIdentifier(testCase.source, status);
332         if (status.errIfFailureAndReset("source MeasureUnitImpl::forIdentifier(\"%s\", ...)",
333                                         testCase.source)) {
334             continue;
335         }
336         MeasureUnitImpl target = MeasureUnitImpl::forIdentifier(testCase.target, status);
337         if (status.errIfFailureAndReset("target MeasureUnitImpl::forIdentifier(\"%s\", ...)",
338                                         testCase.target)) {
339             continue;
340         }
341 
342         ConversionRates conversionRates(status);
343         if (status.errIfFailureAndReset("conversionRates(status)")) {
344             continue;
345         }
346         UnitsConverter converter(source, target, conversionRates, status);
347         if (status.errIfFailureAndReset("UnitsConverter(<%s>, <%s>, ...)", testCase.source,
348                                         testCase.target)) {
349             continue;
350         }
351 
352         double maxDelta = 1e-6 * uprv_fabs(testCase.expectedValue);
353         if (testCase.expectedValue == 0) {
354             maxDelta = 1e-12;
355         }
356         assertEqualsNear(UnicodeString("testConverter: ") + testCase.source + " to " + testCase.target,
357                          testCase.expectedValue, converter.convert(testCase.inputValue), maxDelta);
358 
359         maxDelta = 1e-6 * uprv_fabs(testCase.inputValue);
360         if (testCase.inputValue == 0) {
361             maxDelta = 1e-12;
362         }
363         assertEqualsNear(
364             UnicodeString("testConverter inverse: ") + testCase.target + " back to " + testCase.source,
365             testCase.inputValue, converter.convertInverse(testCase.expectedValue), maxDelta);
366 
367 
368         // TODO: Test UnitsConverter created using CLDR separately.
369         // Test UnitsConverter created by CLDR unit identifiers
370         UnitsConverter converter2(testCase.source, testCase.target, status);
371         if (status.errIfFailureAndReset("UnitsConverter(<%s>, <%s>, ...)", testCase.source,
372                                         testCase.target)) {
373             continue;
374         }
375 
376         maxDelta = 1e-6 * uprv_fabs(testCase.expectedValue);
377         if (testCase.expectedValue == 0) {
378             maxDelta = 1e-12;
379         }
380         assertEqualsNear(UnicodeString("testConverter2: ") + testCase.source + " to " + testCase.target,
381                          testCase.expectedValue, converter2.convert(testCase.inputValue), maxDelta);
382 
383         maxDelta = 1e-6 * uprv_fabs(testCase.inputValue);
384         if (testCase.inputValue == 0) {
385             maxDelta = 1e-12;
386         }
387         assertEqualsNear(
388             UnicodeString("testConverter2 inverse: ") + testCase.target + " back to " + testCase.source,
389             testCase.inputValue, converter2.convertInverse(testCase.expectedValue), maxDelta);
390     }
391 }
392 
393 /**
394  * Trims whitespace off of the specified string.
395  * @param field is two pointers pointing at the start and end of the string.
396  * @return A StringPiece with initial and final space characters trimmed off.
397  */
trimField(char * (& field)[2])398 StringPiece trimField(char *(&field)[2]) {
399     const char *start = field[0];
400     start = u_skipWhitespace(start);
401     if (start >= field[1]) {
402         start = field[1];
403     }
404     const char *end = field[1];
405     while ((start < end) && U_IS_INV_WHITESPACE(*(end - 1))) {
406         end--;
407     }
408     int32_t length = (int32_t)(end - start);
409     return StringPiece(start, length);
410 }
411 
412 // Used for passing context to unitsTestDataLineFn via u_parseDelimitedFile.
413 struct UnitsTestContext {
414     // Provides access to UnitsTest methods like logln.
415     UnitsTest *unitsTest;
416     // Conversion rates: does not take ownership.
417     ConversionRates *conversionRates;
418 };
419 
420 /**
421  * Deals with a single data-driven unit test for unit conversions.
422  *
423  * This is a UParseLineFn as required by u_parseDelimitedFile, intended for
424  * parsing unitsTest.txt.
425  *
426  * @param context Must point at a UnitsTestContext struct.
427  * @param fields A list of pointer-pairs, each pair pointing at the start and
428  * end of each field. End pointers are important because these are *not*
429  * null-terminated strings. (Interpreted as a null-terminated string,
430  * fields[0][0] points at the whole line.)
431  * @param fieldCount The number of fields (pointer pairs) passed to the fields
432  * parameter.
433  * @param pErrorCode Receives status.
434  */
unitsTestDataLineFn(void * context,char * fields[][2],int32_t fieldCount,UErrorCode * pErrorCode)435 void unitsTestDataLineFn(void *context, char *fields[][2], int32_t fieldCount, UErrorCode *pErrorCode) {
436     if (U_FAILURE(*pErrorCode)) {
437         return;
438     }
439     UnitsTestContext *ctx = (UnitsTestContext *)context;
440     UnitsTest *unitsTest = ctx->unitsTest;
441     (void)fieldCount; // unused UParseLineFn variable
442     IcuTestErrorCode status(*unitsTest, "unitsTestDatalineFn");
443 
444     StringPiece quantity = trimField(fields[0]);
445     StringPiece x = trimField(fields[1]);
446     StringPiece y = trimField(fields[2]);
447     StringPiece commentConversionFormula = trimField(fields[3]);
448     StringPiece utf8Expected = trimField(fields[4]);
449 
450     UNumberFormat *nf = unum_open(UNUM_DEFAULT, NULL, -1, "en_US", NULL, status);
451     if (status.errIfFailureAndReset("unum_open failed")) {
452         return;
453     }
454     UnicodeString uExpected = UnicodeString::fromUTF8(utf8Expected);
455     double expected = unum_parseDouble(nf, uExpected.getBuffer(), uExpected.length(), 0, status);
456     unum_close(nf);
457     if (status.errIfFailureAndReset("unum_parseDouble(\"%s\") failed", utf8Expected)) {
458         return;
459     }
460 
461     CharString sourceIdent(x, status);
462     MeasureUnitImpl sourceUnit = MeasureUnitImpl::forIdentifier(x, status);
463     if (status.errIfFailureAndReset("forIdentifier(\"%.*s\")", x.length(), x.data())) {
464         return;
465     }
466 
467     CharString targetIdent(y, status);
468     MeasureUnitImpl targetUnit = MeasureUnitImpl::forIdentifier(y, status);
469     if (status.errIfFailureAndReset("forIdentifier(\"%.*s\")", y.length(), y.data())) {
470         return;
471     }
472 
473     unitsTest->logln("Quantity (Category): \"%.*s\", "
474                      "Expected value of \"1000 %.*s in %.*s\": %f, "
475                      "commentConversionFormula: \"%.*s\", ",
476                      quantity.length(), quantity.data(), x.length(), x.data(), y.length(), y.data(),
477                      expected, commentConversionFormula.length(), commentConversionFormula.data());
478 
479     // Convertibility:
480     auto convertibility = extractConvertibility(sourceUnit, targetUnit, *ctx->conversionRates, status);
481     if (status.errIfFailureAndReset("extractConvertibility(<%s>, <%s>, ...)",
482                                     sourceIdent.data(), targetIdent.data())) {
483         return;
484     }
485     CharString msg;
486     msg.append("convertible: ", status)
487         .append(sourceIdent.data(), status)
488         .append(" -> ", status)
489         .append(targetIdent.data(), status);
490     if (status.errIfFailureAndReset("msg construction")) {
491         return;
492     }
493     unitsTest->assertNotEquals(msg.data(), UNCONVERTIBLE, convertibility);
494 
495     // Conversion:
496     UnitsConverter converter(sourceUnit, targetUnit, *ctx->conversionRates, status);
497     if (status.errIfFailureAndReset("UnitsConverter(<%s>, <%s>, ...)", sourceIdent.data(),
498                                     targetIdent.data())) {
499         return;
500     }
501     double got = converter.convert(1000);
502     msg.clear();
503     msg.append("Converting 1000 ", status).append(x, status).append(" to ", status).append(y, status);
504     unitsTest->assertEqualsNear(msg.data(), expected, got, 0.0001 * expected);
505     double inverted = converter.convertInverse(got);
506     msg.clear();
507     msg.append("Converting back to ", status).append(x, status).append(" from ", status).append(y, status);
508     unitsTest->assertEqualsNear(msg.data(), 1000, inverted, 0.0001);
509 }
510 
511 /**
512  * Runs data-driven unit tests for unit conversion. It looks for the test cases
513  * in source/test/testdata/cldr/units/unitsTest.txt, which originates in CLDR.
514  */
testConverterWithCLDRTests()515 void UnitsTest::testConverterWithCLDRTests() {
516     const char *filename = "unitsTest.txt";
517     const int32_t kNumFields = 5;
518     char *fields[kNumFields][2];
519 
520     IcuTestErrorCode errorCode(*this, "UnitsTest::testConverterWithCLDRTests");
521     const char *sourceTestDataPath = getSourceTestData(errorCode);
522     if (errorCode.errIfFailureAndReset("unable to find the source/test/testdata "
523                                        "folder (getSourceTestData())")) {
524         return;
525     }
526 
527     CharString path(sourceTestDataPath, errorCode);
528     path.appendPathPart("cldr/units", errorCode);
529     path.appendPathPart(filename, errorCode);
530 
531     ConversionRates rates(errorCode);
532     UnitsTestContext ctx = {this, &rates};
533     u_parseDelimitedFile(path.data(), ';', fields, kNumFields, unitsTestDataLineFn, &ctx, errorCode);
534     if (errorCode.errIfFailureAndReset("error parsing %s: %s\n", path.data(), u_errorName(errorCode))) {
535         return;
536     }
537 }
538 
testComplexUnitsConverter()539 void UnitsTest::testComplexUnitsConverter() {
540     IcuTestErrorCode status(*this, "UnitsTest::testComplexUnitsConverter");
541 
542     // DBL_EPSILON is approximately 2.22E-16, and is the precision of double for
543     // values in the range [1.0, 2.0), but half the precision of double for
544     // [2.0, 4.0).
545     U_ASSERT(1.0 + DBL_EPSILON > 1.0);
546     U_ASSERT(2.0 - DBL_EPSILON < 2.0);
547     U_ASSERT(2.0 + DBL_EPSILON == 2.0);
548 
549     struct TestCase {
550         const char* msg;
551         const char* input;
552         const char* output;
553         double value;
554         Measure expected[2];
555         int32_t expectedCount;
556         // For mixed units, accuracy of the smallest unit
557         double accuracy;
558     } testCases[]{
559         // Significantly less than 2.0.
560         {"1.9999",
561          "foot",
562          "foot-and-inch",
563          1.9999,
564          {Measure(1, MeasureUnit::createFoot(status), status),
565           Measure(11.9988, MeasureUnit::createInch(status), status)},
566          2,
567          0},
568 
569         // A minimal nudge under 2.0, rounding up to 2.0 ft, 0 in.
570         {"2-eps",
571          "foot",
572          "foot-and-inch",
573          2.0 - DBL_EPSILON,
574          {Measure(2, MeasureUnit::createFoot(status), status),
575           Measure(0, MeasureUnit::createInch(status), status)},
576          2,
577          0},
578 
579         // A slightly bigger nudge under 2.0, *not* rounding up to 2.0 ft!
580         {"2-3eps",
581          "foot",
582          "foot-and-inch",
583          2.0 - 3 * DBL_EPSILON,
584          {Measure(1, MeasureUnit::createFoot(status), status),
585           // We expect 12*3*DBL_EPSILON inches (7.92e-15) less than 12.
586           Measure(12 - 36 * DBL_EPSILON, MeasureUnit::createInch(status), status)},
587          2,
588          // Might accuracy be lacking with some compilers or on some systems? In
589          // case it is somehow lacking, we'll allow a delta of 12 * DBL_EPSILON.
590          12 * DBL_EPSILON},
591 
592         // Testing precision with meter and light-year.
593         //
594         // DBL_EPSILON light-years, ~2.22E-16 light-years, is ~2.1 meters
595         // (maximum precision when exponent is 0).
596         //
597         // 1e-16 light years is 0.946073 meters.
598 
599         // A 2.1 meter nudge under 2.0 light years, rounding up to 2.0 ly, 0 m.
600         {"2-eps",
601          "light-year",
602          "light-year-and-meter",
603          2.0 - DBL_EPSILON,
604          {Measure(2, MeasureUnit::createLightYear(status), status),
605           Measure(0, MeasureUnit::createMeter(status), status)},
606          2,
607          0},
608 
609         // A 2.1 meter nudge under 1.0 light years, rounding up to 1.0 ly, 0 m.
610         {"1-eps",
611          "light-year",
612          "light-year-and-meter",
613          1.0 - DBL_EPSILON,
614          {Measure(1, MeasureUnit::createLightYear(status), status),
615           Measure(0, MeasureUnit::createMeter(status), status)},
616          2,
617          0},
618 
619         // 1e-15 light years is 9.46073 meters (calculated using "bc" and the
620         // CLDR conversion factor). With double-precision maths in C++, we get
621         // 10.5. In this case, we're off by a bit more than 1 meter. With Java
622         // BigDecimal, we get accurate results.
623         {"1 + 1e-15",
624          "light-year",
625          "light-year-and-meter",
626          1.0 + 1e-15,
627          {Measure(1, MeasureUnit::createLightYear(status), status),
628           Measure(9.46073, MeasureUnit::createMeter(status), status)},
629          2,
630          1.5 /* meters, precision */},
631 
632         // 2.1 meters more than 1 light year is not rounded away.
633         {"1 + eps",
634          "light-year",
635          "light-year-and-meter",
636          1.0 + DBL_EPSILON,
637          {Measure(1, MeasureUnit::createLightYear(status), status),
638           Measure(2.1, MeasureUnit::createMeter(status), status)},
639          2,
640          0.001},
641     };
642     status.assertSuccess();
643 
644     ConversionRates rates(status);
645     MeasureUnit input, output;
646     MeasureUnitImpl tempInput, tempOutput;
647     MaybeStackVector<Measure> measures;
648     auto testATestCase = [&](const ComplexUnitsConverter& converter ,StringPiece initMsg , const TestCase &testCase) {
649         measures = converter.convert(testCase.value, nullptr, status);
650 
651         CharString msg(initMsg, status);
652         msg.append(testCase.msg, status);
653         msg.append(" ", status);
654         msg.append(testCase.input, status);
655         msg.append(" -> ", status);
656         msg.append(testCase.output, status);
657 
658         CharString msgCount(msg, status);
659         msgCount.append(", measures.length()", status);
660         assertEquals(msgCount.data(), testCase.expectedCount, measures.length());
661         for (int i = 0; i < measures.length() && i < testCase.expectedCount; i++) {
662             if (i == testCase.expectedCount-1) {
663                 assertEqualsNear(msg.data(), testCase.expected[i].getNumber().getDouble(status),
664                                  measures[i]->getNumber().getDouble(status), testCase.accuracy);
665             } else {
666                 assertEquals(msg.data(), testCase.expected[i].getNumber().getDouble(status),
667                              measures[i]->getNumber().getDouble(status));
668             }
669             assertEquals(msg.data(), testCase.expected[i].getUnit().getIdentifier(),
670                          measures[i]->getUnit().getIdentifier());
671         }
672     };
673 
674     for (const auto &testCase : testCases)
675     {
676         input = MeasureUnit::forIdentifier(testCase.input, status);
677         output = MeasureUnit::forIdentifier(testCase.output, status);
678         const MeasureUnitImpl& inputImpl = MeasureUnitImpl::forMeasureUnit(input, tempInput, status);
679         const MeasureUnitImpl& outputImpl = MeasureUnitImpl::forMeasureUnit(output, tempOutput, status);
680 
681         ComplexUnitsConverter converter1(inputImpl, outputImpl, rates, status);
682         testATestCase(converter1, "ComplexUnitsConverter #1 " , testCase);
683 
684         // Test ComplexUnitsConverter created with CLDR units identifiers.
685         ComplexUnitsConverter converter2( testCase.input, testCase.output, status);
686         testATestCase(converter2, "ComplexUnitsConverter #1 " , testCase);
687     }
688 
689 
690     status.assertSuccess();
691 
692     // TODO(icu-units#63): test negative numbers!
693 }
694 
testComplexUnitsConverterSorting()695 void UnitsTest::testComplexUnitsConverterSorting() {
696     IcuTestErrorCode status(*this, "UnitsTest::testComplexUnitsConverterSorting");
697     ConversionRates conversionRates(status);
698 
699     status.assertSuccess();
700 
701     struct TestCase {
702         const char *msg;
703         const char *input;
704         const char *output;
705         double inputValue;
706         Measure expected[3];
707         int32_t expectedCount;
708         // For mixed units, accuracy of the smallest unit
709         double accuracy;
710     } testCases[]{{"inch-and-foot",
711                    "meter",
712                    "inch-and-foot",
713                    10.0,
714                    {
715                        Measure(9.70079, MeasureUnit::createInch(status), status),
716                        Measure(32, MeasureUnit::createFoot(status), status),
717                        Measure(0, MeasureUnit::createBit(status), status),
718                    },
719                    2,
720                    0.00001},
721                   {"inch-and-yard-and-foot",
722                    "meter",
723                    "inch-and-yard-and-foot",
724                    100.0,
725                    {
726                        Measure(1.0079, MeasureUnit::createInch(status), status),
727                        Measure(109, MeasureUnit::createYard(status), status),
728                        Measure(1, MeasureUnit::createFoot(status), status),
729                    },
730                    3,
731                    0.0001}};
732 
733     for (const auto &testCase : testCases) {
734         MeasureUnitImpl inputImpl = MeasureUnitImpl::forIdentifier(testCase.input, status);
735         if (status.errIfFailureAndReset()) {
736             continue;
737         }
738         MeasureUnitImpl outputImpl = MeasureUnitImpl::forIdentifier(testCase.output, status);
739         if (status.errIfFailureAndReset()) {
740             continue;
741         }
742         ComplexUnitsConverter converter(inputImpl, outputImpl, conversionRates, status);
743         if (status.errIfFailureAndReset()) {
744             continue;
745         }
746 
747         auto actual = converter.convert(testCase.inputValue, nullptr, status);
748         if (status.errIfFailureAndReset()) {
749             continue;
750         }
751         if (actual.length() < testCase.expectedCount) {
752             errln("converter.convert(...) returned too few Measures");
753             continue;
754         }
755 
756         for (int i = 0; i < testCase.expectedCount; i++) {
757             assertEquals(testCase.msg, testCase.expected[i].getUnit().getIdentifier(),
758                          actual[i]->getUnit().getIdentifier());
759 
760             if (testCase.expected[i].getNumber().getType() == Formattable::Type::kInt64) {
761                 assertEquals(testCase.msg, testCase.expected[i].getNumber().getInt64(),
762                              actual[i]->getNumber().getInt64());
763             } else {
764                 assertEqualsNear(testCase.msg, testCase.expected[i].getNumber().getDouble(),
765                                  actual[i]->getNumber().getDouble(), testCase.accuracy);
766             }
767         }
768     }
769 }
770 
771 /**
772  * This class represents the output fields from unitPreferencesTest.txt. Please
773  * see the documentation at the top of that file for details.
774  *
775  * For "mixed units" output, there are more (repeated) output fields. The last
776  * output unit has the expected output specified as both a rational fraction and
777  * a decimal fraction. This class ignores rational fractions, and expects to
778  * find a decimal fraction for each output unit.
779  */
780 class ExpectedOutput {
781   public:
782     // Counts number of units in the output. When this is more than one, we have
783     // "mixed units" in the expected output.
784     int _compoundCount = 0;
785 
786     // Counts how many fields were skipped: we expect to skip only one per
787     // output unit type (the rational fraction).
788     int _skippedFields = 0;
789 
790     // The expected output units: more than one for "mixed units".
791     MeasureUnit _measureUnits[3];
792 
793     // The amounts of each of the output units.
794     double _amounts[3];
795 
796     /**
797      * Parse an expected output field from the test data file.
798      *
799      * @param output may be a string representation of an integer, a rational
800      * fraction, a decimal fraction, or it may be a unit identifier. Whitespace
801      * should already be trimmed. This function ignores rational fractions,
802      * saving only decimal fractions and their unit identifiers.
803      * @return true if the field was successfully parsed, false if parsing
804      * failed.
805      */
parseOutputField(StringPiece output,UErrorCode & errorCode)806     void parseOutputField(StringPiece output, UErrorCode &errorCode) {
807         if (U_FAILURE(errorCode)) return;
808         DecimalQuantity dqOutputD;
809 
810         dqOutputD.setToDecNumber(output, errorCode);
811         if (U_SUCCESS(errorCode)) {
812             _amounts[_compoundCount] = dqOutputD.toDouble();
813             return;
814         } else if (errorCode == U_DECIMAL_NUMBER_SYNTAX_ERROR) {
815             // Not a decimal fraction, it might be a rational fraction or a unit
816             // identifier: continue.
817             errorCode = U_ZERO_ERROR;
818         } else {
819             // Unexpected error, so we propagate it.
820             return;
821         }
822 
823         _measureUnits[_compoundCount] = MeasureUnit::forIdentifier(output, errorCode);
824         if (U_SUCCESS(errorCode)) {
825             _compoundCount++;
826             _skippedFields = 0;
827             return;
828         }
829         _skippedFields++;
830         if (_skippedFields < 2) {
831             // We are happy skipping one field per output unit: we want to skip
832             // rational fraction fields like "11 / 10".
833             errorCode = U_ZERO_ERROR;
834             return;
835         } else {
836             // Propagate the error.
837             return;
838         }
839     }
840 
841     /**
842      * Produces an output string for debug purposes.
843      */
toDebugString()844     std::string toDebugString() {
845         std::string result;
846         for (int i = 0; i < _compoundCount; i++) {
847             result += std::to_string(_amounts[i]);
848             result += " ";
849             result += _measureUnits[i].getIdentifier();
850             result += " ";
851         }
852         return result;
853     }
854 };
855 
856 // Checks a vector of Measure instances against ExpectedOutput.
checkOutput(UnitsTest * unitsTest,const char * msg,ExpectedOutput expected,const MaybeStackVector<Measure> & actual,double precision)857 void checkOutput(UnitsTest *unitsTest, const char *msg, ExpectedOutput expected,
858                  const MaybeStackVector<Measure> &actual, double precision) {
859     IcuTestErrorCode status(*unitsTest, "checkOutput");
860 
861     CharString testMessage("Test case \"", status);
862     testMessage.append(msg, status);
863     testMessage.append("\": expected output: ", status);
864     testMessage.append(expected.toDebugString().c_str(), status);
865     testMessage.append(", obtained output:", status);
866     for (int i = 0; i < actual.length(); i++) {
867         testMessage.append(" ", status);
868         testMessage.append(std::to_string(actual[i]->getNumber().getDouble(status)), status);
869         testMessage.append(" ", status);
870         testMessage.appendInvariantChars(actual[i]->getUnit().getIdentifier(), status);
871     }
872     if (!unitsTest->assertEquals(testMessage.data(), expected._compoundCount, actual.length())) {
873         return;
874     };
875     for (int i = 0; i < actual.length(); i++) {
876         double permittedDiff = precision * expected._amounts[i];
877         if (permittedDiff == 0) {
878             // If 0 is expected, still permit a small delta.
879             // TODO: revisit this experimentally chosen value:
880             permittedDiff = 0.00000001;
881         }
882         unitsTest->assertEqualsNear(testMessage.data(), expected._amounts[i],
883                                     actual[i]->getNumber().getDouble(status), permittedDiff);
884     }
885 }
886 
887 /**
888  * Runs a single data-driven unit test for unit preferences.
889  *
890  * This is a UParseLineFn as required by u_parseDelimitedFile, intended for
891  * parsing unitPreferencesTest.txt.
892  */
unitPreferencesTestDataLineFn(void * context,char * fields[][2],int32_t fieldCount,UErrorCode * pErrorCode)893 void unitPreferencesTestDataLineFn(void *context, char *fields[][2], int32_t fieldCount,
894                                    UErrorCode *pErrorCode) {
895     if (U_FAILURE(*pErrorCode)) return;
896     UnitsTest *unitsTest = (UnitsTest *)context;
897     IcuTestErrorCode status(*unitsTest, "unitPreferencesTestDatalineFn");
898 
899     if (!unitsTest->assertTrue(u"unitPreferencesTestDataLineFn expects 9 fields for simple and 11 "
900                                u"fields for compound. Other field counts not yet supported. ",
901                                fieldCount == 9 || fieldCount == 11)) {
902         return;
903     }
904 
905     StringPiece quantity = trimField(fields[0]);
906     StringPiece usage = trimField(fields[1]);
907     StringPiece region = trimField(fields[2]);
908     // Unused // StringPiece inputR = trimField(fields[3]);
909     StringPiece inputD = trimField(fields[4]);
910     StringPiece inputUnit = trimField(fields[5]);
911     ExpectedOutput expected;
912     for (int i = 6; i < fieldCount; i++) {
913         expected.parseOutputField(trimField(fields[i]), status);
914     }
915     if (status.errIfFailureAndReset("parsing unitPreferencesTestData.txt test case: %s", fields[0][0])) {
916         return;
917     }
918 
919     DecimalQuantity dqInputD;
920     dqInputD.setToDecNumber(inputD, status);
921     if (status.errIfFailureAndReset("parsing decimal quantity: \"%.*s\"", inputD.length(),
922                                     inputD.data())) {
923         return;
924     }
925     double inputAmount = dqInputD.toDouble();
926 
927     MeasureUnit inputMeasureUnit = MeasureUnit::forIdentifier(inputUnit, status);
928     if (status.errIfFailureAndReset("forIdentifier(\"%.*s\")", inputUnit.length(), inputUnit.data())) {
929         return;
930     }
931 
932     unitsTest->logln("Quantity (Category): \"%.*s\", Usage: \"%.*s\", Region: \"%.*s\", "
933                      "Input: \"%f %s\", Expected Output: %s",
934                      quantity.length(), quantity.data(), usage.length(), usage.data(), region.length(),
935                      region.data(), inputAmount, inputMeasureUnit.getIdentifier(),
936                      expected.toDebugString().c_str());
937 
938     if (U_FAILURE(status)) {
939         return;
940     }
941 
942     UnitsRouter router(inputMeasureUnit, region, usage, status);
943     if (status.errIfFailureAndReset("UnitsRouter(<%s>, \"%.*s\", \"%.*s\", status)",
944                                     inputMeasureUnit.getIdentifier(), region.length(), region.data(),
945                                     usage.length(), usage.data())) {
946         return;
947     }
948 
949     CharString msg(quantity, status);
950     msg.append(" ", status);
951     msg.append(usage, status);
952     msg.append(" ", status);
953     msg.append(region, status);
954     msg.append(" ", status);
955     msg.append(inputD, status);
956     msg.append(" ", status);
957     msg.append(inputMeasureUnit.getIdentifier(), status);
958     if (status.errIfFailureAndReset("Failure before router.route")) {
959         return;
960     }
961     RouteResult routeResult = router.route(inputAmount, nullptr, status);
962     if (status.errIfFailureAndReset("router.route(inputAmount, ...)")) {
963         return;
964     }
965     // TODO: revisit this experimentally chosen precision:
966     checkOutput(unitsTest, msg.data(), expected, routeResult.measures, 0.0000000001);
967 
968     // Test UnitsRouter created with CLDR units identifiers.
969     CharString inputUnitIdentifier(inputUnit, status);
970     UnitsRouter router2(inputUnitIdentifier.data(), region, usage, status);
971     if (status.errIfFailureAndReset("UnitsRouter2(<%s>, \"%.*s\", \"%.*s\", status)",
972                                     inputUnitIdentifier.data(), region.length(), region.data(),
973                                     usage.length(), usage.data())) {
974         return;
975     }
976 
977     CharString msg2(quantity, status);
978     msg2.append(" ", status);
979     msg2.append(usage, status);
980     msg2.append(" ", status);
981     msg2.append(region, status);
982     msg2.append(" ", status);
983     msg2.append(inputD, status);
984     msg2.append(" ", status);
985     msg2.append(inputUnitIdentifier.data(), status);
986     if (status.errIfFailureAndReset("Failure before router2.route")) {
987         return;
988     }
989 
990     RouteResult routeResult2 = router2.route(inputAmount, nullptr, status);
991     if (status.errIfFailureAndReset("router2.route(inputAmount, ...)")) {
992         return;
993     }
994     // TODO: revisit this experimentally chosen precision:
995     checkOutput(unitsTest, msg2.data(), expected, routeResult.measures, 0.0000000001);
996 }
997 
998 /**
999  * Parses the format used by unitPreferencesTest.txt, calling lineFn for each
1000  * line.
1001  *
1002  * This is a modified version of u_parseDelimitedFile, customized for
1003  * unitPreferencesTest.txt, due to it having a variable number of fields per
1004  * line.
1005  */
parsePreferencesTests(const char * filename,char delimiter,char * fields[][2],int32_t maxFieldCount,UParseLineFn * lineFn,void * context,UErrorCode * pErrorCode)1006 void parsePreferencesTests(const char *filename, char delimiter, char *fields[][2],
1007                            int32_t maxFieldCount, UParseLineFn *lineFn, void *context,
1008                            UErrorCode *pErrorCode) {
1009     FileStream *file;
1010     char line[10000];
1011     char *start, *limit;
1012     int32_t i;
1013 
1014     if (U_FAILURE(*pErrorCode)) {
1015         return;
1016     }
1017 
1018     if (fields == NULL || lineFn == NULL || maxFieldCount <= 0) {
1019         *pErrorCode = U_ILLEGAL_ARGUMENT_ERROR;
1020         return;
1021     }
1022 
1023     if (filename == NULL || *filename == 0 || (*filename == '-' && filename[1] == 0)) {
1024         filename = NULL;
1025         file = T_FileStream_stdin();
1026     } else {
1027         file = T_FileStream_open(filename, "r");
1028     }
1029     if (file == NULL) {
1030         *pErrorCode = U_FILE_ACCESS_ERROR;
1031         return;
1032     }
1033 
1034     while (T_FileStream_readLine(file, line, sizeof(line)) != NULL) {
1035         /* remove trailing newline characters */
1036         u_rtrim(line);
1037 
1038         start = line;
1039         *pErrorCode = U_ZERO_ERROR;
1040 
1041         /* skip this line if it is empty or a comment */
1042         if (*start == 0 || *start == '#') {
1043             continue;
1044         }
1045 
1046         /* remove in-line comments */
1047         limit = uprv_strchr(start, '#');
1048         if (limit != NULL) {
1049             /* get white space before the pound sign */
1050             while (limit > start && U_IS_INV_WHITESPACE(*(limit - 1))) {
1051                 --limit;
1052             }
1053 
1054             /* truncate the line */
1055             *limit = 0;
1056         }
1057 
1058         /* skip lines with only whitespace */
1059         if (u_skipWhitespace(start)[0] == 0) {
1060             continue;
1061         }
1062 
1063         /* for each field, call the corresponding field function */
1064         for (i = 0; i < maxFieldCount; ++i) {
1065             /* set the limit pointer of this field */
1066             limit = start;
1067             while (*limit != delimiter && *limit != 0) {
1068                 ++limit;
1069             }
1070 
1071             /* set the field start and limit in the fields array */
1072             fields[i][0] = start;
1073             fields[i][1] = limit;
1074 
1075             /* set start to the beginning of the next field, if any */
1076             start = limit;
1077             if (*start != 0) {
1078                 ++start;
1079             } else {
1080                 break;
1081             }
1082         }
1083         if (i == maxFieldCount) {
1084             *pErrorCode = U_PARSE_ERROR;
1085         }
1086         int fieldCount = i + 1;
1087 
1088         /* call the field function */
1089         lineFn(context, fields, fieldCount, pErrorCode);
1090         if (U_FAILURE(*pErrorCode)) {
1091             break;
1092         }
1093     }
1094 
1095     if (filename != NULL) {
1096         T_FileStream_close(file);
1097     }
1098 }
1099 
1100 /**
1101  * Runs data-driven unit tests for unit preferences. It looks for the test cases
1102  * in source/test/testdata/cldr/units/unitPreferencesTest.txt, which originates
1103  * in CLDR.
1104  */
testUnitPreferencesWithCLDRTests()1105 void UnitsTest::testUnitPreferencesWithCLDRTests() {
1106     const char *filename = "unitPreferencesTest.txt";
1107     const int32_t maxFields = 11;
1108     char *fields[maxFields][2];
1109 
1110     IcuTestErrorCode errorCode(*this, "UnitsTest::testUnitPreferencesWithCLDRTests");
1111     const char *sourceTestDataPath = getSourceTestData(errorCode);
1112     if (errorCode.errIfFailureAndReset("unable to find the source/test/testdata "
1113                                        "folder (getSourceTestData())")) {
1114         return;
1115     }
1116 
1117     CharString path(sourceTestDataPath, errorCode);
1118     path.appendPathPart("cldr/units", errorCode);
1119     path.appendPathPart(filename, errorCode);
1120 
1121     parsePreferencesTests(path.data(), ';', fields, maxFields, unitPreferencesTestDataLineFn, this,
1122                           errorCode);
1123     if (errorCode.errIfFailureAndReset("error parsing %s: %s\n", path.data(), u_errorName(errorCode))) {
1124         return;
1125     }
1126 }
1127 
1128 #endif /* #if !UCONFIG_NO_FORMATTING */
1129