1 #undef G_DISABLE_ASSERT
4 /* We are testing some deprecated APIs here */
5 #define GLIB_DISABLE_DEPRECATION_WARNINGS
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));
35 test_empty_constructor (void)
40 g_assert (!g_date_valid (d));
45 test_dmy_constructor (void)
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);
61 test_julian_constructor (void)
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);
83 g_date_set_time (d, time (NULL));
84 g_assert (g_date_valid (d));
87 g_date_set_time (d, 1);
88 g_assert (g_date_valid (d));
92 g_date_set_time_val (d, &tv);
93 g_assert (g_date_valid (d));
96 g_date_set_julian (d, 1);
97 g_assert (g_date_valid (d));
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));
124 g_date_set_dmy (d, 10, 1, 2000);
125 g_date_strftime (buf, 100, "%x", d);
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);
137 test_year (gconstpointer t)
139 GDateYear y = GPOINTER_TO_INT (t);
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;
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);
163 guint8 dim = g_date_get_days_in_month (m, y);
166 g_date_clear (days, 31);
168 g_assert (dim > 0 && dim < 32);
169 g_assert (g_date_valid_month (m));
174 g_assert (g_date_valid_dmy (day, m, y));
177 //g_assert (!g_date_valid (d));
179 g_date_set_dmy (d, day, m, y);
181 g_assert (g_date_valid (d));
183 if (m == G_DATE_JANUARY && day == 1)
184 first_day_of_year = g_date_get_julian (d);
186 g_assert (first_day_of_year != G_DATE_BAD_JULIAN);
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);
192 g_assert (g_date_get_julian (d) + 1 - first_day_of_year ==
193 g_date_get_day_of_year (d));
195 if (m == G_DATE_DECEMBER && day == 31)
196 g_assert_cmpint (g_date_get_day_of_year (d), ==, days_in_year);
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);
202 if (g_date_get_weekday(d) == G_DATE_MONDAY)
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);
209 g_assert_cmpint (g_date_get_iso8601_week_of_year (d) - iso8601_week_of_year, ==, 1);
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);
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);
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);
226 g_assert_cmpint (g_date_get_sunday_week_of_year (d) - sunday_week_of_year, ==, 0);
228 sunday_week_of_year = g_date_get_sunday_week_of_year (d);
230 g_assert_cmpint (g_date_compare (d, d), ==, 0);
233 while (i < 402) /* Need to get 400 year increments in */
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);
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);
251 g_assert_cmpint (g_date_get_day (d), ==, day);
253 g_date_set_day (d, day);
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);
262 if (m != 2 && day != 29)
263 g_assert_cmpint (g_date_get_day (d), ==, day);
265 g_date_set_day (d, day); /* reset */
270 j = g_date_get_julian (d);
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));
281 g_date_add_days (&tmp, 1);
282 g_assert_cmpint (j + 2, ==, g_date_get_julian (&tmp));
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);
295 g_date_clamp (&o, NULL, NULL);
296 g_assert (g_date_compare (&o, &d) == 0);
298 g_date_clamp (&o, &d1, &d2);
299 g_assert (g_date_compare (&o, &d1) == 0);
301 g_date_set_dmy (&o, 1, 1, 2000);
303 g_date_clamp (&o, &d1, &d2);
304 g_assert (g_date_compare (&o, &d2) == 0);
312 g_date_set_dmy (&d1, 1, 1, 1970);
313 g_date_set_dmy (&d2, 1, 1, 1980);
315 g_assert (g_date_compare (&d1, &d2) == -1);
316 g_date_order (&d2, &d1);
317 g_assert (g_date_compare (&d1, &d2) == 1);
321 main (int argc, char** argv)
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
342 g_setenv ("LANG", "en_US.utf-8", TRUE);
343 setlocale (LC_ALL, "");
345 g_test_init (&argc, &argv, NULL);
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++)
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);
362 return g_test_run ();