Move doc comments inline.
[glib.git] / tests / testgdate.c
blobef4019eab0181661be52d0c70d386e190b69c236
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 void g_date_debug_print(GDate* d)
34 if (!d) g_print("NULL!\n");
35 else
36 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
37 d->julian_days,
38 d->julian ? "valid" : "invalid",
39 d->day,
40 d->month,
41 d->year,
42 d->dmy ? "valid" : "invalid");
44 fflush(stdout);
47 int main(int argc, char** argv)
49 GDate* d;
50 guint32 j;
51 GDateMonth m;
52 GDateYear y, prev_y;
53 GDateDay day;
54 gchar buf[101];
55 gchar* loc;
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);
72 guint i;
73 gboolean discontinuity;
75 g_print("checking GDate...");
77 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
79 d = g_date_new();
81 TEST("Empty constructor produces invalid date", !g_date_valid(d));
83 g_date_free(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));
111 g_date_free(d);
113 loc = setlocale(LC_ALL,"");
114 if (loc)
115 g_print("\nLocale set to %s\n", loc);
116 else
117 g_print("\nLocale unchanged\n");
119 d = g_date_new();
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);
124 g_print("%s", buf);
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);
130 g_print("%s", buf);
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",
137 g_print("%s", buf);
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",
146 g_date_valid(d) &&
147 g_date_get_month(d) == 1 &&
148 g_date_get_day(d) == 10 &&
149 g_date_get_year(d) == 2000);
150 if (failed)
151 g_date_debug_print(d);
153 g_date_free(d);
155 j = G_DATE_BAD_JULIAN;
157 i = 0;
158 discontinuity = TRUE;
159 y = check_years[0];
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);
170 if (discontinuity)
171 g_print(" (Break in sequence of requested years to check)\n");
173 g_print("Checking year %u", y);
175 TEST("Year is valid", g_date_valid_year(y));
177 TEST("Number of Sunday weeks in year is 52 or 53",
178 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
180 TEST("Number of Monday weeks in year is 52 or 53",
181 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
183 m = 1;
184 while (m < 13)
186 guint8 dim = g_date_get_days_in_month(m,y);
187 GDate days[31]; /* This is the fast way, no allocation */
189 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
191 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
193 day = 1;
195 g_date_clear(days, 31);
197 while (day <= dim)
199 guint i;
200 GDate tmp;
202 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
204 /* Create GDate with triplet */
206 d = &days[day-1];
208 TEST("Cleared day is invalid", !g_date_valid(d));
210 g_date_set_dmy(d,day,m,y);
212 TEST("Set day is valid", g_date_valid(d));
214 if (m == G_DATE_JANUARY && day == 1)
216 first_day_of_year = g_date_get_julian(d);
219 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
221 TEST("Date with DMY triplet is valid", g_date_valid(d));
222 TEST("Month accessor works", g_date_get_month(d) == m);
223 TEST("Year accessor works", g_date_get_year(d) == y);
224 TEST("Day of month accessor works", g_date_get_day(d) == day);
226 TEST("Day of year is consistent with Julian dates",
227 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
228 (g_date_get_day_of_year(d))));
230 if (failed)
232 g_print("first day: %u this day: %u day of year: %u\n",
233 first_day_of_year,
234 g_date_get_julian(d),
235 g_date_get_day_of_year(d));
238 if (m == G_DATE_DECEMBER && day == 31)
240 TEST("Last day of year equals number of days in year",
241 g_date_get_day_of_year(d) == days_in_year);
242 if (failed)
244 g_print("last day: %u days in year: %u\n",
245 g_date_get_day_of_year(d), days_in_year);
249 TEST("Day of year is not more than number of days in the year",
250 g_date_get_day_of_year(d) <= days_in_year);
252 TEST("Monday week of year is not more than number of weeks in the year",
253 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
254 if (failed)
256 g_print("Weeks in year: %u\n", monday_weeks_in_year);
257 g_date_debug_print(d);
259 TEST("Monday week of year is >= than last week of year",
260 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
262 if (g_date_get_weekday(d) == G_DATE_MONDAY)
265 TEST("Monday week of year on Monday 1 more than previous day's week of year",
266 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
268 else
270 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
271 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
275 monday_week_of_year = g_date_get_monday_week_of_year(d);
278 TEST("Sunday week of year is not more than number of weeks in the year",
279 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
280 if (failed)
282 g_date_debug_print(d);
284 TEST("Sunday week of year is >= than last week of year",
285 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
287 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
289 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
290 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
292 else
294 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
295 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
298 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
300 TEST("Date is equal to itself",
301 g_date_compare(d,d) == 0);
304 /*************** Increments ***********/
306 i = 1;
307 while (i < 402) /* Need to get 400 year increments in */
310 /***** Days ******/
311 tmp = *d;
312 g_date_add_days(d, i);
314 TEST("Adding days gives a value greater than previous",
315 g_date_compare(d, &tmp) > 0);
317 g_date_subtract_days(d, i);
318 TEST("Forward days then backward days returns us to current day",
319 g_date_get_day(d) == day);
321 if (failed)
323 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
324 g_date_debug_print(d);
327 TEST("Forward days then backward days returns us to current month",
328 g_date_get_month(d) == m);
330 if (failed)
332 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
333 g_date_debug_print(d);
336 TEST("Forward days then backward days returns us to current year",
337 g_date_get_year(d) == y);
339 if (failed)
341 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
342 g_date_debug_print(d);
345 /******* Months ********/
347 tmp = *d;
348 g_date_add_months(d, i);
349 TEST("Adding months gives a larger value",
350 g_date_compare(d, &tmp) > 0);
351 g_date_subtract_months(d, i);
353 TEST("Forward months then backward months returns us to current month",
354 g_date_get_month(d) == m);
356 if (failed)
358 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
359 g_date_debug_print(d);
362 TEST("Forward months then backward months returns us to current year",
363 g_date_get_year(d) == y);
365 if (failed)
367 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
368 g_date_debug_print(d);
372 if (day < 29)
374 /* Day should be unchanged */
376 TEST("Forward months then backward months returns us to current day",
377 g_date_get_day(d) == day);
379 if (failed)
381 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
382 g_date_debug_print(d);
385 else
387 /* reset the day for later tests */
388 g_date_set_day(d, day);
391 /******* Years ********/
393 tmp = *d;
394 g_date_add_years(d, i);
396 TEST("Adding years gives a larger value",
397 g_date_compare(d,&tmp) > 0);
399 g_date_subtract_years(d, i);
401 TEST("Forward years then backward years returns us to current month",
402 g_date_get_month(d) == m);
404 if (failed)
406 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
407 g_date_debug_print(d);
410 TEST("Forward years then backward years returns us to current year",
411 g_date_get_year(d) == y);
413 if (failed)
415 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
416 g_date_debug_print(d);
419 if (m != 2 && day != 29)
421 TEST("Forward years then backward years returns us to current day",
422 g_date_get_day(d) == day);
424 if (failed)
426 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
427 g_date_debug_print(d);
430 else
432 g_date_set_day(d, day); /* reset */
435 i += 10;
438 /***** increment test relative to our local Julian count */
440 if (!discontinuity) {
442 /* We can only run sequence tests between sequential years */
444 TEST("Julians are sequential with increment 1",
445 j+1 == g_date_get_julian(d));
446 if (failed)
448 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
449 j, j+1, g_date_get_julian(d));
452 g_date_add_days(d,1);
453 TEST("Next day has julian 1 higher",
454 g_date_get_julian(d) == j + 2);
455 g_date_subtract_days(d, 1);
457 if (j != G_DATE_BAD_JULIAN)
459 g_date_subtract_days(d, 1);
461 TEST("Previous day has julian 1 lower",
462 g_date_get_julian(d) == j);
464 g_date_add_days(d, 1); /* back to original */
467 discontinuity = FALSE; /* goes away now */
469 fflush(stdout);
470 fflush(stderr);
472 j = g_date_get_julian(d); /* inc current julian */
474 ++day;
476 ++m;
478 g_print(" done\n");
479 ++i;
480 prev_y = y;
481 y = check_years[i];
482 if (prev_y == G_DATE_BAD_YEAR ||
483 (prev_y + 1) != y) discontinuity = TRUE;
487 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
489 return 0;