Search in sources :

Example 11 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer in project j2objc by google.

the class IBMCalendarTest method TestCoverage.

/**
 * Miscellaneous tests to increase coverage.
 */
@Test
public void TestCoverage() {
    // BuddhistCalendar
    BuddhistCalendar bcal = new BuddhistCalendar();
    /*int i =*/
    bcal.getMinimum(Calendar.ERA);
    bcal.add(Calendar.YEAR, 1);
    bcal.add(Calendar.MONTH, 1);
    /*Date d = */
    bcal.getTime();
    // CalendarAstronomer
    // (This class should probably be made package-private.)
    CalendarAstronomer astro = new CalendarAstronomer();
    /*String s = */
    astro.local(0);
    // ChineseCalendar
    ChineseCalendar ccal = new ChineseCalendar(TimeZone.getDefault(), Locale.getDefault());
    ccal.add(Calendar.MONTH, 1);
    ccal.add(Calendar.YEAR, 1);
    ccal.roll(Calendar.MONTH, 1);
    ccal.roll(Calendar.YEAR, 1);
    ccal.getTime();
    // ICU 2.6
    Calendar cal = Calendar.getInstance(Locale.US);
    logln(cal.toString());
    logln(cal.getDisplayName(Locale.US));
    int weekendOnset = -1;
    int weekendCease = -1;
    for (int i = Calendar.SUNDAY; i <= Calendar.SATURDAY; ++i) {
        if (cal.getDayOfWeekType(i) == Calendar.WEEKEND_ONSET) {
            weekendOnset = i;
        }
        if (cal.getDayOfWeekType(i) == Calendar.WEEKEND_CEASE) {
            weekendCease = i;
        }
    }
    // but make the call anyway for coverage reasons
    try {
        /*int x=*/
        cal.getWeekendTransition(weekendOnset);
        /*int x=*/
        cal.getWeekendTransition(weekendCease);
    } catch (IllegalArgumentException e) {
    }
    /*int x=*/
    cal.isWeekend(new Date());
    // new GregorianCalendar(ULocale)
    GregorianCalendar gcal = new GregorianCalendar(ULocale.getDefault());
    if (gcal == null) {
        errln("could not create GregorianCalendar with ULocale");
    } else {
        logln("Calendar display name: " + gcal.getDisplayName(ULocale.getDefault()));
    }
    // cover getAvailableULocales
    final ULocale[] locales = Calendar.getAvailableULocales();
    long count = locales.length;
    if (count == 0)
        errln("getAvailableULocales return empty list");
    logln("" + count + " available ulocales in Calendar.");
    // Jitterbug 4451, for coverage
    class StubCalendar extends Calendar {

        /**
         * For serialization
         */
        private static final long serialVersionUID = -4558903444622684759L;

        @Override
        protected int handleGetLimit(int field, int limitType) {
            if (limitType == Calendar.LEAST_MAXIMUM) {
                return 1;
            } else if (limitType == Calendar.GREATEST_MINIMUM) {
                return 7;
            }
            return -1;
        }

        @Override
        protected int handleComputeMonthStart(int eyear, int month, boolean useMonth) {
            if (useMonth) {
                return eyear * 365 + month * 31;
            } else {
                return eyear * 365;
            }
        }

        @Override
        protected int handleGetExtendedYear() {
            return 2017;
        }

        public void run() {
            if (Calendar.gregorianPreviousMonthLength(2000, 2) != 29) {
                errln("Year 2000 Feb should have 29 days.");
            }
            long millis = Calendar.julianDayToMillis(Calendar.MAX_JULIAN);
            if (millis != Calendar.MAX_MILLIS) {
                errln("Did not get the expected value from julianDayToMillis. Got:" + millis);
            }
            DateFormat df = handleGetDateFormat("", Locale.getDefault());
            if (!df.equals(handleGetDateFormat("", ULocale.getDefault()))) {
                errln("Calendar.handleGetDateFormat(String, Locale) should delegate to ( ,ULocale)");
            }
            if (!getType().equals("unknown")) {
                errln("Calendar.getType() should be 'unknown'");
            }
            // Tests for complete coverage of Calendar functions.
            int julianDay = Calendar.millisToJulianDay(millis - 1);
            assertEquals("Julian max day -1", julianDay, Calendar.MAX_JULIAN - 1);
            DateFormat df1 = handleGetDateFormat("GG yyyy-d:MM", "option=xyz", Locale.getDefault());
            if (!df1.equals(handleGetDateFormat("GG yyyy-d:MM", "option=xyz", ULocale.getDefault()))) {
                errln("Calendar.handleGetDateFormat(String, Locale) should delegate to ( ,ULocale)");
            }
            // Prove that the local overrides are used.
            int leastMsInDay = handleGetLimit(Calendar.MILLISECONDS_IN_DAY, Calendar.LEAST_MAXIMUM);
            assertEquals("getLimit test 1", leastMsInDay, 1);
            int maxMsInDay = handleGetLimit(Calendar.WEEK_OF_MONTH, Calendar.GREATEST_MINIMUM);
            assertEquals("getLimit test 2", 7, maxMsInDay);
            int febLeapLength = handleGetMonthLength(2020, Calendar.FEBRUARY);
            assertEquals("handleMonthLength", 31, febLeapLength);
            int exYear = handleGetExtendedYear();
            assertEquals("handleGetExtendeYear", exYear, 2017);
            int monthStart = handleComputeMonthStart(2016, 4, false);
            assertEquals("handleComputeMonthStart false", 735840, monthStart);
            monthStart = handleComputeMonthStart(2016, 4, true);
            assertEquals("handleComputeMonthStart true", 735964, monthStart);
            Calendar cal = Calendar.getInstance();
            cal.set(1980, 5, 2);
            this.setTime(cal.getTime());
            assertEquals("handleComputeFields: year set", 1980, get(YEAR));
            assertEquals("handleComputeFields: month set", 5, get(MONTH));
            assertEquals("handleComputeFields: day set", 2, get(DAY_OF_MONTH));
        }
    }
    StubCalendar stub = new StubCalendar();
    stub.run();
}
Also used : ChineseCalendar(android.icu.util.ChineseCalendar) ULocale(android.icu.util.ULocale) ChineseCalendar(android.icu.util.ChineseCalendar) TaiwanCalendar(android.icu.util.TaiwanCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) BuddhistCalendar(android.icu.util.BuddhistCalendar) Calendar(android.icu.util.Calendar) JapaneseCalendar(android.icu.util.JapaneseCalendar) GregorianCalendar(android.icu.util.GregorianCalendar) Date(java.util.Date) BuddhistCalendar(android.icu.util.BuddhistCalendar) DateFormat(android.icu.text.DateFormat) SimpleDateFormat(android.icu.text.SimpleDateFormat) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Test(org.junit.Test)

Example 12 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer in project j2objc by google.

the class AstroTest method TestLunarPosition.

@Test
public void TestLunarPosition() {
    GregorianCalendar gc = new GregorianCalendar(new SimpleTimeZone(0, "UTC"));
    CalendarAstronomer astro = new CalendarAstronomer();
    // year, month, day, hour, minute, ascension(radians), declination(radians)
    final double[][] tests = { { 1979, 2, 26, 16, 00, -0.3778379118188744, -0.1399698825594198 } };
    logln("");
    for (int i = 0; i < tests.length; i++) {
        gc.clear();
        gc.set((int) tests[i][0], (int) tests[i][1] - 1, (int) tests[i][2], (int) tests[i][3], (int) tests[i][4]);
        astro.setDate(gc.getTime());
        Equatorial result = astro.getMoonPosition();
        if (result.ascension != tests[i][5]) {
            if ((float) result.ascension == (float) tests[i][5]) {
                logln("result.ascension(" + result.ascension + ") !=  tests[i][5](" + tests[i][5] + ") in double for test " + i);
            } else {
                errln("FAIL: result.ascension(" + result.ascension + ") !=  tests[i][5](" + tests[i][5] + ") for test " + i);
            }
        }
        if (result.declination != tests[i][6]) {
            if ((float) result.declination == (float) tests[i][6]) {
                logln("result.declination(" + result.declination + ") !=  tests[i][6](" + tests[i][6] + ") in double for test " + i);
            } else {
                errln("FAIL: result.declination(" + result.declination + ") !=  tests[i][6](" + tests[i][6] + ") for test " + i);
            }
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Equatorial(android.icu.impl.CalendarAstronomer.Equatorial) Test(org.junit.Test)

Example 13 with CalendarAstronomer

use of android.icu.impl.CalendarAstronomer in project j2objc by google.

the class AstroTest method TestMoonAge.

@Test
public void TestMoonAge() {
    GregorianCalendar gc = new GregorianCalendar(new SimpleTimeZone(0, "GMT"));
    CalendarAstronomer calastro = new CalendarAstronomer();
    // more testcases are around the date 05/20/2012
    // ticket#3785  UDate ud0 = 1337557623000.0;
    double[][] testcase = { { 2012, 5, 20, 16, 48, 59 }, { 2012, 5, 20, 16, 47, 34 }, { 2012, 5, 21, 00, 00, 00 }, { 2012, 5, 20, 14, 55, 59 }, { 2012, 5, 21, 7, 40, 40 }, { 2023, 9, 25, 10, 00, 00 }, { 2008, 7, 7, 15, 00, 33 }, { 1832, 9, 24, 2, 33, 41 }, { 2016, 1, 31, 23, 59, 59 }, { 2099, 5, 20, 14, 55, 59 } };
    // Moon phase angle - Got from http://www.moonsystem.to/checkupe.htm
    double[] angle = { 356.8493418421329, 356.8386760059673, 0.09625415252237701, 355.9986960782416, 3.5714026601303317, 124.26906744384183, 59.80247650195558, 357.54163205513123, 268.41779281511094, 4.82340276581624 };
    double precision = PI / 32;
    for (int i = 0; i < testcase.length; i++) {
        gc.clear();
        String testString = "CASE[" + i + "]: Year " + (int) testcase[i][0] + " Month " + (int) testcase[i][1] + " Day " + (int) testcase[i][2] + " Hour " + (int) testcase[i][3] + " Minutes " + (int) testcase[i][4] + " Seconds " + (int) testcase[i][5];
        gc.set((int) testcase[i][0], (int) testcase[i][1] - 1, (int) testcase[i][2], (int) testcase[i][3], (int) testcase[i][4], (int) testcase[i][5]);
        calastro.setDate(gc.getTime());
        double expectedAge = (angle[i] * PI) / 180;
        double got = calastro.getMoonAge();
        logln(testString);
        if (!(got > expectedAge - precision && got < expectedAge + precision)) {
            errln("FAIL: expected " + expectedAge + " got " + got);
        } else {
            logln("PASS: expected " + expectedAge + " got " + got);
        }
    }
}
Also used : SimpleTimeZone(android.icu.util.SimpleTimeZone) GregorianCalendar(android.icu.util.GregorianCalendar) CalendarAstronomer(android.icu.impl.CalendarAstronomer) Test(org.junit.Test)

Aggregations

CalendarAstronomer (android.icu.impl.CalendarAstronomer)13 Test (org.junit.Test)8 GregorianCalendar (android.icu.util.GregorianCalendar)7 Calendar (android.icu.util.Calendar)5 SimpleTimeZone (android.icu.util.SimpleTimeZone)4 Equatorial (android.icu.impl.CalendarAstronomer.Equatorial)3 DateFormat (android.icu.text.DateFormat)3 Date (java.util.Date)3 Ecliptic (android.icu.impl.CalendarAstronomer.Ecliptic)1 CalendarCache (android.icu.impl.CalendarCache)1 SimpleDateFormat (android.icu.text.SimpleDateFormat)1 BuddhistCalendar (android.icu.util.BuddhistCalendar)1 ChineseCalendar (android.icu.util.ChineseCalendar)1 JapaneseCalendar (android.icu.util.JapaneseCalendar)1 TaiwanCalendar (android.icu.util.TaiwanCalendar)1 TimeZone (android.icu.util.TimeZone)1 ULocale (android.icu.util.ULocale)1