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