source: trunk/third/glib2/tests/testgdate.c @ 18159

Revision 18159, 15.0 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r18158, which included commits to RCS files with non-trunk default branches.
Line 
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
170      if (discontinuity)
171        g_print(" (Break in sequence of requested years to check)\n");
172
173      g_print("Checking year %u", y);
174
175      TEST("Year is valid", g_date_valid_year(y));
176
177      TEST("Number of Sunday weeks in year is 52 or 53",
178           sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
179     
180      TEST("Number of Monday weeks in year is 52 or 53",
181           monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
182           
183      m = 1;
184      while (m < 13)
185        {
186          guint8 dim = g_date_get_days_in_month(m,y);
187          GDate days[31];         /* This is the fast way, no allocation */
188
189          TEST("Sensible number of days in month", (dim > 0 && dim < 32));
190
191          TEST("Month between 1 and 12 is valid", g_date_valid_month(m));
192
193          day = 1;
194
195          g_date_clear(days, 31);
196
197          while (day <= dim)
198            {
199              guint i;
200              GDate tmp;
201
202              TEST("DMY triplet is valid", g_date_valid_dmy(day,m,y));
203
204              /* Create GDate with triplet */
205             
206              d = &days[day-1];
207
208              TEST("Cleared day is invalid", !g_date_valid(d));
209
210              g_date_set_dmy(d,day,m,y);
211
212              TEST("Set day is valid", g_date_valid(d));
213
214              if (m == G_DATE_JANUARY && day == 1)
215                {
216                  first_day_of_year = g_date_get_julian(d);
217                }
218
219              g_assert(first_day_of_year != G_DATE_BAD_JULIAN);
220
221              TEST("Date with DMY triplet is valid", g_date_valid(d));
222              TEST("Month accessor works", g_date_get_month(d) == m);
223              TEST("Year accessor works", g_date_get_year(d) == y);
224              TEST("Day of month accessor works", g_date_get_day(d) == day);
225
226              TEST("Day of year is consistent with Julian dates",
227                   ((g_date_get_julian(d) + 1 - first_day_of_year) ==
228                    (g_date_get_day_of_year(d))));
229
230              if (failed)
231                {
232                  g_print("first day: %u this day: %u day of year: %u\n",
233                          first_day_of_year,
234                          g_date_get_julian(d),
235                          g_date_get_day_of_year(d));
236                }
237             
238              if (m == G_DATE_DECEMBER && day == 31)
239                {
240                  TEST("Last day of year equals number of days in year",
241                       g_date_get_day_of_year(d) == days_in_year);
242                  if (failed)
243                    {
244                      g_print("last day: %u days in year: %u\n",
245                              g_date_get_day_of_year(d), days_in_year);
246                    }
247                }
248
249              TEST("Day of year is not more than number of days in the year",
250                   g_date_get_day_of_year(d) <= days_in_year);
251
252              TEST("Monday week of year is not more than number of weeks in the year",
253                   g_date_get_monday_week_of_year(d) <= monday_weeks_in_year);
254              if (failed)
255                {
256                  g_print("Weeks in year: %u\n", monday_weeks_in_year);
257                  g_date_debug_print(d);
258                }
259              TEST("Monday week of year is >= than last week of year",
260                   g_date_get_monday_week_of_year(d) >= monday_week_of_year);
261
262              if (g_date_get_weekday(d) == G_DATE_MONDAY)
263                {
264                 
265                  TEST("Monday week of year on Monday 1 more than previous day's week of year",
266                       (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 1);
267                }
268              else
269                {
270                  TEST("Monday week of year on non-Monday 0 more than previous day's week of year",
271                       (g_date_get_monday_week_of_year(d) - monday_week_of_year) == 0);
272                }
273
274
275              monday_week_of_year = g_date_get_monday_week_of_year(d);
276
277
278              TEST("Sunday week of year is not more than number of weeks in the year",
279                   g_date_get_sunday_week_of_year(d) <= sunday_weeks_in_year);
280              if (failed)
281                {
282                  g_date_debug_print(d);
283                }
284              TEST("Sunday week of year is >= than last week of year",
285                   g_date_get_sunday_week_of_year(d) >= sunday_week_of_year);
286
287              if (g_date_get_weekday(d) == G_DATE_SUNDAY)
288                {
289                  TEST("Sunday week of year on Sunday 1 more than previous day's week of year",
290                       (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 1);
291                }
292              else
293                {
294                  TEST("Sunday week of year on non-Sunday 0 more than previous day's week of year",
295                       (g_date_get_sunday_week_of_year(d) - sunday_week_of_year) == 0);
296                }
297
298              sunday_week_of_year = g_date_get_sunday_week_of_year(d);
299
300              TEST("Date is equal to itself",
301                   g_date_compare(d,d) == 0);
302
303
304              /*************** Increments ***********/
305
306              i = 1;
307              while (i < 402) /* Need to get 400 year increments in */
308                {
309             
310                  /***** Days ******/
311                  tmp = *d;
312                  g_date_add_days(d, i);
313
314                  TEST("Adding days gives a value greater than previous",
315                       g_date_compare(d, &tmp) > 0);
316
317                  g_date_subtract_days(d, i);
318                  TEST("Forward days then backward days returns us to current day",
319                       g_date_get_day(d) == day);
320
321                  if (failed)
322                    {
323                      g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
324                      g_date_debug_print(d);
325                    }
326
327                  TEST("Forward days then backward days returns us to current month",
328                       g_date_get_month(d) == m);
329
330                  if (failed)
331                    {
332                      g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
333                      g_date_debug_print(d);
334                    }
335
336                  TEST("Forward days then backward days returns us to current year",
337                       g_date_get_year(d) == y);
338
339                  if (failed)
340                    {
341                      g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
342                      g_date_debug_print(d);
343                    }
344
345                  /******* Months ********/
346
347                  tmp = *d;
348                  g_date_add_months(d, i);
349                  TEST("Adding months gives a larger value",
350                       g_date_compare(d, &tmp) > 0);
351                  g_date_subtract_months(d, i);
352
353                  TEST("Forward months then backward months returns us to current month",
354                       g_date_get_month(d) == m);
355
356                  if (failed)
357                    {
358                      g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
359                      g_date_debug_print(d);
360                    }
361
362                  TEST("Forward months then backward months returns us to current year",
363                       g_date_get_year(d) == y);
364
365                  if (failed)
366                    {
367                      g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
368                      g_date_debug_print(d);
369                    }
370
371                 
372                  if (day < 29)
373                    {
374                      /* Day should be unchanged */
375                     
376                      TEST("Forward months then backward months returns us to current day",
377                           g_date_get_day(d) == day);
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                  else
386                    {
387                      /* reset the day for later tests */
388                      g_date_set_day(d, day);
389                    }
390
391                  /******* Years ********/
392
393                  tmp = *d;
394                  g_date_add_years(d, i);
395
396                  TEST("Adding years gives a larger value",
397                       g_date_compare(d,&tmp) > 0);
398                     
399                  g_date_subtract_years(d, i);
400
401                  TEST("Forward years then backward years returns us to current month",
402                       g_date_get_month(d) == m);
403
404                  if (failed)
405                    {
406                      g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
407                      g_date_debug_print(d);
408                    }
409
410                  TEST("Forward years then backward years returns us to current year",
411                       g_date_get_year(d) == y);
412
413                  if (failed)
414                    {
415                      g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
416                      g_date_debug_print(d);
417                    }
418
419                  if (m != 2 && day != 29)
420                    {
421                      TEST("Forward years then backward years returns us to current day",
422                           g_date_get_day(d) == day);
423                     
424                      if (failed)
425                        {
426                          g_print("  (increment %u, dmy %u %u %u) ", i, day, m, y);
427                          g_date_debug_print(d);
428                        }
429                    }
430                  else
431                    {
432                      g_date_set_day(d, day); /* reset */
433                    }
434
435                  i += 10;
436                }
437
438              /*****  increment test relative to our local Julian count */
439
440              if (!discontinuity) {
441
442                /* We can only run sequence tests between sequential years */
443               
444                TEST("Julians are sequential with increment 1",
445                     j+1 == g_date_get_julian(d));
446                if (failed)
447                  {
448                    g_print("Out of sequence, prev: %u expected: %u got: %u\n",
449                            j, j+1, g_date_get_julian(d));
450                  }
451
452                g_date_add_days(d,1);
453                TEST("Next day has julian 1 higher",
454                     g_date_get_julian(d) == j + 2);
455                g_date_subtract_days(d, 1);
456               
457                if (j != G_DATE_BAD_JULIAN)
458                  {
459                    g_date_subtract_days(d, 1);
460                   
461                    TEST("Previous day has julian 1 lower",
462                         g_date_get_julian(d) == j);
463                   
464                    g_date_add_days(d, 1); /* back to original */
465                  }
466              }   
467              discontinuity = FALSE; /* goes away now */           
468
469              fflush(stdout);
470              fflush(stderr);
471
472              j = g_date_get_julian(d); /* inc current julian */
473
474              ++day;
475            }
476          ++m;
477        }
478      g_print(" done\n");
479      ++i;
480      prev_y = y;
481      y = check_years[i];
482      if (prev_y == G_DATE_BAD_YEAR ||
483          (prev_y + 1) != y) discontinuity = TRUE;
484    }
485 
486 
487  g_print("\n%u tests passed, %u failed\n",passed, notpassed);
488
489  return 0;
490}
491
492
Note: See TracBrowser for help on using the repository browser.