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