Search in sources :

Example 1 with DataMap

use of android.icu.dev.test.TestDataModule.DataMap in project j2objc by google.

the class DataDrivenCalendarTest method testOps.

private void testOps(TestDataModule.TestData testData, DataMap settings) {
    // Get 'from' time
    CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet(), paramsSet = new CalendarFieldsSet(), diffSet = new CalendarFieldsSet();
    // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
    // Start the processing
    int n = 0;
    long fromDate = 0;
    long toDate = 0;
    boolean useDate = false;
    for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
        ++n;
        DataMap currentCase = (DataMap) iter.next();
        String caseString = "[case " + n + "]";
        // build to calendar
        // Headers { "locale","from","operation","params","to" }
        // #1 locale
        String param = "locale";
        String locale;
        String testSetting = currentCase.getString(param);
        locale = testSetting;
        ULocale loc = new ULocale(locale);
        Calendar fromCalendar = Calendar.getInstance(loc);
        fromSet.clear();
        // #2 'from' info
        param = "from";
        String from = testSetting = currentCase.getString(param);
        if (from.startsWith(kMILLIS)) {
            useDate = true;
            fromDate = Long.parseLong(from.substring(kMILLIS.length()));
        } else {
            fromSet.parseFrom(testSetting);
        }
        // System.err.println("fromset: ["+testSetting+"] >> " + fromSet);
        // #4 'operation' info
        param = "operation";
        String operation = testSetting = currentCase.getString(param);
        paramsSet.clear();
        // #3 'params' info
        param = "params";
        String paramsData = testSetting = currentCase.getString(param);
        // parse with inheritance.
        paramsSet.parseFrom(paramsData);
        // System.err.println("paramsSet: ["+testSetting+"] >> " + paramsSet);
        toSet.clear();
        // #4 'to' info
        param = "to";
        String to = testSetting = currentCase.getString(param);
        if (to.startsWith(kMILLIS)) {
            useDate = true;
            toDate = Long.parseLong(to.substring(kMILLIS.length()));
        } else {
            toSet.parseFrom(testSetting, fromSet);
        }
        // toSet.parseFrom(testSetting, fromSet); // parse with inheritance.
        // System.err.println("toSet: ["+testSetting+"] >> " + toSet);
        String caseContentsString = locale + ":  from " + from + ": " + operation + " [[[ " + paramsSet + " ]]]   >>> " + to;
        logln(caseString + ": " + caseContentsString);
        // / prepare calendar
        if (useDate) {
            fromCalendar.setTimeInMillis(fromDate);
        } else {
            fromSet.setOnCalendar(fromCalendar);
        }
        // from calendar:  'starting date'
        diffSet.clear();
        // Is the calendar sane after being set?
        if (!fromSet.matches(fromCalendar, diffSet)) {
            String diffs = diffSet.diffFrom(fromSet);
            errln((String) "FAIL: " + caseString + ", SET SOURCE calendar was not set: Differences: " + diffs);
        } else {
            // verifies that the requested fields were set.
            logln(" " + caseString + " SET SOURCE calendar match.");
        }
        // to calendar - copy of from calendar
        Calendar toCalendar = (Calendar) fromCalendar.clone();
        // / perform op on 'to calendar'
        for (int q = 0; q < paramsSet.fieldCount(); q++) {
            if (paramsSet.isSet(q)) {
                if (operation.equals(kROLL)) {
                    toCalendar.roll(q, paramsSet.get(q));
                } else if (operation.equals(kADD)) {
                    toCalendar.add(q, paramsSet.get(q));
                } else {
                    errln(caseString + " FAIL: unknown operation " + operation);
                }
                logln(operation + " of " + paramsSet.get(q));
            }
        }
        // now - what's the result?
        diffSet.clear();
        if (useDate) {
            if (toCalendar.getTimeInMillis() == toDate) {
                logln(caseString + " SUCCESS: got=expected=" + toDate);
                logln("PASS: " + caseString + " matched! ");
            } else {
                // failing because of this.
                if ((caseString.equals("[case 31]") || caseString.equals("[case 36]")) && TimeZone.getDefaultTimeZoneType() == TimeZone.TIMEZONE_JDK) {
                    logln(caseString + " FAIL(expected): got " + toCalendar.getTimeInMillis() + "  expected " + toDate);
                } else {
                    errln(caseString + " FAIL: got " + toCalendar.getTimeInMillis() + "  expected " + toDate);
                }
            }
        } else if (!toSet.matches(toCalendar, diffSet)) {
            String diffs = diffSet.diffFrom(toSet);
            errln((String) "FAIL: " + caseString + " - , " + caseContentsString + " Differences: " + diffs);
        } else {
            logln("PASS: " + caseString + " matched! ");
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) Iterator(java.util.Iterator) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) DataMap(android.icu.dev.test.TestDataModule.DataMap)

Example 2 with DataMap

use of android.icu.dev.test.TestDataModule.DataMap in project j2objc by google.

the class DataDrivenFormatTest method formatTest.

/* (non-Javadoc)
     * @see android.icu.dev.test.ModuleTest#processModules()
     */
@Test
@Parameters(method = "getTestData")
public void formatTest(TestDataPair pair) {
    TestData td = pair.td;
    DataMap settings = pair.dm;
    String type = settings.getString("Type");
    if (type.equals("date_format")) {
        testConvertDate(td, settings, true);
    } else if (type.equals("date_parse")) {
        testConvertDate(td, settings, false);
    } else {
        errln("Unknown type: " + type);
    }
}
Also used : TestData(android.icu.dev.test.TestDataModule.TestData) DataMap(android.icu.dev.test.TestDataModule.DataMap) Parameters(junitparams.Parameters) Test(org.junit.Test) ModuleTest(android.icu.dev.test.ModuleTest)

Example 3 with DataMap

use of android.icu.dev.test.TestDataModule.DataMap in project j2objc by google.

the class DataDrivenFormatTest method testConvertDate.

private void testConvertDate(TestDataModule.TestData testData, DataMap settings, boolean fmt) {
    DateFormat basicFmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
    int n = 0;
    for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
        ++n;
        long now = System.currentTimeMillis();
        DataMap currentCase = (DataMap) iter.next();
        String caseString = "[" + testData.getName() + "#" + n + (fmt ? "format" : "parse") + "]";
        String locale = currentCase.getString("locale");
        String zone = currentCase.getString("zone");
        String spec = currentCase.getString("spec");
        String date = currentCase.getString("date");
        String str = currentCase.getString("str");
        Date fromDate = null;
        boolean useDate = false;
        ULocale loc = new ULocale(locale);
        String pattern = null;
        // boolean usePattern = false;
        DateFormat format = null;
        DateTimeStyleSet styleSet;
        CalendarFieldsSet fromSet = null;
        // parse 'spec'  - either 'PATTERN=yy mm dd' or 'DATE=x,TIME=y'
        if (spec.startsWith(kPATTERN)) {
            pattern = spec.substring(kPATTERN.length());
            // usePattern = true;
            format = new SimpleDateFormat(pattern, loc);
        } else {
            styleSet = new DateTimeStyleSet();
            styleSet.parseFrom(spec);
            format = DateFormat.getDateTimeInstance(styleSet.getDateStyle(), styleSet.getTimeStyle(), loc);
        }
        Calendar cal = Calendar.getInstance(loc);
        if (zone.length() > 0) {
            TimeZone tz = TimeZone.getFrozenTimeZone(zone);
            cal.setTimeZone(tz);
            format.setTimeZone(tz);
        }
        // parse 'date' - either 'MILLIS=12345' or  a CalendarFieldsSet
        if (date.startsWith(kMILLIS)) {
            useDate = true;
            fromDate = new Date(Long.parseLong(date.substring(kMILLIS.length())));
        } else if (date.startsWith(kRELATIVE_MILLIS)) {
            useDate = true;
            fromDate = new Date(now + Long.parseLong(date.substring(kRELATIVE_MILLIS.length())));
        } else if (date.startsWith(kRELATIVE_ADD)) {
            // "add" is a string indicating which fields to add
            String add = date.substring(kRELATIVE_ADD.length());
            CalendarFieldsSet addSet = new CalendarFieldsSet();
            addSet.parseFrom(add);
            useDate = true;
            cal.clear();
            cal.setTimeInMillis(now);
            // / perform op on 'to calendar'
            for (int q = 0; q < addSet.fieldCount(); q++) {
                if (addSet.isSet(q)) {
                    if (q == Calendar.DATE) {
                        cal.add(q, addSet.get(q));
                    } else {
                        cal.set(q, addSet.get(q));
                    }
                }
            }
            fromDate = cal.getTime();
        } else {
            fromSet = new CalendarFieldsSet();
            fromSet.parseFrom(date);
        }
        // run the test
        if (fmt) {
            StringBuffer output = new StringBuffer();
            cal.clear();
            FieldPosition pos = new FieldPosition(0);
            if (useDate) {
                output = format.format(fromDate, output, pos);
            } else {
                fromSet.setOnCalendar(cal);
                format.format(cal, output, pos);
            }
            if (output.toString().equals(str)) {
                logln(caseString + " Success - strings match: " + output);
            } else {
                errln(caseString + " FAIL: got " + output + " expected " + str);
            }
        } else {
            // parse
            cal.clear();
            ParsePosition pos = new ParsePosition(0);
            format.parse(str, cal, pos);
            if (useDate) {
                Date gotDate = cal.getTime();
                if (gotDate.equals(fromDate)) {
                    logln(caseString + " SUCCESS: got=parse=" + str);
                } else {
                    errln(caseString + " FAIL: parsed " + str + " but got " + basicFmt.format(gotDate) + " - " + gotDate + "  expected " + basicFmt.format(fromDate));
                }
            } else {
                CalendarFieldsSet diffSet = new CalendarFieldsSet();
                if (!fromSet.matches(cal, diffSet)) {
                    String diffs = diffSet.diffFrom(fromSet);
                    errln(caseString + " FAIL:  differences: " + diffs);
                } else {
                    logln(caseString + " SUCCESS: got=parse: " + str + " - " + fromSet.toString());
                }
            }
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) FieldPosition(java.text.FieldPosition) Date(java.util.Date) DataMap(android.icu.dev.test.TestDataModule.DataMap) TimeZone(android.icu.util.TimeZone) DateTimeStyleSet(android.icu.dev.test.util.DateTimeStyleSet) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) Iterator(java.util.Iterator) SimpleDateFormat(android.icu.text.SimpleDateFormat) ParsePosition(java.text.ParsePosition)

Example 4 with DataMap

use of android.icu.dev.test.TestDataModule.DataMap in project j2objc by google.

the class ModuleTest method getTestData.

public static List<TestDataPair> getTestData(String moduleLocation, String moduleName) throws Exception {
    List<TestDataPair> list = new ArrayList<TestDataPair>();
    TestDataModule m = ModuleTest.loadTestData(moduleLocation, moduleName);
    Iterator<TestData> tIter = m.getTestDataIterator();
    while (tIter.hasNext()) {
        TestData t = tIter.next();
        for (Iterator siter = t.getSettingsIterator(); siter.hasNext(); ) {
            DataMap settings = (DataMap) siter.next();
            list.add(new TestDataPair(t, settings));
        }
    }
    return list;
}
Also used : TestData(android.icu.dev.test.TestDataModule.TestData) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) DataMap(android.icu.dev.test.TestDataModule.DataMap)

Example 5 with DataMap

use of android.icu.dev.test.TestDataModule.DataMap in project j2objc by google.

the class DataDrivenCalendarTest method testConvert.

private void testConvert(TestDataModule.TestData testData, DataMap settings, boolean forward) {
    Calendar toCalendar = null;
    // build to calendar
    String testSetting = settings.getString("ToCalendar");
    ULocale loc = new ULocale(testSetting);
    toCalendar = Calendar.getInstance(loc);
    CalendarFieldsSet fromSet = new CalendarFieldsSet(), toSet = new CalendarFieldsSet();
    // DateFormat fmt = new SimpleDateFormat("EEE MMM dd yyyy / YYYY'-W'ww-ee");
    // Start the processing
    int n = 0;
    for (Iterator iter = testData.getDataIterator(); iter.hasNext(); ) {
        ++n;
        DataMap currentCase = (DataMap) iter.next();
        String caseString = "[" + testData.getName() + "#" + n + " " + "]";
        String locale = testSetting = currentCase.getString("locale");
        ULocale fromLoc = new ULocale(testSetting);
        Calendar fromCalendar = Calendar.getInstance(fromLoc);
        fromSet.clear();
        toSet.clear();
        String from = currentCase.getString("from");
        fromSet.parseFrom(from);
        String to = currentCase.getString("to");
        toSet.parseFrom(to, fromSet);
        // now, do it.
        if (forward) {
            logln(caseString + " " + locale + "/" + from + " >>> " + loc + "/" + to);
            testConvert(caseString, fromSet, fromCalendar, toSet, toCalendar, forward);
        } else {
            logln(caseString + " " + locale + "/" + from + " <<< " + loc + "/" + to);
            testConvert(caseString, toSet, toCalendar, fromSet, fromCalendar, forward);
        }
    }
}
Also used : ULocale(android.icu.util.ULocale) Calendar(android.icu.util.Calendar) Iterator(java.util.Iterator) CalendarFieldsSet(android.icu.dev.test.util.CalendarFieldsSet) DataMap(android.icu.dev.test.TestDataModule.DataMap)

Aggregations

DataMap (android.icu.dev.test.TestDataModule.DataMap)6 Iterator (java.util.Iterator)4 TestData (android.icu.dev.test.TestDataModule.TestData)3 CalendarFieldsSet (android.icu.dev.test.util.CalendarFieldsSet)3 Calendar (android.icu.util.Calendar)3 ULocale (android.icu.util.ULocale)3 ModuleTest (android.icu.dev.test.ModuleTest)2 Parameters (junitparams.Parameters)2 Test (org.junit.Test)2 DateTimeStyleSet (android.icu.dev.test.util.DateTimeStyleSet)1 DateFormat (android.icu.text.DateFormat)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 TimeZone (android.icu.util.TimeZone)1 FieldPosition (java.text.FieldPosition)1 ParsePosition (java.text.ParsePosition)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1