diff options
author | Andy McFadden <fadden@android.com> | 2011-07-13 14:25:03 -0700 |
---|---|---|
committer | Andy McFadden <fadden@android.com> | 2011-07-13 14:25:03 -0700 |
commit | 747abc3833aec07827fa6b831e58f78e72c139d1 (patch) | |
tree | 3c12a103c0994f83169a3de461fadf07ac8d72f4 | |
parent | 51db63a869ff2d160877901efc10d25577a53d5b (diff) | |
download | calendar-747abc3833aec07827fa6b831e58f78e72c139d1.tar.gz |
Move a couple more classes to calendar-common
Move RecurrenceProcessor and DateException, so that app code can
access the recurrence generator. Useful for e.g. bug 4977517.
Change-Id: Iaeb3e486a6a4133aa3d361d73461a0a0a3771bb4
-rw-r--r-- | src/com/android/calendarcommon/DateException.java | 25 | ||||
-rw-r--r-- | src/com/android/calendarcommon/RecurrenceProcessor.java | 1188 | ||||
-rw-r--r-- | tests/src/com/android/calendarcommon/RRuleTest.java | 756 | ||||
-rw-r--r-- | tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java | 2499 |
4 files changed, 4468 insertions, 0 deletions
diff --git a/src/com/android/calendarcommon/DateException.java b/src/com/android/calendarcommon/DateException.java new file mode 100644 index 0000000..c917885 --- /dev/null +++ b/src/com/android/calendarcommon/DateException.java @@ -0,0 +1,25 @@ +/* + * Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.calendarcommon; + +public class DateException extends Exception +{ + public DateException(String message) + { + super(message); + } +} diff --git a/src/com/android/calendarcommon/RecurrenceProcessor.java b/src/com/android/calendarcommon/RecurrenceProcessor.java new file mode 100644 index 0000000..319b43e --- /dev/null +++ b/src/com/android/calendarcommon/RecurrenceProcessor.java @@ -0,0 +1,1188 @@ +/* //device/content/providers/pim/RecurrenceProcessor.java +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package com.android.calendarcommon; + +import android.text.format.Time; +import android.util.Log; + +import java.util.TreeSet; + +public class RecurrenceProcessor +{ + // these are created once and reused. + private Time mIterator = new Time(Time.TIMEZONE_UTC); + private Time mUntil = new Time(Time.TIMEZONE_UTC); + private StringBuilder mStringBuilder = new StringBuilder(); + private Time mGenerated = new Time(Time.TIMEZONE_UTC); + private DaySet mDays = new DaySet(false); + // Give up after this many loops. This is roughly 1 second of expansion. + private static final int MAX_ALLOWED_ITERATIONS = 2000; + + public RecurrenceProcessor() + { + } + + private static final String TAG = "RecurrenceProcessor"; + + private static final boolean SPEW = false; + + /** + * Returns the time (millis since epoch) of the last occurrence, + * or -1 if the event repeats forever. If there are no occurrences + * (because the exrule or exdates cancel all the occurrences) and the + * event does not repeat forever, then 0 is returned. + * + * This computes a conservative estimate of the last occurrence. That is, + * the time of the actual last occurrence might be earlier than the time + * returned by this method. + * + * @param dtstart the time of the first occurrence + * @param recur the recurrence + * @return an estimate of the time (in UTC milliseconds) of the last + * occurrence, which may be greater than the actual last occurrence + * @throws DateException + */ + public long getLastOccurence(Time dtstart, + RecurrenceSet recur) throws DateException { + return getLastOccurence(dtstart, null /* no limit */, recur); + } + + /** + * Returns the time (millis since epoch) of the last occurrence, + * or -1 if the event repeats forever. If there are no occurrences + * (because the exrule or exdates cancel all the occurrences) and the + * event does not repeat forever, then 0 is returned. + * + * This computes a conservative estimate of the last occurrence. That is, + * the time of the actual last occurrence might be earlier than the time + * returned by this method. + * + * @param dtstart the time of the first occurrence + * @param maxtime the max possible time of the last occurrence. null means no limit + * @param recur the recurrence + * @return an estimate of the time (in UTC milliseconds) of the last + * occurrence, which may be greater than the actual last occurrence + * @throws DateException + */ + public long getLastOccurence(Time dtstart, Time maxtime, + RecurrenceSet recur) throws DateException { + long lastTime = -1; + boolean hasCount = false; + + // first see if there are any "until"s specified. if so, use the latest + // until / rdate. + if (recur.rrules != null) { + for (EventRecurrence rrule : recur.rrules) { + if (rrule.count != 0) { + hasCount = true; + } else if (rrule.until != null) { + // according to RFC 2445, until must be in UTC. + mIterator.parse(rrule.until); + long untilTime = mIterator.toMillis(false /* use isDst */); + if (untilTime > lastTime) { + lastTime = untilTime; + } + } + } + if (lastTime != -1 && recur.rdates != null) { + for (long dt : recur.rdates) { + if (dt > lastTime) { + lastTime = dt; + } + } + } + + // If there were only "until"s and no "count"s, then return the + // last "until" date or "rdate". + if (lastTime != -1 && !hasCount) { + return lastTime; + } + } else if (recur.rdates != null && + recur.exrules == null && recur.exdates == null) { + // if there are only rdates, we can just pick the last one. + for (long dt : recur.rdates) { + if (dt > lastTime) { + lastTime = dt; + } + } + return lastTime; + } + + // Expand the complete recurrence if there were any counts specified, + // or if there were rdates specified. + if (hasCount || recur.rdates != null || maxtime != null) { + // The expansion might not contain any dates if the exrule or + // exdates cancel all the generated dates. + long[] dates = expand(dtstart, recur, + dtstart.toMillis(false /* use isDst */) /* range start */, + (maxtime != null) ? + maxtime.toMillis(false /* use isDst */) : -1 /* range end */); + + // The expansion might not contain any dates if exrule or exdates + // cancel all the generated dates. + if (dates.length == 0) { + return 0; + } + return dates[dates.length - 1]; + } + return -1; + } + + /** + * a -- list of values + * N -- number of values to use in a + * v -- value to check for + */ + private static boolean listContains(int[] a, int N, int v) + { + for (int i=0; i<N; i++) { + if (a[i] == v) { + return true; + } + } + return false; + } + + /** + * a -- list of values + * N -- number of values to use in a + * v -- value to check for + * max -- if a value in a is negative, add that negative value + * to max and compare that instead; this is how we deal with + * negative numbers being offsets from the end value + */ + private static boolean listContains(int[] a, int N, int v, int max) + { + for (int i=0; i<N; i++) { + int w = a[i]; + if (w > 0) { + if (w == v) { + return true; + } + } else { + max += w; // w is negative + if (max == v) { + return true; + } + } + } + return false; + } + + /** + * Filter out the ones for events whose BYxxx rule is for + * a period greater than or equal to the period of the FREQ. + * + * Returns 0 if the event should not be filtered out + * Returns something else (a rule number which is useful for debugging) + * if the event should not be returned + */ + private static int filter(EventRecurrence r, Time iterator) + { + boolean found; + int freq = r.freq; + + if (EventRecurrence.MONTHLY >= freq) { + // BYMONTH + if (r.bymonthCount > 0) { + found = listContains(r.bymonth, r.bymonthCount, + iterator.month + 1); + if (!found) { + return 1; + } + } + } + if (EventRecurrence.WEEKLY >= freq) { + // BYWEEK -- this is just a guess. I wonder how many events + // acutally use BYWEEKNO. + if (r.byweeknoCount > 0) { + found = listContains(r.byweekno, r.byweeknoCount, + iterator.getWeekNumber(), + iterator.getActualMaximum(Time.WEEK_NUM)); + if (!found) { + return 2; + } + } + } + if (EventRecurrence.DAILY >= freq) { + // BYYEARDAY + if (r.byyeardayCount > 0) { + found = listContains(r.byyearday, r.byyeardayCount, + iterator.yearDay, iterator.getActualMaximum(Time.YEAR_DAY)); + if (!found) { + return 3; + } + } + // BYMONTHDAY + if (r.bymonthdayCount > 0 ) { + found = listContains(r.bymonthday, r.bymonthdayCount, + iterator.monthDay, + iterator.getActualMaximum(Time.MONTH_DAY)); + if (!found) { + return 4; + } + } + // BYDAY -- when filtering, we ignore the number field, because it + // only is meaningful when creating more events. +byday: + if (r.bydayCount > 0) { + int a[] = r.byday; + int N = r.bydayCount; + int v = EventRecurrence.timeDay2Day(iterator.weekDay); + for (int i=0; i<N; i++) { + if (a[i] == v) { + break byday; + } + } + return 5; + } + } + if (EventRecurrence.HOURLY >= freq) { + // BYHOUR + found = listContains(r.byhour, r.byhourCount, + iterator.hour, + iterator.getActualMaximum(Time.HOUR)); + if (!found) { + return 6; + } + } + if (EventRecurrence.MINUTELY >= freq) { + // BYMINUTE + found = listContains(r.byminute, r.byminuteCount, + iterator.minute, + iterator.getActualMaximum(Time.MINUTE)); + if (!found) { + return 7; + } + } + if (EventRecurrence.SECONDLY >= freq) { + // BYSECOND + found = listContains(r.bysecond, r.bysecondCount, + iterator.second, + iterator.getActualMaximum(Time.SECOND)); + if (!found) { + return 8; + } + } + // BYSETPOS -- we might have to do this by postprocessing + // the list + + // if we got to here, we didn't filter it out + return 0; + } + + private static final int USE_ITERATOR = 0; + private static final int USE_BYLIST = 1; + + /** + * Return whether we should make this list from the BYxxx list or + * from the component of the iterator. + */ + int generateByList(int count, int freq, int byFreq) + { + if (byFreq >= freq) { + return USE_ITERATOR; + } else { + if (count == 0) { + return USE_ITERATOR; + } else { + return USE_BYLIST; + } + } + } + + private static boolean useBYX(int freq, int freqConstant, int count) + { + return freq > freqConstant && count > 0; + } + + public static class DaySet + { + public DaySet(boolean zulu) + { + mTime = new Time(Time.TIMEZONE_UTC); + } + + void setRecurrence(EventRecurrence r) + { + mYear = 0; + mMonth = -1; + mR = r; + } + + boolean get(Time iterator, int day) + { + int realYear = iterator.year; + int realMonth = iterator.month; + + Time t = null; + + if (SPEW) { + Log.i(TAG, "get called with iterator=" + iterator + + " " + iterator.month + + "/" + iterator.monthDay + + "/" + iterator.year + " day=" + day); + } + if (day < 1 || day > 28) { + // if might be past the end of the month, we need to normalize it + t = mTime; + t.set(day, realMonth, realYear); + unsafeNormalize(t); + realYear = t.year; + realMonth = t.month; + day = t.monthDay; + if (SPEW) { + Log.i(TAG, "normalized t=" + t + " " + t.month + + "/" + t.monthDay + + "/" + t.year); + } + } + + /* + if (true || SPEW) { + Log.i(TAG, "set t=" + t + " " + realMonth + "/" + day + "/" + realYear); + } + */ + if (realYear != mYear || realMonth != mMonth) { + if (t == null) { + t = mTime; + t.set(day, realMonth, realYear); + unsafeNormalize(t); + if (SPEW) { + Log.i(TAG, "set t=" + t + " " + t.month + + "/" + t.monthDay + + "/" + t.year + + " realMonth=" + realMonth + " mMonth=" + mMonth); + } + } + mYear = realYear; + mMonth = realMonth; + mDays = generateDaysList(t, mR); + if (SPEW) { + Log.i(TAG, "generated days list"); + } + } + return (mDays & (1<<day)) != 0; + } + + /** + * Fill in a bit set containing the days of the month on which this + * will occur. + * + * Only call this if the r.freq > DAILY. Otherwise, we should be + * processing the BYDAY, BYMONTHDAY, etc. as filters instead. + * + * monthOffset may be -1, 0 or 1 + */ + private static int generateDaysList(Time generated, EventRecurrence r) + { + int days = 0; + + int i, count, v; + int[] byday, bydayNum, bymonthday; + int j, lastDayThisMonth; + int first; // Time.SUNDAY, etc + int k; + + lastDayThisMonth = generated.getActualMaximum(Time.MONTH_DAY); + + // BYDAY + count = r.bydayCount; + if (count > 0) { + // calculate the day of week for the first of this month (first) + j = generated.monthDay; + while (j >= 8) { + j -= 7; + } + first = generated.weekDay; + if (first >= j) { + first = first - j + 1; + } else { + first = first - j + 8; + } + + // What to do if the event is weekly: + // This isn't ideal, but we'll generate a month's worth of events + // and the code that calls this will only use the ones that matter + // for the current week. + byday = r.byday; + bydayNum = r.bydayNum; + for (i=0; i<count; i++) { + v = bydayNum[i]; + j = EventRecurrence.day2TimeDay(byday[i]) - first + 1; + if (j <= 0) { + j += 7; + } + if (v == 0) { + // v is 0, each day in the month/week + for (; j<=lastDayThisMonth; j+=7) { + if (SPEW) Log.i(TAG, "setting " + j + " for rule " + + v + "/" + EventRecurrence.day2TimeDay(byday[i])); + days |= 1 << j; + } + } + else if (v > 0) { + // v is positive, count from the beginning of the month + // -1 b/c the first one should add 0 + j += 7*(v-1); + if (j <= lastDayThisMonth) { + if (SPEW) Log.i(TAG, "setting " + j + " for rule " + + v + "/" + EventRecurrence.day2TimeDay(byday[i])); + // if it's impossible, we drop it + days |= 1 << j; + } + } + else { + // v is negative, count from the end of the month + // find the last one + for (; j<=lastDayThisMonth; j+=7) { + } + // v is negative + // should do +1 b/c the last one should add 0, but we also + // skipped the j -= 7 b/c the loop to find the last one + // overshot by one week + j += 7*v; + if (j >= 1) { + if (SPEW) Log.i(TAG, "setting " + j + " for rule " + + v + "/" + EventRecurrence.day2TimeDay(byday[i])); + days |= 1 << j; + } + } + } + } + + // BYMONTHDAY + // Q: What happens if we have BYMONTHDAY and BYDAY? + // A: I didn't see it in the spec, so in lieu of that, we'll + // intersect the two. That seems reasonable to me. + if (r.freq > EventRecurrence.WEEKLY) { + count = r.bymonthdayCount; + if (count != 0) { + bymonthday = r.bymonthday; + if (r.bydayCount == 0) { + for (i=0; i<count; i++) { + v = bymonthday[i]; + if (v >= 0) { + days |= 1 << v; + } else { + j = lastDayThisMonth + v + 1; // v is negative + if (j >= 1 && j <= lastDayThisMonth) { + days |= 1 << j; + } + } + } + } else { + // This is O(lastDayThisMonth*count), which is really + // O(count) with a decent sized constant. + for (j=1; j<=lastDayThisMonth; j++) { + next_day : { + if ((days&(1<<j)) != 0) { + for (i=0; i<count; i++) { + if (bymonthday[i] == j) { + break next_day; + } + } + days &= ~(1<<j); + } + } + } + } + } + } + return days; + } + + private EventRecurrence mR; + private int mDays; + private Time mTime; + private int mYear; + private int mMonth; + } + + /** + * Expands the recurrence within the given range using the given dtstart + * value. Returns an array of longs where each element is a date in UTC + * milliseconds. The return value is never null. If there are no dates + * then an array of length zero is returned. + * + * @param dtstart a Time object representing the first occurrence + * @param recur the recurrence rules, including RRULE, RDATES, EXRULE, and + * EXDATES + * @param rangeStartMillis the beginning of the range to expand, in UTC + * milliseconds + * @param rangeEndMillis the non-inclusive end of the range to expand, in + * UTC milliseconds; use -1 for the entire range. + * @return an array of dates, each date is in UTC milliseconds + * @throws DateException + * @throws android.util.TimeFormatException if recur cannot be parsed + */ + public long[] expand(Time dtstart, + RecurrenceSet recur, + long rangeStartMillis, + long rangeEndMillis) throws DateException { + String timezone = dtstart.timezone; + mIterator.clear(timezone); + mGenerated.clear(timezone); + + // We don't need to clear the mUntil (and it wouldn't do any good to + // do so) because the "until" date string is specified in UTC and that + // sets the timezone in the mUntil Time object. + + mIterator.set(rangeStartMillis); + long rangeStartDateValue = normDateTimeComparisonValue(mIterator); + + long rangeEndDateValue; + if (rangeEndMillis != -1) { + mIterator.set(rangeEndMillis); + rangeEndDateValue = normDateTimeComparisonValue(mIterator); + } else { + rangeEndDateValue = Long.MAX_VALUE; + } + + TreeSet<Long> dtSet = new TreeSet<Long>(); + + if (recur.rrules != null) { + for (EventRecurrence rrule : recur.rrules) { + expand(dtstart, rrule, rangeStartDateValue, + rangeEndDateValue, true /* add */, dtSet); + } + } + if (recur.rdates != null) { + for (long dt : recur.rdates) { + // The dates are stored as milliseconds. We need to convert + // them to year/month/day values in the local timezone. + mIterator.set(dt); + long dtvalue = normDateTimeComparisonValue(mIterator); + dtSet.add(dtvalue); + } + } + if (recur.exrules != null) { + for (EventRecurrence exrule : recur.exrules) { + expand(dtstart, exrule, rangeStartDateValue, + rangeEndDateValue, false /* remove */, dtSet); + } + } + if (recur.exdates != null) { + for (long dt : recur.exdates) { + // The dates are stored as milliseconds. We need to convert + // them to year/month/day values in the local timezone. + mIterator.set(dt); + long dtvalue = normDateTimeComparisonValue(mIterator); + dtSet.remove(dtvalue); + } + } + if (dtSet.isEmpty()) { + // this can happen if the recurrence does not occur within the + // expansion window. + return new long[0]; + } + + // The values in dtSet are represented in a special form that is useful + // for fast comparisons and that is easy to generate from year/month/day + // values. We need to convert these to UTC milliseconds and also to + // ensure that the dates are valid. + int len = dtSet.size(); + long[] dates = new long[len]; + int i = 0; + for (Long val: dtSet) { + setTimeFromLongValue(mIterator, val); + dates[i++] = mIterator.toMillis(true /* ignore isDst */); + } + return dates; + } + + /** + * Run the recurrence algorithm. Processes events defined in the local + * timezone of the event. Return a list of iCalendar DATETIME + * strings containing the start date/times of the occurrences; the output + * times are defined in the local timezone of the event. + * + * If you want all of the events, pass Long.MAX_VALUE for rangeEndDateValue. If you pass + * Long.MAX_VALUE for rangeEnd, and the event doesn't have a COUNT or UNTIL field, + * you'll get a DateException. + * + * @param dtstart the dtstart date as defined in RFC2445. This + * {@link Time} should be in the timezone of the event. + * @param r the parsed recurrence, as defiend in RFC2445 + * @param rangeStartDateValue the first date-time you care about, inclusive + * @param rangeEndDateValue the last date-time you care about, not inclusive (so + * if you care about everything up through and including + * Dec 22 1995, set last to Dec 23, 1995 00:00:00 + * @param add Whether or not we should add to out, or remove from out. + * @param out the TreeSet you'd like to fill with the events + * @throws DateException + * @throws android.util.TimeFormatException if r cannot be parsed. + */ + public void expand(Time dtstart, + EventRecurrence r, + long rangeStartDateValue, + long rangeEndDateValue, + boolean add, + TreeSet<Long> out) throws DateException { + unsafeNormalize(dtstart); + long dtstartDateValue = normDateTimeComparisonValue(dtstart); + int count = 0; + + // add the dtstart instance to the recurrence, if within range. + // For example, if dtstart is Mar 1, 2010 and the range is Jan 1 - Apr 1, + // then add it here and increment count. If the range is earlier or later, + // then don't add it here. In that case, count will be incremented later + // inside the loop. It is important that count gets incremented exactly + // once here or in the loop for dtstart. + if (add && dtstartDateValue >= rangeStartDateValue + && dtstartDateValue < rangeEndDateValue) { + out.add(dtstartDateValue); + ++count; + } + + Time iterator = mIterator; + Time until = mUntil; + StringBuilder sb = mStringBuilder; + Time generated = mGenerated; + DaySet days = mDays; + + try { + + days.setRecurrence(r); + if (rangeEndDateValue == Long.MAX_VALUE && r.until == null && r.count == 0) { + throw new DateException( + "No range end provided for a recurrence that has no UNTIL or COUNT."); + } + + // the top-level frequency + int freqField; + int freqAmount = r.interval; + int freq = r.freq; + switch (freq) + { + case EventRecurrence.SECONDLY: + freqField = Time.SECOND; + break; + case EventRecurrence.MINUTELY: + freqField = Time.MINUTE; + break; + case EventRecurrence.HOURLY: + freqField = Time.HOUR; + break; + case EventRecurrence.DAILY: + freqField = Time.MONTH_DAY; + break; + case EventRecurrence.WEEKLY: + freqField = Time.MONTH_DAY; + freqAmount = 7 * r.interval; + if (freqAmount <= 0) { + freqAmount = 7; + } + break; + case EventRecurrence.MONTHLY: + freqField = Time.MONTH; + break; + case EventRecurrence.YEARLY: + freqField = Time.YEAR; + break; + default: + throw new DateException("bad freq=" + freq); + } + if (freqAmount <= 0) { + freqAmount = 1; + } + + int bymonthCount = r.bymonthCount; + boolean usebymonth = useBYX(freq, EventRecurrence.MONTHLY, bymonthCount); + boolean useDays = freq >= EventRecurrence.WEEKLY && + (r.bydayCount > 0 || r.bymonthdayCount > 0); + int byhourCount = r.byhourCount; + boolean usebyhour = useBYX(freq, EventRecurrence.HOURLY, byhourCount); + int byminuteCount = r.byminuteCount; + boolean usebyminute = useBYX(freq, EventRecurrence.MINUTELY, byminuteCount); + int bysecondCount = r.bysecondCount; + boolean usebysecond = useBYX(freq, EventRecurrence.SECONDLY, bysecondCount); + + // initialize the iterator + iterator.set(dtstart); + if (freqField == Time.MONTH) { + if (useDays) { + // if it's monthly, and we're going to be generating + // days, set the iterator day field to 1 because sometimes + // we'll skip months if it's greater than 28. + // XXX Do we generate days for MONTHLY w/ BYHOUR? If so, + // we need to do this then too. + iterator.monthDay = 1; + } + } + + long untilDateValue; + if (r.until != null) { + // Ensure that the "until" date string is specified in UTC. + String untilStr = r.until; + // 15 is length of date-time without trailing Z e.g. "20090204T075959" + // A string such as 20090204 is a valid UNTIL (see RFC 2445) and the + // Z should not be added. + if (untilStr.length() == 15) { + untilStr = untilStr + 'Z'; + } + // The parse() method will set the timezone to UTC + until.parse(untilStr); + + // We need the "until" year/month/day values to be in the same + // timezone as all the generated dates so that we can compare them + // using the values returned by normDateTimeComparisonValue(). + until.switchTimezone(dtstart.timezone); + untilDateValue = normDateTimeComparisonValue(until); + } else { + untilDateValue = Long.MAX_VALUE; + } + + sb.ensureCapacity(15); + sb.setLength(15); // TODO: pay attention to whether or not the event + // is an all-day one. + + if (SPEW) { + Log.i(TAG, "expand called w/ rangeStart=" + rangeStartDateValue + + " rangeEnd=" + rangeEndDateValue); + } + + // go until the end of the range or we're done with this event + boolean eventEnded = false; + int failsafe = 0; // Avoid infinite loops + events: { + while (true) { + int monthIndex = 0; + if (failsafe++ > MAX_ALLOWED_ITERATIONS) { // Give up after about 1 second of processing + throw new DateException("Recurrence processing stuck: " + r.toString()); + } + + unsafeNormalize(iterator); + + int iteratorYear = iterator.year; + int iteratorMonth = iterator.month + 1; + int iteratorDay = iterator.monthDay; + int iteratorHour = iterator.hour; + int iteratorMinute = iterator.minute; + int iteratorSecond = iterator.second; + + // year is never expanded -- there is no BYYEAR + generated.set(iterator); + + if (SPEW) Log.i(TAG, "year=" + generated.year); + + do { // month + int month = usebymonth + ? r.bymonth[monthIndex] + : iteratorMonth; + month--; + if (SPEW) Log.i(TAG, " month=" + month); + + int dayIndex = 1; + int lastDayToExamine = 0; + + // Use this to handle weeks that overlap the end of the month. + // Keep the year and month that days is for, and generate it + // when needed in the loop + if (useDays) { + // Determine where to start and end, don't worry if this happens + // to be before dtstart or after the end, because that will be + // filtered in the inner loop + if (freq == EventRecurrence.WEEKLY) { + int dow = iterator.weekDay; + dayIndex = iterator.monthDay - dow; + lastDayToExamine = dayIndex + 6; + } else { + lastDayToExamine = generated + .getActualMaximum(Time.MONTH_DAY); + } + if (SPEW) Log.i(TAG, "dayIndex=" + dayIndex + + " lastDayToExamine=" + lastDayToExamine + + " days=" + days); + } + + do { // day + int day; + if (useDays) { + if (!days.get(iterator, dayIndex)) { + dayIndex++; + continue; + } else { + day = dayIndex; + } + } else { + day = iteratorDay; + } + if (SPEW) Log.i(TAG, " day=" + day); + + // hour + int hourIndex = 0; + do { + int hour = usebyhour + ? r.byhour[hourIndex] + : iteratorHour; + if (SPEW) Log.i(TAG, " hour=" + hour + " usebyhour=" + usebyhour); + + // minute + int minuteIndex = 0; + do { + int minute = usebyminute + ? r.byminute[minuteIndex] + : iteratorMinute; + if (SPEW) Log.i(TAG, " minute=" + minute); + + // second + int secondIndex = 0; + do { + int second = usebysecond + ? r.bysecond[secondIndex] + : iteratorSecond; + if (SPEW) Log.i(TAG, " second=" + second); + + // we do this here each time, because if we distribute it, we find the + // month advancing extra times, as we set the month to the 32nd, 33rd, etc. + // days. + generated.set(second, minute, hour, day, month, iteratorYear); + unsafeNormalize(generated); + + long genDateValue = normDateTimeComparisonValue(generated); + // sometimes events get generated (BYDAY, BYHOUR, etc.) that + // are before dtstart. Filter these. I believe this is correct, + // but Google Calendar doesn't seem to always do this. + if (genDateValue >= dtstartDateValue) { + // filter and then add + int filtered = filter(r, generated); + if (0 == filtered) { + + // increase the count as long + // as this isn't the same + // as the first instance + // specified by the DTSTART + // (for RRULEs -- additive). + // This condition must be the complement of the + // condition for incrementing count at the + // beginning of the method, so if we don't + // increment count there, we increment it here. + // For example, if add is set and dtstartDateValue + // is inside the start/end range, then it was added + // and count was incremented at the beginning. + // If dtstartDateValue is outside the range or add + // is not set, then we must increment count here. + if (!(dtstartDateValue == genDateValue + && add + && dtstartDateValue >= rangeStartDateValue + && dtstartDateValue < rangeEndDateValue)) { + ++count; + } + // one reason we can stop is that + // we're past the until date + if (genDateValue > untilDateValue) { + if (SPEW) { + Log.i(TAG, "stopping b/c until=" + + untilDateValue + + " generated=" + + genDateValue); + } + break events; + } + // or we're past rangeEnd + if (genDateValue >= rangeEndDateValue) { + if (SPEW) { + Log.i(TAG, "stopping b/c rangeEnd=" + + rangeEndDateValue + + " generated=" + generated); + } + break events; + } + + if (genDateValue >= rangeStartDateValue) { + if (SPEW) { + Log.i(TAG, "adding date=" + generated + " filtered=" + filtered); + } + if (add) { + out.add(genDateValue); + } else { + out.remove(genDateValue); + } + } + // another is that count is high enough + if (r.count > 0 && r.count == count) { + //Log.i(TAG, "stopping b/c count=" + count); + break events; + } + } + } + secondIndex++; + } while (usebysecond && secondIndex < bysecondCount); + minuteIndex++; + } while (usebyminute && minuteIndex < byminuteCount); + hourIndex++; + } while (usebyhour && hourIndex < byhourCount); + dayIndex++; + } while (useDays && dayIndex <= lastDayToExamine); + monthIndex++; + } while (usebymonth && monthIndex < bymonthCount); + + // Add freqAmount to freqField until we get another date that we want. + // We don't want to "generate" dates with the iterator. + // XXX: We do this for days, because there is a varying number of days + // per month + int oldDay = iterator.monthDay; + generated.set(iterator); // just using generated as a temporary. + int n = 1; + while (true) { + int value = freqAmount * n; + switch (freqField) { + case Time.SECOND: + iterator.second += value; + break; + case Time.MINUTE: + iterator.minute += value; + break; + case Time.HOUR: + iterator.hour += value; + break; + case Time.MONTH_DAY: + iterator.monthDay += value; + break; + case Time.MONTH: + iterator.month += value; + break; + case Time.YEAR: + iterator.year += value; + break; + case Time.WEEK_DAY: + iterator.monthDay += value; + break; + case Time.YEAR_DAY: + iterator.monthDay += value; + break; + default: + throw new RuntimeException("bad field=" + freqField); + } + + unsafeNormalize(iterator); + if (freqField != Time.YEAR && freqField != Time.MONTH) { + break; + } + if (iterator.monthDay == oldDay) { + break; + } + n++; + iterator.set(generated); + } + } + } + } + catch (DateException e) { + Log.w(TAG, "DateException with r=" + r + " rangeStart=" + rangeStartDateValue + + " rangeEnd=" + rangeEndDateValue); + throw e; + } + catch (RuntimeException t) { + Log.w(TAG, "RuntimeException with r=" + r + " rangeStart=" + rangeStartDateValue + + " rangeEnd=" + rangeEndDateValue); + throw t; + } + } + + /** + * Normalizes the date fields to give a valid date, but if the time falls + * in the invalid window during a transition out of Daylight Saving Time + * when time jumps forward an hour, then the "normalized" value will be + * invalid. + * <p> + * This method also computes the weekDay and yearDay fields. + * + * <p> + * This method does not modify the fields isDst, or gmtOff. + */ + static void unsafeNormalize(Time date) { + int second = date.second; + int minute = date.minute; + int hour = date.hour; + int monthDay = date.monthDay; + int month = date.month; + int year = date.year; + + int addMinutes = ((second < 0) ? (second - 59) : second) / 60; + second -= addMinutes * 60; + minute += addMinutes; + int addHours = ((minute < 0) ? (minute - 59) : minute) / 60; + minute -= addHours * 60; + hour += addHours; + int addDays = ((hour < 0) ? (hour - 23) : hour) / 24; + hour -= addDays * 24; + monthDay += addDays; + + // We want to make "monthDay" positive. We do this by subtracting one + // from the year and adding a year's worth of days to "monthDay" in + // the following loop while "monthDay" <= 0. + while (monthDay <= 0) { + // If month is after Feb, then add this year's length so that we + // include this year's leap day, if any. + // Otherwise (the month is Feb or earlier), add last year's length. + // Subtract one from the year in either case. This gives the same + // effective date but makes monthDay (the day of the month) much + // larger. Eventually (usually in one iteration) monthDay will + // be positive. + int days = month > 1 ? yearLength(year) : yearLength(year - 1); + monthDay += days; + year -= 1; + } + // At this point, monthDay >= 1. Normalize the month to the range [0,11]. + if (month < 0) { + int years = (month + 1) / 12 - 1; + year += years; + month -= 12 * years; + } else if (month >= 12) { + int years = month / 12; + year += years; + month -= 12 * years; + } + // At this point, month is in the range [0,11] and monthDay >= 1. + // Now loop until the monthDay is in the correct range for the month. + while (true) { + // On January, check if we can jump forward a whole year. + if (month == 0) { + int yearLength = yearLength(year); + if (monthDay > yearLength) { + year++; + monthDay -= yearLength; + } + } + int monthLength = monthLength(year, month); + if (monthDay > monthLength) { + monthDay -= monthLength; + month++; + if (month >= 12) { + month -= 12; + year++; + } + } else break; + } + // At this point, monthDay <= the length of the current month and is + // in the range [1,31]. + + date.second = second; + date.minute = minute; + date.hour = hour; + date.monthDay = monthDay; + date.month = month; + date.year = year; + date.weekDay = weekDay(year, month, monthDay); + date.yearDay = yearDay(year, month, monthDay); + } + + /** + * Returns true if the given year is a leap year. + * + * @param year the given year to test + * @return true if the given year is a leap year. + */ + static boolean isLeapYear(int year) { + return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0)); + } + + /** + * Returns the number of days in the given year. + * + * @param year the given year + * @return the number of days in the given year. + */ + static int yearLength(int year) { + return isLeapYear(year) ? 366 : 365; + } + + private static final int[] DAYS_PER_MONTH = { 31, 28, 31, 30, 31, 30, 31, + 31, 30, 31, 30, 31 }; + private static final int[] DAYS_IN_YEAR_PRECEDING_MONTH = { 0, 31, 59, 90, + 120, 151, 180, 212, 243, 273, 304, 334 }; + + /** + * Returns the number of days in the given month of the given year. + * + * @param year the given year. + * @param month the given month in the range [0,11] + * @return the number of days in the given month of the given year. + */ + static int monthLength(int year, int month) { + int n = DAYS_PER_MONTH[month]; + if (n != 28) { + return n; + } + return isLeapYear(year) ? 29 : 28; + } + + /** + * Computes the weekday, a number in the range [0,6] where Sunday=0, from + * the given year, month, and day. + * + * @param year the year + * @param month the 0-based month in the range [0,11] + * @param day the 1-based day of the month in the range [1,31] + * @return the weekday, a number in the range [0,6] where Sunday=0 + */ + static int weekDay(int year, int month, int day) { + if (month <= 1) { + month += 12; + year -= 1; + } + return (day + (13 * month - 14) / 5 + year + year/4 - year/100 + year/400) % 7; + } + + /** + * Computes the 0-based "year day", given the year, month, and day. + * + * @param year the year + * @param month the 0-based month in the range [0,11] + * @param day the 1-based day in the range [1,31] + * @return the 0-based "year day", the number of days into the year + */ + static int yearDay(int year, int month, int day) { + int yearDay = DAYS_IN_YEAR_PRECEDING_MONTH[month] + day - 1; + if (month >= 2 && isLeapYear(year)) { + yearDay += 1; + } + return yearDay; + } + + /** + * Converts a normalized Time value to a 64-bit long. The mapping of Time + * values to longs provides a total ordering on the Time values so that + * two Time values can be compared efficiently by comparing their 64-bit + * long values. This is faster than converting the Time values to UTC + * millliseconds. + * + * @param normalized a Time object whose date and time fields have been + * normalized + * @return a 64-bit long value that can be used for comparing and ordering + * dates and times represented by Time objects + */ + private static final long normDateTimeComparisonValue(Time normalized) { + // 37 bits for the year, 4 bits for the month, 5 bits for the monthDay, + // 5 bits for the hour, 6 bits for the minute, 6 bits for the second. + return ((long)normalized.year << 26) + (normalized.month << 22) + + (normalized.monthDay << 17) + (normalized.hour << 12) + + (normalized.minute << 6) + normalized.second; + } + + private static final void setTimeFromLongValue(Time date, long val) { + date.year = (int) (val >> 26); + date.month = (int) (val >> 22) & 0xf; + date.monthDay = (int) (val >> 17) & 0x1f; + date.hour = (int) (val >> 12) & 0x1f; + date.minute = (int) (val >> 6) & 0x3f; + date.second = (int) (val & 0x3f); + } +} diff --git a/tests/src/com/android/calendarcommon/RRuleTest.java b/tests/src/com/android/calendarcommon/RRuleTest.java new file mode 100644 index 0000000..743e236 --- /dev/null +++ b/tests/src/com/android/calendarcommon/RRuleTest.java @@ -0,0 +1,756 @@ +/* +** +** Copyright 2009, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +/* Based on http://code.google.com/p/google-rfc-2445/source/browse/trunk/test/com/google/ical/iter/RRuleIteratorImplTest.java */ + +package com.android.calendarcommon; + +import com.android.calendarcommon.RecurrenceSet; + +import android.os.Debug; +import android.test.suitebuilder.annotation.MediumTest; +import android.test.suitebuilder.annotation.Suppress; +import android.text.format.Time; +import junit.framework.TestCase; + +/** + * You can run those tests with: + * + * adb shell am instrument + * -e debug false + * -w + * -e + * class com.android.providers.calendar.RRuleTest + * com.android.providers.calendar.tests/android.test.InstrumentationTestRunner + */ + +public class RRuleTest extends TestCase { + private static final String TAG = "RRuleTest"; + private static final boolean METHOD_TRACE = false; + + private static String[] getFormattedDates(long[] dates, Time time, boolean truncate) { + String[] out = new String[dates.length]; + int i = 0; + for (long date : dates) { + time.set(date); + if (truncate) { + out[i] = time.format2445().substring(0, 8); // Just YYMMDD + } else { + out[i] = time.format2445().substring(0, 15); // YYMMDDThhmmss + } + ++i; + } + return out; + } + + static final String PST = "America/Los_Angeles"; + static final String UTC = "UTC"; + // Use this date as end of recurrence unlessotherwise specified. + static final String DEFAULT_END = "20091212"; + + private void runRecurrenceIteratorTest(String rruleText, String dtStart, int limit, + String golden) throws Exception { + runRecurrenceIteratorTest(rruleText, dtStart, limit, golden, null, null, UTC); + } + + private void runRecurrenceIteratorTest(String rrule, String dtstartStr, int limit, + String golden, String advanceTo, String tz) throws Exception { + runRecurrenceIteratorTest(rrule, dtstartStr, limit, golden, advanceTo, null, tz); + } + + /** + * Tests a recurrence rule + * @param rrule The rule to expand + * @param dtstartStr The dtstart to use + * @param limit Maximum number of entries to expand. if there are more, "..." is appended to + * the result. Note that Android's recurrence expansion doesn't support expanding n results, + * so this is faked by expanding until the endAt date, and then taking limit results. + * @param golden The desired results + * @param advanceTo The starting date for expansion. dtstartStr is used if null is passed in. + * @param endAt The ending date. DEFAULT_END is used if null is passed in. + * @param tz The time zone. UTC is used if null is passed in. + * @throws Exception if anything goes wrong. + */ + private void runRecurrenceIteratorTest(String rrule, String dtstartStr, int limit, + String golden, String advanceTo, String endAt, String tz) throws Exception { + + String rdate = ""; + String exrule = ""; + String exdate = ""; + rrule = rrule.replace("RRULE:", ""); + // RecurrenceSet does not support folding of lines, so fold here + rrule = rrule.replace("\n ", ""); + + Time dtstart = new Time(tz); + Time rangeStart = new Time(tz); + Time rangeEnd = new Time(tz); + Time outCal = new Time(tz); + + dtstart.parse(dtstartStr); + if (advanceTo == null) { + advanceTo = dtstartStr; + } + if (endAt == null) { + endAt = DEFAULT_END; + } + + rangeStart.parse(advanceTo); + rangeEnd.parse(endAt); + + + RecurrenceProcessor rp = new RecurrenceProcessor(); + RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); + + long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), + rangeEnd.toMillis(false /* use isDst */)); + + if (METHOD_TRACE) { + Debug.stopMethodTracing(); + } + + boolean truncate = dtstartStr.length() <= 8; // Just date, not date-time + String[] actual = getFormattedDates(out, outCal, truncate); + + StringBuilder sb = new StringBuilder(); + int k = 0; + while (k < actual.length && --limit >= 0) { + if (k != 0) { + sb.append(','); + } + sb.append(actual[k]); + k++; + } + if (limit < 0) { + sb.append(",..."); + } + assertEquals(golden, sb.toString()); + } + + // Infinite loop, bug 1662110 + @MediumTest + public void testFrequencyLimits() throws Exception { + try { + runRecurrenceIteratorTest("RRULE:FREQ=SECONDLY;BYSECOND=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14," + "15,16,17,18,19,20,21,22,23,24,25,26,27,28,29," + "30,31,32,33,34,35,36,37,38,39,40,41,42,43,44," + "45,46,47,48,49,50,51,52,53,54,55,56,57,58,59", "20000101", 1, ""); + fail("Don't do that"); + } catch (DateException ex) { + // pass + } + } + + @MediumTest + public void testSimpleDaily() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY", "20060120", 5, "20060120,20060121,20060122,20060123,20060124,..."); + } + + @MediumTest + public void testSimpleWeekly() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY", "20060120", 5, "20060120,20060127,20060203,20060210,20060217,..."); + } + + @MediumTest + public void testSimpleMonthly() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY", "20060120", 5, "20060120,20060220,20060320,20060420,20060520,..."); + } + + @MediumTest + public void testSimpleYearly() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY", "20060120", 5, "20060120,20070120,20080120,20090120,20100120,...", null, "20120101", UTC); + } + + // from section 4.3.10 + @MediumTest + public void testMultipleByParts() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU", "19970105", 8, "19970105,19970112,19970119,19970126," + "19990103,19990110,19990117,19990124,..."); + } + + @MediumTest + public void testCountWithInterval() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;COUNT=10;INTERVAL=2", "19970105", 11, "19970105,19970107,19970109,19970111,19970113," + "19970115,19970117,19970119,19970121,19970123"); + } + + // from section 4.6.5 + // Fails: wrong dates + @MediumTest + @Suppress + public void testNegativeOffsetsA() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10", "19970105", 5, "19971026,19981025,19991031,20001029,20011028,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testNegativeOffsetsB() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4", "19970105", 5, "19970406,19980405,19990404,20000402,20010401,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testNegativeOffsetsC() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;UNTIL=19980404T150000Z", "19970105", 5, "19970406"); + } + + // feom section 4.8.5.4 + @MediumTest + public void testDailyFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;COUNT=10", "19970902T090000", 11, "19970902T090000,19970903T090000,19970904T090000,19970905T090000," + "19970906T090000,19970907T090000,19970908T090000,19970909T090000," + "19970910T090000,19970911T090000"); + + } + + @MediumTest + public void testDailyUntilDec4() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;UNTIL=19971204", "19971128", 11, "19971128,19971129,19971130,19971201,19971202,19971203,19971204"); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEveryOtherDayForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;INTERVAL=2", "19971128", 5, "19971128,19971130,19971202,19971204,19971206,..."); + } + + @MediumTest + public void testEvery10Days5Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5", "19970902", 5, "19970902,19970912,19970922,19971002,19971012"); + } + + @MediumTest + public void testWeeklyFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=10", "19970902", 10, "19970902,19970909,19970916,19970923,19970930," + "19971007,19971014,19971021,19971028,19971104"); + } + + @MediumTest + public void testWeeklyUntilDec24() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971224", "19970902", 25, "19970902,19970909,19970916,19970923,19970930," + "19971007,19971014,19971021,19971028,19971104," + "19971111,19971118,19971125,19971202,19971209," + "19971216,19971223"); + } + + @MediumTest + public void testEveryOtherWeekForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=SU", "19970902", 11, "19970902,19970916,19970930,19971014,19971028," + "19971111,19971125,19971209,19971223,19980106," + "19980120,..."); + } + + @MediumTest + public void testWeeklyOnTuesdayAndThursdayFor5Weeks() throws Exception { + // if UNTIL date does not match start date, then until date treated as + // occurring on midnight. + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971007;WKST=SU;BYDAY=TU,TH", "19970902T090000", 11, "19970902T090000,19970904T090000,19970909T090000,19970911T090000," + "19970916T090000,19970918T090000,19970923T090000,19970925T090000," + "19970930T090000,19971002T090000"); + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971007T000000Z;WKST=SU;BYDAY=TU,TH", "19970902T090000", 11, "19970902T090000,19970904T090000,19970909T090000,19970911T090000," + "19970916T090000,19970918T090000,19970923T090000,19970925T090000," + "19970930T090000,19971002T090000"); + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=10;WKST=SU;BYDAY=TU,TH", "19970902", 11, "19970902,19970904,19970909,19970911,19970916," + "19970918,19970923,19970925,19970930,19971002"); + } + + @MediumTest + public void testEveryOtherWeekOnMWFUntilDec24() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903T090000", 25, "19970903T090000,19970905T090000,19970915T090000,19970917T090000," + "19970919T090000,19970929T090000,19971001T090000,19971003T090000," + "19971013T090000,19971015T090000,19971017T090000,19971027T090000," + "19971029T090000,19971031T090000,19971110T090000,19971112T090000," + "19971114T090000,19971124T090000,19971126T090000,19971128T090000," + "19971208T090000,19971210T090000,19971212T090000,19971222T090000"); + } + + @MediumTest + public void testEveryOtherWeekOnMWFUntilDec24a() throws Exception { + // if the UNTIL date is timed, when the start is not, the time should be + // ignored, so we get one more instance + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903", 25, "19970903,19970905,19970915,19970917," + "19970919,19970929,19971001,19971003," + "19971013,19971015,19971017,19971027," + "19971029,19971031,19971110,19971112," + "19971114,19971124,19971126,19971128," + "19971208,19971210,19971212,19971222," + "19971224"); + } + + // Fails with wrong times + @MediumTest + @Suppress + public void testEveryOtherWeekOnMWFUntilDec24b() throws Exception { + // test with an alternate timezone + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T090000Z;WKST=SU;\n" + " BYDAY=MO,WE,FR", "19970903T090000", 25, "19970903T160000,19970905T160000,19970915T160000,19970917T160000," + "19970919T160000,19970929T160000,19971001T160000,19971003T160000," + "19971013T160000,19971015T160000,19971017T160000,19971027T170000," + "19971029T170000,19971031T170000,19971110T170000,19971112T170000," + "19971114T170000,19971124T170000,19971126T170000,19971128T170000," + "19971208T170000,19971210T170000,19971212T170000,19971222T170000", null, PST); + } + + @MediumTest + public void testEveryOtherWeekOnTuThFor8Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH", "19970902", 8, "19970902,19970904,19970916,19970918,19970930," + "19971002,19971014,19971016"); + } + + @MediumTest + public void testMonthlyOnThe1stFridayFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYDAY=1FR", "19970905", 10, "19970905,19971003,19971107,19971205,19980102," + "19980206,19980306,19980403,19980501,19980605"); + } + + @MediumTest + public void testMonthlyOnThe1stFridayUntilDec24() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;UNTIL=19971224T000000Z;BYDAY=1FR", "19970905", 4, "19970905,19971003,19971107,19971205"); + } + + @MediumTest + public void testEveryOtherMonthOnThe1stAndLastSundayFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=2;COUNT=10;BYDAY=1SU,-1SU", "19970907", 10, "19970907,19970928,19971102,19971130,19980104," + "19980125,19980301,19980329,19980503,19980531"); + } + + @MediumTest + public void testMonthlyOnTheSecondToLastMondayOfTheMonthFor6Months() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=6;BYDAY=-2MO", "19970922", 6, "19970922,19971020,19971117,19971222,19980119," + "19980216"); + } + + @MediumTest + public void testMonthlyOnTheThirdToLastDay() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=-3", "19970928", 6, "19970928,19971029,19971128,19971229,19980129,19980226,..."); + } + + @MediumTest + public void testMonthlyOnThe2ndAnd15thFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=2,15", "19970902", 10, "19970902,19970915,19971002,19971015,19971102," + "19971115,19971202,19971215,19980102,19980115"); + } + + @MediumTest + public void testMonthlyOnTheFirstAndLastFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=10;BYMONTHDAY=1,-1", "19970930", 10, "19970930,19971001,19971031,19971101,19971130," + "19971201,19971231,19980101,19980131,19980201"); + } + + @MediumTest + public void testEvery18MonthsOnThe10thThru15thFor10Occ() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;COUNT=10;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 10, "19970910,19970911,19970912,19970913,19970914," + "19970915,19990310,19990311,19990312,19990313"); + } + + @MediumTest + public void testEveryTuesdayEveryOtherMonth() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=2;BYDAY=TU", "19970902", 18, "19970902,19970909,19970916,19970923,19970930," + "19971104,19971111,19971118,19971125,19980106," + "19980113,19980120,19980127,19980303,19980310," + "19980317,19980324,19980331,..."); + } + + @MediumTest + public void testYearlyInJuneAndJulyFor10Occurrences() throws Exception { + // Note: Since none of the BYDAY, BYMONTHDAY or BYYEARDAY components + // are specified, the day is gotten from DTSTART + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=10;BYMONTH=6,7", "19970610", 10, "19970610,19970710,19980610,19980710,19990610," + "19990710,20000610,20000710,20010610,20010710"); + } + + @MediumTest + public void testEveryOtherYearOnJanuaryFebruaryAndMarchFor10Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", "19970310", 10, "19970310,19990110,19990210,19990310,20010110," + "20010210,20010310,20030110,20030210,20030310"); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testEvery3rdYearOnThe1st100thAnd200thDayFor10Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", "19970101", 10, "19970101,19970410,19970719,20000101,20000409," + "20000718,20030101,20030410,20030719,20060101"); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEvery20thMondayOfTheYearForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=20MO", "19970519", 3, "19970519,19980518,19990517,..."); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testMondayOfWeekNumber20WhereTheDefaultStartOfTheWeekIsMonday() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=20;BYDAY=MO", "19970512", 3, "19970512,19980511,19990517,..."); + } + + @MediumTest + public void testEveryThursdayInMarchForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "19970313", 11, "19970313,19970320,19970327,19980305,19980312," + "19980319,19980326,19990304,19990311,19990318," + "19990325,..."); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testEveryThursdayButOnlyDuringJuneJulyAndAugustForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=TH;BYMONTH=6,7,8", "19970605", 39, "19970605,19970612,19970619,19970626,19970703," + "19970710,19970717,19970724,19970731,19970807," + "19970814,19970821,19970828,19980604,19980611," + "19980618,19980625,19980702,19980709,19980716," + "19980723,19980730,19980806,19980813,19980820," + "19980827,19990603,19990610,19990617,19990624," + "19990701,19990708,19990715,19990722,19990729," + "19990805,19990812,19990819,19990826,..."); + } + + //Fails: infinite loop + @MediumTest + @Suppress + public void testEveryFridayThe13thForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", "19970902", 5, "19980213,19980313,19981113,19990813,20001013," + "..."); + } + + @MediumTest + public void testTheFirstSaturdayThatFollowsTheFirstSundayOfTheMonthForever() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", "19970913", 10, "19970913,19971011,19971108,19971213,19980110," + "19980207,19980307,19980411,19980509,19980613," + "..."); + } + + @MediumTest + public void testEvery4YearsThe1stTuesAfterAMonInNovForever() throws Exception { + // US Presidential Election Day + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,\n" + " 5,6,7,8", "19961105", 3, "19961105,20001107,20041102,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testThe3rdInstanceIntoTheMonthOfOneOfTuesWedThursForNext3Months() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;COUNT=3;BYDAY=TU,WE,TH;BYSETPOS=3", "19970904", 3, "19970904,19971007,19971106"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testThe2ndToLastWeekdayOfTheMonth() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-2", "19970929", 7, "19970929,19971030,19971127,19971230,19980129," + "19980226,19980330,..."); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEvery3HoursFrom900AmTo500PmOnASpecificDay() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=HOURLY;INTERVAL=3;UNTIL=19970903T010000Z", "19970902", 7, "00000902,19970909,19970900,19970912,19970900," + "19970915,19970900"); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testEvery15MinutesFor6Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MINUTELY;INTERVAL=15;COUNT=6", "19970902", 13, "00000902,19970909,19970900,19970909,19970915," + "19970909,19970930,19970909,19970945,19970910," + "19970900,19970910,19970915"); + } + + @MediumTest + @Suppress + public void testEveryHourAndAHalfFor4Occurrences() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MINUTELY;INTERVAL=90;COUNT=4", "19970902", 9, "00000902,19970909,19970900,19970910,19970930," + "19970912,19970900,19970913,19970930"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAnExampleWhereTheDaysGeneratedMakesADifferenceBecauseOfWkst() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=MO", "19970805", 4, "19970805,19970810,19970819,19970824"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAnExampleWhereTheDaysGeneratedMakesADifferenceBecauseOfWkst2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", "19970805", 8, "19970805,19970817,19970819,19970831"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testWithByDayAndByMonthDayFilter() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;COUNT=4;BYDAY=TU,SU;" + "BYMONTHDAY=13,14,15,16,17,18,19,20", "19970805", 8, "19970817,19970819,19970914,19970916"); + } + + // Failed: wrong dates + @MediumTest + @Suppress + public void testAnnuallyInAugustOnTuesAndSunBetween13thAnd20th() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,SU;" + "BYMONTHDAY=13,14,15,16,17,18,19,20;BYMONTH=8", "19970605", 8, "19970817,19970819,19980816,19980818"); + } + + // Failed: wrong dates + @MediumTest + @Suppress + public void testLastDayOfTheYearIsASundayOrTuesday() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;COUNT=4;BYDAY=TU,SU;BYYEARDAY=-1", "19940605", 8, "19951231,19961231,20001231,20021231"); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekdayOfMonth() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYSETPOS=-1;BYDAY=-1MO,-1TU,-1WE,-1TH,-1FR", "19940605", 8, "19940630,19940729,19940831,19940930," + "19941031,19941130,19941230,19950131,..."); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testMonthsThatStartOrEndOnFriday() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=1,-1;BYDAY=FR;COUNT=6", "19940605", 8, "19940701,19940930,19950331,19950630,19950901,19951201"); + } + + // Fails: can't go that far into future + @MediumTest + @Suppress + public void testCenturiesThatAreNotLeapYears() throws Exception { + // I can't think of a good reason anyone would want to specify both a + // month day and a year day, so here's a really contrived example + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYYEARDAY=60;BYMONTHDAY=1", "19000101", 4, "19000301,21000301,22000301,23000301,...", null, "25000101", UTC); + } + + // Fails: generates instances when it shouldn't + @MediumTest + @Suppress + public void testNoInstancesGenerated() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=DAILY;UNTIL=19990101", "20000101", 4, ""); + } + + // Fails: generates instances when it shouldn't + @MediumTest + @Suppress + public void testNoInstancesGenerated2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30", "20000101", 4, ""); + } + + // Fails: generates instances when it shouldn't + @MediumTest + @Suppress + public void testNoInstancesGenerated3() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=4;BYYEARDAY=366", "20000101", 4, ""); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekdayOfMarch() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTH=3;BYDAY=SA,SU;BYSETPOS=-1", "20000101", 4, "20000326,20010331,20020331,20030330,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstWeekdayOfMarch() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTH=3;BYDAY=SA,SU;BYSETPOS=1", "20000101", 4, "20000304,20010303,20020302,20030301,..."); + } + + // January 1999 + // Mo Tu We Th Fr Sa Su + // 1 2 3 // < 4 days, so not a week + // 4 5 6 7 8 9 10 + + // January 2000 + // Mo Tu We Th Fr Sa Su + // 1 2 // < 4 days, so not a week + // 3 4 5 6 7 8 9 + + // January 2001 + // Mo Tu We Th Fr Sa Su + // 1 2 3 4 5 6 7 + // 8 9 10 11 12 13 14 + + // January 2002 + // Mo Tu We Th Fr Sa Su + // 1 2 3 4 5 6 + // 7 8 9 10 11 12 13 + + /** + * Find the first weekday of the first week of the year. + * The first week of the year may be partial, and the first week is considered + * to be the first one with at least four days. + */ + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstWeekdayOfFirstWeekOfYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1", "19990101", 4, "19990104,20000103,20010101,20020101,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstSundayOfTheYear1() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=SU", "19990101", 4, "19990110,20000109,20010107,20020106,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstSundayOfTheYear2() throws Exception { + // TODO(msamuel): is this right? + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=1SU", "19990101", 4, "19990103,20000102,20010107,20020106,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstSundayOfTheYear3() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=SU;BYYEARDAY=1,2,3,4,5,6,7,8,9,10,11,12,13" + ";BYSETPOS=1", "19990101", 4, "19990103,20000102,20010107,20020106,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testFirstWeekdayOfYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=1", "19990101", 4, "19990101,20000103,20010101,20020101,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekdayOfFirstWeekOfYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=1;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1", "19990101", 4, "19990108,20000107,20010105,20020104,..."); + } + + // January 1999 + // Mo Tu We Th Fr Sa Su + // 1 2 3 + // 4 5 6 7 8 9 10 + // 11 12 13 14 15 16 17 + // 18 19 20 21 22 23 24 + // 25 26 27 28 29 30 31 + + // Fails: wrong dates + @MediumTest + @Suppress + public void testSecondWeekday1() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=2", "19990101", 4, "19990105,19990112,19990119,19990126,..."); + } + + // January 1997 + // Mo Tu We Th Fr Sa Su + // 1 2 3 4 5 + // 6 7 8 9 10 11 12 + // 13 14 15 16 17 18 19 + // 20 21 22 23 24 25 26 + // 27 28 29 30 31 + + // Fails: wrong dates + @MediumTest + @Suppress + public void testSecondWeekday2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=2", "19970101", 4, "19970102,19970107,19970114,19970121,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testByYearDayAndByDayFilterInteraction() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYYEARDAY=15;BYDAY=3MO", "19990101", 4, "20010115,20070115,20180115,20240115,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testByDayWithNegWeekNoAsFilter() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;BYMONTHDAY=26;BYDAY=-1FR", "19990101", 4, "19990226,19990326,19991126,20000526,..."); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testLastWeekOfTheYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYWEEKNO=-1", "19990101", 6, "19991227,19991228,19991229,19991230,19991231,20001225,..."); + } + + // Fails: not enough dates generated + @MediumTest + @Suppress + public void testUserSubmittedTest1() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;INTERVAL=2;WKST=WE;BYDAY=SU,TU,TH,SA" + ";UNTIL=20000215T113000Z", "20000127T033000", 20, "20000127T033000,20000129T033000,20000130T033000,20000201T033000," + "20000210T033000,20000212T033000,20000213T033000,20000215T033000"); + } + + @MediumTest + public void testAdvanceTo1() throws Exception { + // a bunch of tests grabbed from above with an advance-to date tacked on + + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "19970313", 11, + /*"19970313,19970320,19970327,"*/"19980305,19980312," + "19980319,19980326,19990304,19990311,19990318," + "19990325,20000302,20000309,20000316,...", "19970601", UTC); + } + + // Fails: infinite loop + @MediumTest + @Suppress + public void testAdvanceTo2() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYDAY=20MO", "19970519", 3, + /*"19970519,"*/"19980518,19990517,20000515,...", "19980515", UTC); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAdvanceTo3() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;UNTIL=20090101;BYYEARDAY=1,100,200", "19970101", 10, + /*"19970101,19970410,19970719,20000101,"*/"20000409," + "20000718,20030101,20030410,20030719,20060101,20060410,20060719," + "20090101", "20000228", UTC); + } + + //Fails: wrong dates + @MediumTest + @Suppress + public void testAdvanceTo4() throws Exception { + // make sure that count preserved + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=3;COUNT=10;BYYEARDAY=1,100,200", "19970101", 10, + /*"19970101,19970410,19970719,20000101,"*/"20000409," + "20000718,20030101,20030410,20030719,20060101", "20000228", UTC); + } + + // Fails: too many dates + @MediumTest + @Suppress + public void testAdvanceTo5() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=2;COUNT=10;BYMONTH=1,2,3", "19970310", 10, + /*"19970310,"*/"19990110,19990210,19990310,20010110," + "20010210,20010310,20030110,20030210,20030310", "19980401", UTC); + } + + @MediumTest + public void testAdvanceTo6() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;UNTIL=19971224", "19970902", 25, + /*"19970902,19970909,19970916,19970923,"*/"19970930," + "19971007,19971014,19971021,19971028,19971104," + "19971111,19971118,19971125,19971202,19971209," + "19971216,19971223", "19970930", UTC); + } + + @MediumTest + public void testAdvanceTo7() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 5, + /*"19970910,19970911,19970912,19970913,19970914," + + "19970915,"*/"19990310,19990311,19990312,19990313,19990314,...", "19990101", UTC); + } + + @MediumTest + public void testAdvanceTo8() throws Exception { + // advancing into the past + runRecurrenceIteratorTest("RRULE:FREQ=MONTHLY;INTERVAL=18;BYMONTHDAY=10,11,12,13,14,\n" + " 15", "19970910", 11, "19970910,19970911,19970912,19970913,19970914," + "19970915,19990310,19990311,19990312,19990313,19990314,...", "19970901", UTC); + } + + // Fails: wrong dates + @MediumTest + @Suppress + public void testAdvanceTo9() throws Exception { + // skips first instance + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYMONTH=2;BYMONTHDAY=29", "19000101", 5, + // would return 2000 + "24000229,28000229,32000229,36000229,40000229,...", "20040101", UTC); + } + + // Infinite loop in native code (bug 1686327) + @MediumTest + @Suppress + public void testAdvanceTo10() throws Exception { + // filter hits until date before first instnace + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;INTERVAL=100;BYMONTH=2;BYMONTHDAY=29;UNTIL=21000101", "19000101", 5, "", "20040101", UTC); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testAdvanceTo11() throws Exception { + // advancing something that returns no instances + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30", "20000101", 10, "", "19970901", UTC); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testAdvanceTo12() throws Exception { + // advancing something that returns no instances and has a BYSETPOS rule + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=30,31;BYSETPOS=1", "20000101", 10, "", "19970901", UTC); + } + + // Fails: generates wrong dates + @MediumTest + @Suppress + public void testAdvanceTo13() throws Exception { + // advancing way past year generator timeout + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=2;BYMONTHDAY=28", "20000101", 10, "", "25000101", UTC); + } + + /** + * a testcase that yielded dupes due to bysetPos evilness + */ + @MediumTest + @Suppress + public void testCaseThatYieldedDupes() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=WEEKLY;WKST=SU;INTERVAL=1;BYMONTH=9,1,12,8" + ";BYMONTHDAY=-9,-29,24;BYSETPOS=-1,-4,10,-6,-1,-10,-10,-9,-8", "20060528", 200, "20060924,20061203,20061224,20070902,20071223,20080803,20080824," + "20090823,20100103,20100124,20110123,20120902,20121223,20130922," + "20140803,20140824,20150823,20160103,20160124,20170924,20171203," + "20171224,20180902,20181223,20190922,20200823,20210103,20210124," + "20220123,20230924,20231203,20231224,20240922,20250803,20250824," + "20260823,20270103,20270124,20280123,20280924,20281203,20281224," + "20290902,20291223,20300922,20310803,20310824,20330123,20340924," + "20341203,20341224,20350902,20351223,20360803,20360824,20370823," + "20380103,20380124,20390123,20400902,20401223,20410922,20420803," + "20420824,20430823,20440103,20440124,20450924,20451203,20451224," + "20460902,20461223,20470922,20480823,20490103,20490124,20500123," + "20510924,20511203,20511224,20520922,20530803,20530824,20540823," + "20550103,20550124,20560123,20560924,20561203,20561224,20570902," + "20571223,20580922,20590803,20590824,20610123,20620924,20621203," + "20621224,20630902,20631223,20640803,20640824,20650823,20660103," + "20660124,20670123,20680902,20681223,20690922,20700803,20700824," + "20710823,20720103,20720124,20730924,20731203,20731224,20740902," + "20741223,20750922,20760823,20770103,20770124,20780123,20790924," + "20791203,20791224,20800922,20810803,20810824,20820823,20830103," + "20830124,20840123,20840924,20841203,20841224,20850902,20851223," + "20860922,20870803,20870824,20890123,20900924,20901203,20901224," + "20910902,20911223,20920803,20920824,20930823,20940103,20940124," + "20950123,20960902,20961223,20970922,20980803,20980824,20990823," + "21000103,21000124,21010123,21020924,21021203,21021224,21030902," + "21031223,21040803,21040824,21050823,21060103,21060124,21070123," + "21080902,21081223,21090922,21100803,21100824,21110823,21120103," + "21120124,21130924,21131203,21131224,21140902,21141223,21150922," + "21160823,21170103,21170124,21180123,21190924,21191203,21191224," + "21200922,21210803,21210824,21220823,..."); + } + + @MediumTest + public void testEveryThursdayinMarchEachYear() throws Exception { + runRecurrenceIteratorTest("RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=TH", "20100304", 9, "20100304,20100311,20100318,20100325,20110303,20110310,20110317,20110324,20110331", null, "20111231", UTC); + } +} diff --git a/tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java b/tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java new file mode 100644 index 0000000..9f6f13a --- /dev/null +++ b/tests/src/com/android/calendarcommon/RecurrenceProcessorTest.java @@ -0,0 +1,2499 @@ +/* //device/content/providers/pim/RecurrenceProcessorTest.java +** +** Copyright 2006, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +package com.android.calendarcommon; + +import android.os.Debug; +import android.test.suitebuilder.annotation.LargeTest; +import android.test.suitebuilder.annotation.MediumTest; +import android.test.suitebuilder.annotation.SmallTest; +import android.text.TextUtils; +import android.text.format.Time; +import android.util.Log; +import android.util.TimeFormatException; +import junit.framework.TestCase; + +import java.util.TreeSet; + +public class RecurrenceProcessorTest extends TestCase { + private static final String TAG = "RecurrenceProcessorTest"; + private static final boolean SPEW = true; + private static final boolean METHOD_TRACE = false; + + private static String[] getFormattedDates(long[] dates, Time time) { + String[] out = new String[dates.length]; + int i = 0; + for (long date : dates) { + time.set(date); + out[i] = time.format2445(); + ++i; + } + return out; + } + + private static void printLists(String[] expected, String[] out) { + Log.i(TAG, " expected out"); + int i; + for (i = 0; i < expected.length && i < out.length; i++) { + Log.i(TAG, " [" + i + "] " + expected[i] + + " " + out[i]); + } + for (; i < expected.length; i++) { + Log.i(TAG, " [" + i + "] " + expected[i]); + } + for (; i < out.length; i++) { + Log.i(TAG, " [" + i + "] " + out[i]); + } + } + + public void verifyRecurrence(String dtstartStr, String rrule, String rdate, String exrule, + String exdate, String rangeStartStr, String rangeEndStr, String[] expected) + throws Exception { + verifyRecurrence(dtstartStr, rrule, rdate, exrule, exdate, rangeStartStr, + rangeEndStr, expected, expected[expected.length - 1]); + } + + public void verifyRecurrence(String dtstartStr, String rrule, String rdate, String exrule, + String exdate, String rangeStartStr, String rangeEndStr, String[] expected, + String last) throws Exception { + + // note that the zulu of all parameters here must be the same, expand + // doesn't work otherwise + + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + + " RRULE:" + rrule + + " RDATE:" + rdate + + " EXRULE:" + exrule + + " EXDATE:" + exdate); + } + + // we could use any timezone, incl. UTC, but we use a non-UTC + // timezone to make sure there are no UTC assumptions in the + // recurrence processing code. + String tz = "America/Los_Angeles"; + Time dtstart = new Time(tz); + Time rangeStart = new Time(tz); + Time rangeEnd = new Time(tz); + Time outCal = new Time(tz); + + dtstart.parse(dtstartStr); + + rangeStart.parse(rangeStartStr); + rangeEnd.parse(rangeEndStr); + + if (METHOD_TRACE) { + String fn = "/tmp/trace/" + this.getClass().getName().replace('$', '_'); + String df = fn + ".data"; + String kf = fn + ".key"; + Debug.startMethodTracing(fn, 8 * 1024 * 1024); + } + + RecurrenceProcessor rp = new RecurrenceProcessor(); + RecurrenceSet recur = new RecurrenceSet(rrule, rdate, exrule, exdate); + + long[] out = rp.expand(dtstart, recur, rangeStart.toMillis(false /* use isDst */), + rangeEnd.toMillis(false /* use isDst */)); + + if (METHOD_TRACE) { + Debug.stopMethodTracing(); + } + + int count = out.length; + String[] actual = getFormattedDates(out, outCal); + + if (count != expected.length) { + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); + printLists(expected, actual); + } + throw new RuntimeException("result lengths don't match. " + + " expected=" + expected.length + + " actual=" + count); + } + + for (int i = 0; i < count; i++) { + String s = actual[i]; + if (!s.equals(expected[i])) { + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); + printLists(expected, actual); + Log.i(TAG, "i=" + i); + } + throw new RuntimeException("expected[" + i + "]=" + + expected[i] + " actual=" + actual[i]); + } + } + + long lastOccur = rp.getLastOccurence(dtstart, rangeEnd, recur); + if (lastOccur == 0 && out.length == 0) { + // No occurrence found and 0 returned for lastOccur, this is ok. + return; + } + long lastMillis = -1; + long expectedMillis = -1; + String lastStr = ""; + if (lastOccur != -1) { + outCal.set(lastOccur); + lastStr = outCal.format2445(); + lastMillis = outCal.toMillis(true /* ignore isDst */); + } + if (last != null && last.length() > 0) { + Time expectedLast = new Time(tz); + expectedLast.parse(last); + expectedMillis = expectedLast.toMillis(true /* ignore isDst */); + } + if (lastMillis != expectedMillis) { + if (SPEW) { + Log.i(TAG, "DTSTART:" + dtstartStr + " RRULE:" + rrule); + Log.i(TAG, "Expected: " + last + "; Actual: " + lastStr); + printLists(expected, actual); + } + + throw new RuntimeException("expected last occurrence date does not match." + + " expected=" + last + + " actual=" + lastStr); + } + } + + @SmallTest + public void testMonthly0() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060305T100000", + "20060405T100000" + }); + } + + @SmallTest + public void testMonthly1() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;INTERVAL=2;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060405T100000", + "20060605T100000" + }); + } + + @SmallTest + public void testMonthly2() throws Exception { + // this tests wrapping the year when the interval isn't divisible + // by 12 + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;INTERVAL=5;COUNT=5", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060705T100000", + "20061205T100000", + "20070505T100000", + "20071005T100000" + }); + } + + @SmallTest + public void testMonthly3() throws Exception { + // with a simple BYDAY, spanning two months + verifyRecurrence("20060104T123456", + "FREQ=MONTHLY;UNTIL=20060201T200000Z;BYDAY=TU,WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060104T123456", + "20060110T123456", + "20060111T123456", + "20060117T123456", + "20060118T123456", + "20060124T123456", + "20060125T123456", + "20060131T123456" + }, + "20060201T120000"); + } + + @SmallTest + public void testMonthly4() throws Exception { + // with a BYDAY with +1 / etc., spanning two months and + // one day which isn't in the result + verifyRecurrence("20060101T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYDAY=+1SU,+2MO,+3TU,+4WE,+5MO,+5TU,+5WE,+6TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060101T123456", + "20060109T123456", + "20060117T123456", + "20060125T123456", + "20060130T123456", + "20060131T123456", + "20060205T123456", + "20060213T123456", + "20060221T123456", + "20060222T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly5() throws Exception { + // with a BYDAY with -1 / etc. + verifyRecurrence("20060201T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYDAY=-1SU,-2MO,-3TU,-4TU,-4WE,-5MO,-5TU,-5WE,-6TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060201T123456", + "20060207T123456", + "20060214T123456", + "20060220T123456", + "20060226T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly6() throws Exception { + // With positive BYMONTHDAYs + verifyRecurrence("20060201T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYMONTHDAY=1,2,5,28,31", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060201T123456", + "20060202T123456", + "20060205T123456", + "20060228T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly7() throws Exception { + // With negative BYMONTHDAYs + verifyRecurrence("20060201T123456", + "FREQ=MONTHLY;UNTIL=20060301T200000Z;BYMONTHDAY=-1,-5,-27,-28,-31", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060201T123456", + "20060202T123456", + "20060224T123456", + "20060228T123456" + }, + "20060301T120000"); + } + + @SmallTest + public void testMonthly8() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3", + "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", + null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060207T140000", + "20060305T100000", + "20060307T160000", + "20060405T100000", + "20060407T180000", + }); + } + + @SmallTest + public void testMonthly9() throws Exception { + verifyRecurrence("20060205T100000", null /* rrule */, + "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", + null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060207T140000", + "20060307T160000", + "20060407T180000", + }); + } + + @SmallTest + public void testMonthly10() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", + "America/Los_Angeles;20060207T140000,20060307T160000,20060407T180000", + null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060207T140000", + "20060212T100000", + "20060305T100000", + "20060307T160000", + "20060405T100000", + "20060407T180000", + }); + } + + @SmallTest + public void testMonthly11() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", + null /* rdate */, + "FREQ=MONTHLY;COUNT=2", null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060212T100000", + "20060405T100000", + }); + } + + @SmallTest + public void testMonthly12() throws Exception { + verifyRecurrence("20060205T100000", "FREQ=MONTHLY;COUNT=3\nFREQ=WEEKLY;COUNT=2", + null /* rdate */, null /* exrule */, + "20060212T180000Z,20060405T170000Z" /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060205T100000", + "20060305T100000", + }); + } + + @SmallTest + public void testMonthly13() throws Exception { + verifyRecurrence("20060101T100000", "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13;COUNT=10", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20101231T000000", + new String[]{ + "20060101T100000", + "20060113T100000", + "20061013T100000", + "20070413T100000", + "20070713T100000", + "20080613T100000", + "20090213T100000", + "20090313T100000", + "20091113T100000", + "20100813T100000", + }); + } + + @SmallTest + public void testWeekly0() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000", + "20060301T100000" + }); + } + + @SmallTest + public void testWeekly1() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T100000Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000" + }, "20060301T020000"); + } + + @SmallTest + public void testWeekly2() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T100001Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000" + }, "20060301T020001"); + } + + @SmallTest + public void testWeekly3() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060301T090000Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060222T100000" + }, "20060301T010000"); + } + + @SmallTest + public void testWeekly4() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001Z;BYDAY=TU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060221T100000", + "20060228T100000", + "20060307T100000" + }, "20060311T020001"); + } + + // until without "Z" + @SmallTest + public void testWeekly4a() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001;BYDAY=TU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060221T100000", + "20060228T100000", + "20060307T100000" + }, "20060311T100001"); + } + + @SmallTest + public void testWeekly5() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060311T100001Z;BYDAY=TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060223T100000", + "20060302T100000", + "20060309T100000" + }, "20060311T020001"); + } + + @SmallTest + public void testWeekly6() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060309T100001Z;BYDAY=WE,TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060222T100000", + "20060223T100000", + "20060301T100000", + "20060302T100000", + "20060308T100000" + }, "20060309T020001"); + } + + @SmallTest + public void testWeekly7() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060220T100001Z;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T020001"); + } + + @SmallTest + public void testWeekly8() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;COUNT=4;WKST=SU;BYDAY=TU,TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060221T100000", + "20060223T100000" + }); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestWeekly9() throws Exception { + verifyRecurrence("19970805T100000", + "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=MO", + null /* rdate */, null /* exrule */, null /* exdate */, + "19970101T000000", "19980101T000000", + new String[]{ + "19970805T100000", + "19970810T100000", + "19970819T100000", + "19970824T100000", + }); + } + + @SmallTest + public void testWeekly10() throws Exception { + verifyRecurrence("19970805T100000", + "FREQ=WEEKLY;INTERVAL=2;COUNT=4;BYDAY=TU,SU;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "19970101T000000", "19980101T000000", + new String[]{ + "19970805T100000", + "19970817T100000", + "19970819T100000", + "19970831T100000", + }); + } + + // BUG 1658567: UNTIL=date + @SmallTest + public void testWeekly11() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=20060220;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T000000"); + } + + @SmallTest + public void testWeekly12() throws Exception { + try { + verifyRecurrence("20060215T100000", "FREQ=WEEKLY;UNTIL=junk;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T020001"); + fail("Bad UNTIL string failed to throw exception"); + } catch (TimeFormatException e) { + // expected + } + } + + @SmallTest + public void testDaily0() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=DAILY;COUNT=3", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060217T100000" + }); + } + + @SmallTest + public void testDaily1() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=DAILY;UNTIL=20060302T100001Z", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060216T100000", + "20060217T100000", + "20060218T100000", + "20060219T100000", + "20060220T100000", + "20060221T100000", + "20060222T100000", + "20060223T100000", + "20060224T100000", + "20060225T100000", + "20060226T100000", + "20060227T100000", + "20060228T100000", + "20060301T100000" + }, "20060302T100001Z"); + } + + @SmallTest + public void testDaily2() throws Exception { + verifyRecurrence("20060215T100000", "FREQ=DAILY;UNTIL=20060220T100001Z;BYDAY=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060215T100000", + "20060219T100000" + }, "20060220T020001"); + } + + @SmallTest + public void testDaily3() throws Exception { + verifyRecurrence("20060219T100000", + "FREQ=DAILY;UNTIL=20060225T180304Z;BYDAY=SU,MO,SA;BYHOUR=5,10,22;BYMINUTE=3,59;BYSECOND=2,5", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20080101T000000", + new String[]{ + "20060219T100000", + "20060219T100302", + "20060219T100305", + "20060219T105902", + "20060219T105905", + "20060219T220302", + "20060219T220305", + "20060219T225902", + "20060219T225905", + "20060220T050302", + "20060220T050305", + "20060220T055902", + "20060220T055905", + "20060220T100302", + "20060220T100305", + "20060220T105902", + "20060220T105905", + "20060220T220302", + "20060220T220305", + "20060220T225902", + "20060220T225905", + "20060225T050302", + "20060225T050305", + "20060225T055902", + "20060225T055905", + "20060225T100302" + }, "20060225T100304"); + } + + @MediumTest + public void testFromGoogleCalendar0() throws Exception { + // Tuesday, Thursday (10/2) + verifyRecurrence("20061002T050000", + "FREQ=WEEKLY;UNTIL=20071031T200000Z;INTERVAL=1;BYDAY=TU,TH;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T050000", + "20061003T050000", + "20061005T050000", + "20061010T050000", + "20061012T050000", + "20061017T050000", + "20061019T050000", + "20061024T050000", + "20061026T050000", + "20061031T050000", + "20061102T050000", + "20061107T050000", + "20061109T050000", + "20061114T050000", + "20061116T050000", + "20061121T050000", + "20061123T050000", + "20061128T050000", + "20061130T050000", + "20061205T050000", + "20061207T050000", + "20061212T050000", + "20061214T050000", + "20061219T050000", + "20061221T050000", + "20061226T050000", + "20061228T050000", + "20070102T050000", + "20070104T050000", + "20070109T050000", + "20070111T050000", + "20070116T050000", + "20070118T050000", + "20070123T050000", + "20070125T050000", + "20070130T050000", + "20070201T050000", + "20070206T050000", + "20070208T050000", + "20070213T050000", + "20070215T050000", + "20070220T050000", + "20070222T050000", + "20070227T050000", + "20070301T050000", + "20070306T050000", + "20070308T050000", + "20070313T050000", + "20070315T050000", + "20070320T050000", + "20070322T050000", + "20070327T050000", + "20070329T050000", + "20070403T050000", + "20070405T050000", + "20070410T050000", + "20070412T050000", + "20070417T050000", + "20070419T050000", + "20070424T050000", + "20070426T050000", + "20070501T050000", + "20070503T050000", + "20070508T050000", + "20070510T050000", + "20070515T050000", + "20070517T050000", + "20070522T050000", + "20070524T050000", + "20070529T050000", + "20070531T050000", + "20070605T050000", + "20070607T050000", + "20070612T050000", + "20070614T050000", + "20070619T050000", + "20070621T050000", + "20070626T050000", + "20070628T050000", + "20070703T050000", + "20070705T050000", + "20070710T050000", + "20070712T050000", + "20070717T050000", + "20070719T050000", + "20070724T050000", + "20070726T050000", + "20070731T050000", + "20070802T050000", + "20070807T050000", + "20070809T050000", + "20070814T050000", + "20070816T050000", + "20070821T050000", + "20070823T050000", + "20070828T050000", + "20070830T050000", + "20070904T050000", + "20070906T050000", + "20070911T050000", + "20070913T050000", + "20070918T050000", + "20070920T050000", + "20070925T050000", + "20070927T050000", + "20071002T050000", + "20071004T050000", + "20071009T050000", + "20071011T050000", + "20071016T050000", + "20071018T050000", + "20071023T050000", + "20071025T050000", + "20071030T050000", + }, "20071031T130000"); + } + + @MediumTest + public void testFromGoogleCalendar1() throws Exception { + // Mon Wed Fri + verifyRecurrence("20061002T030000", + "FREQ=WEEKLY;UNTIL=20071025T180000Z;INTERVAL=1;BYDAY=MO,WE,FR;", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T030000", + "20061004T030000", + "20061006T030000", + "20061009T030000", + "20061011T030000", + "20061013T030000", + "20061016T030000", + "20061018T030000", + "20061020T030000", + "20061023T030000", + "20061025T030000", + "20061027T030000", + "20061030T030000", + "20061101T030000", + "20061103T030000", + "20061106T030000", + "20061108T030000", + "20061110T030000", + "20061113T030000", + "20061115T030000", + "20061117T030000", + "20061120T030000", + "20061122T030000", + "20061124T030000", + "20061127T030000", + "20061129T030000", + "20061201T030000", + "20061204T030000", + "20061206T030000", + "20061208T030000", + "20061211T030000", + "20061213T030000", + "20061215T030000", + "20061218T030000", + "20061220T030000", + "20061222T030000", + "20061225T030000", + "20061227T030000", + "20061229T030000", + "20070101T030000", + "20070103T030000", + "20070105T030000", + "20070108T030000", + "20070110T030000", + "20070112T030000", + "20070115T030000", + "20070117T030000", + "20070119T030000", + "20070122T030000", + "20070124T030000", + "20070126T030000", + "20070129T030000", + "20070131T030000", + "20070202T030000", + "20070205T030000", + "20070207T030000", + "20070209T030000", + "20070212T030000", + "20070214T030000", + "20070216T030000", + "20070219T030000", + "20070221T030000", + "20070223T030000", + "20070226T030000", + "20070228T030000", + "20070302T030000", + "20070305T030000", + "20070307T030000", + "20070309T030000", + "20070312T030000", + "20070314T030000", + "20070316T030000", + "20070319T030000", + "20070321T030000", + "20070323T030000", + "20070326T030000", + "20070328T030000", + "20070330T030000", + "20070402T030000", + "20070404T030000", + "20070406T030000", + "20070409T030000", + "20070411T030000", + "20070413T030000", + "20070416T030000", + "20070418T030000", + "20070420T030000", + "20070423T030000", + "20070425T030000", + "20070427T030000", + "20070430T030000", + "20070502T030000", + "20070504T030000", + "20070507T030000", + "20070509T030000", + "20070511T030000", + "20070514T030000", + "20070516T030000", + "20070518T030000", + "20070521T030000", + "20070523T030000", + "20070525T030000", + "20070528T030000", + "20070530T030000", + "20070601T030000", + "20070604T030000", + "20070606T030000", + "20070608T030000", + "20070611T030000", + "20070613T030000", + "20070615T030000", + "20070618T030000", + "20070620T030000", + "20070622T030000", + "20070625T030000", + "20070627T030000", + "20070629T030000", + "20070702T030000", + "20070704T030000", + "20070706T030000", + "20070709T030000", + "20070711T030000", + "20070713T030000", + "20070716T030000", + "20070718T030000", + "20070720T030000", + "20070723T030000", + "20070725T030000", + "20070727T030000", + "20070730T030000", + "20070801T030000", + "20070803T030000", + "20070806T030000", + "20070808T030000", + "20070810T030000", + "20070813T030000", + "20070815T030000", + "20070817T030000", + "20070820T030000", + "20070822T030000", + "20070824T030000", + "20070827T030000", + "20070829T030000", + "20070831T030000", + "20070903T030000", + "20070905T030000", + "20070907T030000", + "20070910T030000", + "20070912T030000", + "20070914T030000", + "20070917T030000", + "20070919T030000", + "20070921T030000", + "20070924T030000", + "20070926T030000", + "20070928T030000", + "20071001T030000", + "20071003T030000", + "20071005T030000", + "20071008T030000", + "20071010T030000", + "20071012T030000", + "20071015T030000", + "20071017T030000", + "20071019T030000", + "20071022T030000", + "20071024T030000", + }, "20071025T110000"); + } + + @SmallTest + public void testFromGoogleCalendar2() throws Exception { + // Monthly on day 2 + verifyRecurrence("20061002T070000", + "FREQ=MONTHLY;INTERVAL=1;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T070000", + "20061102T070000", + "20061202T070000", + "20070102T070000", + "20070202T070000", + "20070302T070000", + "20070402T070000", + "20070502T070000", + "20070602T070000", + "20070702T070000", + "20070802T070000", + "20070902T070000", + "20071002T070000", + "20071102T070000", + "20071202T070000", + "20080102T070000", + "20080202T070000", + "20080302T070000", + "20080402T070000", + "20080502T070000", + "20080602T070000", + "20080702T070000", + "20080802T070000", + "20080902T070000", + "20081002T070000", + "20081102T070000", + "20081202T070000", + }, "20081202T070000"); + } + + @MediumTest + public void testFromGoogleCalendar3() throws Exception { + // Every Weekday + verifyRecurrence("20061002T100000", + "FREQ=WEEKLY;UNTIL=20070215T100000Z;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061002T100000", + "20061003T100000", + "20061004T100000", + "20061005T100000", + "20061006T100000", + "20061009T100000", + "20061010T100000", + "20061011T100000", + "20061012T100000", + "20061013T100000", + "20061016T100000", + "20061017T100000", + "20061018T100000", + "20061019T100000", + "20061020T100000", + "20061023T100000", + "20061024T100000", + "20061025T100000", + "20061026T100000", + "20061027T100000", + "20061030T100000", + "20061031T100000", + "20061101T100000", + "20061102T100000", + "20061103T100000", + "20061106T100000", + "20061107T100000", + "20061108T100000", + "20061109T100000", + "20061110T100000", + "20061113T100000", + "20061114T100000", + "20061115T100000", + "20061116T100000", + "20061117T100000", + "20061120T100000", + "20061121T100000", + "20061122T100000", + "20061123T100000", + "20061124T100000", + "20061127T100000", + "20061128T100000", + "20061129T100000", + "20061130T100000", + "20061201T100000", + "20061204T100000", + "20061205T100000", + "20061206T100000", + "20061207T100000", + "20061208T100000", + "20061211T100000", + "20061212T100000", + "20061213T100000", + "20061214T100000", + "20061215T100000", + "20061218T100000", + "20061219T100000", + "20061220T100000", + "20061221T100000", + "20061222T100000", + "20061225T100000", + "20061226T100000", + "20061227T100000", + "20061228T100000", + "20061229T100000", + "20070101T100000", + "20070102T100000", + "20070103T100000", + "20070104T100000", + "20070105T100000", + "20070108T100000", + "20070109T100000", + "20070110T100000", + "20070111T100000", + "20070112T100000", + "20070115T100000", + "20070116T100000", + "20070117T100000", + "20070118T100000", + "20070119T100000", + "20070122T100000", + "20070123T100000", + "20070124T100000", + "20070125T100000", + "20070126T100000", + "20070129T100000", + "20070130T100000", + "20070131T100000", + "20070201T100000", + "20070202T100000", + "20070205T100000", + "20070206T100000", + "20070207T100000", + "20070208T100000", + "20070209T100000", + "20070212T100000", + "20070213T100000", + "20070214T100000", + }, "20070215T020000"); + } + + @SmallTest + public void testFromGoogleCalendar4() throws Exception { + // Every 5 months on day 2 + verifyRecurrence("20061003T100000", + "FREQ=MONTHLY;INTERVAL=5;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061003T100000", + "20070303T100000", + "20070803T100000", + "20080103T100000", + "20080603T100000", + "20081103T100000", + }, "20081103T100000"); + } + + @MediumTest + public void testFromGoogleCalendar5() throws Exception { + // Tuesday, Thursday (10/3) + verifyRecurrence("20061003T040000", + "FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,TH;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061003T040000", + "20061005T040000", + "20061010T040000", + "20061012T040000", + "20061017T040000", + "20061019T040000", + "20061024T040000", + "20061026T040000", + "20061031T040000", + "20061102T040000", + "20061107T040000", + "20061109T040000", + "20061114T040000", + "20061116T040000", + "20061121T040000", + "20061123T040000", + "20061128T040000", + "20061130T040000", + "20061205T040000", + "20061207T040000", + "20061212T040000", + "20061214T040000", + "20061219T040000", + "20061221T040000", + "20061226T040000", + "20061228T040000", + "20070102T040000", + "20070104T040000", + "20070109T040000", + "20070111T040000", + "20070116T040000", + "20070118T040000", + "20070123T040000", + "20070125T040000", + "20070130T040000", + "20070201T040000", + "20070206T040000", + "20070208T040000", + "20070213T040000", + "20070215T040000", + "20070220T040000", + "20070222T040000", + "20070227T040000", + "20070301T040000", + "20070306T040000", + "20070308T040000", + "20070313T040000", + "20070315T040000", + "20070320T040000", + "20070322T040000", + "20070327T040000", + "20070329T040000", + "20070403T040000", + "20070405T040000", + "20070410T040000", + "20070412T040000", + "20070417T040000", + "20070419T040000", + "20070424T040000", + "20070426T040000", + "20070501T040000", + "20070503T040000", + "20070508T040000", + "20070510T040000", + "20070515T040000", + "20070517T040000", + "20070522T040000", + "20070524T040000", + "20070529T040000", + "20070531T040000", + "20070605T040000", + "20070607T040000", + "20070612T040000", + "20070614T040000", + "20070619T040000", + "20070621T040000", + "20070626T040000", + "20070628T040000", + "20070703T040000", + "20070705T040000", + "20070710T040000", + "20070712T040000", + "20070717T040000", + "20070719T040000", + "20070724T040000", + "20070726T040000", + "20070731T040000", + "20070802T040000", + "20070807T040000", + "20070809T040000", + "20070814T040000", + "20070816T040000", + "20070821T040000", + "20070823T040000", + "20070828T040000", + "20070830T040000", + "20070904T040000", + "20070906T040000", + "20070911T040000", + "20070913T040000", + "20070918T040000", + "20070920T040000", + "20070925T040000", + "20070927T040000", + "20071002T040000", + "20071004T040000", + "20071009T040000", + "20071011T040000", + "20071016T040000", + "20071018T040000", + "20071023T040000", + "20071025T040000", + "20071030T040000", + "20071101T040000", + "20071106T040000", + "20071108T040000", + "20071113T040000", + "20071115T040000", + "20071120T040000", + "20071122T040000", + "20071127T040000", + "20071129T040000", + "20071204T040000", + "20071206T040000", + "20071211T040000", + "20071213T040000", + "20071218T040000", + "20071220T040000", + "20071225T040000", + "20071227T040000", + "20080101T040000", + "20080103T040000", + "20080108T040000", + "20080110T040000", + "20080115T040000", + "20080117T040000", + "20080122T040000", + "20080124T040000", + "20080129T040000", + "20080131T040000", + "20080205T040000", + "20080207T040000", + "20080212T040000", + "20080214T040000", + "20080219T040000", + "20080221T040000", + "20080226T040000", + "20080228T040000", + "20080304T040000", + "20080306T040000", + "20080311T040000", + "20080313T040000", + "20080318T040000", + "20080320T040000", + "20080325T040000", + "20080327T040000", + "20080401T040000", + "20080403T040000", + "20080408T040000", + "20080410T040000", + "20080415T040000", + "20080417T040000", + "20080422T040000", + "20080424T040000", + "20080429T040000", + "20080501T040000", + "20080506T040000", + "20080508T040000", + "20080513T040000", + "20080515T040000", + "20080520T040000", + "20080522T040000", + "20080527T040000", + "20080529T040000", + "20080603T040000", + "20080605T040000", + "20080610T040000", + "20080612T040000", + "20080617T040000", + "20080619T040000", + "20080624T040000", + "20080626T040000", + "20080701T040000", + "20080703T040000", + "20080708T040000", + "20080710T040000", + "20080715T040000", + "20080717T040000", + "20080722T040000", + "20080724T040000", + "20080729T040000", + "20080731T040000", + "20080805T040000", + "20080807T040000", + "20080812T040000", + "20080814T040000", + "20080819T040000", + "20080821T040000", + "20080826T040000", + "20080828T040000", + "20080902T040000", + "20080904T040000", + "20080909T040000", + "20080911T040000", + "20080916T040000", + "20080918T040000", + "20080923T040000", + "20080925T040000", + "20080930T040000", + "20081002T040000", + "20081007T040000", + "20081009T040000", + "20081014T040000", + "20081016T040000", + "20081021T040000", + "20081023T040000", + "20081028T040000", + "20081030T040000", + "20081104T040000", + "20081106T040000", + "20081111T040000", + "20081113T040000", + "20081118T040000", + "20081120T040000", + "20081125T040000", + "20081127T040000", + "20081202T040000", + "20081204T040000", + "20081209T040000", + "20081211T040000", + "20081216T040000", + "20081218T040000", + "20081223T040000", + "20081225T040000", + "20081230T040000", + }, "20081230T040000"); + } + + @MediumTest + public void testFromGoogleCalendar6() throws Exception { + // Weekly on all days + verifyRecurrence("20061003T060000", + "FREQ=WEEKLY;INTERVAL=1;BYDAY=SU,MO,TU,WE,TH,FR,SA;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20071003T060000", + new String[]{ + "20061003T060000", + "20061004T060000", + "20061005T060000", + "20061006T060000", + "20061007T060000", + "20061008T060000", + "20061009T060000", + "20061010T060000", + "20061011T060000", + "20061012T060000", + "20061013T060000", + "20061014T060000", + "20061015T060000", + "20061016T060000", + "20061017T060000", + "20061018T060000", + "20061019T060000", + "20061020T060000", + "20061021T060000", + "20061022T060000", + "20061023T060000", + "20061024T060000", + "20061025T060000", + "20061026T060000", + "20061027T060000", + "20061028T060000", + "20061029T060000", + "20061030T060000", + "20061031T060000", + "20061101T060000", + "20061102T060000", + "20061103T060000", + "20061104T060000", + "20061105T060000", + "20061106T060000", + "20061107T060000", + "20061108T060000", + "20061109T060000", + "20061110T060000", + "20061111T060000", + "20061112T060000", + "20061113T060000", + "20061114T060000", + "20061115T060000", + "20061116T060000", + "20061117T060000", + "20061118T060000", + "20061119T060000", + "20061120T060000", + "20061121T060000", + "20061122T060000", + "20061123T060000", + "20061124T060000", + "20061125T060000", + "20061126T060000", + "20061127T060000", + "20061128T060000", + "20061129T060000", + "20061130T060000", + "20061201T060000", + "20061202T060000", + "20061203T060000", + "20061204T060000", + "20061205T060000", + "20061206T060000", + "20061207T060000", + "20061208T060000", + "20061209T060000", + "20061210T060000", + "20061211T060000", + "20061212T060000", + "20061213T060000", + "20061214T060000", + "20061215T060000", + "20061216T060000", + "20061217T060000", + "20061218T060000", + "20061219T060000", + "20061220T060000", + "20061221T060000", + "20061222T060000", + "20061223T060000", + "20061224T060000", + "20061225T060000", + "20061226T060000", + "20061227T060000", + "20061228T060000", + "20061229T060000", + "20061230T060000", + "20061231T060000", + "20070101T060000", + "20070102T060000", + "20070103T060000", + "20070104T060000", + "20070105T060000", + "20070106T060000", + "20070107T060000", + "20070108T060000", + "20070109T060000", + "20070110T060000", + "20070111T060000", + "20070112T060000", + "20070113T060000", + "20070114T060000", + "20070115T060000", + "20070116T060000", + "20070117T060000", + "20070118T060000", + "20070119T060000", + "20070120T060000", + "20070121T060000", + "20070122T060000", + "20070123T060000", + "20070124T060000", + "20070125T060000", + "20070126T060000", + "20070127T060000", + "20070128T060000", + "20070129T060000", + "20070130T060000", + "20070131T060000", + "20070201T060000", + "20070202T060000", + "20070203T060000", + "20070204T060000", + "20070205T060000", + "20070206T060000", + "20070207T060000", + "20070208T060000", + "20070209T060000", + "20070210T060000", + "20070211T060000", + "20070212T060000", + "20070213T060000", + "20070214T060000", + "20070215T060000", + "20070216T060000", + "20070217T060000", + "20070218T060000", + "20070219T060000", + "20070220T060000", + "20070221T060000", + "20070222T060000", + "20070223T060000", + "20070224T060000", + "20070225T060000", + "20070226T060000", + "20070227T060000", + "20070228T060000", + "20070301T060000", + "20070302T060000", + "20070303T060000", + "20070304T060000", + "20070305T060000", + "20070306T060000", + "20070307T060000", + "20070308T060000", + "20070309T060000", + "20070310T060000", + "20070311T060000", + "20070312T060000", + "20070313T060000", + "20070314T060000", + "20070315T060000", + "20070316T060000", + "20070317T060000", + "20070318T060000", + "20070319T060000", + "20070320T060000", + "20070321T060000", + "20070322T060000", + "20070323T060000", + "20070324T060000", + "20070325T060000", + "20070326T060000", + "20070327T060000", + "20070328T060000", + "20070329T060000", + "20070330T060000", + "20070331T060000", + "20070401T060000", + "20070402T060000", + "20070403T060000", + "20070404T060000", + "20070405T060000", + "20070406T060000", + "20070407T060000", + "20070408T060000", + "20070409T060000", + "20070410T060000", + "20070411T060000", + "20070412T060000", + "20070413T060000", + "20070414T060000", + "20070415T060000", + "20070416T060000", + "20070417T060000", + "20070418T060000", + "20070419T060000", + "20070420T060000", + "20070421T060000", + "20070422T060000", + "20070423T060000", + "20070424T060000", + "20070425T060000", + "20070426T060000", + "20070427T060000", + "20070428T060000", + "20070429T060000", + "20070430T060000", + "20070501T060000", + "20070502T060000", + "20070503T060000", + "20070504T060000", + "20070505T060000", + "20070506T060000", + "20070507T060000", + "20070508T060000", + "20070509T060000", + "20070510T060000", + "20070511T060000", + "20070512T060000", + "20070513T060000", + "20070514T060000", + "20070515T060000", + "20070516T060000", + "20070517T060000", + "20070518T060000", + "20070519T060000", + "20070520T060000", + "20070521T060000", + "20070522T060000", + "20070523T060000", + "20070524T060000", + "20070525T060000", + "20070526T060000", + "20070527T060000", + "20070528T060000", + "20070529T060000", + "20070530T060000", + "20070531T060000", + "20070601T060000", + "20070602T060000", + "20070603T060000", + "20070604T060000", + "20070605T060000", + "20070606T060000", + "20070607T060000", + "20070608T060000", + "20070609T060000", + "20070610T060000", + "20070611T060000", + "20070612T060000", + "20070613T060000", + "20070614T060000", + "20070615T060000", + "20070616T060000", + "20070617T060000", + "20070618T060000", + "20070619T060000", + "20070620T060000", + "20070621T060000", + "20070622T060000", + "20070623T060000", + "20070624T060000", + "20070625T060000", + "20070626T060000", + "20070627T060000", + "20070628T060000", + "20070629T060000", + "20070630T060000", + "20070701T060000", + "20070702T060000", + "20070703T060000", + "20070704T060000", + "20070705T060000", + "20070706T060000", + "20070707T060000", + "20070708T060000", + "20070709T060000", + "20070710T060000", + "20070711T060000", + "20070712T060000", + "20070713T060000", + "20070714T060000", + "20070715T060000", + "20070716T060000", + "20070717T060000", + "20070718T060000", + "20070719T060000", + "20070720T060000", + "20070721T060000", + "20070722T060000", + "20070723T060000", + "20070724T060000", + "20070725T060000", + "20070726T060000", + "20070727T060000", + "20070728T060000", + "20070729T060000", + "20070730T060000", + "20070731T060000", + "20070801T060000", + "20070802T060000", + "20070803T060000", + "20070804T060000", + "20070805T060000", + "20070806T060000", + "20070807T060000", + "20070808T060000", + "20070809T060000", + "20070810T060000", + "20070811T060000", + "20070812T060000", + "20070813T060000", + "20070814T060000", + "20070815T060000", + "20070816T060000", + "20070817T060000", + "20070818T060000", + "20070819T060000", + "20070820T060000", + "20070821T060000", + "20070822T060000", + "20070823T060000", + "20070824T060000", + "20070825T060000", + "20070826T060000", + "20070827T060000", + "20070828T060000", + "20070829T060000", + "20070830T060000", + "20070831T060000", + "20070901T060000", + "20070902T060000", + "20070903T060000", + "20070904T060000", + "20070905T060000", + "20070906T060000", + "20070907T060000", + "20070908T060000", + "20070909T060000", + "20070910T060000", + "20070911T060000", + "20070912T060000", + "20070913T060000", + "20070914T060000", + "20070915T060000", + "20070916T060000", + "20070917T060000", + "20070918T060000", + "20070919T060000", + "20070920T060000", + "20070921T060000", + "20070922T060000", + "20070923T060000", + "20070924T060000", + "20070925T060000", + "20070926T060000", + "20070927T060000", + "20070928T060000", + "20070929T060000", + "20070930T060000", + "20071001T060000", + "20071002T060000", + }, "20071002T060000"); + } + + @MediumTest + public void testFromGoogleCalendar7() throws Exception { + // Every 3 days + verifyRecurrence("20061003T080000", + "FREQ=DAILY;INTERVAL=3;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061003T080000", + "20061006T080000", + "20061009T080000", + "20061012T080000", + "20061015T080000", + "20061018T080000", + "20061021T080000", + "20061024T080000", + "20061027T080000", + "20061030T080000", + "20061102T080000", + "20061105T080000", + "20061108T080000", + "20061111T080000", + "20061114T080000", + "20061117T080000", + "20061120T080000", + "20061123T080000", + "20061126T080000", + "20061129T080000", + "20061202T080000", + "20061205T080000", + "20061208T080000", + "20061211T080000", + "20061214T080000", + "20061217T080000", + "20061220T080000", + "20061223T080000", + "20061226T080000", + "20061229T080000", + "20070101T080000", + "20070104T080000", + "20070107T080000", + "20070110T080000", + "20070113T080000", + "20070116T080000", + "20070119T080000", + "20070122T080000", + "20070125T080000", + "20070128T080000", + "20070131T080000", + "20070203T080000", + "20070206T080000", + "20070209T080000", + "20070212T080000", + "20070215T080000", + "20070218T080000", + "20070221T080000", + "20070224T080000", + "20070227T080000", + "20070302T080000", + "20070305T080000", + "20070308T080000", + "20070311T080000", + "20070314T080000", + "20070317T080000", + "20070320T080000", + "20070323T080000", + "20070326T080000", + "20070329T080000", + "20070401T080000", + "20070404T080000", + "20070407T080000", + "20070410T080000", + "20070413T080000", + "20070416T080000", + "20070419T080000", + "20070422T080000", + "20070425T080000", + "20070428T080000", + "20070501T080000", + "20070504T080000", + "20070507T080000", + "20070510T080000", + "20070513T080000", + "20070516T080000", + "20070519T080000", + "20070522T080000", + "20070525T080000", + "20070528T080000", + "20070531T080000", + "20070603T080000", + "20070606T080000", + "20070609T080000", + "20070612T080000", + "20070615T080000", + "20070618T080000", + "20070621T080000", + "20070624T080000", + "20070627T080000", + "20070630T080000", + "20070703T080000", + "20070706T080000", + "20070709T080000", + "20070712T080000", + "20070715T080000", + "20070718T080000", + "20070721T080000", + "20070724T080000", + "20070727T080000", + "20070730T080000", + "20070802T080000", + "20070805T080000", + "20070808T080000", + "20070811T080000", + "20070814T080000", + "20070817T080000", + "20070820T080000", + "20070823T080000", + "20070826T080000", + "20070829T080000", + "20070901T080000", + "20070904T080000", + "20070907T080000", + "20070910T080000", + "20070913T080000", + "20070916T080000", + "20070919T080000", + "20070922T080000", + "20070925T080000", + "20070928T080000", + "20071001T080000", + "20071004T080000", + "20071007T080000", + "20071010T080000", + "20071013T080000", + "20071016T080000", + "20071019T080000", + "20071022T080000", + "20071025T080000", + "20071028T080000", + "20071031T080000", + "20071103T080000", + "20071106T080000", + "20071109T080000", + "20071112T080000", + "20071115T080000", + "20071118T080000", + "20071121T080000", + "20071124T080000", + "20071127T080000", + "20071130T080000", + "20071203T080000", + "20071206T080000", + "20071209T080000", + "20071212T080000", + "20071215T080000", + "20071218T080000", + "20071221T080000", + "20071224T080000", + "20071227T080000", + "20071230T080000", + "20080102T080000", + "20080105T080000", + "20080108T080000", + "20080111T080000", + "20080114T080000", + "20080117T080000", + "20080120T080000", + "20080123T080000", + "20080126T080000", + "20080129T080000", + "20080201T080000", + "20080204T080000", + "20080207T080000", + "20080210T080000", + "20080213T080000", + "20080216T080000", + "20080219T080000", + "20080222T080000", + "20080225T080000", + "20080228T080000", + "20080302T080000", + "20080305T080000", + "20080308T080000", + "20080311T080000", + "20080314T080000", + "20080317T080000", + "20080320T080000", + "20080323T080000", + "20080326T080000", + "20080329T080000", + "20080401T080000", + "20080404T080000", + "20080407T080000", + "20080410T080000", + "20080413T080000", + "20080416T080000", + "20080419T080000", + "20080422T080000", + "20080425T080000", + "20080428T080000", + "20080501T080000", + "20080504T080000", + "20080507T080000", + "20080510T080000", + "20080513T080000", + "20080516T080000", + "20080519T080000", + "20080522T080000", + "20080525T080000", + "20080528T080000", + "20080531T080000", + "20080603T080000", + "20080606T080000", + "20080609T080000", + "20080612T080000", + "20080615T080000", + "20080618T080000", + "20080621T080000", + "20080624T080000", + "20080627T080000", + "20080630T080000", + "20080703T080000", + "20080706T080000", + "20080709T080000", + "20080712T080000", + "20080715T080000", + "20080718T080000", + "20080721T080000", + "20080724T080000", + "20080727T080000", + "20080730T080000", + "20080802T080000", + "20080805T080000", + "20080808T080000", + "20080811T080000", + "20080814T080000", + "20080817T080000", + "20080820T080000", + "20080823T080000", + "20080826T080000", + "20080829T080000", + "20080901T080000", + "20080904T080000", + "20080907T080000", + "20080910T080000", + "20080913T080000", + "20080916T080000", + "20080919T080000", + "20080922T080000", + "20080925T080000", + "20080928T080000", + "20081001T080000", + "20081004T080000", + "20081007T080000", + "20081010T080000", + "20081013T080000", + "20081016T080000", + "20081019T080000", + "20081022T080000", + "20081025T080000", + "20081028T080000", + "20081031T080000", + "20081103T080000", + "20081106T080000", + "20081109T080000", + "20081112T080000", + "20081115T080000", + "20081118T080000", + "20081121T080000", + "20081124T080000", + "20081127T080000", + "20081130T080000", + "20081203T080000", + "20081206T080000", + "20081209T080000", + "20081212T080000", + "20081215T080000", + "20081218T080000", + "20081221T080000", + "20081224T080000", + "20081227T080000", + "20081230T080000", + }, "20081230T080000"); + } + + @SmallTest + public void testFromGoogleCalendar8() throws Exception { + // Annually on October 4 + verifyRecurrence("20061004T130000", + "FREQ=YEARLY;INTERVAL=1;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061004T130000", + "20071004T130000", + "20081004T130000", + }, "20081004T130000"); + } + + @MediumTest + public void testFromGoogleCalendar9() throws Exception { + // Monthly on the last Monday + verifyRecurrence("20061030T170000", + "FREQ=MONTHLY;INTERVAL=1;BYDAY=-1MO;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061030T170000", + "20061127T170000", + "20061225T170000", + "20070129T170000", + "20070226T170000", + "20070326T170000", + "20070430T170000", + "20070528T170000", + "20070625T170000", + "20070730T170000", + "20070827T170000", + "20070924T170000", + "20071029T170000", + "20071126T170000", + "20071231T170000", + "20080128T170000", + "20080225T170000", + "20080331T170000", + "20080428T170000", + "20080526T170000", + "20080630T170000", + "20080728T170000", + "20080825T170000", + "20080929T170000", + "20081027T170000", + "20081124T170000", + "20081229T170000", + }, "20081229T170000"); + } + + @SmallTest + public void testFromGoogleCalendar10() throws Exception { + // Every 7 weeks on Tuesday, Wednesday + verifyRecurrence("20061004T090000", + "FREQ=WEEKLY;UNTIL=20070223T010000Z;INTERVAL=7;BYDAY=TU,WE;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061004T090000", + "20061121T090000", + "20061122T090000", + "20070109T090000", + "20070110T090000", + }, "20070222T170000"); + } + + @SmallTest + public void testFromGoogleCalendar11() throws Exception { + // Monthly on day 31 + verifyRecurrence("20061031T160000", + "FREQ=MONTHLY;INTERVAL=1;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061031T160000", + "20061231T160000", + "20070131T160000", + "20070331T160000", + "20070531T160000", + "20070731T160000", + "20070831T160000", + "20071031T160000", + "20071231T160000", + "20080131T160000", + "20080331T160000", + "20080531T160000", + "20080731T160000", + "20080831T160000", + "20081031T160000", + "20081231T160000", + }, + "20081231T160000"); + } + + @SmallTest + public void testFromGoogleCalendar12() throws Exception { + // Every 2 months on the first Tuesday + verifyRecurrence("20061004T110000", + "FREQ=MONTHLY;INTERVAL=2;BYDAY=1TU;WKST=SU", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090101T000000", + new String[]{ + "20061004T110000", + "20061205T110000", + "20070206T110000", + "20070403T110000", + "20070605T110000", + "20070807T110000", + "20071002T110000", + "20071204T110000", + "20080205T110000", + "20080401T110000", + "20080603T110000", + "20080805T110000", + "20081007T110000", + "20081202T110000", + }, + "20081202T110000"); + } + + @SmallTest + public void testYearly0() throws Exception { + verifyRecurrence("20080101T100000", + "FREQ=YEARLY;UNTIL=20090131T090000Z;BYMONTH=1;BYDAY=SU,MO", + null /* rdate */, null /* exrule */, null /* exdate */, + "20080101T000000", "20090130T000000", + new String[]{ + "20080101T100000", + "20080106T100000", + "20080107T100000", + "20080113T100000", + "20080114T100000", + "20080120T100000", + "20080121T100000", + "20080127T100000", + "20080128T100000", + "20090104T100000", + "20090105T100000", + "20090111T100000", + "20090112T100000", + "20090118T100000", + "20090119T100000", + "20090125T100000", + "20090126T100000", + }, "20090131T010000"); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly1() throws Exception { + verifyRecurrence("20060101T100000", + "FREQ=YEARLY;COUNT=10;BYYEARDAY=1,100,200", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090131T000000", + new String[]{ + "20060101T100000", + "20060409T100000", + "20060718T100000", + "20070101T100000", + "20070409T100000", + "20070718T100000", + "20080101T100000", + "20080410T100000", + "20080719T100000", + "20090101T100000", + }); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly2() throws Exception { + verifyRecurrence("20060101T100000", + "FREQ=YEARLY;COUNT=5;BYWEEKNO=6;BYDAY=MO;WKST=MO", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090131T000000", + new String[]{ + "20060101T100000", + "20060206T100000", + "20070205T100000", + "20080204T100000", + "20090209T100000", + }); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly3() throws Exception { + verifyRecurrence("20060101T100000", + "FREQ=YEARLY;BYMONTH=3;BYDAY=TH", + null /* rdate */, null /* exrule */, null /* exdate */, + "20060101T000000", "20090131T000000", + new String[]{ + "20060101T100000", + "20060302T100000", + "20060309T100000", + "20060316T100000", + "20060323T100000", + "20060330T100000", + "20070301T100000", + "20070308T100000", + "20070315T100000", + "20070322T100000", + "20070329T100000", + "20080306T100000", + "20080313T100000", + "20080320T100000", + "20080327T100000", + }); + } + + /** + * This test fails because of a bug in RecurrenceProcessor.expand(). We + * don't have time to fix the bug yet but we don't want to lose track of + * this test either. The "failing" prefix on the method name prevents this + * test from being run. Remove the "failing" prefix when the bug is fixed. + * + * @throws Exception + */ + @SmallTest + public void failingTestYearly4() throws Exception { + verifyRecurrence("19920101T100000", + "FREQ=YEARLY;INTERVAL=4;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", + null /* rdate */, null /* exrule */, null /* exdate */, + "19920101T000000", "20121231T000000", + new String[]{ + "19920101T100000", + "19921103T100000", + "19961105T100000", + "20001107T100000", + "20041102T100000", + "20081104T100000", + "20121106T100000", + }); + } + + /** + * Test repeating event from Exchange with count field. + * Time range covers the whole repetition. + * + * @throws Exception + */ + public void testCount1() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100301T000000", "20100630T000000", + new String[]{ + "20100324T153000", + "20100331T153000", + "20100407T153000", + "20100414T153000", + "20100421T153000", + "20100428T153000", + "20100505T153000", + "20100512T153000", + "20100519T153000", + "20100526T153000", + }); + } + + /** + * Test repeating event from Exchange with count field. + * Time range covers the first part of the repetition. + * @throws Exception + */ + public void testCount2() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100501T000000", "20100630T000000", + new String[]{ + "20100505T153000", + "20100512T153000", + "20100519T153000", + "20100526T153000", + }); + } + + /** + * Test repeating event from Exchange with count field. + * Time range is beyond the repetition. + * @throws Exception + */ + public void testCount3() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100601T000000", "20100630T000000", + new String[]{}, + "20100526T153000" /* last */); + } + + /** + * Test repeating event from Exchange with count field. + * Time range is before the repetition + * @throws Exception + */ + public void testCount4() throws Exception { + verifyRecurrence("20100324T153000", + "FREQ=WEEKLY;INTERVAL=1;COUNT=10;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20100101T000000", "20100301T000000", + new String[]{}, + null /* last */); + } + + /** + * Test repeating weekly event with dtstart and dtend (only one occurrence) + * See bug #3267616 + * @throws Exception + */ + public void testWeekly13() throws Exception { + verifyRecurrence("20101117T150000", + "FREQ=WEEKLY;BYDAY=WE", + null /* rdate */, null /* exrule */, null /* exdate */, + "20101117T150000", "20101117T160000", + new String[]{ "20101117T150000" }); + } + + // These recurrence rules are used in the loop that measures the performance + // of recurrence expansion. + private static final String[] performanceRrules = new String[] { + "FREQ=DAILY;COUNT=100", + "FREQ=DAILY;INTERVAL=2;UNTIL=20080101T000000Z", + "FREQ=YEARLY;UNTIL=20090131T090000Z;BYMONTH=1;BYDAY=SU,MO,TU,WE,TH,FR,SA", + "FREQ=WEEKLY;INTERVAL=2;WKST=SU", + "FREQ=WEEKLY;COUNT=100;WKST=SU;BYDAY=MO,TU,WE,TH,FR", + "FREQ=MONTHLY;COUNT=100;BYDAY=1FR", + "FREQ=MONTHLY;INTERVAL=2;COUNT=100;BYDAY=1SU,-1SU", + "FREQ=MONTHLY;BYMONTHDAY=1,15", + "FREQ=MONTHLY;INTERVAL=3;COUNT=100;BYMONTHDAY=10,11,12,13,14", + "FREQ=YEARLY;COUNT=100;BYMONTH=6,7,8", + "FREQ=YEARLY;INTERVAL=2;BYMONTH=1,2,3,6,7,8", + "FREQ=YEARLY;COUNT=100;BYYEARDAY=1,100,200", + "FREQ=YEARLY;BYDAY=2MO", + "FREQ=YEARLY;BYWEEKNO=2,3,4;BYDAY=MO", + "FREQ=YEARLY;BYMONTH=3,4,5;BYDAY=TH", + "FREQ=MONTHLY;BYDAY=FR;BYMONTHDAY=13", + "FREQ=MONTHLY;BYDAY=SA;BYMONTHDAY=7,8,9,10,11,12,13", + "FREQ=YEARLY;INTERVAL=2;BYMONTH=11;BYDAY=TU;BYMONTHDAY=2,3,4,5,6,7,8", + "FREQ=WEEKLY;INTERVAL=2;COUNT=100;BYDAY=TU,SU;WKST=MO", + "FREQ=WEEKLY;INTERVAL=2;COUNT=100;BYDAY=TU,SU;WKST=SU", + }; + + /** + * This test never fails. It just runs for a while (about 10 seconds) + * in order to measure the performance of recurrence expansion. + * + * @throws Exception + */ + @LargeTest + public void performanceTextExpand() throws Exception { + String tz = "America/Los_Angeles"; + Time dtstart = new Time(tz); + Time rangeStart = new Time(tz); + Time rangeEnd = new Time(tz); + TreeSet<Long> out = new TreeSet<Long>(); + + dtstart.parse("20010101T000000"); + rangeStart.parse("20010101T000000"); + rangeEnd.parse("20090101T000000"); + long rangeStartMillis = rangeStart.toMillis(false /* use isDst */); + long rangeEndMillis = rangeEnd.toMillis(false /* use isDst */); + + long startTime = System.currentTimeMillis(); + for (int iterations = 0; iterations < 5; iterations++) { + RecurrenceProcessor rp = new RecurrenceProcessor(); + + int len = performanceRrules.length; + for (int i = 0; i < len; i++) { + String rrule = performanceRrules[i]; + //Log.i(TAG, "expanding rule: " + rrule); + RecurrenceSet recur = new RecurrenceSet(rrule, null, null, null); + + long [] dates = rp.expand(dtstart, recur, rangeStartMillis, rangeEndMillis); + //Log.i(TAG, "num instances: " + out.size()); + + // Also include the time to iterate through the expanded values + for (long date : dates) { + // Use the date so that this loop is not optimized away. + if (date == -1) { + Log.e(TAG, "unexpected date"); + break; + } + } + out.clear(); + } + } + + long endTime = System.currentTimeMillis(); + long elapsed = endTime - startTime; + Log.i(TAG, "testPerformanceExpand() expand() elapsed millis: " + elapsed); + } + + @LargeTest + public void performanceTestNormalize() throws Exception { + final int ITERATIONS = 50000; + + String tz = "America/Los_Angeles"; + Time date = new Time(tz); + date.parse("20090404T100000"); + + long startTime = System.currentTimeMillis(); + + for (int i = 0; i < ITERATIONS; i++) { + date.month += 1; + date.monthDay += 100; + date.normalize(true); + date.month -= 1; + date.monthDay -= 100; + date.normalize(true); + } + + long endTime = System.currentTimeMillis(); + long elapsed = endTime - startTime; + Log.i(TAG, "date: " + date.format2445()); + Log.i(TAG, "testPerformanceNormalize() normalize() elapsed millis: " + elapsed); + + // Time the unsafe version + date.parse("20090404T100000"); + startTime = System.currentTimeMillis(); + for (int i = 0; i < ITERATIONS; i++) { + date.month += 1; + date.monthDay += 100; + RecurrenceProcessor.unsafeNormalize(date); + date.month -= 1; + date.monthDay -= 100; + RecurrenceProcessor.unsafeNormalize(date); + } + + endTime = System.currentTimeMillis(); + elapsed = endTime - startTime; + Log.i(TAG, "date: " + date.format2445()); + Log.i(TAG, "testPerformanceNormalize() unsafeNormalize() elapsed millis: " + elapsed); + } + } |