Simplify subprocesses in tests
[platform/upstream/glib.git] / glib / tests / date.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 /* We are testing some deprecated APIs here */
5 #define GLIB_DISABLE_DEPRECATION_WARNINGS
6
7 #include "glib.h"
8
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <locale.h>
13 #include <time.h>
14
15 static void
16 test_basic (void)
17 {
18   g_assert_cmpint (sizeof (GDate), <,  9);
19   g_assert (!g_date_valid_month (G_DATE_BAD_MONTH));
20   g_assert (!g_date_valid_month (13));
21   g_assert (!g_date_valid_day (G_DATE_BAD_DAY));
22   g_assert (!g_date_valid_day (32));
23   g_assert (!g_date_valid_year (G_DATE_BAD_YEAR));
24   g_assert (!g_date_valid_julian (G_DATE_BAD_JULIAN));
25   g_assert (!g_date_valid_weekday (G_DATE_BAD_WEEKDAY));
26   g_assert (g_date_is_leap_year (2000));
27   g_assert (!g_date_is_leap_year (1999));
28   g_assert (g_date_is_leap_year (1996));
29   g_assert (g_date_is_leap_year (1600));
30   g_assert (!g_date_is_leap_year (2100));
31   g_assert (!g_date_is_leap_year (1800));
32 }
33
34 static void
35 test_empty_constructor (void)
36 {
37   GDate *d;
38
39   d = g_date_new ();
40   g_assert (!g_date_valid (d));
41   g_date_free (d);
42 }
43
44 static void
45 test_dmy_constructor (void)
46 {
47   GDate *d;
48   guint32 j;
49
50   d = g_date_new_dmy (1, 1, 1);
51   g_assert (g_date_valid (d));
52   j = g_date_get_julian (d);
53   g_assert_cmpint (j, ==, 1);
54   g_assert_cmpint (g_date_get_month (d), ==, G_DATE_JANUARY);
55   g_assert_cmpint (g_date_get_day (d), ==, 1);
56   g_assert_cmpint (g_date_get_year (d), ==, 1);
57   g_date_free (d);
58 }
59
60 static void
61 test_julian_constructor (void)
62 {
63   GDate *d1;
64   GDate *d2;
65
66   d1 = g_date_new_julian (4000);
67   d2 = g_date_new_julian (5000);
68   g_assert_cmpint (g_date_get_julian (d1), ==, 4000);
69   g_assert_cmpint (g_date_days_between (d1, d2), ==, 1000);
70   g_date_free (d1);
71   g_date_free (d2);
72 }
73
74 static void
75 test_dates (void)
76 {
77   GDate *d;
78   GTimeVal tv;
79
80   d = g_date_new ();
81
82   /* today */
83   g_date_set_time (d, time (NULL));
84   g_assert (g_date_valid (d));
85
86   /* Unix epoch */
87   g_date_set_time (d, 1);
88   g_assert (g_date_valid (d));
89
90   tv.tv_sec = 0;
91   tv.tv_usec = 0;
92   g_date_set_time_val (d, &tv);
93   g_assert (g_date_valid (d));
94
95   /* Julian day 1 */
96   g_date_set_julian (d, 1);
97   g_assert (g_date_valid (d));
98
99   g_date_set_year (d, 3);
100   g_date_set_day (d, 3);
101   g_date_set_month (d, 3);
102   g_assert (g_date_valid (d));
103   g_assert_cmpint (g_date_get_year (d), ==, 3);
104   g_assert_cmpint (g_date_get_month (d), ==, 3);
105   g_assert_cmpint (g_date_get_day (d), ==, 3);
106   g_assert (!g_date_is_first_of_month (d));
107   g_assert (!g_date_is_last_of_month (d));
108   g_date_set_day (d, 1);
109   g_assert (g_date_is_first_of_month (d));
110   g_date_subtract_days (d, 1);
111   g_assert (g_date_is_last_of_month (d));
112
113   g_date_free (d);
114 }
115
116 static void
117 test_parse (void)
118 {
119   GDate *d;
120   gchar buf[101];
121
122   d = g_date_new ();
123
124   g_date_set_dmy (d, 10, 1, 2000);
125   g_date_strftime (buf, 100, "%x", d);
126
127   g_date_set_parse (d, buf);
128   g_assert (g_date_valid (d));
129   g_assert_cmpint (g_date_get_month (d), ==, 1);
130   g_assert_cmpint (g_date_get_day (d), ==, 10);
131   g_assert_cmpint (g_date_get_year (d), ==, 2000);
132
133   g_date_free (d);
134 }
135
136 static void
137 test_year (gconstpointer t)
138 {
139   GDateYear y = GPOINTER_TO_INT (t);
140   GDateMonth m;
141   GDateDay day;
142   guint32 j;
143   GDate *d;
144   gint i;
145   GDate tmp;
146
147   guint32 first_day_of_year = G_DATE_BAD_JULIAN;
148   guint16 days_in_year = g_date_is_leap_year (y) ? 366 : 365;
149   guint   sunday_week_of_year = 0;
150   guint   sunday_weeks_in_year = g_date_get_sunday_weeks_in_year (y);
151   guint   monday_week_of_year = 0;
152   guint   monday_weeks_in_year = g_date_get_monday_weeks_in_year (y);
153   guint   iso8601_week_of_year = 0;
154
155   g_assert (g_date_valid_year (y));
156   /* Years ought to have roundabout 52 weeks */
157   g_assert (sunday_weeks_in_year == 52 || sunday_weeks_in_year == 53);
158   g_assert (monday_weeks_in_year == 52 || monday_weeks_in_year == 53);
159
160   m = 1;
161   while (m < 13)
162     {
163       guint8 dim = g_date_get_days_in_month (m, y);
164       GDate days[31];
165
166       g_date_clear (days, 31);
167
168       g_assert (dim > 0 && dim < 32);
169       g_assert (g_date_valid_month (m));
170
171       day = 1;
172       while (day <= dim)
173         {
174           g_assert (g_date_valid_dmy (day, m, y));
175
176           d = &days[day - 1];
177           //g_assert (!g_date_valid (d));
178
179           g_date_set_dmy (d, day, m, y);
180
181           g_assert (g_date_valid (d));
182
183           if (m == G_DATE_JANUARY && day == 1)
184             first_day_of_year = g_date_get_julian (d);
185
186           g_assert (first_day_of_year != G_DATE_BAD_JULIAN);
187
188           g_assert_cmpint (g_date_get_month (d), ==, m);
189           g_assert_cmpint (g_date_get_year (d), ==, y);
190           g_assert_cmpint (g_date_get_day (d), ==, day);
191
192           g_assert (g_date_get_julian (d) + 1 - first_day_of_year ==
193                     g_date_get_day_of_year (d));
194
195           if (m == G_DATE_DECEMBER && day == 31)
196             g_assert_cmpint (g_date_get_day_of_year (d), ==, days_in_year);
197
198           g_assert_cmpint (g_date_get_day_of_year (d), <=, days_in_year);
199           g_assert_cmpint (g_date_get_monday_week_of_year (d), <=, monday_weeks_in_year);
200           g_assert_cmpint (g_date_get_monday_week_of_year (d), >=, monday_week_of_year);
201
202           if (g_date_get_weekday(d) == G_DATE_MONDAY)
203             {
204               g_assert_cmpint (g_date_get_monday_week_of_year (d) - monday_week_of_year, ==, 1);
205               if ((m == G_DATE_JANUARY && day <= 4) ||
206                   (m == G_DATE_DECEMBER && day >= 29))
207                  g_assert_cmpint (g_date_get_iso8601_week_of_year (d), ==, 1);
208               else
209                 g_assert_cmpint (g_date_get_iso8601_week_of_year (d) - iso8601_week_of_year, ==, 1);
210             }
211           else
212             {
213               g_assert_cmpint (g_date_get_monday_week_of_year(d) - monday_week_of_year, ==, 0);
214               if (!(day == 1 && m == G_DATE_JANUARY))
215                 g_assert_cmpint (g_date_get_iso8601_week_of_year(d) - iso8601_week_of_year, ==, 0);
216             }
217
218           monday_week_of_year = g_date_get_monday_week_of_year (d);
219           iso8601_week_of_year = g_date_get_iso8601_week_of_year (d);
220
221           g_assert_cmpint (g_date_get_sunday_week_of_year (d), <=, sunday_weeks_in_year);
222           g_assert_cmpint (g_date_get_sunday_week_of_year (d), >=, sunday_week_of_year);
223           if (g_date_get_weekday(d) == G_DATE_SUNDAY)
224             g_assert_cmpint (g_date_get_sunday_week_of_year (d) - sunday_week_of_year, ==, 1);
225           else
226             g_assert_cmpint (g_date_get_sunday_week_of_year (d) - sunday_week_of_year, ==, 0);
227
228           sunday_week_of_year = g_date_get_sunday_week_of_year (d);
229
230           g_assert_cmpint (g_date_compare (d, d), ==, 0);
231
232           i = 1;
233           while (i < 402) /* Need to get 400 year increments in */
234             {
235               tmp = *d;
236               g_date_add_days (d, i);
237               g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
238               g_date_subtract_days (d, i);
239               g_assert_cmpint (g_date_get_day (d), ==, day);
240               g_assert_cmpint (g_date_get_month (d), ==, m);
241               g_assert_cmpint (g_date_get_year (d), ==, y);
242
243               tmp = *d;
244               g_date_add_months (d, i);
245               g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
246               g_date_subtract_months (d, i);
247               g_assert_cmpint (g_date_get_month (d), ==, m);
248               g_assert_cmpint (g_date_get_year (d), ==, y);
249
250               if (day < 29)
251                 g_assert_cmpint (g_date_get_day (d), ==, day);
252               else
253                 g_date_set_day (d, day);
254
255               tmp = *d;
256               g_date_add_years (d, i);
257               g_assert_cmpint (g_date_compare (d, &tmp), >, 0);
258               g_date_subtract_years (d, i);
259               g_assert_cmpint (g_date_get_month (d), ==, m);
260               g_assert_cmpint (g_date_get_year (d), ==, y);
261
262               if (m != 2 && day != 29)
263                 g_assert_cmpint (g_date_get_day (d), ==, day);
264               else
265                 g_date_set_day (d, day); /* reset */
266
267               i += 10;
268             }
269
270           j = g_date_get_julian (d);
271
272           ++day;
273         }
274       ++m;
275    }
276
277   /* at this point, d is the last day of year y */
278   g_date_set_dmy (&tmp, 1, 1, y + 1);
279   g_assert_cmpint (j + 1, ==, g_date_get_julian (&tmp));
280
281   g_date_add_days (&tmp, 1);
282   g_assert_cmpint (j + 2, ==, g_date_get_julian (&tmp));
283 }
284
285 static void
286 test_clamp (void)
287 {
288   GDate d1, d2, d, o;
289
290   g_date_set_dmy (&d1, 1, 1, 1970);
291   g_date_set_dmy (&d2, 1, 1, 1980);
292   g_date_set_dmy (&d, 1, 1, 1);
293
294   o = d;
295   g_date_clamp (&o, NULL, NULL);
296   g_assert (g_date_compare (&o, &d) == 0);
297
298   g_date_clamp (&o,  &d1, &d2);
299   g_assert (g_date_compare (&o, &d1) == 0);
300
301   g_date_set_dmy (&o, 1, 1, 2000);
302
303   g_date_clamp (&o,  &d1, &d2);
304   g_assert (g_date_compare (&o, &d2) == 0);
305 }
306
307 static void
308 test_order (void)
309 {
310   GDate d1, d2;
311
312   g_date_set_dmy (&d1, 1, 1, 1970);
313   g_date_set_dmy (&d2, 1, 1, 1980);
314
315   g_assert (g_date_compare (&d1, &d2) == -1);
316   g_date_order (&d2, &d1);
317   g_assert (g_date_compare (&d1, &d2) == 1);
318 }
319
320 int
321 main (int argc, char** argv)
322 {
323   gchar *path;
324   gint i;
325
326   /* Try to get all the leap year cases. */
327   int check_years[] = {
328     1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
329     11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
330     398, 399, 400, 401, 402, 403, 404, 405, 406,
331     1598, 1599, 1600, 1601, 1602, 1650, 1651,
332     1897, 1898, 1899, 1900, 1901, 1902, 1903,
333     1961, 1962, 1963, 1964, 1965, 1967,
334     1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
335     1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
336     1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
337     1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
338     2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
339     3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
340   };
341
342   g_setenv ("LANG", "en_US.utf-8", TRUE);
343   setlocale (LC_ALL, "");
344
345   g_test_init (&argc, &argv, NULL);
346
347   g_test_add_func ("/date/basic", test_basic);
348   g_test_add_func ("/date/empty", test_empty_constructor);
349   g_test_add_func ("/date/dmy", test_dmy_constructor);
350   g_test_add_func ("/date/julian", test_julian_constructor);
351   g_test_add_func ("/date/dates", test_dates);
352   g_test_add_func ("/date/parse", test_parse);
353   g_test_add_func ("/date/clamp", test_clamp);
354   g_test_add_func ("/date/order", test_order);
355   for (i = 0; i < G_N_ELEMENTS (check_years); i++)
356     {
357       path = g_strdup_printf ("/date/year/%d", check_years[i]);
358       g_test_add_data_func (path, GINT_TO_POINTER(check_years[i]), test_year);
359       g_free (path);
360     }
361
362   return g_test_run ();
363 }
364
365