Search in sources :

Example 26 with RuleBasedNumberFormat

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

the class NumberFormatTest method TestFormatAbstractImplCoverage.

/*
     * Coverage tests for the implementation of abstract format methods not being called otherwise
     */
public void TestFormatAbstractImplCoverage() {
    NumberFormat df = DecimalFormat.getInstance(Locale.ENGLISH);
    NumberFormat cdf = CompactDecimalFormat.getInstance(Locale.ENGLISH, CompactDecimalFormat.CompactStyle.SHORT);
    NumberFormat rbf = new RuleBasedNumberFormat(ULocale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
    /*
         *  Test  NumberFormat.format(BigDecimal,StringBuffer,FieldPosition)
         */
    StringBuffer sb = new StringBuffer();
    String result = df.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"2,000.43".equals(result)) {
        errln("DecimalFormat failed. Expected: 2,000.43 - Actual: " + result);
    }
    sb.delete(0, sb.length());
    result = cdf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"2K".equals(result)) {
        errln("DecimalFormat failed. Expected: 2K - Actual: " + result);
    }
    sb.delete(0, sb.length());
    result = rbf.format(new BigDecimal(2000.43), sb, new FieldPosition(0)).toString();
    if (!"two thousand point four three".equals(result)) {
        errln("DecimalFormat failed. Expected: 'two thousand point four three' - Actual: '" + result + "'");
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) FieldPosition(java.text.FieldPosition) BigDecimal(android.icu.math.BigDecimal) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat)

Example 27 with RuleBasedNumberFormat

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

the class RbnfLenientScannerTest method TestDefaultProvider.

/**
 * Ensure that the default provider is instantiated and used if none is set
 * and lenient parse is on.
 */
@Test
public void TestDefaultProvider() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.SPELLOUT);
    formatter.setLenientScannerProvider(null);
    formatter.setLenientParseMode(true);
    String[][] lpTestData = { { "2 thousand six HUNDRED   fifty-7", "2,657" } };
    doLenientParseTest(formatter, lpTestData);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 28 with RuleBasedNumberFormat

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

the class RbnfLenientScannerTest method TestFrenchSpellout.

/**
 * Perform a simple spot check on the French spellout rules
 */
@Test
public void TestFrenchSpellout() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.FRANCE, RuleBasedNumberFormat.SPELLOUT);
    formatter.setLenientScannerProvider(provider);
    formatter.setLenientParseMode(true);
    String[][] lpTestData = { { "trente-et-un", "31" }, { "un cent quatre vingt dix huit", "198" } };
    doLenientParseTest(formatter, lpTestData);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 29 with RuleBasedNumberFormat

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

the class RbnfLenientScannerTest method TestDurations.

/**
 * Perform a simple spot check on the duration-formatting rules
 */
@Test
public void TestDurations() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.DURATION);
    formatter.setLenientScannerProvider(provider);
    formatter.setLenientParseMode(true);
    String[][] lpTestData = { { "2-51-33", "10,293" } };
    doLenientParseTest(formatter, lpTestData);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 30 with RuleBasedNumberFormat

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

the class RbnfLenientScannerTest method TestAllLocales.

@Test
public void TestAllLocales() {
    StringBuffer errors = null;
    ULocale[] locales = ULocale.getAvailableLocales();
    String[] names = { " (spellout) ", " (ordinal)  ", " (duration) " };
    double[] numbers = { 45.678, 1, 2, 10, 11, 100, 110, 200, 1000, 1111, -1111 };
    Random r = null;
    // RBNF parse is extremely slow when lenient option is enabled.
    // For non-exhaustive mode, we only test a few locales.
    // "nl_NL", "be" had crash problem reported by #6534
    String[] parseLocales = { "en_US", "nl_NL", "be" };
    for (int i = 0; i < locales.length; ++i) {
        ULocale loc = locales[i];
        int count = numbers.length;
        boolean testParse = true;
        if (TestFmwk.getExhaustiveness() <= 5) {
            testParse = false;
            for (int k = 0; k < parseLocales.length; k++) {
                if (loc.toString().equals(parseLocales[k])) {
                    testParse = true;
                    break;
                }
            }
        } else {
        // RBNF parse is too slow.  Increase count only for debugging purpose for now.
        // count = 100;
        }
        for (int j = 0; j < 3; ++j) {
            RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(loc, j + 1);
            for (int c = 0; c < count; c++) {
                double n;
                if (c < numbers.length) {
                    n = numbers[c];
                } else {
                    if (r == null) {
                        r = createRandom();
                    }
                    n = ((int) (r.nextInt(10000) - 3000)) / 16d;
                }
                String s = fmt.format(n);
                logln(loc.getName() + names[j] + "success format: " + n + " -> " + s);
                if (testParse) {
                    // because there are cases which do not round trip by design.
                    try {
                        // non-lenient parse
                        fmt.setLenientParseMode(false);
                        Number num = fmt.parse(s);
                        logln(loc.getName() + names[j] + "success parse: " + s + " -> " + num);
                        // lenient parse
                        fmt.setLenientScannerProvider(provider);
                        fmt.setLenientParseMode(true);
                        num = fmt.parse(s);
                        logln(loc.getName() + names[j] + "success parse (lenient): " + s + " -> " + num);
                    } catch (ParseException pe) {
                        String msg = loc.getName() + names[j] + "ERROR:" + pe.getMessage();
                        logln(msg);
                        if (errors == null) {
                            errors = new StringBuffer();
                        }
                        errors.append("\n" + msg);
                    }
                }
            }
        }
    }
    if (errors != null) {
        // TODO: We need to fix parse problems - see #6895 / #6896
        // errln(errors.toString());
        logln(errors.toString());
    }
}
Also used : ULocale(android.icu.util.ULocale) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Random(java.util.Random) ParseException(java.text.ParseException) Test(org.junit.Test)

Aggregations

RuleBasedNumberFormat (android.icu.text.RuleBasedNumberFormat)55 Test (org.junit.Test)52 ULocale (android.icu.util.ULocale)14 Locale (java.util.Locale)14 ParseException (java.text.ParseException)8 NumberFormat (android.icu.text.NumberFormat)3 BigDecimal (android.icu.math.BigDecimal)2 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)2 DisplayContext (android.icu.text.DisplayContext)2 Random (java.util.Random)2 DecimalFormat (android.icu.text.DecimalFormat)1 BigInteger (java.math.BigInteger)1 FieldPosition (java.text.FieldPosition)1 NumberFormat (java.text.NumberFormat)1