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