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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Author :
* Damon Chaplin <damon@helixcode.com>
*
* Copyright 2000, Helix Code, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*/
/*
* This tests the recurrence rule expansion functions.
*/
#include <config.h>
#include <string.h>
#include <time.h>
#include <glib.h>
#include "cal-recur.h"
static void set_time (CalObjTime *cotime, gint year, gint month, gint day,
gint hour, gint minute, gint second);
static void display_occs (GArray *occs);
static GList* build_list (gint first, ...);
static gchar* time_to_string (CalObjTime *cotime);
static void do_test (gchar *description,
CalObjTime *event_start,
CalObjRecurrence *recur,
CalObjTime *interval_start,
CalObjTime *interval_end);
#define LIST_END 999
static void
test_yearly ()
{
CalObjTime event_start, interval_start, interval_end;
CalObjRecurrence recur;
set_time (&event_start, 2000, 0, 1, 0, 0, 0);
/* We set the interval to a wide range so we just test the event. */
set_time (&interval_start, 2000, 0, 1, 0, 0, 0);
set_time (&interval_end, 2010, 0, 1, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_YEARLY;
recur.interval = 3;
recur.byweekno = build_list (3, 9, 24, LIST_END);
recur.byday = build_list (3, 0, 5, 0, LIST_END);
do_test ("YEARLY every 3 years in weeks 3, 9, 24 on Thu/Sat",
&event_start, &recur, &interval_start, &interval_end);
set_time (&interval_end, 2002, 0, 1, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_YEARLY;
recur.interval = 1;
recur.bymonth = build_list (0, 6, LIST_END);
recur.byday = build_list (0, 0, 6, 0, LIST_END);
do_test ("YEARLY every year in Jan/Jul on Mon/Sun",
&event_start, &recur, &interval_start, &interval_end);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_YEARLY;
recur.interval = 1;
recur.bymonthday = build_list (3, 7, LIST_END);
do_test ("YEARLY every year on 3rd & 7th of the month",
&event_start, &recur, &interval_start, &interval_end);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_YEARLY;
recur.interval = 1;
recur.byyearday = build_list (15, 126, 360, LIST_END);
do_test ("YEARLY every year on 15th, 126th & 360th day of the year",
&event_start, &recur, &interval_start, &interval_end);
}
static void
test_monthly ()
{
CalObjTime event_start, interval_start, interval_end;
CalObjRecurrence recur;
set_time (&event_start, 2000, 0, 1, 0, 0, 0);
/* We set the interval to a wide range so we just test the event. */
set_time (&interval_start, 2000, 0, 1, 0, 0, 0);
set_time (&interval_end, 2002, 0, 1, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_MONTHLY;
recur.interval = 1;
do_test ("MONTHLY every month",
&event_start, &recur, &interval_start, &interval_end);
}
static void
test_weekly ()
{
CalObjTime event_start, interval_start, interval_end;
CalObjRecurrence recur;
set_time (&event_start, 2000, 0, 1, 0, 0, 0);
/* We set the interval to a wide range so we just test the event. */
set_time (&interval_start, 2000, 0, 1, 0, 0, 0);
set_time (&interval_end, 2002, 0, 1, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_WEEKLY;
recur.interval = 1;
do_test ("WEEKLY every week",
&event_start, &recur, &interval_start, &interval_end);
}
static void
test_daily ()
{
CalObjTime event_start, interval_start, interval_end;
CalObjRecurrence recur;
set_time (&event_start, 2000, 0, 1, 0, 0, 0);
/* We set the interval to a wide range so we just test the event. */
set_time (&interval_start, 2000, 0, 1, 0, 0, 0);
set_time (&interval_end, 2000, 6, 1, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_DAILY;
recur.interval = 1;
do_test ("DAILY every day",
&event_start, &recur, &interval_start, &interval_end);
}
static void
test_hourly ()
{
CalObjTime event_start, interval_start, interval_end;
CalObjRecurrence recur;
set_time (&event_start, 2000, 0, 1, 2, 15, 0);
/* We set the interval to a wide range so we just test the event. */
set_time (&interval_start, 2000, 0, 1, 0, 0, 0);
set_time (&interval_end, 2002, 0, 1, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_HOURLY;
recur.interval = 3;
recur.bymonth = build_list (3, 11, LIST_END);
recur.byday = build_list (2, 0, 4, 0, LIST_END);
do_test ("HOURLY every 3 hours in Apr/Dec on Wed & Fri",
&event_start, &recur, &interval_start, &interval_end);
}
static void
test_minutely ()
{
CalObjTime event_start, interval_start, interval_end;
CalObjRecurrence recur;
set_time (&event_start, 2000, 0, 1, 0, 0, 0);
/* We set the interval to a wide range so we just test the event. */
set_time (&interval_start, 2000, 0, 1, 0, 0, 0);
set_time (&interval_end, 2000, 0, 2, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_MINUTELY;
recur.interval = 45;
do_test ("MINUTELY every 45 minutes",
&event_start, &recur, &interval_start, &interval_end);
}
static void
test_secondly ()
{
CalObjTime event_start, interval_start, interval_end;
CalObjRecurrence recur;
set_time (&event_start, 2000, 0, 1, 0, 0, 0);
/* We set the interval to a wide range so we just test the event. */
set_time (&interval_start, 2000, 0, 1, 0, 0, 0);
set_time (&interval_end, 2000, 0, 2, 0, 0, 0);
memset (&recur, 0, sizeof (recur));
recur.type = CAL_RECUR_SECONDLY;
recur.interval = 15;
recur.byhour = build_list (2, 4, 6, LIST_END);
recur.byminute = build_list (0, 30, LIST_END);
do_test ("SECONDLY every 15 seconds at 2:00,2:30,4:00,4:30,6:00,6:30",
&event_start, &recur, &interval_start, &interval_end);
}
int
main (int argc, char *argv[])
{
test_yearly ();
test_monthly ();
test_weekly ();
test_daily ();
test_hourly ();
test_minutely ();
test_secondly ();
return 0;
}
static void
set_time (CalObjTime *cotime, gint year, gint month, gint day,
gint hour, gint minute, gint second)
{
cotime->year = year;
cotime->month = month;
cotime->day = day;
cotime->hour = hour;
cotime->minute = minute;
cotime->second = second;
}
static GList*
build_list (gint first, ...)
{
va_list args;
GList *list;
gint num;
va_start (args, first);
list = g_list_prepend (NULL, GINT_TO_POINTER (first));
num = va_arg (args, gint);
while (num != LIST_END) {
list = g_list_prepend (list, GINT_TO_POINTER (num));
num = va_arg (args, gint);
}
list = g_list_reverse (list);
va_end (args);
return list;
}
static void
do_test (gchar *description,
CalObjTime *event_start,
CalObjRecurrence *recur,
CalObjTime *interval_start,
CalObjTime *interval_end)
{
GArray *occs;
g_print ("========================================================\n");
g_print ("%s\n", description);
g_print ("(From %s", time_to_string (interval_start));
g_print (" To %s)\n\n", time_to_string (interval_end));
occs = cal_obj_expand_recurrence (event_start, recur,
interval_start, interval_end);
display_occs (occs);
g_array_free (occs, TRUE);
}
static gchar*
time_to_string (CalObjTime *cotime)
{
static gchar buffer[64];
gint month;
gchar *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "XXX" };
month = cotime->month;
if (month < 0 || month > 12)
month = 12;
sprintf (buffer, "%s %2i %02i:%02i:%02i %4i",
months[month], cotime->day,
cotime->hour, cotime->minute, cotime->second,
cotime->year);
return buffer;
}
static void
display_occs (GArray *occs)
{
CalObjTime *occ;
gint len, i;
struct tm t;
len = occs->len;
for (i = 0; i < len; i++) {
occ = &g_array_index (occs, CalObjTime, i);
t.tm_sec = occ->second;
t.tm_min = occ->minute;
t.tm_hour = occ->hour;
t.tm_mday = occ->day;
t.tm_mon = occ->month;
t.tm_year = occ->year - 1900;
t.tm_isdst = -1;
mktime (&t);
g_print ("%s", asctime (&t));
}
}
|