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