1 #ifdef GLIB_COMPILATION
2 #undef GLIB_COMPILATION
12 gboolean failed = FALSE;
14 guint32 notpassed = 0;
16 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
20 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
22 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
26 if ((passed+notpassed) % 10000 == 0) g_print ("."); fflush (stdout); \
29 void g_date_debug_print(GDate* d)
31 if (!d) g_print("NULL!\n");
33 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
35 d->julian ? "valid" : "invalid",
39 d->dmy ? "valid" : "invalid");
44 int main(int argc, char** argv)
53 /* Try to get all the leap year cases. */
54 GDateYear check_years[] = {
55 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
56 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
57 398, 399, 400, 401, 402, 403, 404, 405, 406,
58 1598, 1599, 1600, 1601, 1602, 1650, 1651,
59 1897, 1898, 1899, 1900, 1901, 1902, 1903,
60 1961, 1962, 1963, 1964, 1965, 1967,
61 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
62 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
63 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
64 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
65 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
66 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
68 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
70 gboolean discontinuity;
72 g_print("checking GDate...");
74 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
78 TEST("Empty constructor produces invalid date", !g_date_valid(d));
82 d = g_date_new_dmy(1,1,1);
84 TEST("January 1, Year 1 created and valid", g_date_valid(d));
86 j = g_date_get_julian(d);
88 TEST("January 1, Year 1 is Julian date 1", j == 1);
90 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
91 TEST("Returned day is 1", g_date_get_day(d) == 1);
92 TEST("Returned year is 1", g_date_get_year(d) == 1);
94 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
95 TEST("Month 13 is invalid", !g_date_valid_month(13));
96 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
97 TEST("Day 32 is invalid", !g_date_valid_day(32));
98 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
99 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
100 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
101 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
102 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
103 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
104 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
105 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
106 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
110 loc = setlocale(LC_ALL,"");
112 g_print("\nLocale set to %s\n", loc);
114 g_print("\nLocale unchanged\n");
117 g_date_set_time(d, time(NULL));
118 TEST("Today is valid", g_date_valid(d));
120 g_date_strftime(buf,100,"Today is a %A, %x\n", d);
123 g_date_set_time(d, 1);
124 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
126 g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
129 g_date_set_julian(d, 1);
130 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
132 g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
136 g_date_set_dmy(d, 10, 1, 2000);
138 g_date_strftime(buf,100,"%x", d);
140 g_date_set_parse(d, buf);
141 /* Note: this test will hopefully work, but no promises. */
142 TEST("Successfully parsed a %x-formatted string",
144 g_date_get_month(d) == 1 &&
145 g_date_get_day(d) == 10 &&
146 g_date_get_year(d) == 2000);
148 g_date_debug_print(d);
152 j = G_DATE_BAD_JULIAN;
155 discontinuity = TRUE;
157 prev_y = G_DATE_BAD_YEAR;
158 while (i < n_check_years)
160 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
161 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
162 guint sunday_week_of_year = 0;
163 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
164 guint monday_week_of_year = 0;
165 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
168 g_print(" (Break in sequence of requested years to check)\n");
170 g_print("Checking year %u", y);
172 TEST("Year is valid", g_date_valid_year(y));
174 TEST("Number of Sunday weeks in year is 52 or 53",
175 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
177 TEST("Number of Monday weeks in year is 52 or 53",
178 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
183 guint8 dim = g_date_get_days_in_month(m,y);
184 GDate days[31]; /* This is the fast way, no allocation */
186 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
188 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
192 g_date_clear(days, 31);
199 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
201 /* Create GDate with triplet */
205 TEST("Cleared day is invalid", !g_date_valid(d));
207 g_date_set_dmy(d,day,m,y);
209 TEST("Set day is valid", g_date_valid(d));
211 if (m == G_DATE_JANUARY && day == 1)
213 first_day_of_year = g_date_get_julian(d);
216 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
218 TEST("Date with DMY triplet is valid", g_date_valid(d));
219 TEST("Month accessor works", g_date_get_month(d) == m);
220 TEST("Year accessor works", g_date_get_year(d) == y);
221 TEST("Day of month accessor works", g_date_get_day(d) == day);
223 TEST("Day of year is consistent with Julian dates",
224 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
225 (g_date_get_day_of_year(d))));
229 g_print("first day: %u this day: %u day of year: %u\n",
231 g_date_get_julian(d),
232 g_date_get_day_of_year(d));
235 if (m == G_DATE_DECEMBER && day == 31)
237 TEST("Last day of year equals number of days in year",
238 g_date_get_day_of_year(d) == days_in_year);
241 g_print("last day: %u days in year: %u\n",
242 g_date_get_day_of_year(d), days_in_year);
246 TEST("Day of year is not more than number of days in the year",
247 g_date_get_day_of_year(d) <= days_in_year);
249 TEST("Monday week of year is not more than number of weeks in the year",
250 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
253 g_print("Weeks in year: %u\n", monday_weeks_in_year);
254 g_date_debug_print(d);
256 TEST("Monday week of year is >= than last week of year",
257 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
259 if (g_date_get_weekday(d) == G_DATE_MONDAY)
262 TEST("Monday week of year on Monday 1 more than previous day's week of year",
263 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
267 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
268 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
272 monday_week_of_year = g_date_get_monday_week_of_year(d);
275 TEST("Sunday week of year is not more than number of weeks in the year",
276 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
279 g_date_debug_print(d);
281 TEST("Sunday week of year is >= than last week of year",
282 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
284 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
286 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
287 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
291 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
292 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
295 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
297 TEST("Date is equal to itself",
298 g_date_compare(d,d) == 0);
301 /*************** Increments ***********/
304 while (i < 402) /* Need to get 400 year increments in */
309 g_date_add_days(d, i);
311 TEST("Adding days gives a value greater than previous",
312 g_date_compare(d, &tmp) > 0);
314 g_date_subtract_days(d, i);
315 TEST("Forward days then backward days returns us to current day",
316 g_date_get_day(d) == day);
320 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
321 g_date_debug_print(d);
324 TEST("Forward days then backward days returns us to current month",
325 g_date_get_month(d) == m);
329 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
330 g_date_debug_print(d);
333 TEST("Forward days then backward days returns us to current year",
334 g_date_get_year(d) == y);
338 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
339 g_date_debug_print(d);
342 /******* Months ********/
345 g_date_add_months(d, i);
346 TEST("Adding months gives a larger value",
347 g_date_compare(d, &tmp) > 0);
348 g_date_subtract_months(d, i);
350 TEST("Forward months then backward months returns us to current month",
351 g_date_get_month(d) == m);
355 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
356 g_date_debug_print(d);
359 TEST("Forward months then backward months returns us to current year",
360 g_date_get_year(d) == y);
364 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
365 g_date_debug_print(d);
371 /* Day should be unchanged */
373 TEST("Forward months then backward months returns us to current day",
374 g_date_get_day(d) == day);
378 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
379 g_date_debug_print(d);
384 /* reset the day for later tests */
385 g_date_set_day(d, day);
388 /******* Years ********/
391 g_date_add_years(d, i);
393 TEST("Adding years gives a larger value",
394 g_date_compare(d,&tmp) > 0);
396 g_date_subtract_years(d, i);
398 TEST("Forward years then backward years returns us to current month",
399 g_date_get_month(d) == m);
403 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
404 g_date_debug_print(d);
407 TEST("Forward years then backward years returns us to current year",
408 g_date_get_year(d) == y);
412 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
413 g_date_debug_print(d);
416 if (m != 2 && day != 29)
418 TEST("Forward years then backward years returns us to current day",
419 g_date_get_day(d) == day);
423 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
424 g_date_debug_print(d);
429 g_date_set_day(d, day); /* reset */
435 /***** increment test relative to our local Julian count */
437 if (!discontinuity) {
439 /* We can only run sequence tests between sequential years */
441 TEST("Julians are sequential with increment 1",
442 j+1 == g_date_get_julian(d));
445 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
446 j, j+1, g_date_get_julian(d));
449 g_date_add_days(d,1);
450 TEST("Next day has julian 1 higher",
451 g_date_get_julian(d) == j + 2);
452 g_date_subtract_days(d, 1);
454 if (j != G_DATE_BAD_JULIAN)
456 g_date_subtract_days(d, 1);
458 TEST("Previous day has julian 1 lower",
459 g_date_get_julian(d) == j);
461 g_date_add_days(d, 1); /* back to original */
464 discontinuity = FALSE; /* goes away now */
469 j = g_date_get_julian(d); /* inc current julian */
479 if (prev_y == G_DATE_BAD_YEAR ||
480 (prev_y + 1) != y) discontinuity = TRUE;
484 g_print("\n%u tests passed, %u failed\n",passed, notpassed);