First public contribution.
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
2 #undef G_DISABLE_ASSERT
5 #ifdef GLIB_COMPILATION
6 #undef GLIB_COMPILATION
17 #include "mrt2_glib2_test.h"
21 gboolean failed = FALSE;
23 guint32 notpassed = 0;
25 #define TEST(m,cond) G_STMT_START { failed = !(cond); \
27 { assert_failed = TRUE; \
30 g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
32 g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
36 if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \
39 void g_date_debug_print(GDate* d)
41 if (!d) g_print("NULL!\n");
43 g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
45 d->julian ? "valid" : "invalid",
49 d->dmy ? "valid" : "invalid");
54 int main(int argc, char** argv)
64 guint32 num_chars_written_to_buf;
66 /* Try to get all the leap year cases. */
67 GDateYear check_years[] = {
68 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
69 // Less number of years to test on emulator as the time taken
70 // on ATS is very large
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);
91 gboolean discontinuity;
94 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);
95 g_set_print_handler(mrtPrintHandler);
98 //g_print("checking GDate...");
100 TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
104 TEST("Empty constructor produces invalid date", !g_date_valid(d));
108 d = g_date_new_dmy(1,1,1);
110 TEST("January 1, Year 1 created and valid", g_date_valid(d));
112 j = g_date_get_julian(d);
114 TEST("January 1, Year 1 is Julian date 1", j == 1);
116 TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
117 TEST("Returned day is 1", g_date_get_day(d) == 1);
118 TEST("Returned year is 1", g_date_get_year(d) == 1);
120 TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
121 TEST("Month 13 is invalid", !g_date_valid_month(13));
122 TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
123 TEST("Day 32 is invalid", !g_date_valid_day(32));
124 TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
125 TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
126 TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
127 TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
128 TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
129 TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
130 TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
131 TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
132 TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
136 loc = setlocale(LC_ALL,"");
138 g_print("\nLocale unchanged\n");
141 g_date_set_time(d, time(NULL));
142 TEST("Today is valid", g_date_valid(d));
144 num_chars_written_to_buf = g_date_strftime(buf,100,"Today is a %A, %x\n", d);
145 g_assert(num_chars_written_to_buf != 0);
146 if(!num_chars_written_to_buf)
147 g_print("g_date_strftime is failed @ line = %d",__LINE__);
149 g_date_set_time(d, 1);
150 TEST("Beginning of Unix epoch is valid", g_date_valid(d));
152 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);
153 g_assert(num_chars_written_to_buf != 0);
154 if(!num_chars_written_to_buf)
155 g_print("g_date_strftime is failed @ line = %d",__LINE__);
157 g_date_set_julian(d, 1);
158 TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
160 num_chars_written_to_buf = g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
162 g_assert(num_chars_written_to_buf != 0);
163 if(!num_chars_written_to_buf)
164 g_print("g_date_strftime is failed @ line = %d",__LINE__);
166 g_date_set_dmy(d, 10, 1, 2000);
168 num_chars_written_to_buf = g_date_strftime(buf,100,"%x", d);
169 g_assert(num_chars_written_to_buf != 0);
170 if(!num_chars_written_to_buf)
171 g_print("g_date_strftime is failed @ line = %d",__LINE__);
173 g_date_set_parse(d, buf);
174 /* Note: this test will hopefully work, but no promises. */
175 TEST("Successfully parsed a %x-formatted string",
177 g_date_get_month(d) == 1 &&
178 g_date_get_day(d) == 10 &&
179 g_date_get_year(d) == 2000);
181 g_date_debug_print(d);
185 j = G_DATE_BAD_JULIAN;
188 discontinuity = TRUE;
190 prev_y = G_DATE_BAD_YEAR;
191 while (i < n_check_years)
193 guint32 first_day_of_year = G_DATE_BAD_JULIAN;
194 guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
195 guint sunday_week_of_year = 0;
196 guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
197 guint monday_week_of_year = 0;
198 guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
199 guint iso8601_week_of_year = 0;
203 //g_print(" (Break in sequence of requested years to check)\n");
205 //g_print("Checking year %u", y);
207 TEST("Year is valid", g_date_valid_year(y));
209 TEST("Number of Sunday weeks in year is 52 or 53",
210 sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
212 TEST("Number of Monday weeks in year is 52 or 53",
213 monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
218 guint8 dim = g_date_get_days_in_month(m,y);
219 GDate days[31]; /* This is the fast way, no allocation */
221 TEST("Sensible number of days in month", (dim > 0 && dim < 32));
223 TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
227 g_date_clear(days, 31);
234 TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
236 /* Create GDate with triplet */
240 TEST("Cleared day is invalid", !g_date_valid(d));
242 g_date_set_dmy(d,day,m,y);
244 TEST("Set day is valid", g_date_valid(d));
246 if (m == G_DATE_JANUARY && day == 1)
248 first_day_of_year = g_date_get_julian(d);
251 g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
253 TEST("Date with DMY triplet is valid", g_date_valid(d));
254 TEST("Month accessor works", g_date_get_month(d) == m);
255 TEST("Year accessor works", g_date_get_year(d) == y);
256 TEST("Day of month accessor works", g_date_get_day(d) == day);
258 TEST("Day of year is consistent with Julian dates",
259 ((g_date_get_julian(d) + 1 - first_day_of_year) ==
260 (g_date_get_day_of_year(d))));
264 g_print("first day: %u this day: %u day of year: %u\n",
266 g_date_get_julian(d),
267 g_date_get_day_of_year(d));
270 if (m == G_DATE_DECEMBER && day == 31)
272 TEST("Last day of year equals number of days in year",
273 g_date_get_day_of_year(d) == days_in_year);
276 g_print("last day: %u days in year: %u\n",
277 g_date_get_day_of_year(d), days_in_year);
281 TEST("Day of year is not more than number of days in the year",
282 g_date_get_day_of_year(d) <= days_in_year);
284 TEST("Monday week of year is not more than number of weeks in the year",
285 g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
288 g_print("Weeks in year: %u\n", monday_weeks_in_year);
289 g_date_debug_print(d);
291 TEST("Monday week of year is >= than last week of year",
292 g_date_get_monday_week_of_year(d) >= monday_week_of_year);
294 if (g_date_get_weekday(d) == G_DATE_MONDAY)
297 TEST("Monday week of year on Monday 1 more than previous day's week of year",
298 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
299 if ((m == G_DATE_JANUARY && day <= 4) ||
300 (m == G_DATE_DECEMBER && day >= 29)) {
301 TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
302 (g_date_get_iso8601_week_of_year(d) == 1));
304 TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
305 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
310 TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
311 (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
312 if (!(day == 1 && m == G_DATE_JANUARY)) {
313 TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
314 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
319 monday_week_of_year = g_date_get_monday_week_of_year(d);
320 iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
323 TEST("Sunday week of year is not more than number of weeks in the year",
324 g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
327 g_date_debug_print(d);
329 TEST("Sunday week of year is >= than last week of year",
330 g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
332 if (g_date_get_weekday(d) == G_DATE_SUNDAY)
334 TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
335 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
339 TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
340 (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
343 sunday_week_of_year = g_date_get_sunday_week_of_year(d);
345 TEST("Date is equal to itself",
346 g_date_compare(d,d) == 0);
349 /*************** Increments ***********/
352 while (i < 402) /* Need to get 400 year increments in */
357 g_date_add_days(d, i);
359 TEST("Adding days gives a value greater than previous",
360 g_date_compare(d, &tmp) > 0);
362 g_date_subtract_days(d, i);
363 TEST("Forward days then backward days returns us to current day",
364 g_date_get_day(d) == day);
368 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
369 g_date_debug_print(d);
372 TEST("Forward days then backward days returns us to current month",
373 g_date_get_month(d) == m);
377 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
378 g_date_debug_print(d);
381 TEST("Forward days then backward days returns us to current year",
382 g_date_get_year(d) == y);
386 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
387 g_date_debug_print(d);
390 /******* Months ********/
393 g_date_add_months(d, i);
394 TEST("Adding months gives a larger value",
395 g_date_compare(d, &tmp) > 0);
396 g_date_subtract_months(d, i);
398 TEST("Forward months then backward months returns us to current month",
399 g_date_get_month(d) == m);
403 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
404 g_date_debug_print(d);
407 TEST("Forward months then backward months returns us to current year",
408 g_date_get_year(d) == y);
412 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
413 g_date_debug_print(d);
419 /* Day should be unchanged */
421 TEST("Forward months then backward months 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 /* reset the day for later tests */
433 g_date_set_day(d, day);
436 /******* Years ********/
439 g_date_add_years(d, i);
441 TEST("Adding years gives a larger value",
442 g_date_compare(d,&tmp) > 0);
444 g_date_subtract_years(d, i);
446 TEST("Forward years then backward years returns us to current month",
447 g_date_get_month(d) == m);
451 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
452 g_date_debug_print(d);
455 TEST("Forward years then backward years returns us to current year",
456 g_date_get_year(d) == y);
460 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
461 g_date_debug_print(d);
464 if (m != 2 && day != 29)
466 TEST("Forward years then backward years returns us to current day",
467 g_date_get_day(d) == day);
471 g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
472 g_date_debug_print(d);
477 g_date_set_day(d, day); /* reset */
483 /***** increment test relative to our local Julian count */
485 if (!discontinuity) {
487 /* We can only run sequence tests between sequential years */
489 TEST("Julians are sequential with increment 1",
490 j+1 == g_date_get_julian(d));
493 g_print("Out of sequence, prev: %u expected: %u got: %u\n",
494 j, j+1, g_date_get_julian(d));
497 g_date_add_days(d,1);
498 TEST("Next day has julian 1 higher",
499 g_date_get_julian(d) == j + 2);
500 g_date_subtract_days(d, 1);
502 if (j != G_DATE_BAD_JULIAN)
504 g_date_subtract_days(d, 1);
506 TEST("Previous day has julian 1 lower",
507 g_date_get_julian(d) == j);
509 g_date_add_days(d, 1); /* back to original */
512 discontinuity = FALSE; /* goes away now */
517 j = g_date_get_julian(d); /* inc current julian */
523 // g_print(" done\n");
527 if (prev_y == G_DATE_BAD_YEAR ||
528 (prev_y + 1) != y) discontinuity = TRUE;
532 // g_print("\n%u tests passed, %u failed\n",passed, notpassed);
534 testResultXml("testgdate");
535 #endif /* EMULATOR */