Search in sources :

Example 21 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestIsSingleField.

/* Tests the method
     *    public static boolean isSingleField(String skeleton)
     */
@SuppressWarnings("static-access")
@Test
public void TestIsSingleField() {
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance();
    String[] cases = { " ", "m", "mm", "md", "mmd", "mmdd" };
    boolean[] results = { true, true, true, false, false, false };
    for (int i = 0; i < cases.length; i++) {
        if (dtpg.isSingleField(cases[i]) != results[i]) {
            errln("DateTimePatternGenerator.isSingleField(String skeleton) " + "did not return as expected. Value passed was " + cases[i] + " but got back " + dtpg.isSingleField(cases[i]));
        }
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 22 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestJavaLocale.

@Test
public void TestJavaLocale() {
    DateTimePatternGenerator genUloc = DateTimePatternGenerator.getInstance(ULocale.GERMANY);
    DateTimePatternGenerator genLoc = DateTimePatternGenerator.getInstance(Locale.GERMANY);
    final String pat = "yMdHms";
    String patUloc = genUloc.getBestPattern(pat);
    String patLoc = genLoc.getBestPattern(pat);
    assertEquals("German pattern 'yMdHms' - getInstance with Java Locale", patUloc, patLoc);
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 23 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestSetAppendItemFormat.

/* Tests the method
     *        public String setAppendItemFormat(int field)
     */
@Test
public void TestSetAppendItemFormat() {
    DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance();
    String[] cases = { "d", "u", "m", "m", "y" };
    for (int i = 0; i < cases.length; i++) {
        dtpg.setAppendItemFormat(i, cases[i]);
        if (!dtpg.getAppendItemFormat(i).equals(cases[i])) {
            errln("DateTimePatternGenerator.getAppendItemFormat(int field) " + "did not return as expected. Value set at " + i + " was " + cases[i] + " but got back " + dtpg.getAppendItemFormat(i));
        }
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 24 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestAllFieldPatterns.

@Test
public void TestAllFieldPatterns() {
    String[] localeNames = { "root", "root@calendar=japanese", "root@calendar=chinese", "en", "en@calendar=japanese", "en@calendar=chinese" };
    final AllFieldsTestItem[] testItems = { // era
    new AllFieldsTestItem('G', new int[] { 1, 2, 3, 4, 5 }, "G"), // year
    new AllFieldsTestItem('y', new int[] { 1, 2, 3, 4 }, "yU"), // year for week of year
    new AllFieldsTestItem('Y', new int[] { 1, 2, 3, 4 }, "Y"), // extended year
    new AllFieldsTestItem('u', new int[] { 1, 2, 3, 4, 5 }, "yuU"), // cyclic year name
    new AllFieldsTestItem('U', new int[] { 1, 2, 3, 4, 5 }, "yU"), // x
    new AllFieldsTestItem('Q', new int[] { 1, 2, 3, 4 }, "Qq"), // standalone
    new AllFieldsTestItem('q', new int[] { 1, 2, 3, 4 }, "Qq"), // x
    new AllFieldsTestItem('M', new int[] { 1, 2, 3, 4, 5 }, "ML"), // standalone
    new AllFieldsTestItem('L', new int[] { 1, 2, 3, 4, 5 }, "ML"), // week of year
    new AllFieldsTestItem('w', new int[] { 1, 2 }, "w"), // week of month
    new AllFieldsTestItem('W', new int[] { 1 }, "W"), // day of month
    new AllFieldsTestItem('d', new int[] { 1, 2 }, "d"), // day of year
    new AllFieldsTestItem('D', new int[] { 1, 2, 3 }, "D"), // day of week in month
    new AllFieldsTestItem('F', new int[] { 1 }, "F"), // modified julian day
    new AllFieldsTestItem('g', new int[] { 7 }, "g"), // day of week
    new AllFieldsTestItem('E', new int[] { 1, 2, 3, 4, 5, 6 }, "Eec"), // local day of week
    new AllFieldsTestItem('e', new int[] { 1, 2, 3, 4, 5, 6 }, "Eec"), // standalone local day of week
    new AllFieldsTestItem('c', new int[] { 1, 2, 3, 4, 5, 6 }, "Eec"), // 12 (1-12)
    new AllFieldsTestItem('h', new int[] { 1, 2 }, "hK"), // 24 (0-23)
    new AllFieldsTestItem('H', new int[] { 1, 2 }, "Hk"), // 12 (0-11)
    new AllFieldsTestItem('K', new int[] { 1, 2 }, "hK"), // 24 (1-24)
    new AllFieldsTestItem('k', new int[] { 1, 2 }, "Hk"), // locale default
    new AllFieldsTestItem('j', new int[] { 1, 2 }, "hHKk"), // x
    new AllFieldsTestItem('m', new int[] { 1, 2 }, "m"), // x
    new AllFieldsTestItem('s', new int[] { 1, 2 }, "s"), // fractional second
    new AllFieldsTestItem('S', new int[] { 1, 2, 3, 4 }, "S"), // milliseconds in day
    new AllFieldsTestItem('A', new int[] { 8 }, "A"), // x
    new AllFieldsTestItem('z', new int[] { 1, 2, 3, 4 }, "z"), // x
    new AllFieldsTestItem('Z', new int[] { 1, 2, 3, 4, 5 }, "Z"), // x
    new AllFieldsTestItem('O', new int[] { 1, 4 }, "O"), // x
    new AllFieldsTestItem('v', new int[] { 1, 4 }, "v"), // x
    new AllFieldsTestItem('V', new int[] { 1, 2, 3, 4 }, "V"), // x
    new AllFieldsTestItem('X', new int[] { 1, 2, 3, 4, 5 }, "X"), // x
    new AllFieldsTestItem('x', new int[] { 1, 2, 3, 4, 5 }, "x") };
    final int FIELD_LENGTH_MAX = 8;
    for (String localeName : localeNames) {
        ULocale uloc = new ULocale(localeName);
        DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(uloc);
        for (AllFieldsTestItem testItem : testItems) {
            char[] skelBuf = new char[FIELD_LENGTH_MAX];
            for (int chrIndx = 0; chrIndx < FIELD_LENGTH_MAX; chrIndx++) {
                skelBuf[chrIndx] = testItem.patternChar;
            }
            for (int lenIndx = 0; lenIndx < testItem.fieldLengths.length; lenIndx++) {
                int skelLen = testItem.fieldLengths[lenIndx];
                if (skelLen > FIELD_LENGTH_MAX) {
                    continue;
                }
                ;
                String skeleton = new String(skelBuf, 0, skelLen);
                String pattern = dtpgen.getBestPattern(skeleton);
                if (pattern.length() <= 0) {
                    errln("DateTimePatternGenerator getBestPattern for locale " + localeName + ", skeleton " + skeleton + ", produces 0-length pattern");
                } else {
                    // test that resulting pattern has at least one char in mustIncludeOneOf
                    boolean inQuoted = false;
                    int patIndx, patLen = pattern.length();
                    for (patIndx = 0; patIndx < patLen; patIndx++) {
                        char c = pattern.charAt(patIndx);
                        if (c == '\'') {
                            inQuoted = !inQuoted;
                        } else if (!inQuoted && c <= 'z' && c >= 'A') {
                            if (testItem.mustIncludeOneOf.indexOf(c) >= 0) {
                                break;
                            }
                        }
                    }
                    if (patIndx >= patLen) {
                        errln("DateTimePatternGenerator getBestPattern for locale " + localeName + ", skeleton " + skeleton + ", produces pattern without required chars: " + pattern);
                    }
                }
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Example 25 with DateTimePatternGenerator

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

the class DateTimeGeneratorTest method TestC.

@Test
public void TestC() {
    String[][] tests = { { "zh", "Cm", "Bh:mm" }, { "de", "Cm", "HH:mm" }, { "en", "Cm", "h:mm a" }, { "en-BN", "Cm", "h:mm b" }, { "gu-IN", "Cm", "h:mm B" }, { "und-IN", "Cm", "h:mm a" } };
    for (String[] test : tests) {
        DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(ULocale.forLanguageTag(test[0]));
        String skeleton = test[1];
        String pattern = gen.getBestPattern(skeleton);
        assertEquals(test[0] + "/" + skeleton, test[2], pattern);
    }
}
Also used : DateTimePatternGenerator(android.icu.text.DateTimePatternGenerator) Test(org.junit.Test)

Aggregations

DateTimePatternGenerator (android.icu.text.DateTimePatternGenerator)28 Test (org.junit.Test)27 SimpleDateFormat (android.icu.text.SimpleDateFormat)6 ULocale (android.icu.util.ULocale)6 SimpleTimeZone (android.icu.util.SimpleTimeZone)3 TimeZone (android.icu.util.TimeZone)3 Date (java.util.Date)3 LinkedHashSet (java.util.LinkedHashSet)2 Calendar (android.icu.util.Calendar)1 GregorianCalendar (android.icu.util.GregorianCalendar)1 ParsePosition (java.text.ParsePosition)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1