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