Search in sources :

Example 6 with TimeUnitFormat

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

the class TimeUnitTest method TestTimeUnitFormat.

/*
     * Tests the method public TimeUnitFormat(ULocale locale, int style), public TimeUnitFormat(Locale locale, int style)
     */
@SuppressWarnings("unused")
@Test
public void TestTimeUnitFormat() {
    // Tests when "if (style < FULL_NAME || style >= TOTAL_STYLES)" is true
    // TOTAL_STYLES is 2
    int[] cases = { TimeUnitFormat.FULL_NAME - 1, TimeUnitFormat.FULL_NAME - 2, 3 };
    for (int i = 0; i < cases.length; i++) {
        try {
            TimeUnitFormat tuf = new TimeUnitFormat(new ULocale("en_US"), cases[i]);
            errln("TimeUnitFormat(ULocale,int) was suppose to return an " + "exception for a style value of " + cases[i] + "passed into the constructor.");
        } catch (Exception e) {
        }
    }
    for (int i = 0; i < cases.length; i++) {
        try {
            TimeUnitFormat tuf = new TimeUnitFormat(new Locale("en_US"), cases[i]);
            errln("TimeUnitFormat(ULocale,int) was suppose to return an " + "exception for a style value of " + cases[i] + "passed into the constructor.");
        } catch (Exception e) {
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Locale(java.util.Locale) ULocale(android.icu.util.ULocale) TimeUnitFormat(android.icu.text.TimeUnitFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 7 with TimeUnitFormat

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

the class TimeUnitTest method TestBasic.

@Test
public void TestBasic() {
    String[] locales = { "en", "sl", "fr", "zh", "ar", "ru", "zh_Hant" };
    for (int locIndex = 0; locIndex < locales.length; ++locIndex) {
        // System.out.println("locale: " + locales[locIndex]);
        TimeUnitFormat[] formats = new TimeUnitFormat[] { new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.FULL_NAME), new TimeUnitFormat(new ULocale(locales[locIndex]), TimeUnitFormat.ABBREVIATED_NAME) };
        for (int style = TimeUnitFormat.FULL_NAME; style <= TimeUnitFormat.ABBREVIATED_NAME; ++style) {
            final TimeUnit[] values = TimeUnit.values();
            for (int j = 0; j < values.length; ++j) {
                final TimeUnit timeUnit = values[j];
                double[] tests = { 0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 5, 10, 100, 101.35 };
                for (int i = 0; i < tests.length; ++i) {
                    TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
                    String formatted = formats[style].format(source);
                    // System.out.println(formatted);
                    logln(tests[i] + " => " + formatted);
                    try {
                        // Style should not matter when parsing.
                        for (int parseStyle = TimeUnitFormat.FULL_NAME; parseStyle <= TimeUnitFormat.ABBREVIATED_NAME; parseStyle++) {
                            TimeUnitAmount result = (TimeUnitAmount) formats[parseStyle].parseObject(formatted);
                            if (result == null || !source.equals(result)) {
                                errln("No round trip: " + source + " => " + formatted + " => " + result);
                            }
                        }
                    } catch (ParseException e) {
                        errln(e.getMessage());
                    }
                }
            }
        }
    }
}
Also used : TimeUnitAmount(android.icu.util.TimeUnitAmount) ULocale(android.icu.util.ULocale) TimeUnitFormat(android.icu.text.TimeUnitFormat) TimeUnit(android.icu.util.TimeUnit) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 8 with TimeUnitFormat

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

the class TimeUnitTest method Test10219FactionalPluralsParse.

@Test
public void Test10219FactionalPluralsParse() throws ParseException {
    TimeUnitFormat tuf = new TimeUnitFormat(ULocale.ENGLISH, TimeUnitFormat.FULL_NAME);
    ParsePosition ppos = new ParsePosition(0);
    String parseString = "1 minutes";
    tuf.parseObject(parseString, ppos);
    // Parsing should go all the way to the end of the string.
    // We want the longest match, and we don't care if the plural form of the unit
    // matches the plural form of the number.
    assertEquals("Test10219FractionalPluralParse", parseString.length(), ppos.getIndex());
}
Also used : TimeUnitFormat(android.icu.text.TimeUnitFormat) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 9 with TimeUnitFormat

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

the class TimeUnitTest method Test10219FractionalPlurals.

@Test
public void Test10219FractionalPlurals() {
    TimeUnitFormat tuf = new TimeUnitFormat(ULocale.ENGLISH, TimeUnitFormat.FULL_NAME);
    String[] expected = { "1 minute", "1.5 minutes", "1.58 minutes" };
    for (int i = 2; i >= 0; i--) {
        NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
        nf.setRoundingMode(BigDecimal.ROUND_DOWN);
        nf.setMaximumFractionDigits(i);
        tuf.setNumberFormat(nf);
        assertEquals("Test10219", expected[i], tuf.format(new TimeUnitAmount(1.588, TimeUnit.MINUTE)));
    }
}
Also used : TimeUnitAmount(android.icu.util.TimeUnitAmount) TimeUnitFormat(android.icu.text.TimeUnitFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 10 with TimeUnitFormat

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

the class TimeUnitTest method TestBritishShortHourFallback.

// Android-changed: Added @Ignore to suppress the test; it consumes a lot of heap, permanently,
// affecting later tests. http://b/62374714
@Ignore
@Test
public void TestBritishShortHourFallback() {
    // See ticket #11986 "incomplete fallback in MeasureFormat".
    Object oneHour = new TimeUnitAmount(1, TimeUnit.HOUR);
    ULocale en_GB = new ULocale("en_GB");
    TimeUnitFormat formatter = new TimeUnitFormat(en_GB, TimeUnitFormat.ABBREVIATED_NAME);
    String result = formatter.format(oneHour);
    assertEquals("TestBritishShortHourFallback()", "1 hr", result);
    // Check that we can load the time unit formatting data for all locales.
    for (ULocale locale : ULocale.getAvailableLocales()) {
        try {
            new TimeUnitFormat(locale, TimeUnitFormat.ABBREVIATED_NAME);
        } catch (RuntimeException e) {
            errln("failed to load TimeUnitFormat data for " + locale + ": " + e);
        }
    }
}
Also used : TimeUnitAmount(android.icu.util.TimeUnitAmount) ULocale(android.icu.util.ULocale) TimeUnitFormat(android.icu.text.TimeUnitFormat) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TimeUnitFormat (android.icu.text.TimeUnitFormat)15 Test (org.junit.Test)15 ULocale (android.icu.util.ULocale)7 TimeUnitAmount (android.icu.util.TimeUnitAmount)5 NumberFormat (android.icu.text.NumberFormat)3 ParseException (java.text.ParseException)3 ParsePosition (java.text.ParsePosition)3 TimeUnit (android.icu.util.TimeUnit)2 Locale (java.util.Locale)2 MeasureFormat (android.icu.text.MeasureFormat)1 Measure (android.icu.util.Measure)1 Ignore (org.junit.Ignore)1