Update contrib.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved. */
2 #undef G_DISABLE_ASSERT
14 #include "mrt2_glib2_test.h"
18 gboolean failed = FALSE;
20 guint32 notpassed = 0;
22 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
25 assert_failed = TRUE; \
27 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
29 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
33 if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \
36 void g_date_debug_print(GDate* d)
41 void g_print_dummy(const char *format, ...)
45 void fflush_dummy (FILE *f)
51 #define g_print g_print_dummy
52 #define fflush fflush_dummy
56 int main(int argc, char** argv)
66 guint32 num_chars_written_to_buf;
68 /* Try to get all the leap year cases. */
69 GDateYear check_years[] = {
70 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
71 #if !(defined(__WINS__) || defined(__WINSCW__))
72 ,11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
73 398, 399, 400, 401, 402, 403, 404, 405, 406,
74 1598, 1599, 1600, 1601, 1602, 1650, 1651,
75 1897, 1898, 1899, 1900, 1901, 1902, 1903,
76 1961, 1962, 1963, 1964, 1965, 1967,
77 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
78 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
79 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
80 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
81 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
82 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
89 guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
92 gboolean discontinuity = FALSE;
96 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
97 g_set_print_handler(mrtPrintHandler);
100 //g_print("checking GDate...");
102 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
106 TEST("Empty constructor produces invalid date", !g_date_valid(d));
110 d = g_date_new_dmy(1,1,1);
112 TEST("January 1, Year 1 created and valid", g_date_valid(d));
114 j = g_date_get_julian(d);
116 TEST("January 1, Year 1 is Julian date 1", j == 1);
118 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
119 TEST("Returned day is 1", g_date_get_day(d) == 1);
120 TEST("Returned year is 1", g_date_get_year(d) == 1);
122 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
123 TEST("Month 13 is invalid", !g_date_valid_month(13));
124 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
125 TEST("Day 32 is invalid", !g_date_valid_day(32));
126 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
127 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
128 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
129 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
130 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
131 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
132 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
133 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
134 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
138 loc = setlocale(LC_ALL,"");
140 g_print("\nLocale unchanged\n");
143 g_date_set_time(d, time(NULL));
144 TEST("Today is valid", g_date_valid(d));
146 num_chars_written_to_buf = g_date_strftime(buf,100,"Today is a %A, %x\n", d);
147 g_assert(num_chars_written_to_buf != 0);
148 if(!num_chars_written_to_buf)
149 g_print("g_date_strftime is failed @ line = %d",__LINE__);
151 g_date_set_time(d, 1);
152 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
154 num_chars_written_to_buf = g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
155 g_assert(num_chars_written_to_buf != 0);
156 if(!num_chars_written_to_buf)
157 g_print("g_date_strftime is failed @ line : %d",__LINE__);
159 g_date_set_julian(d, 1);
160 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
162 num_chars_written_to_buf = g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
164 g_assert(num_chars_written_to_buf != 0);
165 if(!num_chars_written_to_buf)
166 g_print("g_date_strftime is failed @ line = %d",__LINE__);
169 g_date_set_dmy(d, 10, 1, 2000);
171 num_chars_written_to_buf = g_date_strftime(buf,100,"%x", d);
172 g_assert(num_chars_written_to_buf != 0);
173 if(!num_chars_written_to_buf)
174 g_print("g_date_strftime is failed @ line = %d",__LINE__);
176 g_date_set_parse(d, buf);
177 /* Note: this test will hopefully work, but no promises. */
178 TEST("Successfully parsed a %x-formatted string",
180 g_date_get_month(d) == 1 &&
181 g_date_get_day(d) == 10 &&
182 g_date_get_year(d) == 2000);
184 g_date_debug_print(d);
188 j = G_DATE_BAD_JULIAN;
191 discontinuity = TRUE;
193 prev_y = G_DATE_BAD_YEAR;
194 while (i < n_check_years)
196 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
197 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
198 guint sunday_week_of_year = 0;
199 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
200 guint monday_week_of_year = 0;
201 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
202 guint iso8601_week_of_year = 0;
204 // if (discontinuity)
205 // g_print(" (Break in sequence of requested years to check)\n");
207 //g_print("Checking year %u", y);
209 TEST("Year is valid", g_date_valid_year(y));
211 TEST("Number of Sunday weeks in year is 52 or 53",
212 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
214 TEST("Number of Monday weeks in year is 52 or 53",
215 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
220 guint8 dim = g_date_get_days_in_month(m,y);
221 GDate days[31]; /* This is the fast way, no allocation */
223 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
225 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
229 g_date_clear(days, 31);
236 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
238 /* Create GDate with triplet */
242 TEST("Cleared day is invalid", !g_date_valid(d));
244 g_date_set_dmy(d,day,m,y);
246 TEST("Set day is valid", g_date_valid(d));
248 if (m == G_DATE_JANUARY && day == 1)
250 first_day_of_year = g_date_get_julian(d);
253 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
255 TEST("Date with DMY triplet is valid", g_date_valid(d));
256 TEST("Month accessor works", g_date_get_month(d) == m);
257 TEST("Year accessor works", g_date_get_year(d) == y);
258 TEST("Day of month accessor works", g_date_get_day(d) == day);
260 TEST("Day of year is consistent with Julian dates",
261 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
262 (g_date_get_day_of_year(d))));
266 g_print("first day: %u this day: %u day of year: %u\n",
268 g_date_get_julian(d),
269 g_date_get_day_of_year(d));
272 if (m == G_DATE_DECEMBER && day == 31)
274 TEST("Last day of year equals number of days in year",
275 g_date_get_day_of_year(d) == days_in_year);
278 g_print("last day: %u days in year: %u\n",
279 g_date_get_day_of_year(d), days_in_year);
283 TEST("Day of year is not more than number of days in the year",
284 g_date_get_day_of_year(d) <= days_in_year);
286 TEST("Monday week of year is not more than number of weeks in the year",
287 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
290 g_print("Weeks in year: %u\n", monday_weeks_in_year);
291 g_date_debug_print(d);
293 TEST("Monday week of year is >= than last week of year",
294 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
296 if (g_date_get_weekday(d) == G_DATE_MONDAY)
299 TEST("Monday week of year on Monday 1 more than previous day's week of year",
300 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
301 if ((m == G_DATE_JANUARY && day <= 4) ||
302 (m == G_DATE_DECEMBER && day >= 29)) {
303 TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
304 (g_date_get_iso8601_week_of_year(d) == 1));
306 TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
307 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
312 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
313 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
314 if (!(day == 1 && m == G_DATE_JANUARY)) {
315 TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
316 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
321 monday_week_of_year = g_date_get_monday_week_of_year(d);
322 iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
325 TEST("Sunday week of year is not more than number of weeks in the year",
326 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
329 g_date_debug_print(d);
331 TEST("Sunday week of year is >= than last week of year",
332 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
334 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
336 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
337 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
341 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
342 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
345 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
347 TEST("Date is equal to itself",
348 g_date_compare(d,d) == 0);
351 /*************** Increments ***********/
354 while (i < 402) /* Need to get 400 year increments in */
359 g_date_add_days(d, i);
361 TEST("Adding days gives a value greater than previous",
362 g_date_compare(d, &tmp) > 0);
364 g_date_subtract_days(d, i);
365 TEST("Forward days then backward days returns us to current day",
366 g_date_get_day(d) == day);
370 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
371 g_date_debug_print(d);
374 TEST("Forward days then backward days returns us to current month",
375 g_date_get_month(d) == m);
379 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
380 g_date_debug_print(d);
383 TEST("Forward days then backward days returns us to current year",
384 g_date_get_year(d) == y);
388 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
389 g_date_debug_print(d);
392 /******* Months ********/
395 g_date_add_months(d, i);
396 TEST("Adding months gives a larger value",
397 g_date_compare(d, &tmp) > 0);
398 g_date_subtract_months(d, i);
400 TEST("Forward months then backward months returns us to current month",
401 g_date_get_month(d) == m);
405 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
406 g_date_debug_print(d);
409 TEST("Forward months then backward months returns us to current year",
410 g_date_get_year(d) == y);
414 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
415 g_date_debug_print(d);
421 /* Day should be unchanged */
423 TEST("Forward months then backward months returns us to current day",
424 g_date_get_day(d) == day);
428 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
429 g_date_debug_print(d);
434 /* reset the day for later tests */
435 g_date_set_day(d, day);
438 /******* Years ********/
441 g_date_add_years(d, i);
443 TEST("Adding years gives a larger value",
444 g_date_compare(d,&tmp) > 0);
446 g_date_subtract_years(d, i);
448 TEST("Forward years then backward years returns us to current month",
449 g_date_get_month(d) == m);
453 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
454 g_date_debug_print(d);
457 TEST("Forward years then backward years returns us to current year",
458 g_date_get_year(d) == y);
462 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
463 g_date_debug_print(d);
466 if (m != 2 && day != 29)
468 TEST("Forward years then backward years returns us to current day",
469 g_date_get_day(d) == day);
473 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
474 g_date_debug_print(d);
479 g_date_set_day(d, day); /* reset */
485 /***** increment test relative to our local Julian count */
487 if (!discontinuity) {
489 /* We can only run sequence tests between sequential years */
491 TEST("Julians are sequential with increment 1",
492 j+1 == g_date_get_julian(d));
495 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
496 j, j+1, g_date_get_julian(d));
499 g_date_add_days(d,1);
500 TEST("Next day has julian 1 higher",
501 g_date_get_julian(d) == j + 2);
502 g_date_subtract_days(d, 1);
504 if (j != G_DATE_BAD_JULIAN)
506 g_date_subtract_days(d, 1);
508 TEST("Previous day has julian 1 lower",
509 g_date_get_julian(d) == j);
511 g_date_add_days(d, 1); /* back to original */
514 discontinuity = FALSE; /* goes away now */
519 j = g_date_get_julian(d); /* inc current julian */
525 //g_print(" done\n");
529 if (prev_y == G_DATE_BAD_YEAR ||
530 (prev_y + 1) != y) discontinuity = TRUE;
534 g_print("\n%u tests passed, %u failed\n",passed, notpassed);
538 testResultXml("date-test");