Merge branch 'source-get-id-docs' into 'master'
[glib.git] / tests / testgdate.c
bloba8ec4cfe8d23492267642d403d8e5f61d5a56afd
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #ifdef GLIB_COMPILATION
5 #undef GLIB_COMPILATION
6 #endif
8 #include "glib.h"
10 #include <stdio.h>
11 #include <string.h>
12 #include <locale.h>
13 #include <time.h>
15 gboolean failed = FALSE;
16 guint32 passed = 0;
17 guint32 notpassed = 0;
19 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
20 if (failed) \
21 { ++notpassed; \
22 if (!m) \
23 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
24 else \
25 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
26 } \
27 else \
28 ++passed; \
29 if ((passed+notpassed) % 10000 == 0) g_print ("."); fflush (stdout); \
30 } G_STMT_END
32 static void
33 g_date_debug_print (GDate* d)
35 if (!d) g_print("NULL!\n");
36 else
37 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
38 d->julian_days,
39 d->julian ? "valid" : "invalid",
40 d->day,
41 d->month,
42 d->year,
43 d->dmy ? "valid" : "invalid");
45 fflush(stdout);
48 int main(int argc, char** argv)
50 GDate* d;
51 guint32 j;
52 GDateMonth m;
53 GDateYear y, prev_y;
54 GDateDay day;
55 gchar buf[101];
56 gchar* loc;
57 /* Try to get all the leap year cases. */
58 GDateYear check_years[] = {
59 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
60 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
61 398, 399, 400, 401, 402, 403, 404, 405, 406,
62 1598, 1599, 1600, 1601, 1602, 1650, 1651,
63 1897, 1898, 1899, 1900, 1901, 1902, 1903,
64 1961, 1962, 1963, 1964, 1965, 1967,
65 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
66 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
67 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
68 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
69 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
70 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
72 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
73 guint i, k;
74 gboolean discontinuity;
76 g_print("checking GDate...");
78 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
80 d = g_date_new();
82 TEST("Empty constructor produces invalid date", !g_date_valid(d));
84 g_date_free(d);
86 d = g_date_new_dmy(1,1,1);
88 TEST("January 1, Year 1 created and valid", g_date_valid(d));
90 j = g_date_get_julian(d);
92 TEST("January 1, Year 1 is Julian date 1", j == 1);
94 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
95 TEST("Returned day is 1", g_date_get_day(d) == 1);
96 TEST("Returned year is 1", g_date_get_year(d) == 1);
98 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
99 TEST("Month 13 is invalid", !g_date_valid_month(13));
100 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
101 TEST("Day 32 is invalid", !g_date_valid_day(32));
102 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
103 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
104 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
105 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
106 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
107 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
108 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
109 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
110 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
112 g_date_free(d);
114 loc = setlocale(LC_ALL,"");
115 if (loc)
116 g_print("\nLocale set to %s\n", loc);
117 else
118 g_print("\nLocale unchanged\n");
120 d = g_date_new();
121 g_date_set_time(d, time(NULL));
122 TEST("Today is valid", g_date_valid(d));
124 g_date_strftime(buf,100,"Today is a %A, %x\n", d);
125 g_print("%s", buf);
127 g_date_set_time(d, 1);
128 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
130 g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
131 g_print("%s", buf);
133 g_date_set_julian(d, 1);
134 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
136 g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
138 g_print("%s", buf);
140 g_date_set_dmy(d, 10, 1, 2000);
142 g_date_strftime(buf,100,"%x", d);
144 g_date_set_parse(d, buf);
145 /* Note: this test will hopefully work, but no promises. */
146 TEST("Successfully parsed a %x-formatted string",
147 g_date_valid(d) &&
148 g_date_get_month(d) == 1 &&
149 g_date_get_day(d) == 10 &&
150 g_date_get_year(d) == 2000);
151 if (failed)
152 g_date_debug_print(d);
154 g_date_free(d);
156 j = G_DATE_BAD_JULIAN;
158 i = 0;
159 discontinuity = TRUE;
160 y = check_years[0];
161 prev_y = G_DATE_BAD_YEAR;
162 g_print ("testing %d years\n", n_check_years);
163 while (i < n_check_years)
165 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
166 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
167 guint sunday_week_of_year = 0;
168 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
169 guint monday_week_of_year = 0;
170 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
171 guint iso8601_week_of_year = 0;
173 if (discontinuity)
174 g_print(" (Break in sequence of requested years to check)\n");
176 g_print("Checking year %u", y);
178 TEST("Year is valid", g_date_valid_year(y));
180 TEST("Number of Sunday weeks in year is 52 or 53",
181 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
183 TEST("Number of Monday weeks in year is 52 or 53",
184 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
186 m = 1;
187 while (m < 13)
189 guint8 dim = g_date_get_days_in_month(m,y);
190 GDate days[31]; /* This is the fast way, no allocation */
192 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
194 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
196 day = 1;
198 g_date_clear(days, 31);
200 while (day <= dim)
202 GDate tmp;
204 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
206 /* Create GDate with triplet */
208 d = &days[day-1];
210 TEST("Cleared day is invalid", !g_date_valid(d));
212 g_date_set_dmy(d,day,m,y);
214 TEST("Set day is valid", g_date_valid(d));
216 if (m == G_DATE_JANUARY && day == 1)
218 first_day_of_year = g_date_get_julian(d);
221 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
223 TEST("Date with DMY triplet is valid", g_date_valid(d));
224 TEST("Month accessor works", g_date_get_month(d) == m);
225 TEST("Year accessor works", g_date_get_year(d) == y);
226 TEST("Day of month accessor works", g_date_get_day(d) == day);
228 TEST("Day of year is consistent with Julian dates",
229 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
230 (g_date_get_day_of_year(d))));
232 if (failed)
234 g_print("first day: %u this day: %u day of year: %u\n",
235 first_day_of_year,
236 g_date_get_julian(d),
237 g_date_get_day_of_year(d));
240 if (m == G_DATE_DECEMBER && day == 31)
242 TEST("Last day of year equals number of days in year",
243 g_date_get_day_of_year(d) == days_in_year);
244 if (failed)
246 g_print("last day: %u days in year: %u\n",
247 g_date_get_day_of_year(d), days_in_year);
251 TEST("Day of year is not more than number of days in the year",
252 g_date_get_day_of_year(d) <= days_in_year);
254 TEST("Monday week of year is not more than number of weeks in the year",
255 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
256 if (failed)
258 g_print("Weeks in year: %u\n", monday_weeks_in_year);
259 g_date_debug_print(d);
261 TEST("Monday week of year is >= than last week of year",
262 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
264 if (g_date_get_weekday(d) == G_DATE_MONDAY)
267 TEST("Monday week of year on Monday 1 more than previous day's week of year",
268 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
269 if ((m == G_DATE_JANUARY && day <= 4) ||
270 (m == G_DATE_DECEMBER && day >= 29)) {
271 TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
272 (g_date_get_iso8601_week_of_year(d) == 1));
273 } else {
274 TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
275 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
278 else
280 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
281 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
282 if (!(day == 1 && m == G_DATE_JANUARY)) {
283 TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
284 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
289 monday_week_of_year = g_date_get_monday_week_of_year(d);
290 iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
293 TEST("Sunday week of year is not more than number of weeks in the year",
294 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
295 if (failed)
297 g_date_debug_print(d);
299 TEST("Sunday week of year is >= than last week of year",
300 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
302 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
304 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
305 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
307 else
309 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
310 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
313 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
315 TEST("Date is equal to itself",
316 g_date_compare(d,d) == 0);
319 /*************** Increments ***********/
321 k = 1;
322 while (k < 402) /* Need to get 400 year increments in */
325 /***** Days ******/
326 tmp = *d;
327 g_date_add_days(d, k);
329 TEST("Adding days gives a value greater than previous",
330 g_date_compare(d, &tmp) > 0);
332 g_date_subtract_days(d, k);
333 TEST("Forward days then backward days returns us to current day",
334 g_date_get_day(d) == day);
336 if (failed)
338 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
339 g_date_debug_print(d);
342 TEST("Forward days then backward days returns us to current month",
343 g_date_get_month(d) == m);
345 if (failed)
347 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
348 g_date_debug_print(d);
351 TEST("Forward days then backward days returns us to current year",
352 g_date_get_year(d) == y);
354 if (failed)
356 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
357 g_date_debug_print(d);
360 /******* Months ********/
362 tmp = *d;
363 g_date_add_months(d, k);
364 TEST("Adding months gives a larger value",
365 g_date_compare(d, &tmp) > 0);
366 g_date_subtract_months(d, k);
368 TEST("Forward months then backward months returns us to current month",
369 g_date_get_month(d) == m);
371 if (failed)
373 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
374 g_date_debug_print(d);
377 TEST("Forward months then backward months returns us to current year",
378 g_date_get_year(d) == y);
380 if (failed)
382 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
383 g_date_debug_print(d);
387 if (day < 29)
389 /* Day should be unchanged */
391 TEST("Forward months then backward months returns us to current day",
392 g_date_get_day(d) == day);
394 if (failed)
396 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
397 g_date_debug_print(d);
400 else
402 /* reset the day for later tests */
403 g_date_set_day(d, day);
406 /******* Years ********/
408 tmp = *d;
409 g_date_add_years(d, k);
411 TEST("Adding years gives a larger value",
412 g_date_compare(d,&tmp) > 0);
414 g_date_subtract_years(d, k);
416 TEST("Forward years then backward years returns us to current month",
417 g_date_get_month(d) == m);
419 if (failed)
421 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
422 g_date_debug_print(d);
425 TEST("Forward years then backward years returns us to current year",
426 g_date_get_year(d) == y);
428 if (failed)
430 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
431 g_date_debug_print(d);
434 if (m != 2 && day != 29)
436 TEST("Forward years then backward years returns us to current day",
437 g_date_get_day(d) == day);
439 if (failed)
441 g_print(" (increment %u, dmy %u %u %u) ", k, day, m, y);
442 g_date_debug_print(d);
445 else
447 g_date_set_day(d, day); /* reset */
450 k += 10;
453 /***** increment test relative to our local Julian count */
455 if (!discontinuity) {
457 /* We can only run sequence tests between sequential years */
459 TEST("Julians are sequential with increment 1",
460 j+1 == g_date_get_julian(d));
461 if (failed)
463 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
464 j, j+1, g_date_get_julian(d));
467 g_date_add_days(d,1);
468 TEST("Next day has julian 1 higher",
469 g_date_get_julian(d) == j + 2);
470 g_date_subtract_days(d, 1);
472 if (j != G_DATE_BAD_JULIAN)
474 g_date_subtract_days(d, 1);
476 TEST("Previous day has julian 1 lower",
477 g_date_get_julian(d) == j);
479 g_date_add_days(d, 1); /* back to original */
482 discontinuity = FALSE; /* goes away now */
484 fflush(stdout);
485 fflush(stderr);
487 j = g_date_get_julian(d); /* inc current julian */
489 ++day;
491 ++m;
493 g_print(" done\n");
494 ++i;
495 if (i == n_check_years)
496 break;
497 prev_y = y;
498 y = check_years[i];
499 if (prev_y == G_DATE_BAD_YEAR ||
500 (prev_y + 1) != y) discontinuity = TRUE;
504 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
506 return 0;