add for(;;); after the g_log call so that GCC stops issuing false warnings
[glib.git] / tests / date-test.c
blob1a350301bce376fc33bb748b8834570972290664
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include "glib.h"
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdlib.h>
9 #include <locale.h>
10 #include <time.h>
12 gboolean failed = FALSE;
13 guint32 passed = 0;
14 guint32 notpassed = 0;
16 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
17 if (failed) \
18 exit(1); \
19 } G_STMT_END
21 void g_date_debug_print(GDate* d)
25 void g_print_dummy(const char *format, ...)
29 void fflush_dummy (FILE *f)
34 #define g_print g_print_dummy
35 #define fflush fflush_dummy
37 int main(int argc, char** argv)
39 GDate* d;
40 guint32 j;
41 GDateMonth m;
42 GDateYear y, prev_y;
43 GDateDay day;
44 gchar buf[101];
45 gchar* loc;
47 /* Try to get all the leap year cases. */
48 GDateYear check_years[] = {
49 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
50 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
51 398, 399, 400, 401, 402, 403, 404, 405, 406,
52 1598, 1599, 1600, 1601, 1602, 1650, 1651,
53 1897, 1898, 1899, 1900, 1901, 1902, 1903,
54 1961, 1962, 1963, 1964, 1965, 1967,
55 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
56 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
57 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
58 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
59 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
60 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
63 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
65 guint i = 0;
66 gboolean discontinuity = FALSE;
68 g_print("checking GDate...");
70 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
72 d = g_date_new();
74 TEST("Empty constructor produces invalid date", !g_date_valid(d));
76 g_date_free(d);
78 d = g_date_new_dmy(1,1,1);
80 TEST("January 1, Year 1 created and valid", g_date_valid(d));
82 j = g_date_get_julian(d);
84 TEST("January 1, Year 1 is Julian date 1", j == 1);
86 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
87 TEST("Returned day is 1", g_date_get_day(d) == 1);
88 TEST("Returned year is 1", g_date_get_year(d) == 1);
90 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
91 TEST("Month 13 is invalid", !g_date_valid_month(13));
92 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
93 TEST("Day 32 is invalid", !g_date_valid_day(32));
94 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
95 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
96 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
97 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
98 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
99 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
100 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
101 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
102 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
104 g_date_free(d);
106 loc = setlocale(LC_ALL,"");
107 if (loc)
108 g_print("\nLocale set to %s\n", loc);
109 else
110 g_print("\nLocale unchanged\n");
112 d = g_date_new();
113 g_date_set_time(d, time(NULL));
114 TEST("Today is valid", g_date_valid(d));
116 g_date_strftime(buf,100,"Today is a %A, %x\n", d);
117 g_print("%s", buf);
119 g_date_set_time(d, 1);
120 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
122 g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
123 g_print("%s", buf);
125 g_date_set_julian(d, 1);
126 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
128 g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
130 g_print("%s", buf);
132 g_date_set_dmy(d, 10, 1, 2000);
134 g_date_strftime(buf,100,"%x", d);
136 g_date_set_parse(d, buf);
137 /* Note: this test will hopefully work, but no promises. */
138 TEST("Successfully parsed a %x-formatted string",
139 g_date_valid(d) &&
140 g_date_get_month(d) == 1 &&
141 g_date_get_day(d) == 10 &&
142 g_date_get_year(d) == 2000);
143 if (failed)
144 g_date_debug_print(d);
146 g_date_free(d);
148 j = G_DATE_BAD_JULIAN;
150 i = 0;
151 discontinuity = TRUE;
152 y = check_years[0];
153 prev_y = G_DATE_BAD_YEAR;
154 while (i < n_check_years)
156 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
157 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
158 guint sunday_week_of_year = 0;
159 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
160 guint monday_week_of_year = 0;
161 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
162 guint iso8601_week_of_year = 0;
164 if (discontinuity)
165 g_print(" (Break in sequence of requested years to check)\n");
167 g_print("Checking year %u", y);
169 TEST("Year is valid", g_date_valid_year(y));
171 TEST("Number of Sunday weeks in year is 52 or 53",
172 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
174 TEST("Number of Monday weeks in year is 52 or 53",
175 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
177 m = 1;
178 while (m < 13)
180 guint8 dim = g_date_get_days_in_month(m,y);
181 GDate days[31]; /* This is the fast way, no allocation */
183 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
185 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
187 day = 1;
189 g_date_clear(days, 31);
191 while (day <= dim)
193 guint i;
194 GDate tmp;
196 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
198 /* Create GDate with triplet */
200 d = &days[day-1];
202 TEST("Cleared day is invalid", !g_date_valid(d));
204 g_date_set_dmy(d,day,m,y);
206 TEST("Set day is valid", g_date_valid(d));
208 if (m == G_DATE_JANUARY && day == 1)
210 first_day_of_year = g_date_get_julian(d);
213 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
215 TEST("Date with DMY triplet is valid", g_date_valid(d));
216 TEST("Month accessor works", g_date_get_month(d) == m);
217 TEST("Year accessor works", g_date_get_year(d) == y);
218 TEST("Day of month accessor works", g_date_get_day(d) == day);
220 TEST("Day of year is consistent with Julian dates",
221 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
222 (g_date_get_day_of_year(d))));
224 if (failed)
226 g_print("first day: %u this day: %u day of year: %u\n",
227 first_day_of_year,
228 g_date_get_julian(d),
229 g_date_get_day_of_year(d));
232 if (m == G_DATE_DECEMBER && day == 31)
234 TEST("Last day of year equals number of days in year",
235 g_date_get_day_of_year(d) == days_in_year);
236 if (failed)
238 g_print("last day: %u days in year: %u\n",
239 g_date_get_day_of_year(d), days_in_year);
243 TEST("Day of year is not more than number of days in the year",
244 g_date_get_day_of_year(d) <= days_in_year);
246 TEST("Monday week of year is not more than number of weeks in the year",
247 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
248 if (failed)
250 g_print("Weeks in year: %u\n", monday_weeks_in_year);
251 g_date_debug_print(d);
253 TEST("Monday week of year is >= than last week of year",
254 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
256 if (g_date_get_weekday(d) == G_DATE_MONDAY)
259 TEST("Monday week of year on Monday 1 more than previous day's week of year",
260 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
261 if ((m == G_DATE_JANUARY && day <= 4) ||
262 (m == G_DATE_DECEMBER && day >= 29)) {
263 TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
264 (g_date_get_iso8601_week_of_year(d) == 1));
265 } else {
266 TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
267 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
270 else
272 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
273 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
274 if (!(day == 1 && m == G_DATE_JANUARY)) {
275 TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
276 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
281 monday_week_of_year = g_date_get_monday_week_of_year(d);
282 iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
285 TEST("Sunday week of year is not more than number of weeks in the year",
286 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
287 if (failed)
289 g_date_debug_print(d);
291 TEST("Sunday week of year is >= than last week of year",
292 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
294 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
296 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
297 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
299 else
301 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
302 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
305 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
307 TEST("Date is equal to itself",
308 g_date_compare(d,d) == 0);
311 /*************** Increments ***********/
313 i = 1;
314 while (i < 402) /* Need to get 400 year increments in */
317 /***** Days ******/
318 tmp = *d;
319 g_date_add_days(d, i);
321 TEST("Adding days gives a value greater than previous",
322 g_date_compare(d, &tmp) > 0);
324 g_date_subtract_days(d, i);
325 TEST("Forward days then backward days returns us to current day",
326 g_date_get_day(d) == day);
328 if (failed)
330 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
331 g_date_debug_print(d);
334 TEST("Forward days then backward days returns us to current month",
335 g_date_get_month(d) == m);
337 if (failed)
339 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
340 g_date_debug_print(d);
343 TEST("Forward days then backward days returns us to current year",
344 g_date_get_year(d) == y);
346 if (failed)
348 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
349 g_date_debug_print(d);
352 /******* Months ********/
354 tmp = *d;
355 g_date_add_months(d, i);
356 TEST("Adding months gives a larger value",
357 g_date_compare(d, &tmp) > 0);
358 g_date_subtract_months(d, i);
360 TEST("Forward months then backward months returns us to current month",
361 g_date_get_month(d) == m);
363 if (failed)
365 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
366 g_date_debug_print(d);
369 TEST("Forward months then backward months returns us to current year",
370 g_date_get_year(d) == y);
372 if (failed)
374 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
375 g_date_debug_print(d);
379 if (day < 29)
381 /* Day should be unchanged */
383 TEST("Forward months then backward months returns us to current day",
384 g_date_get_day(d) == day);
386 if (failed)
388 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
389 g_date_debug_print(d);
392 else
394 /* reset the day for later tests */
395 g_date_set_day(d, day);
398 /******* Years ********/
400 tmp = *d;
401 g_date_add_years(d, i);
403 TEST("Adding years gives a larger value",
404 g_date_compare(d,&tmp) > 0);
406 g_date_subtract_years(d, i);
408 TEST("Forward years then backward years returns us to current month",
409 g_date_get_month(d) == m);
411 if (failed)
413 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
414 g_date_debug_print(d);
417 TEST("Forward years then backward years returns us to current year",
418 g_date_get_year(d) == y);
420 if (failed)
422 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
423 g_date_debug_print(d);
426 if (m != 2 && day != 29)
428 TEST("Forward years then backward years returns us to current day",
429 g_date_get_day(d) == day);
431 if (failed)
433 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
434 g_date_debug_print(d);
437 else
439 g_date_set_day(d, day); /* reset */
442 i += 10;
445 /***** increment test relative to our local Julian count */
447 if (!discontinuity) {
449 /* We can only run sequence tests between sequential years */
451 TEST("Julians are sequential with increment 1",
452 j+1 == g_date_get_julian(d));
453 if (failed)
455 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
456 j, j+1, g_date_get_julian(d));
459 g_date_add_days(d,1);
460 TEST("Next day has julian 1 higher",
461 g_date_get_julian(d) == j + 2);
462 g_date_subtract_days(d, 1);
464 if (j != G_DATE_BAD_JULIAN)
466 g_date_subtract_days(d, 1);
468 TEST("Previous day has julian 1 lower",
469 g_date_get_julian(d) == j);
471 g_date_add_days(d, 1); /* back to original */
474 discontinuity = FALSE; /* goes away now */
476 fflush(stdout);
477 fflush(stderr);
479 j = g_date_get_julian(d); /* inc current julian */
481 ++day;
483 ++m;
485 g_print(" done\n");
486 ++i;
487 prev_y = y;
488 y = check_years[i];
489 if (prev_y == G_DATE_BAD_YEAR ||
490 (prev_y + 1) != y) discontinuity = TRUE;
494 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
496 return 0;