Search in sources :

Example 1 with ResourceReader

use of android.icu.impl.data.ResourceReader in project j2objc by google.

the class NumberFormatTest method TestCases.

// InputStream is will be closed by the ResourceReader.
@SuppressWarnings("resource")
@Test
public void TestCases() {
    String caseFileName = "NumberFormatTestCases.txt";
    java.io.InputStream is = NumberFormatTest.class.getResourceAsStream(caseFileName);
    ResourceReader reader = new ResourceReader(is, caseFileName, "utf-8");
    TokenIterator tokens = new TokenIterator(reader);
    Locale loc = new Locale("en", "US", "");
    DecimalFormat ref = null, fmt = null;
    MeasureFormat mfmt = null;
    String pat = null, str = null, mloc = null;
    boolean strict = false;
    try {
        for (; ; ) {
            String tok = tokens.next();
            if (tok == null) {
                break;
            }
            String where = "(" + tokens.getLineNumber() + ") ";
            int cmd = keywordIndex(tok);
            switch(cmd) {
                case 0:
                    // ref= <reference pattern>
                    ref = new DecimalFormat(tokens.next(), new DecimalFormatSymbols(Locale.US));
                    ref.setParseStrict(strict);
                    logln("Setting reference pattern to:\t" + ref);
                    break;
                case 1:
                    // loc= <locale>
                    loc = LocaleUtility.getLocaleFromName(tokens.next());
                    pat = ((DecimalFormat) NumberFormat.getInstance(loc)).toPattern();
                    logln("Setting locale to:\t" + loc + ", \tand pattern to:\t" + pat);
                    break;
                // f:
                case 2:
                // fp:
                case 3:
                // rt:
                case 4:
                case // p:
                5:
                    tok = tokens.next();
                    if (!tok.equals("-")) {
                        pat = tok;
                    }
                    try {
                        fmt = new DecimalFormat(pat, new DecimalFormatSymbols(loc));
                        fmt.setParseStrict(strict);
                    } catch (IllegalArgumentException iae) {
                        errln(where + "Pattern \"" + pat + '"');
                        iae.printStackTrace();
                        // consume remaining tokens
                        tokens.next();
                        // tokens.next();
                        if (cmd == 3)
                            tokens.next();
                        continue;
                    }
                    str = null;
                    try {
                        if (cmd == 2 || cmd == 3 || cmd == 4) {
                            // f: <pattern or '-'> <number> <exp. string>
                            // fp: <pattern or '-'> <number> <exp. string> <exp. number>
                            // rt: <pattern or '-'> <number> <string>
                            String num = tokens.next();
                            str = tokens.next();
                            Number n = ref.parse(num);
                            assertEquals(where + '"' + pat + "\".format(" + num + ")", str, fmt.format(n));
                            if (cmd == 3) {
                                // fp:
                                n = ref.parse(tokens.next());
                            }
                            if (cmd != 2) {
                                // != f:
                                assertEquals(where + '"' + pat + "\".parse(\"" + str + "\")", n, fmt.parse(str));
                            }
                        } else // p: <pattern or '-'> <string to parse> <exp. number>
                        {
                            str = tokens.next();
                            String expstr = tokens.next();
                            Number parsed = fmt.parse(str);
                            Number exp = ref.parse(expstr);
                            assertEquals(where + '"' + pat + "\".parse(\"" + str + "\")", exp, parsed);
                        }
                    } catch (ParseException e) {
                        errln(where + '"' + pat + "\".parse(\"" + str + "\") threw an exception");
                        e.printStackTrace();
                    }
                    break;
                case 6:
                    // perr: <pattern or '-'> <invalid string>
                    errln("Under construction");
                    return;
                case 7:
                    // pat: <pattern> <exp. toPattern, or '-' or 'err'>
                    String testpat = tokens.next();
                    String exppat = tokens.next();
                    boolean err = exppat.equals("err");
                    if (testpat.equals("-")) {
                        if (err) {
                            errln("Invalid command \"pat: - err\" at " + tokens.describePosition());
                            continue;
                        }
                        testpat = pat;
                    }
                    if (exppat.equals("-"))
                        exppat = testpat;
                    try {
                        DecimalFormat f = null;
                        if (testpat == pat) {
                            // [sic]
                            f = fmt;
                        } else {
                            f = new DecimalFormat(testpat);
                            f.setParseStrict(strict);
                        }
                        if (err) {
                            errln(where + "Invalid pattern \"" + testpat + "\" was accepted");
                        } else {
                            assertEquals(where + '"' + testpat + "\".toPattern()", exppat, f.toPattern());
                        }
                    } catch (IllegalArgumentException iae2) {
                        if (err) {
                            logln("Ok: " + where + "Invalid pattern \"" + testpat + "\" threw an exception");
                        } else {
                            errln(where + "Valid pattern \"" + testpat + "\" threw an exception");
                            iae2.printStackTrace();
                        }
                    }
                    break;
                case // fpc:
                8:
                    tok = tokens.next();
                    if (!tok.equals("-")) {
                        mloc = tok;
                        ULocale l = new ULocale(mloc);
                        try {
                            mfmt = MeasureFormat.getCurrencyFormat(l);
                        } catch (IllegalArgumentException iae) {
                            errln(where + "Loc \"" + tok + '"');
                            iae.printStackTrace();
                            // consume remaining tokens
                            tokens.next();
                            tokens.next();
                            tokens.next();
                            continue;
                        }
                    }
                    str = null;
                    try {
                        // fpc: <loc or '-'> <curr.amt> <exp. string> <exp. curr.amt>
                        String currAmt = tokens.next();
                        str = tokens.next();
                        CurrencyAmount target = parseCurrencyAmount(currAmt, ref, '/');
                        String formatResult = mfmt.format(target);
                        assertEquals(where + "getCurrencyFormat(" + mloc + ").format(" + currAmt + ")", str, formatResult);
                        target = parseCurrencyAmount(tokens.next(), ref, '/');
                        CurrencyAmount parseResult = (CurrencyAmount) mfmt.parseObject(str);
                        assertEquals(where + "getCurrencyFormat(" + mloc + ").parse(\"" + str + "\")", target, parseResult);
                    } catch (ParseException e) {
                        errln(where + '"' + pat + "\".parse(\"" + str + "\") threw an exception");
                        e.printStackTrace();
                    }
                    break;
                case // strict= true or false
                9:
                    strict = "true".equalsIgnoreCase(tokens.next());
                    logln("Setting strict to:\t" + strict);
                    break;
                case -1:
                    errln("Unknown command \"" + tok + "\" at " + tokens.describePosition());
                    return;
            }
        }
    } catch (java.io.IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            reader.close();
        } catch (IOException ignored) {
        }
    }
}
Also used : ResourceReader(android.icu.impl.data.ResourceReader) Locale(java.util.Locale) ULocale(android.icu.util.ULocale) ULocale(android.icu.util.ULocale) DecimalFormatSymbols(android.icu.text.DecimalFormatSymbols) CompactDecimalFormat(android.icu.text.CompactDecimalFormat) DecimalFormat(android.icu.text.DecimalFormat) IOException(java.io.IOException) IOException(java.io.IOException) CurrencyAmount(android.icu.util.CurrencyAmount) TokenIterator(android.icu.impl.data.TokenIterator) ParseException(java.text.ParseException) MeasureFormat(android.icu.text.MeasureFormat) Test(org.junit.Test)

Aggregations

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 DecimalFormatSymbols (android.icu.text.DecimalFormatSymbols)1 MeasureFormat (android.icu.text.MeasureFormat)1 CurrencyAmount (android.icu.util.CurrencyAmount)1 ULocale (android.icu.util.ULocale)1 IOException (java.io.IOException)1 ParseException (java.text.ParseException)1 Locale (java.util.Locale)1 Test (org.junit.Test)1