Make prefix argument const. (#91662, Gustavo Carneiro)
[glib.git] / tests / date-test.c
blobd6e1d72493407aa7626c44d5f5d803d720860eec
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);
163 if (discontinuity)
164 g_print(" (Break in sequence of requested years to check)\n");
166 g_print("Checking year %u", y);
168 TEST("Year is valid", g_date_valid_year(y));
170 TEST("Number of Sunday weeks in year is 52 or 53",
171 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
173 TEST("Number of Monday weeks in year is 52 or 53",
174 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
176 m = 1;
177 while (m < 13)
179 guint8 dim = g_date_get_days_in_month(m,y);
180 GDate days[31]; /* This is the fast way, no allocation */
182 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
184 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
186 day = 1;
188 g_date_clear(days, 31);
190 while (day <= dim)
192 guint i;
193 GDate tmp;
195 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
197 /* Create GDate with triplet */
199 d = &days[day-1];
201 TEST("Cleared day is invalid", !g_date_valid(d));
203 g_date_set_dmy(d,day,m,y);
205 TEST("Set day is valid", g_date_valid(d));
207 if (m == G_DATE_JANUARY && day == 1)
209 first_day_of_year = g_date_get_julian(d);
212 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
214 TEST("Date with DMY triplet is valid", g_date_valid(d));
215 TEST("Month accessor works", g_date_get_month(d) == m);
216 TEST("Year accessor works", g_date_get_year(d) == y);
217 TEST("Day of month accessor works", g_date_get_day(d) == day);
219 TEST("Day of year is consistent with Julian dates",
220 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
221 (g_date_get_day_of_year(d))));
223 if (failed)
225 g_print("first day: %u this day: %u day of year: %u\n",
226 first_day_of_year,
227 g_date_get_julian(d),
228 g_date_get_day_of_year(d));
231 if (m == G_DATE_DECEMBER && day == 31)
233 TEST("Last day of year equals number of days in year",
234 g_date_get_day_of_year(d) == days_in_year);
235 if (failed)
237 g_print("last day: %u days in year: %u\n",
238 g_date_get_day_of_year(d), days_in_year);
242 TEST("Day of year is not more than number of days in the year",
243 g_date_get_day_of_year(d) <= days_in_year);
245 TEST("Monday week of year is not more than number of weeks in the year",
246 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
247 if (failed)
249 g_print("Weeks in year: %u\n", monday_weeks_in_year);
250 g_date_debug_print(d);
252 TEST("Monday week of year is >= than last week of year",
253 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
255 if (g_date_get_weekday(d) == G_DATE_MONDAY)
258 TEST("Monday week of year on Monday 1 more than previous day's week of year",
259 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
261 else
263 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
264 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
268 monday_week_of_year = g_date_get_monday_week_of_year(d);
271 TEST("Sunday week of year is not more than number of weeks in the year",
272 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
273 if (failed)
275 g_date_debug_print(d);
277 TEST("Sunday week of year is >= than last week of year",
278 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
280 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
282 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
283 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
285 else
287 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
288 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
291 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
293 TEST("Date is equal to itself",
294 g_date_compare(d,d) == 0);
297 /*************** Increments ***********/
299 i = 1;
300 while (i < 402) /* Need to get 400 year increments in */
303 /***** Days ******/
304 tmp = *d;
305 g_date_add_days(d, i);
307 TEST("Adding days gives a value greater than previous",
308 g_date_compare(d, &tmp) > 0);
310 g_date_subtract_days(d, i);
311 TEST("Forward days then backward days returns us to current day",
312 g_date_get_day(d) == day);
314 if (failed)
316 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
317 g_date_debug_print(d);
320 TEST("Forward days then backward days returns us to current month",
321 g_date_get_month(d) == m);
323 if (failed)
325 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
326 g_date_debug_print(d);
329 TEST("Forward days then backward days returns us to current year",
330 g_date_get_year(d) == y);
332 if (failed)
334 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
335 g_date_debug_print(d);
338 /******* Months ********/
340 tmp = *d;
341 g_date_add_months(d, i);
342 TEST("Adding months gives a larger value",
343 g_date_compare(d, &tmp) > 0);
344 g_date_subtract_months(d, i);
346 TEST("Forward months then backward months returns us to current month",
347 g_date_get_month(d) == m);
349 if (failed)
351 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
352 g_date_debug_print(d);
355 TEST("Forward months then backward months returns us to current year",
356 g_date_get_year(d) == y);
358 if (failed)
360 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
361 g_date_debug_print(d);
365 if (day < 29)
367 /* Day should be unchanged */
369 TEST("Forward months then backward months returns us to current day",
370 g_date_get_day(d) == day);
372 if (failed)
374 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
375 g_date_debug_print(d);
378 else
380 /* reset the day for later tests */
381 g_date_set_day(d, day);
384 /******* Years ********/
386 tmp = *d;
387 g_date_add_years(d, i);
389 TEST("Adding years gives a larger value",
390 g_date_compare(d,&tmp) > 0);
392 g_date_subtract_years(d, i);
394 TEST("Forward years then backward years returns us to current month",
395 g_date_get_month(d) == m);
397 if (failed)
399 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
400 g_date_debug_print(d);
403 TEST("Forward years then backward years returns us to current year",
404 g_date_get_year(d) == y);
406 if (failed)
408 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
409 g_date_debug_print(d);
412 if (m != 2 && day != 29)
414 TEST("Forward years then backward years returns us to current day",
415 g_date_get_day(d) == day);
417 if (failed)
419 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
420 g_date_debug_print(d);
423 else
425 g_date_set_day(d, day); /* reset */
428 i += 10;
431 /***** increment test relative to our local Julian count */
433 if (!discontinuity) {
435 /* We can only run sequence tests between sequential years */
437 TEST("Julians are sequential with increment 1",
438 j+1 == g_date_get_julian(d));
439 if (failed)
441 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
442 j, j+1, g_date_get_julian(d));
445 g_date_add_days(d,1);
446 TEST("Next day has julian 1 higher",
447 g_date_get_julian(d) == j + 2);
448 g_date_subtract_days(d, 1);
450 if (j != G_DATE_BAD_JULIAN)
452 g_date_subtract_days(d, 1);
454 TEST("Previous day has julian 1 lower",
455 g_date_get_julian(d) == j);
457 g_date_add_days(d, 1); /* back to original */
460 discontinuity = FALSE; /* goes away now */
462 fflush(stdout);
463 fflush(stderr);
465 j = g_date_get_julian(d); /* inc current julian */
467 ++day;
469 ++m;
471 g_print(" done\n");
472 ++i;
473 prev_y = y;
474 y = check_years[i];
475 if (prev_y == G_DATE_BAD_YEAR ||
476 (prev_y + 1) != y) discontinuity = TRUE;
480 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
482 return 0;