Search in sources :

Example 16 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class PluralRangesTest method TestFormatting.

@Test
public void TestFormatting() {
    Object[][] tests = { { 0.0, 1.0, ULocale.FRANCE, FormatWidth.WIDE, MeasureUnit.FAHRENHEIT, "0–1 degré Fahrenheit" }, { 1.0, 2.0, ULocale.FRANCE, FormatWidth.WIDE, MeasureUnit.FAHRENHEIT, "1–2 degrés Fahrenheit" }, { 3.1, 4.25, ULocale.FRANCE, FormatWidth.SHORT, MeasureUnit.FAHRENHEIT, "3,1–4,25 °F" }, { 3.1, 4.25, ULocale.ENGLISH, FormatWidth.SHORT, MeasureUnit.FAHRENHEIT, "3.1–4.25°F" }, { 3.1, 4.25, ULocale.CHINESE, FormatWidth.WIDE, MeasureUnit.INCH, "3.1-4.25英寸" }, { 0.0, 1.0, ULocale.ENGLISH, FormatWidth.WIDE, MeasureUnit.INCH, "0–1 inches" }, { 0.0, 1.0, ULocale.ENGLISH, FormatWidth.NARROW, Currency.getInstance("EUR"), "€0.00–1.00" }, { 0.0, 1.0, ULocale.FRENCH, FormatWidth.NARROW, Currency.getInstance("EUR"), "0,00–1,00 €" }, { 0.0, 100.0, ULocale.FRENCH, FormatWidth.NARROW, Currency.getInstance("JPY"), "0–100\u00a0JPY" }, { 0.0, 1.0, ULocale.ENGLISH, FormatWidth.SHORT, Currency.getInstance("EUR"), "EUR0.00–1.00" }, { 0.0, 1.0, ULocale.FRENCH, FormatWidth.SHORT, Currency.getInstance("EUR"), "0,00–1,00\u00a0EUR" }, { 0.0, 100.0, ULocale.FRENCH, FormatWidth.SHORT, Currency.getInstance("JPY"), "0–100\u00a0JPY" }, { 0.0, 1.0, ULocale.ENGLISH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0.00–1.00 euros" }, { 0.0, 1.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0,00–1,00 euro" }, { 0.0, 2.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("EUR"), "0,00–2,00 euros" }, { 0.0, 100.0, ULocale.FRENCH, FormatWidth.WIDE, Currency.getInstance("JPY"), "0–100 yens japonais" } };
    int i = 0;
    for (Object[] test : tests) {
        ++i;
        double low = (Double) test[0];
        double high = (Double) test[1];
        final ULocale locale = (ULocale) test[2];
        final FormatWidth width = (FormatWidth) test[3];
        final MeasureUnit unit = (MeasureUnit) test[4];
        final Object expected = test[5];
        MeasureFormat mf = MeasureFormat.getInstance(locale, width);
        Object actual;
        try {
            actual = mf.formatMeasureRange(new Measure(low, unit), new Measure(high, unit));
        } catch (Exception e) {
            actual = e.getClass();
        }
        assertEquals(i + " Formatting unit", expected, actual);
    }
}
Also used : ULocale(android.icu.util.ULocale) MeasureUnit(android.icu.util.MeasureUnit) Measure(android.icu.util.Measure) FormatWidth(android.icu.text.MeasureFormat.FormatWidth) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 17 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method testSimplePer.

@Test
public void testSimplePer() {
    Object DONT_CARE = null;
    Object[][] data = new Object[][] { // per unit pattern
    { FormatWidth.WIDE, 1.0, MeasureUnit.SECOND, "1 pound per second", DONT_CARE, 0, 0 }, { FormatWidth.WIDE, 2.0, MeasureUnit.SECOND, "2 pounds per second", DONT_CARE, 0, 0 }, // compound pattern
    { FormatWidth.WIDE, 1.0, MeasureUnit.MINUTE, "1 pound per minute", DONT_CARE, 0, 0 }, { FormatWidth.WIDE, 2.0, MeasureUnit.MINUTE, "2 pounds per minute", DONT_CARE, 0, 0 }, // per unit
    { FormatWidth.SHORT, 1.0, MeasureUnit.SECOND, "1 lb/s", DONT_CARE, 0, 0 }, { FormatWidth.SHORT, 2.0, MeasureUnit.SECOND, "2 lb/s", DONT_CARE, 0, 0 }, // compound
    { FormatWidth.SHORT, 1.0, MeasureUnit.MINUTE, "1 lb/min", DONT_CARE, 0, 0 }, { FormatWidth.SHORT, 2.0, MeasureUnit.MINUTE, "2 lb/min", DONT_CARE, 0, 0 }, // per unit
    { FormatWidth.NARROW, 1.0, MeasureUnit.SECOND, "1#/s", DONT_CARE, 0, 0 }, { FormatWidth.NARROW, 2.0, MeasureUnit.SECOND, "2#/s", DONT_CARE, 0, 0 }, // compound
    { FormatWidth.NARROW, 1.0, MeasureUnit.MINUTE, "1#/min", DONT_CARE, 0, 0 }, { FormatWidth.NARROW, 2.0, MeasureUnit.MINUTE, "2#/min", DONT_CARE, 0, 0 }, // field positions
    { FormatWidth.SHORT, 23.3, MeasureUnit.SECOND, "23.3 lb/s", NumberFormat.Field.DECIMAL_SEPARATOR, 2, 3 }, { FormatWidth.SHORT, 23.3, MeasureUnit.SECOND, "23.3 lb/s", NumberFormat.Field.INTEGER, 0, 2 }, { FormatWidth.SHORT, 23.3, MeasureUnit.MINUTE, "23.3 lb/min", NumberFormat.Field.DECIMAL_SEPARATOR, 2, 3 }, { FormatWidth.SHORT, 23.3, MeasureUnit.MINUTE, "23.3 lb/min", NumberFormat.Field.INTEGER, 0, 2 } };
    for (Object[] row : data) {
        FormatWidth formatWidth = (FormatWidth) row[0];
        Number amount = (Number) row[1];
        MeasureUnit perUnit = (MeasureUnit) row[2];
        String expected = row[3].toString();
        NumberFormat.Field field = (NumberFormat.Field) row[4];
        int startOffset = ((Integer) row[5]).intValue();
        int endOffset = ((Integer) row[6]).intValue();
        MeasureFormat mf = MeasureFormat.getInstance(ULocale.ENGLISH, formatWidth);
        FieldPosition pos = field != null ? new FieldPosition(field) : new FieldPosition(0);
        String prefix = "Prefix: ";
        assertEquals("", prefix + expected, mf.formatMeasurePerUnit(new Measure(amount, MeasureUnit.POUND), perUnit, new StringBuilder(prefix), pos).toString());
        if (field != DONT_CARE) {
            assertEquals("startOffset", startOffset, pos.getBeginIndex() - prefix.length());
            assertEquals("endOffset", endOffset, pos.getEndIndex() - prefix.length());
        }
    }
}
Also used : FormatWidth(android.icu.text.MeasureFormat.FormatWidth) FieldPosition(java.text.FieldPosition) Field(java.lang.reflect.Field) MeasureUnit(android.icu.util.MeasureUnit) Measure(android.icu.util.Measure) MeasureFormat(android.icu.text.MeasureFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 18 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method testUnitPerUnitResolution.

@Test
public void testUnitPerUnitResolution() {
    // Ticket 11274
    MeasureFormat fmt = MeasureFormat.getInstance(Locale.ENGLISH, FormatWidth.SHORT);
    // This fails unless we resolve to MeasureUnit.POUND_PER_SQUARE_INCH
    assertEquals("", "50 psi", fmt.formatMeasurePerUnit(new Measure(50, MeasureUnit.POUND), MeasureUnit.SQUARE_INCH, new StringBuilder(), new FieldPosition(0)).toString());
}
Also used : Measure(android.icu.util.Measure) FieldPosition(java.text.FieldPosition) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 19 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method testCurrencies.

@Test
public void testCurrencies() {
    Measure USD_1 = new Measure(1.0, Currency.getInstance("USD"));
    Measure USD_2 = new Measure(2.0, Currency.getInstance("USD"));
    Measure USD_NEG_1 = new Measure(-1.0, Currency.getInstance("USD"));
    MeasureFormat mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE);
    assertEquals("Wide currency", "-1.00 US dollars", mf.format(USD_NEG_1));
    assertEquals("Wide currency", "1.00 US dollars", mf.format(USD_1));
    assertEquals("Wide currency", "2.00 US dollars", mf.format(USD_2));
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.SHORT);
    assertEquals("short currency", "-USD1.00", mf.format(USD_NEG_1));
    assertEquals("short currency", "USD1.00", mf.format(USD_1));
    assertEquals("short currency", "USD2.00", mf.format(USD_2));
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.NARROW);
    assertEquals("narrow currency", "-$1.00", mf.format(USD_NEG_1));
    assertEquals("narrow currency", "$1.00", mf.format(USD_1));
    assertEquals("narrow currency", "$2.00", mf.format(USD_2));
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.NUMERIC);
    assertEquals("numeric currency", "-$1.00", mf.format(USD_NEG_1));
    assertEquals("numeric currency", "$1.00", mf.format(USD_1));
    assertEquals("numeric currency", "$2.00", mf.format(USD_2));
    mf = MeasureFormat.getInstance(ULocale.JAPAN, FormatWidth.WIDE);
    assertEquals("Wide currency", "-1.00\u7C73\u30C9\u30EB", mf.format(USD_NEG_1));
    assertEquals("Wide currency", "1.00\u7C73\u30C9\u30EB", mf.format(USD_1));
    assertEquals("Wide currency", "2.00\u7C73\u30C9\u30EB", mf.format(USD_2));
    Measure CAD_1 = new Measure(1.0, Currency.getInstance("CAD"));
    mf = MeasureFormat.getInstance(ULocale.CANADA, FormatWidth.SHORT);
    assertEquals("short currency", "CAD1.00", mf.format(CAD_1));
}
Also used : Measure(android.icu.util.Measure) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Example 20 with Measure

use of android.icu.util.Measure in project j2objc by google.

the class MeasureUnitTest method TestGreek.

@Test
public void TestGreek() {
    String[] locales = { "el_GR", "el" };
    final MeasureUnit[] units = new MeasureUnit[] { MeasureUnit.SECOND, MeasureUnit.MINUTE, MeasureUnit.HOUR, MeasureUnit.DAY, MeasureUnit.WEEK, MeasureUnit.MONTH, MeasureUnit.YEAR };
    FormatWidth[] styles = new FormatWidth[] { FormatWidth.WIDE, FormatWidth.SHORT };
    int[] numbers = new int[] { 1, 7 };
    String[] expected = { // "el_GR" 1 wide
    "1 δευτερόλεπτο", "1 λεπτό", "1 ώρα", "1 ημέρα", "1 εβδομάδα", "1 μήνας", "1 έτος", // "el_GR" 1 short
    "1 δευτ.", "1 λεπ.", "1 ώρα", "1 ημέρα", "1 εβδ.", "1 μήν.", // year (one)
    "1 έτ.", // "el_GR" 7 wide
    "7 δευτερόλεπτα", "7 λεπτά", "7 ώρες", "7 ημέρες", "7 εβδομάδες", "7 μήνες", "7 έτη", // "el_GR" 7 short
    "7 δευτ.", "7 λεπ.", // hour (other)
    "7 ώρ.", "7 ημέρες", "7 εβδ.", "7 μήν.", // year (other)
    "7 έτ.", // "el" 1 wide
    "1 δευτερόλεπτο", "1 λεπτό", "1 ώρα", "1 ημέρα", "1 εβδομάδα", "1 μήνας", "1 έτος", // "el" 1 short
    "1 δευτ.", "1 λεπ.", "1 ώρα", "1 ημέρα", "1 εβδ.", "1 μήν.", // year (one)
    "1 έτ.", // "el" 7 wide
    "7 δευτερόλεπτα", "7 λεπτά", "7 ώρες", "7 ημέρες", "7 εβδομάδες", "7 μήνες", "7 έτη", // "el" 7 short
    "7 δευτ.", "7 λεπ.", // hour (other)
    "7 ώρ.", "7 ημέρες", "7 εβδ.", "7 μήν.", // year (other
    "7 έτ." };
    int counter = 0;
    String formatted;
    for (int locIndex = 0; locIndex < locales.length; ++locIndex) {
        for (int numIndex = 0; numIndex < numbers.length; ++numIndex) {
            for (int styleIndex = 0; styleIndex < styles.length; ++styleIndex) {
                for (int unitIndex = 0; unitIndex < units.length; ++unitIndex) {
                    Measure m = new Measure(numbers[numIndex], units[unitIndex]);
                    MeasureFormat fmt = MeasureFormat.getInstance(new ULocale(locales[locIndex]), styles[styleIndex]);
                    formatted = fmt.format(m);
                    assertEquals("locale: " + locales[locIndex] + ", style: " + styles[styleIndex] + ", units: " + units[unitIndex] + ", value: " + numbers[numIndex], expected[counter], formatted);
                    ++counter;
                }
            }
        }
    }
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) ULocale(android.icu.util.ULocale) Measure(android.icu.util.Measure) FormatWidth(android.icu.text.MeasureFormat.FormatWidth) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Aggregations

Measure (android.icu.util.Measure)31 MeasureFormat (android.icu.text.MeasureFormat)26 Test (org.junit.Test)22 ULocale (android.icu.util.ULocale)7 FieldPosition (java.text.FieldPosition)7 ArrayList (java.util.ArrayList)7 SpannableStringBuilder (android.text.SpannableStringBuilder)6 TtsSpan (android.text.style.TtsSpan)6 Locale (java.util.Locale)6 FormatWidth (android.icu.text.MeasureFormat.FormatWidth)4 MeasureUnit (android.icu.util.MeasureUnit)4 NumberFormat (android.icu.text.NumberFormat)3 DontCareFieldPosition (android.icu.impl.DontCareFieldPosition)2 TimeUnitFormat (android.icu.text.TimeUnitFormat)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1 Collection (java.util.Collection)1