1 #undef G_DISABLE_ASSERT
4 #ifdef GLIB_COMPILATION
5 #undef GLIB_COMPILATION
15 gboolean failed = FALSE;
17 guint32 notpassed = 0;
19 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
23 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
25 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
29 if ((passed+notpassed) % 10000 == 0) g_print ("."); fflush (stdout); \
32 void g_date_debug_print(GDate* d)
34 if (!d) g_print("NULL!\n");
36 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
38 d->julian ? "valid" : "invalid",
42 d->dmy ? "valid" : "invalid");
47 int main(int argc, char** argv)
56 /* Try to get all the leap year cases. */
57 GDateYear check_years[] = {
58 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
59 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
60 398, 399, 400, 401, 402, 403, 404, 405, 406,
61 1598, 1599, 1600, 1601, 1602, 1650, 1651,
62 1897, 1898, 1899, 1900, 1901, 1902, 1903,
63 1961, 1962, 1963, 1964, 1965, 1967,
64 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
65 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
66 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
67 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
68 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
69 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
71 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
73 gboolean discontinuity;
75 g_print("checking GDate...");
77 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
81 TEST("Empty constructor produces invalid date", !g_date_valid(d));
85 d = g_date_new_dmy(1,1,1);
87 TEST("January 1, Year 1 created and valid", g_date_valid(d));
89 j = g_date_get_julian(d);
91 TEST("January 1, Year 1 is Julian date 1", j == 1);
93 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
94 TEST("Returned day is 1", g_date_get_day(d) == 1);
95 TEST("Returned year is 1", g_date_get_year(d) == 1);
97 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
98 TEST("Month 13 is invalid", !g_date_valid_month(13));
99 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
100 TEST("Day 32 is invalid", !g_date_valid_day(32));
101 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
102 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
103 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
104 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
105 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
106 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
107 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
108 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
109 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
113 loc = setlocale(LC_ALL,"");
115 g_print("\nLocale set to %s\n", loc);
117 g_print("\nLocale unchanged\n");
120 g_date_set_time(d, time(NULL));
121 TEST("Today is valid", g_date_valid(d));
123 g_date_strftime(buf,100,"Today is a %A, %x\n", d);
126 g_date_set_time(d, 1);
127 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
129 g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
132 g_date_set_julian(d, 1);
133 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
135 g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
139 g_date_set_dmy(d, 10, 1, 2000);
141 g_date_strftime(buf,100,"%x", d);
143 g_date_set_parse(d, buf);
144 /* Note: this test will hopefully work, but no promises. */
145 TEST("Successfully parsed a %x-formatted string",
147 g_date_get_month(d) == 1 &&
148 g_date_get_day(d) == 10 &&
149 g_date_get_year(d) == 2000);
151 g_date_debug_print(d);
155 j = G_DATE_BAD_JULIAN;
158 discontinuity = TRUE;
160 prev_y = G_DATE_BAD_YEAR;
161 while (i < n_check_years)
163 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
164 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
165 guint sunday_week_of_year = 0;
166 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
167 guint monday_week_of_year = 0;
168 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
169 guint iso8601_week_of_year = 0;
172 g_print(" (Break in sequence of requested years to check)\n");
174 g_print("Checking year %u", y);
176 TEST("Year is valid", g_date_valid_year(y));
178 TEST("Number of Sunday weeks in year is 52 or 53",
179 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
181 TEST("Number of Monday weeks in year is 52 or 53",
182 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
187 guint8 dim = g_date_get_days_in_month(m,y);
188 GDate days[31]; /* This is the fast way, no allocation */
190 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
192 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
196 g_date_clear(days, 31);
203 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
205 /* Create GDate with triplet */
209 TEST("Cleared day is invalid", !g_date_valid(d));
211 g_date_set_dmy(d,day,m,y);
213 TEST("Set day is valid", g_date_valid(d));
215 if (m == G_DATE_JANUARY && day == 1)
217 first_day_of_year = g_date_get_julian(d);
220 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
222 TEST("Date with DMY triplet is valid", g_date_valid(d));
223 TEST("Month accessor works", g_date_get_month(d) == m);
224 TEST("Year accessor works", g_date_get_year(d) == y);
225 TEST("Day of month accessor works", g_date_get_day(d) == day);
227 TEST("Day of year is consistent with Julian dates",
228 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
229 (g_date_get_day_of_year(d))));
233 g_print("first day: %u this day: %u day of year: %u\n",
235 g_date_get_julian(d),
236 g_date_get_day_of_year(d));
239 if (m == G_DATE_DECEMBER && day == 31)
241 TEST("Last day of year equals number of days in year",
242 g_date_get_day_of_year(d) == days_in_year);
245 g_print("last day: %u days in year: %u\n",
246 g_date_get_day_of_year(d), days_in_year);
250 TEST("Day of year is not more than number of days in the year",
251 g_date_get_day_of_year(d) <= days_in_year);
253 TEST("Monday week of year is not more than number of weeks in the year",
254 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
257 g_print("Weeks in year: %u\n", monday_weeks_in_year);
258 g_date_debug_print(d);
260 TEST("Monday week of year is >= than last week of year",
261 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
263 if (g_date_get_weekday(d) == G_DATE_MONDAY)
266 TEST("Monday week of year on Monday 1 more than previous day's week of year",
267 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
268 if ((m == G_DATE_JANUARY && day <= 4) ||
269 (m == G_DATE_DECEMBER && day >= 29)) {
270 TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
271 (g_date_get_iso8601_week_of_year(d) == 1));
273 TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
274 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
279 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
280 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
281 if (!(day == 1 && m == G_DATE_JANUARY)) {
282 TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
283 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
288 monday_week_of_year = g_date_get_monday_week_of_year(d);
289 iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
292 TEST("Sunday week of year is not more than number of weeks in the year",
293 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
296 g_date_debug_print(d);
298 TEST("Sunday week of year is >= than last week of year",
299 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
301 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
303 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
304 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
308 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
309 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
312 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
314 TEST("Date is equal to itself",
315 g_date_compare(d,d) == 0);
318 /*************** Increments ***********/
321 while (i < 402) /* Need to get 400 year increments in */
326 g_date_add_days(d, i);
328 TEST("Adding days gives a value greater than previous",
329 g_date_compare(d, &tmp) > 0);
331 g_date_subtract_days(d, i);
332 TEST("Forward days then backward days returns us to current day",
333 g_date_get_day(d) == day);
337 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
338 g_date_debug_print(d);
341 TEST("Forward days then backward days returns us to current month",
342 g_date_get_month(d) == m);
346 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
347 g_date_debug_print(d);
350 TEST("Forward days then backward days returns us to current year",
351 g_date_get_year(d) == y);
355 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
356 g_date_debug_print(d);
359 /******* Months ********/
362 g_date_add_months(d, i);
363 TEST("Adding months gives a larger value",
364 g_date_compare(d, &tmp) > 0);
365 g_date_subtract_months(d, i);
367 TEST("Forward months then backward months returns us to current month",
368 g_date_get_month(d) == m);
372 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
373 g_date_debug_print(d);
376 TEST("Forward months then backward months returns us to current year",
377 g_date_get_year(d) == y);
381 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
382 g_date_debug_print(d);
388 /* Day should be unchanged */
390 TEST("Forward months then backward months returns us to current day",
391 g_date_get_day(d) == day);
395 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
396 g_date_debug_print(d);
401 /* reset the day for later tests */
402 g_date_set_day(d, day);
405 /******* Years ********/
408 g_date_add_years(d, i);
410 TEST("Adding years gives a larger value",
411 g_date_compare(d,&tmp) > 0);
413 g_date_subtract_years(d, i);
415 TEST("Forward years then backward years returns us to current month",
416 g_date_get_month(d) == m);
420 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
421 g_date_debug_print(d);
424 TEST("Forward years then backward years returns us to current year",
425 g_date_get_year(d) == y);
429 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
430 g_date_debug_print(d);
433 if (m != 2 && day != 29)
435 TEST("Forward years then backward years returns us to current day",
436 g_date_get_day(d) == day);
440 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
441 g_date_debug_print(d);
446 g_date_set_day(d, day); /* reset */
452 /***** increment test relative to our local Julian count */
454 if (!discontinuity) {
456 /* We can only run sequence tests between sequential years */
458 TEST("Julians are sequential with increment 1",
459 j+1 == g_date_get_julian(d));
462 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
463 j, j+1, g_date_get_julian(d));
466 g_date_add_days(d,1);
467 TEST("Next day has julian 1 higher",
468 g_date_get_julian(d) == j + 2);
469 g_date_subtract_days(d, 1);
471 if (j != G_DATE_BAD_JULIAN)
473 g_date_subtract_days(d, 1);
475 TEST("Previous day has julian 1 lower",
476 g_date_get_julian(d) == j);
478 g_date_add_days(d, 1); /* back to original */
481 discontinuity = FALSE; /* goes away now */
486 j = g_date_get_julian(d); /* inc current julian */
496 if (prev_y == G_DATE_BAD_YEAR ||
497 (prev_y + 1) != y) discontinuity = TRUE;
501 g_print("\n%u tests passed, %u failed\n",passed, notpassed);