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