glib/tests/testgdate.c
changeset 18 47c74d1534e1
child 34 5fae379060a7
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 #undef G_DISABLE_ASSERT
       
    19 #undef G_LOG_DOMAIN
       
    20 
       
    21 #ifdef GLIB_COMPILATION
       
    22 #undef GLIB_COMPILATION
       
    23 #endif
       
    24 
       
    25 #include "glib.h"
       
    26 
       
    27 #include <stdio.h>
       
    28 #include <string.h>
       
    29 #include <locale.h>
       
    30 #include <time.h>
       
    31 
       
    32 #ifdef __SYMBIAN32__
       
    33 #include "mrt2_glib2_test.h"
       
    34 #endif /*__SYMBIAN32__*/
       
    35 
       
    36 
       
    37 gboolean failed = FALSE;
       
    38 guint32 passed = 0;
       
    39 guint32 notpassed = 0;
       
    40 
       
    41 #define	TEST(m,cond)	G_STMT_START { failed = !(cond); \
       
    42 if (failed) \
       
    43   { ++notpassed; \
       
    44     if (!m) \
       
    45       g_print ("\n(%s:%d) failed for: %s\n", __FILE__, __LINE__, ( # cond )); \
       
    46     else \
       
    47       g_print ("\n(%s:%d) failed for: %s: (%s)\n", __FILE__, __LINE__, ( # cond ), (gchar*)m); \
       
    48   } \
       
    49 else \
       
    50   ++passed;    \
       
    51   if ((passed+notpassed) % 10000 == 0) /*g_print (".")*/; fflush (stdout); \
       
    52 } G_STMT_END
       
    53 
       
    54 void g_date_debug_print(GDate* d)
       
    55 {
       
    56   if (!d) g_print("NULL!\n");
       
    57   else 
       
    58     g_print("julian: %u (%s) DMY: %u %u %u (%s)\n",
       
    59 	    d->julian_days, 
       
    60 	    d->julian ? "valid" : "invalid",
       
    61 	    d->day,
       
    62 	    d->month,
       
    63 	    d->year,
       
    64 	    d->dmy ? "valid" : "invalid");
       
    65   
       
    66   fflush(stdout);
       
    67 }
       
    68 
       
    69 int main(int argc, char** argv)
       
    70 {
       
    71   GDate* d;
       
    72   guint32 j;
       
    73   GDateMonth m;
       
    74   GDateYear y, prev_y;
       
    75   GDateDay day;
       
    76   gchar buf[101];
       
    77   gchar* loc;
       
    78   /* Try to get all the leap year cases. */
       
    79   GDateYear check_years[] = { 
       
    80     1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
       
    81     11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397, 
       
    82     398, 399, 400, 401, 402, 403, 404, 405, 406,
       
    83     1598, 1599, 1600, 1601, 1602, 1650, 1651,
       
    84     1897, 1898, 1899, 1900, 1901, 1902, 1903, 
       
    85     1961, 1962, 1963, 1964, 1965, 1967,
       
    86     1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
       
    87     1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 
       
    88     1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 
       
    89     1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 
       
    90     2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
       
    91     3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
       
    92   };
       
    93   guint n_check_years = sizeof(check_years)/sizeof(GDateYear);
       
    94   guint i;
       
    95   gboolean discontinuity;
       
    96 	
       
    97   #ifdef __SYMBIAN32__
       
    98   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);
       
    99   g_set_print_handler(mrtPrintHandler);
       
   100   #endif /*__SYMBIAN32__*/
       
   101 	  
       
   102   g_print("checking GDate...");
       
   103   
       
   104   TEST("sizeof(GDate) is not more than 8 bytes on this platform", sizeof(GDate) < 9);
       
   105 
       
   106   d = g_date_new();
       
   107 
       
   108   TEST("Empty constructor produces invalid date", !g_date_valid(d));
       
   109 
       
   110   g_date_free(d);
       
   111 
       
   112   d = g_date_new_dmy(1,1,1);
       
   113 
       
   114   TEST("January 1, Year 1 created and valid", g_date_valid(d));
       
   115 
       
   116   j = g_date_get_julian(d);
       
   117   
       
   118   TEST("January 1, Year 1 is Julian date 1", j == 1);
       
   119 
       
   120   TEST("Returned month is January", g_date_get_month(d) == G_DATE_JANUARY);
       
   121   TEST("Returned day is 1", g_date_get_day(d) == 1);
       
   122   TEST("Returned year is 1", g_date_get_year(d) == 1);
       
   123 
       
   124   TEST("Bad month is invalid", !g_date_valid_month(G_DATE_BAD_MONTH));
       
   125   TEST("Month 13 is invalid",  !g_date_valid_month(13));
       
   126   TEST("Bad day is invalid",   !g_date_valid_day(G_DATE_BAD_DAY));
       
   127   TEST("Day 32 is invalid",     !g_date_valid_day(32));
       
   128   TEST("Bad year is invalid",  !g_date_valid_year(G_DATE_BAD_YEAR));
       
   129   TEST("Bad julian is invalid", !g_date_valid_julian(G_DATE_BAD_JULIAN));
       
   130   TEST("Bad weekday is invalid", !g_date_valid_weekday(G_DATE_BAD_WEEKDAY));
       
   131   TEST("Year 2000 is a leap year", g_date_is_leap_year(2000));
       
   132   TEST("Year 1999 is not a leap year", !g_date_is_leap_year(1999));
       
   133   TEST("Year 1996 is a leap year", g_date_is_leap_year(1996));
       
   134   TEST("Year 1600 is a leap year", g_date_is_leap_year(1600));
       
   135   TEST("Year 2100 is not a leap year", !g_date_is_leap_year(2100));
       
   136   TEST("Year 1800 is not a leap year", !g_date_is_leap_year(1800));
       
   137 
       
   138   g_date_free(d);
       
   139   
       
   140   loc = setlocale(LC_ALL,"");
       
   141   if (loc) 
       
   142     g_print("\nLocale set to %s\n", loc);
       
   143   else 
       
   144     g_print("\nLocale unchanged\n");
       
   145 
       
   146   d = g_date_new();
       
   147   g_date_set_time(d, time(NULL));
       
   148   TEST("Today is valid", g_date_valid(d));
       
   149 
       
   150   g_date_strftime(buf,100,"Today is a %A, %x\n", d);
       
   151   g_print("%s", buf);
       
   152 
       
   153   g_date_set_time(d, 1);
       
   154   TEST("Beginning of Unix epoch is valid", g_date_valid(d));
       
   155 
       
   156   g_date_strftime(buf,100,"1 second into the Unix epoch it was a %A, in the month of %B, %x\n", d);
       
   157   g_print("%s", buf);
       
   158 
       
   159   g_date_set_julian(d, 1);
       
   160   TEST("GDate's \"Julian\" epoch's first day is valid", g_date_valid(d));
       
   161 
       
   162   g_date_strftime(buf,100,"Our \"Julian\" epoch begins on a %A, in the month of %B, %x\n",
       
   163 		  d);
       
   164   g_print("%s", buf);
       
   165 
       
   166   g_date_set_dmy(d, 10, 1, 2000);
       
   167 
       
   168   g_date_strftime(buf,100,"%x", d);
       
   169 
       
   170   g_date_set_parse(d, buf);
       
   171   /* Note: this test will hopefully work, but no promises. */
       
   172   TEST("Successfully parsed a %x-formatted string", 
       
   173        g_date_valid(d) && 
       
   174        g_date_get_month(d) == 1 && 
       
   175        g_date_get_day(d) == 10 && 
       
   176        g_date_get_year(d) == 2000);
       
   177   if (failed)
       
   178     g_date_debug_print(d);
       
   179   
       
   180   g_date_free(d);
       
   181 
       
   182   j = G_DATE_BAD_JULIAN;
       
   183 
       
   184   i = 0;
       
   185   discontinuity = TRUE;
       
   186   y      = check_years[0];
       
   187   prev_y = G_DATE_BAD_YEAR;
       
   188   while (i < n_check_years) 
       
   189     {
       
   190       guint32 first_day_of_year = G_DATE_BAD_JULIAN;
       
   191       guint16 days_in_year = g_date_is_leap_year(y) ? 366 : 365;
       
   192       guint   sunday_week_of_year = 0;
       
   193       guint   sunday_weeks_in_year = g_date_get_sunday_weeks_in_year(y);
       
   194       guint   monday_week_of_year = 0;
       
   195       guint   monday_weeks_in_year = g_date_get_monday_weeks_in_year(y);
       
   196       guint   iso8601_week_of_year = 0;
       
   197 
       
   198       if (discontinuity)
       
   199         g_print(" (Break in sequence of requested years to check)\n");
       
   200 
       
   201       g_print("Checking year %u", y);
       
   202 
       
   203       TEST("Year is valid", g_date_valid_year(y));
       
   204 
       
   205       TEST("Number of Sunday weeks in year is 52 or 53", 
       
   206 	   sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
       
   207       
       
   208       TEST("Number of Monday weeks in year is 52 or 53", 
       
   209 	   monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
       
   210 	   
       
   211       m = 1;
       
   212       while (m < 13) 
       
   213 	{
       
   214 	  guint8 dim = g_date_get_days_in_month(m,y);
       
   215 	  GDate days[31];         /* This is the fast way, no allocation */
       
   216 
       
   217 	  TEST("Sensible number of days in month", (dim > 0 && dim < 32));
       
   218 
       
   219 	  TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
       
   220 
       
   221 	  day = 1;
       
   222 
       
   223 	  g_date_clear(days, 31);
       
   224 
       
   225 	  while (day <= dim) 
       
   226 	    {
       
   227 	      guint i;
       
   228               GDate tmp;
       
   229 
       
   230 	      TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
       
   231 
       
   232 	      /* Create GDate with triplet */
       
   233 	      
       
   234 	      d = &days[day-1];
       
   235 
       
   236 	      TEST("Cleared day is invalid", !g_date_valid(d));
       
   237 
       
   238 	      g_date_set_dmy(d,day,m,y);
       
   239 
       
   240 	      TEST("Set day is valid", g_date_valid(d));
       
   241 
       
   242 	      if (m == G_DATE_JANUARY && day == 1) 
       
   243 		{
       
   244 		  first_day_of_year = g_date_get_julian(d);
       
   245 		}
       
   246 
       
   247 	      g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
       
   248 
       
   249 	      TEST("Date with DMY triplet is valid", g_date_valid(d));
       
   250 	      TEST("Month accessor works", g_date_get_month(d) == m);
       
   251 	      TEST("Year accessor works", g_date_get_year(d) == y);
       
   252 	      TEST("Day of month accessor works", g_date_get_day(d) == day);
       
   253 
       
   254 	      TEST("Day of year is consistent with Julian dates",
       
   255 		   ((g_date_get_julian(d) + 1 - first_day_of_year) ==
       
   256 		    (g_date_get_day_of_year(d))));
       
   257 
       
   258 	      if (failed) 
       
   259 		{
       
   260 		  g_print("first day: %u this day: %u day of year: %u\n", 
       
   261 			  first_day_of_year, 
       
   262 			  g_date_get_julian(d),
       
   263 			  g_date_get_day_of_year(d));
       
   264 		}
       
   265 	      
       
   266 	      if (m == G_DATE_DECEMBER && day == 31) 
       
   267 		{
       
   268 		  TEST("Last day of year equals number of days in year", 
       
   269 		       g_date_get_day_of_year(d) == days_in_year);
       
   270 		  if (failed) 
       
   271 		    {
       
   272 		      g_print("last day: %u days in year: %u\n", 
       
   273 			      g_date_get_day_of_year(d), days_in_year);
       
   274 		    }
       
   275 		}
       
   276 
       
   277 	      TEST("Day of year is not more than number of days in the year",
       
   278 		   g_date_get_day_of_year(d) <= days_in_year);
       
   279 
       
   280 	      TEST("Monday week of year is not more than number of weeks in the year",
       
   281 		   g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
       
   282 	      if (failed)
       
   283 		{
       
   284 		  g_print("Weeks in year: %u\n", monday_weeks_in_year);
       
   285 		  g_date_debug_print(d);
       
   286 		}
       
   287 	      TEST("Monday week of year is >= than last week of year",
       
   288 		   g_date_get_monday_week_of_year(d) >= monday_week_of_year);
       
   289 
       
   290 	      if (g_date_get_weekday(d) == G_DATE_MONDAY) 
       
   291 		{
       
   292 		  
       
   293 		  TEST("Monday week of year on Monday 1 more than previous day's week of year",
       
   294 		       (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
       
   295 		  if ((m == G_DATE_JANUARY && day <= 4) ||
       
   296 		      (m == G_DATE_DECEMBER && day >= 29)) {
       
   297 		    TEST("ISO 8601 week of year on Monday Dec 29 - Jan 4 is 1",
       
   298 			 (g_date_get_iso8601_week_of_year(d) == 1));
       
   299 		  } else {
       
   300 		    TEST("ISO 8601 week of year on Monday 1 more than previous day's week of year",
       
   301 			 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 1);
       
   302 		  }
       
   303 		}
       
   304 	      else 
       
   305 		{
       
   306 		  TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
       
   307 		       (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
       
   308 		  if (!(day == 1 && m == G_DATE_JANUARY)) {
       
   309 		    TEST("ISO 8601 week of year on non-Monday 0 more than previous day's week of year (",
       
   310 			 (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year) == 0);
       
   311 		  }
       
   312 		}
       
   313 
       
   314 
       
   315 	      monday_week_of_year = g_date_get_monday_week_of_year(d);
       
   316 	      iso8601_week_of_year = g_date_get_iso8601_week_of_year(d);
       
   317 
       
   318 
       
   319 	      TEST("Sunday week of year is not more than number of weeks in the year",
       
   320 		   g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
       
   321 	      if (failed)
       
   322 		{
       
   323 		  g_date_debug_print(d);
       
   324 		}
       
   325 	      TEST("Sunday week of year is >= than last week of year",
       
   326 		   g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
       
   327 
       
   328 	      if (g_date_get_weekday(d) == G_DATE_SUNDAY) 
       
   329 		{
       
   330 		  TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
       
   331 		       (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
       
   332 		}
       
   333 	      else 
       
   334 		{
       
   335 		  TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
       
   336 		       (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
       
   337 		}
       
   338 
       
   339 	      sunday_week_of_year = g_date_get_sunday_week_of_year(d);
       
   340 
       
   341 	      TEST("Date is equal to itself",
       
   342 		   g_date_compare(d,d) == 0);
       
   343 
       
   344 
       
   345 	      /*************** Increments ***********/
       
   346 
       
   347               i = 1;
       
   348               while (i < 402) /* Need to get 400 year increments in */ 
       
   349                 {
       
   350 	      
       
   351                   /***** Days ******/
       
   352                   tmp = *d;
       
   353                   g_date_add_days(d, i);
       
   354 
       
   355                   TEST("Adding days gives a value greater than previous",
       
   356                        g_date_compare(d, &tmp) > 0);
       
   357 
       
   358                   g_date_subtract_days(d, i);
       
   359                   TEST("Forward days then backward days returns us to current day",
       
   360                        g_date_get_day(d) == day);
       
   361 
       
   362                   if (failed) 
       
   363                     {
       
   364                       g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   365                       g_date_debug_print(d);
       
   366                     }
       
   367 
       
   368                   TEST("Forward days then backward days returns us to current month",
       
   369                        g_date_get_month(d) == m);
       
   370 
       
   371                   if (failed) 
       
   372                     {
       
   373                       g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   374                       g_date_debug_print(d);
       
   375                     }
       
   376 
       
   377                   TEST("Forward days then backward days returns us to current year",
       
   378                        g_date_get_year(d) == y);
       
   379 
       
   380                   if (failed) 
       
   381                     {
       
   382                       g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   383                       g_date_debug_print(d);
       
   384                     }
       
   385 
       
   386                   /******* Months ********/
       
   387 
       
   388                   tmp = *d;
       
   389                   g_date_add_months(d, i);
       
   390                   TEST("Adding months gives a larger value",
       
   391                        g_date_compare(d, &tmp) > 0);
       
   392                   g_date_subtract_months(d, i);
       
   393 
       
   394                   TEST("Forward months then backward months returns us to current month",
       
   395                        g_date_get_month(d) == m);
       
   396 
       
   397                   if (failed) 
       
   398                     {
       
   399                       g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   400                       g_date_debug_print(d);
       
   401                     }
       
   402 
       
   403                   TEST("Forward months then backward months returns us to current year",
       
   404                        g_date_get_year(d) == y);
       
   405 
       
   406                   if (failed) 
       
   407                     {
       
   408                       g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   409                       g_date_debug_print(d);
       
   410                     }
       
   411 
       
   412 		  
       
   413                   if (day < 29) 
       
   414                     {
       
   415                       /* Day should be unchanged */
       
   416 		      
       
   417                       TEST("Forward months then backward months returns us to current day",
       
   418                            g_date_get_day(d) == day);
       
   419 		      
       
   420                       if (failed) 
       
   421                         {
       
   422                           g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   423                           g_date_debug_print(d);
       
   424                         }
       
   425                     }
       
   426                   else 
       
   427                     {
       
   428                       /* reset the day for later tests */
       
   429                       g_date_set_day(d, day);
       
   430                     }
       
   431 
       
   432                   /******* Years ********/
       
   433 
       
   434                   tmp = *d;
       
   435                   g_date_add_years(d, i);
       
   436 
       
   437                   TEST("Adding years gives a larger value",
       
   438                        g_date_compare(d,&tmp) > 0);
       
   439 		      
       
   440                   g_date_subtract_years(d, i);
       
   441 
       
   442                   TEST("Forward years then backward years returns us to current month",
       
   443                        g_date_get_month(d) == m);
       
   444 
       
   445                   if (failed) 
       
   446                     {
       
   447                       g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   448                       g_date_debug_print(d);
       
   449                     }
       
   450 
       
   451                   TEST("Forward years then backward years returns us to current year",
       
   452                        g_date_get_year(d) == y);
       
   453 
       
   454                   if (failed) 
       
   455                     {
       
   456                       g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   457                       g_date_debug_print(d);
       
   458                     }
       
   459 
       
   460                   if (m != 2 && day != 29) 
       
   461                     {
       
   462                       TEST("Forward years then backward years returns us to current day",
       
   463                            g_date_get_day(d) == day);
       
   464 		      
       
   465                       if (failed) 
       
   466                         {
       
   467                           g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
       
   468                           g_date_debug_print(d);
       
   469                         }
       
   470                     }
       
   471                   else 
       
   472                     {
       
   473                       g_date_set_day(d, day); /* reset */
       
   474                     }
       
   475 
       
   476                   i += 10;
       
   477                 }
       
   478 
       
   479 	      /*****  increment test relative to our local Julian count */
       
   480 
       
   481               if (!discontinuity) {
       
   482 
       
   483                 /* We can only run sequence tests between sequential years */
       
   484                 
       
   485                 TEST("Julians are sequential with increment 1",
       
   486                      j+1 == g_date_get_julian(d));
       
   487                 if (failed) 
       
   488                   {
       
   489                     g_print("Out of sequence, prev: %u expected: %u got: %u\n",
       
   490                             j, j+1, g_date_get_julian(d));
       
   491                   }
       
   492 
       
   493                 g_date_add_days(d,1);
       
   494                 TEST("Next day has julian 1 higher",
       
   495                      g_date_get_julian(d) == j + 2);
       
   496                 g_date_subtract_days(d, 1);
       
   497                 
       
   498                 if (j != G_DATE_BAD_JULIAN) 
       
   499                   {
       
   500                     g_date_subtract_days(d, 1);
       
   501                     
       
   502                     TEST("Previous day has julian 1 lower",
       
   503                          g_date_get_julian(d) == j);
       
   504                     
       
   505                     g_date_add_days(d, 1); /* back to original */
       
   506                   }
       
   507               }    
       
   508               discontinuity = FALSE; /* goes away now */            
       
   509 
       
   510               fflush(stdout);
       
   511               fflush(stderr);
       
   512 
       
   513 	      j = g_date_get_julian(d); /* inc current julian */
       
   514 
       
   515 	      ++day;
       
   516 	    } 
       
   517 	  ++m;
       
   518 	}
       
   519       g_print(" done\n");
       
   520       ++i;
       
   521       prev_y = y;
       
   522       y = check_years[i];
       
   523       if (prev_y == G_DATE_BAD_YEAR || 
       
   524           (prev_y + 1) != y) discontinuity = TRUE;
       
   525     }
       
   526   
       
   527   
       
   528   g_print("\n%u tests passed, %u failed\n",passed, notpassed);
       
   529 #ifdef __SYMBIAN32__
       
   530   testResultXml("testgdate");
       
   531 #endif /* EMULATOR */
       
   532   return 0;
       
   533 }
       
   534 
       
   535