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