1 #undef G_DISABLE_ASSERT
4 /* We are testing some deprecated APIs here */
5 #define GLIB_DISABLE_DEPRECATION_WARNINGS
16 #define WIN32_LEAN_AND_MEAN
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));
40 test_empty_constructor (void)
45 g_assert (!g_date_valid (d
));
50 test_dmy_constructor (void)
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);
66 test_julian_constructor (void)
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);
90 g_date_set_time (d
, time (NULL
));
91 g_assert (g_date_valid (d
));
94 g_date_set_time (d
, 1);
95 g_assert (g_date_valid (d
));
99 g_date_set_time_val (d
, &tv
);
100 g_assert (g_date_valid (d
));
103 g_date_set_julian (d
, 1);
104 g_assert (g_date_valid (d
));
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
));
131 g_date_set_dmy (d
, 10, 1, 2000);
132 g_date_strftime (buf
, 100, "%x", d
);
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);
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);
146 g_date_set_parse (d
, "2001 10");
147 g_assert (!g_date_valid (d
));
149 g_date_set_parse (d
, "2001 10 1 1");
150 g_assert (!g_date_valid (d
));
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);
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);
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);
170 g_date_set_parse (d
, "20011320");
171 g_assert (!g_date_valid (d
));
177 test_year (gconstpointer t
)
179 GDateYear y
= GPOINTER_TO_INT (t
);
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;
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);
203 guint8 dim
= g_date_get_days_in_month (m
, y
);
206 g_date_clear (days
, 31);
208 g_assert (dim
> 0 && dim
< 32);
209 g_assert (g_date_valid_month (m
));
214 g_assert (g_date_valid_dmy (day
, m
, y
));
217 //g_assert (!g_date_valid (d));
219 g_date_set_dmy (d
, day
, m
, y
);
221 g_assert (g_date_valid (d
));
223 if (m
== G_DATE_JANUARY
&& day
== 1)
224 first_day_of_year
= g_date_get_julian (d
);
226 g_assert (first_day_of_year
!= G_DATE_BAD_JULIAN
);
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
);
232 g_assert (g_date_get_julian (d
) + 1 - first_day_of_year
==
233 g_date_get_day_of_year (d
));
235 if (m
== G_DATE_DECEMBER
&& day
== 31)
236 g_assert_cmpint (g_date_get_day_of_year (d
), ==, days_in_year
);
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
);
242 if (g_date_get_weekday(d
) == G_DATE_MONDAY
)
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);
249 g_assert_cmpint (g_date_get_iso8601_week_of_year (d
) - iso8601_week_of_year
, ==, 1);
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);
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
);
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);
266 g_assert_cmpint (g_date_get_sunday_week_of_year (d
) - sunday_week_of_year
, ==, 0);
268 sunday_week_of_year
= g_date_get_sunday_week_of_year (d
);
270 g_assert_cmpint (g_date_compare (d
, d
), ==, 0);
273 while (i
< 402) /* Need to get 400 year increments in */
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
);
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
);
291 g_assert_cmpint (g_date_get_day (d
), ==, day
);
293 g_date_set_day (d
, day
);
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
);
302 if (m
!= 2 && day
!= 29)
303 g_assert_cmpint (g_date_get_day (d
), ==, day
);
305 g_date_set_day (d
, day
); /* reset */
310 j
= g_date_get_julian (d
);
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
));
321 g_date_add_days (&tmp
, 1);
322 g_assert_cmpint (j
+ 2, ==, g_date_get_julian (&tmp
));
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);
335 g_date_clamp (&o
, NULL
, NULL
);
336 g_assert (g_date_compare (&o
, &d
) == 0);
338 g_date_clamp (&o
, &d1
, &d2
);
339 g_assert (g_date_compare (&o
, &d1
) == 0);
341 g_date_set_dmy (&o
, 1, 1, 2000);
343 g_date_clamp (&o
, &d1
, &d2
);
344 g_assert (g_date_compare (&o
, &d2
) == 0);
352 g_date_set_dmy (&d1
, 1, 1, 1970);
353 g_date_set_dmy (&d2
, 1, 1, 1980);
355 g_assert (g_date_compare (&d1
, &d2
) == -1);
356 g_date_order (&d2
, &d1
);
357 g_assert (g_date_compare (&d1
, &d2
) == 1);
367 g_assert_false (g_date_valid (d
));
370 g_assert_nonnull (c
);
371 g_assert_false (g_date_valid (c
));
374 g_date_set_day (d
, 10);
377 g_date_set_month (c
, 1);
378 g_date_set_year (c
, 2015);
379 g_assert_true (g_date_valid (c
));
380 g_assert_cmpuint (g_date_get_day (c
), ==, 10);
387 main (int argc
, char** argv
)
392 /* Try to get all the leap year cases. */
393 int check_years
[] = {
394 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
395 11, 12, 13, 14, 98, 99, 100, 101, 102, 103, 397,
396 398, 399, 400, 401, 402, 403, 404, 405, 406,
397 1598, 1599, 1600, 1601, 1602, 1650, 1651,
398 1897, 1898, 1899, 1900, 1901, 1902, 1903,
399 1961, 1962, 1963, 1964, 1965, 1967,
400 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976,
401 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985,
402 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
403 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
404 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012,
405 3000, 3001, 3002, 3998, 3999, 4000, 4001, 4002, 4003
408 g_setenv ("LC_ALL", "en_US.utf-8", TRUE
);
409 setlocale (LC_ALL
, "");
411 SetThreadLocale (MAKELCID (MAKELANGID (LANG_ENGLISH
, SUBLANG_ENGLISH_US
), SORT_DEFAULT
));
414 g_test_init (&argc
, &argv
, NULL
);
416 g_test_add_func ("/date/basic", test_basic
);
417 g_test_add_func ("/date/empty", test_empty_constructor
);
418 g_test_add_func ("/date/dmy", test_dmy_constructor
);
419 g_test_add_func ("/date/julian", test_julian_constructor
);
420 g_test_add_func ("/date/dates", test_dates
);
421 g_test_add_func ("/date/parse", test_parse
);
422 g_test_add_func ("/date/clamp", test_clamp
);
423 g_test_add_func ("/date/order", test_order
);
424 for (i
= 0; i
< G_N_ELEMENTS (check_years
); i
++)
426 path
= g_strdup_printf ("/date/year/%d", check_years
[i
]);
427 g_test_add_data_func (path
, GINT_TO_POINTER(check_years
[i
]), test_year
);
430 g_test_add_func ("/date/copy", test_copy
);
432 return g_test_run ();