1 #undef G_DISABLE_ASSERT
4 #ifdef GLIB_COMPILATION
5 #undef GLIB_COMPILATION
16 gboolean failed = FALSE;
18 guint32 notpassed = 0;
20 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
24 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
26 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
30 if ((passed+notpassed) % 10000 == 0) \
36 g_date_debug_print (GDate* d)
38 if (!d) g_print("NULL!\n");
40 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
42 d->julian ? "valid" : "invalid",
46 d->dmy ? "valid" : "invalid");
51 int main(int argc, char** argv)
60 /* Try to get all the leap year cases. */
61 GDateYear check_years[] = {
62 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
63 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
64 398, 399, 400, 401, 402, 403, 404, 405, 406,
65 1598, 1599, 1600, 1601, 1602, 1650, 1651,
66 1897, 1898, 1899, 1900, 1901, 1902, 1903,
67 1961, 1962, 1963, 1964, 1965, 1967,
68 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
69 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
70 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
71 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
72 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
73 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
75 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
77 gboolean discontinuity;
79 g_print("checking GDate...");
81 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
85 TEST("Empty constructor produces invalid date", !g_date_valid(d));
89 d = g_date_new_dmy(1,1,1);
91 TEST("January 1, Year 1 created and valid", g_date_valid(d));
93 j = g_date_get_julian(d);
95 TEST("January 1, Year 1 is Julian date 1", j == 1);
97 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
98 TEST("Returned day is 1", g_date_get_day(d) == 1);
99 TEST("Returned year is 1", g_date_get_year(d) == 1);
101 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
102 TEST("Month 13 is invalid", !g_date_valid_month(13));
103 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
104 TEST("Day 32 is invalid", !g_date_valid_day(32));
105 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
106 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
107 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
108 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
109 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
110 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
111 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
112 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
113 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
117 loc = setlocale(LC_ALL,"");
119 g_print("\nLocale set to %s\n", loc);
121 g_print("\nLocale unchanged\n");
124 g_date_set_time(d, time(NULL));
125 TEST("Today is valid", g_date_valid(d));
127 g_date_strftime(buf,100,"Today is a %A, %x\n", d);
130 g_date_set_time(d, 1);
131 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
133 g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
136 g_date_set_julian(d, 1);
137 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
140 g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
144 g_print ("But Windows FILETIME does not support dates before Jan 1 1601, so we can't strftime() the beginning of the \"Julian\" epoch.\n");
146 g_date_set_dmy(d, 10, 1, 2000);
148 g_date_strftime(buf,100,"%x", d);
150 g_date_set_parse(d, buf);
151 /* Note: this test will hopefully work, but no promises. */
152 TEST("Successfully parsed a %x-formatted string",
154 g_date_get_month(d) == 1 &&
155 g_date_get_day(d) == 10 &&
156 g_date_get_year(d) == 2000);
158 g_date_debug_print(d);
162 j = G_DATE_BAD_JULIAN;
165 discontinuity = TRUE;
167 prev_y = G_DATE_BAD_YEAR;
168 g_print ("testing %d years\n", n_check_years);
169 while (i < n_check_years)
171 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
172 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
173 guint sunday_week_of_year = 0;
174 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
175 guint monday_week_of_year = 0;
176 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
177 guint iso8601_week_of_year = 0;
180 g_print(" (Break in sequence of requested years to check)\n");
182 g_print("Checking year %u", y);
184 TEST("Year is valid", g_date_valid_year(y));
186 TEST("Number of Sunday weeks in year is 52 or 53",
187 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
189 TEST("Number of Monday weeks in year is 52 or 53",
190 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
195 guint8 dim = g_date_get_days_in_month(m,y);
196 GDate days[31]; /* This is the fast way, no allocation */
198 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
200 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
204 g_date_clear(days, 31);
210 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
212 /* Create GDate with triplet */
216 TEST("Cleared day is invalid", !g_date_valid(d));
218 g_date_set_dmy(d,day,m,y);
220 TEST("Set day is valid", g_date_valid(d));
222 if (m == G_DATE_JANUARY && day == 1)
224 first_day_of_year = g_date_get_julian(d);
227 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
229 TEST("Date with DMY triplet is valid", g_date_valid(d));
230 TEST("Month accessor works", g_date_get_month(d) == m);
231 TEST("Year accessor works", g_date_get_year(d) == y);
232 TEST("Day of month accessor works", g_date_get_day(d) == day);
234 TEST("Day of year is consistent with Julian dates",
235 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
236 (g_date_get_day_of_year(d))));
240 g_print("first day: %u this day: %u day of year: %u\n",
242 g_date_get_julian(d),
243 g_date_get_day_of_year(d));
246 if (m == G_DATE_DECEMBER && day == 31)
248 TEST("Last day of year equals number of days in year",
249 g_date_get_day_of_year(d) == days_in_year);
252 g_print("last day: %u days in year: %u\n",
253 g_date_get_day_of_year(d), days_in_year);
257 TEST("Day of year is not more than number of days in the year",
258 g_date_get_day_of_year(d) <= days_in_year);
260 TEST("Monday week of year is not more than number of weeks in the year",
261 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
264 g_print("Weeks in year: %u\n", monday_weeks_in_year);
265 g_date_debug_print(d);
267 TEST("Monday week of year is >= than last week of year",
268 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
270 if (g_date_get_weekday(d) == G_DATE_MONDAY)
273 TEST("Monday week of year on Monday 1 more than previous day's week of year",
274 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
275 if ((m == G_DATE_JANUARY && day <= 4) ||
276 (m == G_DATE_DECEMBER && day >= 29)) {
277 TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
278 (g_date_get_iso8601_week_of_year(d) == 1));
280 TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
281 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
286 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
287 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
288 if (!(day == 1 && m == G_DATE_JANUARY)) {
289 TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
290 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
295 monday_week_of_year = g_date_get_monday_week_of_year(d);
296 iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
299 TEST("Sunday week of year is not more than number of weeks in the year",
300 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
303 g_date_debug_print(d);
305 TEST("Sunday week of year is >= than last week of year",
306 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
308 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
310 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
311 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
315 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
316 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
319 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
321 TEST("Date is equal to itself",
322 g_date_compare(d,d) == 0);
325 /*************** Increments ***********/
328 while (k < 402) /* Need to get 400 year increments in */
333 g_date_add_days(d, k);
335 TEST("Adding days gives a value greater than previous",
336 g_date_compare(d, &tmp) > 0);
338 g_date_subtract_days(d, k);
339 TEST("Forward days then backward days returns us to current day",
340 g_date_get_day(d) == day);
344 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
345 g_date_debug_print(d);
348 TEST("Forward days then backward days returns us to current month",
349 g_date_get_month(d) == m);
353 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
354 g_date_debug_print(d);
357 TEST("Forward days then backward days returns us to current year",
358 g_date_get_year(d) == y);
362 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
363 g_date_debug_print(d);
366 /******* Months ********/
369 g_date_add_months(d, k);
370 TEST("Adding months gives a larger value",
371 g_date_compare(d, &tmp) > 0);
372 g_date_subtract_months(d, k);
374 TEST("Forward months then backward months returns us to current month",
375 g_date_get_month(d) == m);
379 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
380 g_date_debug_print(d);
383 TEST("Forward months then backward months returns us to current year",
384 g_date_get_year(d) == y);
388 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
389 g_date_debug_print(d);
395 /* Day should be unchanged */
397 TEST("Forward months then backward months returns us to current day",
398 g_date_get_day(d) == day);
402 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
403 g_date_debug_print(d);
408 /* reset the day for later tests */
409 g_date_set_day(d, day);
412 /******* Years ********/
415 g_date_add_years(d, k);
417 TEST("Adding years gives a larger value",
418 g_date_compare(d,&tmp) > 0);
420 g_date_subtract_years(d, k);
422 TEST("Forward years then backward years returns us to current month",
423 g_date_get_month(d) == m);
427 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
428 g_date_debug_print(d);
431 TEST("Forward years then backward years returns us to current year",
432 g_date_get_year(d) == y);
436 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
437 g_date_debug_print(d);
440 if (m != 2 && day != 29)
442 TEST("Forward years then backward years returns us to current day",
443 g_date_get_day(d) == day);
447 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
448 g_date_debug_print(d);
453 g_date_set_day(d, day); /* reset */
459 /***** increment test relative to our local Julian count */
461 if (!discontinuity) {
463 /* We can only run sequence tests between sequential years */
465 TEST("Julians are sequential with increment 1",
466 j+1 == g_date_get_julian(d));
469 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
470 j, j+1, g_date_get_julian(d));
473 g_date_add_days(d,1);
474 TEST("Next day has julian 1 higher",
475 g_date_get_julian(d) == j + 2);
476 g_date_subtract_days(d, 1);
478 if (j != G_DATE_BAD_JULIAN)
480 g_date_subtract_days(d, 1);
482 TEST("Previous day has julian 1 lower",
483 g_date_get_julian(d) == j);
485 g_date_add_days(d, 1); /* back to original */
488 discontinuity = FALSE; /* goes away now */
493 j = g_date_get_julian(d); /* inc current julian */
501 if (i == n_check_years)
505 if (prev_y == G_DATE_BAD_YEAR ||
506 (prev_y + 1) != y) discontinuity = TRUE;
510 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
512 return (notpassed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;