From e03dce66effc836eb6d81edc100c2cff17f1c33a Mon Sep 17 00:00:00 2001 From: Younies Date: Wed, 8 Sep 2021 16:58:23 +0000 Subject: [PATCH] ICU-21730 Port `testGetPreferencesFor` ICU4C tests to ICU4J See #1850 --- .../source/test/intltest/units_data_test.cpp | 1 + .../com/ibm/icu/dev/test/impl/UnitsTest.java | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/icu4c/source/test/intltest/units_data_test.cpp b/icu4c/source/test/intltest/units_data_test.cpp index 304a974a15f..b0ca7e2173e 100644 --- a/icu4c/source/test/intltest/units_data_test.cpp +++ b/icu4c/source/test/intltest/units_data_test.cpp @@ -20,6 +20,7 @@ class UnitsDataTest : public IntlTest { void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par = NULL) override; void testGetUnitCategory(); + // This is a sanity check that only exists in ICU4C. void testGetAllConversionRates(); void testGetPreferencesFor(); }; diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/UnitsTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/UnitsTest.java index 8c91c7aba89..37679e95bb8 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/UnitsTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/impl/UnitsTest.java @@ -13,6 +13,8 @@ import java.math.MathContext; import java.util.ArrayList; import java.util.List; +import com.ibm.icu.impl.units.UnitPreferences; +import com.ibm.icu.impl.units.UnitsData; import org.junit.Test; import com.ibm.icu.dev.test.TestUtil; @@ -695,4 +697,71 @@ public class UnitsTest { } } + + /** + * This test is dependent upon CLDR Data: when the preferences change, the test + * may fail: see the constants for expected Max/Min unit identifiers, for US and + * World, and for Roads and default lengths. + */ + @Test + public void testGetPreferencesFor() { + final String USRoadMax = "mile"; + final String USRoadMin = "foot"; + final String USLenMax = "mile"; + final String USLenMin = "inch"; + final String WorldRoadMax = "kilometer"; + final String WorldRoadMin = "meter"; + final String WorldLenMax = "kilometer"; + final String WorldLenMin = "centimeter"; + class TestCase { + final String name; + final String category; + final String usage; + final String region; + final String expectedBiggest; + final String expectedSmallest; + + public TestCase(String name, String category, String usage, String region, String expectedBiggest, String expectedSmallest) { + this.name = name; + this.category = category; + this.usage = usage; + this.region = region; + this.expectedBiggest = expectedBiggest; + this.expectedSmallest = expectedSmallest; + } + } + + TestCase testCases[] = { + new TestCase("US road", "length", "road", "US", USRoadMax, USRoadMin), + new TestCase("001 road", "length", "road", "001", WorldRoadMax, WorldRoadMin), + new TestCase("US lengths", "length", "default", "US", USLenMax, USLenMin), + new TestCase("001 lengths", "length", "default", "001", WorldLenMax, WorldLenMin), + new TestCase("XX road falls back to 001", "length", "road", "XX", WorldRoadMax, WorldRoadMin), + new TestCase("XX default falls back to 001", "length", "default", "XX", WorldLenMax, WorldLenMin), + new TestCase("Unknown usage US", "length", "foobar", "US", USLenMax, USLenMin), + new TestCase("Unknown usage 001", "length", "foobar", "XX", WorldLenMax, WorldLenMin), + new TestCase("Fallback", "length", "person-height-xyzzy", "DE", "centimeter", "centimeter"), + new TestCase("Fallback twice", "length", "person-height-xyzzy-foo", "DE", "centimeter", + "centimeter"), + // Confirming results for some unitPreferencesTest.txt test cases + new TestCase("001 area", "area", "default", "001", "square-kilometer", "square-centimeter"), + new TestCase("GB area", "area", "default", "GB", "square-mile", "square-inch"), + new TestCase("001 area geograph", "area", "geograph", "001", "square-kilometer", "square-kilometer"), + new TestCase("GB area geograph", "area", "geograph", "GB", "square-mile", "square-mile"), + new TestCase("CA person-height", "length", "person-height", "CA", "foot-and-inch", "inch"), + new TestCase("AT person-height", "length", "person-height", "AT", "meter-and-centimeter", + "meter-and-centimeter"), + }; + + UnitsData data = new UnitsData(); + for (TestCase t : testCases) { + UnitPreferences.UnitPreference prefs[] = data.getPreferencesFor(t.category, t.usage, t.region); + if (prefs.length > 0) { + assertEquals(t.name + " - max unit", t.expectedBiggest, prefs[0].getUnit()); + assertEquals(t.name + " - min unit", t.expectedSmallest, prefs[prefs.length - 1].getUnit()); + } else { + fail(t.name + ": failed to find preferences"); + } + } + } } -- 2.40.0