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