Search in sources :

Example 1 with TimeUnitAmount

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

the class MeasureUnitTest method TestFormatPeriodEn.

@Test
public void TestFormatPeriodEn() {
    TimeUnitAmount[] _19m = { new TimeUnitAmount(19.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _1h_23_5s = { new TimeUnitAmount(1.0, TimeUnit.HOUR), new TimeUnitAmount(23.5, TimeUnit.SECOND) };
    TimeUnitAmount[] _1h_23_5m = { new TimeUnitAmount(1.0, TimeUnit.HOUR), new TimeUnitAmount(23.5, TimeUnit.MINUTE) };
    TimeUnitAmount[] _1h_0m_23s = { new TimeUnitAmount(1.0, TimeUnit.HOUR), new TimeUnitAmount(0.0, TimeUnit.MINUTE), new TimeUnitAmount(23.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _2y_5M_3w_4d = { new TimeUnitAmount(2.0, TimeUnit.YEAR), new TimeUnitAmount(5.0, TimeUnit.MONTH), new TimeUnitAmount(3.0, TimeUnit.WEEK), new TimeUnitAmount(4.0, TimeUnit.DAY) };
    TimeUnitAmount[] _1m_59_9996s = { new TimeUnitAmount(1.0, TimeUnit.MINUTE), new TimeUnitAmount(59.9996, TimeUnit.SECOND) };
    TimeUnitAmount[] _5h_17m = { new TimeUnitAmount(5.0, TimeUnit.HOUR), new TimeUnitAmount(17.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _neg5h_17m = { new TimeUnitAmount(-5.0, TimeUnit.HOUR), new TimeUnitAmount(17.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _19m_28s = { new TimeUnitAmount(19.0, TimeUnit.MINUTE), new TimeUnitAmount(28.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _0h_0m_9s = { new TimeUnitAmount(0.0, TimeUnit.HOUR), new TimeUnitAmount(0.0, TimeUnit.MINUTE), new TimeUnitAmount(9.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _0h_0m_17s = { new TimeUnitAmount(0.0, TimeUnit.HOUR), new TimeUnitAmount(0.0, TimeUnit.MINUTE), new TimeUnitAmount(17.0, TimeUnit.SECOND) };
    TimeUnitAmount[] _6h_56_92m = { new TimeUnitAmount(6.0, TimeUnit.HOUR), new TimeUnitAmount(56.92, TimeUnit.MINUTE) };
    TimeUnitAmount[] _3h_4s_5m = { new TimeUnitAmount(3.0, TimeUnit.HOUR), new TimeUnitAmount(4.0, TimeUnit.SECOND), new TimeUnitAmount(5.0, TimeUnit.MINUTE) };
    TimeUnitAmount[] _6_7h_56_92m = { new TimeUnitAmount(6.7, TimeUnit.HOUR), new TimeUnitAmount(56.92, TimeUnit.MINUTE) };
    TimeUnitAmount[] _3h_5h = { new TimeUnitAmount(3.0, TimeUnit.HOUR), new TimeUnitAmount(5.0, TimeUnit.HOUR) };
    Object[][] fullData = { { _1m_59_9996s, "1 minute, 59.9996 seconds" }, { _19m, "19 minutes" }, { _1h_23_5s, "1 hour, 23.5 seconds" }, { _1h_23_5m, "1 hour, 23.5 minutes" }, { _1h_0m_23s, "1 hour, 0 minutes, 23 seconds" }, { _2y_5M_3w_4d, "2 years, 5 months, 3 weeks, 4 days" } };
    Object[][] abbrevData = { { _1m_59_9996s, "1 min, 59.9996 sec" }, { _19m, "19 min" }, { _1h_23_5s, "1 hr, 23.5 sec" }, { _1h_23_5m, "1 hr, 23.5 min" }, { _1h_0m_23s, "1 hr, 0 min, 23 sec" }, { _2y_5M_3w_4d, "2 yrs, 5 mths, 3 wks, 4 days" } };
    Object[][] narrowData = { { _1m_59_9996s, "1m 59.9996s" }, { _19m, "19m" }, { _1h_23_5s, "1h 23.5s" }, { _1h_23_5m, "1h 23.5m" }, { _1h_0m_23s, "1h 0m 23s" }, { _2y_5M_3w_4d, "2y 5m 3w 4d" } };
    Object[][] numericData = { { _1m_59_9996s, "1:59.9996" }, { _19m, "19m" }, { _1h_23_5s, "1:00:23.5" }, { _1h_0m_23s, "1:00:23" }, { _1h_23_5m, "1:23.5" }, { _5h_17m, "5:17" }, { _neg5h_17m, "-5h 17m" }, { _19m_28s, "19:28" }, { _2y_5M_3w_4d, "2y 5m 3w 4d" }, { _0h_0m_9s, "0:00:09" }, { _6h_56_92m, "6:56.92" }, { _6_7h_56_92m, "6:56.92" }, { _3h_4s_5m, "3h 4s 5m" }, { _3h_5h, "3h 5h" } };
    Object[][] fullDataDe = { { _1m_59_9996s, "1 Minute, 59,9996 Sekunden" }, { _19m, "19 Minuten" }, { _1h_23_5s, "1 Stunde, 23,5 Sekunden" }, { _1h_23_5m, "1 Stunde, 23,5 Minuten" }, { _1h_0m_23s, "1 Stunde, 0 Minuten und 23 Sekunden" }, { _2y_5M_3w_4d, "2 Jahre, 5 Monate, 3 Wochen und 4 Tage" } };
    Object[][] numericDataDe = { { _1m_59_9996s, "1:59,9996" }, { _19m, "19 Min." }, { _1h_23_5s, "1:00:23,5" }, { _1h_0m_23s, "1:00:23" }, { _1h_23_5m, "1:23,5" }, { _5h_17m, "5:17" }, { _19m_28s, "19:28" }, { _2y_5M_3w_4d, "2 J, 5 M, 3 W und 4 T" }, { _0h_0m_17s, "0:00:17" }, { _6h_56_92m, "6:56,92" }, { _3h_5h, "3 Std., 5 Std." } };
    NumberFormat nf = NumberFormat.getNumberInstance(ULocale.ENGLISH);
    nf.setMaximumFractionDigits(4);
    MeasureFormat mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.WIDE, nf);
    verifyFormatPeriod("en FULL", mf, fullData);
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.SHORT, nf);
    verifyFormatPeriod("en SHORT", mf, abbrevData);
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.NARROW, nf);
    verifyFormatPeriod("en NARROW", mf, narrowData);
    mf = MeasureFormat.getInstance(ULocale.ENGLISH, FormatWidth.NUMERIC, nf);
    verifyFormatPeriod("en NUMERIC", mf, numericData);
    nf = NumberFormat.getNumberInstance(ULocale.GERMAN);
    nf.setMaximumFractionDigits(4);
    mf = MeasureFormat.getInstance(ULocale.GERMAN, FormatWidth.WIDE, nf);
    verifyFormatPeriod("de FULL", mf, fullDataDe);
    mf = MeasureFormat.getInstance(ULocale.GERMAN, FormatWidth.NUMERIC, nf);
    verifyFormatPeriod("de NUMERIC", mf, numericDataDe);
    // Same tests, with Java Locale
    nf = NumberFormat.getNumberInstance(Locale.GERMAN);
    nf.setMaximumFractionDigits(4);
    mf = MeasureFormat.getInstance(Locale.GERMAN, FormatWidth.WIDE, nf);
    verifyFormatPeriod("de FULL(Java Locale)", mf, fullDataDe);
    mf = MeasureFormat.getInstance(Locale.GERMAN, FormatWidth.NUMERIC, nf);
    verifyFormatPeriod("de NUMERIC(Java Locale)", mf, numericDataDe);
}
Also used : TimeUnitAmount(android.icu.util.TimeUnitAmount) MeasureFormat(android.icu.text.MeasureFormat) NumberFormat(android.icu.text.NumberFormat) Test(org.junit.Test)

Example 2 with TimeUnitAmount

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

the class TimeUnitTest method formatParsing.

private void formatParsing(TimeUnitFormat format) {
    final TimeUnit[] values = TimeUnit.values();
    for (int j = 0; j < values.length; ++j) {
        final TimeUnit timeUnit = values[j];
        double[] tests = { 0, 0.5, 1, 2, 3, 5 };
        for (int i = 0; i < tests.length; ++i) {
            TimeUnitAmount source = new TimeUnitAmount(tests[i], timeUnit);
            String formatted = format.format(source);
            // System.out.println(formatted);
            logln(tests[i] + " => " + formatted);
            try {
                TimeUnitAmount result = (TimeUnitAmount) format.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) TimeUnit(android.icu.util.TimeUnit) ParseException(java.text.ParseException)

Example 3 with TimeUnitAmount

use of android.icu.util.TimeUnitAmount 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 4 with TimeUnitAmount

use of android.icu.util.TimeUnitAmount 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 5 with TimeUnitAmount

use of android.icu.util.TimeUnitAmount 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

TimeUnitAmount (android.icu.util.TimeUnitAmount)8 Test (org.junit.Test)6 TimeUnitFormat (android.icu.text.TimeUnitFormat)5 TimeUnit (android.icu.util.TimeUnit)4 NumberFormat (android.icu.text.NumberFormat)3 ULocale (android.icu.util.ULocale)3 ParseException (java.text.ParseException)3 MeasureFormat (android.icu.text.MeasureFormat)1 Ignore (org.junit.Ignore)1