Search in sources :

Example 1 with Output

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

the class CollationTest method checkCompareTwo.

private boolean checkCompareTwo(String norm, String prevFileLine, String prevString, String s, int expectedOrder, int expectedLevel) {
    // Get the sort keys first, for error debug output.
    Output<CollationKey> prevKeyOut = new Output<CollationKey>();
    CollationKey prevKey;
    if (!getCollationKey(norm, fileLine, prevString, prevKeyOut)) {
        return false;
    }
    prevKey = prevKeyOut.value;
    Output<CollationKey> keyOut = new Output<CollationKey>();
    CollationKey key;
    if (!getCollationKey(norm, fileLine, s, keyOut)) {
        return false;
    }
    key = keyOut.value;
    int order = coll.compare(prevString, s);
    if (order != expectedOrder) {
        logln(fileTestName);
        logln(prevFileLine);
        logln(fileLine);
        logln(printCollationKey(prevKey));
        logln(printCollationKey(key));
        errln("line " + fileLineNumber + " Collator(" + norm + ").compare(previous, current) wrong order: " + order + " != " + expectedOrder);
        return false;
    }
    order = coll.compare(s, prevString);
    if (order != -expectedOrder) {
        logln(fileTestName);
        logln(prevFileLine);
        logln(fileLine);
        logln(printCollationKey(prevKey));
        logln(printCollationKey(key));
        errln("line " + fileLineNumber + " Collator(" + norm + ").compare(current, previous) wrong order: " + order + " != " + -expectedOrder);
        return false;
    }
    order = prevKey.compareTo(key);
    if (order != expectedOrder) {
        logln(fileTestName);
        logln(prevFileLine);
        logln(fileLine);
        logln(printCollationKey(prevKey));
        logln(printCollationKey(key));
        errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey(previous, current).compareTo() wrong order: " + order + " != " + expectedOrder);
        return false;
    }
    boolean collHasCaseLevel = ((RuleBasedCollator) coll).isCaseLevel();
    int level = getDifferenceLevel(prevKey, key, order, collHasCaseLevel);
    if (order != Collation.EQUAL && expectedLevel != Collation.NO_LEVEL) {
        if (level != expectedLevel) {
            logln(fileTestName);
            logln(prevFileLine);
            logln(fileLine);
            logln(printCollationKey(prevKey));
            logln(printCollationKey(key));
            errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey(previous, current).compareTo()=" + order + " wrong level: " + level + " != " + expectedLevel);
            return false;
        }
    }
    // If either string contains U+FFFE, then their sort keys must compare the same as
    // the merged sort keys of each string's between-FFFE segments.
    // 
    // It is not required that
    // sortkey(str1 + "\uFFFE" + str2) == mergeSortkeys(sortkey(str1), sortkey(str2))
    // only that those two methods yield the same order.
    // 
    // Use bit-wise OR so that getMergedCollationKey() is always called for both strings.
    Output<CollationKey> outPrevKey = new Output<CollationKey>(prevKey);
    Output<CollationKey> outKey = new Output<CollationKey>(key);
    if (getMergedCollationKey(prevString, outPrevKey) | getMergedCollationKey(s, outKey)) {
        prevKey = outPrevKey.value;
        key = outKey.value;
        order = prevKey.compareTo(key);
        if (order != expectedOrder) {
            logln(fileTestName);
            errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey" + "(previous, current segments between U+FFFE)).merge().compareTo() wrong order: " + order + " != " + expectedOrder);
            logln(prevFileLine);
            logln(fileLine);
            logln(printCollationKey(prevKey));
            logln(printCollationKey(key));
            return false;
        }
        int mergedLevel = getDifferenceLevel(prevKey, key, order, collHasCaseLevel);
        if (order != Collation.EQUAL && expectedLevel != Collation.NO_LEVEL) {
            if (mergedLevel != level) {
                logln(fileTestName);
                errln("line " + fileLineNumber + " Collator(" + norm + ").getCollationKey" + "(previous, current segments between U+FFFE)).merge().compareTo()=" + order + " wrong level: " + mergedLevel + " != " + level);
                logln(prevFileLine);
                logln(fileLine);
                logln(printCollationKey(prevKey));
                logln(printCollationKey(key));
                return false;
            }
        }
    }
    return true;
}
Also used : RuleBasedCollator(android.icu.text.RuleBasedCollator) CollationKey(android.icu.text.CollationKey) RawCollationKey(android.icu.text.RawCollationKey) Output(android.icu.util.Output)

Example 2 with Output

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

the class CollatorServiceShim method makeInstance.

// Ported from C++ Collator::makeInstance().
private static final Collator makeInstance(ULocale desiredLocale) {
    Output<ULocale> validLocale = new Output<ULocale>(ULocale.ROOT);
    CollationTailoring t = CollationLoader.loadTailoring(desiredLocale, validLocale);
    return new RuleBasedCollator(t, validLocale.value);
}
Also used : ULocale(android.icu.util.ULocale) Output(android.icu.util.Output) CollationTailoring(android.icu.impl.coll.CollationTailoring)

Example 3 with Output

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

the class DateIntervalFormatTest method TestDateIntervalFormatCoverage.

@Test
public void TestDateIntervalFormatCoverage() throws Exception {
    long date1 = 1299090600000L;
    long date2 = 1299115800000L;
    DateInterval dtitv = new DateInterval(date1, date2);
    DateIntervalFormat dtitvfmt = DateIntervalFormat.getInstance("MMMd", Locale.ENGLISH);
    DateIntervalInfo dtintinf = new DateIntervalInfo(ULocale.ENGLISH);
    // Check the default private constructor
    checkDefaultPrivateConstructor(DateIntervalFormat.class);
    // Check clone
    DateIntervalFormat dtitvfmtClone = (DateIntervalFormat) dtitvfmt.clone();
    assertEquals("DateIntervalFormat.clone() failed", dtitvfmt.format(dtitv), dtitvfmtClone.format(dtitv));
    // Coverage for getInstance
    assertNotNull("Expected DateIntervalFormat object", DateIntervalFormat.getInstance("MMMd", dtintinf));
    assertNotNull("Expected DateIntervalFormat object", DateIntervalFormat.getInstance("MMMdHHmm", Locale.ENGLISH, dtintinf));
    // Coverage for parseObject. Exception expected.
    try {
        dtitvfmt.parseObject("", new ParsePosition(0));
        errln("Exception was expected when calling DateIntervalFormat.parseObject()");
    } catch (Exception e) {
    /* No op */
    }
    // Check getPatterns()
    Output<String> secondPart = new Output<String>();
    Calendar fromCalendar = Calendar.getInstance(Locale.ENGLISH);
    fromCalendar.set(2016, 5, 22);
    Calendar toCalendar = Calendar.getInstance(Locale.ENGLISH);
    toCalendar.set(2016, 5, 23);
    assertEquals("Date interval pattern mismatch.", dtitvfmt.getPatterns(fromCalendar, toCalendar, secondPart), "MMM d – ");
    assertEquals("Date interval pattern mismatch.", secondPart.value, "d");
}
Also used : DateIntervalInfo(android.icu.text.DateIntervalInfo) Output(android.icu.util.Output) Calendar(android.icu.util.Calendar) DateIntervalFormat(android.icu.text.DateIntervalFormat) DateInterval(android.icu.util.DateInterval) ParseException(java.text.ParseException) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Example 4 with Output

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

the class LocaleValidityChecker method isValidU.

/**
 * @param locale
 * @param datatype
 * @param extension
 * @param where
 * @return
 */
private boolean isValidU(ULocale locale, Datatype datatype, String extensionString, Where where) {
    String key = "";
    int typeCount = 0;
    ValueType valueType = null;
    SpecialCase specialCase = null;
    StringBuilder prefix = new StringBuilder();
    Set<String> seen = new HashSet<String>();
    StringBuilder tBuffer = datatype == Datatype.t ? new StringBuilder() : null;
    for (String subtag : SEPARATOR.split(extensionString)) {
        if (subtag.length() == 2 && (tBuffer == null || subtag.charAt(1) <= '9')) {
            // if we have accumulated a t buffer, check that first
            if (tBuffer != null) {
                // Check t buffer. Empty after 't' is ok.
                if (tBuffer.length() != 0 && !isValidLocale(tBuffer.toString(), where)) {
                    return false;
                }
                tBuffer = null;
            }
            key = KeyTypeData.toBcpKey(subtag);
            if (key == null) {
                return where.set(datatype, subtag);
            }
            if (!allowsDeprecated && KeyTypeData.isDeprecated(key)) {
                return where.set(datatype, key);
            }
            valueType = KeyTypeData.getValueType(key);
            specialCase = SpecialCase.get(key);
            typeCount = 0;
        } else if (tBuffer != null) {
            if (tBuffer.length() != 0) {
                tBuffer.append('-');
            }
            tBuffer.append(subtag);
        } else {
            ++typeCount;
            switch(valueType) {
                case single:
                    if (typeCount > 1) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    break;
                case incremental:
                    if (typeCount == 1) {
                        prefix.setLength(0);
                        prefix.append(subtag);
                    } else {
                        prefix.append('-').append(subtag);
                        subtag = prefix.toString();
                    }
                    break;
                case multiple:
                    if (typeCount == 1) {
                        seen.clear();
                    }
                    break;
            }
            switch(specialCase) {
                case anything:
                    continue;
                case codepoints:
                    try {
                        if (Integer.parseInt(subtag, 16) > 0x10FFFF) {
                            return where.set(datatype, key + "-" + subtag);
                        }
                    } catch (NumberFormatException e) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    continue;
                case reorder:
                    boolean newlyAdded = seen.add(subtag.equals("zzzz") ? "others" : subtag);
                    if (!newlyAdded || !isScriptReorder(subtag)) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    continue;
                case subdivision:
                    if (!isSubdivision(locale, subtag)) {
                        return where.set(datatype, key + "-" + subtag);
                    }
                    continue;
                case rgKey:
                    if (subtag.length() < 6 || !subtag.endsWith("zzzz")) {
                        return where.set(datatype, subtag);
                    }
                    if (!isValid(Datatype.region, subtag.substring(0, subtag.length() - 4), where)) {
                        return false;
                    }
                    continue;
            }
            // en-u-sd-usca
            // en-US-u-sd-usca
            Output<Boolean> isKnownKey = new Output<Boolean>();
            Output<Boolean> isSpecialType = new Output<Boolean>();
            String type = KeyTypeData.toBcpType(key, subtag, isKnownKey, isSpecialType);
            if (type == null) {
                return where.set(datatype, key + "-" + subtag);
            }
            if (!allowsDeprecated && KeyTypeData.isDeprecated(key, subtag)) {
                return where.set(datatype, key + "-" + subtag);
            }
        }
    }
    // Check t buffer. Empty after 't' is ok.
    if (tBuffer != null && tBuffer.length() != 0 && !isValidLocale(tBuffer.toString(), where)) {
        return false;
    }
    return true;
}
Also used : ValueType(android.icu.impl.locale.KeyTypeData.ValueType) Output(android.icu.util.Output) HashSet(java.util.HashSet)

Example 5 with Output

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

the class TimeZoneFormatTest method TestParse.

@Test
public void TestParse() {
    final Object[][] DATA = { // parseOptions            expected            outpos      time type
    { "Z", 0, "en_US", Style.ISO_EXTENDED_FULL, null, "Etc/GMT", 1, TimeType.UNKNOWN }, { "Z", 0, "en_US", Style.SPECIFIC_LONG, null, "Etc/GMT", 1, TimeType.UNKNOWN }, { "Zambia time", 0, "en_US", Style.ISO_EXTENDED_FULL, EnumSet.of(ParseOption.ALL_STYLES), "Etc/GMT", 1, TimeType.UNKNOWN }, { "Zambia time", 0, "en_US", Style.GENERIC_LOCATION, null, "Africa/Lusaka", 11, TimeType.UNKNOWN }, { "Zambia time", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, EnumSet.of(ParseOption.ALL_STYLES), "Africa/Lusaka", 11, TimeType.UNKNOWN }, { "+00:00", 0, "en_US", Style.ISO_EXTENDED_FULL, null, "Etc/GMT", 6, TimeType.UNKNOWN }, { "-01:30:45", 0, "en_US", Style.ISO_EXTENDED_FULL, null, "GMT-01:30:45", 9, TimeType.UNKNOWN }, { "-7", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, null, "GMT-07:00", 2, TimeType.UNKNOWN }, { "-2222", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, null, "GMT-22:22", 5, TimeType.UNKNOWN }, { "-3333", 0, "en_US", Style.ISO_BASIC_LOCAL_FULL, null, "GMT-03:33", 4, TimeType.UNKNOWN }, { "XXX+01:30YYY", 3, "en_US", Style.LOCALIZED_GMT, null, "GMT+01:30", 9, TimeType.UNKNOWN }, { "GMT0", 0, "en_US", Style.SPECIFIC_SHORT, null, "Etc/GMT", 3, TimeType.UNKNOWN }, { "EST", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/New_York", 3, TimeType.STANDARD }, { "ESTx", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/New_York", 3, TimeType.STANDARD }, { "EDTx", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/New_York", 3, TimeType.DAYLIGHT }, { "EST", 0, "en_US", Style.SPECIFIC_LONG, null, null, 0, TimeType.UNKNOWN }, { "EST", 0, "en_US", Style.SPECIFIC_LONG, EnumSet.of(ParseOption.ALL_STYLES), "America/New_York", 3, TimeType.STANDARD }, { "EST", 0, "en_CA", Style.SPECIFIC_SHORT, null, "America/Toronto", 3, TimeType.STANDARD }, { "CST", 0, "en_US", Style.SPECIFIC_SHORT, null, "America/Chicago", 3, TimeType.STANDARD }, { "CST", 0, "en_GB", Style.SPECIFIC_SHORT, null, null, 0, TimeType.UNKNOWN }, { "CST", 0, "en_GB", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "America/Chicago", 3, TimeType.STANDARD }, { "--CST--", 2, "en_GB", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "America/Chicago", 5, TimeType.STANDARD }, { "CST", 0, "zh_CN", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "Asia/Shanghai", 3, TimeType.STANDARD }, { "AEST", 0, "en_AU", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "Australia/Sydney", 4, TimeType.STANDARD }, { "AST", 0, "ar_SA", Style.SPECIFIC_SHORT, EnumSet.of(ParseOption.TZ_DATABASE_ABBREVIATIONS), "Asia/Riyadh", 3, TimeType.STANDARD }, { "AQTST", 0, "en", Style.SPECIFIC_LONG, null, null, 0, TimeType.UNKNOWN }, { "AQTST", 0, "en", Style.SPECIFIC_LONG, EnumSet.of(ParseOption.ALL_STYLES), null, 0, TimeType.UNKNOWN }, { "AQTST", 0, "en", Style.SPECIFIC_LONG, EnumSet.of(ParseOption.ALL_STYLES, ParseOption.TZ_DATABASE_ABBREVIATIONS), "Asia/Aqtobe", 5, TimeType.DAYLIGHT }, { "hora de verano británica", 0, "es", Style.SPECIFIC_LONG, null, "Europe/London", 24, TimeType.DAYLIGHT } };
    for (Object[] test : DATA) {
        String text = (String) test[0];
        int inPos = (Integer) test[1];
        ULocale loc = new ULocale((String) test[2]);
        Style style = (Style) test[3];
        EnumSet<ParseOption> options = (EnumSet<ParseOption>) test[4];
        String expID = (String) test[5];
        int expPos = (Integer) test[6];
        TimeType expType = (TimeType) test[7];
        TimeZoneFormat tzfmt = TimeZoneFormat.getInstance(loc);
        Output<TimeType> timeType = new Output<TimeType>(TimeType.UNKNOWN);
        ParsePosition pos = new ParsePosition(inPos);
        TimeZone tz = tzfmt.parse(style, text, pos, options, timeType);
        String errMsg = null;
        if (tz == null) {
            if (expID != null) {
                errMsg = "Parse failure - expected: " + expID;
            }
        } else if (!tz.getID().equals(expID)) {
            errMsg = "Time zone ID: " + tz.getID() + " - expected: " + expID;
        } else if (pos.getIndex() != expPos) {
            errMsg = "Parsed pos: " + pos.getIndex() + " - expected: " + expPos;
        } else if (timeType.value != expType) {
            errMsg = "Time type: " + timeType + " - expected: " + expType;
        }
        if (errMsg != null) {
            errln("Fail: " + errMsg + " [text=" + text + ", pos=" + inPos + ", locale=" + loc + ", style=" + style + "]");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) EnumSet(java.util.EnumSet) TimeZoneFormat(android.icu.text.TimeZoneFormat) TimeType(android.icu.text.TimeZoneFormat.TimeType) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SimpleTimeZone(android.icu.util.SimpleTimeZone) TimeZone(android.icu.util.TimeZone) BasicTimeZone(android.icu.util.BasicTimeZone) Output(android.icu.util.Output) Style(android.icu.text.TimeZoneFormat.Style) ParseOption(android.icu.text.TimeZoneFormat.ParseOption) ParsePosition(java.text.ParsePosition) Test(org.junit.Test)

Aggregations

Output (android.icu.util.Output)12 ULocale (android.icu.util.ULocale)6 Test (org.junit.Test)6 TimeZone (android.icu.util.TimeZone)5 TimeType (android.icu.text.TimeZoneFormat.TimeType)4 BasicTimeZone (android.icu.util.BasicTimeZone)4 TimeZoneFormat (android.icu.text.TimeZoneFormat)3 SimpleTimeZone (android.icu.util.SimpleTimeZone)3 ParsePosition (java.text.ParsePosition)3 Date (java.util.Date)3 EnumSet (java.util.EnumSet)3 PluralRules (android.icu.text.PluralRules)2 Style (android.icu.text.TimeZoneFormat.Style)2 Calendar (android.icu.util.Calendar)2 AttributedString (java.text.AttributedString)2 HashSet (java.util.HashSet)2 DayPeriodRules (android.icu.impl.DayPeriodRules)1 TZDBTimeZoneNames (android.icu.impl.TZDBTimeZoneNames)1 GenericMatchInfo (android.icu.impl.TimeZoneGenericNames.GenericMatchInfo)1 CollationTailoring (android.icu.impl.coll.CollationTailoring)1