1 #undef G_DISABLE_ASSERT
4 #ifdef GLIB_COMPILATION
5 #undef GLIB_COMPILATION
15 gboolean failed
= FALSE
;
17 guint32 notpassed
= 0;
19 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
23 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
25 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
29 if ((passed+notpassed) % 10000 == 0) g_print ("."); fflush (stdout); \
32 void g_date_debug_print(GDate
* d
)
34 if (!d
) g_print("NULL!\n");
36 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
38 d
->julian
? "valid" : "invalid",
42 d
->dmy
? "valid" : "invalid");
47 int main(int argc
, char** argv
)
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
);
73 gboolean discontinuity
;
75 g_print("checking GDate...");
77 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate
) < 9);
81 TEST("Empty constructor produces invalid date", !g_date_valid(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));
113 loc
= setlocale(LC_ALL
,"");
115 g_print("\nLocale set to %s\n", loc
);
117 g_print("\nLocale unchanged\n");
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
);
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
);
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",
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",
147 g_date_get_month(d
) == 1 &&
148 g_date_get_day(d
) == 10 &&
149 g_date_get_year(d
) == 2000);
151 g_date_debug_print(d
);
155 j
= G_DATE_BAD_JULIAN
;
158 discontinuity
= TRUE
;
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
);
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);
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
));
195 g_date_clear(days
, 31);
202 TEST("DMY triplet is valid", g_date_valid_dmy(day
,m
,y
));
204 /* Create GDate with triplet */
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
))));
232 g_print("first day: %u this day: %u day of year: %u\n",
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
);
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
);
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);
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
);
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);
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 ***********/
307 while (i
< 402) /* Need to get 400 year increments in */
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
);
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
);
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
);
341 g_print(" (increment %u, dmy %u %u %u) ", i
, day
, m
, y
);
342 g_date_debug_print(d
);
345 /******* Months ********/
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
);
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
);
367 g_print(" (increment %u, dmy %u %u %u) ", i
, day
, m
, y
);
368 g_date_debug_print(d
);
374 /* Day should be unchanged */
376 TEST("Forward months then backward months returns us to current day",
377 g_date_get_day(d
) == day
);
381 g_print(" (increment %u, dmy %u %u %u) ", i
, day
, m
, y
);
382 g_date_debug_print(d
);
387 /* reset the day for later tests */
388 g_date_set_day(d
, day
);
391 /******* Years ********/
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
);
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
);
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
);
426 g_print(" (increment %u, dmy %u %u %u) ", i
, day
, m
, y
);
427 g_date_debug_print(d
);
432 g_date_set_day(d
, day
); /* reset */
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
));
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 */
472 j
= g_date_get_julian(d
); /* inc current julian */
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
);