Search in sources :

Example 41 with RuleBasedNumberFormat

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

the class RbnfTest method TestGetNameListForLocale.

/* Tests the method
     *      private String[] getNameListForLocale(ULocale loc)
     *      public String[] getRuleSetDisplayNames(ULocale loc)
     */
@Test
public void TestGetNameListForLocale() {
    // Tests when "if (names != null)" is false and
    // "if (loc != null && ruleSetDisplayNames != null)" is false
    RuleBasedNumberFormat rbnf = new RuleBasedNumberFormat("dummy");
    rbnf.getRuleSetDisplayNames(null);
    try {
        rbnf.getRuleSetDisplayNames(null);
    } catch (Exception e) {
        errln("RuleBasedNumberFormat.getRuleSetDisplayNames(ULocale loc) " + "was not suppose to have an exception.");
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) ParseException(java.text.ParseException) Test(org.junit.Test)

Example 42 with RuleBasedNumberFormat

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

the class RbnfTest method TestDurations.

/**
 * Perform a simple spot check on the duration-formatting rules
 */
@Test
public void TestDurations() {
    RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(Locale.US, RuleBasedNumberFormat.DURATION);
    String[][] testData = { // move me and I fail
    { "3,600", "1:00:00" }, { "0", "0 sec." }, { "1", "1 sec." }, { "24", "24 sec." }, { "60", "1:00" }, { "73", "1:13" }, { "145", "2:25" }, { "666", "11:06" }, // { "3,600", "1:00:00" },
    { "3,740", "1:02:20" }, { "10,293", "2:51:33" } };
    doTest(formatter, testData, true);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 43 with RuleBasedNumberFormat

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

the class RbnfTest method doTest.

void doTest(RuleBasedNumberFormat formatter, String[][] testData, boolean testParsing) {
    // NumberFormat decFmt = NumberFormat.getInstance(Locale.US);
    NumberFormat decFmt = new DecimalFormat("#,###.################");
    try {
        for (int i = 0; i < testData.length; i++) {
            String number = testData[i][0];
            String expectedWords = testData[i][1];
            if (isVerbose()) {
                logln("test[" + i + "] number: " + number + " target: " + expectedWords);
            }
            Number num = decFmt.parse(number);
            String actualWords = formatter.format(num);
            if (!actualWords.equals(expectedWords)) {
                errln("Spot check format failed: for " + number + ", expected\n    " + expectedWords + ", but got\n    " + actualWords);
            } else if (testParsing) {
                String actualNumber = decFmt.format(formatter.parse(actualWords));
                if (!actualNumber.equals(number)) {
                    errln("Spot check parse failed: for " + actualWords + ", expected " + number + ", but got " + actualNumber);
                }
            }
        }
    } catch (Throwable e) {
        e.printStackTrace();
        errln("Test failed with exception: " + e.toString());
    }
}
Also used : DecimalFormat(android.icu.text.DecimalFormat) RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) NumberFormat(android.icu.text.NumberFormat)

Example 44 with RuleBasedNumberFormat

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

the class RBNFParseTest method TestLenientParse.

@Test
public void TestLenientParse() throws Exception {
    RuleBasedNumberFormat rbnf_en, rbnf_fr;
    // TODO: this still passes, but setLenientParseMode should have no effect now.
    // Did it ever test what it was supposed to?
    rbnf_en = new RuleBasedNumberFormat(Locale.ENGLISH, RuleBasedNumberFormat.SPELLOUT);
    rbnf_en.setLenientParseMode(true);
    rbnf_fr = new RuleBasedNumberFormat(Locale.FRENCH, RuleBasedNumberFormat.SPELLOUT);
    rbnf_fr.setLenientParseMode(true);
    Number n = rbnf_en.parse("1,2 million");
    logln(n.toString());
    String[][] lists = { { "1,2", "twelve", "un virgule deux" }, { "1,2 million", "twelve million", "un virgule deux" }, { "1,2 millions", "twelve million", "un million deux cent mille" }, { "1.2", "one point two", "douze" }, { "1.2 million", "one million two hundred thousand", "douze" }, { "1.2 millions", "one million two hundred thousand", "douze millions" } };
    Locale.setDefault(Locale.FRANCE);
    logln("Default locale:" + Locale.getDefault());
    logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName());
    logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName());
    parseList(rbnf_en, rbnf_fr, lists);
    Locale.setDefault(Locale.US);
    logln("Default locale:" + Locale.getDefault());
    logln("rbnf_en:" + rbnf_en.getDefaultRuleSetName());
    logln("rbnf_fr:" + rbnf_en.getDefaultRuleSetName());
    parseList(rbnf_en, rbnf_fr, lists);
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) Test(org.junit.Test)

Example 45 with RuleBasedNumberFormat

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

the class RBNFParseTest method TestParse.

@Test
public void TestParse() {
    // these rules make no sense but behave rationally
    String[] okrules = { "random text", "%foo:bar", "%foo: bar", "0:", "0::", "%%foo:;", "-", "-1", "-:", ".", ".1", "[", "]", "[]", "[foo]", "[[]", "[]]", "[[]]", "[][]", "<", ">", "=", "==", "===", "=foo=" };
    String[] exceptrules = { "", ";", ";;", ":", "::", ":1", ":;", ":;:;", "<<", "<<<", "10:;9:;", ">>", ">>>", // formatting any value with a one's digit will fail
    "10:", // formating a multiple of 10 causes rollback rule to fail
    "11: << x", "%%foo: 0 foo; 10: =%%bar=; %%bar: 0: bar; 10: =%%foo=;" };
    String[][] allrules = { okrules, exceptrules };
    for (int j = 0; j < allrules.length; ++j) {
        String[] tests = allrules[j];
        boolean except = tests == exceptrules;
        for (int i = 0; i < tests.length; ++i) {
            logln("----------");
            logln("rules: '" + tests[i] + "'");
            boolean caughtException = false;
            try {
                RuleBasedNumberFormat fmt = new RuleBasedNumberFormat(tests[i], Locale.US);
                logln("1.23: " + fmt.format(20));
                logln("-123: " + fmt.format(-123));
                logln(".123: " + fmt.format(.123));
                logln(" 123: " + fmt.format(123));
            } catch (Exception e) {
                if (!except) {
                    errln("Unexpected exception: " + e.getMessage());
                } else {
                    caughtException = true;
                }
            }
            if (except && !caughtException) {
                errln("expected exception but didn't get one!");
            }
        }
    }
}
Also used : RuleBasedNumberFormat(android.icu.text.RuleBasedNumberFormat) 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