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