sl@0: /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ sl@0: #undef G_DISABLE_ASSERT sl@0: #undef G_LOG_DOMAIN sl@0: sl@0: #ifdef GLIB_COMPILATION sl@0: #undef GLIB_COMPILATION sl@0: #endif sl@0: sl@0: #include "glib.h" sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #ifdef SYMBIAN sl@0: #include "mrt2_glib2_test.h" sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: sl@0: gboolean failed = FALSE; sl@0: guint32 passed = 0; sl@0: guint32 notpassed = 0; sl@0: sl@0: #define TEST(m,cond) G_STMT_START { failed = !(cond); \ sl@0: if (failed) \ sl@0: { assert_failed = TRUE; \ sl@0: ++notpassed; \ sl@0: if (!m) \ sl@0: g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \ sl@0: else \ sl@0: g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \ sl@0: } \ sl@0: else \ sl@0: ++passed; \ sl@0: if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \ sl@0: } G_STMT_END sl@0: sl@0: void g_date_debug_print(GDate* d) sl@0: { sl@0: if (!d) g_print("NULL!\n"); sl@0: else sl@0: g_print("julian: %u (%s) DMY: %u %u %u (%s)\n", sl@0: d->julian_days, sl@0: d->julian ? "valid" : "invalid", sl@0: d->day, sl@0: d->month, sl@0: d->year, sl@0: d->dmy ? "valid" : "invalid"); sl@0: sl@0: fflush(stdout); sl@0: } sl@0: sl@0: int main(int argc, char** argv) sl@0: { sl@0: GDate* d; sl@0: guint32 j; sl@0: GDateMonth m; sl@0: GDateYear y, prev_y; sl@0: GDateDay day; sl@0: gchar buf[101]; sl@0: gchar* loc; sl@0: sl@0: guint32 num_chars_written_to_buf; sl@0: sl@0: /* Try to get all the leap year cases. */ sl@0: GDateYear check_years[] = { sl@0: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 sl@0: // Less number of years to test on emulator as the time taken sl@0: // on ATS is very large sl@0: #if !defined(__WINS__) && !defined(__WINSCW__) sl@0: ,11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397, sl@0: 398, 399, 400, 401, 402, 403, 404, 405, 406, sl@0: 1598, 1599, 1600, 1601, 1602, 1650, 1651, sl@0: 1897, 1898, 1899, 1900, 1901, 1902, 1903, sl@0: 1961, 1962, 1963, 1964, 1965, 1967, sl@0: 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, sl@0: 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, sl@0: 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, sl@0: 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, sl@0: 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, sl@0: 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003 sl@0: #endif sl@0: }; sl@0: sl@0: sl@0: sl@0: sl@0: guint n_check_years = sizeof(check_years)/sizeof(GDateYear); sl@0: guint i; sl@0: gboolean discontinuity; sl@0: sl@0: #ifdef SYMBIAN sl@0: 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); sl@0: g_set_print_handler(mrtPrintHandler); sl@0: #endif /*SYMBIAN*/ sl@0: sl@0: //g_print("checking GDate..."); sl@0: sl@0: TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9); sl@0: sl@0: d = g_date_new(); sl@0: sl@0: TEST("Empty constructor produces invalid date", !g_date_valid(d)); sl@0: sl@0: g_date_free(d); sl@0: sl@0: d = g_date_new_dmy(1,1,1); sl@0: sl@0: TEST("January 1, Year 1 created and valid", g_date_valid(d)); sl@0: sl@0: j = g_date_get_julian(d); sl@0: sl@0: TEST("January 1, Year 1 is Julian date 1", j == 1); sl@0: sl@0: TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY); sl@0: TEST("Returned day is 1", g_date_get_day(d) == 1); sl@0: TEST("Returned year is 1", g_date_get_year(d) == 1); sl@0: sl@0: TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH)); sl@0: TEST("Month 13 is invalid", !g_date_valid_month(13)); sl@0: TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY)); sl@0: TEST("Day 32 is invalid", !g_date_valid_day(32)); sl@0: TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR)); sl@0: TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN)); sl@0: TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY)); sl@0: TEST("Year 2000 is a leap year", g_date_is_leap_year(2000)); sl@0: TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999)); sl@0: TEST("Year 1996 is a leap year", g_date_is_leap_year(1996)); sl@0: TEST("Year 1600 is a leap year", g_date_is_leap_year(1600)); sl@0: TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100)); sl@0: TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800)); sl@0: sl@0: g_date_free(d); sl@0: sl@0: loc = setlocale(LC_ALL,""); sl@0: if (!loc) sl@0: g_print("\nLocale unchanged\n"); sl@0: sl@0: d = g_date_new(); sl@0: g_date_set_time(d, time(NULL)); sl@0: TEST("Today is valid", g_date_valid(d)); sl@0: sl@0: num_chars_written_to_buf = g_date_strftime(buf,100,"Today is a %A, %x\n", d); sl@0: g_assert(num_chars_written_to_buf != 0); sl@0: if(!num_chars_written_to_buf) sl@0: g_print("g_date_strftime is failed @ line = %d",__LINE__); sl@0: sl@0: g_date_set_time(d, 1); sl@0: TEST("Beginning of Unix epoch is valid", g_date_valid(d)); sl@0: sl@0: 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); sl@0: g_assert(num_chars_written_to_buf != 0); sl@0: if(!num_chars_written_to_buf) sl@0: g_print("g_date_strftime is failed @ line = %d",__LINE__); sl@0: sl@0: g_date_set_julian(d, 1); sl@0: TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d)); sl@0: sl@0: num_chars_written_to_buf = g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n", sl@0: d); sl@0: g_assert(num_chars_written_to_buf != 0); sl@0: if(!num_chars_written_to_buf) sl@0: g_print("g_date_strftime is failed @ line = %d",__LINE__); sl@0: sl@0: g_date_set_dmy(d, 10, 1, 2000); sl@0: sl@0: num_chars_written_to_buf = g_date_strftime(buf,100,"%x", d); sl@0: g_assert(num_chars_written_to_buf != 0); sl@0: if(!num_chars_written_to_buf) sl@0: g_print("g_date_strftime is failed @ line = %d",__LINE__); sl@0: sl@0: g_date_set_parse(d, buf); sl@0: /* Note: this test will hopefully work, but no promises. */ sl@0: TEST("Successfully parsed a %x-formatted string", sl@0: g_date_valid(d) && sl@0: g_date_get_month(d) == 1 && sl@0: g_date_get_day(d) == 10 && sl@0: g_date_get_year(d) == 2000); sl@0: if (failed) sl@0: g_date_debug_print(d); sl@0: sl@0: g_date_free(d); sl@0: sl@0: j = G_DATE_BAD_JULIAN; sl@0: sl@0: i = 0; sl@0: discontinuity = TRUE; sl@0: y = check_years[0]; sl@0: prev_y = G_DATE_BAD_YEAR; sl@0: while (i < n_check_years) sl@0: { sl@0: guint32 first_day_of_year = G_DATE_BAD_JULIAN; sl@0: guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365; sl@0: guint sunday_week_of_year = 0; sl@0: guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y); sl@0: guint monday_week_of_year = 0; sl@0: guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y); sl@0: guint iso8601_week_of_year = 0; sl@0: sl@0: if (discontinuity) sl@0: ; sl@0: //g_print(" (Break in sequence of requested years to check)\n"); sl@0: sl@0: //g_print("Checking year %u", y); sl@0: sl@0: TEST("Year is valid", g_date_valid_year(y)); sl@0: sl@0: TEST("Number of Sunday weeks in year is 52 or 53", sl@0: sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53); sl@0: sl@0: TEST("Number of Monday weeks in year is 52 or 53", sl@0: monday_weeks_in_year == 52 || monday_weeks_in_year == 53); sl@0: sl@0: m = 1; sl@0: while (m < 13) sl@0: { sl@0: guint8 dim = g_date_get_days_in_month(m,y); sl@0: GDate days[31]; /* This is the fast way, no allocation */ sl@0: sl@0: TEST("Sensible number of days in month", (dim > 0 && dim < 32)); sl@0: sl@0: TEST("Month between 1 and 12 is valid", g_date_valid_month(m)); sl@0: sl@0: day = 1; sl@0: sl@0: g_date_clear(days, 31); sl@0: sl@0: while (day <= dim) sl@0: { sl@0: guint i; sl@0: GDate tmp; sl@0: sl@0: TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y)); sl@0: sl@0: /* Create GDate with triplet */ sl@0: sl@0: d = &days[day-1]; sl@0: sl@0: TEST("Cleared day is invalid", !g_date_valid(d)); sl@0: sl@0: g_date_set_dmy(d,day,m,y); sl@0: sl@0: TEST("Set day is valid", g_date_valid(d)); sl@0: sl@0: if (m == G_DATE_JANUARY && day == 1) sl@0: { sl@0: first_day_of_year = g_date_get_julian(d); sl@0: } sl@0: sl@0: g_assert(first_day_of_year != G_DATE_BAD_JULIAN); sl@0: sl@0: TEST("Date with DMY triplet is valid", g_date_valid(d)); sl@0: TEST("Month accessor works", g_date_get_month(d) == m); sl@0: TEST("Year accessor works", g_date_get_year(d) == y); sl@0: TEST("Day of month accessor works", g_date_get_day(d) == day); sl@0: sl@0: TEST("Day of year is consistent with Julian dates", sl@0: ((g_date_get_julian(d) + 1 - first_day_of_year) == sl@0: (g_date_get_day_of_year(d)))); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print("first day: %u this day: %u day of year: %u\n", sl@0: first_day_of_year, sl@0: g_date_get_julian(d), sl@0: g_date_get_day_of_year(d)); sl@0: } sl@0: sl@0: if (m == G_DATE_DECEMBER && day == 31) sl@0: { sl@0: TEST("Last day of year equals number of days in year", sl@0: g_date_get_day_of_year(d) == days_in_year); sl@0: if (failed) sl@0: { sl@0: g_print("last day: %u days in year: %u\n", sl@0: g_date_get_day_of_year(d), days_in_year); sl@0: } sl@0: } sl@0: sl@0: TEST("Day of year is not more than number of days in the year", sl@0: g_date_get_day_of_year(d) <= days_in_year); sl@0: sl@0: TEST("Monday week of year is not more than number of weeks in the year", sl@0: g_date_get_monday_week_of_year(d) <= monday_weeks_in_year); sl@0: if (failed) sl@0: { sl@0: g_print("Weeks in year: %u\n", monday_weeks_in_year); sl@0: g_date_debug_print(d); sl@0: } sl@0: TEST("Monday week of year is >= than last week of year", sl@0: g_date_get_monday_week_of_year(d) >= monday_week_of_year); sl@0: sl@0: if (g_date_get_weekday(d) == G_DATE_MONDAY) sl@0: { sl@0: sl@0: TEST("Monday week of year on Monday 1 more than previous day's week of year", sl@0: (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1); sl@0: if ((m == G_DATE_JANUARY && day <= 4) || sl@0: (m == G_DATE_DECEMBER && day >= 29)) { sl@0: TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1", sl@0: (g_date_get_iso8601_week_of_year(d) == 1)); sl@0: } else { sl@0: TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year", sl@0: (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: TEST("Monday week of year on non-Monday 0 more than previous day's week of year", sl@0: (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0); sl@0: if (!(day == 1 && m == G_DATE_JANUARY)) { sl@0: TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (", sl@0: (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0); sl@0: } sl@0: } sl@0: sl@0: sl@0: monday_week_of_year = g_date_get_monday_week_of_year(d); sl@0: iso8601_week_of_year = g_date_get_iso8601_week_of_year(d); sl@0: sl@0: sl@0: TEST("Sunday week of year is not more than number of weeks in the year", sl@0: g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year); sl@0: if (failed) sl@0: { sl@0: g_date_debug_print(d); sl@0: } sl@0: TEST("Sunday week of year is >= than last week of year", sl@0: g_date_get_sunday_week_of_year(d) >= sunday_week_of_year); sl@0: sl@0: if (g_date_get_weekday(d) == G_DATE_SUNDAY) sl@0: { sl@0: TEST("Sunday week of year on Sunday 1 more than previous day's week of year", sl@0: (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1); sl@0: } sl@0: else sl@0: { sl@0: TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year", sl@0: (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0); sl@0: } sl@0: sl@0: sunday_week_of_year = g_date_get_sunday_week_of_year(d); sl@0: sl@0: TEST("Date is equal to itself", sl@0: g_date_compare(d,d) == 0); sl@0: sl@0: sl@0: /*************** Increments ***********/ sl@0: sl@0: i = 1; sl@0: while (i < 402) /* Need to get 400 year increments in */ sl@0: { sl@0: sl@0: /***** Days ******/ sl@0: tmp = *d; sl@0: g_date_add_days(d, i); sl@0: sl@0: TEST("Adding days gives a value greater than previous", sl@0: g_date_compare(d, &tmp) > 0); sl@0: sl@0: g_date_subtract_days(d, i); sl@0: TEST("Forward days then backward days returns us to current day", sl@0: g_date_get_day(d) == day); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: sl@0: TEST("Forward days then backward days returns us to current month", sl@0: g_date_get_month(d) == m); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: sl@0: TEST("Forward days then backward days returns us to current year", sl@0: g_date_get_year(d) == y); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: sl@0: /******* Months ********/ sl@0: sl@0: tmp = *d; sl@0: g_date_add_months(d, i); sl@0: TEST("Adding months gives a larger value", sl@0: g_date_compare(d, &tmp) > 0); sl@0: g_date_subtract_months(d, i); sl@0: sl@0: TEST("Forward months then backward months returns us to current month", sl@0: g_date_get_month(d) == m); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: sl@0: TEST("Forward months then backward months returns us to current year", sl@0: g_date_get_year(d) == y); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: sl@0: sl@0: if (day < 29) sl@0: { sl@0: /* Day should be unchanged */ sl@0: sl@0: TEST("Forward months then backward months returns us to current day", sl@0: g_date_get_day(d) == day); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: /* reset the day for later tests */ sl@0: g_date_set_day(d, day); sl@0: } sl@0: sl@0: /******* Years ********/ sl@0: sl@0: tmp = *d; sl@0: g_date_add_years(d, i); sl@0: sl@0: TEST("Adding years gives a larger value", sl@0: g_date_compare(d,&tmp) > 0); sl@0: sl@0: g_date_subtract_years(d, i); sl@0: sl@0: TEST("Forward years then backward years returns us to current month", sl@0: g_date_get_month(d) == m); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: sl@0: TEST("Forward years then backward years returns us to current year", sl@0: g_date_get_year(d) == y); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: sl@0: if (m != 2 && day != 29) sl@0: { sl@0: TEST("Forward years then backward years returns us to current day", sl@0: g_date_get_day(d) == day); sl@0: sl@0: if (failed) sl@0: { sl@0: g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y); sl@0: g_date_debug_print(d); sl@0: } sl@0: } sl@0: else sl@0: { sl@0: g_date_set_day(d, day); /* reset */ sl@0: } sl@0: sl@0: i += 10; sl@0: } sl@0: sl@0: /***** increment test relative to our local Julian count */ sl@0: sl@0: if (!discontinuity) { sl@0: sl@0: /* We can only run sequence tests between sequential years */ sl@0: sl@0: TEST("Julians are sequential with increment 1", sl@0: j+1 == g_date_get_julian(d)); sl@0: if (failed) sl@0: { sl@0: g_print("Out of sequence, prev: %u expected: %u got: %u\n", sl@0: j, j+1, g_date_get_julian(d)); sl@0: } sl@0: sl@0: g_date_add_days(d,1); sl@0: TEST("Next day has julian 1 higher", sl@0: g_date_get_julian(d) == j + 2); sl@0: g_date_subtract_days(d, 1); sl@0: sl@0: if (j != G_DATE_BAD_JULIAN) sl@0: { sl@0: g_date_subtract_days(d, 1); sl@0: sl@0: TEST("Previous day has julian 1 lower", sl@0: g_date_get_julian(d) == j); sl@0: sl@0: g_date_add_days(d, 1); /* back to original */ sl@0: } sl@0: } sl@0: discontinuity = FALSE; /* goes away now */ sl@0: sl@0: fflush(stdout); sl@0: fflush(stderr); sl@0: sl@0: j = g_date_get_julian(d); /* inc current julian */ sl@0: sl@0: ++day; sl@0: } sl@0: ++m; sl@0: } sl@0: // g_print(" done\n"); sl@0: ++i; sl@0: prev_y = y; sl@0: y = check_years[i]; sl@0: if (prev_y == G_DATE_BAD_YEAR || sl@0: (prev_y + 1) != y) discontinuity = TRUE; sl@0: } sl@0: sl@0: sl@0: // g_print("\n%u tests passed, %u failed\n",passed, notpassed); sl@0: #ifdef SYMBIAN sl@0: testResultXml("testgdate"); sl@0: #endif /* EMULATOR */ sl@0: return 0; sl@0: } sl@0: sl@0: