Search in sources :

Example 21 with MeasureFormat

use of android.icu.text.MeasureFormat 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 22 with MeasureFormat

use of android.icu.text.MeasureFormat 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 23 with MeasureFormat

use of android.icu.text.MeasureFormat 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 24 with MeasureFormat

use of android.icu.text.MeasureFormat 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)

Example 25 with MeasureFormat

use of android.icu.text.MeasureFormat in project j2objc by google.

the class MeasureUnitTest method testDisplayNames.

@Test
public void testDisplayNames() {
    Object[][] data = new Object[][] { // Unit, locale, width, expected result
    { MeasureUnit.YEAR, "en", FormatWidth.WIDE, "years" }, { MeasureUnit.YEAR, "ja", FormatWidth.WIDE, "年" }, { MeasureUnit.YEAR, "es", FormatWidth.WIDE, "años" }, { MeasureUnit.YEAR, "pt", FormatWidth.WIDE, "anos" }, { MeasureUnit.YEAR, "pt-PT", FormatWidth.WIDE, "anos" }, { MeasureUnit.AMPERE, "en", FormatWidth.WIDE, "amperes" }, { MeasureUnit.AMPERE, "ja", FormatWidth.WIDE, "アンペア" }, { MeasureUnit.AMPERE, "es", FormatWidth.WIDE, "amperios" }, { MeasureUnit.AMPERE, "pt", FormatWidth.WIDE, "amperes" }, { MeasureUnit.AMPERE, "pt-PT", FormatWidth.WIDE, "amperes" }, { MeasureUnit.METER_PER_SECOND_SQUARED, "pt", FormatWidth.WIDE, "metros por segundo ao quadrado" }, { MeasureUnit.METER_PER_SECOND_SQUARED, "pt-PT", FormatWidth.WIDE, "metros por segundo quadrado" }, { MeasureUnit.SQUARE_KILOMETER, "pt", FormatWidth.NARROW, "km²" }, { MeasureUnit.SQUARE_KILOMETER, "pt", FormatWidth.SHORT, "km²" }, { MeasureUnit.SQUARE_KILOMETER, "pt", FormatWidth.WIDE, "quilômetros quadrados" }, { MeasureUnit.SECOND, "pt-PT", FormatWidth.NARROW, "s" }, { MeasureUnit.SECOND, "pt-PT", FormatWidth.SHORT, "s" }, { MeasureUnit.SECOND, "pt-PT", FormatWidth.WIDE, "segundos" }, { MeasureUnit.SECOND, "pt", FormatWidth.NARROW, "seg" }, { MeasureUnit.SECOND, "pt", FormatWidth.SHORT, "segs" }, { MeasureUnit.SECOND, "pt", FormatWidth.WIDE, "segundos" } };
    for (Object[] test : data) {
        MeasureUnit unit = (MeasureUnit) test[0];
        ULocale locale = ULocale.forLanguageTag((String) test[1]);
        FormatWidth formatWidth = (FormatWidth) test[2];
        String expected = (String) test[3];
        MeasureFormat mf = MeasureFormat.getInstance(locale, formatWidth);
        String actual = mf.getUnitDisplayName(unit);
        assertEquals(String.format("Unit Display Name for %s, %s, %s", unit, locale, formatWidth), expected, actual);
    }
}
Also used : MeasureUnit(android.icu.util.MeasureUnit) ULocale(android.icu.util.ULocale) FormatWidth(android.icu.text.MeasureFormat.FormatWidth) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Aggregations

MeasureFormat (android.icu.text.MeasureFormat)38 Test (org.junit.Test)32 Measure (android.icu.util.Measure)26 ULocale (android.icu.util.ULocale)11 Locale (java.util.Locale)8 ArrayList (java.util.ArrayList)7 SpannableStringBuilder (android.text.SpannableStringBuilder)6 TtsSpan (android.text.style.TtsSpan)6 FormatWidth (android.icu.text.MeasureFormat.FormatWidth)5 NumberFormat (android.icu.text.NumberFormat)5 FieldPosition (java.text.FieldPosition)5 MeasureUnit (android.icu.util.MeasureUnit)4 CurrencyAmount (android.icu.util.CurrencyAmount)3 ParseException (java.text.ParseException)3 BigDecimal (android.icu.math.BigDecimal)2 IOException (java.io.IOException)2 ResourceReader (android.icu.impl.data.ResourceReader)1 TokenIterator (android.icu.impl.data.TokenIterator)1 CompactDecimalFormat (android.icu.text.CompactDecimalFormat)1 DecimalFormat (android.icu.text.DecimalFormat)1