1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/glib/tsrc/BC/tests/testgdate.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,539 @@
1.4 +/* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/
1.5 +#undef G_DISABLE_ASSERT
1.6 +#undef G_LOG_DOMAIN
1.7 +
1.8 +#ifdef GLIB_COMPILATION
1.9 +#undef GLIB_COMPILATION
1.10 +#endif
1.11 +
1.12 +#include "glib.h"
1.13 +
1.14 +#include <stdio.h>
1.15 +#include <string.h>
1.16 +#include <locale.h>
1.17 +#include <time.h>
1.18 +
1.19 +#ifdef SYMBIAN
1.20 +#include "mrt2_glib2_test.h"
1.21 +#endif /*SYMBIAN*/
1.22 +
1.23 +
1.24 +gboolean failed = FALSE;
1.25 +guint32 passed = 0;
1.26 +guint32 notpassed = 0;
1.27 +
1.28 +#define TEST(m,cond) G_STMT_START { failed = !(cond); \
1.29 +if (failed) \
1.30 + { assert_failed = TRUE; \
1.31 + ++notpassed; \
1.32 + if (!m) \
1.33 + g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
1.34 + else \
1.35 + g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
1.36 + } \
1.37 +else \
1.38 + ++passed; \
1.39 + if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \
1.40 +} G_STMT_END
1.41 +
1.42 +void g_date_debug_print(GDate* d)
1.43 +{
1.44 + if (!d) g_print("NULL!\n");
1.45 + else
1.46 + g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
1.47 + d->julian_days,
1.48 + d->julian ? "valid" : "invalid",
1.49 + d->day,
1.50 + d->month,
1.51 + d->year,
1.52 + d->dmy ? "valid" : "invalid");
1.53 +
1.54 + fflush(stdout);
1.55 +}
1.56 +
1.57 +int main(int argc, char** argv)
1.58 +{
1.59 + GDate* d;
1.60 + guint32 j;
1.61 + GDateMonth m;
1.62 + GDateYear y, prev_y;
1.63 + GDateDay day;
1.64 + gchar buf[101];
1.65 + gchar* loc;
1.66 +
1.67 + guint32 num_chars_written_to_buf;
1.68 +
1.69 + /* Try to get all the leap year cases. */
1.70 + GDateYear check_years[] = {
1.71 + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
1.72 + // Less number of years to test on emulator as the time taken
1.73 + // on ATS is very large
1.74 +#if !defined(__WINS__) && !defined(__WINSCW__)
1.75 + ,11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
1.76 + 398, 399, 400, 401, 402, 403, 404, 405, 406,
1.77 + 1598, 1599, 1600, 1601, 1602, 1650, 1651,
1.78 + 1897, 1898, 1899, 1900, 1901, 1902, 1903,
1.79 + 1961, 1962, 1963, 1964, 1965, 1967,
1.80 + 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
1.81 + 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
1.82 + 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
1.83 + 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
1.84 + 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
1.85 + 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
1.86 +#endif
1.87 + };
1.88 +
1.89 +
1.90 +
1.91 +
1.92 + guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
1.93 + guint i;
1.94 + gboolean discontinuity;
1.95 +
1.96 + #ifdef SYMBIAN
1.97 + 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);
1.98 + g_set_print_handler(mrtPrintHandler);
1.99 + #endif /*SYMBIAN*/
1.100 +
1.101 + //g_print("checking GDate...");
1.102 +
1.103 + TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
1.104 +
1.105 + d = g_date_new();
1.106 +
1.107 + TEST("Empty constructor produces invalid date", !g_date_valid(d));
1.108 +
1.109 + g_date_free(d);
1.110 +
1.111 + d = g_date_new_dmy(1,1,1);
1.112 +
1.113 + TEST("January 1, Year 1 created and valid", g_date_valid(d));
1.114 +
1.115 + j = g_date_get_julian(d);
1.116 +
1.117 + TEST("January 1, Year 1 is Julian date 1", j == 1);
1.118 +
1.119 + TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
1.120 + TEST("Returned day is 1", g_date_get_day(d) == 1);
1.121 + TEST("Returned year is 1", g_date_get_year(d) == 1);
1.122 +
1.123 + TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
1.124 + TEST("Month 13 is invalid", !g_date_valid_month(13));
1.125 + TEST("Bad day is invalid", !g_date_valid_day(G_DATE_BAD_DAY));
1.126 + TEST("Day 32 is invalid", !g_date_valid_day(32));
1.127 + TEST("Bad year is invalid", !g_date_valid_year(G_DATE_BAD_YEAR));
1.128 + TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
1.129 + TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
1.130 + TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
1.131 + TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
1.132 + TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
1.133 + TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
1.134 + TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
1.135 + TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
1.136 +
1.137 + g_date_free(d);
1.138 +
1.139 + loc = setlocale(LC_ALL,"");
1.140 + if (!loc)
1.141 + g_print("\nLocale unchanged\n");
1.142 +
1.143 + d = g_date_new();
1.144 + g_date_set_time(d, time(NULL));
1.145 + TEST("Today is valid", g_date_valid(d));
1.146 +
1.147 + num_chars_written_to_buf = g_date_strftime(buf,100,"Today is a %A, %x\n", d);
1.148 + g_assert(num_chars_written_to_buf != 0);
1.149 + if(!num_chars_written_to_buf)
1.150 + g_print("g_date_strftime is failed @ line = %d",__LINE__);
1.151 +
1.152 + g_date_set_time(d, 1);
1.153 + TEST("Beginning of Unix epoch is valid", g_date_valid(d));
1.154 +
1.155 + 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);
1.156 + g_assert(num_chars_written_to_buf != 0);
1.157 + if(!num_chars_written_to_buf)
1.158 + g_print("g_date_strftime is failed @ line = %d",__LINE__);
1.159 +
1.160 + g_date_set_julian(d, 1);
1.161 + TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
1.162 +
1.163 + num_chars_written_to_buf = g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
1.164 + d);
1.165 + g_assert(num_chars_written_to_buf != 0);
1.166 + if(!num_chars_written_to_buf)
1.167 + g_print("g_date_strftime is failed @ line = %d",__LINE__);
1.168 +
1.169 + g_date_set_dmy(d, 10, 1, 2000);
1.170 +
1.171 + num_chars_written_to_buf = g_date_strftime(buf,100,"%x", d);
1.172 + g_assert(num_chars_written_to_buf != 0);
1.173 + if(!num_chars_written_to_buf)
1.174 + g_print("g_date_strftime is failed @ line = %d",__LINE__);
1.175 +
1.176 + g_date_set_parse(d, buf);
1.177 + /* Note: this test will hopefully work, but no promises. */
1.178 + TEST("Successfully parsed a %x-formatted string",
1.179 + g_date_valid(d) &&
1.180 + g_date_get_month(d) == 1 &&
1.181 + g_date_get_day(d) == 10 &&
1.182 + g_date_get_year(d) == 2000);
1.183 + if (failed)
1.184 + g_date_debug_print(d);
1.185 +
1.186 + g_date_free(d);
1.187 +
1.188 + j = G_DATE_BAD_JULIAN;
1.189 +
1.190 + i = 0;
1.191 + discontinuity = TRUE;
1.192 + y = check_years[0];
1.193 + prev_y = G_DATE_BAD_YEAR;
1.194 + while (i < n_check_years)
1.195 + {
1.196 + guint32 first_day_of_year = G_DATE_BAD_JULIAN;
1.197 + guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
1.198 + guint sunday_week_of_year = 0;
1.199 + guint sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
1.200 + guint monday_week_of_year = 0;
1.201 + guint monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
1.202 + guint iso8601_week_of_year = 0;
1.203 +
1.204 + if (discontinuity)
1.205 + ;
1.206 + //g_print(" (Break in sequence of requested years to check)\n");
1.207 +
1.208 + //g_print("Checking year %u", y);
1.209 +
1.210 + TEST("Year is valid", g_date_valid_year(y));
1.211 +
1.212 + TEST("Number of Sunday weeks in year is 52 or 53",
1.213 + sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
1.214 +
1.215 + TEST("Number of Monday weeks in year is 52 or 53",
1.216 + monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
1.217 +
1.218 + m = 1;
1.219 + while (m < 13)
1.220 + {
1.221 + guint8 dim = g_date_get_days_in_month(m,y);
1.222 + GDate days[31]; /* This is the fast way, no allocation */
1.223 +
1.224 + TEST("Sensible number of days in month", (dim > 0 && dim < 32));
1.225 +
1.226 + TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
1.227 +
1.228 + day = 1;
1.229 +
1.230 + g_date_clear(days, 31);
1.231 +
1.232 + while (day <= dim)
1.233 + {
1.234 + guint i;
1.235 + GDate tmp;
1.236 +
1.237 + TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
1.238 +
1.239 + /* Create GDate with triplet */
1.240 +
1.241 + d = &days[day-1];
1.242 +
1.243 + TEST("Cleared day is invalid", !g_date_valid(d));
1.244 +
1.245 + g_date_set_dmy(d,day,m,y);
1.246 +
1.247 + TEST("Set day is valid", g_date_valid(d));
1.248 +
1.249 + if (m == G_DATE_JANUARY && day == 1)
1.250 + {
1.251 + first_day_of_year = g_date_get_julian(d);
1.252 + }
1.253 +
1.254 + g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
1.255 +
1.256 + TEST("Date with DMY triplet is valid", g_date_valid(d));
1.257 + TEST("Month accessor works", g_date_get_month(d) == m);
1.258 + TEST("Year accessor works", g_date_get_year(d) == y);
1.259 + TEST("Day of month accessor works", g_date_get_day(d) == day);
1.260 +
1.261 + TEST("Day of year is consistent with Julian dates",
1.262 + ((g_date_get_julian(d) + 1 - first_day_of_year) ==
1.263 + (g_date_get_day_of_year(d))));
1.264 +
1.265 + if (failed)
1.266 + {
1.267 + g_print("first day: %u this day: %u day of year: %u\n",
1.268 + first_day_of_year,
1.269 + g_date_get_julian(d),
1.270 + g_date_get_day_of_year(d));
1.271 + }
1.272 +
1.273 + if (m == G_DATE_DECEMBER && day == 31)
1.274 + {
1.275 + TEST("Last day of year equals number of days in year",
1.276 + g_date_get_day_of_year(d) == days_in_year);
1.277 + if (failed)
1.278 + {
1.279 + g_print("last day: %u days in year: %u\n",
1.280 + g_date_get_day_of_year(d), days_in_year);
1.281 + }
1.282 + }
1.283 +
1.284 + TEST("Day of year is not more than number of days in the year",
1.285 + g_date_get_day_of_year(d) <= days_in_year);
1.286 +
1.287 + TEST("Monday week of year is not more than number of weeks in the year",
1.288 + g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
1.289 + if (failed)
1.290 + {
1.291 + g_print("Weeks in year: %u\n", monday_weeks_in_year);
1.292 + g_date_debug_print(d);
1.293 + }
1.294 + TEST("Monday week of year is >= than last week of year",
1.295 + g_date_get_monday_week_of_year(d) >= monday_week_of_year);
1.296 +
1.297 + if (g_date_get_weekday(d) == G_DATE_MONDAY)
1.298 + {
1.299 +
1.300 + TEST("Monday week of year on Monday 1 more than previous day's week of year",
1.301 + (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
1.302 + if ((m == G_DATE_JANUARY && day <= 4) ||
1.303 + (m == G_DATE_DECEMBER && day >= 29)) {
1.304 + TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
1.305 + (g_date_get_iso8601_week_of_year(d) == 1));
1.306 + } else {
1.307 + TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
1.308 + (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
1.309 + }
1.310 + }
1.311 + else
1.312 + {
1.313 + TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
1.314 + (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
1.315 + if (!(day == 1 && m == G_DATE_JANUARY)) {
1.316 + TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
1.317 + (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
1.318 + }
1.319 + }
1.320 +
1.321 +
1.322 + monday_week_of_year = g_date_get_monday_week_of_year(d);
1.323 + iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
1.324 +
1.325 +
1.326 + TEST("Sunday week of year is not more than number of weeks in the year",
1.327 + g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
1.328 + if (failed)
1.329 + {
1.330 + g_date_debug_print(d);
1.331 + }
1.332 + TEST("Sunday week of year is >= than last week of year",
1.333 + g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
1.334 +
1.335 + if (g_date_get_weekday(d) == G_DATE_SUNDAY)
1.336 + {
1.337 + TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
1.338 + (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
1.339 + }
1.340 + else
1.341 + {
1.342 + TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
1.343 + (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
1.344 + }
1.345 +
1.346 + sunday_week_of_year = g_date_get_sunday_week_of_year(d);
1.347 +
1.348 + TEST("Date is equal to itself",
1.349 + g_date_compare(d,d) == 0);
1.350 +
1.351 +
1.352 + /*************** Increments ***********/
1.353 +
1.354 + i = 1;
1.355 + while (i < 402) /* Need to get 400 year increments in */
1.356 + {
1.357 +
1.358 + /***** Days ******/
1.359 + tmp = *d;
1.360 + g_date_add_days(d, i);
1.361 +
1.362 + TEST("Adding days gives a value greater than previous",
1.363 + g_date_compare(d, &tmp) > 0);
1.364 +
1.365 + g_date_subtract_days(d, i);
1.366 + TEST("Forward days then backward days returns us to current day",
1.367 + g_date_get_day(d) == day);
1.368 +
1.369 + if (failed)
1.370 + {
1.371 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.372 + g_date_debug_print(d);
1.373 + }
1.374 +
1.375 + TEST("Forward days then backward days returns us to current month",
1.376 + g_date_get_month(d) == m);
1.377 +
1.378 + if (failed)
1.379 + {
1.380 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.381 + g_date_debug_print(d);
1.382 + }
1.383 +
1.384 + TEST("Forward days then backward days returns us to current year",
1.385 + g_date_get_year(d) == y);
1.386 +
1.387 + if (failed)
1.388 + {
1.389 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.390 + g_date_debug_print(d);
1.391 + }
1.392 +
1.393 + /******* Months ********/
1.394 +
1.395 + tmp = *d;
1.396 + g_date_add_months(d, i);
1.397 + TEST("Adding months gives a larger value",
1.398 + g_date_compare(d, &tmp) > 0);
1.399 + g_date_subtract_months(d, i);
1.400 +
1.401 + TEST("Forward months then backward months returns us to current month",
1.402 + g_date_get_month(d) == m);
1.403 +
1.404 + if (failed)
1.405 + {
1.406 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.407 + g_date_debug_print(d);
1.408 + }
1.409 +
1.410 + TEST("Forward months then backward months returns us to current year",
1.411 + g_date_get_year(d) == y);
1.412 +
1.413 + if (failed)
1.414 + {
1.415 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.416 + g_date_debug_print(d);
1.417 + }
1.418 +
1.419 +
1.420 + if (day < 29)
1.421 + {
1.422 + /* Day should be unchanged */
1.423 +
1.424 + TEST("Forward months then backward months returns us to current day",
1.425 + g_date_get_day(d) == day);
1.426 +
1.427 + if (failed)
1.428 + {
1.429 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.430 + g_date_debug_print(d);
1.431 + }
1.432 + }
1.433 + else
1.434 + {
1.435 + /* reset the day for later tests */
1.436 + g_date_set_day(d, day);
1.437 + }
1.438 +
1.439 + /******* Years ********/
1.440 +
1.441 + tmp = *d;
1.442 + g_date_add_years(d, i);
1.443 +
1.444 + TEST("Adding years gives a larger value",
1.445 + g_date_compare(d,&tmp) > 0);
1.446 +
1.447 + g_date_subtract_years(d, i);
1.448 +
1.449 + TEST("Forward years then backward years returns us to current month",
1.450 + g_date_get_month(d) == m);
1.451 +
1.452 + if (failed)
1.453 + {
1.454 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.455 + g_date_debug_print(d);
1.456 + }
1.457 +
1.458 + TEST("Forward years then backward years returns us to current year",
1.459 + g_date_get_year(d) == y);
1.460 +
1.461 + if (failed)
1.462 + {
1.463 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.464 + g_date_debug_print(d);
1.465 + }
1.466 +
1.467 + if (m != 2 && day != 29)
1.468 + {
1.469 + TEST("Forward years then backward years returns us to current day",
1.470 + g_date_get_day(d) == day);
1.471 +
1.472 + if (failed)
1.473 + {
1.474 + g_print(" (increment %u, dmy %u %u %u) ", i, day, m, y);
1.475 + g_date_debug_print(d);
1.476 + }
1.477 + }
1.478 + else
1.479 + {
1.480 + g_date_set_day(d, day); /* reset */
1.481 + }
1.482 +
1.483 + i += 10;
1.484 + }
1.485 +
1.486 + /***** increment test relative to our local Julian count */
1.487 +
1.488 + if (!discontinuity) {
1.489 +
1.490 + /* We can only run sequence tests between sequential years */
1.491 +
1.492 + TEST("Julians are sequential with increment 1",
1.493 + j+1 == g_date_get_julian(d));
1.494 + if (failed)
1.495 + {
1.496 + g_print("Out of sequence, prev: %u expected: %u got: %u\n",
1.497 + j, j+1, g_date_get_julian(d));
1.498 + }
1.499 +
1.500 + g_date_add_days(d,1);
1.501 + TEST("Next day has julian 1 higher",
1.502 + g_date_get_julian(d) == j + 2);
1.503 + g_date_subtract_days(d, 1);
1.504 +
1.505 + if (j != G_DATE_BAD_JULIAN)
1.506 + {
1.507 + g_date_subtract_days(d, 1);
1.508 +
1.509 + TEST("Previous day has julian 1 lower",
1.510 + g_date_get_julian(d) == j);
1.511 +
1.512 + g_date_add_days(d, 1); /* back to original */
1.513 + }
1.514 + }
1.515 + discontinuity = FALSE; /* goes away now */
1.516 +
1.517 + fflush(stdout);
1.518 + fflush(stderr);
1.519 +
1.520 + j = g_date_get_julian(d); /* inc current julian */
1.521 +
1.522 + ++day;
1.523 + }
1.524 + ++m;
1.525 + }
1.526 + // g_print(" done\n");
1.527 + ++i;
1.528 + prev_y = y;
1.529 + y = check_years[i];
1.530 + if (prev_y == G_DATE_BAD_YEAR ||
1.531 + (prev_y + 1) != y) discontinuity = TRUE;
1.532 + }
1.533 +
1.534 +
1.535 + // g_print("\n%u tests passed, %u failed\n",passed, notpassed);
1.536 +#ifdef SYMBIAN
1.537 + testResultXml("testgdate");
1.538 +#endif /* EMULATOR */
1.539 + return 0;
1.540 +}
1.541 +
1.542 +